Repository: OCA/donation (language: python)

Issue:
Dificulties to create mandate from donation - customer´s bank accounts not selectable
When you have to create a mandate from the donation, you get a mandate form. The first parameter is bank account, when you browse on it you don't get any record, you cannot select an existing bank account, it doesn´t recognize the customer´s bank accounts. 



Discussion / hints:

Fix patch:
diff --git a/donation_direct_debit/README.rst b/donation_direct_debit/README.rst
new file mode 100644
index 000000000..a7be5176d
--- /dev/null
+++ b/donation_direct_debit/README.rst
@@ -0,0 +1,101 @@
+=====================
+Donation Direct Debit
+=====================
+
+.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+   !! This file is generated by oca-gen-addon-readme !!
+   !! changes will be overwritten.                   !!
+   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+.. |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%2Fdonation-lightgray.png?logo=github
+    :target: https://github.com/OCA/donation/tree/14.0/donation_direct_debit
+    :alt: OCA/donation
+.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
+    :target: https://translation.odoo-community.org/projects/donation-14-0/donation-14-0-donation_direct_debit
+    :alt: Translate me on Weblate
+.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
+    :target: https://runbot.odoo-community.org/runbot/180/14.0
+    :alt: Try me on Runbot
+
+|badge1| |badge2| |badge3| |badge4| |badge5| 
+
+With this module, when you validate a donation that has a payment method linked to a SEPA direct debit payment mode:
+
+* if a draft direct debit order for SEPA Direct Debit already exists, a new payment line linked to the donation is added to it,
+
+* otherwise, a new SEPA direct debit order is created and a payment line linked to the donation is added to it.
+
+A message is added in the chatter of the donation with a link to the payment order.
+
+**Table of contents**
+
+.. contents::
+   :local:
+
+Configuration
+=============
+
+In the menu *Invoicing > Configuration > Management > Payment Modes*, make sure that you have a payment mode for SEPA Direct Debit that has the donation option active.
+
+Usage
+=====
+
+On the donor (i.e. partner) form, create a bank account (IBAN) and create a related mandate. Validate the mandate.
+
+Create a new donation for that donor. Select the SEPA direct debit payment mode: a new required field *Mandate* will appear: select the mandate of the donor. Create the donation line(s).
+
+Validate the donation. In the chatter of the donation, you will see a message about the debit order that has been created or updated with a link to it. You can then click on the link and finalize the debit order.
+
+Bug Tracker
+===========
+
+Bugs are tracked on `GitHub Issues <https://github.com/OCA/donation/issues>`_.
+In case of trouble, please check there if your issue has already been reported.
+If you spotted it first, help us smashing it by providing a detailed and welcomed
+`feedback <https://github.com/OCA/donation/issues/new?body=module:%20donation_direct_debit%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
+
+Do not contact contributors directly about support or help with technical issues.
+
+Credits
+=======
+
+Authors
+~~~~~~~
+
+* Akretion
+
+Contributors
+~~~~~~~~~~~~
+
+* Alexis de Lattre <alexis.delattre@akretion.com>
+
+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.
+
+.. |maintainer-alexis-via| image:: https://github.com/alexis-via.png?size=40px
+    :target: https://github.com/alexis-via
+    :alt: alexis-via
+
+Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
+
+|maintainer-alexis-via| 
+
+This module is part of the `OCA/donation <https://github.com/OCA/donation/tree/14.0/donation_direct_debit>`_ project on GitHub.
+
+You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
diff --git a/donation_direct_debit/__init__.py b/donation_direct_debit/__init__.py
new file mode 100644
index 000000000..0650744f6
--- /dev/null
+++ b/donation_direct_debit/__init__.py
@@ -0,0 +1,1 @@
+from . import models
diff --git a/donation_direct_debit/__manifest__.py b/donation_direct_debit/__manifest__.py
new file mode 100644
index 000000000..735a0f682
--- /dev/null
+++ b/donation_direct_debit/__manifest__.py
@@ -0,0 +1,21 @@
+# Copyright 2015-2021 Akretion France (http://www.akretion.com/)
+# @author: Alexis de Lattre <alexis.delattre@akretion.com>
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+
+
+{
+    "name": "Donation Direct Debit",
+    "version": "16.0.1.0.0",
+    "category": "Accounting",
+    "license": "AGPL-3",
+    "summary": "Auto-generate direct debit order on donation validation",
+    "author": "Akretion, Odoo Community Association (OCA)",
+    "maintainers": ["alexis-via"],
+    "website": "https://github.com/OCA/donation",
+    "depends": ["account_banking_sepa_direct_debit", "donation"],
+    "data": [
+        "views/donation.xml",
+    ],
+    "demo": ["demo/donation_demo.xml"],
+    "installable": True,
+}
diff --git a/donation_direct_debit/demo/donation_demo.xml b/donation_direct_debit/demo/donation_demo.xml
new file mode 100644
index 000000000..415ee4bf0
--- /dev/null
+++ b/donation_direct_debit/demo/donation_demo.xml
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!--
+  © 2015-2016 Akretion France (Alexis de Lattre <alexis.delattre@akretion.com>)
+  License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+-->
+<odoo noupdate="1">
+
+
+<record id="donor2_iban" model="res.partner.bank">
+    <field name="acc_number">FR93 1277 1277 1277 1277 1277 127</field>
+    <field name="partner_id" ref="donation_base.donor2" />
+    <field name="bank_id" ref="account_payment_mode.bank_la_banque_postale" />
+</record>
+
+<record id="donor2_mandate" model="account.banking.mandate">
+    <field name="partner_bank_id" ref="donor2_iban" />
+    <field name="type">recurrent</field>
+    <field name="recurrent_sequence_type">first</field>
+    <field name="signature_date" eval="time.strftime('%Y-01-01')" />
+    <field name="state">valid</field>
+</record>
+
+<record id="donor4" model="res.partner">
+    <field name="name">Bella Andalouse</field>
+    <field name="donor_rank" eval="1" />
+    <field name="street">42 avenue de la République</field>
+    <field name="zip">50550</field>
+    <field name="city">Saint-Vaast la Hougue</field>
+    <field name="country_id" ref="base.fr" />
+    <field name="email">bella.andalouse@yahoo.example.com</field>
+    <field name="tax_receipt_option">annual</field>
+</record>
+
+<record id="donor4_iban" model="res.partner.bank">
+    <field name="acc_number">FR50 4266 4266 4266 4266 4266 426</field>
+    <field name="partner_id" ref="donor4" />
+    <field name="bank_id" ref="account_payment_mode.bank_la_banque_postale" />
+</record>
+
+<record id="donor4_mandate" model="account.banking.mandate">
+    <field name="partner_bank_id" ref="donor4_iban" />
+    <field name="type">recurrent</field>
+    <field name="recurrent_sequence_type">first</field>
+    <field name="signature_date" eval="time.strftime('%Y-%m-01')" />
+    <field name="state">valid</field>
+</record>
+
+<record id="donation6" model="donation.donation">
+    <field name="currency_id" ref="base.EUR" />
+    <field name="check_total">150</field>
+    <field name="partner_id" ref="donor4" />
+    <field name="donation_date" eval="time.strftime('%Y-%m-01')" />
+    <field name="payment_ref">Don prelev SEPA</field>
+    <field name="campaign_id" ref="donation.prospecting_origin" />
+    <field name="mandate_id" ref="donor4_mandate" />
+    <field
+            name="payment_mode_id"
+            ref="account_banking_sepa_direct_debit.payment_mode_inbound_sepa_dd1"
+        />
+    <field name="tax_receipt_option">annual</field>
+</record>
+
+<record id="donation6_l1" model="donation.line">
+    <field name="donation_id" ref="donation6" />
+    <field name="product_id" ref="donation_base.product_product_donation" />
+    <field name="quantity">1</field>
+    <field name="unit_price">150</field>
+</record>
+
+
+</odoo>
diff --git a/donation_direct_debit/i18n/de.po b/donation_direct_debit/i18n/de.po
new file mode 100644
index 000000000..fcd024184
--- /dev/null
+++ b/donation_direct_debit/i18n/de.po
@@ -0,0 +1,48 @@
+# Translation of Odoo Server.
+# This file contains the translation of the following modules:
+#	* donation_direct_debit
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: Odoo Server 10.0\n"
+"Report-Msgid-Bugs-To: \n"
+"Last-Translator: Automatically generated\n"
+"Language-Team: none\n"
+"Language: de\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: \n"
+"Plural-Forms: nplurals=2; plural=n != 1;\n"
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:75
+#, python-format
+msgid "A new draft direct debit order %s has been automatically created"
+msgstr ""
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:84
+#, python-format
+msgid "A new payment line has been automatically added to the existing draft direct debit order %s"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model,name:donation_direct_debit.model_donation_donation
+msgid "Donation"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation_mandate_id
+msgid "Mandate"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation_payment_mode_id
+msgid "Payment Mode"
+msgstr ""
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:101
+#, python-format
+msgid "You cannot cancel a donation which is linked to a payment line in a direct debit order. Remove it from the following direct debit order: %s."
+msgstr ""
diff --git a/donation_direct_debit/i18n/donation_direct_debit.pot b/donation_direct_debit/i18n/donation_direct_debit.pot
new file mode 100644
index 000000000..6d5ba9526
--- /dev/null
+++ b/donation_direct_debit/i18n/donation_direct_debit.pot
@@ -0,0 +1,76 @@
+# Translation of Odoo Server.
+# This file contains the translation of the following modules:
+# 	* donation_direct_debit
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: Odoo Server 14.0\n"
+"Report-Msgid-Bugs-To: \n"
+"Last-Translator: \n"
+"Language-Team: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: \n"
+"Plural-Forms: \n"
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/models/donation.py:0
+#, python-format
+msgid ""
+"A new draft direct debit order <a href=# data-oe-model=account.payment.order"
+" data-oe-id=%d>%s</a> has been automatically created"
+msgstr ""
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/models/donation.py:0
+#, python-format
+msgid ""
+"A new payment line has been automatically added to the existing draft direct"
+" debit order <a href=# data-oe-model=account.payment.order data-oe-"
+"id=%d>%s</a>."
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,help:donation_direct_debit.field_donation_donation__mandate_required
+msgid ""
+"Activate this option if this payment method requires your customer to sign a"
+" direct debit mandate with your company."
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation__display_name
+msgid "Display Name"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model,name:donation_direct_debit.model_donation_donation
+msgid "Donation"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation__id
+msgid "ID"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation____last_update
+msgid "Last Modified on"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation__mandate_id
+msgid "Mandate"
+msgstr ""
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation__mandate_required
+msgid "Mandate Required"
+msgstr ""
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/models/donation.py:0
+#, python-format
+msgid ""
+"You cannot cancel a donation which is linked to a payment line in a direct "
+"debit order. Remove it from the following direct debit order: %s."
+msgstr ""
diff --git a/donation_direct_debit/i18n/fr.po b/donation_direct_debit/i18n/fr.po
new file mode 100644
index 000000000..c68386a26
--- /dev/null
+++ b/donation_direct_debit/i18n/fr.po
@@ -0,0 +1,64 @@
+# Translation of Odoo Server.
+# This file contains the translation of the following modules:
+# 	* donation_direct_debit
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: Odoo Server 8.0\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2015-05-29 23:01+0000\n"
+"PO-Revision-Date: 2015-05-29 23:01+0000\n"
+"Last-Translator: <>\n"
+"Language-Team: \n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: \n"
+"Plural-Forms: \n"
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:75
+#, python-format
+msgid "A new draft direct debit order %s has been automatically created"
+msgstr "Un nouvel ordre de prélèvement brouillon %s a été créé automatiquement"
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:84
+#, python-format
+msgid ""
+"A new payment line has been automatically added to the existing draft direct "
+"debit order %s"
+msgstr ""
+"Une nouvelle ligne de paiement a été automatiquement ajoutée à l'ordre de "
+"prélèvement brouillon existant %s"
+
+#. module: donation_direct_debit
+#: model:ir.model,name:donation_direct_debit.model_donation_donation
+msgid "Donation"
+msgstr "Don"
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation_mandate_id
+msgid "Mandate"
+msgstr "Mandat"
+
+#. module: donation_direct_debit
+#: model:ir.model.fields,field_description:donation_direct_debit.field_donation_donation_payment_mode_id
+msgid "Payment Mode"
+msgstr ""
+
+#. module: donation_direct_debit
+#: code:addons/donation_direct_debit/donation.py:101
+#, python-format
+msgid ""
+"You cannot cancel a donation which is linked to a payment line in a direct "
+"debit order. Remove it from the following direct debit order: %s."
+msgstr ""
+"Vous ne pouvez pas annuler un don lié à une ligne de paiement d'un ordre de "
+"prélèvement. Vous devez la supprimer de l'ordre de prélèvement %s."
+
+#~ msgid "No valid mandate found for donor %s"
+#~ msgstr "Aucun mandat valide trouvé pour le donateur %s"
+
+#~ msgid "There are 2 payment modes with the same bank journal!"
+#~ msgstr "Il y a 2 modes de paiements liés au même journal de banque !"
diff --git a/donation_direct_debit/models/__init__.py b/donation_direct_debit/models/__init__.py
new file mode 100644
index 000000000..b1847a5ab
--- /dev/null
+++ b/donation_direct_debit/models/__init__.py
@@ -0,0 +1,1 @@
+from . import donation
diff --git a/donation_direct_debit/models/donation.py b/donation_direct_debit/models/donation.py
new file mode 100644
index 000000000..df7ff0e65
--- /dev/null
+++ b/donation_direct_debit/models/donation.py
@@ -0,0 +1,153 @@
+# Copyright 2015-2021 Akretion France (http://www.akretion.com/)
+# @author: Alexis de Lattre <alexis.delattre@akretion.com>
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+
+
+class DonationDonation(models.Model):
+    _inherit = "donation.donation"
+
+    mandate_id = fields.Many2one(
+        "account.banking.mandate",
+        string="Mandate",
+        states={"done": [("readonly", True)]},
+        tracking=True,
+        check_company=True,
+        ondelete="restrict",
+        domain="[('state', '=', 'valid'), "
+        "('partner_id', '=', commercial_partner_id), "
+        "('company_id', '=', company_id)]",
+    )
+    mandate_required = fields.Boolean(
+        related="payment_mode_id.payment_method_id.mandate_required",
+    )
+
+    @api.onchange("payment_mode_id")
+    def donation_partner_direct_debit_change(self):
+        if (
+            self.partner_id
+            and self.payment_mode_id
+            and self.payment_mode_id.payment_method_id.mandate_required
+            and not self.mandate_id
+        ):
+            mandate = self.env["account.banking.mandate"].search(
+                [
+                    ("state", "=", "valid"),
+                    ("partner_id", "=", self.commercial_partner_id.id),
+                    ("company_id", "=", self.company_id.id),
+                ],
+                limit=1,
+            )
+            if mandate:
+                self.mandate_id = mandate
+
+    def _prepare_donation_move(self):
+        vals = super()._prepare_donation_move()
+        vals.update(
+            {
+                "mandate_id": self.mandate_id.id or False,
+                "payment_mode_id": self.payment_mode_id.id,
+            }
+        )
+        return vals
+
+    def _prepare_payment_order(self):
+        self.ensure_one()
+        vals = {"payment_mode_id": self.payment_mode_id.id}
+        return vals
+
+    def validate(self):
+        """Create Direct debit payment order on donation validation or update
+        an existing draft Direct Debit pay order"""
+        res = super().validate()
+        apoo = self.env["account.payment.order"].sudo()
+        for donation in self:
+            if (
+                donation.payment_mode_id
+                and donation.payment_mode_id.payment_type == "inbound"
+                and donation.payment_mode_id.payment_order_ok
+                and donation.move_id
+            ):
+                if donation.mandate_required and not donation.mandate_id:
+                    raise UserError(
+                        _("Mandate is missing on donation '%s'.")
+                        % donation.display_name
+                    )
+                payorder = apoo.search(
+                    [
+                        ("state", "=", "draft"),
+                        ("company_id", "=", donation.company_id.id),
+                        ("payment_mode_id", "=", donation.payment_mode_id.id),
+                    ],
+                    limit=1,
+                )
+                msg = False
+                if not payorder:
+                    payorder_vals = donation._prepare_payment_order()
+                    payorder = apoo.create(payorder_vals)
+                    payorder.message_post(
+                        body=_(
+                            "Payment order created automatically upon validation of "
+                            "donation <a href=# data-oe-model=donation.donation "
+                            "data-oe-id=%(donation_id)d>%(donation)s</a>.",
+                            donation_id=donation.id,
+                            donation=donation.display_name,
+                        )
+                    )
+                    msg = _(
+                        "A new draft direct debit order "
+                        "<a href=# data-oe-model=account.payment.order "
+                        "data-oe-id=%(payorder_id)d>%(payorder)s</a> "
+                        "has been automatically created",
+                        payorder_id=payorder.id,
+                        payorder=payorder.display_name,
+                    )
+                # add payment line
+                payment_account_id = donation._prepare_counterpart_move_line(
+                    1, 1, donation.move_id.journal_id
+                )["account_id"]
+                for mline in donation.move_id.line_ids:
+                    if mline.account_id.id == payment_account_id:
+                        mline.sudo().create_payment_line_from_move_line(payorder)
+                        break
+                if not msg:
+                    msg = _(
+                        "A new payment line has been automatically added "
+                        "to the existing draft direct debit order "
+                        "<a href=# data-oe-model=account.payment.order "
+                        "data-oe-id=%(payorder_id)d>%(payorder)s</a>.",
+                        payorder_id=payorder.id,
+                        payorder=payorder.display_name,
+                    )
+                donation.message_post(body=msg)
+        return res
+
+    def done2cancel(self):
+        for donation in self:
+            if donation.move_id:
+                donation_mv_line_ids = [
+                    line.id
+                    for line in donation.move_id.line_ids
+                    if line.account_id.reconcile
+                ]
+                if donation_mv_line_ids:
+                    plines = self.env["account.payment.line"].search(
+                        [
+                            ("move_line_id", "in", donation_mv_line_ids),
+                            ("company_id", "=", donation.company_id.id),
+                            ("state", "in", ("draft", "open")),
+                        ]
+                    )
+                    if plines:
+                        raise UserError(
+                            _(
+                                "You cannot cancel a donation "
+                                "which is linked to a payment line in a "
+                                "direct debit order. Remove it from the "
+                                "following direct debit order: %s."
+                            )
+                            % plines[0].order_id.display_name
+                        )
+        return super().done2cancel()
diff --git a/donation_direct_debit/readme/CONFIGURE.rst b/donation_direct_debit/readme/CONFIGURE.rst
new file mode 100644
index 000000000..d07c7114d
--- /dev/null
+++ b/donation_direct_debit/readme/CONFIGURE.rst
@@ -0,0 +1,1 @@
+In the menu *Invoicing > Configuration > Management > Payment Modes*, make sure that you have a payment mode for SEPA Direct Debit that has the donation option active.
diff --git a/donation_direct_debit/readme/CONTRIBUTORS.rst b/donation_direct_debit/readme/CONTRIBUTORS.rst
new file mode 100644
index 000000000..ff65d68ce
--- /dev/null
+++ b/donation_direct_debit/readme/CONTRIBUTORS.rst
@@ -0,0 +1,1 @@
+* Alexis de Lattre <alexis.delattre@akretion.com>
diff --git a/donation_direct_debit/readme/DESCRIPTION.rst b/donation_direct_debit/readme/DESCRIPTION.rst
new file mode 100644
index 000000000..315e6e2bd
--- /dev/null
+++ b/donation_direct_debit/readme/DESCRIPTION.rst
@@ -0,0 +1,7 @@
+With this module, when you validate a donation that has a payment method linked to a SEPA direct debit payment mode:
+
+* if a draft direct debit order for SEPA direct debit already exists, a new payment line linked to the donation is added to it,
+
+* otherwise, a new SEPA direct debit order is created and a payment line linked to the donation is added to it.
+
+A message is added in the chatter of the donation with a link to the payment order.
diff --git a/donation_direct_debit/readme/USAGE.rst b/donation_direct_debit/readme/USAGE.rst
new file mode 100644
index 000000000..3a3cdbdde
--- /dev/null
+++ b/donation_direct_debit/readme/USAGE.rst
@@ -0,0 +1,5 @@
+On the donor (i.e. partner) form, create a bank account (IBAN) and create a related mandate. Validate the mandate.
+
+Create a new donation for that donor. Select the SEPA direct debit payment mode: a new required field *Mandate* will appear: select the mandate of the donor. Create the donation line(s).
+
+Validate the donation. In the chatter of the donation, you will see a message about the debit order that has been created or updated with a link to it. You can then click on the link and finalize the debit order.
diff --git a/donation_direct_debit/static/description/icon.png b/donation_direct_debit/static/description/icon.png
new file mode 100644
index 000000000..e519e6f54
Binary files /dev/null and b/donation_direct_debit/static/description/icon.png differ
diff --git a/donation_direct_debit/static/description/index.html b/donation_direct_debit/static/description/index.html
new file mode 100644
index 000000000..bc9c05ce5
--- /dev/null
+++ b/donation_direct_debit/static/description/index.html
@@ -0,0 +1,438 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
+<title>Donation Direct Debit</title>
+<style type="text/css">
+
+/*
+:Author: David Goodger (goodger@python.org)
+:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
+:Copyright: This stylesheet has been placed in the public domain.
+
+Default cascading style sheet for the HTML output of Docutils.
+
+See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
+customize this style sheet.
+*/
+
+/* used to remove borders from tables and images */
+.borderless, table.borderless td, table.borderless th {
+  border: 0 }
+
+table.borderless td, table.borderless th {
+  /* Override padding for "table.docutils td" with "! important".
+     The right padding separates the table cells. */
+  padding: 0 0.5em 0 0 ! important }
+
+.first {
+  /* Override more specific margin styles with "! important". */
+  margin-top: 0 ! important }
+
+.last, .with-subtitle {
+  margin-bottom: 0 ! important }
+
+.hidden {
+  display: none }
+
+.subscript {
+  vertical-align: sub;
+  font-size: smaller }
+
+.superscript {
+  vertical-align: super;
+  font-size: smaller }
+
+a.toc-backref {
+  text-decoration: none ;
+  color: black }
+
+blockquote.epigraph {
+  margin: 2em 5em ; }
+
+dl.docutils dd {
+  margin-bottom: 0.5em }
+
+object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
+  overflow: hidden;
+}
+
+/* Uncomment (and remove this text!) to get bold-faced definition list terms
+dl.docutils dt {
+  font-weight: bold }
+*/
+
+div.abstract {
+  margin: 2em 5em }
+
+div.abstract p.topic-title {
+  font-weight: bold ;
+  text-align: center }
+
+div.admonition, div.attention, div.caution, div.danger, div.error,
+div.hint, div.important, div.note, div.tip, div.warning {
+  margin: 2em ;
+  border: medium outset ;
+  padding: 1em }
+
+div.admonition p.admonition-title, div.hint p.admonition-title,
+div.important p.admonition-title, div.note p.admonition-title,
+div.tip p.admonition-title {
+  font-weight: bold ;
+  font-family: sans-serif }
+
+div.attention p.admonition-title, div.caution p.admonition-title,
+div.danger p.admonition-title, div.error p.admonition-title,
+div.warning p.admonition-title, .code .error {
+  color: red ;
+  font-weight: bold ;
+  font-family: sans-serif }
+
+/* Uncomment (and remove this text!) to get reduced vertical space in
+   compound paragraphs.
+div.compound .compound-first, div.compound .compound-middle {
+  margin-bottom: 0.5em }
+
+div.compound .compound-last, div.compound .compound-middle {
+  margin-top: 0.5em }
+*/
+
+div.dedication {
+  margin: 2em 5em ;
+  text-align: center ;
+  font-style: italic }
+
+div.dedication p.topic-title {
+  font-weight: bold ;
+  font-style: normal }
+
+div.figure {
+  margin-left: 2em ;
+  margin-right: 2em }
+
+div.footer, div.header {
+  clear: both;
+  font-size: smaller }
+
+div.line-block {
+  display: block ;
+  margin-top: 1em ;
+  margin-bottom: 1em }
+
+div.line-block div.line-block {
+  margin-top: 0 ;
+  margin-bottom: 0 ;
+  margin-left: 1.5em }
+
+div.sidebar {
+  margin: 0 0 0.5em 1em ;
+  border: medium outset ;
+  padding: 1em ;
+  background-color: #ffffee ;
+  width: 40% ;
+  float: right ;
+  clear: right }
+
+div.sidebar p.rubric {
+  font-family: sans-serif ;
+  font-size: medium }
+
+div.system-messages {
+  margin: 5em }
+
+div.system-messages h1 {
+  color: red }
+
+div.system-message {
+  border: medium outset ;
+  padding: 1em }
+
+div.system-message p.system-message-title {
+  color: red ;
+  font-weight: bold }
+
+div.topic {
+  margin: 2em }
+
+h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
+h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
+  margin-top: 0.4em }
+
+h1.title {
+  text-align: center }
+
+h2.subtitle {
+  text-align: center }
+
+hr.docutils {
+  width: 75% }
+
+img.align-left, .figure.align-left, object.align-left, table.align-left {
+  clear: left ;
+  float: left ;
+  margin-right: 1em }
+
+img.align-right, .figure.align-right, object.align-right, table.align-right {
+  clear: right ;
+  float: right ;
+  margin-left: 1em }
+
+img.align-center, .figure.align-center, object.align-center {
+  display: block;
+  margin-left: auto;
+  margin-right: auto;
+}
+
+table.align-center {
+  margin-left: auto;
+  margin-right: auto;
+}
+
+.align-left {
+  text-align: left }
+
+.align-center {
+  clear: both ;
+  text-align: center }
+
+.align-right {
+  text-align: right }
+
+/* reset inner alignment in figures */
+div.align-right {
+  text-align: inherit }
+
+/* div.align-center * { */
+/*   text-align: left } */
+
+.align-top    {
+  vertical-align: top }
+
+.align-middle {
+  vertical-align: middle }
+
+.align-bottom {
+  vertical-align: bottom }
+
+ol.simple, ul.simple {
+  margin-bottom: 1em }
+
+ol.arabic {
+  list-style: decimal }
+
+ol.loweralpha {
+  list-style: lower-alpha }
+
+ol.upperalpha {
+  list-style: upper-alpha }
+
+ol.lowerroman {
+  list-style: lower-roman }
+
+ol.upperroman {
+  list-style: upper-roman }
+
+p.attribution {
+  text-align: right ;
+  margin-left: 50% }
+
+p.caption {
+  font-style: italic }
+
+p.credits {
+  font-style: italic ;
+  font-size: smaller }
+
+p.label {
+  white-space: nowrap }
+
+p.rubric {
+  font-weight: bold ;
+  font-size: larger ;
+  color: maroon ;
+  text-align: center }
+
+p.sidebar-title {
+  font-family: sans-serif ;
+  font-weight: bold ;
+  font-size: larger }
+
+p.sidebar-subtitle {
+  font-family: sans-serif ;
+  font-weight: bold }
+
+p.topic-title {
+  font-weight: bold }
+
+pre.address {
+  margin-bottom: 0 ;
+  margin-top: 0 ;
+  font: inherit }
+
+pre.literal-block, pre.doctest-block, pre.math, pre.code {
+  margin-left: 2em ;
+  margin-right: 2em }
+
+pre.code .ln { color: grey; } /* line numbers */
+pre.code, code { background-color: #eeeeee }
+pre.code .comment, code .comment { color: #5C6576 }
+pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
+pre.code .literal.string, code .literal.string { color: #0C5404 }
+pre.code .name.builtin, code .name.builtin { color: #352B84 }
+pre.code .deleted, code .deleted { background-color: #DEB0A1}
+pre.code .inserted, code .inserted { background-color: #A3D289}
+
+span.classifier {
+  font-family: sans-serif ;
+  font-style: oblique }
+
+span.classifier-delimiter {
+  font-family: sans-serif ;
+  font-weight: bold }
+
+span.interpreted {
+  font-family: sans-serif }
+
+span.option {
+  white-space: nowrap }
+
+span.pre {
+  white-space: pre }
+
+span.problematic {
+  color: red }
+
+span.section-subtitle {
+  /* font-size relative to parent (h1..h6 element) */
+  font-size: 80% }
+
+table.citation {
+  border-left: solid 1px gray;
+  margin-left: 1px }
+
+table.docinfo {
+  margin: 2em 4em }
+
+table.docutils {
+  margin-top: 0.5em ;
+  margin-bottom: 0.5em }
+
+table.footnote {
+  border-left: solid 1px black;
+  margin-left: 1px }
+
+table.docutils td, table.docutils th,
+table.docinfo td, table.docinfo th {
+  padding-left: 0.5em ;
+  padding-right: 0.5em ;
+  vertical-align: top }
+
+table.docutils th.field-name, table.docinfo th.docinfo-name {
+  font-weight: bold ;
+  text-align: left ;
+  white-space: nowrap ;
+  padding-left: 0 }
+
+/* "booktabs" style (no vertical lines) */
+table.docutils.booktabs {
+  border: 0px;
+  border-top: 2px solid;
+  border-bottom: 2px solid;
+  border-collapse: collapse;
+}
+table.docutils.booktabs * {
+  border: 0px;
+}
+table.docutils.booktabs th {
+  border-bottom: thin solid;
+  text-align: left;
+}
+
+h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
+h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
+  font-size: 100% }
+
+ul.auto-toc {
+  list-style-type: none }
+
+</style>
+</head>
+<body>
+<div class="document" id="donation-direct-debit">
+<h1 class="title">Donation Direct Debit</h1>
+
+<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+!! This file is generated by oca-gen-addon-readme !!
+!! changes will be overwritten.                   !!
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
+<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/donation/tree/14.0/donation_direct_debit"><img alt="OCA/donation" src="https://img.shields.io/badge/github-OCA%2Fdonation-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/donation-14-0/donation-14-0-donation_direct_debit"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/180/14.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
+<p>With this module, when you validate a donation that has a payment method linked to a SEPA direct debit payment mode:</p>
+<ul class="simple">
+<li>if a draft direct debit order for SEPA Direct Debit already exists, a new payment line linked to the donation is added to it,</li>
+<li>otherwise, a new SEPA direct debit order is created and a payment line linked to the donation is added to it.</li>
+</ul>
+<p>A message is added in the chatter of the donation with a link to the payment order.</p>
+<p><strong>Table of contents</strong></p>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#configuration" id="id1">Configuration</a></li>
+<li><a class="reference internal" href="#usage" id="id2">Usage</a></li>
+<li><a class="reference internal" href="#bug-tracker" id="id3">Bug Tracker</a></li>
+<li><a class="reference internal" href="#credits" id="id4">Credits</a><ul>
+<li><a class="reference internal" href="#authors" id="id5">Authors</a></li>
+<li><a class="reference internal" href="#contributors" id="id6">Contributors</a></li>
+<li><a class="reference internal" href="#maintainers" id="id7">Maintainers</a></li>
+</ul>
+</li>
+</ul>
+</div>
+<div class="section" id="configuration">
+<h1><a class="toc-backref" href="#id1">Configuration</a></h1>
+<p>In the menu <em>Invoicing &gt; Configuration &gt; Management &gt; Payment Modes</em>, make sure that you have a payment mode for SEPA Direct Debit that has the donation option active.</p>
+</div>
+<div class="section" id="usage">
+<h1><a class="toc-backref" href="#id2">Usage</a></h1>
+<p>On the donor (i.e. partner) form, create a bank account (IBAN) and create a related mandate. Validate the mandate.</p>
+<p>Create a new donation for that donor. Select the SEPA direct debit payment mode: a new required field <em>Mandate</em> will appear: select the mandate of the donor. Create the donation line(s).</p>
+<p>Validate the donation. In the chatter of the donation, you will see a message about the debit order that has been created or updated with a link to it. You can then click on the link and finalize the debit order.</p>
+</div>
+<div class="section" id="bug-tracker">
+<h1><a class="toc-backref" href="#id3">Bug Tracker</a></h1>
+<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/donation/issues">GitHub Issues</a>.
+In case of trouble, please check there if your issue has already been reported.
+If you spotted it first, help us smashing it by providing a detailed and welcomed
+<a class="reference external" href="https://github.com/OCA/donation/issues/new?body=module:%20donation_direct_debit%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
+<p>Do not contact contributors directly about support or help with technical issues.</p>
+</div>
+<div class="section" id="credits">
+<h1><a class="toc-backref" href="#id4">Credits</a></h1>
+<div class="section" id="authors">
+<h2><a class="toc-backref" href="#id5">Authors</a></h2>
+<ul class="simple">
+<li>Akretion</li>
+</ul>
+</div>
+<div class="section" id="contributors">
+<h2><a class="toc-backref" href="#id6">Contributors</a></h2>
+<ul class="simple">
+<li>Alexis de Lattre &lt;<a class="reference external" href="mailto:alexis.delattre&#64;akretion.com">alexis.delattre&#64;akretion.com</a>&gt;</li>
+</ul>
+</div>
+<div class="section" id="maintainers">
+<h2><a class="toc-backref" href="#id7">Maintainers</a></h2>
+<p>This module is maintained by the OCA.</p>
+<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
+<p>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.</p>
+<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
+<p><a class="reference external" href="https://github.com/alexis-via"><img alt="alexis-via" src="https://github.com/alexis-via.png?size=40px" /></a></p>
+<p>This module is part of the <a class="reference external" href="https://github.com/OCA/donation/tree/14.0/donation_direct_debit">OCA/donation</a> project on GitHub.</p>
+<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
+</div>
+</div>
+</div>
+</body>
+</html>
diff --git a/donation_direct_debit/views/donation.xml b/donation_direct_debit/views/donation.xml
new file mode 100644
index 000000000..b1523d535
--- /dev/null
+++ b/donation_direct_debit/views/donation.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!--
+  Copyright 2015-2021 Akretion France (http://www.akretion.com/)
+  @author: Alexis de Lattre <alexis.delattre@akretion.com>
+  License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+-->
+<odoo>
+
+
+<record id="donation_form" model="ir.ui.view">
+    <field name="name">mandate.donation.form</field>
+    <field name="model">donation.donation</field>
+    <field name="inherit_id" ref="donation.donation_form" />
+    <field name="arch" type="xml">
+        <field name="payment_mode_id" position="after">
+                <field
+                    name="mandate_id"
+                    options="{'no_create_edit': True}"
+                    attrs="{'required': [('mandate_required', '=', True)], 'invisible': [('mandate_required', '=', False)]}"
+                />
+                <field name="mandate_required" invisible="1" />
+        </field>
+    </field>
+</record>
+
+
+</odoo>
diff --git a/setup/donation_direct_debit/odoo/addons/donation_direct_debit b/setup/donation_direct_debit/odoo/addons/donation_direct_debit
new file mode 120000
index 000000000..f2dcfaea7
--- /dev/null
+++ b/setup/donation_direct_debit/odoo/addons/donation_direct_debit
@@ -0,0 +1,1 @@
+../../../../donation_direct_debit
\ No newline at end of file
diff --git a/setup/donation_direct_debit/setup.py b/setup/donation_direct_debit/setup.py
new file mode 100644
index 000000000..28c57bb64
--- /dev/null
+++ b/setup/donation_direct_debit/setup.py
@@ -0,0 +1,6 @@
+import setuptools
+
+setuptools.setup(
+    setup_requires=['setuptools-odoo'],
+    odoo_addon=True,
+)


Test patch:
diff --git a/donation_direct_debit/tests/__init__.py b/donation_direct_debit/tests/__init__.py
new file mode 100644
index 000000000..280cc6f93
--- /dev/null
+++ b/donation_direct_debit/tests/__init__.py
@@ -0,0 +1,1 @@
+from . import test_direct_debit
diff --git a/donation_direct_debit/tests/test_direct_debit.py b/donation_direct_debit/tests/test_direct_debit.py
new file mode 100644
index 000000000..1120d001b
--- /dev/null
+++ b/donation_direct_debit/tests/test_direct_debit.py
@@ -0,0 +1,49 @@
+# Copyright 2016-2021 Akretion France (http://www.akretion.com/)
+# @author: Alexis de Lattre <alexis.delattre@akretion.com>
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+
+from odoo.tests.common import TransactionCase
+
+
+class TestDirectDebit(TransactionCase):
+    def test_direct_debit(self):
+        donation = self.env.ref("donation_direct_debit.donation6")
+        dd_payment_mode = self.env.ref(
+            "account_banking_sepa_direct_debit.payment_mode_inbound_sepa_dd1"
+        )
+        bank_journal = self.env["account.journal"].create(
+            {
+                "type": "bank",
+                "name": "Bank account test",
+            }
+        )
+        dd_payment_mode.write(
+            {
+                "donation": True,
+                "bank_account_link": "fixed",
+                "fixed_journal_id": bank_journal.id,
+            }
+        )
+        donation.validate()
+        self.assertEqual(donation.state, "done")
+        self.assertTrue(donation.move_id)
+        self.assertEqual(donation.mandate_id, donation.move_id.mandate_id)
+        self.assertEqual(donation.payment_mode_id, donation.move_id.payment_mode_id)
+        paylines = self.env["account.payment.line"].search(
+            [
+                ("communication", "=", donation.payment_ref),
+                ("partner_id", "=", donation.commercial_partner_id.id),
+            ]
+        )
+        self.assertEqual(len(paylines), 1)
+        payline = paylines[0]
+        self.assertFalse(
+            donation.currency_id.compare_amounts(
+                payline.amount_currency, donation.check_total
+            )
+        )
+        self.assertEqual(payline.currency_id, donation.currency_id)
+        self.assertTrue(payline.move_line_id in donation.move_id.line_ids)
+        self.assertEqual(payline.partner_bank_id, donation.mandate_id.partner_bank_id)
+        self.assertEqual(payline.mandate_id, donation.mandate_id)
+        self.assertEqual(payline.order_id.state, "draft")