From df0e95e907c713521da47b5e64cb6e8d31225419 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Wed, 28 Aug 2024 14:27:16 +0200 Subject: [PATCH] [IMP] rma: allow standard refund --- rma/models/rma.py | 1 + rma/models/rma_operation.py | 1 + rma/tests/test_rma_operation.py | 48 +++++++++++++++++++++++++++++++++ rma_sale/models/rma.py | 21 +++++++++++++++ rma_sale/tests/test_rma_sale.py | 44 ++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) 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..1898e717c 100644 --- a/rma_sale/models/rma.py +++ b/rma_sale/models/rma.py @@ -175,3 +175,24 @@ 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_delivery_procurement_vals(self, scheduled_date=None): + vals = super()._prepare_delivery_procurement_vals(scheduled_date=scheduled_date) + if ( + self.move_id + and self.move_id.sale_line_id + and self.operation_id.action_create_refund == "update_quantity" + ): + vals["sale_line_id"] = self.move_id.sale_line_id.id + return vals + + 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 + and self.operation_id.action_create_refund == "update_quantity" + ): + 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..f670f2918 100644 --- a/rma_sale/tests/test_rma_sale.py +++ b/rma_sale/tests/test_rma_sale.py @@ -222,3 +222,47 @@ 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) + delivery_form = Form( + self.env["rma.delivery.wizard"].with_context( + active_ids=rma.ids, + rma_delivery_type="return", + ) + ) + delivery_form.product_uom_qty = rma.product_uom_qty + delivery_wizard = delivery_form.save() + delivery_wizard.action_deliver() + picking = rma.delivery_move_ids.picking_id + picking.move_ids.quantity_done = rma.product_uom_qty + picking._action_done() + self.assertEqual(order.order_line.qty_delivered, 5)