Skip to content

Commit c8dc4cf

Browse files
[FIX] product_multi_company_stock: Adding a company to a product must not affect existing lots
Bug: adding a new company to a product's company_ids triggers a recompute of stock.lot.company_id (which depends on product_id.company_id). Both lots evaluate product_id.company_id in the same env context, so they end up with the same company_id, causing _check_unique_lot to raise a ValidationError even though no lot data changed.
1 parent d2ca5fb commit c8dc4cf

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from . import product_template
1+
from . import product_template, stock_lot
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2025 ForgeFlow S.L.
2+
# (http://www.forgeflow.com)
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4+
5+
from odoo import api, models
6+
7+
8+
class StockLot(models.Model):
9+
_inherit = "stock.lot"
10+
11+
@api.depends("product_id.company_id", "product_id.company_ids")
12+
def _compute_company_id(self):
13+
# For lots belonging to single-company products, defer to standard logic.
14+
# For multi-company products, preserve the stored company_id so that a
15+
# change to product.company_ids (e.g. adding a third company) never
16+
# overwrites the lot's original company — which would cause _check_unique_lot
17+
# to fire when two lots share the same name across different companies.
18+
single_company_lots = self.filtered(
19+
lambda l: len(l.product_id.sudo().company_ids) == 1
20+
)
21+
res = super(StockLot, single_company_lots)._compute_company_id()
22+
for lot in self - single_company_lots:
23+
lot.company_id = lot._origin.company_id or lot.product_id.company_id
24+
return res

product_multi_company_stock/tests/test_product_multi_company_stock.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,39 @@ def test_remove_company_with_quants_or_moves_archived_product(self):
116116
with self.assertRaises(UserError) as error_move:
117117
product.write({"company_ids": [(6, 0, [self.company_2.id])]})
118118
self.assertIn("stock moves", str(error_move.exception))
119+
120+
def test_add_company_does_not_break_same_name_lots(self):
121+
"""Adding a company to a product must not affect existing lots.
122+
123+
Adding a new company to a product's company_ids triggers a
124+
recompute of stock.lot.company_id (which depends on product_id.company_id).
125+
should not change lots company.
126+
"""
127+
product = self.env["product.product"].create(
128+
{
129+
"name": "Test Tracked Product",
130+
"is_storable": True,
131+
"tracking": "lot",
132+
"company_ids": [(6, 0, [self.company_1.id, self.company_2.id])],
133+
}
134+
)
135+
company_3 = self.company_obj.create({"name": "Test company 3"})
136+
lot_1 = self.env["stock.lot"].with_company(self.company_1).create(
137+
{
138+
"name": "LOT001",
139+
"product_id": product.id,
140+
"company_id": self.company_1.id,
141+
}
142+
)
143+
lot_2 = self.env["stock.lot"].with_company(self.company_2).create(
144+
{
145+
"name": "LOT001",
146+
"product_id": product.id,
147+
"company_id": self.company_2.id,
148+
}
149+
)
150+
# Adding a third company should neither raise nor alter the lot companies.
151+
# This currently raises a ValidationError from _check_unique_lot — the bug.
152+
product.write({"company_ids": [(4, company_3.id)]})
153+
self.assertEqual(lot_1.company_id, self.company_1)
154+
self.assertEqual(lot_2.company_id, self.company_2)

0 commit comments

Comments
 (0)