From f0a8d57b23c8d1a8ad8c98afb7e3e38fdb313467 Mon Sep 17 00:00:00 2001
From: Benjamin <benjamin@le-filament.com>
Date: Thu, 1 Apr 2021 17:36:43 +0200
Subject: [PATCH] [add] exempt boolean and view on contribution line

---
 __manifest__.py             |  1 +
 models/scop_contribution.py | 38 +++++++++++++++++++++++++++++++++++++
 views/account_invoice.xml   | 14 ++++++++++++++
 views/res_partner.xml       | 19 +++++++++++++++++++
 4 files changed, 72 insertions(+)
 create mode 100644 views/res_partner.xml

diff --git a/__manifest__.py b/__manifest__.py
index 9063489..d026b4c 100755
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -21,6 +21,7 @@
         "views/account_payment_term.xml",
         "views/account_payment_order.xml",
         "views/res_config_settings.xml",
+        "views/res_partner.xml",
         "views/scop_cotisation_task.xml",
     ],
     'post_init_hook': '_configure_journals',
diff --git a/models/scop_contribution.py b/models/scop_contribution.py
index f1cae59..1755bc8 100644
--- a/models/scop_contribution.py
+++ b/models/scop_contribution.py
@@ -15,6 +15,11 @@ class ScopContributions(models.Model):
         compute='_compute_amount_paid', store=True)
     amount_remaining = fields.Float(
         compute='_compute_amount_remaining_previous', store=True)
+    is_exempt = fields.Boolean(
+        string='Exonération',
+        compute='_compute_is_exempt',
+        default=False
+    )
 
     @api.depends('amount_remaining')
     @api.multi
@@ -27,3 +32,36 @@ class ScopContributions(models.Model):
     def _compute_amount_remaining_previous(self):
         for r in self:
             r.amount_remaining = r.invoice_id.residual
+
+    @api.multi
+    def _compute_is_exempt(self):
+        for contrib in self:
+            if contrib.invoice_id:
+                is_refund = contrib.invoice_id.search([
+                    ('refund_invoice_id', '=', contrib.invoice_id.id),
+                    ('state', 'in', ['open', 'paid'])])
+                if is_refund:
+                    contrib.is_exempt = True
+                else:
+                    contrib.is_exempt = False
+            else:
+                contrib.is_exempt = False
+
+    def view_refund(self):
+        tree_id = self.env.ref(
+            'cgscop_cotisation.invoice_scop_contribution_refund_tree').id
+        refund_ids = self.invoice_id.search([
+            ('refund_invoice_id', '=', self.invoice_id.id),
+            ('state', 'in', ['open', 'paid'])
+        ])
+        print(refund_ids)
+        print(tree_id)
+        return {
+            'type': 'ir.actions.act_window',
+            'name': 'Exonérations',
+            'res_model': 'account.invoice',
+            'views': [[tree_id, 'tree']],
+            'target': 'new',
+            'domain': [['id', 'in', refund_ids.ids]],
+            'flags': {'action_buttons': False}
+        }
diff --git a/views/account_invoice.xml b/views/account_invoice.xml
index 73a5f56..d1b466a 100644
--- a/views/account_invoice.xml
+++ b/views/account_invoice.xml
@@ -70,6 +70,20 @@
             </field>
         </record>
 
+        <record id="invoice_scop_contribution_refund_tree" model="ir.ui.view">
+            <field name="name">account.invoice.tree.scop.contribution</field>
+            <field name="model">account.invoice</field>
+            <field name="arch" type="xml">
+                <tree create="0" edit="0" delete="0">
+                    <field name="name"/>
+                    <field name="date_invoice" string="Date d'émission"/>
+                    <field name="number"/>
+                    <field name="amount_total_signed" widget="monetary"/>
+                    <field name="state" invisible="1"/>
+                </tree>
+            </field>
+        </record>
+
         <!-- Search -->
         <record id="invoice_search_scop_inherited" model="ir.ui.view">
             <field name="name">account.invoice.search.scop.inherited</field>
diff --git a/views/res_partner.xml b/views/res_partner.xml
new file mode 100644
index 0000000..3a99463
--- /dev/null
+++ b/views/res_partner.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+    <!--  Copyright 2020 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_partner.scop_contact_view_form"/>
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='contribution_ids']/tree/field[@name='spreading']" position="before">
+                    <field name="is_exempt" style="text-align: center;"/>
+                    <button name="view_refund" type="object" icon="fa-eye" attrs="{'invisible': [('is_exempt', '=', False)]}" style="pointer-events: visible;"/>
+                </xpath>
+            </field>
+        </record>
+    </data>
+</odoo>
\ No newline at end of file
-- 
GitLab