From 617ef49959d027fab52e2be74aa4c0dc3ce60e30 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 31 Jul 2014 09:07:01 +0200 Subject: [PATCH] [IMP] purchase: Hook for the base price of a purchase line To be able to change the logic without copying large chunk of code Closes #1468 --- addons/purchase/purchase.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 3841bec12eb..c5f985f8b82 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -37,6 +37,7 @@ class purchase_order(osv.osv): def _amount_all(self, cr, uid, ids, field_name, arg, context=None): res = {} cur_obj=self.pool.get('res.currency') + line_obj = self.pool['purchase.order.line'] for order in self.browse(cr, uid, ids, context=context): res[order.id] = { 'amount_untaxed': 0.0, @@ -46,8 +47,14 @@ class purchase_order(osv.osv): val = val1 = 0.0 cur = order.pricelist_id.currency_id for line in order.order_line: - val1 += line.price_subtotal - for c in self.pool.get('account.tax').compute_all(cr, uid, line.taxes_id, line.price_unit, line.product_qty, line.product_id, order.partner_id)['taxes']: + val1 += line.price_subtotal + line_price = line_obj._calc_line_base_price(cr, uid, line, + context=context) + line_qty = line_obj._calc_line_quantity(cr, uid, line, + context=context) + for c in self.pool['account.tax'].compute_all( + cr, uid, line.taxes_id, line_price, line_qty, + line.product_id, order.partner_id)['taxes']: val += c.get('amount', 0.0) res[order.id]['amount_tax']=cur_obj.round(cr, uid, cur, val) res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1) @@ -1020,12 +1027,34 @@ class purchase_order(osv.osv): class purchase_order_line(osv.osv): + def _calc_line_base_price(self, cr, uid, line, context=None): + """Return the base price of the line to be used for tax calculation. + + This function can be extended by other modules to modify this base + price (adding a discount, for example). + """ + return line.price_unit + + def _calc_line_quantity(self, cr, uid, line, context=None): + """Return the base quantity of the line to be used for the subtotal. + + This function can be extended by other modules to modify this base + quantity (adding for example offers 3x2 and so on). + """ + return line.product_qty + def _amount_line(self, cr, uid, ids, prop, arg, context=None): res = {} cur_obj=self.pool.get('res.currency') tax_obj = self.pool.get('account.tax') for line in self.browse(cr, uid, ids, context=context): - taxes = tax_obj.compute_all(cr, uid, line.taxes_id, line.price_unit, line.product_qty, line.product_id, line.order_id.partner_id) + line_price = self._calc_line_base_price(cr, uid, line, + context=context) + line_qty = self._calc_line_quantity(cr, uid, line, + context=context) + taxes = tax_obj.compute_all(cr, uid, line.taxes_id, line_price, + line_qty, line.product_id, + line.order_id.partner_id) cur = line.order_id.pricelist_id.currency_id res[line.id] = cur_obj.round(cr, uid, cur, taxes['total']) return res