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.

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