[FIX]purchase: add lead time on purchase order line

bzr revid: csn@openerp.com-20130603103019-gxurr7m3qn1ik0y1
This commit is contained in:
Cedric Snauwaert 2013-06-03 12:30:19 +02:00
parent 88a77ec6d5
commit 40b4ccea0a
2 changed files with 38 additions and 11 deletions

View File

@ -867,6 +867,24 @@ class purchase_order_line(osv.osv):
res[line.id] = cur_obj.round(cr, uid, cur, taxes['total'])
return res
def _set_lead_time(self, cr, uid, ids, name, value, arg, context=None):
if not value: return False
date_planned = self.browse(cr, uid, ids, context=context)[0].date_planned
date = _get_date_planned(self, cr, uid, False, date_planned, value, context=None)
self.write(cr, uid, ids, {'lead_time': value, 'date_planned': date}, context=context)
return True
def _get_product_lead_time(self, cr, uid, ids, field_name, arg, context=None):
res={}
products = self.browse(cr, uid, ids, context=context)
for product in products:
supplierdelay = 0
for supplier in product.product_id.seller_ids:
if product.partner_id and (supplier.name.id == product.partner_id):
supplierdelay = float(supplier.delay)
res[product.id] = float(product.lead_time) or supplierdelay
return res
_columns = {
'name': fields.text('Description', required=True),
'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
@ -891,12 +909,14 @@ class purchase_order_line(osv.osv):
'partner_id': fields.related('order_id','partner_id',string='Partner',readonly=True,type="many2one", relation="res.partner", store=True),
'date_order': fields.related('order_id','date_order',string='Order Date',readonly=True,type="date"),
'lead_time': fields.float('Delivery Lead Time', help="Average delay in days to deliver this product."),
'product_lead_time': fields.function(_get_product_lead_time, fnct_inv=_set_lead_time, type='float', string='Delivery Lead Time', help="Average delay in days to deliver this product.")
}
_defaults = {
'product_qty': lambda *a: 1.0,
'state': lambda *args: 'draft',
'invoiced': lambda *a: 0,
'lead_time': 0,
}
_table = 'purchase_order_line'
_name = 'purchase.order.line'
@ -910,7 +930,7 @@ class purchase_order_line(osv.osv):
def onchange_product_uom(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, context=None):
name=False, price_unit=False, lead_time=False, context=None):
"""
onchange handler of product_uom.
"""
@ -920,7 +940,7 @@ class purchase_order_line(osv.osv):
partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned,
name=name, price_unit=price_unit, context=context)
def _get_date_planned(self, cr, uid, supplier_info, date_order_str, context=None):
def _get_date_planned(self, cr, uid, supplier_info, date_order_str, lead_time, context=None):
"""Return the datetime value to use as Schedule Date (``date_planned``) for
PO Lines that correspond to the given product.supplierinfo,
when ordered at `date_order_str`.
@ -933,6 +953,8 @@ class purchase_order_line(osv.osv):
:return: desired Schedule Date for the PO line
"""
supplier_delay = int(supplier_info.delay) if supplier_info else 0
if lead_time:
supplierdelay = lead_time
return datetime.strptime(date_order_str, DEFAULT_SERVER_DATE_FORMAT) + relativedelta(days=supplier_delay)
def _check_product_uom_group(self, cr, uid, context=None):
@ -943,7 +965,7 @@ class purchase_order_line(osv.osv):
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, context=None):
name=False, price_unit=False, lead_time=False, context=None):
"""
onchange handler of product_id.
"""
@ -1011,12 +1033,16 @@ class purchase_order_line(osv.osv):
if qty:
res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier has a minimal quantity set to %s %s, you should not purchase less.') % (supplierinfo.min_qty, supplierinfo.product_uom.name)}
qty = min_qty
dt = self._get_date_planned(cr, uid, supplierinfo, date_order, context=context).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
dt = self._get_date_planned(cr, uid, supplierinfo, date_order, lead_time, context=context).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
qty = qty or 1.0
res['value'].update({'date_planned': date_planned or dt})
if qty:
res['value'].update({'product_qty': qty})
# - determine lead time value for product
product_lead_time = lead_time or supplierinfo.delay if supplierinfo else 0
res['value'].update({'product_lead_time': product_lead_time})
# - determine price_unit and taxes_id
if pricelist_id:
price = product_pricelist.price_get(cr, uid, [pricelist_id],

View File

@ -206,14 +206,14 @@
<page string="Purchase Order">
<field name="order_line">
<tree string="Purchase Order Lines" editable="bottom">
<field name="product_id" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)"/>
<field name="product_id" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)"/>
<field name="name"/>
<field name="date_planned"/>
<field name="lead_time"/>
<field name="product_lead_time" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit, product_lead_time, context)" />
<field name="company_id" groups="base.group_multi_company" widget="selection"/>
<field name="account_analytic_id" groups="purchase.group_analytic_accounting" domain="[('type','not in',('view','template'))]"/>
<field name="product_qty" on_change="onchange_product_id(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id,parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)"/>
<field name="product_uom" groups="product.group_uom" on_change="onchange_product_uom(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)"/>
<field name="product_qty" on_change="onchange_product_id(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id,parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)"/>
<field name="product_uom" groups="product.group_uom" on_change="onchange_product_uom(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)"/>
<field name="price_unit"/>
<field name="taxes_id" widget="many2many_tags" domain="[('parent_id','=',False),('type_tax_use','!=','sale')]"/>
<field name="price_subtotal"/>
@ -392,17 +392,18 @@
<sheet>
<group>
<group>
<field name="product_id" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)"/>
<field name="product_id" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)"/>
<label for="product_qty"/>
<div>
<field name="product_qty" on_change="onchange_product_id(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id,parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)" class="oe_inline"/>
<field name="product_uom" groups="product.group_uom" on_change="onchange_product_uom(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)" class="oe_inline"/>
<field name="product_qty" on_change="onchange_product_id(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id,parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)" class="oe_inline"/>
<field name="product_uom" groups="product.group_uom" on_change="onchange_product_uom(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,product_lead_time,context)" class="oe_inline"/>
</div>
<field name="price_unit"/>
</group>
<group>
<field name="taxes_id" widget="many2many_tags" domain="[('parent_id','=',False),('type_tax_use','!=','sale')]"/>
<field name="date_planned" widget="date"/>
<field name="product_lead_time" />
<field name="account_analytic_id" colspan="2" groups="purchase.group_analytic_accounting"/>
<field name="company_id" groups="base.group_multi_company" widget="selection"/>
</group>