Skip to content

Commit

Permalink
[16.0][IMP] rma, add feature to require origin field in rma approval …
Browse files Browse the repository at this point in the history
…process
  • Loading branch information
ChrisOForgeFlow committed Sep 25, 2024
1 parent 44a3669 commit e4645bd
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rma/models/rma_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ def _default_routes(self):
required=True,
default=lambda self: self.env.user.company_id,
)
require_origin_field_filled = fields.Boolean(
default=False,
help="RMA document can't be continue in process if at least one origin field is filled",
)
12 changes: 12 additions & 0 deletions rma/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,20 @@ def _check_lot_assigned(self):
% (rec.product_id.display_name)
)

@api.model
def _origin_fields(self):
return ["reference_move_id"]

def _check_origin_fields_filled(self):
for rec in self.filtered(lambda x: x.operation_id.require_origin_field_filled):
if not any(rec[origin_field] for origin_field in self._origin_fields()):
raise UserError(
_("You should fill at least one origin field to continue")
)

def action_rma_to_approve(self):
self._check_lot_assigned()
self._check_origin_fields_filled()
self.write({"state": "to_approve"})
for rec in self:
if rec.product_id.rma_approval_policy == "one_step":
Expand Down
41 changes: 41 additions & 0 deletions rma/tests/test_rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,3 +1093,44 @@ def test_09_rma_state(self):
self.assertEqual(
rma.rma_line_ids.mapped("state"), ["approved", "approved", "approved"]
)

def test_10_rma_require_origin_fields(self):
self.rma_cust_replace_op_id.write(
{
"require_origin_field_filled": True,
}
)
rma_line = Form(
self.rma_line,
view=self.env.ref("rma.view_rma_line_form").id,
)
rma_line.partner_id = self.partner_id
rma_line.operation_id = self.rma_cust_replace_op_id
rma_line.product_id = self.product_1
rma_line = rma_line.save()
with self.assertRaises(UserError):
rma_line.action_rma_to_approve()

self._create_inventory(self.product_1, 5, self.stock_location)
picking_type = self._get_picking_type(
self.wh, self.stock_location, self.customer_location
)
picking = self._create_picking(self.partner_id, picking_type)
move_values = self._prepare_move(
self.product_1,
5,
self.stock_location,
self.customer_location,
picking,
)
move_created = self.env["stock.move"].create(move_values)
self._do_picking(picking)

rma_line = Form(
rma_line,
view=self.env.ref("rma.view_rma_line_form").id,
)
rma_line.reference_move_id = move_created
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self.assertIn(rma_line.state, ("to_approve", "approved"))
1 change: 1 addition & 0 deletions rma/views/rma_operation_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<group name="policies" string="Policies">
<field name="receipt_policy" />
<field name="delivery_policy" />
<field name="require_origin_field_filled" />
</group>
<group name="inbound" string="Inbound">
<field name="in_route_id" />
Expand Down
5 changes: 5 additions & 0 deletions rma_account/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def _compute_refund_count(self):
ondelete="restrict",
)

@api.model
def _origin_fields(self):
res = super()._origin_fields()
return res + ["account_move_line_id"]

@api.depends("partner_id")
def _compute_commercial_partner_id(self):
for rma_line in self:
Expand Down
34 changes: 34 additions & 0 deletions rma_account/tests/test_rma_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import fields
from odoo.exceptions import UserError
from odoo.fields import Date
from odoo.tests import common
from odoo.tests.common import Form
Expand Down Expand Up @@ -303,3 +304,36 @@ def test_07_add_lines_from_rma(self):
self.assertEqual(rma_1.product_id.id, bill_product.id)
self.assertEqual(bill.rma_count, 1)
self.assertEqual(bill.used_in_rma_count, 0)

def test_08_rma_require_origin_fields(self):
partner = self.inv_customer.partner_id
operation = self.env.ref("rma.rma_operation_customer_replace")
rma_line = Form(
self.rma_line_obj,
view=self.env.ref("rma_account.view_rma_line_form").id,
)
rma_line.partner_id = partner
rma_line.operation_id = operation
rma_line.product_id = self.product_1
rma_line = rma_line.save()
self.assertFalse(bool(rma_line.account_move_line_id))
rma_line.operation_id.write(
{
"require_origin_field_filled": True,
}
)
self.assertTrue(rma_line.operation_id.require_origin_field_filled)
with self.assertRaises(UserError):
rma_line.action_rma_to_approve()

rma_line = Form(
rma_line,
view=self.env.ref("rma.view_rma_line_form").id,
)

move_line = self.inv_customer.invoice_line_ids[0]

rma_line.account_move_line_id = move_line
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self.assertIn(rma_line.state, ("to_approve", "approved"))
5 changes: 5 additions & 0 deletions rma_purchase/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def _compute_qty_purchase(self):
compute="_compute_qty_purchase",
)

@api.model
def _origin_fields(self):
res = super()._origin_fields()
return res + ["purchase_order_line_id"]

@api.onchange("product_id", "partner_id")
def _onchange_product_id(self):
"""Domain for purchase_order_line_id is computed here to make
Expand Down
5 changes: 5 additions & 0 deletions rma_sale/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def _compute_sales_count(self):
)
sales_count = fields.Integer(compute="_compute_sales_count", string="# of Sales")

@api.model
def _origin_fields(self):
res = super()._origin_fields()
return res + ["sale_line_id"]

@api.onchange("product_id", "partner_id")
def _onchange_product_id(self):
"""Domain for sale_line_id is computed here to make it dynamic."""
Expand Down

0 comments on commit e4645bd

Please sign in to comment.