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.

1

How to do calculation using buttons in OEv7?

By
Nishant Kashyap
on 6/19/13, 4:24 AM 1,931 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">
                <data>
                        <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"/>
                        </field>
            </data>
        </field>
    </record>

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':\
    record.number_of_days_temp/12})
    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.

4

Sandeep

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

Hey

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})

Thanks
Sandeep

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
1

Francesco OpenCode

--Francesco OpenCode--
3608
| 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

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

0 follower(s)

Stats

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