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.

96 lines
3.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 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 AccountPaymentService(Component):
  14. _name = "account.payment.service"
  15. _inherit = "emc.rest.service"
  16. _usage = "payment"
  17. _description = """
  18. Account Payment Services
  19. """
  20. def create(self, **params): # pylint: disable=method-required-super
  21. params = self._prepare_create(params)
  22. payment = self.env["account.payment"].create(params)
  23. payment.post()
  24. return self._to_dict(payment)
  25. def _prepare_create(self, params):
  26. """Prepare a writable dictionary of values"""
  27. journal = self.env["account.journal"].search(
  28. [("_api_external_id", "=", params["journal"])]
  29. )
  30. if not journal:
  31. raise wrapJsonException(
  32. NotFound(_("No journal %s on platform") % params["journal"])
  33. )
  34. invoice = self.env["account.invoice"].search(
  35. [("_api_external_id", "=", params["invoice"])]
  36. )
  37. if not invoice:
  38. raise wrapJsonException(
  39. NotFound(_("No invoice %s on platform") % params["invoice"])
  40. )
  41. payment_method_id = self.env["account.payment.method"].search(
  42. [
  43. ("code", "=", params["payment_method"]),
  44. ("payment_type", "=", params["payment_type"]),
  45. ]
  46. )
  47. if not payment_method_id:
  48. codes = (
  49. self.env["account.payment.method"].search([]).mapped("code")
  50. )
  51. raise wrapJsonException(
  52. NotFound(_("Payment method must be one of %s") % codes)
  53. )
  54. return {
  55. "payment_date": params["payment_date"],
  56. "amount": params["amount"],
  57. "payment_type": params["payment_type"],
  58. "communication": params["communication"],
  59. "invoice_ids": [(4, invoice.id, False)],
  60. "payment_method_id": payment_method_id.id,
  61. "journal_id": journal.id,
  62. "partner_type": "customer",
  63. }
  64. def _to_dict(self, payment):
  65. invoice = {
  66. "id": payment.invoice_ids.get_api_external_id(),
  67. "name": payment.invoice_ids.number,
  68. }
  69. return {
  70. "id": payment.get_api_external_id(),
  71. "journal": self._one_to_many_to_dict(payment.journal_id),
  72. "invoice": invoice,
  73. "payment_date": Date.to_string(payment.payment_date),
  74. "amount": payment.amount,
  75. "communication": payment.communication,
  76. }
  77. def _validator_create(self):
  78. return schemas.S_PAYMENT_CREATE
  79. def _validator_return_create(self):
  80. return schemas.S_PAYMENT_RETURN_GET