From eec2d5fc6a6b89928e45208b79ed32bf22ed9cf7 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Thu, 9 Jun 2022 19:25:56 +0200
Subject: [PATCH] [update] wizard new member

---
 wizard/scop_cotisation_cg_wizard.py  | 41 +++++++++++++++++++---------
 wizard/scop_cotisation_cg_wizard.xml | 34 +++++++++++++++++++----
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/wizard/scop_cotisation_cg_wizard.py b/wizard/scop_cotisation_cg_wizard.py
index 3e3d30f..ad9a770 100644
--- a/wizard/scop_cotisation_cg_wizard.py
+++ b/wizard/scop_cotisation_cg_wizard.py
@@ -12,9 +12,20 @@ class ScopCotisationWizard(models.TransientModel):
     _name = "scop.cotisation.cg.wizard"
     _description = "Wizard: Génération des cotisations nouveaux adhérents"
 
-    year = fields.Char(string="Année", required=True)
+    @api.model
+    def _default_cotisation_cg_id(self):
+        return self.env.context.get("active_id")
+
+    @api.model
+    def _default_year(self):
+        return self.env["scop.cotisation.cg"].browse(
+            self.env.context.get("active_id")).year
+
+    year = fields.Char(string="Année", required=True, default=_default_year)
     cotisation_cg_id = fields.Many2one(
-        comodel_name="scop.cotisation.cg", string="Base de cotisation"
+        comodel_name="scop.cotisation.cg",
+        string="Base de cotisation",
+        default=_default_cotisation_cg_id,
     )
     nb_quarter = fields.Selection(
         string="Nombre de trimestres de cotisation",
@@ -31,26 +42,30 @@ class ScopCotisationWizard(models.TransientModel):
         default="all",
     )
     partner_ids = fields.Many2many(comodel_name="res.partner", string="Coopératives")
-    date = fields.Date("Date de cotisation", default=fields.Date.today())
+    date = fields.Date("Date de bordereau", default=fields.Date.today())
+    date_start = fields.Date("Adhésion après le")
+    date_limit = fields.Date("Adhésion avant le")
 
     # ------------------------------------------------------
     # Button function
     # ------------------------------------------------------
-    @api.onchange("type")
+    @api.onchange("type", "date_start", "date_limit")
     def onchange_domain_partner_ids(self):
         if self.type == "selected":
-            id_cotisation_cg = self.env.context["id_cotisation_cg"]
-            invoiced_members = self.cotisation_cg_id.browse(
-                id_cotisation_cg
-            ).invoice_ids.mapped("partner_id")
+            invoiced_members = self.cotisation_cg_id.bordereau_ids.mapped('partner_id')
+            domain = [
+                ("is_cooperative", "=", True),
+                ("type", "!=", "facility"),
+                ("id", "not in", invoiced_members.mapped("id")),
+            ]
+            if self.date_limit:
+                domain.append(("member_start", "<=", self.date_limit))
+            if self.date_start:
+                domain.append(("member_start", ">=", self.date_start))
 
             res = {
                 "domain": {
-                    "partner_ids": [
-                        ("is_company", "=", True),
-                        ("type", "!=", "facility"),
-                        ("id", "not in", invoiced_members.mapped("id")),
-                    ]
+                    "partner_ids": domain
                 }
             }
             return res
diff --git a/wizard/scop_cotisation_cg_wizard.xml b/wizard/scop_cotisation_cg_wizard.xml
index 33c52e8..93d8bf4 100644
--- a/wizard/scop_cotisation_cg_wizard.xml
+++ b/wizard/scop_cotisation_cg_wizard.xml
@@ -12,15 +12,37 @@
                     <group>
                         <field name="year" required="1" invisible="1" />
                         <field name="date" required="1" />
+                    </group>
+                    <div class="mt16 text-muted">
+                        La sélection des coopératives est faite sur <strong
+                        >la date d'adhésion</strong> (est non sur <em
+                        >la date d'adhésion statistique</em>)
+                    </div>
+                    <group>
+                        <field name="date_start" />
+                        <field name="date_limit" />
                         <field name="nb_quarter" required="1" />
                         <field name="type" widget="radio" />
-                        <field
-                            name="partner_ids"
-                            attrs="{'invisible': [('type', '=', 'all')]}"
-                            widget="many2many_tags"
-                            options="{'no_create': True, 'no_open': True}"
-                        />
                     </group>
+                    <label
+                        for="partner_ids"
+                        attrs="{'invisible': [('type', '=', 'all')]}"
+                    />
+                    <field
+                        name="partner_ids"
+                        attrs="{'invisible': [('type', '=', 'all')]}"
+                        options="{'no_create': True, 'no_open': True}"
+                        widget="many2many"
+                        mode="tree"
+                    >
+                        <tree>
+                            <field name="member_number" string="N° Adhérent" />
+                            <field name="name" />
+                            <field name="ur_id" />
+                            <field name="member_start" />
+                            <field name="member_start_stat" />
+                        </tree>
+                    </field>
                     <footer>
                         <button
                             name="cotiz_generate_new_adherents"
-- 
GitLab