odoo/odoo#188341

Created by fw-bot
Merged at fc20a82157e37c79f49459da710b727c42379d67

Statuses:

label
odoo-dev:saas-17.4-17.0-opw-4043539-no_more_mistakenly_untracked_products-hael-h_21-fw
head
6943b0a71b3cf8494637ba7f3353de22e43bfa6d
merged
1 year ago by Hadi El Yakhni (hael)
odoo/odoo
17.0 #187526
saas-17.2 #188336
saas-17.4 #188341
18.0 #188350
saas-18.2
saas-18.3
saas-18.4
19.0
master #188355

[FW][FIX] mrp: no unexpected untracked products

Steps to reproduce:

  • Create a product tracked by lot with a BOM
  • Create and confirm an MO for 5 units of that product
  • Assign a producing lot via the [+] smart button, and produce all
  • Create, confirm, and validate the shipping for a SO of 3 items of that product
  • Go back to the MO, unlock it, and change the quantity producing to 10
  • Go to the product page, click on "On Hand"
    > See that there exists 12 products with a lot id, and -5 without a lot id

Expected behavior:
There should be 7 products with lot it, and NO products without lot id

Cause of the issue:
When increasing the quantity producing, the newly created stock move line
(SML) is created without a lot id: https://github.com/odoo/odoo/blob/d5a7a3d02e3e2b4e47977abc8b9fc0d5d6135937/addons/stock/models/stock_move.py#L1471-L1477
Then only later, we update this new created move line to give it a lot_id
https://github.com/odoo/odoo/blob/a4ca3a3d6eb368bebcbcad00d81e098ee86a8610/addons/mrp/models/mrp_production.py#L900-L901
This update will first cause an undo of some move lines, calling _synchronize_quant (first call)
https://github.com/odoo/odoo/blob/49d25c58db2329d097566dc222d97cd8988d640d/addons/stock/models/stock_move_line.py#L459-L461
then, the actual lot_id update is committed
https://github.com/odoo/odoo/blob/49d25c58db2329d097566dc222d97cd8988d640d/addons/stock/models/stock_move_line.py#L473
then after that, another call to _synchronize_quant is being made to apply the changes (second call) https://github.com/odoo/odoo/blob/49d25c58db2329d097566dc222d97cd8988d640d/addons/stock/models/stock_move_line.py#L476-L477
However, since _synchronize_quant depends on reading the untracked quantities to decide
if it can compensate for negative values
https://github.com/odoo/odoo/blob/49d25c58db2329d097566dc222d97cd8988d640d/addons/stock/models/stock_move_line.py#L645
it sees different value of untracked_qty in the first call than that in the second call,
which makes things go out of sync.

Solution:
The newly created move line has the correct lot_id at the moment of the creation,
so we avoid the issue of updating it later, and risking the quantities to go out of sync.

Another solution might be to edit the write method of StockMoveLine, by committing
changes related to lot_id before undoing any move lines

if updates['lot_id'] and ml.lot_id != updates['lot_id']:
    super(StockMoveLine, ml).write({ 'lot_id': updates['lot_id'] })
# then here proceed to undoing

opw-4043539

Forward-Port-Of: #187526