diff --git a/addons/mrp/mrp.py b/addons/mrp/mrp.py index 58a474d56b4..aff1a51ffaa 100644 --- a/addons/mrp/mrp.py +++ b/addons/mrp/mrp.py @@ -606,6 +606,26 @@ class mrp_production(osv.osv): self.write(cr, uid, ids, {'state': 'picking_except'}) return True + def _prepare_lines(self, cr, uid, production, properties=None, context=None): + # search BoM structure and route + bom_obj = self.pool.get('mrp.bom') + uom_obj = self.pool.get('product.uom') + bom_point = production.bom_id + bom_id = production.bom_id.id + if not bom_point: + bom_id = bom_obj._bom_find(cr, uid, production.product_id.id, production.product_uom.id, properties) + if bom_id: + bom_point = bom_obj.browse(cr, uid, bom_id) + routing_id = bom_point.routing_id.id or False + self.write(cr, uid, [production.id], {'bom_id': bom_id, 'routing_id': routing_id}) + + if not bom_id: + raise osv.except_osv(_('Error!'), _("Cannot find a bill of material for this product.")) + + # get components and workcenter_lines from BoM structure + factor = uom_obj._compute_qty(cr, uid, production.product_uom.id, production.product_qty, bom_point.product_uom.id) + return bom_obj._bom_explode(cr, uid, bom_point, factor / bom_point.product_qty, properties, routing_id=production.routing_id.id) + def _action_compute_lines(self, cr, uid, ids, properties=None, context=None): """ Compute product_lines and workcenter_lines from BoM structure @return: product_lines @@ -614,8 +634,6 @@ class mrp_production(osv.osv): if properties is None: properties = [] results = [] - bom_obj = self.pool.get('mrp.bom') - uom_obj = self.pool.get('product.uom') prod_line_obj = self.pool.get('mrp.production.product.line') workcenter_line_obj = self.pool.get('mrp.production.workcenter.line') @@ -625,23 +643,8 @@ class mrp_production(osv.osv): #unlink workcenter_lines workcenter_line_obj.unlink(cr, SUPERUSER_ID, [line.id for line in production.workcenter_lines], context=context) - - # search BoM structure and route - bom_point = production.bom_id - bom_id = production.bom_id.id - if not bom_point: - bom_id = bom_obj._bom_find(cr, uid, production.product_id.id, production.product_uom.id, properties) - if bom_id: - bom_point = bom_obj.browse(cr, uid, bom_id) - routing_id = bom_point.routing_id.id or False - self.write(cr, uid, [production.id], {'bom_id': bom_id, 'routing_id': routing_id}) - - if not bom_id: - raise osv.except_osv(_('Error!'), _("Cannot find a bill of material for this product.")) - - # get components and workcenter_lines from BoM structure - factor = uom_obj._compute_qty(cr, uid, production.product_uom.id, production.product_qty, bom_point.product_uom.id) - res = bom_obj._bom_explode(cr, uid, bom_point, factor / bom_point.product_qty, properties, routing_id=production.routing_id.id) + + res = self._prepare_lines(cr, uid, production, properties=properties, context=context) results = res[0] # product_lines results2 = res[1] # workcenter_lines @@ -886,7 +889,7 @@ class mrp_production(osv.osv): """ res = True for production in self.browse(cr, uid, ids): - boms = self._action_compute_lines(cr, uid, [production.id]) + boms = self._prepare_lines(cr, uid, production)[0] res = False for bom in boms: product = self.pool.get('product.product').browse(cr, uid, bom['product_id'])