From 806eaa2ba4bee27c7956196cd6f14e9379cceda1 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Tue, 22 Mar 2022 16:42:54 +0100
Subject: [PATCH] [update] add batch queue for new contribution wizard and
 remove threading

---
 wizard/scop_cotisation_aura_wizard.py | 31 +++++++++++++++------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/wizard/scop_cotisation_aura_wizard.py b/wizard/scop_cotisation_aura_wizard.py
index e9026ef..a208235 100644
--- a/wizard/scop_cotisation_aura_wizard.py
+++ b/wizard/scop_cotisation_aura_wizard.py
@@ -85,20 +85,23 @@ class ScopCotisationWizard(models.TransientModel):
             message_id = self.env['message.wizard'].create(
                 {'message': message})
 
-            # Création de la task
-            cotiz_aura_task = self.env['scop.cotisation.task'].create({
-                'year': self.year,
-                'cotiz_to_create': len(members_to_invoice),
-                'message': "En cours de création",
-                'status': 'in_process'
-            })
-            cotiz_aura_task.env.cr.commit()
-            # Lancement du calcul en arrière plan
-            threaded_cotiz = threading.Thread(
-                target=self.cotisation_aura_id.process_cotiz_generate,
-                args=(members_to_invoice, cotiz_aura_task,
-                      self.nb_quarter_aura, self.date))
-            threaded_cotiz.start()
+            # Job queue
+            batch_name = (fields.Datetime.to_string(fields.Datetime.now()) +
+                          " Cotisations nouveaux adhérents " + str(self.year))
+            batch = self.env['queue.job.batch'].get_new_batch(batch_name)
+            for member in members_to_invoice:
+                liasse = self.cotisation_aura_id.get_liasse(member)
+                amount_aura = self.cotisation_aura_id.round_to_closest_multiple(
+                    self.cotisation_aura_id.get_cotiz_aura(member, liasse), 4)
+
+                # Mise en file d'attente de la création du bordereau
+                self.cotisation_aura_id.with_context(
+                    job_batch=batch).with_delay().create_contribution_aura(
+                    partner=member, amount=amount_aura,
+                    nb_quarter_aura=4, liasse=liasse, date=False)
+
+                # Lancement de la file d'attente
+            batch.enqueue()
         else:
             message = ("<p class='text-center'>Tous les appels de cotisations "
                        "pour les nouveaux adhérents connus ont déjà été "
-- 
GitLab