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.0 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 = "base.rest.service"
  15. _name = "subscription.request.services"
  16. _usage = "subscription-request"
  17. _collection = "emc.services"
  18. _description = """
  19. Subscription requests
  20. """
  21. def get(self, _id):
  22. sr = self.env["subscription.request"].search(
  23. [("external_id", "=", _id)]
  24. )
  25. if sr:
  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. response = {
  42. "count": len(requests),
  43. "rows": [self._to_dict(sr) for sr in requests],
  44. }
  45. return response
  46. def create(self, **params): # pylint: disable=method-required-super
  47. params = self._prepare_create(params)
  48. sr = self.env["subscription.request"].create(params)
  49. return self._to_dict(sr)
  50. def update(self, _id, **params):
  51. params = self._prepare_update(params)
  52. sr = self.env["subscription.request"].search(
  53. [("external_id", "=", _id)]
  54. )
  55. if not sr:
  56. raise wrapJsonException(
  57. NotFound(_("No subscription request for id %s") % _id)
  58. )
  59. sr.write(params)
  60. return self._to_dict(sr)
  61. def validate(self, _id, **params):
  62. sr = self.env["subscription.request"].search(
  63. [("external_id", "=", _id)]
  64. )
  65. if not sr:
  66. raise wrapJsonException(
  67. NotFound(_("No subscription request for id %s") % _id)
  68. )
  69. if sr.state != "draft":
  70. raise wrapJsonException(
  71. BadRequest(
  72. _("Subscription request %s is not in draft state") % _id
  73. )
  74. )
  75. sr.validate_subscription_request()
  76. return self._to_dict(sr)
  77. def _to_dict(self, sr):
  78. sr.ensure_one()
  79. if sr.capital_release_request:
  80. invoice_ids = [
  81. invoice.get_external_id()
  82. for invoice in sr.capital_release_request
  83. ]
  84. else:
  85. invoice_ids = []
  86. return {
  87. "id": sr.get_external_id(),
  88. "name": sr.name,
  89. "email": sr.email,
  90. "state": sr.state,
  91. "date": Date.to_string(sr.date),
  92. "ordered_parts": sr.ordered_parts,
  93. "share_product": {
  94. "id": sr.share_product_id.product_tmpl_id.id,
  95. "name": sr.share_product_id.product_tmpl_id.name,
  96. },
  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_SUBSCRIPTION_REQUEST_RETURN_GET