diff --git a/models/scop_cotisation_cg.py b/models/scop_cotisation_cg.py index c58fa0d7f3819f027a2e72a5f54d819f5155dd5c..397f7568595bc7f3f6ee9ff66ba7dcabce6111e8 100644 --- a/models/scop_cotisation_cg.py +++ b/models/scop_cotisation_cg.py @@ -78,6 +78,15 @@ class ScopCotisation(models.Model): graph_values = fields.Text(compute="_compute_graph_values") + batch_id = fields.Many2one( + comodel_name="queue.job.batch", + name="Files d'attente en cours" + ) + batch_ids_str = fields.Char() + batch_count = fields.Integer( + compute="_compute_batch_count" + ) + # ------------------------------------------------------ # Contraintes SQL # ------------------------------------------------------ @@ -191,6 +200,13 @@ class ScopCotisation(models.Model): elif cotiz.state != "ongoing": cotiz.state = "ongoing" + def _compute_batch_count(self): + for base in self: + if not base.batch_ids_str: + base.batch_count = 0 + else: + base.batch_count = len(base.batch_ids_str.split(",")) + # ------------------------------------------------------ # Button functions # ------------------------------------------------------ @@ -286,6 +302,17 @@ class ScopCotisation(models.Model): Create contribution for members :params member_ids: list of member ids """ + # Vérifie que des tâches ne sont pas en cours + self.ensure_one() + queue_ids = self.env["queue.job"].search([ + ("job_batch_id", "in", [int(x) for x in self.batch_ids_str.split(",")]), + ("state", "not in", ["done", "cancelled", "failed"]) + ]) + if queue_ids: + raise exceptions.UserError(_( + "Des tâches de création sont encore en cours." + )) + member_to_invoice = self.env["res.partner"].browse(member_ids) # Job queue batch_name = ( @@ -294,9 +321,22 @@ class ScopCotisation(models.Model): + self.year ) batch = self.env["queue.job.batch"].get_new_batch(batch_name) + # Stocke le dernier batch en cours + self.batch_id = batch + # Stocke la liste des batchs au format texte + if not self.batch_ids_str: + self.batch_ids_str = str(batch.id) + else: + self.batch_ids_str += ", %s" % (str(batch.id),) + # Ajoute les bordereaux au batch for member in member_to_invoice: liasse_id = self.get_liasse(member) - self.with_context(job_batch=batch).with_delay().create_bordereau( + description = "Cotisation %s - Adhérent n°%s - %s" % ( + self.year, member.member_number, member.name + ) + self.with_context( + job_batch=batch).with_delay( + max_retries=3, description=description).create_bordereau( member=member, liasse=liasse_id, nb_quarter="4", date=False ) batch.enqueue() @@ -758,6 +798,20 @@ class ScopCotisation(models.Model): "flags": {"mode": "readonly", "default_buttons": False}, } + def show_batch(self): + self.ensure_one() + batch_ids = self.env["queue.job.batch"].browse( + [int(x) for x in self.batch_ids_str.split(",")] + ) + return { + "name": "Files d'attente", + "type": "ir.actions.act_window", + "view_type": "form", + "view_mode": "tree,form", + "res_model": "queue.job.batch", + "domain": [("id", "in", batch_ids.ids)], + } + # ------------------------------------------------------ # Global functions # ------------------------------------------------------ diff --git a/views/scop_cotisation_cg.xml b/views/scop_cotisation_cg.xml index 028432cee60ffe239868012bfa10379e8f2d37fb..bc7947ec78cb9a06259b5f33d9d9196430a52656 100644 --- a/views/scop_cotisation_cg.xml +++ b/views/scop_cotisation_cg.xml @@ -62,6 +62,16 @@ <field name="state" widget="statusbar" clickable="False" /> </header> <sheet> + <div class="oe_button_box" name="contribution_button_box"> + <button name="show_batch" + type="object" + class="oe_stat_button" + icon="fa-list" + attrs="{'invisible': [('batch_count', '=', 0)]}" + > + <field string="Files d'attente" name="batch_count" widget="statinfo" /> + </button> + </div> <h1><field name="name" /></h1> <group> <group name="contribution" string="Cotisations">