diff --git a/rma/models/rma.py b/rma/models/rma.py index 90112535a..697e1bca7 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -774,6 +774,7 @@ def _prepare_reception_procurement_vals(self, group=None): vals = self._prepare_common_procurement_vals(group=group) vals["route_ids"] = self.warehouse_id.rma_in_route_id vals["rma_receiver_ids"] = [(6, 0, self.ids)] + vals["to_refund"] = self.operation_id.action_create_refund == "update_quantity" if self.move_id: vals["origin_returned_move_id"] = self.move_id.id return vals diff --git a/rma/models/rma_operation.py b/rma/models/rma_operation.py index 39f25d9ae..32ddef1fd 100644 --- a/rma/models/rma_operation.py +++ b/rma/models/rma_operation.py @@ -36,6 +36,7 @@ class RmaOperation(models.Model): ("automatic_on_confirm", "Automatically on Confirm"), ("manual_after_receipt", "Manually After Receipt"), ("automatic_after_receipt", "Automatically After Receipt"), + ("update_quantity", "Update Quantities"), ], string="Refund Action", default="manual_after_receipt", diff --git a/rma/tests/test_rma_operation.py b/rma/tests/test_rma_operation.py index c90f60381..49dc92b45 100644 --- a/rma/tests/test_rma_operation.py +++ b/rma/tests/test_rma_operation.py @@ -233,3 +233,51 @@ def test_13(self): rma.reception_move_id.picking_id._action_done() self.assertEqual(rma.state, "received") self.assertFalse(rma.delivery_move_ids) + + def test_14(self): + """if the refund action is not ment to update quantity, return picking line + to_refund field should be False""" + self.operation.action_create_refund = "manual_after_receipt" + origin_delivery = self._create_delivery() + stock_return_picking_form = Form( + self.env["stock.return.picking"].with_context( + active_ids=origin_delivery.ids, + active_id=origin_delivery.id, + active_model="stock.picking", + ) + ) + stock_return_picking_form.create_rma = True + stock_return_picking_form.rma_operation_id = self.operation + return_wizard = stock_return_picking_form.save() + return_line = return_wizard.product_return_moves.filtered( + lambda m, p=self.product: m.product_id == p + ) + self.assertEqual(return_line.rma_operation_id, self.operation) + picking_action = return_wizard.create_returns() + reception = self.env["stock.picking"].browse(picking_action["res_id"]) + move = reception.move_ids.filtered(lambda m, p=self.product: m.product_id == p) + self.assertFalse(move.to_refund) + + def test_15(self): + """if the refund action is ment to update quantity, return picking line + to_refund field should be True""" + self.operation.action_create_refund = "update_quantity" + origin_delivery = self._create_delivery() + stock_return_picking_form = Form( + self.env["stock.return.picking"].with_context( + active_ids=origin_delivery.ids, + active_id=origin_delivery.id, + active_model="stock.picking", + ) + ) + stock_return_picking_form.create_rma = True + stock_return_picking_form.rma_operation_id = self.operation + return_wizard = stock_return_picking_form.save() + return_line = return_wizard.product_return_moves.filtered( + lambda m, p=self.product: m.product_id == p + ) + self.assertEqual(return_line.rma_operation_id, self.operation) + picking_action = return_wizard.create_returns() + reception = self.env["stock.picking"].browse(picking_action["res_id"]) + move = reception.move_ids.filtered(lambda m, p=self.product: m.product_id == p) + self.assertTrue(move.to_refund) diff --git a/rma_sale/models/rma.py b/rma_sale/models/rma.py index 7662ba5fa..986c02463 100644 --- a/rma_sale/models/rma.py +++ b/rma_sale/models/rma.py @@ -175,3 +175,10 @@ def _prepare_delivery_procurements(self, scheduled_date=None, qty=None, uom=None return super()._prepare_delivery_procurements( scheduled_date=scheduled_date, qty=qty, uom=uom ) + + def _prepare_reception_procurement_vals(self, group=None): + """This method is used only for reception and a specific RMA IN route.""" + vals = super()._prepare_reception_procurement_vals(group=group) + if self.move_id and self.move_id.sale_line_id: + vals["sale_line_id"] = self.move_id.sale_line_id.id + return vals diff --git a/rma_sale/tests/test_rma_sale.py b/rma_sale/tests/test_rma_sale.py index 532230d1d..a62c150b1 100644 --- a/rma_sale/tests/test_rma_sale.py +++ b/rma_sale/tests/test_rma_sale.py @@ -222,3 +222,34 @@ def test_report_rma(self): res = str(res[0]) self.assertRegex(res, self.sale_order.name) self.assertRegex(res, operation.name) + + def test_manual_refund_no_quantity_impact(self): + """If the operation is meant for a manual refund, the delivered quantity + should not be updated.""" + self.operation.action_create_refund = "manual_after_receipt" + order = self.sale_order + order_line = order.order_line + self.assertEqual(order_line.qty_delivered, 5) + wizard = self._rma_sale_wizard(order) + rma = self.env["rma"].browse(wizard.create_and_open_rma()["res_id"]) + self.assertEqual(rma.reception_move_id.sale_line_id, order_line) + rma.action_confirm() + rma.reception_move_id.quantity_done = rma.product_uom_qty + rma.reception_move_id.picking_id._action_done() + self.assertEqual(order.order_line.qty_delivered, 5) + + def test_no_manual_refund_quantity_impact(self): + """If the operation is meant for a manual refund, the delivered quantity + should not be updated.""" + self.operation.action_create_refund = "update_quantity" + order = self.sale_order + order_line = order.order_line + self.assertEqual(order_line.qty_delivered, 5) + wizard = self._rma_sale_wizard(order) + rma = self.env["rma"].browse(wizard.create_and_open_rma()["res_id"]) + self.assertEqual(rma.reception_move_id.sale_line_id, order_line) + rma.action_confirm() + self.assertFalse(rma.can_be_refunded) + rma.reception_move_id.quantity_done = rma.product_uom_qty + rma.reception_move_id.picking_id._action_done() + self.assertEqual(order.order_line.qty_delivered, 0)