Skip to Content
Menu
This question has been flagged
3 Replies
5860 Views
@api.multi
@api.depends
('totalfee')
def _get_details_am(self):
cr = self.env.cr
for record in self:
# if record.appl_id:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
for each in returned_record:
if each['am']:
print each['am']
if record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']
elif record.total_amount < each['am']:
#pass
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')

in the above method
when i click save button ValidationError is displaying and recording is storing in database


please help me.....
thanks in advance
Avatar
Discard
Best Answer

Hi,

Try to use @api.constrains decorator, it will work when saving the record.


@api.constrains('totalfee')
def _get_details_am(self):


Thanks

Avatar
Discard
Best Answer

Hi, 

If I understand you correctly, You need to validate all records first and show an error if the amount less than total amount without saving anything to the DB. If this is the case then you have to validate all records first and raise an error if your condition is met and if all records data is okay then you will save it to the DB. 

If self have only one record you can use the below code:


@api.multi
@api.depends('totalfee')
def _get_details_am(self):
cr = self.env.cr
for record in self:
# if record.appl_id:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
if any(record.total_amount < each['am'] for each in returned_record):
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')
else:
for each in returned_record:
if each['am']:
if record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']


If self has multi record and you want to validate them all before saving them to the DB, Use the below:

@api.multi
@api.depends('totalfee')
def _get_details_am(self):
cr = self.env.cr
# This loop to check all records first and raise an error if
for record in self:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
if any(record.total_amount < each['am'] for each in returned_record):
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')


for record in self:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
for each in returned_record:
if each['am'] and record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']

Avatar
Discard
Best Answer

Add

from odoo.exceptions import ValidationError 

before defining any function took me a while to solve the similar kind of error.

Hope this might help you.



Avatar
Discard
Related Posts Replies Views Activity
2
Jun 24
489
1
Dec 23
574
1
Apr 23
668
1
Mar 23
1250
0
Dec 22
1137