diff --git a/addons/mrp/mrp.py b/addons/mrp/mrp.py index 4cd594cd178..aeacf4a71a2 100644 --- a/addons/mrp/mrp.py +++ b/addons/mrp/mrp.py @@ -680,6 +680,17 @@ class mrp_production(osv.osv): res = False return res + def _get_subproduct_factor(self, cr, uid, production_id, move_id=None, context=None): + """ Compute the factor to compute the qty of procucts to produce for the given production_id. By default, + it's always equal to the quantity encoded in the production order or the production wizard, but if the + module mrp_subproduct is installed, then we must use the move_id to identify the product to produce + and its quantity. + :param production_id: ID of the mrp.order + :param move_id: ID of the stock move that needs to be produced. Will be used in mrp_subproduct. + :return: The factor to apply to the quantity that we should produce for the given production order. + """ + return 1 + def action_produce(self, cr, uid, production_id, production_qty, production_mode, context=None): """ To produce final product based on production mode (consume/consume&produce). If Production mode is consume, all stock move lines of raw materials will be done/consumed. @@ -693,7 +704,6 @@ class mrp_production(osv.osv): stock_mov_obj = self.pool.get('stock.move') production = self.browse(cr, uid, production_id, context=context) - produced_qty = 0 if production_mode == 'consume_produce': produced_qty = production_qty @@ -748,11 +758,12 @@ class mrp_production(osv.osv): for produce_product in production.move_created_ids: produced_qty = produced_products.get(produce_product.product_id.id, 0) - rest_qty = production.product_qty - produced_qty + subproduct_factor = self._get_subproduct_factor(cr, uid, production.id, produce_product.id, context=context) + rest_qty = (subproduct_factor * production.product_qty) - produced_qty if rest_qty <= production_qty: production_qty = rest_qty if rest_qty > 0 : - stock_mov_obj.action_consume(cr, uid, [produce_product.id], production_qty, context=context) + stock_mov_obj.action_consume(cr, uid, [produce_product.id], (subproduct_factor * production_qty), context=context) for raw_product in production.move_lines2: new_parent_ids = [] diff --git a/addons/mrp/wizard/mrp_product_produce.py b/addons/mrp/wizard/mrp_product_produce.py index 69fc3a9dd7d..dc9a235ff63 100644 --- a/addons/mrp/wizard/mrp_product_produce.py +++ b/addons/mrp/wizard/mrp_product_produce.py @@ -49,8 +49,9 @@ class mrp_product_produce(osv.osv_memory): context['active_id'], context=context) done = 0.0 for move in prod.move_created_ids2: - if not move.scrapped: - done += move.product_qty + if move.product_id == prod.product_id: + if not move.scrapped: + done += move.product_qty return (prod.product_qty - done) or prod.product_qty _defaults = { diff --git a/addons/mrp_subproduct/mrp_subproduct.py b/addons/mrp_subproduct/mrp_subproduct.py index 130411987c1..bb3b6789574 100644 --- a/addons/mrp_subproduct/mrp_subproduct.py +++ b/addons/mrp_subproduct/mrp_subproduct.py @@ -29,11 +29,14 @@ class mrp_subproduct(osv.osv): 'product_id': fields.many2one('product.product', 'Product', required=True), 'product_qty': fields.float('Product Qty', required=True), 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True), - 'subproduct_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Quantity Type', required=True), + 'subproduct_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Quantity Type', required=True, help="Define how the quantity of subproducts will be set on the production orders using this BoM.\ + 'Fixed' depicts a situation where the quantity of created subproduct is always equal to the quantity set on the BoM, regardless of how many are created in the production order.\ + By opposition, 'Variable' means that the quantity will be computed as\ + '(quantity of subproduct set on the BoM / quantity of manufactured product set on the BoM * quantity of manufactured product in the production order.)'"), 'bom_id': fields.many2one('mrp.bom', 'BoM'), } _defaults={ - 'subproduct_type': lambda *args: 'fixed' + 'subproduct_type': 'variable', } def onchange_product_id(self, cr, uid, ids, product_id, context=None): @@ -97,5 +100,27 @@ class mrp_production(osv.osv): self.pool.get('stock.move').create(cr, uid, data) return picking_id + def _get_subproduct_factor(self, cr, uid, production_id, move_id=None, context=None): + """Compute the factor to compute the qty of procucts to produce for the given production_id. By default, + it's always equal to the quantity encoded in the production order or the production wizard, but with + the module mrp_subproduct installed it can differ for subproducts having type 'variable'. + :param production_id: ID of the mrp.order + :param move_id: ID of the stock move that needs to be produced. Identify the product to produce. + :return: The factor to apply to the quantity that we should produce for the given production order and stock move. + """ + sub_obj = self.pool.get('mrp.subproduct') + move_obj = self.pool.get('stock.move') + production_obj = self.pool.get('mrp.production') + production_browse = production_obj.browse(cr, uid, production_id, context=context) + move_browse = move_obj.browse(cr, uid, move_id, context=context) + subproduct_factor = 1 + sub_id = sub_obj.search(cr, uid,[('product_id', '=', move_browse.product_id.id),('bom_id', '=', production_browse.bom_id.id), ('subproduct_type', '=', 'variable')], context=context) + if sub_id: + subproduct_record = sub_obj.browse(cr ,uid, sub_id[0], context=context) + if subproduct_record.bom_id.product_qty: + subproduct_factor = subproduct_record.product_qty / subproduct_record.bom_id.product_qty + return subproduct_factor + return super(mrp_production, self)._get_subproduct_factor(cr, uid, production_id, move_id, context=context) + mrp_production() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: