From 24835fd423c2fc0f75d8d5713b14dc04552d8fb7 Mon Sep 17 00:00:00 2001 From: Hitesh Trivedi Date: Thu, 4 Dec 2014 17:00:33 +0530 Subject: [PATCH] [IMP] hr_holidays: when searching on holidays.status with an employee_id in context and no specific order, order on limited leaves then on remaining leaves. This is usefull for users having their normal leaves being displayed in top of the list (legal leaves, compensatory leaves, then sick and unpaid for example). Also updated the name_get of holidays status, displaying (remaining leaves / max leaves) instead of (taken leaves / max leaves). What is important for users is the number of remaining leaves, not the number of leaves already taken in that type. Also updated and reindented demo data (compensatory is now limited + order of declaration due to this change). --- addons/hr_holidays/hr_holidays.py | 31 ++++++++-- addons/hr_holidays/hr_holidays_data.xml | 7 ++- addons/hr_holidays/hr_holidays_demo.xml | 82 ++++++++++++------------- 3 files changed, 72 insertions(+), 48 deletions(-) diff --git a/addons/hr_holidays/hr_holidays.py b/addons/hr_holidays/hr_holidays.py index 4c190a69800..287f2d8eeef 100644 --- a/addons/hr_holidays/hr_holidays.py +++ b/addons/hr_holidays/hr_holidays.py @@ -96,19 +96,42 @@ class hr_holidays_status(osv.osv): } def name_get(self, cr, uid, ids, context=None): - - if not context.get('employee_id',False): + if context is None: + context = {} + if not context.get('employee_id'): # leave counts is based on employee_id, would be inaccurate if not based on correct employee return super(hr_holidays_status, self).name_get(cr, uid, ids, context=context) - res = [] for record in self.browse(cr, uid, ids, context=context): name = record.name if not record.limit: - name = name + (' (%g/%g)' % (record.leaves_taken or 0.0, record.max_leaves or 0.0)) + name = name + (' (%g/%g)' % (record.virtual_remaining_leaves or 0.0, record.max_leaves or 0.0)) res.append((record.id, name)) return res + def _search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None): + """ Override _search to order the results, according to some employee. + The order is the following + + - limit (limited leaves first, such as Legal Leaves) + - virtual remaining leaves (higher the better, so using reverse on sorted) + + This override is necessary because those fields are not stored and depends + on an employee_id given in context. This sort will be done when there + is an employee_id in context and that no other order has been given + to the method. """ + if context is None: + context = {} + ids = super(hr_holidays_status, self)._search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count, access_rights_uid=access_rights_uid) + if not count and not order and context.get('employee_id'): + leaves = self.browse(cr, uid, ids, context=context) + # perform a 2-steps sort because sorting on reminaing leaves is reversed + # sorted keep the order and is stable so 2-steps + sorted_leaves = leaves.sorted(key=attrgetter('limit')) + sorted_leaves = leaves.sorted(key=attrgetter('virtual_remaining_leaves'), reverse=True) + return [leave.id for leave in sorted_leaves] + return ids + class hr_holidays(osv.osv): _name = "hr.holidays" diff --git a/addons/hr_holidays/hr_holidays_data.xml b/addons/hr_holidays/hr_holidays_data.xml index 1bdcb1310e7..1f4292a9d7c 100644 --- a/addons/hr_holidays/hr_holidays_data.xml +++ b/addons/hr_holidays/hr_holidays_data.xml @@ -22,27 +22,28 @@ + black Sick Leaves - True + red Compensatory Days - True + lavender Unpaid - True + brown diff --git a/addons/hr_holidays/hr_holidays_demo.xml b/addons/hr_holidays/hr_holidays_demo.xml index de2f261dbfa..f70fa072aaa 100644 --- a/addons/hr_holidays/hr_holidays_demo.xml +++ b/addons/hr_holidays/hr_holidays_demo.xml @@ -1,61 +1,61 @@ - + - Casual Leave for Peter Parker + Legal Leaves for Peter Parker add 20 - - Trip with Family - - - - remove - 3 - - - - - Doctor Appointment - - - - remove - draft - 3 - + + International Tour + + + + add + 7 + - Summer Vacation - - - - add - draft - 7 - - - - - International Tour - - - - add - 7 - + Summer Vacation + + + + add + draft + 7 + + + + + Trip with Family + + + + remove + 3 + + + + + Doctor Appointment + + + + remove + draft + 3 + + - - +