From dcf1a5b7fd26d0db695355bae04cf3ce4f5a5b5d Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Fri, 28 Jan 2022 11:34:42 +0100
Subject: [PATCH] [update] move print button on report form view, remove from
 tree view & add compute field

---
 __manifest__.py                     |  5 +++--
 report/scop_contribution_report.py  | 30 ++++++++++++++++++++++++++++-
 report/scop_contribution_report.xml | 24 +++++++++++++++++++++++
 views/res_partner.xml               | 20 -------------------
 4 files changed, 56 insertions(+), 23 deletions(-)
 create mode 100644 report/scop_contribution_report.xml
 delete mode 100644 views/res_partner.xml

diff --git a/__manifest__.py b/__manifest__.py
index 3812e95..ebaf4ba 100755
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -24,17 +24,18 @@
         "datas/mail_data.xml",
         "datas/queue_job_data.xml",
         "datas/ir_sequence_data.xml",
-        # Reports
+        # Templates
         "templates/report_scop_bordereau.xml",
         "templates/report_scop_bordereau_payments.xml",
         "templates/report_scop_bordereau_refund.xml",
         # Views
         "views/account_invoice.xml",
         "views/res_config_settings.xml",
-        "views/res_partner.xml",
         "views/scop_cotisation_cg.xml",
         "views/scop_cotisation_simulation.xml",
         "views/scop_liasse_fiscale.xml",
+        # Reports
+        "report/scop_contribution_report.xml",
         # Wizards
         "wizard/export_journal_wizard_view.xml",
         "wizard/scop_cotisation_cg_regul_wizard.xml",
diff --git a/report/scop_contribution_report.py b/report/scop_contribution_report.py
index 5f9a450..d4c0fa3 100644
--- a/report/scop_contribution_report.py
+++ b/report/scop_contribution_report.py
@@ -1,13 +1,41 @@
 # © 2022 Le Filament (<http://www.le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
-from odoo import models
+from odoo import models, fields, api
 from odoo.exceptions import UserError
 
 
 class ScopContributionReport(models.Model):
     _inherit = "scop.contribution.report"
 
+    has_bordereau = fields.Boolean("Bordereau lié", compute="_compute_has_bordereau")
+
+    # ------------------------------------------------------
+    # Compute
+    # ------------------------------------------------------
+    @api.multi
+    def _compute_has_bordereau(self):
+        contrib_cg = self.env.ref('cgscop_partner.riga_14397')
+        for contribution in self:
+            # exclude contribution before 2021 from RIGA
+            if contribution.year > '2020':
+                if contribution.type_contribution_id == contrib_cg:
+                    bordereau = self.env['scop.bordereau'].search([
+                        ('partner_id', '=', self.partner_id.id),
+                        ('year', '=', self.year),
+                    ])
+                    if len(bordereau) == 1:
+                        contribution.has_bordereau = True
+                    else:
+                        contribution.has_bordereau = False
+                else:
+                    contribution.has_bordereau = False
+            else:
+                contribution.has_bordereau = False
+
+    # ------------------------------------------------------
+    # Button
+    # ------------------------------------------------------
     def print_report_with_payment(self):
         bordereau = self.env['scop.bordereau'].search([
             ('partner_id', '=', self.partner_id.id),
diff --git a/report/scop_contribution_report.xml b/report/scop_contribution_report.xml
new file mode 100644
index 0000000..bdf5c44
--- /dev/null
+++ b/report/scop_contribution_report.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+    <data>
+        <!-- FORM VIEW -->
+        <record model="ir.ui.view" id="scop_contribution_report_cg_form">
+            <field name="name">scop.contribution.report.cg.form</field>
+            <field name="model">scop.contribution.report</field>
+            <field name="inherit_id" ref="cgscop_cotisation.scop_contribution_report_form"/>
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='payments']" position="before">
+                    <field name="has_bordereau" invisible="1"/>
+                    <div attrs="{'invisible': [('has_bordereau', '=', False)]}" class="mt16 mb16">
+                        <button name="print_report_with_payment"
+                                type="object"
+                                string="Imprimer le bordereau"
+                                icon="fa-file-pdf-o"
+                                class="btn btn-sm btn-outline-info"
+                        />
+                    </div>
+                </xpath>
+            </field>
+        </record>
+    </data>
+</odoo>
diff --git a/views/res_partner.xml b/views/res_partner.xml
deleted file mode 100644
index 7a94d47..0000000
--- a/views/res_partner.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<odoo>
-    <!--  Copyright 2022 Le Filament
-          License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).  -->
-    <data>
-
-        <record id="scop_contact_view_form" model="ir.ui.view">
-            <field name="name">res.partner.scop.view.form</field>
-            <field name="model">res.partner</field>
-            <field name="inherit_id" ref="cgscop_cotisation.scop_contact_view_form"/>
-            <field name="arch" type="xml">
-                <xpath expr="//page[@name='contribution']/field[@name='contribution_report_ids']/tree" position="inside">
-                     <button name="print_report_with_payment" type="object"
-                             help="Télécharger l'état des paiements" icon="fa-file-pdf-o"
-                             attrs="{'invisible':[('type_contribution_id','!=',%(cgscop_partner.riga_14397)d)]}"/>
-                </xpath>
-            </field>
-        </record>
-    </data>
-</odoo>
\ No newline at end of file
-- 
GitLab