You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.4 KiB

  1. # Copyright (C) 2019 Open Source Integrators
  2. # <https://www.opensourceintegrators.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. from datetime import datetime, timedelta
  6. from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
  7. from pytz import timezone
  8. class BackendVoicent(models.Model):
  9. _name = 'backend.voicent'
  10. _description = 'Voicent Backend'
  11. _inherit = ['connector.backend']
  12. name = fields.Char(string='Name', required=True)
  13. host = fields.Char(string='Host', default='localhost', required=True)
  14. port = fields.Integer(string='Port', default='8155', required=True)
  15. callerid = fields.Char(string='Caller ID', required=True)
  16. line = fields.Integer(string='Number of lines', required=True)
  17. next_call = fields.Datetime(string='Next Call', copy=False)
  18. call_line_ids = fields.One2many(
  19. string='Call Lines',
  20. comodel_name='backend.voicent.call.line',
  21. inverse_name='backend_id')
  22. time_line_ids = fields.One2many(
  23. string='Call Times',
  24. comodel_name='backend.voicent.time.line',
  25. inverse_name='backend_id')
  26. active = fields.Boolean('Active', default=True)
  27. @api.model
  28. def _run_update_next_call(self):
  29. """ This method is called from a cron job. """
  30. cr_time_list = []
  31. backends = self.search([('active', '=', True)])
  32. for backend in backends:
  33. current_dt = datetime.now(timezone('UTC'))
  34. user_tz = timezone(
  35. self.env.context.get('tz') or self.env.user.tz or 'UTC')
  36. dt_value = current_dt.astimezone(user_tz)
  37. convt_dt_strf = dt_value.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  38. convt_dt = datetime.strptime(
  39. convt_dt_strf,
  40. DEFAULT_SERVER_DATETIME_FORMAT)
  41. current_time = convt_dt.strftime("%H:%M")
  42. for time_line_rec in backend.time_line_ids:
  43. hours, minutes = divmod(abs(time_line_rec.time) * 60, 60)
  44. minutes = round(minutes)
  45. if minutes == 60:
  46. minutes = 0
  47. hours += 1
  48. line_time = '%02d:%02d' % (hours, minutes)
  49. cr_time_list.append(line_time)
  50. cr_time_list = sorted(cr_time_list)
  51. next_call = False
  52. for time_entry in cr_time_list:
  53. if time_entry > current_time:
  54. next_call = datetime.now().replace(
  55. hour=int(time_entry.split(':')[0]),
  56. minute=int(time_entry.split(':')[1]),
  57. second=0)
  58. break
  59. if not next_call:
  60. next_call = datetime.now().replace(
  61. hour=int(cr_time_list[0].split(':')[0]),
  62. minute=int(cr_time_list[0].split(':')[1]),
  63. second=0) + timedelta(
  64. days=0)
  65. next_call_tz = timezone(self.env.context.get('tz') or
  66. self.env.user.tz).localize(next_call,
  67. is_dst=False)
  68. next_call_utc = next_call_tz.astimezone(timezone('UTC'))
  69. next_call_utc = datetime.strptime(
  70. fields.Datetime.to_string(next_call_utc),
  71. DEFAULT_SERVER_DATETIME_FORMAT)
  72. backend.next_call = fields.Datetime.to_string(next_call_utc)