From c30d7a068fdffa15530c77c1fba3f2814530373c Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Thu, 12 May 2022 15:17:46 +0200
Subject: [PATCH] [add] IBAN and mandate checks on payment order

---
 __manifest__.py                             |  1 +
 models/account_payment_order.py             | 35 +++++++++++++++++++++
 views/account_payment_order.xml             | 19 ++++++++++-
 wizard/__init__.py                          |  1 +
 wizard/account_payment_line_create.py       | 24 ++++++++++++++
 wizard/account_payment_line_create_view.xml | 24 ++++++++++++++
 6 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100755 wizard/account_payment_line_create.py
 create mode 100755 wizard/account_payment_line_create_view.xml

diff --git a/__manifest__.py b/__manifest__.py
index 4ac1922..2fece79 100755
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -26,6 +26,7 @@
         "views/res_partner.xml",
         "views/scop_cotisation_task.xml",
         "report/scop_contribution_report.xml",
+        "wizard/account_payment_line_create_view.xml",
     ],
     "qweb": [
         "static/src/xml/*.xml",
diff --git a/models/account_payment_order.py b/models/account_payment_order.py
index c4685cd..1591a34 100644
--- a/models/account_payment_order.py
+++ b/models/account_payment_order.py
@@ -20,6 +20,7 @@ class AccountPaymentOrder(models.Model):
         comodel_name='ir.attachment',
         compute='_compute_attachment_ids'
     )
+    mandate_validity = fields.Boolean("Mandats valides", compute="_compute_mandate_validity")
 
     # ------------------------------------------------------
     # Compute fields
@@ -47,6 +48,16 @@ class AccountPaymentOrder(models.Model):
                ('res_id', '=', po.id)
            ])
 
+    @api.multi
+    def _compute_mandate_validity(self):
+        for o in self:
+            validity = o.mapped("payment_line_ids.mandate_id").filtered(
+                lambda m: m.state != "valid")
+            if validity:
+                o.mandate_validity = False
+            else:
+                o.mandate_validity = True
+
     # ------------------------------------------------------
     # Button function
     # ------------------------------------------------------
@@ -78,6 +89,30 @@ class AccountPaymentOrder(models.Model):
             'domain': [['payment_order_id', '=', self.id]],
         }
 
+    def view_wrong_iban(self):
+        self.ensure_one()
+        bank_ids = self.mapped("payment_line_ids.partner_bank_id").filtered(
+            lambda b: b.acc_type != 'iban')
+        return {
+            'type': 'ir.actions.act_window',
+            'name': "Comptes bancaires",
+            'res_model': 'res.partner.bank',
+            'views': [[False, 'tree'], [False, 'form']],
+            'domain': [['id', 'in', bank_ids.ids]],
+        }
+
+    def view_wrong_mandate(self):
+        self.ensure_one()
+        mandate_ids = self.mapped("payment_line_ids.mandate_id").filtered(
+            lambda m: m.state != "valid")
+        return {
+            'type': 'ir.actions.act_window',
+            'name': "Mandats non valides",
+            'res_model': 'account.banking.mandate',
+            'views': [[False, 'tree'], [False, 'form']],
+            'domain': [['id', 'in', mandate_ids.ids]],
+        }
+
     # ------------------------------------------------------
     # Common function
     # ------------------------------------------------------
diff --git a/views/account_payment_order.xml b/views/account_payment_order.xml
index 47a3907..fce883f 100644
--- a/views/account_payment_order.xml
+++ b/views/account_payment_order.xml
@@ -23,7 +23,24 @@
                     <button name="view_payment_line"
                             type="object"
                             string="Modifier les lignes de paiement"
-                            attrs="{'invisible': [('state', '!=', 'draft')]}"/>
+                            attrs="{'invisible': [('state', '!=', 'draft')]}"
+                    />
+                </xpath>
+                <xpath expr="//notebook" position="before">
+                    <field name="mandate_validity" invisible="1"/>
+                    <button name="view_wrong_iban"
+                            type="object"
+                            string="Voir les IBAN à corriger"
+                            class="btn-danger"
+                            attrs="{'invisible': [('sepa', '=', True)]}"
+                            style="margin-right: 10px;"
+                    />
+                    <button name="view_wrong_mandate"
+                            type="object"
+                            string="Voir les Mandats non valides"
+                            class="btn-danger"
+                            attrs="{'invisible': [('mandate_validity', '=', True)]}"
+                    />
                 </xpath>
                 <xpath expr="//field[@name='description']" position="after">
                     <button name="view_account_move"
diff --git a/wizard/__init__.py b/wizard/__init__.py
index 19819ad..e6dcf9d 100644
--- a/wizard/__init__.py
+++ b/wizard/__init__.py
@@ -2,3 +2,4 @@
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
 from . import account_invoice_refund
+from . import account_payment_line_create
diff --git a/wizard/account_payment_line_create.py b/wizard/account_payment_line_create.py
new file mode 100755
index 0000000..b7e39f6
--- /dev/null
+++ b/wizard/account_payment_line_create.py
@@ -0,0 +1,24 @@
+# © 2020 Le Filament (<http://www.le-filament.com>)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import models, fields, api, _
+
+
+class AccountPaymentLineCreate(models.TransientModel):
+    _inherit = 'account.payment.line.create'
+
+    start_date = fields.Date(string="Date Mini")
+    ur_ids = fields.Many2many(
+        comodel_name="union.regionale",
+        string="Union Régionale"
+    )
+
+    def _prepare_move_line_domain(self):
+        self.ensure_one()
+        domain = super()._prepare_move_line_domain()
+        if self.start_date:
+            domain += [("date_maturity", ">=", self.start_date)]
+        if self.ur_ids:
+            domain += [("partner_id.ur_id", "in", self.ur_ids.ids)]
+
+        return domain
diff --git a/wizard/account_payment_line_create_view.xml b/wizard/account_payment_line_create_view.xml
new file mode 100755
index 0000000..5faf1d6
--- /dev/null
+++ b/wizard/account_payment_line_create_view.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+    <data>
+        <record id="account_payment_line_create_form" model="ir.ui.view">
+            <field name="name">account_payment_line_create.form</field>
+            <field name="model">account.payment.line.create</field>
+            <field name="inherit_id" ref="account_payment_order.account_payment_line_create_form"/>
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='due_date']" position="before">
+                    <field name="start_date"
+                           attrs="{'invisible': [('date_type', '!=', 'due')]}"
+                    />
+                </xpath>
+                <xpath expr="//field[@name='journal_ids']" position="after">
+                    <field name="ur_ids"
+                           widget="many2many_tags"
+                           options="{'no_create': 1, 'no_edit': 1}"
+                           placeholder="Laisser vide pour toutes les UR"
+                    />
+                </xpath>
+            </field>
+        </record>
+    </data>
+</odoo>
-- 
GitLab