product_lot_sequence_per_pr.../models/stock_quant.py
2025-10-29 15:09:11 +07:00

39 lines
1.8 KiB
Python

from odoo import api, models, fields, _
from odoo.tools.float_utils import float_compare
class StockQuant(models.Model):
_inherit = 'stock.quant'
def _get_inventory_move_values(self, qty, location_id, location_dest_id, package_id=False, package_dest_id=False):
"""Override to handle automatic lot generation for inventory adjustments."""
# Check if we need to generate a lot for this inventory adjustment
if (self.product_id.tracking in ['lot', 'serial'] and
float_compare(qty, 0, precision_rounding=self.product_uom_id.rounding) > 0 and
not self.lot_id and
self.product_id.product_tmpl_id.lot_sequence_id):
# Generate lot number using the product's sequence
lot_sequence = self.product_id.product_tmpl_id.lot_sequence_id
lot_name = lot_sequence.next_by_id()
# Create the lot record
lot = self.env['stock.lot'].create({
'name': lot_name,
'product_id': self.product_id.id,
'company_id': self.company_id.id,
})
# Update the quant with the new lot BEFORE creating the move
self.lot_id = lot.id
# Call the original method to get the move values
move_vals = super()._get_inventory_move_values(qty, location_id, location_dest_id, package_id, package_dest_id)
# Make sure the lot_id is properly set in the move line values
if self.lot_id and 'move_line_ids' in move_vals:
for line_command in move_vals['move_line_ids']:
if line_command[0] in [0, 1] and line_command[2]: # create or update command
line_command[2]['lot_id'] = self.lot_id.id
return move_vals