1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2013 OpenERP S.A. <http://www.openerp.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp.addons.base_action_rule.base_action_rule import get_datetime
23 from openerp.osv import fields, osv
26 class base_action_rule(osv.Model):
27 """ Add resource and calendar for time-based conditions """
28 _name = 'base.action.rule'
29 _inherit = ['base.action.rule']
32 'trg_date_resource_field_id': fields.many2one(
33 'ir.model.fields', 'Use employee work schedule',
34 help='Use the user\'s working schedule.',
38 def _check_delay(self, cr, uid, action, record, record_dt, context=None):
39 """ Override the check of delay to try to use a user-related calendar.
40 If no calendar is found, fallback on the default behavior. """
41 if action.trg_date_calendar_id and action.trg_date_range_type == 'day' and action.trg_date_resource_field_id:
42 user = record[action.trg_date_resource_field_id.name]
43 if user.employee_ids and user.employee_ids[0].contract_id \
44 and user.employee_ids[0].contract_id.working_hours:
45 calendar = user.employee_ids[0].contract_id.working_hours
46 start_dt = get_datetime(record_dt)
47 resource_id = user.employee_ids[0].resource_id.id
48 action_dt = self.pool['resource.calendar'].schedule_days_get_date(
49 cr, uid, calendar.id, action.trg_date_range,
50 day_date=start_dt, compute_leaves=True, resource_id=resource_id,
54 return super(base_action_rule, self)._check_delay(cr, uid, action, record, record_dt, context=context)