This question has been flagged
2 Replies
4584 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.

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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

Best Answer

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})
Avatar
Discard
Author

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.