[ADD] breadcrumb return_url to Website
[odoo/odoo.git] / addons / hr_holidays / hr_holidays.py
index 6dc58bb..1af2fc0 100644 (file)
@@ -26,6 +26,7 @@ import math
 import time
 from operator import attrgetter
 
+from openerp.exceptions import Warning
 from openerp import tools
 from openerp.osv import fields, osv
 from openerp.tools.translate import _
@@ -129,6 +130,19 @@ class hr_holidays(osv.osv):
                 result[hol.id] = hol.number_of_days_temp
         return result
 
+    def _get_can_reset(self, cr, uid, ids, name, arg, context=None):
+        """User can reset a leave request if it is its own leave request or if
+        he is an Hr Manager. """
+        user = self.pool['res.users'].browse(cr, uid, uid, context=context)
+        group_hr_manager_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'group_hr_manager')[1]
+        if group_hr_manager_id in [g.id for g in user.groups_id]:
+            return dict.fromkeys(ids, True)
+        result = dict.fromkeys(ids, False)
+        for holiday in self.browse(cr, uid, ids, context=context):
+            if holiday.employee_id and holiday.employee_id.user_id and holiday.employee_id.user_id.id == uid:
+                result[holiday.id] = True
+        return result
+
     def _check_date(self, cr, uid, ids):
         for holiday in self.browse(cr, uid, ids):
             holiday_ids = self.search(cr, uid, [('date_from', '<=', holiday.date_to), ('date_to', '>=', holiday.date_from), ('employee_id', '=', holiday.employee_id.id), ('id', '<>', holiday.id)])
@@ -136,6 +150,8 @@ class hr_holidays(osv.osv):
                 return False
         return True
 
+    _check_holidays = lambda self, cr, uid, ids, context=None: self.check_holidays(cr, uid, ids, context=context)
+
     _columns = {
         'name': fields.char('Description', size=64),
         'state': fields.selection([('draft', 'To Submit'), ('cancel', 'Cancelled'),('confirm', 'To Approve'), ('refuse', 'Refused'), ('validate1', 'Second Approval'), ('validate', 'Approved')],
@@ -162,16 +178,20 @@ class hr_holidays(osv.osv):
         'holiday_type': fields.selection([('employee','By Employee'),('category','By Employee Tag')], 'Allocation Mode', readonly=True, states={'draft':[('readonly',False)], 'confirm':[('readonly',False)]}, help='By Employee: Allocation/Request for individual Employee, By Employee Tag: Allocation/Request for group of employees in category', required=True),
         'manager_id2': fields.many2one('hr.employee', 'Second Approval', readonly=True, help='This area is automaticly filled by the user who validate the leave with second level (If Leave type need second validation)'),
         'double_validation': fields.related('holiday_status_id', 'double_validation', type='boolean', relation='hr.holidays.status', string='Apply Double Validation'),
+        'can_reset': fields.function(
+            _get_can_reset,
+            type='boolean'),
     }
     _defaults = {
         'employee_id': _employee_get,
-        'state': 'draft',
+        'state': 'confirm',
         'type': 'remove',
         'user_id': lambda obj, cr, uid, context: uid,
         'holiday_type': 'employee'
     }
     _constraints = [
         (_check_date, 'You can not have 2 leaves that overlaps on same day!', ['date_from','date_to']),
+        (_check_holidays, 'The number of remaining leaves is not sufficient for this leave type', ['state','number_of_days_temp'])
     ] 
     
     _sql_constraints = [
@@ -297,34 +317,25 @@ class hr_holidays(osv.osv):
         if context is None:
             context = {}
         context = dict(context, mail_create_nolog=True)
-        return super(hr_holidays, self).create(cr, uid, values, context=context)
-
-    def write(self, cr, uid, ids, vals, context=None):
-        check_fnct = self.pool.get('hr.holidays.status').check_access_rights
-        for  holiday in self.browse(cr, uid, ids, context=context):
-            if holiday.state in ('validate','validate1') and not check_fnct(cr, uid, 'write', raise_exception=False):
-                raise osv.except_osv(_('Warning!'),_('You cannot modify a leave request that has been approved. Contact a human resource manager.'))
-        return super(hr_holidays, self).write(cr, uid, ids, vals, context=context)
+        hol_id = super(hr_holidays, self).create(cr, uid, values, context=context)
+        return hol_id
 
-    def set_to_draft(self, cr, uid, ids, context=None):
+    def holidays_reset(self, cr, uid, ids, context=None):
         self.write(cr, uid, ids, {
             'state': 'draft',
             'manager_id': False,
             'manager_id2': False,
         })
-        self.delete_workflow(cr, uid, ids)
-        self.create_workflow(cr, uid, ids)
         to_unlink = []
         for record in self.browse(cr, uid, ids, context=context):
             for record2 in record.linked_request_ids:
-                self.set_to_draft(cr, uid, [record2.id], context=context)
+                self.holidays_reset(cr, uid, [record2.id], context=context)
                 to_unlink.append(record2.id)
         if to_unlink:
             self.unlink(cr, uid, to_unlink, context=context)
         return True
 
     def holidays_first_validate(self, cr, uid, ids, context=None):
-        self.check_holidays(cr, uid, ids, context=context)
         obj_emp = self.pool.get('hr.employee')
         ids2 = obj_emp.search(cr, uid, [('user_id', '=', uid)])
         manager = ids2 and ids2[0] or False
@@ -332,7 +343,6 @@ class hr_holidays(osv.osv):
         return self.write(cr, uid, ids, {'state':'validate1', 'manager_id': manager})
 
     def holidays_validate(self, cr, uid, ids, context=None):
-        self.check_holidays(cr, uid, ids, context=context)
         obj_emp = self.pool.get('hr.employee')
         ids2 = obj_emp.search(cr, uid, [('user_id', '=', uid)])
         manager = ids2 and ids2[0] or False
@@ -384,7 +394,6 @@ class hr_holidays(osv.osv):
         return True
 
     def holidays_confirm(self, cr, uid, ids, context=None):
-        self.check_holidays(cr, uid, ids, context=context)
         for record in self.browse(cr, uid, ids, context=context):
             if record.employee_id and record.employee_id.parent_id and record.employee_id.parent_id.user_id:
                 self.message_subscribe_users(cr, uid, [record.id], user_ids=[record.employee_id.parent_id.user_id.id], context=context)
@@ -420,12 +429,10 @@ class hr_holidays(osv.osv):
             if record.holiday_type != 'employee' or record.type != 'remove' or not record.employee_id or record.holiday_status_id.limit:
                 continue
             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]
-            if leave_days['remaining_leaves'] < record.number_of_days_temp:
-                raise osv.except_osv(_('Warning!'),
-                                     _('There are not enough remaining days available in %s for employee %s.') % (record.holiday_status_id.name, record.employee_id.name))
-            if leave_days['virtual_remaining_leaves'] < record.number_of_days_temp:
-                raise osv.except_osv(_('Warning!'),
-                                     _('Other pending requests already book too much days in %s for employee %s.') % (record.holiday_status_id.name, record.employee_id.name))
+            if leave_days['remaining_leaves'] < 0 or leave_days['virtual_remaining_leaves'] < 0:
+                # Raising a warning gives a more user-friendly feedback than the default constraint error
+                raise Warning(_('The number of remaining leaves is not sufficient for this leave type.\n'
+                                'Please verify also the leaves waiting for validation.'))
         return True
 
     # -----------------------------