From 5e0af90ad59a33bec3f42c0a80151e603b693467 Mon Sep 17 00:00:00 2001
From: Juliana <juliana@le-filament.com>
Date: Mon, 2 May 2022 16:38:38 +0200
Subject: [PATCH] [ADD]Add functionality to get all data from enedis

---
 models/acc_operation.py                | 16 ++++++++++++++++
 wizards/acc_operation_wizard.py        | 15 +++++++++++++++
 wizards/acc_operation_wizard_views.xml | 10 ++++++++--
 3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/models/acc_operation.py b/models/acc_operation.py
index f4a31b3..a175a49 100644
--- a/models/acc_operation.py
+++ b/models/acc_operation.py
@@ -88,6 +88,22 @@ class AccOperation(models.Model):
         # lancement de la file d'attente
         batch.enqueue()
 
+    def get_curves_all(self, date_start):
+        # Calcul du nombre de mois entre la date de début de contrat
+        # de l'opération et la date du jour pour lancer la récupération des données
+        num_months = (date.today().year - date_start.year) * 12 + (
+                date.today().month - date_start.month)
+
+        date_start_it = date_start
+        date_end_it = date_start + relativedelta(months=1, days=-1)
+
+        i = 1
+        while i < num_months:
+            self.get_curves(date_start_it, date_end_it)
+            date_start_it = date_start + relativedelta(months=1)
+            date_end_it = date_start_it + relativedelta(months=1, days=-1)
+            i += 1
+
     def get_perimeter(self):
         for operation in self:
             operation.perimeter()
diff --git a/wizards/acc_operation_wizard.py b/wizards/acc_operation_wizard.py
index 647a971..b1ea583 100644
--- a/wizards/acc_operation_wizard.py
+++ b/wizards/acc_operation_wizard.py
@@ -39,6 +39,8 @@ class AccOperationWizard(models.TransientModel):
     # Actions
     # ------------------------------------------------------
     def get_curves(self):
+        if not self.date_end and not self.date_start:
+            raise UserError(_("Les champs Date de début et Date de fin sont obligatoires"))
         if (self.date_end - self.date_start).days > 31:
             raise UserError(_("L'intervalle de temps ne doit pas dépasser 31 Jours"))
         context = dict(self._context or {})
@@ -48,6 +50,19 @@ class AccOperationWizard(models.TransientModel):
             )
         return {"type": "ir.actions.act_window_close"}
 
+    def get_curves_all(self):
+        context = dict(self._context or {})
+        if context.get("active_ids", False):
+            op = self.env["acc.operation"].browse(context.get("active_ids"))
+            if not op.date_start_contract:
+                raise UserError(
+                    _("Renseigner une date de début de contrat pour pouvoir récupérer les courbes à partie de cette date."))
+            date_start = op.date_start_contract
+            self.env["acc.operation"].browse(context.get("active_ids")).get_curves_all(
+                date_start
+            )
+        return {"type": "ir.actions.act_window_close"}
+
     # ------------------------------------------------------
     # Business methods
     # ------------------------------------------------------
diff --git a/wizards/acc_operation_wizard_views.xml b/wizards/acc_operation_wizard_views.xml
index 2f720fd..8ccd622 100644
--- a/wizards/acc_operation_wizard_views.xml
+++ b/wizards/acc_operation_wizard_views.xml
@@ -7,8 +7,8 @@
         <field name="arch" type="xml">
             <form string="Choix de la période">
                 <group name="period" string="Période" col="2">
-                    <field name="date_start" required="1" />
-                    <field name="date_end" required="1" />
+                    <field name="date_start"/>
+                    <field name="date_end"/>
                 </group>
                 <footer>
                     <button
@@ -17,6 +17,12 @@
                         string="Récupérer"
                         type="object"
                     />
+                    <button
+                        class="btn btn-sm btn-primary"
+                        name="get_curves_all"
+                        string="Récupérer depuis le début"
+                        type="object"
+                    />
                     <button
                         class="btn btn-sm btn-default"
                         special="cancel"
-- 
GitLab