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.

190 lines
6.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. 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 as bad_request:
  71. raise ValidationError(
  72. _(
  73. "The Synergie platform replied with this error message:"
  74. "\n\n %s \n\n"
  75. "Please contact your system administrator."
  76. )
  77. % bad_request.description
  78. )
  79. ai_adapter = AccountInvoiceAdapter(backend=self.backend)
  80. return ai_adapter.to_write_values(invoice_dict)
  81. def to_write_values(self, api_dict):
  82. Country = self.backend.env["res.country"]
  83. ProductTemplateBinding = self.backend.env[
  84. "emc.binding.product.template"
  85. ]
  86. address = api_dict["address"]
  87. country = Country.search([("code", "=", address["country"])])
  88. external_product_id = api_dict["share_product"]["id"]
  89. share_product_binding = ProductTemplateBinding.search_binding(
  90. self.backend, external_product_id
  91. )
  92. if not share_product_binding:
  93. raise UserError(
  94. _(
  95. "No binding exists for share product %s. Please contact "
  96. "system administrator "
  97. )
  98. % api_dict["share_product"]["name"]
  99. )
  100. product_product = share_product_binding.internal_id.product_variant_id
  101. external_id = api_dict["id"]
  102. writable_dict = {
  103. "email": api_dict["email"],
  104. "name": api_dict["name"],
  105. "date": api_dict["date"],
  106. "state": api_dict["state"],
  107. "lang": api_dict["lang"],
  108. "ordered_parts": api_dict["ordered_parts"],
  109. "address": address["street"],
  110. "zip_code": address["zip_code"],
  111. "city": address["city"],
  112. "country_id": country.id,
  113. "share_product_id": product_product.id,
  114. "source": "emc_api",
  115. }
  116. return external_id, writable_dict
  117. class AccountInvoiceAdapter(AbstractEMCAdapter):
  118. _model = "account.invoice"
  119. _service = "invoice"
  120. def to_write_values(self, api_dict):
  121. external_id = api_dict.pop("id")
  122. writable_dict = api_dict
  123. return external_id, writable_dict
  124. class AccountPaymentAdapter(AbstractEMCAdapter):
  125. _model = "account.payment"
  126. _service = "payment"
  127. def to_write_values(self, api_dict):
  128. api_dict = api_dict.copy()
  129. external_id = api_dict.pop("id")
  130. writable_dict = api_dict
  131. return external_id, writable_dict
  132. def to_api_dict(self, record):
  133. if not record.journal_id.binding_id:
  134. raise ValidationError(
  135. _(
  136. "Journal %s is not bound to a journal on the platform. "
  137. "Please contact system administrator."
  138. )
  139. % record.journal_id.name
  140. )
  141. if not record.invoice_ids.binding_id:
  142. raise ValidationError(
  143. _(
  144. "Invoice %s is not bound to a journal on the platform. "
  145. "Please contact system administrator."
  146. )
  147. % record.invoice_ids.name
  148. )
  149. return {
  150. "journal": record.journal_id.binding_id.external_id,
  151. "invoice": record.invoice_ids.binding_id.external_id,
  152. "payment_date": Date.to_string(record.payment_date),
  153. "amount": record.amount,
  154. "communication": record.communication,
  155. "payment_type": record.payment_type,
  156. "payment_method": record.payment_method_id.code,
  157. }