Browse Source
[ADD] emcc: post payment for emc-api invoice sends the payment to platform
pull/115/head
[ADD] emcc: post payment for emc-api invoice sends the payment to platform
pull/115/head
robin.keunen
5 years ago
16 changed files with 422 additions and 102 deletions
-
10easy_my_coop_connector/demo/demo.xml
-
2easy_my_coop_connector/models/__init__.py
-
16easy_my_coop_connector/models/account_journal.py
-
48easy_my_coop_connector/models/account_payment.py
-
58easy_my_coop_connector/models/emc_adapters.py
-
13easy_my_coop_connector/models/emc_backend.py
-
18easy_my_coop_connector/models/emc_bindings.py
-
21easy_my_coop_connector/models/subscription_request.py
-
2easy_my_coop_connector/security/ir.model.access.csv
-
1easy_my_coop_connector/tests/__init__.py
-
92easy_my_coop_connector/tests/test_data.py
-
72easy_my_coop_connector/tests/test_payment.py
-
89easy_my_coop_connector/tests/test_subscription_request.py
-
12easy_my_coop_connector/views/actions.xml
-
56easy_my_coop_connector/views/emc_bindings.xml
-
14easy_my_coop_connector/views/menus.xml
@ -1,4 +1,6 @@ |
|||||
from . import emc_backend |
from . import emc_backend |
||||
from . import emc_bindings |
from . import emc_bindings |
||||
from . import account_invoice |
from . import account_invoice |
||||
|
from . import account_journal |
||||
|
from . import account_payment |
||||
from . import subscription_request |
from . import subscription_request |
@ -0,0 +1,16 @@ |
|||||
|
# 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 import fields, models |
||||
|
|
||||
|
|
||||
|
class AccountJournal(models.Model): |
||||
|
_inherit = "account.journal" |
||||
|
|
||||
|
binding_id = fields.One2many( |
||||
|
comodel_name="emc.binding.account.journal", |
||||
|
inverse_name="internal_id", |
||||
|
string="Binding ID", |
||||
|
required=False, |
||||
|
) |
@ -0,0 +1,48 @@ |
|||||
|
# 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 import _, api, fields, models |
||||
|
from odoo.exceptions import ValidationError |
||||
|
|
||||
|
from .emc_adapters import AccountPaymentAdapter |
||||
|
|
||||
|
|
||||
|
class AccountPayment(models.Model): |
||||
|
_inherit = "account.payment" |
||||
|
|
||||
|
binding_id = fields.One2many( |
||||
|
comodel_name="emc.binding.account.payment", |
||||
|
inverse_name="internal_id", |
||||
|
string="Binding ID", |
||||
|
required=False, |
||||
|
) |
||||
|
|
||||
|
@api.multi |
||||
|
def post(self): |
||||
|
res = super(AccountPayment, self).post() |
||||
|
for payment in self: |
||||
|
if any(payment.invoice_ids.mapped("release_capital_request")): |
||||
|
invoice_id = payment.invoice_ids |
||||
|
if len(invoice_id) > 1: |
||||
|
raise ValidationError( |
||||
|
_( |
||||
|
"This version of easy my coop connector " |
||||
|
"can't handle several invoice per" |
||||
|
"payment. Please contact your " |
||||
|
"system administrator" |
||||
|
) |
||||
|
) |
||||
|
|
||||
|
backend = self.env["emc.backend"].get_backend() |
||||
|
adapter = AccountPaymentAdapter(backend=backend) |
||||
|
external_id, external_record = adapter.create(payment) |
||||
|
self.env["emc.binding.account.payment"].create( |
||||
|
{ |
||||
|
"backend": backend.id, |
||||
|
"internal_id": payment.id, |
||||
|
"external_id": external_id, |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
return res |
@ -1 +1,2 @@ |
|||||
from . import test_subscription_request |
from . import test_subscription_request |
||||
|
from . import test_payment |
@ -0,0 +1,92 @@ |
|||||
|
# 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). |
||||
|
|
||||
|
|
||||
|
import json |
||||
|
|
||||
|
from odoo.fields import Date |
||||
|
|
||||
|
|
||||
|
def dict_to_dump(content): |
||||
|
return json.dumps(content).encode("utf-8") |
||||
|
|
||||
|
|
||||
|
NOT_FOUND_ERROR = {"name": "Not Found", "code": 404} |
||||
|
FORBIDDEN_ERROR = {"name": "Forbidden", "code": 403} |
||||
|
SERVER_ERROR = {"name": "Server Error", "code": 500} |
||||
|
NO_RESULT = {"count": 0, "rows": []} |
||||
|
|
||||
|
SR_SEARCH_RESULT = { |
||||
|
"count": 1, |
||||
|
"rows": [ |
||||
|
{ |
||||
|
"id": 1, |
||||
|
"date": "2020-05-14", |
||||
|
"email": "manuel@demo.net", |
||||
|
"address": { |
||||
|
"city": "Brussels", |
||||
|
"street": "schaerbeekstraat", |
||||
|
"zip_code": "1111", |
||||
|
"country": "BE", |
||||
|
}, |
||||
|
"lang": "en_US", |
||||
|
"ordered_parts": 3, |
||||
|
"name": "Manuel Dublues", |
||||
|
"share_product": {"name": "Part B - Worker", "id": 31}, |
||||
|
"state": "draft", |
||||
|
} |
||||
|
], |
||||
|
} |
||||
|
|
||||
|
SR_GET_RESULT = { |
||||
|
"id": 1, |
||||
|
"name": "Robin Des Bois", |
||||
|
"date": "2020-05-14", |
||||
|
"email": "manuel@demo.net", |
||||
|
"address": { |
||||
|
"city": "Brussels", |
||||
|
"street": "schaerbeekstraat", |
||||
|
"zip_code": "1111", |
||||
|
"country": "BE", |
||||
|
}, |
||||
|
"lang": "en_US", |
||||
|
"ordered_parts": 3, |
||||
|
"share_product": {"name": "Part B - Worker", "id": 31}, |
||||
|
"state": "draft", |
||||
|
} |
||||
|
|
||||
|
SR_VALIDATE_RESULT = { |
||||
|
"id": 9999, |
||||
|
"number": "SUBJ/2020/001", |
||||
|
"date_due": "2020-08-12", |
||||
|
"state": "open", |
||||
|
"date_invoice": "2020-08-12", |
||||
|
"date": "2020-08-12", |
||||
|
"type": "out_invoice", |
||||
|
"subscription_request": {"name": "Manuel Dublues", "id": 1}, |
||||
|
"partner": {"name": "Manuel Dublues", "id": 1}, |
||||
|
"invoice_lines": [ |
||||
|
{ |
||||
|
"price_unit": 25.0, |
||||
|
"quantity": 3.0, |
||||
|
"account": {"name": "Product Sales", "id": 2}, |
||||
|
"name": "Part B - Worker", |
||||
|
"product": {"name": "Part B - Worker", "id": 2}, |
||||
|
} |
||||
|
], |
||||
|
"journal": {"name": "Subscription Journal", "id": 1}, |
||||
|
"account": {"name": "Cooperators", "id": 1}, |
||||
|
} |
||||
|
|
||||
|
AP_CREATE_RESULT = { |
||||
|
"id": 9876, |
||||
|
"journal": {"id": 1, "name": "bank"}, |
||||
|
"invoice": { |
||||
|
"id": SR_VALIDATE_RESULT["id"], |
||||
|
"name": SR_VALIDATE_RESULT["number"], |
||||
|
}, |
||||
|
"payment_date": Date.to_string(Date.today()), |
||||
|
"amount": 75.0, |
||||
|
"communication": SR_VALIDATE_RESULT["number"], |
||||
|
} |
@ -0,0 +1,72 @@ |
|||||
|
# 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 unittest.mock import Mock, patch |
||||
|
|
||||
|
import requests |
||||
|
|
||||
|
from odoo.fields import Date |
||||
|
|
||||
|
from odoo.addons.easy_my_coop.tests.test_base import EMCBaseCase |
||||
|
|
||||
|
from .test_data import AP_CREATE_RESULT, SR_VALIDATE_RESULT, dict_to_dump |
||||
|
|
||||
|
|
||||
|
class EMCPaymentConnectorCase(EMCBaseCase): |
||||
|
def setUp(self): |
||||
|
super().setUp() |
||||
|
self.backend = self.browse_ref( |
||||
|
"easy_my_coop_connector.emc_backend_demo" |
||||
|
) |
||||
|
self.env["emc.binding.account.journal"].create( |
||||
|
{ |
||||
|
"backend_id": self.backend.id, |
||||
|
"internal_id": self.bank_journal.id, |
||||
|
"external_id": 1, |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
def test_post_payment_sends_and_binds_request(self): |
||||
|
srequest = self.browse_ref("easy_my_coop.subscription_request_1_demo") |
||||
|
with patch.object(requests, "post") as mock_get: |
||||
|
mock_get.return_value = mock_response = Mock() |
||||
|
mock_response.status_code = 200 |
||||
|
mock_response.content = dict_to_dump(SR_VALIDATE_RESULT) |
||||
|
|
||||
|
srequest.validate_subscription_request() |
||||
|
|
||||
|
capital_release_request = srequest.capital_release_request |
||||
|
|
||||
|
payment_method_manual_in = self.env.ref( |
||||
|
"account.account_payment_method_manual_in" |
||||
|
) |
||||
|
ctx = { |
||||
|
"active_model": "account.invoice", |
||||
|
"active_ids": [capital_release_request.id], |
||||
|
} |
||||
|
register_payments = ( |
||||
|
self.env["account.register.payments"] |
||||
|
.with_context(ctx) |
||||
|
.create( |
||||
|
{ |
||||
|
"payment_date": Date.today(), |
||||
|
"journal_id": self.bank_journal.id, |
||||
|
"payment_method_id": payment_method_manual_in.id, |
||||
|
} |
||||
|
) |
||||
|
) |
||||
|
|
||||
|
with patch.object(requests, "post") as mock_get: |
||||
|
mock_get.return_value = mock_response = Mock() |
||||
|
mock_response.status_code = 200 |
||||
|
mock_response.content = dict_to_dump(AP_CREATE_RESULT) |
||||
|
|
||||
|
register_payments.create_payments() |
||||
|
|
||||
|
self.assertEquals(capital_release_request.state, "paid") |
||||
|
|
||||
|
payment = capital_release_request.payment_ids |
||||
|
self.assertEquals(payment.state, "posted") |
||||
|
self.assertEquals(payment.binding_id.external_id, 9876) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue