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.

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