Merge lp:~openerp-dev/openobject-addons/trunk-hr-leave-count-correct-dka into lp:openobject-addons

Proposed by Darshan Kalola(OpenERP)
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-addons/trunk-hr-leave-count-correct-dka
Merge into: lp:openobject-addons
Diff against target: 145 lines (+63/-36)
1 file modified
hr_holidays/hr_holidays.py (+63/-36)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/trunk-hr-leave-count-correct-dka
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+218789@code.launchpad.net

Description of the change

Hello,

   I have fix the problem of leaves counts not correct in hr_holidays.
   for more details see : https://pad.openerp.com/p/openerp-project.task-0687EIYQ81

Thanks,
Darshan

To post a comment you must log in.

Unmerged revisions

9429. By Darshan Kalola(OpenERP)

[MERGE]sync with trunk.

9428. By Darshan Kalola(OpenERP)

[CLEAN]remove variable which is used only once.

9427. By Darshan Kalola(OpenERP)

[MERGE]with lp:~openerp-dev/openobject-addons/saas-3-hr-leave-count-correct-mba.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hr_holidays/hr_holidays.py'
2--- hr_holidays/hr_holidays.py 2014-05-07 08:35:56 +0000
3+++ hr_holidays/hr_holidays.py 2014-05-08 11:50:22 +0000
4@@ -36,40 +36,8 @@
5 _name = "hr.holidays.status"
6 _description = "Leave Type"
7
8- def get_days(self, cr, uid, ids, employee_id, context=None):
9- result = dict((id, dict(max_leaves=0, leaves_taken=0, remaining_leaves=0,
10- virtual_remaining_leaves=0)) for id in ids)
11- holiday_ids = self.pool['hr.holidays'].search(cr, uid, [('employee_id', '=', employee_id),
12- ('state', 'in', ['confirm', 'validate1', 'validate']),
13- ('holiday_status_id', 'in', ids)
14- ], context=context)
15- for holiday in self.pool['hr.holidays'].browse(cr, uid, holiday_ids, context=context):
16- status_dict = result[holiday.holiday_status_id.id]
17- if holiday.type == 'add':
18- status_dict['virtual_remaining_leaves'] += holiday.number_of_days
19- if holiday.state == 'validate':
20- status_dict['max_leaves'] += holiday.number_of_days
21- status_dict['remaining_leaves'] += holiday.number_of_days
22- elif holiday.type == 'remove': # number of days is negative
23- status_dict['virtual_remaining_leaves'] += holiday.number_of_days
24- if holiday.state == 'validate':
25- status_dict['leaves_taken'] -= holiday.number_of_days
26- status_dict['remaining_leaves'] += holiday.number_of_days
27- return result
28-
29 def _user_left_days(self, cr, uid, ids, name, args, context=None):
30- employee_id = False
31- if context and 'employee_id' in context:
32- employee_id = context['employee_id']
33- else:
34- employee_ids = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
35- if employee_ids:
36- employee_id = employee_ids[0]
37- if employee_id:
38- res = self.get_days(cr, uid, ids, employee_id, context=context)
39- else:
40- res = dict.fromkeys(ids, {'leaves_taken': 0, 'remaining_leaves': 0, 'max_leaves': 0})
41- return res
42+ return self.pool.get('hr.holidays').get_stats(cr, uid, ids, context=context)
43
44 _columns = {
45 'name': fields.char('Leave Type', size=64, required=True, translate=True),
46@@ -82,6 +50,7 @@
47 'leaves_taken': fields.function(_user_left_days, string='Leaves Already Taken', help='This value is given by the sum of all holidays requests with a negative value.', multi='user_left_days'),
48 'remaining_leaves': fields.function(_user_left_days, string='Remaining Leaves', help='Maximum Leaves Allowed - Leaves Already Taken', multi='user_left_days'),
49 'virtual_remaining_leaves': fields.function(_user_left_days, string='Virtual Remaining Leaves', help='Maximum Leaves Allowed - Leaves Already Taken - Leaves Waiting Approval', multi='user_left_days'),
50+ 'leaves_tag': fields.function(_user_left_days, string='Leaves Tag', multi='user_left_days'),
51 'double_validation': fields.boolean('Apply Double Validation', help="When selected, the Allocation/Leave Requests for this type require a second validation to be approved."),
52 }
53 _defaults = {
54@@ -94,7 +63,7 @@
55 for record in self.browse(cr, uid, ids, context=context):
56 name = record.name
57 if not record.limit:
58- name = name + (' (%d/%d)' % (record.leaves_taken or 0.0, record.max_leaves or 0.0))
59+ name = record.leaves_tag
60 res.append((record.id, name))
61 return res
62
63@@ -112,6 +81,38 @@
64 },
65 }
66
67+ def get_stats(self, cr, uid, type_ids, employee_id=None, context=None):
68+ if context is None:
69+ context = {}
70+ if employee_id is None:
71+ employee_id = context.get('employee_id')
72+ if not employee_id:
73+ employee_ids = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
74+ if employee_ids:
75+ employee_id = employee_ids[0]
76+
77+ domain = [('holiday_status_id', 'in', type_ids), ('state', 'in', ['confirm', 'validate1', 'validate'])]
78+ if employee_id:
79+ domain += [('employee_id', '=', employee_id)]
80+ status_types = self.pool.get('hr.holidays.status').browse(cr, uid, type_ids, context=context)
81+ result = dict((type.id, dict(max_leaves=0, leaves_taken=0, remaining_leaves=0,
82+ virtual_remaining_leaves=0, leaves_tag=type.name)) for type in status_types)
83+ holiday_ids = self.search(cr, uid, domain, context=context)
84+ for holiday in self.browse(cr, uid, holiday_ids, context=context):
85+ status_dict = result[holiday.holiday_status_id.id]
86+ if holiday.type == 'add':
87+ status_dict['virtual_remaining_leaves'] += holiday.number_of_days
88+ if holiday.state == 'validate':
89+ status_dict['max_leaves'] += holiday.number_of_days
90+ status_dict['remaining_leaves'] += holiday.number_of_days
91+ elif holiday.type == 'remove': # number of days is negative
92+ status_dict['virtual_remaining_leaves'] += holiday.number_of_days
93+ if holiday.state == 'validate':
94+ status_dict['leaves_taken'] -= holiday.number_of_days
95+ status_dict['remaining_leaves'] += holiday.number_of_days
96+ status_dict['leaves_tag'] = '%s (%d/%d)' % (holiday.holiday_status_id.name, status_dict.get('leaves_taken'), status_dict.get('max_leaves',0.0))
97+ return result
98+
99 def _employee_get(self, cr, uid, context=None):
100 emp_id = context.get('default_employee_id', False)
101 if emp_id:
102@@ -202,7 +203,33 @@
103 ('date_check2', "CHECK ( (type='add') OR (date_from <= date_to))", "The start date must be anterior to the end date."),
104 ('date_check', "CHECK ( number_of_days_temp >= 0 )", "The number of days must be greater than 0."),
105 ]
106-
107+
108+ def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
109+ if context is None:
110+ context = {}
111+ employee_flag = False
112+ if fields and 'employee_id' not in fields:
113+ fields.append('employee_id')
114+ employee_flag = True
115+ results = super(hr_holidays, self).read(cr, uid, ids, fields=fields, context=context, load=load)
116+
117+ if isinstance(results, (int, str)):
118+ results = results[0]
119+ for holiday in results:
120+ status_id = holiday.get('holiday_status_id')
121+ employee_id = holiday.get('employee_id')
122+ if isinstance(employee_id, (list, tuple)):
123+ employee_id = employee_id[0]
124+ if isinstance(status_id, (list, tuple)):
125+ status_id = holiday.get('holiday_status_id')[0]
126+ status = self.get_stats(cr, uid, [status_id], employee_id=employee_id, context=context)[status_id]
127+ holiday['holiday_status_id'] = (status_id, status['leaves_tag'])
128+ if employee_flag:
129+ del holiday['employee_id']
130+ if isinstance(ids, (int, str)):
131+ results = results[0]
132+ return results
133+
134 def copy(self, cr, uid, id, default=None, context=None):
135 if default is None:
136 default = {}
137@@ -439,7 +466,7 @@
138 for record in self.browse(cr, uid, ids, context=context):
139 if record.holiday_type != 'employee' or record.type != 'remove' or not record.employee_id or record.holiday_status_id.limit:
140 continue
141- leave_days = self.pool.get('hr.holidays.status').get_days(cr, uid, [record.holiday_status_id.id], record.employee_id.id, context=context)[record.holiday_status_id.id]
142+ leave_days = self.get_stats(cr, uid, [record.holiday_status_id.id], record.employee_id.id, context=context)[record.holiday_status_id.id]
143 if leave_days['remaining_leaves'] < 0 or leave_days['virtual_remaining_leaves'] < 0:
144 # Raising a warning gives a more user-friendly feedback than the default constraint error
145 raise Warning(_('The number of remaining leaves is not sufficient for this leave type.\n'

Subscribers

People subscribed via source and target branches

to all changes: