Skip to content

Commit

Permalink
[IMP] rma: allow standard refund
Browse files Browse the repository at this point in the history
  • Loading branch information
sbejaoui committed Sep 3, 2024
1 parent 0880d90 commit df0e95e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions rma/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rma/models/rma_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions rma/tests/test_rma_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions rma_sale/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions rma_sale/tests/test_rma_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit df0e95e

Please sign in to comment.