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.

223 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 = "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. return self._to_dict(sr)
  26. else:
  27. raise wrapJsonException(
  28. NotFound(_("No subscription request for id %s") % _id)
  29. )
  30. def search(self, date_from=None, date_to=None):
  31. _logger.info("search from {} to {}".format(date_from, date_to))
  32. domain = []
  33. if date_from:
  34. date_from = Date.from_string(date_from)
  35. domain.append(("date", ">=", date_from))
  36. if date_to:
  37. date_to = Date.from_string(date_to)
  38. domain.append(("date", "<=", date_to))
  39. requests = self.env["subscription.request"].search(domain)
  40. response = {
  41. "count": len(requests),
  42. "rows": [self._to_dict(sr) for sr in requests],
  43. }
  44. return response
  45. def create(self, **params): # pylint: disable=method-required-super
  46. params = self._prepare_create(params)
  47. sr = self.env["subscription.request"].create(params)
  48. return self._to_dict(sr)
  49. def update(self, _id, **params):
  50. params = self._prepare_update(params)
  51. sr = self.env["subscription.request"].search(
  52. [("_api_external_id", "=", _id)]
  53. )
  54. if not sr:
  55. raise wrapJsonException(
  56. NotFound(_("No subscription request for id %s") % _id)
  57. )
  58. sr.write(params)
  59. return self._to_dict(sr)
  60. def validate(self, _id, **params):
  61. sr = self.env["subscription.request"].search(
  62. [("_api_external_id", "=", _id)]
  63. )
  64. if not sr:
  65. raise wrapJsonException(
  66. NotFound(_("No subscription request for id %s") % _id)
  67. )
  68. if sr.state != "draft":
  69. raise wrapJsonException(
  70. BadRequest(
  71. _("Subscription request %s is not in draft state") % _id
  72. )
  73. )
  74. invoice = sr.validate_subscription_request()
  75. invoice_service = self.work.component(usage="invoice")
  76. return invoice_service.get(invoice.get_api_external_id())
  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 _get_country(self, code):
  105. country = self.env["res.country"].search([("code", "=", code)])
  106. if country:
  107. return country
  108. else:
  109. raise wrapJsonException(
  110. BadRequest(_("No country for isocode %s") % code)
  111. )
  112. def _get_share_product(self, template_id):
  113. product = self.env["product.product"].search(
  114. [("product_tmpl_id", "=", template_id)]
  115. )
  116. if product:
  117. return product
  118. else:
  119. raise wrapJsonException(
  120. BadRequest(_("No share for id %s") % template_id)
  121. )
  122. def _prepare_create(self, params):
  123. """Prepare a writable dictionary of values"""
  124. address = params["address"]
  125. country = self._get_country(address["country"])
  126. share_product_id = self._get_share_product(params["share_product"])
  127. return {
  128. "name": params["name"],
  129. "email": params["email"],
  130. "ordered_parts": params["ordered_parts"],
  131. "share_product_id": share_product_id.id,
  132. "address": address["street"],
  133. "zip_code": address["zip_code"],
  134. "city": address["city"],
  135. "country_id": country.id,
  136. "lang": params["lang"],
  137. }
  138. def _prepare_update(self, params):
  139. if "address" in params:
  140. address = params["address"]
  141. if "country" in address:
  142. country = self._get_country(address["country"]).id
  143. address["country"] = country.id
  144. else:
  145. address = {}
  146. if "share_product" in params:
  147. share_product_id = self._get_share_product(
  148. params["share_product"]
  149. ).id
  150. else:
  151. share_product_id = None
  152. params = {
  153. "name": params.get("name"),
  154. "email": params.get("email"),
  155. "state": params.get("state"),
  156. "ordered_parts": params.get("ordered_parts"),
  157. "share_product_id": share_product_id,
  158. "address": address.get("street"),
  159. "zip_code": address.get("zip_code"),
  160. "city": address.get("city"),
  161. "country_id": address.get("country"),
  162. "lang": params.get("lang"),
  163. }
  164. params = {k: v for k, v in params.items() if v is not None}
  165. return params
  166. def _validator_get(self):
  167. return schemas.S_SUBSCRIPTION_REQUEST_GET
  168. def _validator_return_get(self):
  169. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  170. def _validator_search(self):
  171. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  172. def _validator_return_search(self):
  173. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  174. def _validator_create(self):
  175. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  176. def _validator_return_create(self):
  177. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  178. def _validator_update(self):
  179. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  180. def _validator_return_update(self):
  181. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  182. def _validator_validate(self):
  183. return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE
  184. def _validator_return_validate(self):
  185. return schemas.S_INVOICE_RETURN_GET