summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Martinelli <nim@odoo.com>2016-05-02 20:05:57 +0200
committerNicolas Martinelli <nim@odoo.com>2016-05-03 13:37:30 +0200
commitaa87e66c46f1d4eea764ea6b62b7d0e79fecd256 (patch)
tree55a64e4862fd1fb1b1392c631a010e83127a368b
parent6b32ee456c18af39b088b66f1abcea38793d4bff (diff)
[IMP] hr_holidays: resource calendar in hr_holidays
The module hr_holidays now uses the resource calendar to compute the number of days in a leave request. opw-676596
-rw-r--r--addons/hr_holidays/__openerp__.py2
-rw-r--r--addons/hr_holidays/hr_holidays.py34
-rw-r--r--addons/hr_holidays/hr_holidays_view.xml4
3 files changed, 27 insertions, 13 deletions
diff --git a/addons/hr_holidays/__openerp__.py b/addons/hr_holidays/__openerp__.py
index 32a7f5ab2a3..3c39fb66d13 100644
--- a/addons/hr_holidays/__openerp__.py
+++ b/addons/hr_holidays/__openerp__.py
@@ -25,7 +25,7 @@ You can keep track of leaves in different ways by following reports:
A synchronization with an internal agenda (Meetings of the CRM module) is also possible in order to automatically create a meeting when a leave request is accepted by setting up a type of meeting in Leave Type.
""",
- 'depends': ['hr', 'calendar', 'resource'],
+ 'depends': ['hr', 'calendar', 'resource', 'product'],
'data': [
'data/report_paperformat.xml',
'security/ir.model.access.csv',
diff --git a/addons/hr_holidays/hr_holidays.py b/addons/hr_holidays/hr_holidays.py
index 7151517512a..fdf21b96707 100644
--- a/addons/hr_holidays/hr_holidays.py
+++ b/addons/hr_holidays/hr_holidays.py
@@ -276,16 +276,30 @@ class hr_holidays(osv.osv):
result['value'] = {'department_id': employee.department_id.id}
return result
- # TODO: can be improved using resource calendar method
- def _get_number_of_days(self, date_from, date_to):
+ def _get_number_of_days(self, cr, uid, date_from, date_to, employee_id, context=None):
"""Returns a float equals to the timedelta between two dates given as string."""
+ resource_obj = self.pool['resource.resource']
+ employee_obj = self.pool['hr.employee']
+ model_data_obj = self.pool['ir.model.data']
+ uom_obj = self.pool['product.uom']
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT)
to_dt = datetime.datetime.strptime(date_to, DATETIME_FORMAT)
+
+ if employee_id:
+ employee = employee_obj.browse(cr, uid, [employee_id], context=context)
+ resource_ids = resource_obj.search(cr, uid, [('user_id', '=', employee.user_id.id)], limit=1, context=context)
+ resource = resource_obj.browse(cr, uid, resource_ids, context=context)
+ if resource and resource.calendar_id:
+ hours = resource.calendar_id.get_working_hours(from_dt, to_dt, resource_id=resource.id, compute_leaves=True)
+ uom_hour = model_data_obj.xmlid_to_object(cr, uid, 'product.product_uom_hour')
+ uom_day = model_data_obj.xmlid_to_object(cr, uid, 'product.product_uom_day')
+ if uom_hour and uom_day:
+ return uom_obj._compute_qty_obj(cr, uid, uom_hour, hours[0], uom_day, context=context)
+
timedelta = to_dt - from_dt
- diff_day = timedelta.days + float(timedelta.seconds) / 86400
- return diff_day
+ return math.ceil(timedelta.days + float(timedelta.seconds) / 86400)
def unlink(self, cr, uid, ids, context=None):
for rec in self.browse(cr, uid, ids, context=context):
@@ -293,7 +307,7 @@ class hr_holidays(osv.osv):
raise UserError(_('You cannot delete a leave which is in %s state.') % (rec.state,))
return super(hr_holidays, self).unlink(cr, uid, ids, context)
- def onchange_date_from(self, cr, uid, ids, date_to, date_from):
+ def onchange_date_from(self, cr, uid, ids, date_to, date_from, employee_id, context=None):
"""
If there are no date set for date_to, automatically set one 8 hours later than
the date_from.
@@ -312,14 +326,14 @@ class hr_holidays(osv.osv):
# Compute and update the number of days
if (date_to and date_from) and (date_from <= date_to):
- diff_day = self._get_number_of_days(date_from, date_to)
- result['value']['number_of_days_temp'] = round(math.floor(diff_day))+1
+ diff_day = self._get_number_of_days(cr, uid, date_from, date_to, employee_id, context=context)
+ result['value']['number_of_days_temp'] = diff_day
else:
result['value']['number_of_days_temp'] = 0
return result
- def onchange_date_to(self, cr, uid, ids, date_to, date_from):
+ def onchange_date_to(self, cr, uid, ids, date_to, date_from, employee_id, context=None):
"""
Update the number_of_days.
"""
@@ -331,8 +345,8 @@ class hr_holidays(osv.osv):
# Compute and update the number of days
if (date_to and date_from) and (date_from <= date_to):
- diff_day = self._get_number_of_days(date_from, date_to)
- result['value']['number_of_days_temp'] = round(math.floor(diff_day))+1
+ diff_day = self._get_number_of_days(cr, uid, date_from, date_to, employee_id, context=context)
+ result['value']['number_of_days_temp'] = diff_day
else:
result['value']['number_of_days_temp'] = 0
return result
diff --git a/addons/hr_holidays/hr_holidays_view.xml b/addons/hr_holidays/hr_holidays_view.xml
index 8ff9f4ca9ce..68b53cdb74a 100644
--- a/addons/hr_holidays/hr_holidays_view.xml
+++ b/addons/hr_holidays/hr_holidays_view.xml
@@ -100,9 +100,9 @@
<label for="number_of_days_temp" string="Duration"/>
<div>
<div attrs="{'invisible': [('type', '=', 'add')]}">
- <field name="date_from" on_change="onchange_date_from(date_to, date_from)" attrs="{'required':[('type', '=', 'remove')]}" class="oe_inline"/>
+ <field name="date_from" on_change="onchange_date_from(date_to, date_from, employee_id, context)" attrs="{'required':[('type', '=', 'remove')]}" class="oe_inline"/>
<label string="-" class="oe_inline"/>
- <field name="date_to" on_change="onchange_date_to(date_to, date_from)" attrs="{'required':[('type', '=', 'remove')]}" class="oe_inline"/>
+ <field name="date_to" on_change="onchange_date_to(date_to, date_from, employee_id, context)" attrs="{'required':[('type', '=', 'remove')]}" class="oe_inline"/>
</div>
<div>
<field name="number_of_days_temp" class="oe_inline"/> days