在Odoo 8我的代码有什么问题?

时间:2017-06-13 10:21:37

标签: python python-2.7 openerp odoo-8

我在discount

中添加了一个新字段account.invoice.line
_inherit = "account.invoice.line"

discount = fields.Float(string='Discount (%)',
                        digits=(16, 10),
                        # digits= dp.get_precision('Discount'),
                        default=0.0)

现在每行的逻辑price_subtotal = price_unit* qty *(1-discount)。 在另一个班级account.invoice中,我发现了计算amount_totalamount_tax

的函数
_inherit = "account.invoice"

@api.one
@api.depends('invoice_line.price_subtotal' ,'tax_line.amount')
def _compute_amount(self):
    disc = 0.0
    for inv in self:
        for line in inv.invoice_line:
            disc += (line.quantity * line.price_unit) * (line.discount) / 100
    self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
    self.amount_tax = sum(line.amount*self.change_rate for line in self.tax_line)
    self.amount_discount = disc
    self.amount_total = self.amount_untaxed + self.amount_tax

问题是如果我在某行中更改折扣价值,发票的amoun_tax不会改变

提前致谢

2 个答案:

答案 0 :(得分:0)

折扣字段添加到 @ api.depends 中,以便在更新折扣字段时,您的方法将按照您的逻辑执行并计算金额。

尝试使用以下代码:

@api.one
@api.depends('invoice_line.price_subtotal' ,'tax_line.amount', 'invoice_line.discount')
def _compute_amount(self):
    disc = 0.0
    for inv in self:
        for line in inv.invoice_line:
            disc += (line.quantity * line.price_unit) * (line.discount) / 100
    self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
    self.amount_tax = sum(line.amount*self.change_rate for line in self.tax_line)
    self.amount_discount = disc
    self.amount_total = self.amount_untaxed + self.amount_tax

答案 1 :(得分:0)

经过艰苦搜索并尝试后,我找到了amount_tax无法正确计算的原因,并且我只编辑了一行计算同一函数中的税费_compute_amount account.invoice

@api.one
@api.depends('invoice_line.price_subtotal',  'invoice_line.discount', 'tax_line.amount')
def _compute_amount(self):
    disc = 0.0
    for inv in self:
        for line in inv.invoice_line:
            disc += (line.quantity * line.price_unit) * (line.discount) / 100
    self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
    self.amount_tax = sum(line.price_subtotal*(sum(tax.amount for tax in line.invoice_line_tax_id)) for line in self.invoice_line)
    self.amount_discount = disc
    self.amount_total = self.amount_untaxed + self.amount_tax