diff --git a/account_payment_return_revoke_mandate/README.rst b/account_payment_return_revoke_mandate/README.rst new file mode 100644 index 00000000000..e2ebefb6e94 --- /dev/null +++ b/account_payment_return_revoke_mandate/README.rst @@ -0,0 +1,86 @@ +===================================== +Account Payment Return Revoke Mandate +===================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:25ba2eeb30287a53f7a54aa47cf20c00a33b607dc41e8a3a557cd3808b402056 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--payment-lightgray.png?logo=github + :target: https://github.com/OCA/account-payment/tree/18.0/account_payment_return_revoke_mandate + :alt: OCA/account-payment +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-payment-18-0/account-payment-18-0-account_payment_return_revoke_mandate + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/account-payment&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds an option on return codes in order to cancel mandates +associated. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Check 'Revoke mandates' on payment return reasons that should revoke + mandates. +2. When a payment return line is received with such a reason, + automatically cancel mandates linked to concerned invoices. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ACSONE SA/NV + +Contributors +------------ + +- Quentin Groulard +- Hughes Damry + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/account-payment `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_payment_return_revoke_mandate/__init__.py b/account_payment_return_revoke_mandate/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/account_payment_return_revoke_mandate/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_payment_return_revoke_mandate/__manifest__.py b/account_payment_return_revoke_mandate/__manifest__.py new file mode 100644 index 00000000000..749a1c8244d --- /dev/null +++ b/account_payment_return_revoke_mandate/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Account Payment Return Revoke Mandate", + "summary": """ + This addon adds an option on return codes + in order to cancel mandates associated.""", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-payment", + "depends": ["account_banking_mandate", "account_payment_return"], + "installable": True, +} diff --git a/account_payment_return_revoke_mandate/models/__init__.py b/account_payment_return_revoke_mandate/models/__init__.py new file mode 100644 index 00000000000..51cdc28b47c --- /dev/null +++ b/account_payment_return_revoke_mandate/models/__init__.py @@ -0,0 +1,2 @@ +from . import payment_return_reason +from . import account_move diff --git a/account_payment_return_revoke_mandate/models/account_move.py b/account_payment_return_revoke_mandate/models/account_move.py new file mode 100644 index 00000000000..fd019dbdb75 --- /dev/null +++ b/account_payment_return_revoke_mandate/models/account_move.py @@ -0,0 +1,38 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + +from odoo import _, models +from odoo.exceptions import UserError + +_logger = logging.getLogger(__name__) + + +class AccountMoveLine(models.Model): + _inherit = "account.move" + + def _payment_returned(self, return_line): + res = super()._payment_returned(return_line) + if return_line.reason_id.revoke_mandates: + self._revoke_mandate(return_line) + return res + + def _revoke_mandate(self, return_line): + for rec in self: + if rec.mandate_id: + try: + rec.mandate_id.cancel() + msg = _( + "Mandate revoked in payment return %s", + return_line.return_id.name, + ) + rec.mandate_id.message_post(body=msg) + except UserError: + # May happen if the mandate is not draft or valid + _logger.error( + "Trying to revoke mandate %s from payment return %s but it" + " has the state %s.", + rec.mandate_id.unique_mandate_reference, + return_line.return_id.name, + rec.mandate_id.state, + ) diff --git a/account_payment_return_revoke_mandate/models/payment_return_reason.py b/account_payment_return_revoke_mandate/models/payment_return_reason.py new file mode 100644 index 00000000000..61be519b8db --- /dev/null +++ b/account_payment_return_revoke_mandate/models/payment_return_reason.py @@ -0,0 +1,10 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class PaymentReturnReason(models.Model): + _inherit = "payment.return.reason" + + revoke_mandates = fields.Boolean(company_dependent=True) diff --git a/account_payment_return_revoke_mandate/pyproject.toml b/account_payment_return_revoke_mandate/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/account_payment_return_revoke_mandate/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/account_payment_return_revoke_mandate/readme/CONTRIBUTORS.md b/account_payment_return_revoke_mandate/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..b5ab24ff726 --- /dev/null +++ b/account_payment_return_revoke_mandate/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Quentin Groulard \ +- Hughes Damry \ diff --git a/account_payment_return_revoke_mandate/readme/DESCRIPTION.md b/account_payment_return_revoke_mandate/readme/DESCRIPTION.md new file mode 100644 index 00000000000..1c6b9cec4bc --- /dev/null +++ b/account_payment_return_revoke_mandate/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module adds an option on return codes in order to cancel mandates +associated. diff --git a/account_payment_return_revoke_mandate/readme/USAGE.md b/account_payment_return_revoke_mandate/readme/USAGE.md new file mode 100644 index 00000000000..bc41cff8c46 --- /dev/null +++ b/account_payment_return_revoke_mandate/readme/USAGE.md @@ -0,0 +1,4 @@ +1. Check 'Revoke mandates' on payment return reasons that should revoke + mandates. +2. When a payment return line is received with such a reason, + automatically cancel mandates linked to concerned invoices. diff --git a/account_payment_return_revoke_mandate/static/description/icon.png b/account_payment_return_revoke_mandate/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/account_payment_return_revoke_mandate/static/description/icon.png differ diff --git a/account_payment_return_revoke_mandate/static/description/index.html b/account_payment_return_revoke_mandate/static/description/index.html new file mode 100644 index 00000000000..f231908b436 --- /dev/null +++ b/account_payment_return_revoke_mandate/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +Account Payment Return Revoke Mandate + + + +
+

Account Payment Return Revoke Mandate

+ + +

Beta License: AGPL-3 OCA/account-payment Translate me on Weblate Try me on Runboat

+

This module adds an option on return codes in order to cancel mandates +associated.

+

Table of contents

+ +
+

Usage

+
    +
  1. Check ‘Revoke mandates’ on payment return reasons that should revoke +mandates.
  2. +
  3. When a payment return line is received with such a reason, +automatically cancel mandates linked to concerned invoices.
  4. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/account-payment project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/account_payment_return_revoke_mandate/tests/__init__.py b/account_payment_return_revoke_mandate/tests/__init__.py new file mode 100644 index 00000000000..164780b64cd --- /dev/null +++ b/account_payment_return_revoke_mandate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_payment_return_revoke_mandate diff --git a/account_payment_return_revoke_mandate/tests/test_payment_return_revoke_mandate.py b/account_payment_return_revoke_mandate/tests/test_payment_return_revoke_mandate.py new file mode 100644 index 00000000000..e9a4f49bf89 --- /dev/null +++ b/account_payment_return_revoke_mandate/tests/test_payment_return_revoke_mandate.py @@ -0,0 +1,173 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.fields import Command +from odoo.tests import Form +from odoo.tools import mute_logger + +from odoo.addons.base.tests.common import BaseCommon + + +class TestPaymentReturnRevokeMandate(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.company = cls.env.ref("base.main_company") + cls.partner = cls.env["res.partner"].create({"name": "test partner"}) + cls.bank = cls.env["res.bank"].create({"name": "test bank"}) + bank_account = cls.env["res.partner.bank"].create( + { + "acc_number": "0023032234211123", + "partner_id": cls.partner.id, + "bank_id": cls.bank.id, + "company_id": cls.company.id, + } + ) + cls.mandate = cls.env["account.banking.mandate"].create( + { + "partner_bank_id": bank_account.id, + "signature_date": "2015-01-01", + "company_id": cls.company.id, + } + ) + cls.mandate.validate() + + cls.mode_inbound = cls.env["account.payment.mode"].create( + { + "name": "Inbound Credit test Bank", + "company_id": cls.company.id, + "bank_account_link": "variable", + "payment_method_id": cls.env.ref( + "account.account_payment_method_manual_in" + ).id, + } + ) + cls.bank_journal = cls.env["account.journal"].search( + [ + ("type", "=", "bank"), + ("company_id", "=", cls.company.id), + ], + limit=1, + ) + cls.mode_inbound.variable_journal_ids = cls.bank_journal + cls.mode_inbound.payment_method_id.mandate_required = True + cls.mode_inbound.payment_order_ok = True + cls.partner.customer_payment_mode_id = cls.mode_inbound + + cls.invoice_account = cls.env["account.account"].search( + [ + ("account_type", "=", "asset_receivable"), + ("company_ids", "in", [cls.company.id]), + ], + limit=1, + ) + invoice_line_account = ( + cls.env["account.account"] + .search( + [ + ("account_type", "=", "expense"), + ("company_ids", "in", [cls.company.id]), + ], + limit=1, + ) + .id + ) + cls.product = cls.env["product.product"].create( + { + "name": "Test product", + "type": "service", + } + ) + invoice_vals = [ + ( + Command.create( + { + "product_id": cls.product.id, + "quantity": 1.0, + "account_id": invoice_line_account, + "price_unit": 200.00, + }, + ) + ) + ] + cls.invoice = cls.env["account.move"].create( + { + "partner_id": cls.partner.id, + "move_type": "out_invoice", + "company_id": cls.company.id, + "journal_id": cls.env["account.journal"] + .search( + [("type", "=", "sale"), ("company_id", "=", cls.company.id)], + limit=1, + ) + .id, + "invoice_line_ids": invoice_vals, + } + ) + cls.invoice.action_post() + cls.reason = cls.env["payment.return.reason"].create( + {"code": "RTEST", "name": "Reason Test"} + ) + # Create payment from invoice + cls.payment_register_model = cls.env["account.payment.register"] + payment_register = Form( + cls.payment_register_model.with_context( + active_model="account.move", active_ids=cls.invoice.ids + ) + ) + cls.payment = payment_register.save()._create_payments() + cls.payment_move = cls.payment.move_id + cls.payment_line = cls.payment_move.line_ids.filtered( + lambda x: x.account_id.account_type == "asset_receivable" + ) + + def test_payment_return_no_revoke_mandate(self): + self.assertEqual(self.mandate.state, "valid") + self.assertFalse(self.reason.revoke_mandates) + # Create payment return + payment_return = self.env["payment.return"].create( + { + "journal_id": self.bank_journal.id, + "line_ids": [ + Command.create( + { + "partner_id": self.partner.id, + "move_line_ids": [Command.set(self.payment_line.ids)], + "amount": self.payment_line.credit, + "expense_partner_id": self.partner.id, + "reason_id": self.reason.id, + }, + ) + ], + } + ) + payment_return.action_confirm() + self.assertEqual(payment_return.state, "done") + self.assertEqual(self.mandate.state, "valid") + + @mute_logger( + "odoo.addons.account_payment_return_revoke_mandate.models.account_move" + ) + def test_payment_return_revoke_mandate(self): + self.reason.revoke_mandates = True + self.assertEqual(self.mandate.state, "valid") + # Create payment return + payment_return = self.env["payment.return"].create( + { + "journal_id": self.bank_journal.id, + "line_ids": [ + Command.create( + { + "partner_id": self.partner.id, + "move_line_ids": [Command.set(self.payment_line.ids)], + "amount": self.payment_line.credit, + "expense_partner_id": self.partner.id, + "reason_id": self.reason.id, + }, + ) + ], + } + ) + payment_return.action_confirm() + self.assertEqual(payment_return.state, "done") + self.assertEqual(self.mandate.state, "cancel") diff --git a/setup/account_payment_return_revoke_mandate/odoo/addons/account_payment_return_revoke_mandate b/setup/account_payment_return_revoke_mandate/odoo/addons/account_payment_return_revoke_mandate new file mode 120000 index 00000000000..b7282377e6d --- /dev/null +++ b/setup/account_payment_return_revoke_mandate/odoo/addons/account_payment_return_revoke_mandate @@ -0,0 +1 @@ +../../../../account_payment_return_revoke_mandate \ No newline at end of file diff --git a/setup/account_payment_return_revoke_mandate/setup.py b/setup/account_payment_return_revoke_mandate/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/account_payment_return_revoke_mandate/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 00000000000..d445a5103fa --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +odoo-addon-account_payment_return @ git+https://github.com/OCA/account-payment.git@refs/pull/879/head#subdirectory=account_payment_return