diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 34d64f50fb0..e424be4fd14 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -736,12 +736,18 @@ class MrpProduction(models.Model): @api.onchange('lot_producing_id') def _onchange_lot_producing(self): - if self.product_id.tracking == 'serial' and self.lot_producing_id: - message, dummy = self.env['stock.quant'].sudo()._check_serial_number(self.product_id, - self.lot_producing_id, - self.company_id) + res = self._can_produce_serial_number() + if res is not True: + return res + + def _can_produce_serial_number(self, sn=None): + self.ensure_one() + sn = sn or self.lot_producing_id + if self.product_id.tracking == 'serial' and sn: + message, dummy = self.env['stock.quant'].sudo()._check_serial_number(self.product_id, sn, self.company_id) if message: return {'warning': {'title': _('Warning'), 'message': message}} + return True @api.onchange('product_id', 'move_raw_ids') def _onchange_product_id(self): diff --git a/addons/mrp/models/mrp_workorder.py b/addons/mrp/models/mrp_workorder.py index 571de8ea775..a3aabdbf008 100644 --- a/addons/mrp/models/mrp_workorder.py +++ b/addons/mrp/models/mrp_workorder.py @@ -437,6 +437,12 @@ class MrpWorkorder(models.Model): ) return interval['hours'] * 60 + @api.onchange('finished_lot_id') + def _onchange_finished_lot_id(self): + res = self.production_id._can_produce_serial_number(sn=self.finished_lot_id) + if res is not True: + return res + def write(self, values): if 'production_id' in values and any(values['production_id'] != w.production_id.id for w in self): raise UserError(_('You cannot link this work order to another manufacturing order.')) @@ -843,18 +849,6 @@ class MrpWorkorder(models.Model): float_round(self.qty_producing, precision_rounding=rounding) ) - def _check_sn_uniqueness(self): - """ Alert the user if the serial number as already been produced """ - if self.product_tracking == 'serial' and self.finished_lot_id: - sml = self.env['stock.move.line'].search_count([ - ('lot_id', '=', self.finished_lot_id.id), - ('location_id.usage', '=', 'production'), - ('qty_done', '=', 1), - ('state', '=', 'done') - ]) - if sml: - raise UserError(_('This serial number for product %s has already been produced', self.product_id.name)) - def _should_start_timer(self): return True