diff --git a/rma_sale/models/sale.py b/rma_sale/models/sale.py index fa91e86c..82e8dce6 100644 --- a/rma_sale/models/sale.py +++ b/rma_sale/models/sale.py @@ -110,6 +110,13 @@ class SaleOrderLine(models.Model): def prepare_sale_rma_data(self): self.ensure_one() + # Method helper to filter chained moves + + def destination_moves(_move): + return _move.mapped("move_dest_ids").filtered( + lambda r: r.state in ["partially_available", "assigned", "done"] + ) + product = self.product_id if self.product_id.type not in ["product", "consu"]: return {} @@ -117,11 +124,20 @@ class SaleOrderLine(models.Model): data = [] if moves: for move in moves: + # Look for chained moves to check how many items we can allow + # to return. When a product is re-delivered it should be + # allowed to open an RMA again on it. qty = move.product_uom_qty - move_dest = move.move_dest_ids.filtered( - lambda r: r.state in ["partially_available", "assigned", "done"] - ) - qty -= sum(move_dest.mapped("product_uom_qty")) + qty_returned = 0 + move_dest = destination_moves(move) + while move_dest: + qty_returned -= sum(move_dest.mapped("product_uom_qty")) + move_dest = destination_moves(move_dest) + if move_dest: + qty += sum(move_dest.mapped("product_uom_qty")) + move_dest = destination_moves(move_dest) + # If by chance we get a negative qty we should ignore it + qty = max(0, sum((qty, qty_returned))) data.append( { "product": product, diff --git a/rma_sale/tests/test_rma_sale.py b/rma_sale/tests/test_rma_sale.py index c61ccbec..7a22c22b 100644 --- a/rma_sale/tests/test_rma_sale.py +++ b/rma_sale/tests/test_rma_sale.py @@ -35,6 +35,10 @@ class TestRmaSale(SavepointCase): cls.order_out_picking.move_lines.quantity_done = 5 cls.order_out_picking.button_validate() + def _rma_sale_wizard(self, order): + wizard_id = order.action_create_rma()["res_id"] + return self.env["sale.order.rma.wizard"].browse(wizard_id) + def test_create_rma_with_so(self): rma_form = Form(self.env["rma"]) rma_form.partner_id = self.partner @@ -78,3 +82,35 @@ class TestRmaSale(SavepointCase): rma.reception_move_id.picking_id.action_done() rma.action_refund() self.assertEqual(rma.refund_id.user_id, user) + + def test_create_recurrent_rma(self): + """An RMA of a product that had an RMA in the past should be possible""" + wizard = self._rma_sale_wizard(self.sale_order) + rma = self.env["rma"].browse(wizard.create_and_open_rma()["res_id"]) + rma.reception_move_id.quantity_done = rma.product_uom_qty + rma.reception_move_id.picking_id.action_done() + wizard = self._rma_sale_wizard(self.sale_order) + self.assertEqual( + wizard.line_ids.quantity, + 0, + "There shouldn't be any allowed quantities for RMAs", + ) + 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_lines.quantity_done = rma.product_uom_qty + picking.action_done() + # The product is returned to the customer, so we should be able to make + # another RMA in the future + wizard = self._rma_sale_wizard(self.sale_order) + self.assertEqual( + wizard.line_ids.quantity, + rma.product_uom_qty, + "We should be allowed to return the product again", + )