| 
						
						
							
								
							
						
						
					 | 
					@ -22,7 +22,8 @@ | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					############################################################################## | 
					 | 
					 | 
					############################################################################## | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					from odoo import api, models, fields, _ | 
					 | 
					 | 
					from odoo import api, models, fields, _ | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					from odoo.exceptions import UserError | 
					 | 
					 | 
					from odoo.exceptions import UserError, ValidationError | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					from psycopg2 import IntegrityError | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					from odoo.addons.mrp.models.mrp_production import MrpProduction as mp | 
					 | 
					 | 
					from odoo.addons.mrp.models.mrp_production import MrpProduction as mp | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -39,15 +40,32 @@ class MrpProduction(models.Model): | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        ('cancel', 'Cancelled')], string='State', | 
					 | 
					 | 
					        ('cancel', 'Cancelled')], string='State', | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        copy=False, default='draft', track_visibility='onchange') | 
					 | 
					 | 
					        copy=False, default='draft', track_visibility='onchange') | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					    _sql_constraints = [ | 
					 | 
					 | 
					    @api.model_cr | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        ('name_uniq', "check(state='draft' or UNIQUE(name,company_id))", 'Reference must be unique per Company!'), | 
					 | 
					 | 
					    def init(self): | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					    ] | 
					 | 
					 | 
					        self.env.cr.execute('ALTER TABLE mrp_production DROP CONSTRAINT mrp_production_name_uniq') | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        self.env.cr.execute( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            '''CREATE UNIQUE INDEX mrp_mo_unique ON mrp_production (name,company_id) WHERE (state != 'draft')''') | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    @api.model | 
					 | 
					 | 
					    @api.model | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    def create(self, values): | 
					 | 
					 | 
					    def create(self, values): | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        production = super(mp, self).create(values) | 
					 | 
					 | 
					        production = super(mp, self).create(values) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        return production | 
					 | 
					 | 
					        return production | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    @api.multi | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    def write(self, vals): | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        try: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            res = super(MrpProduction, self).write(vals) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        except IntegrityError: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            raise ValidationError(_("Reference must be unique per Company for confirmed orders!")) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        if 'date_planned_start' in vals: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            moves = (self.mapped('move_raw_ids') + self.mapped('move_finished_ids')).filtered( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                lambda r: r.state not in ['done', 'cancel']) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            moves.write({ | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                'date_expected': vals['date_planned_start'], | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            }) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        if res: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            return res | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    @api.multi | 
					 | 
					 | 
					    @api.multi | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    def unlink(self): | 
					 | 
					 | 
					    def unlink(self): | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        if any(production.state not in ['draft', 'cancel'] for production in self): | 
					 | 
					 | 
					        if any(production.state not in ['draft', 'cancel'] for production in self): | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -56,9 +74,17 @@ class MrpProduction(models.Model): | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    @api.multi | 
					 | 
					 | 
					    @api.multi | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    def action_confirm(self): | 
					 | 
					 | 
					    def action_confirm(self): | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					        if not self.name or self.name == _('New'): | 
					 | 
					 | 
					        self.onchange_product_id() | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					            self.name = self.env['ir.sequence'].next_by_code('mrp.production') or _('New') | 
					 | 
					 | 
					        for production in self: | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        if not self.procurement_group_id: | 
					 | 
					 | 
					            if not production.name or production.name == _('New'): | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					            self.procurement_group_id = self.env["procurement.group"].create({'name': self.name}).id | 
					 | 
					 | 
					                picking_type_id = production._get_default_picking_type() | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        self._generate_moves() | 
					 | 
					 | 
					                picking_type_id = self.env['stock.picking.type'].browse(picking_type_id) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        self.state = 'confirmed' | 
					 | 
					 | 
					                if picking_type_id: | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                    production.name = picking_type_id.sequence_id.next_by_id() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                else: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                    production.name = self.env['ir.sequence'].next_by_code('mrp.production') or _('New') | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            production.state = 'confirmed' | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            if not self.procurement_group_id: | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					                production.procurement_group_id = self.env["procurement.group"].create({'name': production.name}).id | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					            production._generate_moves() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					
  |