Skip to content

Commit

Permalink
[IMP] invader_payment_adyen_sepa - Multi company
Browse files Browse the repository at this point in the history
- Currently the acquirer set on the payment method is the same for every companies. Change this field to be company dependent. (because we can not duplicate the payment method as the code is unique)
- Add related unit test for this multi-company use-case
  • Loading branch information
acsonefho committed Dec 7, 2023
1 parent 621a3f3 commit 5c8d781
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 9 deletions.
1 change: 1 addition & 0 deletions invader_payment_adyen_sepa/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/shopinvader/odoo-shopinvader-payment",
"depends": [
"base_technical_user",
"invader_payment_adyen",
"account_banking_sepa_direct_debit",
"base_iban",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class AccountPaymentMethod(models.Model):
payment_acquirer_id = fields.Many2one(
comodel_name="payment.acquirer",
string="Acquirer",
company_dependent=True,
)
25 changes: 17 additions & 8 deletions invader_payment_adyen_sepa/models/account_payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,29 @@ def open2generated(self):
@api.model
def _cron_create_transaction(self):
""" """
payment_orders = self.search(
all_payment_orders = self.sudo().search(
[
("state", "=", "open"),
("payment_mode_id.payment_type", "=", "inbound"),
("payment_method_id.code", "=", "sepa_direct_debit"),
]
)
# Sepa is compute not stored so filter manually
# + filter only to adyen
for payment_order in payment_orders.filtered(
lambda p: p.sepa
and p.payment_method_id.payment_acquirer_id.provider == "adyen"
):
payment_order.open2generated()
companies = all_payment_orders.mapped("company_id")
for company in companies:
payment_orders = (
all_payment_orders.filtered(
lambda o, c=company: o.company_id == c
)
.with_company(company)
.sudo_tech()
)
# Sepa is compute not stored so filter manually
# + filter only to adyen
for payment_order in payment_orders.filtered(
lambda p: p.sepa
and p.payment_method_id.payment_acquirer_id.provider == "adyen"
):
payment_order.open2generated()

def _generate_transaction_adyen(self):
transaction_data = self._invader_prepare_payment_transaction_data(
Expand Down
125 changes: 124 additions & 1 deletion invader_payment_adyen_sepa/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ def setUpClass(cls):
"currency_id": cls.euro.id,
"country_id": cls.env.ref("base.fr").id,
}
company_fr_data2 = {
"name": "French company 2",
"currency_id": cls.euro.id,
"country_id": cls.env.ref("base.fr").id,
}
cls.company_fr = cls.env["res.company"].create(company_fr_data)
cls.company_fr2 = cls.env["res.company"].create(company_fr_data2)
cls.env.ref("l10n_generic_coa.configurable_chart_template")._load(
15.0, 15.0, cls.company_fr
)
cls.env.ref("l10n_generic_coa.configurable_chart_template")._load(
15.0, 15.0, cls.company_fr2
)
cls.env = cls.env(
context=dict(
cls.env.context,
Expand All @@ -42,7 +51,13 @@ def setUpClass(cls):
cls.AccountPaymentLineCreate = cls.env["account.payment.line.create"]
cls.belgium = cls.env.ref("base.be")
product = cls.env.ref("product.product_product_4").copy()
product2 = (
cls.env.ref("product.product_product_4")
.with_company(cls.company_fr2)
.copy()
)
product.write({"company_id": cls.company_fr.id})
product2.write({"company_id": cls.company_fr2.id})
cls.acquirer = cls.PaymentAcquirer.create(
{
"name": "Shopinvader Adyen - Unit test",
Expand Down Expand Up @@ -79,13 +94,30 @@ def setUpClass(cls):
"partner_id": cls.company_fr.partner_id.id,
}
)
cls.bank2 = cls.PartnerBank.create(
{
"acc_number": "BE68 5390 0754 7034",
"acc_type": "iban",
"partner_id": cls.company_fr2.partner_id.id,
"company_id": cls.company_fr2.id,
}
)
cls.bank_partner = cls.PartnerBank.create(
{
"acc_number": "FR7630006000011234567890189",
"acc_type": "iban",
"partner_id": cls.partner.id,
}
)
cls.bank_partner2 = cls.PartnerBank.with_company(
cls.company_fr2
).create(
{
"acc_number": "FR7630006000011234567890189",
"acc_type": "iban",
"partner_id": cls.partner.id,
}
)
cls.mandate = cls.Mandate.create(
{
"partner_bank_id": cls.bank_partner.id,
Expand All @@ -94,6 +126,14 @@ def setUpClass(cls):
}
)
cls.mandate.validate()
cls.mandate2 = cls.Mandate.with_company(cls.company_fr2).create(
{
"partner_bank_id": cls.bank_partner2.id,
"signature_date": "2015-01-01",
"company_id": cls.company_fr2.id,
}
)
cls.mandate2.validate()
cls.invoice_line_account = cls.AccountAccount.create(
{
"name": "Test account",
Expand All @@ -104,7 +144,19 @@ def setUpClass(cls):
"company_id": cls.company_fr.id,
}
)
payment_method_sepa = cls.PaymentMethod.create(
cls.invoice_line_account2 = cls.AccountAccount.create(
{
"name": "Test account 2",
"code": "TEST12",
"user_type_id": cls.env.ref(
"account.data_account_type_expenses"
).id,
"company_id": cls.company_fr2.id,
}
)
cls.payment_method_sepa = (
payment_method_sepa
) = cls.PaymentMethod.create(
{
"name": "SEPA IN",
"code": "sepa_direct_debit",
Expand All @@ -127,6 +179,19 @@ def setUpClass(cls):
"currency_id": cls.euro.id,
}
)
cls.journal2 = cls.AccountJournal.with_company(cls.company_fr2).create(
{
"name": "SEPA Journal",
"code": "sepa123",
"type": "bank",
"company_id": cls.company_fr2.id,
"bank_account_id": cls.bank2.id,
"outbound_payment_method_ids": [
(6, False, payment_method_sepa.ids)
],
"currency_id": cls.euro.id,
}
)
cls.AccountJournal.create(
{
"name": "General",
Expand All @@ -140,6 +205,19 @@ def setUpClass(cls):
"currency_id": cls.euro.id,
}
)
cls.AccountJournal.with_company(cls.company_fr2).create(
{
"name": "General",
"code": "general1236",
"type": "general",
"company_id": cls.company_fr2.id,
"bank_account_id": cls.bank2.id,
"outbound_payment_method_ids": [
(6, False, payment_method_sepa.ids)
],
"currency_id": cls.euro.id,
}
)
cls.payment_mode = cls.PaymentMode.create(
{
"name": "test_mode",
Expand All @@ -149,6 +227,16 @@ def setUpClass(cls):
"fixed_journal_id": cls.journal.id,
}
)
cls.payment_mode2 = cls.PaymentMode.create(
{
"name": "test_mode",
"active": True,
"payment_method_id": payment_method_sepa.id,
"bank_account_link": "fixed",
"fixed_journal_id": cls.journal2.id,
"company_id": cls.company_fr2.id,
}
)
cls.invoice = cls.AccountMove.create(
{
"partner_id": cls.partner.id,
Expand All @@ -158,6 +246,7 @@ def setUpClass(cls):
"invoice_date": fields.Date.today(),
"company_id": cls.company_fr.id,
"currency_id": cls.euro.id,
"mandate_id": cls.mandate.id,
"invoice_line_ids": [
(
0,
Expand All @@ -173,13 +262,47 @@ def setUpClass(cls):
],
}
)
cls.invoice2 = cls.AccountMove.with_company(cls.company_fr2).create(
{
"partner_id": cls.partner.id,
"move_type": "out_invoice",
"ref": "myref321-2",
"payment_mode_id": cls.payment_mode2.id,
"invoice_date": fields.Date.today(),
"company_id": cls.company_fr2.id,
"currency_id": cls.euro.id,
"mandate_id": cls.mandate2.id,
"invoice_line_ids": [
(
0,
False,
{
"product_id": product2.id,
"quantity": 1.0,
"price_unit": 100.0,
"name": "product that cost 100",
"account_id": cls.invoice_line_account2.id,
},
)
],
}
)
cls.payment_order = cls.AccountPaymentOrder.create(
{
"payment_mode_id": cls.payment_mode.id,
"journal_id": cls.journal.id,
"date_prefered": "due",
}
)
cls.payment_order2 = cls.AccountPaymentOrder.with_company(
cls.company_fr2
).create(
{
"payment_mode_id": cls.payment_mode2.id,
"journal_id": cls.journal2.id,
"date_prefered": "due",
}
)

def _wizard_fill_payment_lines(self, payment_order):
AccountPaymentLineCreate = self.AccountPaymentLineCreate.with_context(
Expand Down
85 changes: 85 additions & 0 deletions invader_payment_adyen_sepa/tests/test_account_payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,88 @@ def test_cron_create_transaction(self):
self.assertEqual(transaction.partner_id, payment_line.partner_id)
self.assertEqual(transaction.acquirer_id, self.acquirer)
self.assertEqual(transaction.reference, payment_line.communication)
self.assertEqual(
self.payment_order.payment_mode_id.company_id,
self.payment_order.company_id,
)

def test_cron_create_transaction_multi_company_empty(self):
# Disable the payment acquirer with current company.
# So we shouldn't have a transaction
self.payment_method_sepa.write(
{
"payment_acquirer_id": False,
}
)
self.invoice.action_post()
self.assertEqual("posted", self.invoice.state)
self.assertTrue(self.payment_order.sepa)
self._wizard_fill_payment_lines(self.payment_order)
# Depending on lines, sepa could be False now
self.assertTrue(self.payment_order.sepa)
self.assertGreater(len(self.payment_order.payment_line_ids), 0)
self.payment_order.draft2open()
self.AccountPaymentOrder._cron_create_transaction()
# Ensure no transaction and normal workflow still ok
self.assertFalse(
self.payment_order.payment_line_ids.transaction_ids,
)
self.assertEqual("open", self.payment_order.state)

def test_cron_create_transaction_multi_company(self):
# Disable the payment acquirer with current company.
self.payment_method_sepa.write(
{
"payment_acquirer_id": False,
}
)
# But enable it with another company
other_company = self.company_fr2
self.payment_order2 = self.payment_order2.with_company(other_company)
self.payment_method_sepa.with_company(other_company).write(
{
"payment_acquirer_id": self.acquirer.id,
}
)
other_invoice = self.invoice2.with_company(other_company)
self.invoice.action_post()
other_invoice.action_post()
self.assertEqual("posted", self.invoice.state)
self.assertEqual("posted", other_invoice.state)
self.assertTrue(self.payment_order.sepa)
self.assertTrue(self.payment_order2.sepa)
self._wizard_fill_payment_lines(self.payment_order)
self._wizard_fill_payment_lines(self.payment_order2)
# Depending on lines, sepa could be False now
self.assertTrue(self.payment_order.sepa)
self.assertTrue(self.payment_order2.sepa)
self.assertGreater(len(self.payment_order.payment_line_ids), 0)
self.payment_order.draft2open()
self.payment_order2.draft2open()
self.AccountPaymentOrder._cron_create_transaction()
# Ensure no transaction and normal workflow still ok
self.assertFalse(
self.payment_order.payment_line_ids.transaction_ids,
)
# The flow for this first payment_order stop here as no acquirer set on this company
self.assertEqual("open", self.payment_order.state)
# Payment order in second company
self.assertTrue(self.payment_order2.payment_line_ids)
self.assertEqual(
len(self.payment_order2.payment_line_ids.transaction_ids),
len(self.payment_order2.payment_line_ids),
)
self.assertEqual("uploaded", self.payment_order2.state)
for payment_line in self.payment_order2.payment_line_ids:
transaction = payment_line.transaction_ids
self.assertAlmostEqual(
transaction.amount, payment_line.amount_currency
)
self.assertEqual(transaction.currency_id, payment_line.currency_id)
self.assertEqual(transaction.partner_id, payment_line.partner_id)
self.assertEqual(transaction.acquirer_id, self.acquirer)
self.assertEqual(transaction.reference, payment_line.communication)
self.assertEqual(
self.payment_order2.payment_mode_id.company_id,
self.payment_order2.company_id,
)

0 comments on commit 5c8d781

Please sign in to comment.