From e912c523edc289a1100d34ba181d0d9ab6689098 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 | 24 +++++++- rma/models/rma_operation.py | 5 ++ rma/tests/test_rma_operation.py | 54 ++++++++++++++++++ rma/views/rma_operation.xml | 9 ++- rma/views/rma_views.xml | 6 ++ rma_sale/models/rma.py | 7 +++ rma_sale/tests/test_rma_sale.py | 55 +++++++++++++++++++ rma_sale/wizard/sale_order_rma_wizard.py | 9 +++ .../wizard/sale_order_rma_wizard_views.xml | 5 ++ 9 files changed, 171 insertions(+), 3 deletions(-) diff --git a/rma/models/rma.py b/rma/models/rma.py index 90112535a..126a05515 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -329,6 +329,14 @@ def _domain_location_id(self): show_create_refund = fields.Boolean( string="Show Create refund Button", compute="_compute_show_refund_replace" ) + return_product_id = fields.Many2one( + "product.product", + help="Product to be returned if it's different from the originally delivered " + "item.", + ) + different_return_product = fields.Boolean( + related="operation_id.different_return_product" + ) @api.depends("operation_id.action_create_receipt", "state", "reception_move_id") def _compute_show_create_receipt(self): @@ -774,6 +782,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 @@ -787,13 +796,24 @@ def _prepare_reception_procurements(self): group = rma.procurement_group_id if not group: group = group_model.create(rma._prepare_procurement_group_vals()) + product = self.product_id + if self.different_return_product: + if not self.return_product_id: + raise ValidationError( + _( + "The selected operation requires a return product different" + " from the originally delivered item. Please select the " + "product to return." + ) + ) + product = self.return_product_id procurements.append( group_model.Procurement( - rma.product_id, + product, rma.product_uom_qty, rma.product_uom, rma.location_id, - rma.product_id.display_name, + product.display_name, group.name, rma.company_id, rma._prepare_reception_procurement_vals(group), diff --git a/rma/models/rma_operation.py b/rma/models/rma_operation.py index 39f25d9ae..17a05af0e 100644 --- a/rma/models/rma_operation.py +++ b/rma/models/rma_operation.py @@ -19,6 +19,10 @@ class RmaOperation(models.Model): default="automatic_on_confirm", help="Define how the receipt action should be handled.", ) + different_return_product = fields.Boolean( + help="If checked, allows the return of a product different from the one " + "originally ordered. Used if the delivery is created automatically", + ) action_create_delivery = fields.Selection( [ ("manual_on_confirm", "Manually on Confirm"), @@ -36,6 +40,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..2957ed4bd 100644 --- a/rma/tests/test_rma_operation.py +++ b/rma/tests/test_rma_operation.py @@ -149,7 +149,13 @@ def test_07(self): ValidationError, msg="Complete the replacement information" ): rma.action_confirm() + rma.return_product_id = self.product_product.create( + {"name": "return Product test 1", "type": "product"} + ) rma.action_confirm() + self.assertEqual(rma.delivery_move_ids.product_id, rma.product_id) + self.assertEqual(rma.reception_move_id.product_id, rma.return_product_id) + self.assertEqual(rma.state, "waiting_return") def test_08(self): """test refund, manually after confirm""" @@ -233,3 +239,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/views/rma_operation.xml b/rma/views/rma_operation.xml index 02de01896..581059c86 100644 --- a/rma/views/rma_operation.xml +++ b/rma/views/rma_operation.xml @@ -17,12 +17,19 @@ - + + + + diff --git a/rma/views/rma_views.xml b/rma/views/rma_views.xml index e100dcac9..b95aaaeb1 100644 --- a/rma/views/rma_views.xml +++ b/rma/views/rma_views.xml @@ -272,6 +272,11 @@ force_save="1" attrs="{'readonly': ['|', ('picking_id', '!=', False), ('state', '!=', 'draft')]}" /> +