resource.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding:utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime
  4. from odoo import fields, models
  5. from odoo.osv.expression import AND
  6. class ResourceCalendar(models.Model):
  7. _inherit = 'resource.calendar'
  8. contracts_count = fields.Integer("# Contracts using it", compute='_compute_contracts_count', groups="hr_contract.group_hr_contract_manager")
  9. def transfer_leaves_to(self, other_calendar, resources=None, from_date=None):
  10. """
  11. Transfer some resource.calendar.leaves from 'self' to another calendar 'other_calendar'.
  12. Transfered leaves linked to `resources` (or all if `resources` is None) and starting
  13. after 'from_date' (or today if None).
  14. """
  15. from_date = from_date or fields.Datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  16. domain = [
  17. ('calendar_id', 'in', self.ids),
  18. ('date_from', '>=', from_date),
  19. ]
  20. domain = AND([domain, [('resource_id', 'in', resources.ids)]]) if resources else domain
  21. self.env['resource.calendar.leaves'].search(domain).write({
  22. 'calendar_id': other_calendar.id,
  23. })
  24. def _compute_contracts_count(self):
  25. count_data = self.env['hr.contract']._read_group(
  26. [('resource_calendar_id', 'in', self.ids)],
  27. ['resource_calendar_id'],
  28. ['resource_calendar_id'])
  29. mapped_counts = {cd['resource_calendar_id'][0]: cd['resource_calendar_id_count'] for cd in count_data}
  30. for calendar in self:
  31. calendar.contracts_count = mapped_counts.get(calendar.id, 0)
  32. def action_open_contracts(self):
  33. self.ensure_one()
  34. action = self.env["ir.actions.actions"]._for_xml_id("hr_contract.action_hr_contract")
  35. action.update({'domain': [('resource_calendar_id', '=', self.id)]})
  36. return action