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.

226 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 Request Services
  20. """
  21. def get(self, _id):
  22. sr = self.env["subscription.request"].search(
  23. [("_api_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. [("_api_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. [("_api_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_api_external_id()
  82. for invoice in sr.capital_release_request
  83. ]
  84. else:
  85. invoice_ids = []
  86. share_product = sr.share_product_id.product_tmpl_id
  87. return {
  88. "id": sr.get_api_external_id(),
  89. "name": sr.name,
  90. "email": sr.email,
  91. "state": sr.state,
  92. "date": Date.to_string(sr.date),
  93. "ordered_parts": sr.ordered_parts,
  94. "share_product": {
  95. "id": share_product.get_api_external_id(),
  96. "name": share_product.name,
  97. },
  98. "address": {
  99. "street": sr.address,
  100. "zip_code": sr.zip_code,
  101. "city": sr.city,
  102. "country": sr.country_id.code,
  103. },
  104. "lang": sr.lang,
  105. "capital_release_request": invoice_ids,
  106. }
  107. def _get_country(self, code):
  108. country = self.env["res.country"].search([("code", "=", code)])
  109. if country:
  110. return country
  111. else:
  112. raise wrapJsonException(
  113. BadRequest(_("No country for isocode %s") % code)
  114. )
  115. def _get_share_product(self, template_id):
  116. product = self.env["product.product"].search(
  117. [("product_tmpl_id", "=", template_id)]
  118. )
  119. if product:
  120. return product
  121. else:
  122. raise wrapJsonException(
  123. BadRequest(_("No share for id %s") % template_id)
  124. )
  125. def _prepare_create(self, params):
  126. """Prepare a writable dictionary of values"""
  127. address = params["address"]
  128. country = self._get_country(address["country"])
  129. share_product_id = self._get_share_product(params["share_product"])
  130. return {
  131. "name": params["name"],
  132. "email": params["email"],
  133. "ordered_parts": params["ordered_parts"],
  134. "share_product_id": share_product_id.id,
  135. "address": address["street"],
  136. "zip_code": address["zip_code"],
  137. "city": address["city"],
  138. "country_id": country.id,
  139. "lang": params["lang"],
  140. }
  141. def _prepare_update(self, params):
  142. if "address" in params:
  143. address = params["address"]
  144. if "country" in address:
  145. country = self._get_country(address["country"]).id
  146. address["country"] = country.id
  147. else:
  148. address = {}
  149. if "share_product" in params:
  150. share_product_id = self._get_share_product(
  151. params["share_product"]
  152. ).id
  153. else:
  154. share_product_id = None
  155. params = {
  156. "name": params.get("name"),
  157. "email": params.get("email"),
  158. "state": params.get("state"),
  159. "ordered_parts": params.get("ordered_parts"),
  160. "share_product_id": share_product_id,
  161. "address": address.get("street"),
  162. "zip_code": address.get("zip_code"),
  163. "city": address.get("city"),
  164. "country_id": address.get("country"),
  165. "lang": params.get("lang"),
  166. }
  167. params = {k: v for k, v in params.items() if v is not None}
  168. return params
  169. def _validator_get(self):
  170. return schemas.S_SUBSCRIPTION_REQUEST_GET
  171. def _validator_return_get(self):
  172. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  173. def _validator_search(self):
  174. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  175. def _validator_return_search(self):
  176. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  177. def _validator_create(self):
  178. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  179. def _validator_return_create(self):
  180. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  181. def _validator_update(self):
  182. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  183. def _validator_return_update(self):
  184. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  185. def _validator_validate(self):
  186. return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE
  187. def _validator_return_validate(self):
  188. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET