subcontract_generate_lot/models/stock_move_line.py
2026-01-28 13:25:23 +07:00

84 lines
3.0 KiB
Python

from odoo import api, models
class StockMoveLine(models.Model):
_inherit = 'stock.move.line'
def _prepare_new_lot_vals(self):
"""
Ensure that bogus base like '0' or empty lot_name does not create a lot literally named '0'.
If lot_name is empty or equals '0', let standard Odoo logic handles it
(or if previously relied on custom overrides, we ensure we pass False to trigger sequence usage if available).
"""
self.ensure_one()
# Normalize lot_name
lot_name = (self.lot_name or '').strip()
normalized_name = lot_name if lot_name and lot_name != '0' else False
vals = {
'name': normalized_name,
'product_id': self.product_id.id,
}
if self.product_id.company_id and self.company_id in (self.product_id.company_id.all_child_ids | self.product_id.company_id):
vals['company_id'] = self.company_id.id
return vals
def action_open_lot_generator(self):
"""Open the lot generator wizard for this move line."""
self.ensure_one()
if not self.product_id:
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': 'No Product',
'message': 'Please select a product first.',
'type': 'warning',
'sticky': False,
}
}
if self.product_id.tracking == 'none':
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': 'No Tracking Required',
'message': 'This product does not require lot/serial tracking.',
'type': 'info',
'sticky': False,
}
}
# Odoo 19 standardizing on 'quantity' for stock.move.line
quantity_value = self.quantity or 0.0
src_uom = self.product_uom_id
if quantity_value <= 0:
# Fallback logic if line has no quantity
if self.move_id:
done = self.move_id.quantity
demand = self.move_id.product_uom_qty
remaining = max(demand - done, 0.0)
quantity_value = remaining if remaining > 0 else 1.0
src_uom = self.move_id.product_uom
else:
quantity_value = 1.0
src_uom = self.product_uom_id
# Return action to open wizard
return {
'name': 'Generate Lots for Move Line',
'type': 'ir.actions.act_window',
'res_model': 'subcontract.lot.generator',
'view_mode': 'form',
'target': 'new',
'context': {
'default_move_id': self.move_id.id,
'default_picking_id': self.picking_id.id,
'default_product_id': self.product_id.id,
'default_quantity': quantity_value,
},
}