Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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%.'),
- ]
Add Comment
Please, Sign In to add comment