Skip to content

Commit e74af12

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 e74af12

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

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

product_multi_company_stock/tests/test_product_multi_company_stock.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Copyright 2025 ForgeFlow S.L.
2-
# (http://www.forgeflow.com)
1+
# Copyright 2025-26 ForgeFlow S.L. (http://www.forgeflow.com)
32
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
43

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

0 commit comments

Comments
 (0)