From 285dd8827199f6d1a56a7013d9cbbbb343f99e5d Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Tue, 9 Apr 2024 19:08:13 +0200 Subject: [PATCH] [FIX] sale_order_lot_selection: remove deprecated method for domain _onchange_product_id_set_lot_domain() causes a warning as 'domain' is deprecated as return value, and also, doesn't work as intented, the domain is specified in the view. In short you can't customize it by overriding this method anyway. So we take the other approach, as describe here: https://github.com/odoo/odoo/pull/41918#issuecomment-824946980 Tests have been modified accordingly. --- .../models/sale_order_line.py | 25 +++++++++++++++---- .../tests/test_sale_order_lot_selection.py | 15 ++--------- sale_order_lot_selection/view/sale_view.xml | 4 +-- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/sale_order_lot_selection/models/sale_order_line.py b/sale_order_lot_selection/models/sale_order_line.py index 4012beef9463..03f20d3ecf5f 100644 --- a/sale_order_lot_selection/models/sale_order_line.py +++ b/sale_order_lot_selection/models/sale_order_line.py @@ -4,7 +4,26 @@ class SaleOrderLine(models.Model): _inherit = "sale.order.line" - lot_id = fields.Many2one("stock.production.lot", "Lot", copy=False) + lot_id = fields.Many2one( + comodel_name="stock.production.lot", + string="Lot", + domain="[('id', 'in', allowed_lot_ids)]", + copy=False, + ) + allowed_lot_ids = fields.Many2many( + comodel_name="stock.production.lot", + compute="_compute_allowed_lot_ids", + ) + + @api.depends("product_id") + def _compute_allowed_lot_ids(self): + lot_model = self.env["stock.production.lot"] + for rec in self: + rec.allowed_lot_ids = lot_model.search( + [ + ("product_id", "=", rec.product_id.id), + ] + ) def _prepare_procurement_values(self, group_id=False): vals = super()._prepare_procurement_values(group_id=group_id) @@ -17,7 +36,3 @@ def product_id_change(self): res = super().product_id_change() self.lot_id = False return res - - @api.onchange("product_id") - def _onchange_product_id_set_lot_domain(self): - return {"domain": {"lot_id": [("product_id", "=", self.product_id.id)]}} diff --git a/sale_order_lot_selection/tests/test_sale_order_lot_selection.py b/sale_order_lot_selection/tests/test_sale_order_lot_selection.py index 50dad93b74a8..eebced5fae0e 100644 --- a/sale_order_lot_selection/tests/test_sale_order_lot_selection.py +++ b/sale_order_lot_selection/tests/test_sale_order_lot_selection.py @@ -297,12 +297,7 @@ def test_sale_order_lot_selection(self): picking_move_line_ids[0].location_id = self.stock_location picking.button_validate() - onchange_res = self.sol3._onchange_product_id_set_lot_domain() - self.assertEqual( - onchange_res["domain"]["lot_id"], [("product_id", "=", self.prd_cable.id)] - ) - # put back the lot because it is removed by onchange - self.sol3.lot_id = lot10.id + self.assertEqual(self.sol3.allowed_lot_ids.product_id, self.prd_cable) # I'll try to confirm it to check lot reservation: # lot10 was delivered by order1 lot10_qty_available = self._stock_quantity( @@ -313,13 +308,7 @@ def test_sale_order_lot_selection(self): # products are not available for reservation (lot unavailable) self.assertEqual(self.order3.picking_ids[0].state, "confirmed") - # also test on_change for order2 - onchange_res = self.sol2a._onchange_product_id_set_lot_domain() - self.assertEqual( - onchange_res["domain"]["lot_id"], [("product_id", "=", self.product_46.id)] - ) - # onchange remove lot_id, we put it back - self.sol2a.lot_id = lot11.id + self.assertEqual(self.sol2a.allowed_lot_ids.product_id, self.product_46) self.order2.action_confirm() picking = self.order2.picking_ids picking.action_assign() diff --git a/sale_order_lot_selection/view/sale_view.xml b/sale_order_lot_selection/view/sale_view.xml index 09071c33bae2..8eac0e24ba38 100644 --- a/sale_order_lot_selection/view/sale_view.xml +++ b/sale_order_lot_selection/view/sale_view.xml @@ -8,9 +8,9 @@ expr="//field[@name='order_line']/tree/field[@name='product_id']" position="after" > + @@ -19,9 +19,9 @@ expr="//field[@name='order_line']/form/group/group/field[@name='product_id']" position="after" > +