From 053911edca44a8e372ec30eb90a77451cf191950 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 23 Jun 2015 20:01:17 +0200 Subject: [PATCH] Up-port from 7 to 8 new stuff for hr_holidays_usability --- hr_holidays_usability/hr_holidays.py | 77 ++++++++++++------- hr_holidays_usability/hr_holidays_view.xml | 41 +++++++--- .../wizard/hr_holidays_mass_allocation.py | 6 +- .../wizard/hr_holidays_post.py | 23 +++--- 4 files changed, 97 insertions(+), 50 deletions(-) diff --git a/hr_holidays_usability/hr_holidays.py b/hr_holidays_usability/hr_holidays.py index 85ec30a..4cb2318 100644 --- a/hr_holidays_usability/hr_holidays.py +++ b/hr_holidays_usability/hr_holidays.py @@ -50,12 +50,16 @@ class HrHolidays(models.Model): # For allocation (type = add), we don't change anything: # The user writes in the field number_of_days_temp and # number_of_days = number_of_days_temp -# For leave (type = remove), we don't let users enter the number of days, -# we compute it for them +# IN 7.0: For leave (type = remove), we don't let users enter the number +# of days, we compute it for them # -> new computed field "number_of_days_remove' that compute the number # of days depending on the computation method defined on 'type' # Redefine the field 'number_of_days' to take into accout # 'number_of_days_remove' when type == remove (= number_of_days_remove * -1) +# IN 8.0: for leaves, use the on_change on the dates to set the +# number_of_days_temp. But we want to have this field readonly, so we also +# inherit write + create (even in v8, readonly fields are not written in DB) + # change date time fields by date + selection morning/noon # How do we set the dates : @@ -74,11 +78,7 @@ class HrHolidays(models.Model): # 1 for 'unpaid leaves' + repos compensateur + congés conventionnels + maladie # 1 or 2 for normal holidays - @api.one - @api.depends( - 'vacation_date_from', 'vacation_time_from', 'vacation_date_to', - 'vacation_time_to', 'number_of_days_temp', 'type', 'holiday_type', - 'holiday_status_id.vacation_compute_method') + @api.model def _compute_number_of_days(self): # depend on the holiday_status_id hhpo = self.env['hr.holidays.public'] @@ -89,7 +89,8 @@ class HrHolidays(models.Model): self.vacation_date_from and self.vacation_time_from and self.vacation_date_to and - self.vacation_time_to): + self.vacation_time_to and + self.vacation_date_from <= self.vacation_date_to): if self.holiday_status_id.vacation_compute_method == 'business': business = True else: @@ -145,19 +146,7 @@ class HrHolidays(models.Model): days -= 0.5 break date_dt += relativedelta(days=1) - - # PASTE - if self.type == 'remove': - # read number_of_days_remove instead of number_of_days_temp - number_of_days = days * -1 - number_of_days_remove = days - else: - # for allocations, we read the native field number_of_days_temp - number_of_days = self.number_of_days_temp - number_of_days_remove = 0 - - self.number_of_days = number_of_days - self.number_of_days_remove = number_of_days_remove + return days @api.one @api.depends('holiday_type', 'employee_id', 'holiday_status_id') @@ -169,8 +158,7 @@ class HrHolidays(models.Model): self.holiday_type == 'employee' and self.employee_id and self.holiday_status_id): - days = self.holiday_status_id.get_days( - self.employee_id.id, False) + days = self.holiday_status_id.get_days(self.employee_id.id) total_allocated_leaves =\ days[self.holiday_status_id.id]['max_leaves'] current_leaves_taken =\ @@ -205,11 +193,6 @@ class HrHolidays(models.Model): default='evening', help="For example, if you leave one full calendar week, " "the end of vacation is Friday Evening") - number_of_days_remove = fields.Float( - compute='_compute_number_of_days', - string="Number of Days of Vacation", readonly=True) - # number_of_days is a native field that I inherit - number_of_days = fields.Float(compute='_compute_number_of_days') current_leaves_taken = fields.Float( compute='_compute_current_leaves', string='Current Leaves Taken', readonly=True) @@ -223,6 +206,7 @@ class HrHolidays(models.Model): related='holiday_status_id.limit', string='Allow to Override Limit') posted_date = fields.Date( string='Posted Date', track_visibility='onchange') + number_of_days_temp = fields.Float(string="Number of days") @api.one @api.constrains( @@ -306,9 +290,46 @@ class HrHolidays(models.Model): datetime_dt.astimezone(pytz.utc)) self.date_to = datetime_str + @api.onchange( + 'vacation_date_from', 'vacation_time_from', 'vacation_date_to', + 'vacation_time_to', 'number_of_days_temp', 'type', 'holiday_type', + 'holiday_status_id') + def leave_number_of_days_change(self): + if self.type == 'remove': + days = self._compute_number_of_days() + self.number_of_days_temp = days + + # Neutralize the native on_change on dates + def onchange_date_from(self, cr, uid, ids, date_to, date_from): + return {} + + def onchange_date_to(self, cr, uid, ids, date_to, date_from): + return {} + # in v8, no more need to inherit check_holidays() as I did in v8 # because it doesn't use number_of_days_temp + # I want to set number_of_days_temp as readonly in the view of leaves + # and, even in v8, we can't write on a readonly field + # So I inherit write and create + @api.model + def create(self, vals): + obj = super(HrHolidays, self).create(vals) + if obj.type == 'remove': + days = obj._compute_number_of_days() + obj.number_of_days_temp = days + return obj + + @api.multi + def write(self, vals): + res = super(HrHolidays, self).write(vals) + for obj in self: + if obj.type == 'remove': + days = obj._compute_number_of_days() + if days != obj.number_of_days_temp: + obj.number_of_days_temp = days + return res + class ResCompany(models.Model): _inherit = 'res.company' diff --git a/hr_holidays_usability/hr_holidays_view.xml b/hr_holidays_usability/hr_holidays_view.xml index c353716..6c92eae 100644 --- a/hr_holidays_usability/hr_holidays_view.xml +++ b/hr_holidays_usability/hr_holidays_view.xml @@ -27,10 +27,7 @@ hr_holidays.edit_holiday_new is used for both leaves and allocation --> 1 - {'invisible': [('type', '=', 'remove')]} - - - + {'readonly': [('type', '=', 'remove')]} @@ -69,12 +66,6 @@ hr_holidays.edit_holiday_new is used for both leaves and allocation --> hr.holidays - - 1 - - - - 1 @@ -98,6 +89,29 @@ hr_holidays.edit_holiday_new is used for both leaves and allocation --> + + hr_holidays_usability.leave_request_tree + hr.holidays + + + + + + + + + + hr_holidays_usability.hr_holiday_graph + hr.holidays + + + + + + + + + hr_holidays_usability.hr.holidays.status.form hr.holidays.status @@ -122,7 +136,14 @@ hr_holidays.edit_holiday_new is used for both leaves and allocation --> + + + + + graph + + diff --git a/hr_holidays_usability/wizard/hr_holidays_mass_allocation.py b/hr_holidays_usability/wizard/hr_holidays_mass_allocation.py index 227e063..e631618 100644 --- a/hr_holidays_usability/wizard/hr_holidays_mass_allocation.py +++ b/hr_holidays_usability/wizard/hr_holidays_mass_allocation.py @@ -28,11 +28,13 @@ class HrHolidaysMassAllocation(models.TransientModel): _name = 'hr.holidays.mass.allocation' _description = 'Wizard for mass allocation of holidays' + @api.model def _get_all_employees(self): - return self.pool['hr.employee'].search([]) + return self.env['hr.employee'].search([]) + @api.model def _get_default_holiday_status(self): - res = self._user.company_id.\ + res = self.env.user.company_id.\ mass_allocation_default_holiday_status_id or False return res diff --git a/hr_holidays_usability/wizard/hr_holidays_post.py b/hr_holidays_usability/wizard/hr_holidays_post.py index 952a4ac..68e681d 100644 --- a/hr_holidays_usability/wizard/hr_holidays_post.py +++ b/hr_holidays_usability/wizard/hr_holidays_post.py @@ -50,7 +50,7 @@ class HrHolidaysPost(models.TransientModel): @api.multi def select_date(self): self.ensure_one() - hols = self.pool['hr.holidays'].search([ + hols = self.env['hr.holidays'].search([ ('type', '=', 'remove'), ('holiday_type', '=', 'employee'), ('state', '=', 'validate'), @@ -69,18 +69,21 @@ class HrHolidaysPost(models.TransientModel): @api.multi def run(self): self.ensure_one() + # I have to make a copy of self.holidays_to_post_ids in a variable + # because, after the write, it doesn't have a value any more !!! + holidays_to_post = self.holidays_to_post_ids today = fields.Date.context_today(self) if not self.holidays_to_post_ids: raise Warning( _('No leave request to post.')) self.holidays_to_post_ids.write({'posted_date': today}) - # On v8, return a graph view ! - action = self.env['ir.actions.act_window'].for_xml_id( - 'hr_holidays', 'open_ask_holidays') - action.update({ - 'target': 'current', - 'domain': [('id', 'in', self.holidays_to_post_ids.ids)], - 'nodestroy': True, - 'context': {'group_by': ['employee_id', 'holiday_status_id']}, - }) + view_id = self.env.ref('hr_holidays_usability.hr_holiday_graph').id + action = { + 'name': _('Leave Requests'), + 'res_model': 'hr.holidays', + 'type': 'ir.actions.act_window', + 'domain': [('id', 'in', holidays_to_post.ids)], + 'view_mode': 'graph', + 'view_id': view_id, + } return action