From fb2ced986222fd8a49684d4bcfa64cc987845280 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Tue, 16 Apr 2024 14:41:50 +0200
Subject: [PATCH] [UPD] add track visibility and year filter

---
 __manifest__.py                            |  1 +
 models/account_invoice.py                  | 10 ++++++--
 models/account_journal_dashboard.py        | 13 +++++++++-
 report/account_invoice_employee_report.xml |  6 +++++
 report/account_invoice_report.xml          | 28 ++++++++++++++++++++++
 views/account_invoice_views.xml            | 17 +++++++++++++
 6 files changed, 72 insertions(+), 3 deletions(-)
 create mode 100644 report/account_invoice_report.xml

diff --git a/__manifest__.py b/__manifest__.py
index de4502e..693799c 100644
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -14,6 +14,7 @@
         "templates/invoice_allocation_templates.xml",
         # templates
         "report/account_invoice_employee_report.xml",
+        "report/account_invoice_report.xml",
         # views
         "views/assets.xml",
         "views/account_invoice_views.xml",
diff --git a/models/account_invoice.py b/models/account_invoice.py
index db70cc1..dfebfad 100644
--- a/models/account_invoice.py
+++ b/models/account_invoice.py
@@ -8,8 +8,14 @@ class AccountInvoice(models.Model):
     _inherit = "account.invoice"
 
     with_detail = fields.Boolean("Facture avec détail", default=False)
-    subcontracting_amount = fields.Monetary("Montant sous-traitance (HT)", track_visibility=True,)
-    expense_amount = fields.Monetary("Montant frais (HT)", track_visibility=True,)
+    subcontracting_amount = fields.Monetary(
+        "Montant sous-traitance (HT)",
+        track_visibility=True,
+    )
+    expense_amount = fields.Monetary(
+        "Montant frais (HT)",
+        track_visibility=True,
+    )
     production_amount = fields.Monetary(
         string="CA production",
         compute="_compute_production_amount",
diff --git a/models/account_journal_dashboard.py b/models/account_journal_dashboard.py
index 6564e78..d347bf8 100644
--- a/models/account_journal_dashboard.py
+++ b/models/account_journal_dashboard.py
@@ -1,7 +1,7 @@
 # Copyright 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 api, models
 
 
 class AccountJournal(models.Model):
@@ -26,3 +26,14 @@ class AccountJournal(models.Model):
             WHERE journal_id = %(journal_id)s AND state = 'draft';""",
             {"journal_id": self.id},
         )
+
+    @api.multi
+    def open_action(self):
+        """
+        Surcharge la fonction parente pour modifier le contexte de
+        l'action sur les factures clients
+        """
+        action = super(AccountJournal, self).open_action()
+        if action["xml_id"] == "account.action_invoice_tree1":
+            action["context"].update({"search_default_this_year": 1})
+        return action
diff --git a/report/account_invoice_employee_report.xml b/report/account_invoice_employee_report.xml
index 1acf236..df954e3 100644
--- a/report/account_invoice_employee_report.xml
+++ b/report/account_invoice_employee_report.xml
@@ -10,6 +10,11 @@
                     <field name="partner_id" />
                     <field name="invoice_id" />
                     <field name="user_id" />
+                    <filter
+                        string="Cette année"
+                        name="this_year"
+                        domain="['|', ('date_invoice', '&gt;=', context_today().strftime('%Y-01-01')), ('date_invoice', '=', False)]"
+                    />
                     <group expand="0" string="Group By">
                         <filter
                             name="group_by_date"
@@ -86,6 +91,7 @@
             <field name="type">ir.actions.act_window</field>
             <field name="res_model">account.invoice.employee.report</field>
             <field name="view_mode">pivot,graph,tree</field>
+            <field name="context">{'search_default_this_year': 1}</field>
         </record>
 
         <!-- Menu -->
diff --git a/report/account_invoice_report.xml b/report/account_invoice_report.xml
new file mode 100644
index 0000000..45f5a81
--- /dev/null
+++ b/report/account_invoice_report.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<odoo>
+    <data>
+        <!-- SEARCH VIEW -->
+        <record model="ir.ui.view" id="view_account_invoice_report_search">
+            <field name="name">account.invoice.report.search</field>
+            <field name="model">account.invoice.report</field>
+            <field name="inherit_id" ref="account.view_account_invoice_report_search" />
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='date']" position="before">
+                    <filter
+                        string="Cette année"
+                        name="this_year"
+                        domain="['|', ('date', '&gt;=', context_today().strftime('%Y-01-01')), ('date', '=', False)]"
+                    />
+                </xpath>
+            </field>
+        </record>
+
+        <!-- ACTION -->
+        <record
+            id="account_invoice_employee_report_action"
+            model="ir.actions.act_window"
+        >
+            <field name="context">{'search_default_current':1, 'search_default_customer':1, 'group_by':[], 'group_by_no_leaf':1,'search_default_this_year': 1}</field>
+        </record>
+    </data>
+</odoo>
diff --git a/views/account_invoice_views.xml b/views/account_invoice_views.xml
index b6db5c0..5bd9e07 100644
--- a/views/account_invoice_views.xml
+++ b/views/account_invoice_views.xml
@@ -103,9 +103,26 @@
                         name="assign_error"
                         domain="[('is_allocation_error', '=', True)]"
                     />
+                    <separator />
+                    <filter
+                        string="Cette année"
+                        name="this_year"
+                        domain="['|', ('date_invoice', '&gt;=', context_today().strftime('%Y-01-01')), ('date_invoice', '=', False)]"
+                    />
                 </xpath>
             </field>
         </record>
 
+        <!-- Inherit action -->
+        <record id="account.action_invoice_tree1" model="ir.actions.act_window">
+            <field
+                name="context"
+            >{'type':'out_invoice', 'journal_type': 'sale', 'search_default_this_year': 1}</field>
+        </record>
+        <record id="account.action_invoice_out_refund" model="ir.actions.act_window">
+            <field
+                name="context"
+            >{'default_type': 'out_refund', 'type': 'out_refund', 'journal_type': 'sale', 'search_default_this_year': 1}</field>
+        </record>
     </data>
 </odoo>
-- 
GitLab