Odoo 8 override _amount_line

给你一囗甜甜゛ 提交于 2019-12-12 02:46:36

问题


I'm trying to override the _amount_line function in sale.order.line model in order to add custum logic.

Here is my code:

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    def _amount_line(self):
        tax_obj = self.env['account.tax']
        cur_obj = self.env['res.currency']
        res = {}
        for line in self:
            print line.tax_id
            price = self._calc_line_base_price(line)
            qty = self._calc_line_quantity(line)
            print" price:{} & quantity: {}".format(price,qty)
            taxes = tax_obj.compute_all(line.tax_id, price, qty,
                                        line.product_id,
                                        line.order_id.partner_id)
            cur = line.order_id.pricelist_id.currency_id
            res[line.id] = cur_obj.round(cur, taxes['total'])
        return res

    remise_palier = fields.Float('Remise palier (%)')
    remise_total = fields.Float('Remise totale (%)')
    price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')

When i run Odoo, i get that error:


回答1:


You have made mistake in compute_all method calling.

This method should be like this,

@api.multi
def _amount_line(self):
    tax_obj = self.env['account.tax']
    cur_obj = self.env['res.currency']
    for line in self:
        print line.tax_id
        price = self._calc_line_base_price(line)
        qty = self._calc_line_quantity(line)
        print" price:{} & quantity: {}".format(price,qty)
        taxes = line.tax_id.compute_all(price, qty, line.product_id, line.order_id.partner_id)        
        cur = line.order_id.pricelist_id.currency_id
        line.price_subtotal = cur.round(taxes['total'])



回答2:


In Odoo V8+ the compute_all call should be on the taxes itself. You don't need to call it as "class method". Following snippet should work for you:

@api.multi
# @api.depends() use it for recomputation triggers
def _amount_line(self):
    for line in self:
        print line.tax_id
        price = self._calc_line_base_price(line)
        qty = self._calc_line_quantity(line)
        print" price:{} & quantity: {}".format(price,qty)
        taxes = line.tax_id.compute_all(
            price, qty, line.product_id, line.order_id.partner_id)
        cur = line.order_id.pricelist_id.currency_id
        line.price_subtotal = cur.round(taxes['total'])

Edit: You don't need to return something on new API computed field functions. Just assign the values to the computed field(s).

Edit2: There is a new API style method for currency round(), too. I've changed my snippet.



来源:https://stackoverflow.com/questions/39754720/odoo-8-override-amount-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!