resource_mixin.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from dateutil.relativedelta import relativedelta
  5. from pytz import utc
  6. from odoo import api, fields, models
  7. def timezone_datetime(time):
  8. if not time.tzinfo:
  9. time = time.replace(tzinfo=utc)
  10. return time
  11. class ResourceMixin(models.AbstractModel):
  12. _name = "resource.mixin"
  13. _description = 'Resource Mixin'
  14. resource_id = fields.Many2one(
  15. 'resource.resource', 'Resource',
  16. auto_join=True, index=True, ondelete='restrict', required=True)
  17. company_id = fields.Many2one(
  18. 'res.company', 'Company',
  19. default=lambda self: self.env.company,
  20. index=True, related='resource_id.company_id', store=True, readonly=False)
  21. resource_calendar_id = fields.Many2one(
  22. 'resource.calendar', 'Working Hours',
  23. default=lambda self: self.env.company.resource_calendar_id,
  24. index=True, related='resource_id.calendar_id', store=True, readonly=False)
  25. tz = fields.Selection(
  26. string='Timezone', related='resource_id.tz', readonly=False,
  27. help="This field is used in order to define in which timezone the resources will work.")
  28. @api.model_create_multi
  29. def create(self, vals_list):
  30. resources_vals_list = []
  31. calendar_ids = [vals['resource_calendar_id'] for vals in vals_list if vals.get('resource_calendar_id')]
  32. calendars_tz = {calendar.id: calendar.tz for calendar in self.env['resource.calendar'].browse(calendar_ids)}
  33. for vals in vals_list:
  34. if not vals.get('resource_id'):
  35. resources_vals_list.append(
  36. self._prepare_resource_values(
  37. vals,
  38. vals.pop('tz', False) or calendars_tz.get(vals.get('resource_calendar_id'))
  39. )
  40. )
  41. if resources_vals_list:
  42. resources = self.env['resource.resource'].create(resources_vals_list)
  43. resources_iter = iter(resources.ids)
  44. for vals in vals_list:
  45. if not vals.get('resource_id'):
  46. vals['resource_id'] = next(resources_iter)
  47. return super(ResourceMixin, self.with_context(check_idempotence=True)).create(vals_list)
  48. def _prepare_resource_values(self, vals, tz):
  49. resource_vals = {'name': vals.get(self._rec_name)}
  50. if tz:
  51. resource_vals['tz'] = tz
  52. company_id = vals.get('company_id', self.env.company.id)
  53. if company_id:
  54. resource_vals['company_id'] = company_id
  55. calendar_id = vals.get('resource_calendar_id')
  56. if calendar_id:
  57. resource_vals['calendar_id'] = calendar_id
  58. return resource_vals
  59. def copy_data(self, default=None):
  60. if default is None:
  61. default = {}
  62. resource_default = {}
  63. if 'company_id' in default:
  64. resource_default['company_id'] = default['company_id']
  65. if 'resource_calendar_id' in default:
  66. resource_default['calendar_id'] = default['resource_calendar_id']
  67. resource = self.resource_id.copy(resource_default)
  68. default['resource_id'] = resource.id
  69. default['company_id'] = resource.company_id.id
  70. default['resource_calendar_id'] = resource.calendar_id.id
  71. return super(ResourceMixin, self).copy_data(default)
  72. def _get_work_days_data_batch(self, from_datetime, to_datetime, compute_leaves=True, calendar=None, domain=None):
  73. """
  74. By default the resource calendar is used, but it can be
  75. changed using the `calendar` argument.
  76. `domain` is used in order to recognise the leaves to take,
  77. None means default value ('time_type', '=', 'leave')
  78. Returns a dict {'days': n, 'hours': h} containing the
  79. quantity of working time expressed as days and as hours.
  80. """
  81. resources = self.mapped('resource_id')
  82. mapped_employees = {e.resource_id.id: e.id for e in self}
  83. result = {}
  84. # naive datetimes are made explicit in UTC
  85. from_datetime = timezone_datetime(from_datetime)
  86. to_datetime = timezone_datetime(to_datetime)
  87. mapped_resources = defaultdict(lambda: self.env['resource.resource'])
  88. for record in self:
  89. mapped_resources[calendar or record.resource_calendar_id] |= record.resource_id
  90. for calendar, calendar_resources in mapped_resources.items():
  91. if not calendar:
  92. for calendar_resource in calendar_resources:
  93. result[calendar_resource.id] = {'days': 0, 'hours': 0}
  94. continue
  95. day_total = calendar._get_resources_day_total(from_datetime, to_datetime, calendar_resources)
  96. # actual hours per day
  97. if compute_leaves:
  98. intervals = calendar._work_intervals_batch(from_datetime, to_datetime, calendar_resources, domain)
  99. else:
  100. intervals = calendar._attendance_intervals_batch(from_datetime, to_datetime, calendar_resources)
  101. for calendar_resource in calendar_resources:
  102. result[calendar_resource.id] = calendar._get_days_data(intervals[calendar_resource.id], day_total[calendar_resource.id])
  103. # convert "resource: result" into "employee: result"
  104. return {mapped_employees[r.id]: result[r.id] for r in resources}
  105. def _get_leave_days_data_batch(self, from_datetime, to_datetime, calendar=None, domain=None):
  106. """
  107. By default the resource calendar is used, but it can be
  108. changed using the `calendar` argument.
  109. `domain` is used in order to recognise the leaves to take,
  110. None means default value ('time_type', '=', 'leave')
  111. Returns a dict {'days': n, 'hours': h} containing the number of leaves
  112. expressed as days and as hours.
  113. """
  114. resources = self.mapped('resource_id')
  115. mapped_employees = {e.resource_id.id: e.id for e in self}
  116. result = {}
  117. # naive datetimes are made explicit in UTC
  118. from_datetime = timezone_datetime(from_datetime)
  119. to_datetime = timezone_datetime(to_datetime)
  120. mapped_resources = defaultdict(lambda: self.env['resource.resource'])
  121. for record in self:
  122. mapped_resources[calendar or record.resource_calendar_id] |= record.resource_id
  123. for calendar, calendar_resources in mapped_resources.items():
  124. day_total = calendar._get_resources_day_total(from_datetime, to_datetime, calendar_resources)
  125. # compute actual hours per day
  126. attendances = calendar._attendance_intervals_batch(from_datetime, to_datetime, calendar_resources)
  127. leaves = calendar._leave_intervals_batch(from_datetime, to_datetime, calendar_resources, domain)
  128. for calendar_resource in calendar_resources:
  129. result[calendar_resource.id] = calendar._get_days_data(
  130. attendances[calendar_resource.id] & leaves[calendar_resource.id],
  131. day_total[calendar_resource.id]
  132. )
  133. # convert "resource: result" into "employee: result"
  134. return {mapped_employees[r.id]: result[r.id] for r in resources}
  135. def _adjust_to_calendar(self, start, end):
  136. resource_results = self.resource_id._adjust_to_calendar(start, end)
  137. # change dict keys from resources to associated records.
  138. return {
  139. record: resource_results[record.resource_id]
  140. for record in self
  141. }
  142. def list_work_time_per_day(self, from_datetime, to_datetime, calendar=None, domain=None):
  143. """
  144. By default the resource calendar is used, but it can be
  145. changed using the `calendar` argument.
  146. `domain` is used in order to recognise the leaves to take,
  147. None means default value ('time_type', '=', 'leave')
  148. Returns a list of tuples (day, hours) for each day
  149. containing at least an attendance.
  150. """
  151. resource = self.resource_id
  152. calendar = calendar or self.resource_calendar_id
  153. # naive datetimes are made explicit in UTC
  154. if not from_datetime.tzinfo:
  155. from_datetime = from_datetime.replace(tzinfo=utc)
  156. if not to_datetime.tzinfo:
  157. to_datetime = to_datetime.replace(tzinfo=utc)
  158. compute_leaves = self.env.context.get('compute_leaves', True)
  159. intervals = calendar._work_intervals_batch(from_datetime, to_datetime, resource, domain, compute_leaves=compute_leaves)[resource.id]
  160. result = defaultdict(float)
  161. for start, stop, meta in intervals:
  162. result[start.date()] += (stop - start).total_seconds() / 3600
  163. return sorted(result.items())
  164. def list_leaves(self, from_datetime, to_datetime, calendar=None, domain=None):
  165. """
  166. By default the resource calendar is used, but it can be
  167. changed using the `calendar` argument.
  168. `domain` is used in order to recognise the leaves to take,
  169. None means default value ('time_type', '=', 'leave')
  170. Returns a list of tuples (day, hours, resource.calendar.leaves)
  171. for each leave in the calendar.
  172. """
  173. resource = self.resource_id
  174. calendar = calendar or self.resource_calendar_id
  175. # naive datetimes are made explicit in UTC
  176. if not from_datetime.tzinfo:
  177. from_datetime = from_datetime.replace(tzinfo=utc)
  178. if not to_datetime.tzinfo:
  179. to_datetime = to_datetime.replace(tzinfo=utc)
  180. attendances = calendar._attendance_intervals_batch(from_datetime, to_datetime, resource)[resource.id]
  181. leaves = calendar._leave_intervals_batch(from_datetime, to_datetime, resource, domain)[resource.id]
  182. result = []
  183. for start, stop, leave in (leaves & attendances):
  184. hours = (stop - start).total_seconds() / 3600
  185. result.append((start.date(), hours, leave))
  186. return result