计算字段没有更新它自我odoo 8

时间:2016-04-05 18:34:27

标签: python python-2.7 openerp

account.report类中的计算字段(余额)具有store = True parm并且它取决于accounts.accounts.entries.balance,它应该在依赖字段的余额更改时更改,如果添加journal.entries则它会起作用手动记录,但如果它是通过exmaple createjournal()函数创建的,则account.type和account.report中的余额不会更新。任何解决get_balance函数的解决方案。

@api.multi
def createjournal(self):
    journal = self.env['journal.entries']
    journal.sudo().create({'name': "new record"})

class JournalEntries(models.Model):
    _name = 'journal.entries'

    debit = fields.Float()
    credit = fields.Float()
    balance = fields.Float(compute="get_balance", store=True)


    @api.depends('credit','debit')
    def get_balance(self):
        for x in self:
            balance = x.debit - x.credit
            x.balance = abs(balance)

class ErpAccount(models.Model):
    _name = 'erp.account'

    entries = fields.One2many('journal.entries','account')
    balance = fields.Float(compute="get_balance")
    debit = fields.Float(compute="get_debit")
    credit = fields.Float(compute="get_credit")

    @api.multi
    @api.depends('debit','credit')
    def get_balance(self):
        for x in self:
            balance = x.debit - x.credit
            x.balance = abs(balance)


    @api.multi
    def get_debit(self):
        for x in self:
            x.debit = sum(line.debit for line in x.entries)


    @api.multi
    def get_credit(self):
        for x in self:
            x.credit = sum(line.credit for line in x.entries)

class AccountType(models.Model):
    _name = 'account.type'

    accounts = fields.One2many('erp.account','account_type')
    balance = fields.Float(compute="get_balance")
    debit = fields.Float(compute="get_debit")
    credit = fields.Float(compute="get_credit")


    @api.multi
    @api.depends('accounts.entries.balance')
    def get_balance(self):
        for x in self:
            x.balance = sum(line.balance for line in x.accounts)


    @api.multi
    def get_debit(self):
        for x in self:
            x.debit = sum(line.debit for line in x.accounts)


    @api.multi
    def get_credit(self):
        for x in self:
            x.credit = sum(line.credit for line in x.accounts)


class AccountReportName(models.Model):
    _name = 'account.report'


    types = fields.One2many('account.type','report_account')
    balance = fields.Float(compute="get_balance", store=True)
    debit = fields.Float(compute="get_debit")
    credit = fields.Float(compute="get_credit")

    @api.multi
    @api.depends('types.accounts.entries.balance')
    def get_balance(self):
        for x in self:
            x.balance = sum(line.balance for line in x.types)


    @api.multi
    def get_debit(self):
        for x in self:
            x.debit = sum(line.debit for line in x.types)


    @api.multi
    def get_credit(self):
        for x in self:
            x.credit = sum(line.credit for line in x.types)

2 个答案:

答案 0 :(得分:0)

如果您希望更新余额,则应删除store=True 因为只有在您编辑journal.entries记录时才会更新。

答案 1 :(得分:0)

问题解决了我在erp.account上添加@ api.depends(' entries.balance',' entries.active'),它运行正常。