Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to write a function to calculate a percentage in openerp?

By
More9ike
on 3/1/13, 8:36 AM 1,497 views

Hello all, please I have a field called annual_salary and i want to calculate the percentage increase in that field and display the result in another filed called annual_increase. How do I achieve it in OpenERP?

Thanks.

I assume you have the annual salary of the previous year available, am I right? In this case, where do you have it?

Lucio
on 3/1/13, 8:55 AM

Yes i have it in another field called previous_annual_salary.

More9ike
on 3/1/13, 9:45 AM

Thanks for your help but the issue is, I have a field called previous_annual_salary and and a field called new_annual salary. but the new_annual salary must not be greater that 25% of previous_annual_salary. The function should be able to tell you if the new_annual_salary you imputed is greater than 25%. Thanks

More9ike
on 3/1/13, 11:17 AM
0
Lucio
On 3/1/13, 10:08 AM

You should add a constraint in the definition of your model, like this:

def _check_annual_increase(self, cr, uid, ids,context=None):
    delta_salary = obj.previous_annual_salary and\
                        obj.annual_salary / obj.previous_annual_salary
    return (delta_salary < 1,25) 

_columns = { 
    ...
    annual_salary : ...
    previous_annual_salary : ...
    ...
}

_constraints = [
    (_check_annual_increase, ('Error! Annual increase can not exceed 25%'),
]

If none of your salary fields is of type float you need to cast at least one of the before doing the division in _check_annual_increase.

Hope now it solves the issue!

I will try it and get back to you. Thanks for your help!

More9ike
on 3/1/13, 11:55 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/1/13, 8:36 AM
Seen: 1497 times
Last updated: 3/16/15, 8:10 AM