diff --git a/mrp_production_batch/models/mrp_production.py b/mrp_production_batch/models/mrp_production.py index b7d99c8e4..bffa16022 100644 --- a/mrp_production_batch/models/mrp_production.py +++ b/mrp_production_batch/models/mrp_production.py @@ -157,7 +157,7 @@ def mrp_production_batch_create_wizard_action(self): def _action_generate_consumption_wizard(self, consumption_issues): if not self.env.context.get("mrp_production_batch_create"): return super()._action_generate_consumption_wizard(consumption_issues) - if self.bom_id.bom_line_ids.product_id.ids != self.move_raw_ids.product_id.ids: + if self.bom_id.bom_line_ids.product_id != self.move_raw_ids.product_id: raise ValidationError( _( "A problem has been detected in production %s. Make sure you " @@ -200,28 +200,27 @@ def action_view_production_batch(self): "res_id": self.production_batch_id.id, } - # TODO: Refactor translation management and raise of "You need to supply..." - def action_check_with_batch(self): - self.ensure_one() - try: - with self.env.cr.savepoint(): - self_wc = self.with_context(mrp_production_batch_create=True) - lot = self_wc.env["stock.production.lot"].create( - { - "name": "", - "product_id": self.product_id.id, - "company_id": self.company_id.id, - } - ) - self_wc.lot_producing_id = lot.id - self_wc.button_mark_done() - raise FakeException("") - except FakeException: - self.is_ready_to_produce = True - self.error_message = False - except (UserError, ValidationError) as e: - self.is_ready_to_produce = False - self.error_message = e.name + def action_produce_batch(self): + for rec in self: + try: + with rec.env.cr.savepoint(): + self_wc = rec.with_context(mrp_production_batch_create=True) + lot = self_wc.env["stock.production.lot"].create( + { + "name": "", + "product_id": rec.product_id.id, + "company_id": rec.company_id.id, + } + ) + self_wc.lot_producing_id = lot.id + self_wc.button_mark_done() + raise FakeException("") + except FakeException: + rec.is_ready_to_produce = True + rec.error_message = False + except (UserError, ValidationError) as e: + rec.is_ready_to_produce = False + rec.error_message = e.name def create_batch_lot_by_bom(self): self.ensure_one() diff --git a/mrp_production_batch/models/mrp_production_batch.py b/mrp_production_batch/models/mrp_production_batch.py index 0f0f1e36b..62be829c0 100644 --- a/mrp_production_batch/models/mrp_production_batch.py +++ b/mrp_production_batch/models/mrp_production_batch.py @@ -125,8 +125,7 @@ def action_check(self): self.ensure_one() productions = self.production_ids.filtered(lambda r: not r.is_ready_to_produce) init_production_count = len(productions) - for production in productions: - production.action_check_with_batch() + productions.action_produce_batch() final_production_count = len( self.production_ids.filtered(lambda r: not r.is_ready_to_produce) ) @@ -139,31 +138,27 @@ def action_check(self): def _check_unique_serial_lot_in_batch(self): for rec in self: - serial_lots = [] + lot_production_map = {} for production in rec.production_ids: - serial_move_line = production.move_raw_ids.filtered( + for move_line in production.move_raw_ids.filtered( lambda x: x.product_id.tracking == "serial" - ).move_line_ids - serial_lot_ids = serial_move_line.mapped("lot_id").ids - serial_lots.extend(serial_lot_ids) - for lot in serial_lots: - if serial_lots.count(lot) > 1: - move = ( - self.env["stock.move.line"] - .search([("lot_id", "=", lot)]) - .filtered( - lambda x: x.move_id.raw_material_production_id - in rec.production_ids + ).move_line_ids: + lot_id = move_line.lot_id.id + if lot_id in lot_production_map: + dup_production, dup_product = lot_production_map[lot_id] + raise ValidationError( + _( + "The following productions [%s, %s] are using the " + "component %s with the same serial number. Please " + "make sure that the serial numbers are unique." + ) + % ( + dup_production.display_name, + production.display_name, + dup_product.display_name, + ) ) - .move_id - ) - raise ValidationError( - _( - "The following productions are using the same serial number in" - " some of their components: %s" - % move.mapped("raw_material_production_id.display_name") - ) - ) + lot_production_map[lot_id] = (production, move_line.product_id) def action_done(self): self.ensure_one() @@ -205,7 +200,7 @@ def action_view_production_ready(self): action["domain"] = [("id", "in", self.ready_production_ids.ids)] return action - def get_user_timezone_datetime(self): + def get_user_locale_tz_datetime_string(self, date): self.ensure_one() lang = self.env["res.lang"].search([("code", "=", self.env.user.lang)]) datetime_format = " ".join( @@ -217,11 +212,7 @@ def get_user_timezone_datetime(self): ), ) ) - timezone = self._context.get("tz") or self.env.user.partner_id.tz or "UTC" - self_tz = self.with_context(tz=timezone) - return fields.Datetime.context_timestamp( - self_tz, fields.Datetime.from_string(self.create_date) - ).strftime(datetime_format) + return fields.Datetime.context_timestamp(self, date).strftime(datetime_format) @api.depends("name", "state") def name_get(self): @@ -229,7 +220,7 @@ def name_get(self): for rec in self: if rec.state == "draft": state = _("Draft") - user_datetime = rec.get_user_timezone_datetime() + user_datetime = rec.get_user_locale_tz_datetime_string(rec.create_date) name = "%s [%s] %s" % ( state, user_datetime, diff --git a/mrp_production_batch/views/mrp_production_views.xml b/mrp_production_batch/views/mrp_production_views.xml index 01440a889..2b2a608eb 100644 --- a/mrp_production_batch/views/mrp_production_views.xml +++ b/mrp_production_batch/views/mrp_production_views.xml @@ -50,7 +50,7 @@