res_partner.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, models
  4. from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. def _compute_im_status(self):
  8. super(ResPartner, self)._compute_im_status()
  9. absent_now = self._get_on_leave_ids()
  10. for partner in self:
  11. if partner.id in absent_now:
  12. if partner.im_status == 'online':
  13. partner.im_status = 'leave_online'
  14. elif partner.im_status == 'away':
  15. partner.im_status = 'leave_away'
  16. else:
  17. partner.im_status = 'leave_offline'
  18. @api.model
  19. def _get_on_leave_ids(self):
  20. return self.env['res.users']._get_on_leave_ids(partner=True)
  21. def mail_partner_format(self, fields=None):
  22. """Override to add the current leave status."""
  23. partners_format = super().mail_partner_format(fields=fields)
  24. if not fields:
  25. fields = {'out_of_office_date_end': True}
  26. for partner in self:
  27. if 'out_of_office_date_end' in fields:
  28. # in the rare case of multi-user partner, return the earliest possible return date
  29. dates = partner.mapped('user_ids.leave_date_to')
  30. states = partner.mapped('user_ids.current_leave_state')
  31. date = sorted(dates)[0] if dates and all(dates) else False
  32. state = sorted(states)[0] if states and all(states) else False
  33. partners_format.get(partner).update({
  34. 'out_of_office_date_end': date.strftime(DEFAULT_SERVER_DATE_FORMAT) if state == 'validate' and date else False,
  35. })
  36. return partners_format