Reputation: 3348
I am trying to calculate the interest of a payment depending of the amountPay
and the interestType
fields that the user completes on a form. When the user hits submit I need to insert in the interest field lets say amountPay * 0.05
.
The problem is that as interest
is a function field
and it searches for the value of the current amountPay
and interestType
records but it fails because those values haven't been saved yet.
I noticed that if after saving I edit the field and save it again then I get the correct interest.
Is there any way I can do this on the fly just by hitting submit 1 time?
Any tip in the right direction will be much appreciated, thanks!
Upvotes: 0
Views: 95
Reputation: 2499
If I understand you correctly, you have the amountPay and interestType fields on the form as entry fields. If interest is a functional field then each time the form is displayed (when loaded, after a save etc), the calculated functional field value is displayed, but you can use on_change to do what you need.
Add an on_change to both the amountPay and interestType fields. This can call the same on_change method passing in those two fields.
In the on_change method, calculate the correct interest and return it like this.
def on_change_interest_fields(self, cr, uid, ids, amountPay, interestType, context):
interest = amountPay * 0.5
return {'value': {'interest': interest,},}
Although the interest field is a readonly functional field, the value held in the form will be changed to the value returned in the value dictionary. Then when the use saves or reloads the form, the functional field will be properly calculated and displayed. The only downside of this method is you are duplicating the logic used by the functional field. To solve that, pull the logic out of the method called by the functional field into a separate method you can call from both the functional field method and the on_change method.
One last point minor point, remember python isn't java. To follow PEP008 and OpenERP convention, your fields should be amount_pay and interest_type.
Upvotes: 1