Last active
October 16, 2018 23:09
-
-
Save aek/1acc0c0572e237ec1572d7b1ece5cd0a to your computer and use it in GitHub Desktop.
Purchase Order Global Discount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openerp import models, api | |
from openerp.osv import fields, osv | |
import openerp.addons.decimal_precision as dp | |
class PurchaseOrder(models.Model): | |
_inherit = "purchase.order" | |
def _get_order(self, cr, uid, ids, context=None): | |
result = {} | |
for line in self.pool.get('purchase.order.line').browse(cr, uid, ids, context=context): | |
result[line.order_id.id] = True | |
return result.keys() | |
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, | |
'amount_tax': 0.0, | |
'amount_total': 0.0, | |
} | |
val = val1 = 0.0 | |
cur = order.pricelist_id.currency_id | |
for line in order.order_line: | |
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) | |
if order.discount: | |
res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1) * (1 - (order.discount or 0.0) / 100.0) | |
else: | |
res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1) | |
res[order.id]['amount_total']=res[order.id]['amount_untaxed'] + res[order.id]['amount_tax'] | |
return res | |
_columns = { | |
'discount': fields.float( | |
string='Discount (%)', | |
digits_compute=dp.get_precision('Discount') | |
), | |
'amount_untaxed': fields.function( | |
_amount_all, | |
digits_compute=dp.get_precision('Account'), | |
string='Untaxed Amount', | |
store={ | |
'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10), | |
'purchase.order.line': (_get_order, None, 10), | |
}, | |
multi="sums", | |
help="The amount without tax", | |
track_visibility='always' | |
), | |
'amount_tax': fields.function( | |
_amount_all, digits_compute=dp.get_precision('Account'), string='Taxes', | |
store={ | |
'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10), | |
'purchase.order.line': (_get_order, None, 10), | |
}, | |
multi="sums", | |
help="The tax amount" | |
), | |
'amount_total': fields.function( | |
_amount_all, | |
digits_compute=dp.get_precision('Account'), | |
string='Total', | |
store={ | |
'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10), | |
'purchase.order.line': (_get_order, None, 10), | |
}, | |
multi="sums", | |
help="The total amount" | |
), | |
} | |
_sql_constraints = [ | |
('order_discount_limit', 'CHECK (discount <= 100.0)', | |
'Discount must be lower than 100%.'), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<openerp> | |
<data> | |
<record id="purchase_order_discount" model="ir.ui.view"> | |
<field name="name">purchase.order.discount</field> | |
<field name="model">purchase.order</field> | |
<field name="inherit_id" ref="purchase.purchase_order_form" /> | |
<field name="arch" type="xml"> | |
<field name="date_order" position="after"> | |
<field name="discount"/> | |
</field> | |
</field> | |
</record> | |
</data> | |
</openerp> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment