0
votes

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.

2

2 Answers

0
votes
def calc_monthly_quota(self, cr, uid, ids, context=None):

   for record in self.browse(cr, uid, ids):
        if record.state :
                result=record.number_of_days_temp/12
   return  self.write(cr, uid, [record.id],{'monthly_quota':result,})

I tried this and this is working fine

0
votes

I send a support email and video to openerp to help me for this issue they send me a mail as a response,

I have tested your issue by applying your code at my end and the reason
that the state is 'To Approve' before you save the record is: In Allocation
Request , whenever a record(allocation request) is created , it will be
directly in 'To Approve' state as per the workflow.

According to workflow, an allocation request when created is always in
state confirm i.e 'To Approve' because a request after creating is to be
submited only so it is directly passed to 'To Approve' state.

Now as in your case, when you click on the button 'Calculate Monthly
Quota',first that record is created and saved in the database to perform
further action on that record and then after browsing that record from
database then you write to that record. So even if you don't click on Save,
the record is actually saved to your database and as the record is created
it will be transfered to state 'To Approve' as per the workflow of holidays.

I am sorry for responding so late, but this is the reply that I got from support.Thanks