diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index fd1d8e8d121..20be9cd4dec 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -240,6 +240,18 @@ class base_action_rule(osv.osv): data.update({'model': model.model}) return {'value': data} + def _check_delay(self, cr, uid, action, record, record_dt, context=None): + if action.trg_date_calendar_id and action.trg_date_range_type == 'day': + start_dt = get_datetime(record_dt) + action_dt = self.pool['resource.calendar'].schedule_days_get_date( + cr, uid, action.trg_date_calendar_id.id, action.trg_date_range, + day_date=start_dt, compute_leaves=True, context=context + ) + else: + delay = DATE_RANGE_FUNCTION[action.trg_date_range_type](action.trg_date_range) + action_dt = get_datetime(record_dt) + delay + return action_dt + def _check(self, cr, uid, automatic=False, use_new_cursor=False, context=None): """ This Function is called by scheduler. """ context = context or {} @@ -266,21 +278,12 @@ class base_action_rule(osv.osv): else: get_record_dt = lambda record: record[date_field] - delay = DATE_RANGE_FUNCTION[action.trg_date_range_type](action.trg_date_range) - # process action on the records that should be executed for record in model.browse(cr, uid, record_ids, context=context): record_dt = get_record_dt(record) if not record_dt: continue - if action.trg_date_calendar_id and action.trg_date_range_type == 'day': - start_dt = get_datetime(record_dt) - action_dt = self.pool['resource.calendar'].schedule_days( - cr, uid, action.trg_date_calendar_id.id, action.trg_date_range, - date=start_dt, compute_leaves=True - ) - else: - action_dt = get_datetime(record_dt) + delay + action_dt = self._check_delay(cr, uid, action, record, record_dt, context=context) if last_run and (last_run <= action_dt < now) or (action_dt < now): try: self._process(cr, uid, action, [record.id], context=context) diff --git a/addons/hr_contract/__init__.py b/addons/hr_contract/__init__.py index 36375675bda..9bec9d6fffe 100644 --- a/addons/hr_contract/__init__.py +++ b/addons/hr_contract/__init__.py @@ -2,7 +2,7 @@ ############################################################################## # # OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). +# Copyright (C) 2004-Today OpenERP SA ( +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.addons.base_action_rule.base_action_rule import get_datetime +from openerp.osv import fields, osv + + +class base_action_rule(osv.Model): + """ Add resource and calendar for time-based conditions """ + _name = 'base.action.rule' + _inherit = ['base.action.rule'] + + _columns = { + 'trg_date_resource_field_id': fields.many2one( + 'ir.model.fields', 'User related field', + help='Use the user\'s working schedule.', + ), + } + + def _check_delay(self, cr, uid, action, record, record_dt, context=None): + """ Override the check of delay to try to use a user-related calendar. + If no calendar is found, fallback on the default behavior. """ + if action.trg_date_calendar_id and action.trg_date_range_type == 'day' and action.trg_date_resource_field_id: + user = record[action.trg_date_resource_field_id.name] + if user.employee_ids and user.employee_ids[0].contract_id \ + and user.employee_ids[0].contract_id.working_hours: + calendar = user.employee_ids[0].contract_id.working_hours + start_dt = get_datetime(record_dt) + action_dt = self.pool['resource.calendar'].schedule_days_get_date( + cr, uid, calendar.id, action.trg_date_range, + day_date=start_dt, compute_leaves=True, context=context + ) + return action_dt + return super(base_action_rule, self)._check_delay(cr, uid, action, record, record_dt, context=context) diff --git a/addons/hr_contract/base_action_rule_view.xml b/addons/hr_contract/base_action_rule_view.xml new file mode 100644 index 00000000000..c37628b8c37 --- /dev/null +++ b/addons/hr_contract/base_action_rule_view.xml @@ -0,0 +1,19 @@ + + + + + + base.action.rule.form + base.action.rule + + + + + + + + + + diff --git a/addons/resource/resource.py b/addons/resource/resource.py index 859e31e82a0..3824f154681 100644 --- a/addons/resource/resource.py +++ b/addons/resource/resource.py @@ -481,7 +481,7 @@ class resource_calendar(osv.osv): """Wrapper on _schedule_days: return the beginning/ending datetime of a days scheduling. """ res = self._schedule_days(cr, uid, id, days, day_date, compute_leaves, resource_id, context) - return res[-1][1].date() + return res[-1][1] def schedule_days(self, cr, uid, id, days, day_date=None, compute_leaves=False, resource_id=None, context=None): """Wrapper on _schedule_days: return the working intervals of a days diff --git a/addons/resource/resource_view.xml b/addons/resource/resource_view.xml index dcd7e1a67c0..6f5bc71863a 100644 --- a/addons/resource/resource_view.xml +++ b/addons/resource/resource_view.xml @@ -60,12 +60,13 @@
- - - - - - + + + + + + +
@@ -245,14 +246,17 @@ + Resource Leaves resource.calendar.leaves tree,form,calendar + - - + + + diff --git a/addons/resource/tests/test_resource.py b/addons/resource/tests/test_resource.py index fdb4982af90..1413c6732c6 100644 --- a/addons/resource/tests/test_resource.py +++ b/addons/resource/tests/test_resource.py @@ -367,16 +367,16 @@ class TestResource(TestResourceCommon): self.assertEqual(td.total_seconds() / 3600.0, 40.0, 'resource_calendar: wrong hours scheduling') res = self.resource_calendar._interval_hours_get(cr, uid, self.calendar_id, self.date1.replace(hour=6, minute=0), self.date2.replace(hour=23, minute=0) + relativedelta(days=7), resource_id=self.resource1_id, exclude_leaves=False) - print res + # print res res = self.resource_calendar.get_working_hours(cr, uid, self.calendar_id, self.date1.replace(hour=6, minute=0), self.date2.replace(hour=23, minute=0) + relativedelta(days=7), compute_leaves=False, resource_id=self.resource1_id) - print res + # print res res = self.resource_calendar._interval_hours_get(cr, uid, self.calendar_id, self.date1.replace(hour=6, minute=0), self.date2.replace(hour=23, minute=0) + relativedelta(days=7), resource_id=self.resource1_id, exclude_leaves=True) - print res + # print res res = self.resource_calendar.get_working_hours(cr, uid, self.calendar_id, self.date1.replace(hour=6, minute=0), self.date2.replace(hour=23, minute=0) + relativedelta(days=7), compute_leaves=True, resource_id=self.resource1_id) - print res + # print res def test_50_calendar_schedule_days(self): """ Testing calendar days scheduling """ @@ -384,12 +384,12 @@ class TestResource(TestResourceCommon): _format = '%Y-%m-%d %H:%M:%S' res = self.resource_calendar.schedule_days_get_date(cr, uid, self.calendar_id, 5, day_date=self.date1) - self.assertEqual(res, datetime.strptime('2013-02-26 00:0:00', _format).date(), 'resource_calendar: wrong days scheduling') + self.assertEqual(res.date(), datetime.strptime('2013-02-26 00:0:00', _format).date(), 'resource_calendar: wrong days scheduling') res = self.resource_calendar.schedule_days_get_date( cr, uid, self.calendar_id, 5, day_date=self.date1, compute_leaves=True, resource_id=self.resource1_id) - self.assertEqual(res, datetime.strptime('2013-03-01 00:0:00', _format).date(), 'resource_calendar: wrong days scheduling') + self.assertEqual(res.date(), datetime.strptime('2013-03-01 00:0:00', _format).date(), 'resource_calendar: wrong days scheduling') # -------------------------------------------------- # Misc