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.

81 lines
2.5 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. if invoice.subscription_request:
  34. sr_external_id = invoice.subscription_request.get_api_external_id()
  35. else:
  36. sr_external_id = None
  37. # todo return dictionaries for Many2one fields
  38. data = {
  39. "id": invoice.get_api_external_id(),
  40. "name": invoice.name,
  41. "state": invoice.state,
  42. "type": invoice.type,
  43. "date": Date.to_string(invoice.date),
  44. "date_due": Date.to_string(invoice.date_due),
  45. "date_invoice": Date.to_string(invoice.date_invoice),
  46. "partner": invoice.partner_id.get_api_external_id(),
  47. "journal": invoice.journal_id.get_api_external_id(),
  48. "account": invoice.account_id.get_api_external_id(),
  49. "subscription_request": sr_external_id,
  50. "invoice_lines": [
  51. self._line_to_dict(line) for line in invoice.invoice_line_ids
  52. ],
  53. }
  54. return data
  55. def _line_to_dict(self, line):
  56. return {
  57. "name": line.name,
  58. "account": line.account_id.get_api_external_id(),
  59. "product": line.product_id.product_tmpl_id.get_api_external_id(),
  60. "quantity": line.quantity,
  61. "price_unit": line.price_unit,
  62. }
  63. def _validator_get(self):
  64. return schemas.S_INVOICE_GET
  65. def _validator_return_get(self):
  66. return schemas.S_INVOICE_RETURN_GET