[IMP] mrp, mrp_subproduct: cleaner implementation of the fix for lp:794431 proposed by aag

lp bug: https://launchpad.net/bugs/794431 fixed

bzr revid: qdp-launchpad@openerp.com-20111109151038-oqo76jgzbd87t7v9
This commit is contained in:
Quentin (OpenERP) 2011-11-09 16:10:38 +01:00
parent c6ab1b152a
commit cbaf08b7db
2 changed files with 33 additions and 38 deletions

View File

@ -680,24 +680,16 @@ class mrp_production(osv.osv):
res = False
return res
def _get_quantity_to_produce(self, cr, uid, production_id, move_id=None, context=None):
""" Compute Production Qty of product.This method will be overwritten by mrp_subproduct.
@return: Dictionary of values.
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.
"""
if context is None:
context = {}
production_obj = self.pool.get('mrp.production')
production_browse = production_obj.browse(cr, uid, production_id, context=context)
if context.get('product_qty',False):
product_qty = context['product_qty']
sub_qty = context['sub_qty']
else:
product_qty = production_browse.product_qty
sub_qty = 1
res = {'product_qty': product_qty, 'sub_qty': sub_qty}
return res
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).
@ -766,12 +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)
get_qty = self._get_quantity_to_produce(cr, uid, production.id, produce_product.id, context=context)
rest_qty = get_qty['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 * get_qty['sub_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 = []

View File

@ -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, help="Production Type of Product"),
'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: 'variable'
'subproduct_type': 'variable',
}
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
@ -97,27 +100,27 @@ class mrp_production(osv.osv):
self.pool.get('stock.move').create(cr, uid, data)
return picking_id
def _get_quantity_to_produce(self, cr, uid, production_id, move_id=None, context=None):
""" Compute Production Qty of product.This method is overwrite of mrp_production.
@return: Dictionary of values.
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.
"""
if context is None:
context = {}
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)
move_browse = move_obj.browse(cr, uid, move_id, context)
sub_qty = 1
sub_id = sub_obj.search(cr, uid,[('product_id', '=', move_browse.product_id.id),('bom_id', '=', production_browse.bom_id.id)], context=context )
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:
sub_qty = sub_obj.browse(cr ,uid, sub_id[0], context=context).product_qty
context ['product_qty'] = production_browse.product_qty * sub_qty
context ['sub_qty'] = sub_qty
return super(mrp_production, self)._get_quantity_to_produce(cr, uid, production_id, move_id, context=context)
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: