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
=== modified file 'hrself_holidays/hrself_holidays.py'
--- hrself_holidays/hrself_holidays.py 2011-11-25 10:02:29 +0000
+++ hrself_holidays/hrself_holidays.py 2011-11-25 16:57:23 +0000
@@ -721,12 +721,19 @@
721 if not self._disable_checks(cr, uid, leave_request.type_id.id, activity_context_id) and not (leave_request.duration > 0):721 if not self._disable_checks(cr, uid, leave_request.type_id.id, activity_context_id) and not (leave_request.duration > 0):
722 return False722 return False
723 return True723 return True
724
725 def _check_am_pm(self, cr, uid, ids, context=None):
726 for leave in self.browse(cr, uid, ids, context=context):
727 if leave.date_to != leave.date_from and leave.am_pm != AM_PM_ALL_DAY:
728 return False
729 return True
724730
725 _constraints = [731 _constraints = [
726 (_check_duration, _('Duration must be greater than 0 !'), ['duration']),732 (_check_duration, _('Duration must be greater than 0 !'), ['duration']),
727 (_check_mandatory_reason, _('Type of holiday requires a reason'), ['reason']),733 (_check_am_pm, _('The leave last more then one day, you should select all day'), ['am_pm']),
728 (_check_limit_date, _check_limit_date_message, ['date_to']),734 (_check_limit_date, _check_limit_date_message, ['date_to']),
729 (_check_minimum_date, _check_minimum_date_message, ['date_from']),735 (_check_minimum_date, _check_minimum_date_message, ['date_from']),
736 (_check_mandatory_reason, _('Type of holiday requires a reason'), ['reason']),
730 (_check_type_activity_context, _('This type is not compatible with activity context of employee'), ['type']),737 (_check_type_activity_context, _('This type is not compatible with activity context of employee'), ['type']),
731 ]738 ]
732739
@@ -752,7 +759,7 @@
752 old_date_to = hrself_datetime.to_date(leave.date_to)759 old_date_to = hrself_datetime.to_date(leave.date_to)
753 new_date_to = _to_date(values.get('date_to'), old_date_to)760 new_date_to = _to_date(values.get('date_to'), old_date_to)
754 if (new_date_from != old_date_from) or (new_date_to != old_date_to):761 if (new_date_from != old_date_from) or (new_date_to != old_date_to):
755 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)762 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)
756 warning = on_change.get('warning')763 warning = on_change.get('warning')
757 if warning:764 if warning:
758 logger.warning(warning['message'])765 logger.warning(warning['message'])
@@ -764,106 +771,58 @@
764 write_ids.append(leave.id)771 write_ids.append(leave.id)
765 return super(hrself_holidays_request_leave, self).write(cr, uid, write_ids, values, context=context)772 return super(hrself_holidays_request_leave, self).write(cr, uid, write_ids, values, context=context)
766 773
767 def onchange_employee_id(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):774 def onchange_date(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context=None):
768 """Action to execute when the user changes employee_id."""
769 return self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
770
771 def onchange_date_from(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
772 """Action to execute when the user changes date_from."""
773 return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
774
775 def onchange_date_to(self, cr, uid, ids, employee_id, date_to, date_from, type_id, am_pm, context):
776 """Action to execute when the user changes date_to."""775 """Action to execute when the user changes date_to."""
777 return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)776 print 'on_change_date'
778777 if not date_from or not date_to:
779 def onchange_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):778 print "return empty"
780 """Action to execute when the user changes the type."""779 return {}
781 return self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)780
781 if date_from > date_to:
782 message = _('The start date (%s) must be smaller or equal to the end date (%s)!') % (date_from, date_to)
783 return {'warning' : {'title': _('Invalid dates'), 'message': message}}
784 print 'onchange type finaly'
785 return self.onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
782786
783 def _disable_checks(self, cr, uid, type_id, activity_context_id):787 def _disable_checks(self, cr, uid, type_id, activity_context_id):
784 """Disable checks?"""788 """Disable checks?"""
785 result = False789 if not type_id or not activity_context_id:
786 if type_id and activity_context_id:790 return False
787 precision_id = self.pool.get('hrself.holidays.type').browse(cr, uid, type_id).precision_id_for_context(activity_context_id)791
788 if precision_id:792 precision_id = self.pool.get('hrself.holidays.type').browse(cr, uid, type_id).precision_id_for_context(activity_context_id)
789 prestation_code = self.pool.get('hrself.holidays.type.precision').browse(cr, uid, precision_id).prestation_code_id793 if precision_id:
790 if prestation_code and prestation_code.gross_leave:794 prestation_code = self.pool.get('hrself.holidays.type.precision').browse(cr, uid, precision_id).prestation_code_id
791 result = True795 if prestation_code and prestation_code.gross_leave:
792 return result796 result = True
793797
794 def onchange_duration(self, cr, uid, ids, employee_id, date_from, date_to, type_id, duration, unit, am_pm, context=None):798 return False
795 """Action to execute when the user changes the duration."""799
796 result = {}800 def onchange_date_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context=None):
797 if employee_id and date_from and date_to:
798 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
799 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
800 if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
801 allowed_duration = self._duration(cr, uid, type_id, am_pm, activities)['value']['duration']
802 if unit == hrself.TIME_UNIT_HOUR and date_from == date_to:
803 if duration > allowed_duration:
804 result['warning'] = {
805 'title': _('Warning'),
806 'message': _('The leave request duration is greater than allowed. \n You have %d %s(s) left.') %
807 (allowed_duration, hrself.TIME_UNITS_DICT[unit])
808 }
809 result['value'] = {'duration': allowed_duration}
810 elif duration <> allowed_duration:
811 result['warning'] = {'title': _('Warning'), 'message': _('You cannot change the duration.')}
812 result['value'] = {'duration': allowed_duration}
813 return result
814
815 def onchange_unit(self, cr, uid, ids, employee_id, date_from, date_to, type_id, unit, am_pm, context=None):
816 """Action to execute when the user changes the unit."""
817 result = {}
818 date_from = date_from or fields.date.today()
819 date_to = date_to or fields.date.today()
820 if employee_id:
821 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
822 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
823 allowed_unit = self._duration(cr, uid, type_id, am_pm, activities)['value']['unit']
824 if unit <> allowed_unit:
825 result['warning'] = {'title': _('Warning'), 'message': _('You cannot change the unit.')}
826 result['value'] = {'unit': allowed_unit}
827 return result
828
829 def _onchange_dates_am_pm(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
830 """Action to execute when the user changes dates (from or to) or the AM PM indicator."""
831 result = {}
832 if am_pm != AM_PM_ALL_DAY and date_from != date_to:
833 am_pm = AM_PM_ALL_DAY
834 result['warning'] = {'title': _('Warning'), 'message': _('You cannot set AM or PM when start date is not equal to end date.')}
835 result.update(self._onchange_date_type(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context))
836 result['value']['am_pm'] = am_pm
837 return result
838
839 def onchange_am_pm(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
840 """Action to execute when the user changes the AM PM indicator."""
841 return self._onchange_dates_am_pm(cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context)
842
843 def _onchange_date_type(self, cr, uid, ids, employee_id, date_from, date_to, type_id, am_pm, context):
844 """Action to execute when the user changes a date (from or to) or the type."""801 """Action to execute when the user changes a date (from or to) or the type."""
802 print "on change type", employee_id, date_from, date_to, type_id, am_pm
803
845 result = {}804 result = {}
846 activities = None805 activities = None
847 if date_from and date_to:806 if not (date_from and date_to and type_id and employee_id):
848 if date_from > date_to:807 return result
849 message = _('The start date (%s) must be smaller or equal to the end date (%s)!') % (date_from, date_to)808
809
810 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
811 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
812 if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
813 present = False
814 absence_days = set()
815 for activity in activities:
816 if activity['presence_absence'] == 'A':
817 date = hrself_datetime.to_string(hrself_datetime.to_date(activity['date']))
818 absence_days.add(date)
819 elif activity['presence_absence'] == 'P':
820 present = True
821 if not present:
822 message = _('You cannot take leave on absence days: ') + ', '.join(absence_days)
850 result['warning'] = {'title': _('Invalid dates'), 'message': message}823 result['warning'] = {'title': _('Invalid dates'), 'message': message}
851 elif employee_id and type_id:824
852 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)825 result.update(self._duration(cr, uid, type_id, am_pm, activities))
853 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
854 if not self._disable_checks(cr, uid, type_id, activities and activities[0] and activities[0]['activity_context']):
855 presence_days = []
856 absence_days = []
857 for activity in activities:
858 date = hrself_datetime.to_string(hrself_datetime.to_date(activity['date']))
859 if activity['presence_absence'] == 'A' and date not in absence_days:
860 absence_days.append(date)
861 elif activity['presence_absence'] == 'P' and date not in presence_days:
862 presence_days.append(date)
863 if not presence_days:
864 message = _('You cannot take leave on absence days: ') + ', '.join(absence_days)
865 result['warning'] = {'title': _('Invalid dates'), 'message': message}
866 result.update(self._duration(cr, uid, type_id, am_pm, activities))
867 return result826 return result
868827
869 def _duration(self, cr, uid, type_id, am_pm, activities):828 def _duration(self, cr, uid, type_id, am_pm, activities):
@@ -893,7 +852,7 @@
893 duration = duration_in_hour852 duration = duration_in_hour
894 else:853 else:
895 type = self.pool.get('hrself.holidays.type').browse (cr, uid, type_id)854 type = self.pool.get('hrself.holidays.type').browse (cr, uid, type_id)
896 message = _('No precision defined for the holidays type named %s identified by %d') % (type.name , type.id)855 message = _('No precision defined for the holidays type named %s identified by %d, please contact your administrator') % (type.name , type.id)
897 result['warning'] = {'title': _('No precision'), 'message': message}856 result['warning'] = {'title': _('No precision'), 'message': message}
898 result['value'] = {857 result['value'] = {
899 'duration': duration, 858 'duration': duration,
@@ -943,16 +902,21 @@
943 return None 902 return None
944903
945 def _check_allowed_duration(self, cr, uid, ids, context=None):904 def _check_allowed_duration(self, cr, uid, ids, context=None):
946 """Check that the leave request duration is not greater than allowed.905 """
947 For the moment, only takes into account leaves 906 Check that the leave request duration is not greater than allowed.
948 where date from is equal to date to.907 For the moment, only takes into account leaves
949 :return: (allowed duration, unit)908 where date from is equal to date to.
950 :rtype: (int, str)"""909 :return: (allowed duration, unit)
910 :rtype: (int, str)
911 """
951 for leave in self.browse(cr, uid, ids, context=context):912 for leave in self.browse(cr, uid, ids, context=context):
952 date_from = leave.date_from913 date_from = leave.date_from
953 date_to = leave.date_to914 date_to = leave.date_to
915 employee_id = leave.employee_id.id
916 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)
917 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
954 if date_from == date_to:918 if date_from == date_to:
955 employee_id = leave.employee_id.id919
956 search_ids = self.search(cr, uid, [920 search_ids = self.search(cr, uid, [
957 ('employee_id', '=', employee_id),921 ('employee_id', '=', employee_id),
958 '|', ('state', '=', STATE_APPROVED), ('state', '=', STATE_CONFIRMED),922 '|', ('state', '=', STATE_APPROVED), ('state', '=', STATE_CONFIRMED),
@@ -972,12 +936,17 @@
972 already_taken += search_leave.duration936 already_taken += search_leave.duration
973 else:937 else:
974 already_taken += search_leave.duration_in_hour938 already_taken += search_leave.duration_in_hour
975 services = self.pool.get('hrself.services').hrself_holidays_service(cr, uid, context=context)939
976 activities = services.get_theoretical_activities(cr, uid, employee_id, date_from, date_to, context=context)
977 max_duration = self._duration(cr, uid, leave.type_id.id, AM_PM_ALL_DAY, activities)['value']['duration_in_hour']940 max_duration = self._duration(cr, uid, leave.type_id.id, AM_PM_ALL_DAY, activities)['value']['duration_in_hour']
978 allowed_duration = max(round(max_duration - already_taken, 2), 0.0)941 allowed_duration = max(round(max_duration - already_taken, 2), 0.0)
979 if leave.duration > allowed_duration:942 if leave.duration > allowed_duration:
980 return (allowed_duration, hrself.TIME_UNIT_HOUR_LABEL)943 return (allowed_duration, hrself.TIME_UNIT_HOUR_LABEL)
944 elif leave.unit != hrself.TIME_UNIT_HOUR:
945 compute_duration = self._duration(cr, uid, leave.type_id.id, leave.am_pm, activities).get('value', {}).get('duration', -1)
946 disable_check = self._disable_checks(cr, uid, leave.type_id.id, leave.activity_context_id.id)
947 if leave.duration != compute_duration and not disable_check:
948 return (compute_duration, hrself.TIME_UNIT_DAY_LABEL)
949
981 return False950 return False
982951
983 def _check_priority(self, cr, uid, ids, context=None):952 def _check_priority(self, cr, uid, ids, context=None):
@@ -1068,7 +1037,7 @@
1068 1037
1069 allowed = self._check_allowed_duration(cr, uid, ids, context=context)1038 allowed = self._check_allowed_duration(cr, uid, ids, context=context)
1070 if allowed :1039 if allowed :
1071 raise osv.except_osv(_('Warning!'), _('The leave request duration is greater than allowed. \n You have %.2f %s(s) left.') % allowed)1040 raise osv.except_osv(_('Warning!'), _('The leave request duration is different than allowed. \n %.2f %s(s) is allowed.') % allowed)
1072 1041
1073 for leave_request in self.browse(cr, uid, ids, context=context):1042 for leave_request in self.browse(cr, uid, ids, context=context):
1074 type_name = self._check_priority(cr, uid, ids, context)1043 type_name = self._check_priority(cr, uid, ids, context)
10751044
=== modified file 'hrself_holidays/view/hrself_holidays_view.xml'
--- hrself_holidays/view/hrself_holidays_view.xml 2011-11-24 10:12:14 +0000
+++ hrself_holidays/view/hrself_holidays_view.xml 2011-11-25 16:57:23 +0000
@@ -204,15 +204,15 @@
204 <field name="arch" type="xml">204 <field name="arch" type="xml">
205 <form string="Leave Request">205 <form string="Leave Request">
206 <group col="6" colspan="4">206 <group col="6" colspan="4">
207 <field name="employee_id" on_change="onchange_employee_id(employee_id,date_from,date_to,type_id,am_pm,context)"/>207 <field name="employee_id" on_change="onchange_date_type(employee_id,date_from,date_to,type_id,am_pm, context)" />
208 <field name="date_from" on_change="onchange_date_from(employee_id,date_from,date_to,type_id,am_pm,context)"/>208 <field name="date_from" on_change="onchange_date(employee_id,date_from,date_to,type_id,am_pm, context)" />
209 <field name="date_to" required="1" on_change="onchange_date_to(employee_id,date_to,date_from,type_id,am_pm,context)"/>209 <field name="date_to" required="1" on_change="onchange_date(employee_id,date_from,date_to,type_id,am_pm, context)" />
210 <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)]}"/>210 <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)]}" />
211 <field name="duration" on_change="onchange_duration(employee_id,date_from,date_to,type_id,duration,unit,am_pm,context)"/>211 <field name="duration"/>
212 <field name="unit" on_change="onchange_unit(employee_id,date_from,date_to,type_id,unit,am_pm,context)"/>212 <field name="unit" />
213 <field name="reason_id" domain="[('type_id', '=', type_id)]"/>213 <field name="reason_id" domain="[('type_id', '=', type_id)]" />
214 <field name="name"/>214 <field name="name" />
215 <field name="am_pm" on_change="onchange_am_pm(employee_id,date_from,date_to,type_id,am_pm,context)"/>215 <field name="am_pm" on_change="onchange_date_type(employee_id,date_from,date_to,type_id,am_pm, context)"/>
216 <!-- Duration (in hour) is visible when the unit is day.216 <!-- Duration (in hour) is visible when the unit is day.
217 So the opposite is: duration (in hour) is invisible when the unit is not day. -->217 So the opposite is: duration (in hour) is invisible when the unit is not day. -->
218 <field name="duration_in_hour" attrs="{'invisible': [('unit', '!=', 1)]}"/>218 <field name="duration_in_hour" attrs="{'invisible': [('unit', '!=', 1)]}"/>

Subscribers

People subscribed via source and target branches

to all changes: