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.

130 lines
4.2 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. from ..examples import voicent
  9. class BackendVoicent(models.Model):
  10. _name = 'backend.voicent'
  11. _description = 'Voicent Backend'
  12. _inherit = ['connector.backend']
  13. _rec_name = 'host'
  14. host = fields.Char(
  15. string='Host',
  16. required=True,
  17. )
  18. port = fields.Integer(
  19. string='Port',
  20. required=True,
  21. )
  22. next_call = fields.Datetime(
  23. string='Next Call',
  24. copy=False,
  25. )
  26. call_line_ids = fields.One2many(
  27. string='Call Lines',
  28. comodel_name='backend.voicent.call.line',
  29. inverse_name='backend_id',
  30. )
  31. time_line_ids = fields.One2many(
  32. string='Call Times',
  33. comodel_name='backend.voicent.time.line',
  34. inverse_name='backend_id',
  35. )
  36. is_active = fields.Boolean('Is Active')
  37. @api.model
  38. def _run_check_the_voicent_status(self):
  39. ''' This method is called from a cron job. '''
  40. cr_time_list = []
  41. is_next_day = False
  42. backend_voicent_rec = self.search([('is_active', '=', True)])
  43. for backend_voicent in backend_voicent_rec:
  44. current_dt = datetime.now(timezone('UTC'))
  45. user_tz = timezone(
  46. self.env.context.get('tz') or self.env.user.tz or 'UTC')
  47. dt_value = current_dt.astimezone(user_tz)
  48. convt_dt_strf = dt_value.strftime(
  49. DEFAULT_SERVER_DATETIME_FORMAT)
  50. convt_dt = datetime.strptime(
  51. convt_dt_strf,
  52. DEFAULT_SERVER_DATETIME_FORMAT)
  53. current_time = convt_dt.strftime("%H:%M")
  54. for time_line_rec in backend_voicent.time_line_ids:
  55. hours, minutes = divmod(abs(time_line_rec.time) * 60, 60)
  56. minutes = round(minutes)
  57. if minutes == 60:
  58. minutes = 0
  59. hours += 1
  60. line_time = '%02d:%02d' % (hours, minutes)
  61. cr_time_list.append(line_time)
  62. cr_time_list = sorted(cr_time_list)
  63. next_call = datetime.now()
  64. for each_time_entry in cr_time_list:
  65. if each_time_entry > current_time and not is_next_day:
  66. next_call = datetime.now().replace(
  67. hour=int(each_time_entry.split(':')[0]),
  68. minute=int(each_time_entry.split(':')[1]))
  69. is_next_day = True
  70. if cr_time_list and not is_next_day:
  71. next_call = datetime.now().replace(
  72. hour=int(cr_time_list[0].split(':')[0]),
  73. minute=int(cr_time_list[0].split(':')[1])) + timedelta(
  74. days=1)
  75. next_call_tz = timezone(self.env.context.get(
  76. 'tz') or self.env.user.tz).localize(next_call, is_dst=False)
  77. next_call_utc = next_call_tz.astimezone(timezone('UTC'))
  78. next_call_utc = datetime.strptime(
  79. fields.Datetime.to_string(next_call_utc),
  80. DEFAULT_SERVER_DATETIME_FORMAT)
  81. backend_voicent.next_call = fields.Datetime.to_string(
  82. next_call_utc)
  83. class BackendVoicentTimeLine(models.Model):
  84. _name = 'backend.voicent.time.line'
  85. _description = 'Voicent Backend Time Line'
  86. name = fields.Char(
  87. string='Name',
  88. required=True,
  89. )
  90. time = fields.Float(
  91. string='Time',
  92. copy=False,
  93. )
  94. backend_id = fields.Many2one(
  95. string='Backend',
  96. comodel_name='backend.voicent',
  97. ondelete='set null',
  98. )
  99. class BackendVoicentCallLine(models.Model):
  100. _name = 'backend.voicent.call.line'
  101. _description = 'Voicent Backend Call Line'
  102. name = fields.Char(
  103. string='Name',
  104. required=True,
  105. )
  106. applies_on = fields.Selection(
  107. string='Applies on',
  108. selection=[],
  109. )
  110. voicent_app = fields.Char(
  111. string='Voicent App',
  112. )
  113. backend_id = fields.Many2one(
  114. string='Backend',
  115. comodel_name='backend.voicent',
  116. ondelete='set null',
  117. )