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
在重写create
方法中,当我比较字段amount
和balance
时,
balance
值为False。它不是compute=_get_balance
方法
对不起我的英语:)
答案 0 :(得分:0)
在创建中,self只是一个空模型,因此每个字段都应为False:
试试这个:
@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
但我认为使用约束来解决这类问题更好:
# 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."))