mirror of
https://github.com/OCA/rma.git
synced 2025-02-16 17:11:47 +02:00
[FIX] rma_sale: recurrent RMA
If a product was already in an RMA in the past, we should be able to place another RMA in the future.
This commit is contained in:
@@ -114,6 +114,12 @@ 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 {}
|
||||
@@ -121,11 +127,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': move.product_id,
|
||||
'quantity': qty,
|
||||
|
||||
@@ -38,6 +38,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
|
||||
@@ -83,3 +87,34 @@ 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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user