hr_holidays.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. # (c) 2016 Alfredo de la Fuente - AvanzOSC
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from openerp import models, fields, api
  5. from pytz import timezone, utc
  6. class HrHolidays(models.Model):
  7. _inherit = 'hr.holidays'
  8. @api.multi
  9. def onchange_employee(self, employee_id, date_to, date_from):
  10. res = super(HrHolidays, self).onchange_employee(employee_id)
  11. val = self.onchange_date_from(date_to, date_from, employee_id)
  12. if date_to and date_from:
  13. res['value']['number_of_days_temp'] = (
  14. val['value'].get('number_of_days_temp', False))
  15. return res
  16. @api.multi
  17. def onchange_date_from(self, date_to, date_from, employee_id):
  18. res = super(HrHolidays, self).onchange_date_from(date_to, date_from)
  19. if date_from and not date_to:
  20. date_to = res['value'].get('date_to', False)
  21. if (date_from and date_to and employee_id and
  22. res['value'].get('number_of_days_temp', False) > 0):
  23. res['value']['number_of_days_temp'] = self._remove_holidays(
  24. int(res['value'].get('number_of_days_temp')), date_to,
  25. date_from, employee_id)
  26. return res
  27. @api.multi
  28. def onchange_date_to(self, date_to, date_from, employee_id):
  29. res = super(HrHolidays, self).onchange_date_to(date_to, date_from)
  30. if (date_from and date_to and employee_id and
  31. res['value'].get('number_of_days_temp', False) > 0):
  32. res['value']['number_of_days_temp'] = self._remove_holidays(
  33. int(res['value'].get('number_of_days_temp')), date_to,
  34. date_from, employee_id)
  35. return res
  36. def _remove_holidays(self, days, date_to, date_from, employee_id):
  37. employee = self.env['hr.employee'].browse(employee_id)
  38. if not employee.address_home_id:
  39. return days
  40. date_to = fields.Date.to_string(
  41. fields.Datetime.from_string(date_to).date())
  42. date_from = fields.Date.to_string(
  43. fields.Datetime.from_string(date_from).date())
  44. cond = [('partner', '=', employee.address_home_id.id),
  45. ('date', '>=', date_from),
  46. ('date', '<=', date_to),
  47. ('festive', '=', True)]
  48. return days - len(self.env['res.partner.calendar.day'].search(cond))
  49. @api.multi
  50. def holidays_validate(self):
  51. contract_obj = self.env['hr.contract']
  52. res = super(
  53. HrHolidays,
  54. self.with_context(tracking_disable=True)).holidays_validate()
  55. for holiday in self.filtered(lambda x: x.type == 'remove'):
  56. days = holiday._find_calendar_days_from_holidays()
  57. days.write(contract_obj._prepare_partner_day_information(
  58. holiday.holiday_status_id.id))
  59. return res
  60. @api.multi
  61. def holidays_refuse(self):
  62. res = super(HrHolidays, self).holidays_refuse()
  63. for holiday in self.filtered(lambda x: x.type == 'remove'):
  64. days = holiday._find_calendar_days_from_holidays()
  65. days.write({'absence_type': False})
  66. return res
  67. @api.multi
  68. def _find_calendar_days_from_holidays(self):
  69. calendar_day_obj = self.env['res.partner.calendar.day']
  70. new_date = (fields.Datetime.from_string(self.date_from) if
  71. isinstance(self.date_from, str) else
  72. self.date_from)
  73. new_date = new_date.replace(tzinfo=utc)
  74. date_from = new_date.astimezone(
  75. timezone(self.env.user.tz)).replace(tzinfo=None)
  76. new_date = (fields.Datetime.from_string(self.date_to) if
  77. isinstance(self.date_to, str) else
  78. self.date_to)
  79. new_date = new_date.replace(tzinfo=utc)
  80. date_to = new_date.astimezone(
  81. timezone(self.env.user.tz)).replace(tzinfo=None)
  82. cond = [('partner', '=', self.employee_id.address_home_id.id),
  83. ('date', '>=', date_from.date()),
  84. ('date', '<=', date_to.date())]
  85. days = calendar_day_obj.search(cond)
  86. return days
  87. @api.multi
  88. def write(self, values):
  89. if (('state' in values and values.get('state') == 'validate') or
  90. ('manager_id' in values or 'manager_id2' in values or
  91. 'meeting_id' in values)):
  92. return super(
  93. HrHolidays,
  94. self.with_context(tracking_disable=True)).write(values)
  95. return super(HrHolidays, self).write(values)