Merge lp:~openerp-groupes/hrself-addons/clean-onchange-leave-request into lp:hrself-addons

Proposed by tfr (Openerp) on 2011-11-25
Status: Needs review
Proposed branch: lp:~openerp-groupes/hrself-addons/clean-onchange-leave-request
Merge into: lp:hrself-addons
Diff against target: 279 lines (+82/-113)
2 files modified
hrself_holidays/hrself_holidays.py (+73/-104)
hrself_holidays/view/hrself_holidays_view.xml (+9/-9)
To merge this branch: bzr merge lp:~openerp-groupes/hrself-addons/clean-onchange-leave-request
Reviewer Review Type Date Requested Status
OpenERP Groupe S 2011-11-25 Pending
Review via email: mp+83417@code.launchpad.net
To post a comment you must log in.

Unmerged revisions

624. By tfr (Openerp) on 2011-11-25

[CLEAN] refactor onchanges and constraints in leave request form view for details
See : http://pad.openerp.com/groupes-spec

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hrself_holidays/hrself_holidays.py'
2--- hrself_holidays/hrself_holidays.py 2011-11-25 10:02:29 +0000
3+++ hrself_holidays/hrself_holidays.py 2011-11-25 16:57:23 +0000
4@@ -721,12 +721,19 @@
5 if not self._disable_checks(cr, uid, leave_request.type_id.id, activity_context_id) and not (leave_request.duration > 0):
6 return False
7 return True
8+
9+ def _check_am_pm(self, cr, uid, ids, context=None):
10+ for leave in self.browse(cr, uid, ids, context=context):
11+ if leave.date_to != leave.date_from and leave.am_pm != AM_PM_ALL_DAY:
12+ return False
13+ return True
14
15 _constraints = [
16 (_check_duration, _('Duration must be greater than 0 !'), ['duration']),
17- (_check_mandatory_reason, _('Type of holiday requires a reason'), ['reason']),
18+ (_check_am_pm, _('The leave last more then one day, you should select all day'), ['am_pm']),
19 (_check_limit_date, _check_limit_date_message, ['date_to']),
20 (_check_minimum_date, _check_minimum_date_message, ['date_from']),
21+ (_check_mandatory_reason, _('Type of holiday requires a reason'), ['reason']),
22 (_check_type_activity_context, _('This type is not compatible with activity context of employee'), ['type']),
23 ]
24
25@@ -752,7 +759,7 @@
26 old_date_to = hrself_datetime.to_date(leave.date_to)
27 new_date_to = _to_date(values.get('date_to'), old_date_to)
28 if (new_date_from != old_date_from) or (new_date_to != old_date_to):
29- on_change = self._onchange_date_type(cr, uid, [leave.id], leave.employee_id.id, new_date_from, new_date_to, leave.type_id.id, leave.am_pm, context)
30+ on_change = self.onchange_date_type(cr, uid, [leave.id], leave.employee_id.id, new_date_from, new_date_to, leave.type_id.id, leave.am_pm, context)
31 warning = on_change.get('warning')
32 if warning:
33 logger.warning(warning['message'])
34@@ -764,106 +771,58 @@
35 write_ids.append(leave.id)
36 return super(hrself_holidays_request_leave, self).write(cr, uid, write_ids, values, context=context)
37
38- def onchange_employee_id(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
39- """Action to execute when the user changes employee_id."""
40- return self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
41-
42- def onchange_date_from(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
43- """Action to execute when the user changes date_from."""
44- return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
45-
46- def onchange_date_to(self, cr, uid, ids, employee_id, date_to, date_from, type_id, am_pm, context):
47+ def onchange_date(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context=None):
48 """Action to execute when the user changes date_to."""
49- return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
50-
51- def onchange_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
52- """Action to execute when the user changes the type."""
53- return self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
54+ print 'on_change_date'
55+ if not date_from or not date_to:
56+ print "return empty"
57+ return {}
58+
59+ if date_from > date_to:
60+ message = _('The start date (%s) must be smaller or equal to the end date (%s)!') % (date_from, date_to)
61+ return {'warning' : {'title': _('Invalid dates'), 'message': message}}
62+ print 'onchange type finaly'
63+ return self.onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
64
65 def _disable_checks(self, cr, uid, type_id, activity_context_id):
66 """Disable checks?"""
67- result = False
68- if type_id and activity_context_id:
69- precision_id = self.pool.get('hrself.holidays.type').browse(cr, uid, type_id).precision_id_for_context(activity_context_id)
70- if precision_id:
71- prestation_code = self.pool.get('hrself.holidays.type.precision').browse(cr, uid, precision_id).prestation_code_id
72- if prestation_code and prestation_code.gross_leave:
73- result = True
74- return result
75-
76- def onchange_duration(self, cr, uid, ids, employee_id, date_from, date_to, type_id, duration, unit, am_pm, context=None):
77- """Action to execute when the user changes the duration."""
78- result = {}
79- if employee_id and date_from and date_to:
80- services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
81- activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
82- if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
83- allowed_duration = self._duration(cr, uid, type_id, am_pm, activities)['value']['duration']
84- if unit == hrself.TIME_UNIT_HOUR and date_from == date_to:
85- if duration > allowed_duration:
86- result['warning'] = {
87- 'title': _('Warning'),
88- 'message': _('The leave request duration is greater than allowed. \n You have %d %s(s) left.') %
89- (allowed_duration, hrself.TIME_UNITS_DICT[unit])
90- }
91- result['value'] = {'duration': allowed_duration}
92- elif duration <> allowed_duration:
93- result['warning'] = {'title': _('Warning'), 'message': _('You cannot change the duration.')}
94- result['value'] = {'duration': allowed_duration}
95- return result
96-
97- def onchange_unit(self, cr, uid, ids, employee_id, date_from, date_to, type_id, unit, am_pm, context=None):
98- """Action to execute when the user changes the unit."""
99- result = {}
100- date_from = date_from or fields.date.today()
101- date_to = date_to or fields.date.today()
102- if employee_id:
103- services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
104- activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
105- allowed_unit = self._duration(cr, uid, type_id, am_pm, activities)['value']['unit']
106- if unit <> allowed_unit:
107- result['warning'] = {'title': _('Warning'), 'message': _('You cannot change the unit.')}
108- result['value'] = {'unit': allowed_unit}
109- return result
110-
111- def _onchange_dates_am_pm(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
112- """Action to execute when the user changes dates (from or to) or the AM PM indicator."""
113- result = {}
114- if am_pm != AM_PM_ALL_DAY and date_from != date_to:
115- am_pm = AM_PM_ALL_DAY
116- result['warning'] = {'title': _('Warning'), 'message': _('You cannot set AM or PM when start date is not equal to end date.')}
117- result.update(self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context))
118- result['value']['am_pm'] = am_pm
119- return result
120-
121- def onchange_am_pm(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
122- """Action to execute when the user changes the AM PM indicator."""
123- return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
124-
125- def _onchange_date_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
126+ if not type_id or not activity_context_id:
127+ return False
128+
129+ precision_id = self.pool.get('hrself.holidays.type').browse(cr, uid, type_id).precision_id_for_context(activity_context_id)
130+ if precision_id:
131+ prestation_code = self.pool.get('hrself.holidays.type.precision').browse(cr, uid, precision_id).prestation_code_id
132+ if prestation_code and prestation_code.gross_leave:
133+ result = True
134+
135+ return False
136+
137+ def onchange_date_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context=None):
138 """Action to execute when the user changes a date (from or to) or the type."""
139+ print "on change type", employee_id, date_from, date_to, type_id, am_pm
140+
141 result = {}
142 activities = None
143- if date_from and date_to:
144- if date_from > date_to:
145- message = _('The start date (%s) must be smaller or equal to the end date (%s)!') % (date_from, date_to)
146+ if not (date_from and date_to and type_id and employee_id):
147+ return result
148+
149+
150+ services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
151+ activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
152+ if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
153+ present = False
154+ absence_days = set()
155+ for activity in activities:
156+ if activity['presence_absence'] == 'A':
157+ date = hrself_datetime.to_string(hrself_datetime.to_date(activity['date']))
158+ absence_days.add(date)
159+ elif activity['presence_absence'] == 'P':
160+ present = True
161+ if not present:
162+ message = _('You cannot take leave on absence days: ') + ', '.join(absence_days)
163 result['warning'] = {'title': _('Invalid dates'), 'message': message}
164- elif employee_id and type_id:
165- services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
166- activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
167- if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
168- presence_days = []
169- absence_days = []
170- for activity in activities:
171- date = hrself_datetime.to_string(hrself_datetime.to_date(activity['date']))
172- if activity['presence_absence'] == 'A' and date not in absence_days:
173- absence_days.append(date)
174- elif activity['presence_absence'] == 'P' and date not in presence_days:
175- presence_days.append(date)
176- if not presence_days:
177- message = _('You cannot take leave on absence days: ') + ', '.join(absence_days)
178- result['warning'] = {'title': _('Invalid dates'), 'message': message}
179- result.update(self._duration(cr, uid, type_id, am_pm, activities))
180+
181+ result.update(self._duration(cr, uid, type_id, am_pm, activities))
182 return result
183
184 def _duration(self, cr, uid, type_id, am_pm, activities):
185@@ -893,7 +852,7 @@
186 duration = duration_in_hour
187 else:
188 type = self.pool.get('hrself.holidays.type').browse (cr, uid, type_id)
189- message = _('No precision defined for the holidays type named %s identified by %d') % (type.name , type.id)
190+ message = _('No precision defined for the holidays type named %s identified by %d, please contact your administrator') % (type.name , type.id)
191 result['warning'] = {'title': _('No precision'), 'message': message}
192 result['value'] = {
193 'duration': duration,
194@@ -943,16 +902,21 @@
195 return None
196
197 def _check_allowed_duration(self, cr, uid, ids, context=None):
198- """Check that the leave request duration is not greater than allowed.
199- For the moment, only takes into account leaves
200- where date from is equal to date to.
201- :return: (allowed duration, unit)
202- :rtype: (int, str)"""
203+ """
204+ Check that the leave request duration is not greater than allowed.
205+ For the moment, only takes into account leaves
206+ where date from is equal to date to.
207+ :return: (allowed duration, unit)
208+ :rtype: (int, str)
209+ """
210 for leave in self.browse(cr, uid, ids, context=context):
211 date_from = leave.date_from
212 date_to = leave.date_to
213+ employee_id = leave.employee_id.id
214+ services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
215+ activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
216 if date_from == date_to:
217- employee_id = leave.employee_id.id
218+
219 search_ids = self.search(cr, uid, [
220 ('employee_id', '=', employee_id),
221 '|', ('state', '=', STATE_APPROVED), ('state', '=', STATE_CONFIRMED),
222@@ -972,12 +936,17 @@
223 already_taken += search_leave.duration
224 else:
225 already_taken += search_leave.duration_in_hour
226- services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
227- activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
228+
229 max_duration = self._duration(cr, uid, leave.type_id.id, AM_PM_ALL_DAY, activities)['value']['duration_in_hour']
230 allowed_duration = max(round(max_duration - already_taken, 2), 0.0)
231 if leave.duration > allowed_duration:
232 return (allowed_duration, hrself.TIME_UNIT_HOUR_LABEL)
233+ elif leave.unit != hrself.TIME_UNIT_HOUR:
234+ compute_duration = self._duration(cr, uid, leave.type_id.id, leave.am_pm, activities).get('value', {}).get('duration', -1)
235+ disable_check = self._disable_checks(cr, uid, leave.type_id.id, leave.activity_context_id.id)
236+ if leave.duration != compute_duration and not disable_check:
237+ return (compute_duration, hrself.TIME_UNIT_DAY_LABEL)
238+
239 return False
240
241 def _check_priority(self, cr, uid, ids, context=None):
242@@ -1068,7 +1037,7 @@
243
244 allowed = self._check_allowed_duration(cr, uid, ids, context=context)
245 if allowed :
246- raise osv.except_osv(_('Warning!'), _('The leave request duration is greater than allowed. \n You have %.2f %s(s) left.') % allowed)
247+ raise osv.except_osv(_('Warning!'), _('The leave request duration is different than allowed. \n %.2f %s(s) is allowed.') % allowed)
248
249 for leave_request in self.browse(cr, uid, ids, context=context):
250 type_name = self._check_priority(cr, uid, ids, context)
251
252=== modified file 'hrself_holidays/view/hrself_holidays_view.xml'
253--- hrself_holidays/view/hrself_holidays_view.xml 2011-11-24 10:12:14 +0000
254+++ hrself_holidays/view/hrself_holidays_view.xml 2011-11-25 16:57:23 +0000
255@@ -204,15 +204,15 @@
256 <field name="arch" type="xml">
257 <form string="Leave Request">
258 <group col="6" colspan="4">
259- <field name="employee_id" on_change="onchange_employee_id(employee_id,date_from,date_to,type_id,am_pm,context)"/>
260- <field name="date_from" on_change="onchange_date_from(employee_id,date_from,date_to,type_id,am_pm,context)"/>
261- <field name="date_to" required="1" on_change="onchange_date_to(employee_id,date_to,date_from,type_id,am_pm,context)"/>
262- <field name="type_id" default_focus="1" on_change="onchange_type(employee_id,date_from,date_to,type_id,am_pm,context)" context="{'employee_id': employee_id, 'date_from': date_from, 'date_to': date_to}" attrs="{'readonly': ['|', ('date_from', '=', False), ('employee_id', '=', False)]}"/>
263- <field name="duration" on_change="onchange_duration(employee_id,date_from,date_to,type_id,duration,unit,am_pm,context)"/>
264- <field name="unit" on_change="onchange_unit(employee_id,date_from,date_to,type_id,unit,am_pm,context)"/>
265- <field name="reason_id" domain="[('type_id', '=', type_id)]"/>
266- <field name="name"/>
267- <field name="am_pm" on_change="onchange_am_pm(employee_id,date_from,date_to,type_id,am_pm,context)"/>
268+ <field name="employee_id" on_change="onchange_date_type(employee_id,date_from,date_to,type_id,am_pm, context)" />
269+ <field name="date_from" on_change="onchange_date(employee_id,date_from,date_to,type_id,am_pm, context)" />
270+ <field name="date_to" required="1" on_change="onchange_date(employee_id,date_from,date_to,type_id,am_pm, context)" />
271+ <field name="type_id" default_focus="1" on_change="onchange_date_type(employee_id,date_from,date_to,type_id,am_pm, context)" context="{'employee_id': employee_id, 'date_from': date_from, 'date_to': date_to}" attrs="{'readonly': [('employee_id', '=', False)]}" />
272+ <field name="duration"/>
273+ <field name="unit" />
274+ <field name="reason_id" domain="[('type_id', '=', type_id)]" />
275+ <field name="name" />
276+ <field name="am_pm" on_change="onchange_date_type(employee_id,date_from,date_to,type_id,am_pm, context)"/>
277 <!-- Duration (in hour) is visible when the unit is day.
278 So the opposite is: duration (in hour) is invisible when the unit is not day. -->
279 <field name="duration_in_hour" attrs="{'invisible': [('unit', '!=', 1)]}"/>

Subscribers

People subscribed via source and target branches

to all changes: