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 :)