robin.keunen
5 years ago
6 changed files with 375 additions and 5 deletions
-
1easy_my_coop_api/services/__init__.py
-
81easy_my_coop_api/services/account_invoice_service.py
-
35easy_my_coop_api/services/schemas.py
-
1easy_my_coop_api/tests/__init__.py
-
161easy_my_coop_api/tests/common.py
-
101easy_my_coop_api/tests/test_account_invoice.py
@ -1,2 +1,3 @@ |
|||
from . import ping_service |
|||
from . import subscription_request_service |
|||
from . import account_invoice_service |
@ -0,0 +1,81 @@ |
|||
# Copyright 2019 Coop IT Easy SCRL fs |
|||
# Robin Keunen <robin@coopiteasy.be> |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|||
# pylint: disable=consider-merging-classes-inherited |
|||
|
|||
import logging |
|||
|
|||
from werkzeug.exceptions import NotFound |
|||
|
|||
from odoo import _ |
|||
from odoo.fields import Date |
|||
|
|||
from odoo.addons.base_rest.http import wrapJsonException |
|||
from odoo.addons.component.core import Component |
|||
|
|||
from . import schemas |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class AccountInvoiceService(Component): |
|||
_inherit = "base.rest.service" |
|||
_name = "account.invoice.services" |
|||
_usage = "invoice" |
|||
_collection = "emc.services" |
|||
_description = """ |
|||
Account Invoice Services |
|||
""" |
|||
|
|||
def get(self, _id): |
|||
sr = self.env["account.invoice"].search( |
|||
[("_api_external_id", "=", _id)] |
|||
) |
|||
if sr: |
|||
return self._to_dict(sr) |
|||
else: |
|||
raise wrapJsonException( |
|||
NotFound(_("No invoice found for id %s") % _id) |
|||
) |
|||
|
|||
def _to_dict(self, invoice): |
|||
invoice.ensure_one() |
|||
|
|||
if invoice.subscription_request: |
|||
sr_external_id = invoice.subscription_request.get_api_external_id() |
|||
else: |
|||
sr_external_id = None |
|||
|
|||
# todo return dictionaries for Many2one fields |
|||
data = { |
|||
"id": invoice.get_api_external_id(), |
|||
"name": invoice.name, |
|||
"state": invoice.state, |
|||
"type": invoice.type, |
|||
"date": Date.to_string(invoice.date), |
|||
"date_due": Date.to_string(invoice.date_due), |
|||
"date_invoice": Date.to_string(invoice.date_invoice), |
|||
"partner": invoice.partner_id.get_api_external_id(), |
|||
"journal": invoice.journal_id.get_api_external_id(), |
|||
"account": invoice.account_id.get_api_external_id(), |
|||
"subscription_request": sr_external_id, |
|||
"invoice_lines": [ |
|||
self._line_to_dict(line) for line in invoice.invoice_line_ids |
|||
], |
|||
} |
|||
return data |
|||
|
|||
def _line_to_dict(self, line): |
|||
return { |
|||
"name": line.name, |
|||
"account": line.account_id.get_api_external_id(), |
|||
"product": line.product_id.product_tmpl_id.get_api_external_id(), |
|||
"quantity": line.quantity, |
|||
"price_unit": line.price_unit, |
|||
} |
|||
|
|||
def _validator_get(self): |
|||
return schemas.S_INVOICE_GET |
|||
|
|||
def _validator_return_get(self): |
|||
return schemas.S_INVOICE_RETURN_GET |
@ -0,0 +1,101 @@ |
|||
# Copyright 2020 Coop IT Easy SCRL fs |
|||
# Robin Keunen <robin@coopiteasy.be> |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from odoo.fields import Date |
|||
|
|||
from odoo.addons.base_rest.controllers.main import _PseudoCollection |
|||
from odoo.addons.component.core import WorkContext |
|||
|
|||
from .common import BaseEMCRestCase |
|||
|
|||
|
|||
class TestAccountInvoiceController(BaseEMCRestCase): |
|||
@classmethod |
|||
def setUpClass(cls, *args, **kwargs): |
|||
super().setUpClass(*args, **kwargs) |
|||
|
|||
def setUp(self): |
|||
res = super().setUp() |
|||
collection = _PseudoCollection("emc.services", self.env) |
|||
emc_services_env = WorkContext( |
|||
model_name="rest.service.registration", collection=collection |
|||
) |
|||
self.ai_service = emc_services_env.component(usage="invoice") |
|||
|
|||
self.share_type_A = self.browse_ref( |
|||
"easy_my_coop.product_template_share_type_1_demo" |
|||
) |
|||
self._capital_release_create() |
|||
|
|||
today = Date.to_string(Date.today()) |
|||
self.demo_invoice_dict = { |
|||
"id": 1, |
|||
"name": "Capital Release Example", |
|||
"partner": 1, |
|||
"account": 1, |
|||
"journal": 1, |
|||
"subscription_request": None, |
|||
"state": "open", |
|||
"date": today, |
|||
"date_invoice": today, |
|||
"date_due": today, |
|||
"type": "out_invoice", |
|||
"invoice_lines": [ |
|||
{ |
|||
"account": 2, |
|||
"name": "Share Type A", |
|||
"price_unit": 100.0, |
|||
"product": 1, |
|||
"quantity": 2.0, |
|||
} |
|||
], |
|||
} |
|||
return res |
|||
|
|||
def _capital_release_create(self): |
|||
self.coop_candidate = self.env["res.partner"].create( |
|||
{ |
|||
"name": "Catherine des Champs", |
|||
"company_id": self.company.id, |
|||
"property_account_receivable_id": self.receivable.id, |
|||
"property_account_payable_id": self.payable.id, |
|||
} |
|||
) |
|||
|
|||
capital_release_line = [ |
|||
( |
|||
0, |
|||
False, |
|||
{ |
|||
"name": "Share Type A", |
|||
"account_id": self.equity_account.id, |
|||
"quantity": 2.0, |
|||
"price_unit": 100.0, |
|||
"product_id": self.share_type_A.product_variant_id.id, |
|||
}, |
|||
) |
|||
] |
|||
|
|||
self.capital_release = self.env["account.invoice"].create( |
|||
{ |
|||
"name": "Capital Release Example", |
|||
"partner_id": self.coop_candidate.id, |
|||
"type": "out_invoice", |
|||
"invoice_line_ids": capital_release_line, |
|||
"account_id": self.cooperator_account.id, |
|||
"journal_id": self.subscription_journal.id, |
|||
} |
|||
) |
|||
self.capital_release.action_invoice_open() |
|||
|
|||
def test_service_get(self): |
|||
external_id = self.capital_release.get_api_external_id() |
|||
result = self.ai_service.get(external_id) |
|||
self.assertEquals(self.demo_invoice_dict, result) |
|||
|
|||
def test_route_get(self): |
|||
external_id = self.capital_release.get_api_external_id() |
|||
route = "/api/invoice/%s" % external_id |
|||
content = self.http_get_content(route) |
|||
self.assertEquals(self.demo_invoice_dict, content) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue