From 41949f398371efc1dabdddd6d04a85e4dde32266 Mon Sep 17 00:00:00 2001
From: Quentin <quentin@l-filament.com>
Date: Mon, 30 Oct 2023 17:28:20 +0100
Subject: [PATCH] =?UTF-8?q?[ADD]=20Champs=20de=20date=20de=20d=C3=A9but=20?=
 =?UTF-8?q?et=20fin=20de=20redevance?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 models/account_move.py | 33 +++++++++++++++++++++++++++++++++
 views/account_move.xml |  9 +++++++++
 2 files changed, 42 insertions(+)

diff --git a/models/account_move.py b/models/account_move.py
index 02148cc..09d59f0 100644
--- a/models/account_move.py
+++ b/models/account_move.py
@@ -1,6 +1,7 @@
 # Copyright 2021 Le Filament (<http://www.le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
+from dateutil.relativedelta import relativedelta
 from odoo import api, fields, models
 
 
@@ -17,6 +18,20 @@ class AccountMove(models.Model):
         default=False,
         help="Affiche le n° SIRET sur la facture client",
     )
+    fee_start_date = fields.Date(
+        string="Date de début de redevance",
+        compute="inverse_date_deadline",
+        readonly=False,
+        store=True,
+        default=False
+    )
+    fee_deadline_date = fields.Date(
+        string="Date de fin de redevance",
+        compute="compute_date_deadline",
+        readonly=False,
+        store=True,
+        default=False
+    )
 
     # ------------------------------------------------------
     # Override Parent
@@ -52,3 +67,21 @@ class AccountMove(models.Model):
             for line in self.line_ids:
                 if line.account_id.user_type_id.type in ("receivable", "payable"):
                     line.account_id = self.third_account_id
+
+    @api.depends("fee_start_date")
+    def compute_date_deadline(self):
+        """
+        Calcule la date de fin de redevance si la date de début de redevance est modifiée
+        """
+        for invoice in self:
+            if invoice.fee_start_date:
+                invoice.fee_deadline_date = invoice.fee_start_date + relativedelta(days=365)
+
+    @api.depends("fee_deadline_date")
+    def inverse_date_deadline(self):
+        """
+        Calcule la date de début de redevance si la date de fin de redevance est modifiée
+        """
+        for invoice in self:
+            if invoice.fee_deadline_date:
+                invoice.fee_start_date = invoice.fee_deadline_date - relativedelta(days=365)
diff --git a/views/account_move.xml b/views/account_move.xml
index 533057e..633cd57 100644
--- a/views/account_move.xml
+++ b/views/account_move.xml
@@ -27,6 +27,15 @@
                         attrs="{'readonly': [('posted_before', '=', True)]}"
                         domain="[('id', 'in', third_account_journal_ids)]"
                     />
+                    <label for="fee_deadline_date"
+                        string="Dates de redevance"
+                    />
+                    <div class="d-flex" attrs="{'invisible': [('fee_start_date', '=', False), ('state', '!=', 'draft')]}">
+                        <span class="mr-2">Du</span>
+                        <field name="fee_start_date" attrs="{'readonly': [('posted_before', '=', True)]}"/>
+                        <span class="mx-2">au</span>
+                        <field name="fee_deadline_date"  attrs="{'readonly': [('posted_before', '=', True)]}"/>
+                    </div>
                 </xpath>
             </field>
         </record>
-- 
GitLab