问题
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