fix: Ensure generated lot lines use the stock move's UOM when it differs from the product's base UOM.

This commit is contained in:
Suherdy Yacob 2026-01-23 09:51:12 +07:00
parent 3521e593ca
commit e22c36f4d7

View File

@ -64,6 +64,51 @@ class StockMove(models.Model):
# by passing a non-zero base and then overriding the names in the returned list.
fake_first = 'SEQDUMMY-1'
vals_list = super().action_generate_lot_line_vals(context, mode, fake_first, count, lot_text)
# Fix UOM issue: if Move UOM is different from Product UOM,
# the default generation might use Base UOM but with Move's quantity value
move = False
# 1. Try finding move via active_id
if context.get('active_id'):
candidate = self.env['stock.move'].browse(context.get('active_id'))
if candidate.exists() and candidate.product_id.id == product_id:
move = candidate
# 2. If not found, try finding via context parameters (passed by JS widget)
if not move and product_id:
domain = [
('product_id', '=', product_id),
('state', 'not in', ['done', 'cancel']),
]
if context.get('default_location_id'):
domain.append(('location_id', '=', context.get('default_location_id')))
if context.get('default_location_dest_id'):
domain.append(('location_dest_id', '=', context.get('default_location_dest_id')))
if context.get('default_picking_id'):
domain.append(('picking_id', '=', context.get('default_picking_id')))
elif context.get('active_model') == 'stock.picking' and context.get('active_id'):
domain.append(('picking_id', '=', context.get('active_id')))
moves = self.env['stock.move'].search(domain, limit=1)
if moves:
move = moves[0]
try:
if move:
move_uom = move.product_uom
base_uom = move.product_id.uom_id
if move_uom and base_uom and move_uom != base_uom:
# Update all generate lines to use the move's UOM
# assuming reasonable consistency
for vals in vals_list:
# Pass as tuple (id, name) so frontend displays it correctly immediately
vals['product_uom_id'] = (move_uom.id, move_uom.display_name)
except Exception as e:
# Use a safe logger if available, otherwise just print to not break flow
import logging
_logger = logging.getLogger(__name__)
_logger.error(f"Error fixing UOM in product_lot_sequence_per_product: {e}")
# Overwrite the lot_name with sequence-based names; keep all other computed values (uom, putaway).
for vals, name in zip(vals_list, generated_names):
vals['lot_name'] = name