[REF] product: refactor move function field on variant in order to allow to move the seller_ids from the template to the variant easily

bzr revid: sebastien.beau@akretion.com.br-20120711002333-frtn37s96ovjahkp
This commit is contained in:
sebastien beau 2012-07-11 02:23:33 +02:00
parent e9c288b4bf
commit a2ad095f61
1 changed files with 35 additions and 33 deletions

View File

@ -258,32 +258,6 @@ class product_template(osv.osv):
_name = "product.template"
_description = "Product Template"
def _get_main_product_supplier(self, cr, uid, product, context=None):
"""Determines the main (best) product supplier for ``product``,
returning the corresponding ``supplierinfo`` record, or False
if none were found. The default strategy is to select the
supplier with the highest priority (i.e. smallest sequence).
:param browse_record product: product to supply
:rtype: product.supplierinfo browse_record or False
"""
sellers = [(seller_info.sequence, seller_info)
for seller_info in product.seller_ids or []
if seller_info and isinstance(seller_info.sequence, (int, long))]
return sellers and sellers[0][1] or False
def _calc_seller(self, cr, uid, ids, fields, arg, context=None):
result = {}
for product in self.browse(cr, uid, ids, context=context):
main_supplier = self._get_main_product_supplier(cr, uid, product, context=context)
result[product.id] = {
'seller_info_id': main_supplier and main_supplier.id or False,
'seller_delay': main_supplier and main_supplier.delay or 1,
'seller_qty': main_supplier and main_supplier.qty or 0.0,
'seller_id': main_supplier and main_supplier.name.id or False
}
return result
_columns = {
'name': fields.char('Name', size=128, required=True, translate=True, select=True),
'product_manager': fields.many2one('res.users','Product Manager',help="This is use as task responsible"),
@ -320,11 +294,7 @@ class product_template(osv.osv):
help='Coefficient to convert Unit of Measure to UOS\n'
' uos = uom * coeff'),
'mes_type': fields.selection((('fixed', 'Fixed'), ('variable', 'Variable')), 'Measure Type', required=True),
'seller_info_id': fields.function(_calc_seller, type='many2one', relation="product.supplierinfo", multi="seller_info"),
'seller_delay': fields.function(_calc_seller, type='integer', string='Supplier Lead Time', multi="seller_info", help="This is the average delay in days between the purchase order confirmation and the reception of goods for this product and for the default supplier. It is used by the scheduler to order requests based on reordering delays."),
'seller_qty': fields.function(_calc_seller, type='float', string='Supplier Quantity', multi="seller_info", help="This is minimum quantity to purchase from Main Supplier."),
'seller_id': fields.function(_calc_seller, type='many2one', relation="res.partner", string='Main Supplier', help="Main Supplier who has highest priority in Supplier List.", multi="seller_info"),
'seller_ids': fields.one2many('product.supplierinfo', 'product_id', 'Partners'),
'seller_ids': fields.one2many('product.supplierinfo', 'product_template_id', 'Partners'),
'loc_rack': fields.char('Rack', size=16),
'loc_row': fields.char('Row', size=16),
'loc_case': fields.char('Case', size=16),
@ -491,6 +461,34 @@ class product_product(osv.osv):
res[p.id] = (data['code'] and ('['+data['code']+'] ') or '') + \
(data['name'] or '') + (data['variants'] and (' - '+data['variants']) or '')
return res
def _get_main_product_supplier(self, cr, uid, product, context=None):
"""Determines the main (best) product supplier for ``product``,
returning the corresponding ``supplierinfo`` record, or False
if none were found. The default strategy is to select the
supplier with the highest priority (i.e. smallest sequence).
:param browse_record product: product to supply
:rtype: product.supplierinfo browse_record or False
"""
sellers = [(seller_info.sequence, seller_info)
for seller_info in product.seller_ids or []
if seller_info and isinstance(seller_info.sequence, (int, long))]
return sellers and sellers[0][1] or False
def _calc_seller(self, cr, uid, ids, fields, arg, context=None):
result = {}
for product in self.browse(cr, uid, ids, context=context):
main_supplier = self._get_main_product_supplier(cr, uid, product, context=context)
result[product.id] = {
'seller_info_id': main_supplier and main_supplier.id or False,
'seller_delay': main_supplier and main_supplier.delay or 1,
'seller_qty': main_supplier and main_supplier.qty or 0.0,
'seller_id': main_supplier and main_supplier.name.id or False
}
return result
_defaults = {
'active': lambda *a: 1,
@ -526,6 +524,10 @@ class product_product(osv.osv):
'name_template': fields.related('product_tmpl_id', 'name', string="Name", type='char', size=128, store=True, select=True),
'color': fields.integer('Color Index'),
'product_image': fields.binary('Image'),
'seller_info_id': fields.function(_calc_seller, type='many2one', relation="product.supplierinfo", multi="seller_info"),
'seller_delay': fields.function(_calc_seller, type='integer', string='Supplier Lead Time', multi="seller_info", help="This is the average delay in days between the purchase order confirmation and the reception of goods for this product and for the default supplier. It is used by the scheduler to order requests based on reordering delays."),
'seller_qty': fields.function(_calc_seller, type='float', string='Supplier Quantity', multi="seller_info", help="This is minimum quantity to purchase from Main Supplier."),
'seller_id': fields.function(_calc_seller, type='many2one', relation="res.partner", string='Main Supplier', help="Main Supplier who has highest priority in Supplier List.", multi="seller_info"),
}
def create(self, cr, uid, vals, context=None):
@ -783,10 +785,10 @@ class product_supplierinfo(osv.osv):
'product_name': fields.char('Supplier Product Name', size=128, help="This supplier's product name will be used when printing a request for quotation. Keep empty to use the internal one."),
'product_code': fields.char('Supplier Product Code', size=64, help="This supplier's product code will be used when printing a request for quotation. Keep empty to use the internal one."),
'sequence' : fields.integer('Sequence', help="Assigns the priority to the list of product supplier."),
'product_uom': fields.related('product_id', 'uom_po_id', type='many2one', relation='product.uom', string="Supplier Unit of Measure", readonly="1", help="This comes from the product form."),
'product_uom': fields.related('product_template_id', 'uom_po_id', type='many2one', relation='product.uom', string="Supplier Unit of Measure", readonly="1", help="This comes from the product form."),
'min_qty': fields.float('Minimal Quantity', required=True, help="The minimal quantity to purchase to this supplier, expressed in the supplier Product Unit of Measure if not empty, in the default unit of measure of the product otherwise."),
'qty': fields.function(_calc_qty, store=True, type='float', string='Quantity', multi="qty", help="This is a quantity which is converted into Default Unit of Measure."),
'product_id' : fields.many2one('product.template', 'Product', required=True, ondelete='cascade', select=True),
'product_template_id' : fields.many2one('product.template', 'Product Template', required=True, ondelete='cascade', select=True),
'delay' : fields.integer('Delivery Lead Time', required=True, help="Lead time in days between the confirmation of the purchase order and the reception of the products in your warehouse. Used by the scheduler for automatic computation of the purchase order planning."),
'pricelist_ids': fields.one2many('pricelist.partnerinfo', 'suppinfo_id', 'Supplier Pricelist'),
'company_id':fields.many2one('res.company','Company',select=1),