Odoo Help


How to do calculation using buttons in OEv7?

Nishant Kashyap
on 6/19/13, 4:24 AM 2,130 views

I am facing a problem to do some simple calculation on the Allocation requests in the leaves section in Menu . I have added a button in the view xml file:

<record model="ir.ui.view" id="view_edit_holiday_allocation_form">
            <field name="name">allocation</field>
            <field name="model">hr.holidays</field>
            <field name="inherit_id" ref="hr_holidays.allocation_leave_new"/>
            <field name="arch" type="xml">
                        <field name="department_id" position="after">
                <field name="monthly_quota"/>
                        <field name="refusal_date"/>
                        <button name="calc_monthly_quota" type="object" string="Calculate Monthly Quota"/>

and in .py file

class hr_holidays(osv.osv):
    _inherit = "hr.holidays"
def calc_monthly_quota(self, cr, uid, ids, context=None):

    for record in self.browse(cr, uid, ids):
        if record.state :
            self.write(cr, uid, [record.id],{'monthly_quota':\
    return True

_columns = {
        "monthly_quota": fields.float("Monthly Quota", readonly=True,
     states={'draft':[('readonly',False)]}, help="If monthly leave \
     limit is more then xero then employee can not take leave more \
     then monthly allocation in total allocation. If monthly quota \
     is zero user can take leave as per his allocation limit."),
        "refusal_date" : fields.date('Date of Refusal'),
        "create_date" : fields.date('Create Date'),

Here I just want to calculate the monthly quota of the leaves on the click of the button . suppose if I enter 12 in allocation(number_of_days_temp) then I should get monthly as 1. Every thing is working just fine as expected , except the state of the record. after the click of the button the state of the record changes from "To Submit" i.e draft to "To Approve" i.e confirm. before saving the form the state of the form changes itself, Ideally the state of the form should only change after we hit the save button. I have read the openerp 7.0 documentation there it says that

After a button has been clicked, the record should always be reloaded.

I did not still get what is required to change the state of the form without saving it. Any comments highly appreciable.



| 6 5 8
Delhi, India
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
On 6/19/13, 7:13 AM


There should not be anything happen for state if you just write this , state will be remain same

         self.write(cr, uid, [record.id],{'monthly_quota': record.number_of_days_temp/12})


yes, thats what I was wondering but this is not happening. If you want I can mail you the module .

Nishant Kashyap
on 6/19/13, 7:37 AM

Francesco OpenCode

--Francesco OpenCode--
| 5 7 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 6/19/13, 5:25 AM

Change this line

self.write(cr, uid, [record.id],{'monthly_quota': record.number_of_days_temp/12})

With this:

self.write(cr, uid, [record.id],{'monthly_quota': record.number_of_days_temp/12, 'state':record.state})

Thanks for the reply Fancesco Apruzzese, but this does not actually solves my problem. Before its comes to def calc_monthly_quota the state of the form changes to "To Approve". If I use pdb.set_trace() above the for loop, the state of the form changes to "To Approve" before it comes to pdb; But the state of the form should change on the form submit , not on the button action.

Nishant Kashyap
on 6/19/13, 5:49 AM

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 6/19/13, 4:24 AM
Seen: 2130 times
Last updated: 3/16/15, 8:10 AM