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.

225 lines
7.1 KiB

  1. # Copyright 2019 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. # pylint: disable=consider-merging-classes-inherited
  5. import logging
  6. from werkzeug.exceptions import BadRequest, NotFound
  7. from odoo import _
  8. from odoo.fields import Date
  9. from odoo.addons.base_rest.http import wrapJsonException
  10. from odoo.addons.component.core import Component
  11. from . import schemas
  12. _logger = logging.getLogger(__name__)
  13. class SubscriptionRequestService(Component):
  14. _inherit = "emc.rest.service"
  15. _name = "subscription.request.services"
  16. _usage = "subscription-request"
  17. _description = """
  18. Subscription Request Services
  19. """
  20. def get(self, _id):
  21. sr = self.env["subscription.request"].search(
  22. [("_api_external_id", "=", _id)]
  23. )
  24. if sr:
  25. sr._timestamp_export()
  26. return self._to_dict(sr)
  27. else:
  28. raise wrapJsonException(
  29. NotFound(_("No subscription request for id %s") % _id)
  30. )
  31. def search(self, date_from=None, date_to=None):
  32. _logger.info("search from {} to {}".format(date_from, date_to))
  33. domain = []
  34. if date_from:
  35. date_from = Date.from_string(date_from)
  36. domain.append(("date", ">=", date_from))
  37. if date_to:
  38. date_to = Date.from_string(date_to)
  39. domain.append(("date", "<=", date_to))
  40. requests = self.env["subscription.request"].search(domain)
  41. requests._timestamp_export()
  42. response = {
  43. "count": len(requests),
  44. "rows": [self._to_dict(sr) for sr in requests],
  45. }
  46. return response
  47. def create(self, **params): # pylint: disable=method-required-super
  48. params = self._prepare_create(params)
  49. sr = self.env["subscription.request"].create(params)
  50. return self._to_dict(sr)
  51. def update(self, _id, **params):
  52. params = self._prepare_update(params)
  53. sr = self.env["subscription.request"].search(
  54. [("_api_external_id", "=", _id)]
  55. )
  56. if not sr:
  57. raise wrapJsonException(
  58. NotFound(_("No subscription request for id %s") % _id)
  59. )
  60. sr.write(params)
  61. return self._to_dict(sr)
  62. def validate(self, _id, **params):
  63. sr = self.env["subscription.request"].search(
  64. [("_api_external_id", "=", _id)]
  65. )
  66. if not sr:
  67. raise wrapJsonException(
  68. NotFound(_("No subscription request for id %s") % _id)
  69. )
  70. if sr.state != "draft":
  71. raise wrapJsonException(
  72. BadRequest(
  73. _("Subscription request %s is not in draft state") % _id
  74. )
  75. )
  76. invoice = sr.validate_subscription_request()
  77. invoice_service = self.work.component(usage="invoice")
  78. return invoice_service.get(invoice.get_api_external_id())
  79. def _to_dict(self, sr):
  80. sr.ensure_one()
  81. if sr.capital_release_request:
  82. invoice_ids = [
  83. invoice.get_api_external_id()
  84. for invoice in sr.capital_release_request
  85. ]
  86. else:
  87. invoice_ids = []
  88. share_product = sr.share_product_id.product_tmpl_id
  89. return {
  90. "id": sr.get_api_external_id(),
  91. "name": sr.name,
  92. "email": sr.email,
  93. "state": sr.state,
  94. "date": Date.to_string(sr.date),
  95. "ordered_parts": sr.ordered_parts,
  96. "share_product": self._one_to_many_to_dict(share_product),
  97. "address": {
  98. "street": sr.address,
  99. "zip_code": sr.zip_code,
  100. "city": sr.city,
  101. "country": sr.country_id.code,
  102. },
  103. "lang": sr.lang,
  104. "capital_release_request": invoice_ids,
  105. }
  106. def _get_country(self, code):
  107. country = self.env["res.country"].search([("code", "=", code)])
  108. if country:
  109. return country
  110. else:
  111. raise wrapJsonException(
  112. BadRequest(_("No country for isocode %s") % code)
  113. )
  114. def _get_share_product(self, template_id):
  115. product = self.env["product.product"].search(
  116. [("product_tmpl_id", "=", template_id)]
  117. )
  118. if product:
  119. return product
  120. else:
  121. raise wrapJsonException(
  122. BadRequest(_("No share for id %s") % template_id)
  123. )
  124. def _prepare_create(self, params):
  125. """Prepare a writable dictionary of values"""
  126. address = params["address"]
  127. country = self._get_country(address["country"])
  128. share_product_id = self._get_share_product(params["share_product"])
  129. return {
  130. "name": params["name"],
  131. "email": params["email"],
  132. "ordered_parts": params["ordered_parts"],
  133. "share_product_id": share_product_id.id,
  134. "address": address["street"],
  135. "zip_code": address["zip_code"],
  136. "city": address["city"],
  137. "country_id": country.id,
  138. "lang": params["lang"],
  139. }
  140. def _prepare_update(self, params):
  141. if "address" in params:
  142. address = params["address"]
  143. if "country" in address:
  144. country = self._get_country(address["country"]).id
  145. address["country"] = country.id
  146. else:
  147. address = {}
  148. if "share_product" in params:
  149. share_product_id = self._get_share_product(
  150. params["share_product"]
  151. ).id
  152. else:
  153. share_product_id = None
  154. params = {
  155. "name": params.get("name"),
  156. "email": params.get("email"),
  157. "state": params.get("state"),
  158. "ordered_parts": params.get("ordered_parts"),
  159. "share_product_id": share_product_id,
  160. "address": address.get("street"),
  161. "zip_code": address.get("zip_code"),
  162. "city": address.get("city"),
  163. "country_id": address.get("country"),
  164. "lang": params.get("lang"),
  165. }
  166. params = {k: v for k, v in params.items() if v is not None}
  167. return params
  168. def _validator_get(self):
  169. return schemas.S_SUBSCRIPTION_REQUEST_GET
  170. def _validator_return_get(self):
  171. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  172. def _validator_search(self):
  173. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  174. def _validator_return_search(self):
  175. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  176. def _validator_create(self):
  177. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  178. def _validator_return_create(self):
  179. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  180. def _validator_update(self):
  181. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  182. def _validator_return_update(self):
  183. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  184. def _validator_validate(self):
  185. return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE
  186. def _validator_return_validate(self):
  187. return schemas.S_INVOICE_RETURN_GET