コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
213 ビュー

 I've developed a mod to validate in real time in stock.move.line that serial numbers used in other pickings or duplicates are not entered on the same delivery note.  Can someone help me?

アバター
破棄

What kind of help do you think readers can provide without seeing your code?

最善の回答

Hi,

Please refer to the code below:


from odoo import api, models
from odoo.exceptions import ValidationError

class StockMoveLine(models.Model):
_inherit = 'stock.move.line'

@api.onchange('lot_id')
def _onchange_lot_id_serial_validation(self):
for line in self:
if not line.lot_id or not line.picking_id:
continue

# 1. Check for duplicate in same picking
duplicate_lines = self.env['stock.move.line'].search([
('picking_id', '=', line.picking_id.id),
('lot_id', '=', line.lot_id.id),
('id', '!=', line.id)
])
if duplicate_lines:
raise ValidationError("This serial number is already used in this delivery note.")

# 2. Check if serial is used in any other *validated* delivery
other_pickings = self.env['stock.move.line'].search([
('lot_id', '=', line.lot_id.id),
('picking_id', '!=', line.picking_id.id),
('picking_id.state', '=', 'done')
], limit=1)
if other_pickings:
raise ValidationError(f"The serial number '{line.lot_id.name}' is already used in another completed delivery.")

@api.constrains('lot_id', 'picking_id')
def _check_serial_number_constraints(self):
for line in self:
if not line.lot_id or not line.picking_id:
continue

# Same logic as onchange, repeated for backend safety

duplicate_lines = self.env['stock.move.line'].search([
('picking_id', '=', line.picking_id.id),
('lot_id', '=', line.lot_id.id),
('id', '!=', line.id)
])
if duplicate_lines:
raise ValidationError("This serial number is already used in this delivery note.")

other_pickings = self.env['stock.move.line'].search([
('lot_id', '=', line.lot_id.id),
('picking_id', '!=', line.picking_id.id),
('picking_id.state', '=', 'done')
], limit=1)
if other_pickings:
raise ValidationError(f"The serial number '{line.lot_id.name}' is already used in another completed delivery.")

Hope it helps.

アバター
破棄