From cae334e9c04cebf21d32df96ef041f8bf3e00257 Mon Sep 17 00:00:00 2001
From: jordan <jordan@le-filament.com>
Date: Wed, 17 Feb 2021 10:48:24 +0100
Subject: [PATCH] [update] conf for email with compute recipients

---
 models/res_company.py         |  5 +++++
 models/res_config_settings.py | 12 ++++++++++++
 models/scop_cotisation.py     | 28 ++++++++++++++++++++++++++++
 views/res_config_settings.xml | 18 +++++++++++++-----
 4 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/models/res_company.py b/models/res_company.py
index e76b03a..74b1685 100644
--- a/models/res_company.py
+++ b/models/res_company.py
@@ -13,3 +13,8 @@ class ScopCotisationCompany(models.Model):
         string='Journal des cotisations',
         domain="[('type', '=', 'sale')]"
     )
+    tag_id = fields.Many2one(
+        comodel_name='res.partner.category',
+        string='Etiquette de cotisation',
+        ondelete='restrict'
+    )
diff --git a/models/res_config_settings.py b/models/res_config_settings.py
index 8214d9c..ad517e9 100644
--- a/models/res_config_settings.py
+++ b/models/res_config_settings.py
@@ -18,6 +18,15 @@ class CotisationsConfigSettings(models.TransientModel):
         string='Journal des cotisations',
         domain="[('type', '=', 'sale')]")
 
+    tag_id = fields.Many2one(
+        comodel_name='res.partner.category',
+        related="company_id.tag_id",
+        readonly=False,
+        string='Etiquette de cotisation')
+
+    # ------------------------------------------------------
+    # Actions
+    # ------------------------------------------------------
     def add_company_to_menu(self, menu, bool_condition):
         """
         Add current company to the list of companies allowed to see menu
@@ -37,6 +46,9 @@ class CotisationsConfigSettings(models.TransientModel):
                     "company_ids": [(3, current_company_id.id)]
                 })
 
+    # ------------------------------------------------------
+    # Override Parent
+    # ------------------------------------------------------
     def execute(self):
         """
         Rewrite execute() function to add current company to the list
diff --git a/models/scop_cotisation.py b/models/scop_cotisation.py
index c601900..32a95d6 100644
--- a/models/scop_cotisation.py
+++ b/models/scop_cotisation.py
@@ -41,6 +41,17 @@ class ScopCotisation(models.AbstractModel):
         "Cotisations créées",
         compute='_compute_invoiced_member_count')
 
+    # Emails management
+    recipient_ids = fields.One2many(
+        comodel_name='res.partner',
+        compute='_compute_recipient'
+    )
+    # TODO : Use when email active
+    # email_count = fields.Integer(
+    #     string='Nombre de mails',
+    #     compute='_compute_emails'
+    # )
+
     # ------------------------------------------------------
     # Compute fields
     # ------------------------------------------------------
@@ -60,6 +71,18 @@ class ScopCotisation(models.AbstractModel):
             cotiz.invoiced_member_count = len(
                 cotiz.invoice_ids.mapped('partner_id'))
 
+    @api.multi
+    def _compute_recipient(self):
+        tag_id = self.env.user.company_id.tag_id
+        for cotiz in self:
+            child_ids = cotiz.partner_id.child_ids.filtered(
+                lambda child: (tag_id in child.category_id) and child.email
+            )
+            if cotiz.partner_id.email:
+                cotiz.recipient_ids = cotiz.partner_id + child_ids
+            else:
+                cotiz.recipient_ids = child_ids
+
     # ------------------------------------------------------
     # Global functions
     # ------------------------------------------------------
@@ -118,3 +141,8 @@ class ScopCotisation(models.AbstractModel):
             ('end', '>', date(self.year, 1, 1))
         ]).mapped('partner_id')
         return members
+
+    # TODO : Use when email active
+    # Email
+    # def get_recipients(self):
+    #     return ','.join(map(lambda x: str(x), self.recipient_ids.ids))
diff --git a/views/res_config_settings.xml b/views/res_config_settings.xml
index ec13cc9..c6c1c7b 100644
--- a/views/res_config_settings.xml
+++ b/views/res_config_settings.xml
@@ -15,20 +15,28 @@
                             <div class="o_setting_left_pane">
                                 <field name="is_contribution" />
                             </div>
-                             <div class="o_setting_right_pane">
+                            <div class="o_setting_right_pane">
                                 <label for="is_contribution"/>
                                 <div class="text-muted">
                                     Activer la gestion des cotisations pour cette société
                                 </div>
-                             </div>
-                             <div class="o_setting_left_pane"/>
-                             <div class="o_setting_right_pane">
+                            </div>
+                            <div class="o_setting_left_pane"/>
+                            <div class="o_setting_right_pane">
                                 <label for="contribution_journal_id"/>
                                 <div class="text-muted">
                                     Définit le journal des cotisations
                                 </div>
                                 <field name="contribution_journal_id" options="{'no_open': True, 'no_create': True}"/>
-                             </div>
+                            </div>
+                            <div class="o_setting_left_pane"/>
+                            <div class="o_setting_right_pane">
+                                <label for="tag_id"/>
+                                <div class="text-muted">
+                                    Etiquette associée aux contacts cotisations
+                                </div>
+                                <field name="tag_id" options="{'no_open': True, 'no_create': True}"/>
+                            </div>
                         </div>
                     </div>
                 </xpath>
-- 
GitLab