Skip to content

Commit

Permalink
Merge branch '14.0-remove-deprecated-onchange' of https://github.com/…
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiocorato committed Apr 18, 2024
2 parents 75f1f95 + 285dd88 commit 1cda66f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
25 changes: 20 additions & 5 deletions sale_order_lot_selection/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)]}}
15 changes: 2 additions & 13 deletions sale_order_lot_selection/tests/test_sale_order_lot_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions sale_order_lot_selection/view/sale_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
expr="//field[@name='order_line']/tree/field[@name='product_id']"
position="after"
>
<field name="allowed_lot_ids" invisible="1" />
<field
name="lot_id"
domain="[('product_id','=', product_id)]"
context="{'default_product_id': product_id, 'default_company_id': parent.company_id}"
groups="stock.group_production_lot"
/>
Expand All @@ -19,9 +19,9 @@
expr="//field[@name='order_line']/form/group/group/field[@name='product_id']"
position="after"
>
<field name="allowed_lot_ids" invisible="1" />
<field
name="lot_id"
domain="[('product_id','=', product_id)]"
context="{'default_product_id': product_id}"
groups="stock.group_production_lot"
/>
Expand Down

0 comments on commit 1cda66f

Please sign in to comment.