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.

121 lines
4.1 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from datetime import date, timedelta
  6. from odoo import api, fields, models
  7. from ..components.emc_adapters import SubscriptionRequestAdapter
  8. _logger = logging.getLogger(__name__)
  9. class SubscriptionRequest(models.Model):
  10. _inherit = "subscription.request"
  11. source = fields.Selection(selection_add=[("emc_api", "Easy My Coop API")])
  12. binding_id = fields.One2many(
  13. comodel_name="emc.binding.subscription.request",
  14. inverse_name="internal_id",
  15. string="Binding ID",
  16. required=False,
  17. )
  18. @api.model
  19. def fetch_subscription_requests(self, date_from=None, date_to=None):
  20. SRBinding = self.env["emc.binding.subscription.request"]
  21. backend = self.env["emc.backend"].get_backend()
  22. adapter = SubscriptionRequestAdapter(backend=backend)
  23. requests_dict = adapter.search(date_from=date_from, date_to=date_to)
  24. for external_id, request_dict in requests_dict["rows"]:
  25. sr_binding = SRBinding.search_binding(backend, external_id)
  26. if sr_binding: # update request
  27. sr_binding.internal_id.write(request_dict)
  28. else:
  29. srequest = self.env["subscription.request"].create(
  30. request_dict
  31. )
  32. SRBinding.create(
  33. {
  34. "backend_id": backend.id,
  35. "external_id": external_id,
  36. "internal_id": srequest.id,
  37. }
  38. )
  39. external_ids = [
  40. external_id for external_id, _ in requests_dict["rows"]
  41. ]
  42. srequests = SRBinding.search(
  43. [
  44. ("backend_id", "=", backend.id),
  45. ("external_id", "in", external_ids),
  46. ]
  47. ).mapped("internal_id")
  48. return srequests
  49. @api.model
  50. def backend_read(self, external_id):
  51. SRBinding = self.env["emc.binding.subscription.request"]
  52. backend = self.env["emc.backend"].get_backend()
  53. adapter = SubscriptionRequestAdapter(backend)
  54. _, request_values = adapter.read(external_id)
  55. sr_binding = SRBinding.search_binding(backend, external_id)
  56. if sr_binding: # update request
  57. srequest = sr_binding.internal_id
  58. srequest.write(request_values)
  59. else:
  60. srequest = self.env["subscription.request"].create(request_values)
  61. SRBinding.create(
  62. {
  63. "backend_id": backend.id,
  64. "external_id": external_id,
  65. "internal_id": srequest.id,
  66. }
  67. )
  68. return srequest
  69. @api.model
  70. def fetch_subscription_requests_cron(self):
  71. backend = self.env["emc.backend"].get_backend()
  72. date_to = date.today()
  73. date_from = date_to - timedelta(days=1)
  74. _logger.info(
  75. "fetching subscription requests at {backend} from {date_from} to "
  76. "{date_to}.".format(
  77. backend=backend.name, date_from=date_from, date_to=date_to
  78. )
  79. )
  80. self.fetch_subscription_requests(date_from=date_from, date_to=date_to)
  81. _logger.info("fetch done.")
  82. @api.multi
  83. def validate_subscription_request(self):
  84. self.ensure_one()
  85. invoice = super(
  86. SubscriptionRequest, self
  87. ).validate_subscription_request()
  88. if self.source == "emc_api":
  89. backend = self.env["emc.backend"].get_backend()
  90. sr_adapter = SubscriptionRequestAdapter(backend=backend)
  91. external_id, invoice_dict = sr_adapter.validate(
  92. self.binding_id.external_id
  93. )
  94. InvoiceBinding = self.env["emc.binding.account.invoice"]
  95. InvoiceBinding.create(
  96. {
  97. "backend_id": backend.id,
  98. "external_id": external_id,
  99. "internal_id": invoice.id,
  100. }
  101. )
  102. return invoice