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.

78 lines
2.3 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 AccountInvoiceService(Component):
  14. _name = "account.invoice.service"
  15. _inherit = "emc.rest.service"
  16. _usage = "invoice"
  17. _description = """
  18. Account Invoice Services
  19. """
  20. def get(self, _id):
  21. sr = self.env["account.invoice"].search(
  22. [("_api_external_id", "=", _id)]
  23. )
  24. if sr:
  25. return self._to_dict(sr)
  26. else:
  27. raise wrapJsonException(
  28. NotFound(_("No invoice found for id %s") % _id)
  29. )
  30. def _to_dict(self, invoice):
  31. invoice.ensure_one()
  32. data = {
  33. "id": invoice.get_api_external_id(),
  34. "name": invoice.name,
  35. "state": invoice.state,
  36. "type": invoice.type,
  37. "date": Date.to_string(invoice.date),
  38. "date_due": Date.to_string(invoice.date_due),
  39. "date_invoice": Date.to_string(invoice.date_invoice),
  40. "partner": self._one_to_many_to_dict(invoice.partner_id),
  41. "journal": self._one_to_many_to_dict(invoice.journal_id),
  42. "account": self._one_to_many_to_dict(invoice.account_id),
  43. "subscription_request": self._one_to_many_to_dict(
  44. invoice.subscription_request
  45. ),
  46. "invoice_lines": [
  47. self._line_to_dict(line) for line in invoice.invoice_line_ids
  48. ],
  49. }
  50. return data
  51. def _line_to_dict(self, line):
  52. return {
  53. "name": line.name,
  54. "account": self._one_to_many_to_dict(line.account_id),
  55. "product": self._one_to_many_to_dict(
  56. line.product_id.product_tmpl_id
  57. ),
  58. "quantity": line.quantity,
  59. "price_unit": line.price_unit,
  60. }
  61. def _validator_get(self):
  62. return schemas.S_INVOICE_GET
  63. def _validator_return_get(self):
  64. return schemas.S_INVOICE_RETURN_GET