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.

189 lines
6.0 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. from os.path import join
  5. from werkzeug.exceptions import BadRequest
  6. from odoo import _
  7. from odoo.exceptions import UserError, ValidationError
  8. from odoo.fields import Date
  9. class AbstractEMCAdapter:
  10. _model = "set in implementation class"
  11. _root = "api"
  12. _service = "set in implementation class"
  13. def __init__(self, backend):
  14. self.backend = backend
  15. def _get_url(self, args=None):
  16. """args is a list of path elements
  17. :return the complete route to the service
  18. """
  19. if args is None:
  20. args = []
  21. return join("/", self._root, self._service, *args)
  22. def search(self, **params):
  23. raise NotImplementedError
  24. def read(self, id_):
  25. # pylint: disable=method-required-super
  26. url = self._get_url([str(id_)])
  27. api_dict = self.backend.http_get_content(url)
  28. return self.to_write_values(api_dict)
  29. def create(self, record):
  30. # pylint: disable=method-required-super
  31. url = self._get_url()
  32. api_dict = self.to_api_dict(record)
  33. external_record = self.backend.http_post_content(url, api_dict)
  34. external_id, writeable_dict = self.to_write_values(external_record)
  35. return external_id, writeable_dict
  36. def update(self):
  37. raise NotImplementedError
  38. def delete(self):
  39. raise NotImplementedError
  40. def to_write_values(self, api_dict):
  41. """
  42. :return a tuple with
  43. - the external id
  44. - a writable dictionary for _model
  45. received from the api
  46. """
  47. raise NotImplementedError
  48. def to_api_dict(self, record):
  49. raise NotImplementedError
  50. class SubscriptionRequestAdapter(AbstractEMCAdapter):
  51. _model = "subscription.request"
  52. _service = "subscription-request"
  53. def search(self, date_from=None, date_to=None):
  54. url = self._get_url()
  55. params = {}
  56. if date_from:
  57. params.update({"date_from": Date.to_string(date_from)})
  58. if date_to:
  59. params.update({"date_to": Date.to_string(date_to)})
  60. sr_list = self.backend.http_get_content(url, params=params)
  61. return {
  62. "count": sr_list["count"],
  63. "rows": [self.to_write_values(row) for row in sr_list["rows"]],
  64. }
  65. def validate(self, id_):
  66. url = self._get_url([str(id_), "validate"])
  67. data = {}
  68. try:
  69. invoice_dict = self.backend.http_post_content(url, data)
  70. except BadRequest:
  71. raise ValidationError(
  72. _(
  73. "The request was already validated on the "
  74. "platform. Please check data consistency "
  75. "with your system administrator."
  76. )
  77. )
  78. ai_adapter = AccountInvoiceAdapter(backend=self.backend)
  79. return ai_adapter.to_write_values(invoice_dict)
  80. def to_write_values(self, api_dict):
  81. Country = self.backend.env["res.country"]
  82. ProductTemplateBinding = self.backend.env[
  83. "emc.binding.product.template"
  84. ]
  85. address = api_dict["address"]
  86. country = Country.search([("code", "=", address["country"])])
  87. external_product_id = api_dict["share_product"]["id"]
  88. share_product_binding = ProductTemplateBinding.search_binding(
  89. self.backend, external_product_id
  90. )
  91. if not share_product_binding:
  92. raise UserError(
  93. _(
  94. "No binding exists for share product %s. Please contact "
  95. "system administrator "
  96. )
  97. % api_dict["share_product"]["name"]
  98. )
  99. product_product = share_product_binding.internal_id.product_variant_id
  100. external_id = api_dict["id"]
  101. writable_dict = {
  102. "email": api_dict["email"],
  103. "name": api_dict["name"],
  104. "date": api_dict["date"],
  105. "state": api_dict["state"],
  106. "lang": api_dict["lang"],
  107. "ordered_parts": api_dict["ordered_parts"],
  108. "address": address["street"],
  109. "zip_code": address["zip_code"],
  110. "city": address["city"],
  111. "country_id": country.id,
  112. "share_product_id": product_product.id,
  113. "source": "emc_api",
  114. }
  115. return external_id, writable_dict
  116. class AccountInvoiceAdapter(AbstractEMCAdapter):
  117. _model = "account.invoice"
  118. _service = "invoice"
  119. def to_write_values(self, api_dict):
  120. external_id = api_dict.pop("id")
  121. writable_dict = api_dict
  122. return external_id, writable_dict
  123. class AccountPaymentAdapter(AbstractEMCAdapter):
  124. _model = "account.payment"
  125. _service = "payment"
  126. def to_write_values(self, api_dict):
  127. api_dict = api_dict.copy()
  128. external_id = api_dict.pop("id")
  129. writable_dict = api_dict
  130. return external_id, writable_dict
  131. def to_api_dict(self, record):
  132. if not record.journal_id.binding_id:
  133. raise ValidationError(
  134. _(
  135. "Journal %s is not bound to a journal on the platform. "
  136. "Please contact system administrator."
  137. )
  138. % record.journal_id.name
  139. )
  140. if not record.invoice_ids.binding_id:
  141. raise ValidationError(
  142. _(
  143. "Invoice %s is not bound to a journal on the platform. "
  144. "Please contact system administrator."
  145. )
  146. % record.invoice_ids.name
  147. )
  148. return {
  149. "journal": record.journal_id.binding_id.external_id,
  150. "invoice": record.invoice_ids.binding_id.external_id,
  151. "payment_date": Date.to_string(record.payment_date),
  152. "amount": record.amount,
  153. "communication": record.communication,
  154. "payment_type": record.payment_type,
  155. "payment_method": record.payment_method_id.code,
  156. }