2
votes
class aa_pettycash(models.Model):
    _name = "aa.pettycash"

    amount = fields.Float('Paid Amount', store=True)
    balance = fields.Float(compute=_get_balance, string='Balance', readonly=True, store=True)

    @api.multi
    @api.depends('journal_id')
    def _get_balance(self):
        aa_obj = self.env['account.analytic.account']
        start_date = fields.Date.context_today(self)
        for rec in self:
            bank_balance = 0
            today_balance = 0
            branch_id = rec.branch_id
            if rec.journal_id:            
                sql_query = ''
                if branch_id:
                    analytic_branch = aa_obj.search([('segmen','=',3),('branch_id','=',branch_id.id),('type','=','normal'),('state','not in',('close','cancelled'))])
                    analytic_cc = aa_obj.search([('segmen','=',4),('type','=','normal'),('state','not in',('close','cancelled')),('parent_id','child_of',analytic_branch.ids)])
                    sql_query = ' AND l.analytic_account_id in %s' % str(tuple(analytic_cc.ids)).replace(',)', ')')
                bank_balance = rec.journal_id.default_credit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=True,sql_query=sql_query).balance or rec.journal_id.default_debit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=True,sql_query=sql_query).balance
                today_balance = rec.journal_id.default_credit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=False,sql_query=sql_query).balance or rec.journal_id.default_debit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=False,sql_query=sql_query).balance
            rec.balance = bank_balance + today_balance
        return True

    @api.model
    def create(self,vals,context=None):

        if vals['amount']>self.balance:
            raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))

        ....................................
        ...................................

        return value

In overridden create method, when I compare the fields amount and balance, the balance value is False. It is not the value in compute=_get_balance method

nb: sorry for my english :)

1
couldn't get your problem...qwerty
in code ' if vals['amount']>self.balance: ' value at self.balance is False, and always return True i want get balance value by compute _get_balance methodArif Mustaqim
return true from _get_balance seems redundant. You may want to try putting a breakpoint/print in _getbalance to confirm if it is indeed being called.Himanshu

1 Answers

0
votes

In create self is just an empty model so every field should be False:

Try this:

 @api.model
 def create(self):
    rec = super(YourClass, self).create(vals)
    # rec is a record
    if rec.amount > rec.balance:
        raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))
    return rec

But i think using constrains for this kind of problem is better:

# no need to override create at all
@api.constrains('amount', 'balance')
def check_amount(self):
    for rec in self:
        if rec.amount > rec.balance:
            raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))