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

Inverse Function ind Odoo 9

By
Judith
on 7/25/16, 12:38 PM 268 views

[Odoo 9]

I made an extension for the hr.expense module, which calculates the additional meal allowance an employee gets when he went on a business trip. 

I have a main model, which inherits hr.employee. The calculations are done in this model after the employee entered a few informations. This models has a one2many field in it that refers to an other model.

That other model, expenses.summary, is shown in a tree view under the main page of the hr.expense original module and is  automatically filled with the details of the calculation for every day of the business trip . I managed all right the compute function to fill in expenses.summary automatically and to calculate the allowance automatically (in one function). 

The simplified table for expenses.summary looks like this:

Date
Time out of office
Destination of trip
(drop down menu with countries, where you also find infos like allowance for the selected country)
breakfast paid for you by employer (boolean)
amount (computed field)

But i want this tree view of expense.summary to be editable, like so:

if i change the destination or check the box for breakfast paid, I'd like the amount to be automatically recalculated.

I understand i have to do this with an inverse function, since the whole table is automatically computed (automatic fill in with @api.depends). My problem is that i cant find any example for complex use of the inverse function.


Does someone here have any experience with the use of the inverse function in order to change the content of a table depending of the content of some fields in this table?


I'll post some of the code tomorrow, i do not have it on the computer i'm using at the moment

Auditum AG, Judith
on 7/25/16, 3:00 PM
0
Judith
On 7/26/16, 6:11 AM

 class Expenses_countries(models.Model): #table flat rate for business trips, sorted by destination country

_name = 'expenses.countries'

name = fields.Char('Country')

short_stay = fields.Float('From 8 to 24 Hours') #for arrival & departure days and for stays that are longer than 8hrs but no longer than 24 hrs

long_stay = fields.Float('At least 24 Hours') #for stays that are longer than 24 hrs

night = fields.Float('Flat rate for one overnight stay') #for the hotel in case the hotel stay is paid by the employer

class Expenses_summary(models.Model):

_name = 'expenses.summary'

day = fields.Date('Date', readonly = 'True')

time = fields.Char('Time away', readonly = 'True')

destination_ids = fields.Many2one('expenses.countries', string='Destination', store='True')

breakfast = fields.Boolean('Breakfast paid by employer?', default='True')

lunch = fields.Boolean('Lunch paid by employer?')

supper = fields.Boolean('Supper paid by employer?')

night = fields.Boolean('Hotel paid by employer?')

amount_due = fields.Float('Amount', readonly = 'True')

expenses_detail_ids = fields.Many2one('expenses.details', string = 'Expense Line')

class Expenses(models.Model):

_inherit = 'hr.expense'

#

#Case: Only one business trip, no previous or no following trip

arrival_date = fields.Datetime('First day')

departure_date = fields.Datetime('Last day')

destination_ids = fields.Many2one('expenses.countries', string = 'Destination', store = 'True', required = 'True')

breakfast = fields.Boolean('Breakfast paid by employer?', default='True')

lunch = fields.Boolean('Lunch paid by employer?')

supper = fields.Boolean('Supper paid by employer?')

night = fields.Boolean('Hotel paid by employer?')

expenses_summary_id = fields.One2many('expenses.summary','expenses_detail_ids', compute = '_set_details', inverse = '_change_details')

#Write expenses in table (Model: expenses_summary_ids)

i = 0

[...]

for day in range(d):

[...]

self.expenses_summary_id |= self.env['expenses.summary'].create({'day': day_x,

'time': time_gone,

'destination_ids': dest,

'lunch': self.lunch,

'supper': self.supper,

'night': self.night,

'amount_due': amount})

i += 1



@api.depends('destination_ids', 'previous_country', 'breakfast', 'lunch', 'supper', 'night') 'breakfast_previous', 'lunch_previous', 'supper_previous', 'night_previous','last_day')

 

def _calculate_allowance(self, i, d, start_date, end_date, tgone):

#temp values are used to calculate the deductions from the allowance for meals paid for by the employer

temp1 = 0.0

temp2 = 0.0

temp3 = 0.0

temp4 = 0.0

if start_date == end_date: # 1 day trip

t = datetime.strptime('8:00:00', '%H:%M:%S')

t = timedelta(hours = t.hour, minutes = t.minute)

if tgone < t : #trip shorter that 8hrs

amount = 0

return amount

else:#trip longer than 8hrs

amount = self.destination_ids.short_stay

return amount

else: #trip that lasts more than 1 day

if i == 1: #first day of trip

#amount if the first day really is the first day of the trip

amount = self.destination_ids.short_stay

if self.breakfast:

temp1 = 0.2*amount

if self.lunch:

temp2 = 0.4*amount

if self.supper:

temp3 = 0.4*amount

if self.night:

temp4 = amount + self.destination_ids.night

amount = amount - temp1 - temp2 - temp3 + temp4

if amount < 0:

amount = 0

return amount


#Making the tree view "expenses_detail_ids" editable

@api.one

@api.onchange('expenses_detail_ids.destination_ids', 'expenses_detail_ids.breakfast', 'expenses_detail_ids.lunch',

'expenses_detail_ids.supper', 'expenses_detail_ids.night')

def _change_details(self):

logging.warning('INVERSE CALLED')

temp1 = 0.0

temp2 = 0.0

temp3 = 0.0

temp4 = 0.0

if start_date == end_date: # 1 day trip

t = datetime.strptime('8:00:00', '%H:%M:%S')

t = timedelta(hours = t.hour, minutes = t.minute)

if tgone < t : #trip shorter that 8hrs

amount = 0

return amount

else:#trip longer than 8hrs

amount = self.expenses_detail_ids.destination_ids.short_stay

return amount

else: #trip that lasts more than 1 day

if i == 1: #first day of trip

if self.first_day: #amout if the first day is a day back from a previous trip

amount = self.previous_country.short_stay

night = self.previous_country.night

if amount < self.expenses_detail_ids.destination_ids.short_stay:

amount = self.expenses_detail_ids.destination_ids.short_stay

if self.breakfast_previous:

temp1 = 0.2*amount

if self.lunch_previous:

temp2 = 0.4*amount

if self.supper_previous:

temp3 = 0.4*amount

if self.night_previous:

temp4 = amount + self.expenses_detail_ids.destination_ids.night

amount = amount - temp1 - temp2 - temp3 + temp4

if amount < 0:

amount = 0

return amount

 

self.expenses_summary_id.amount_due = amount

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

3 follower(s)

Stats

Asked: 7/25/16, 12:38 PM
Seen: 268 times
Last updated: 7/26/16, 6:11 AM