From a481430782068811d4bd98b72903a1b6f59f77c3 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Thu, 17 Oct 2024 10:02:22 +0200 Subject: [PATCH] [IMP] rma_lot: add onchange lot&product --- rma_lot/models/rma.py | 21 ++++++++++++++++++++- rma_lot/tests/test_rma_lot.py | 8 ++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/rma_lot/models/rma.py b/rma_lot/models/rma.py index faa7d7cb..55d18a98 100644 --- a/rma_lot/models/rma.py +++ b/rma_lot/models/rma.py @@ -8,7 +8,14 @@ class Rma(models.Model): _inherit = "rma" - lot_id = fields.Many2one(comodel_name="stock.lot", string="Lot/Serial Number") + lot_id = fields.Many2one( + comodel_name="stock.lot", + string="Lot/Serial Number", + domain="[('product_id', '=?', product_id)]", + compute="_compute_lot_id", + store=True, + readonly=False, + ) lots_visible = fields.Boolean(compute="_compute_lots_visible") @api.depends("product_id.tracking") @@ -20,3 +27,15 @@ class Rma(models.Model): vals = super()._prepare_reception_procurement_vals(group=group) vals["restrict_lot_id"] = self.lot_id.id return vals + + @api.depends("move_id", "lot_id") + def _compute_product_id(self): + res = super()._compute_product_id() + for rec in self: + if not rec.move_id and rec.lot_id: + self.product_id = rec.lot_id.product_id + return res + + @api.depends("product_id") + def _compute_lot_id(self): + self.update({"lot_id": False}) diff --git a/rma_lot/tests/test_rma_lot.py b/rma_lot/tests/test_rma_lot.py index 5cd033e8..7f16241b 100644 --- a/rma_lot/tests/test_rma_lot.py +++ b/rma_lot/tests/test_rma_lot.py @@ -93,3 +93,11 @@ class TestRMALot(TransactionCase): self.assertEqual(rma_lot_2.reception_move_id.restrict_lot_id, self.lot_2) self.assertEqual(rma_lot_2.reception_move_id.state, "assigned") self.assertEqual(rma_lot_2.reception_move_id.move_line_ids.lot_id, self.lot_2) + + def test_rma_form(self): + rma_form = Form(self.env["rma"]) + self.assertFalse(rma_form.product_id) + rma_form.lot_id = self.lot_1 + self.assertEqual(rma_form.product_id, self.product) + rma_form.product_id = self.env.ref("product.product_product_4") + self.assertFalse(rma_form.lot_id)