account.invoice add custom computed&过滤字段

时间:2016-09-06 06:11:12

标签: python openerp odoo-8 odoo-9

对不起我的英文

我正在编写一个自定义的Odoo模块,我的目标是在account.invoice中添加一个自定义计算字段,其中包含tax_line_ids金额字段中存储的每个税值的总和(不包括负扣除);这是我的代码:

# -*- coding: utf-8 -*-
from openerp import models, fields, api

class account_invoice(models.Model):
    _inherit = 'account.invoice'
    x_sum_stored_taxes_exclude_withholding = fields.Float('Total Taxes', compute='_compute_total_taxes', digits=(12,2), store=True)

    @api.one
    @api.depends('tax_line_ids.amount')
    def _compute_total_taxes(self):
        for record in self:
                record.x_sum_stored_taxes_exclude_withholding = sum(line.amount for line in record.x_sum_stored_taxes_exclude_withholding)

但是新字段中的结果" x_sum_stored_taxes_exclude_withholding"只填充零。我真的尝试了很多方法而找不到合适的方法!

帮助!! :'(

1 个答案:

答案 0 :(得分:1)

record.x_sum_stored_taxes_exclude_withholding =\
     sum(line.amount for line in record.x_sum_stored_taxes_exclude_withholding)

您应该使用`tax_line_ids?

record.x_sum_stored_taxes_exclude_withholding =\
     sum([line.amount for line in record.tax_line_ids])

当然,你只需要积极的价值观:

record.x_sum_stored_taxes_exclude_withholding =\
     sum([line.amount for line in record.tax_line_ids if line.amount >= 0.0])