From 64a1bae946a652058fcb4b236adc482ba112f6b2 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Thu, 11 Apr 2024 19:09:34 +0200
Subject: [PATCH] [IMP] add survey type, move training survey to training menu

---
 __manifest__.py            |   2 +
 models/__init__.py         |   1 +
 models/survey.py           |  66 ++++++++++++++++++
 models/training.py         |  23 +++++--
 models/training_program.py |  38 ++++++++--
 views/menus.xml            |  12 ++++
 views/survey.xml           | 138 +++++++++++++++++++++++++++++++++++++
 views/training.xml         |   2 +-
 views/training_program.xml |  26 ++++---
 9 files changed, 285 insertions(+), 23 deletions(-)
 create mode 100644 models/survey.py
 create mode 100644 views/menus.xml
 create mode 100644 views/survey.xml

diff --git a/__manifest__.py b/__manifest__.py
index 96ae557..de6967d 100644
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -15,9 +15,11 @@
         # datas
         # templates
         # views
+        "views/survey.xml",
         "views/training.xml",
         "views/training_program.xml",
         # menus
+        "views/menus.xml",
     ],
     "assets": {
         "web.report_assets_common": [],
diff --git a/models/__init__.py b/models/__init__.py
index a9d1403..972ef02 100644
--- a/models/__init__.py
+++ b/models/__init__.py
@@ -1,2 +1,3 @@
+from . import survey
 from . import training
 from . import training_program
diff --git a/models/survey.py b/models/survey.py
new file mode 100644
index 0000000..be2104d
--- /dev/null
+++ b/models/survey.py
@@ -0,0 +1,66 @@
+# Copyright 2019-2022 Le Filament (<https://le-filament.com>)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import fields, models
+
+
+class Survey(models.Model):
+    _inherit = "survey.survey"
+
+    survey_type = fields.Selection(
+        selection=[
+            ("training", "formation"),
+        ],
+        string="Type de questionnaire",
+    )
+    training_survey_type = fields.Selection(
+        selection=[
+            ("subscribe", "Inscription / Positionnement"),
+            ("aeci", "AECI"),
+            ("aect", "AECT"),
+            ("satisfaction", "Satisfaction"),
+        ],
+        string="Type de questionnaire",
+    )
+    training_ids = fields.One2many(
+        comodel_name="training.training",
+        compute="_compute_training_ids",
+    )
+    program_ids = fields.One2many(
+        comodel_name="training.program", compute="_compute_program_ids"
+    )
+
+    # ------------------------------------------------------
+    # Override ORM
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Compute
+    # ------------------------------------------------------
+    def _compute_training_ids(self):
+        for survey in self:
+            if survey.survey_type == "training":
+                survey.training_ids = self.env["training.training"].search(
+                    [("satisfaction_survey_id", "=", survey.id)]
+                )
+            else:
+                survey.training_ids = False
+
+    def _compute_program_ids(self):
+        for survey in self:
+            if survey.survey_type == "training":
+                survey.program_ids = self.env["training.program"].search(
+                    [
+                        "|",
+                        "|",
+                        ("registration_survey_id", "=", survey.id),
+                        ("aeci_survey_id", "=", survey.id),
+                        ("aect_survey_id", "=", survey.id),
+                    ]
+                )
+            else:
+                survey.program_ids = False
+
+    # ------------------------------------------------------
+    # Buttons
+    # ------------------------------------------------------
diff --git a/models/training.py b/models/training.py
index 4b0a261..8753ca8 100644
--- a/models/training.py
+++ b/models/training.py
@@ -1,14 +1,21 @@
-# Copyright 2019-2022 Le Filament (<https://le-filament.com>)
+# Copyright 2024 Le Filament (<https://le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
-from odoo import fields, models
+from odoo import _, fields, models
+from odoo.exceptions import UserError
 
 
 class Training(models.Model):
     _inherit = "training.training"
 
     satisfaction_survey_id = fields.Many2one(
-        comodel_name="survey.survey", string="Satisfaction", ondelete="restrict"
+        comodel_name="survey.survey",
+        string="Satisfaction",
+        domain=[
+            ("survey_type", "=", "survey_type"),
+            ("training_survey_type", "=", "satisfaction"),
+        ],
+        ondelete="restrict",
     )
 
     # ------------------------------------------------------
@@ -16,9 +23,17 @@ class Training(models.Model):
     # ------------------------------------------------------
 
     # ------------------------------------------------------
-    # compute
+    # Compute
     # ------------------------------------------------------
 
+    # ------------------------------------------------------
+    # Inherit parent
+    # ------------------------------------------------------
+    def action_valid(self):
+        if not self.satisfaction_survey_id:
+            raise UserError(_("Le questionnaire de satisfaction doit être renseigné."))
+        super().action_valid()
+
     # ------------------------------------------------------
     # Buttons
     # ------------------------------------------------------
diff --git a/models/training_program.py b/models/training_program.py
index c60cce8..9fe49e4 100644
--- a/models/training_program.py
+++ b/models/training_program.py
@@ -1,7 +1,8 @@
-# Copyright 2019-2022 Le Filament (<https://le-filament.com>)
+# Copyright 2024 Le Filament (<https://le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
-from odoo import fields, models
+from odoo import _, fields, models
+from odoo.exceptions import UserError
 
 
 class TrainingProgram(models.Model):
@@ -10,15 +11,44 @@ class TrainingProgram(models.Model):
     registration_survey_id = fields.Many2one(
         comodel_name="survey.survey",
         string="Inscription/Positionnement",
+        domain=[
+            ("survey_type", "=", "training"),
+            ("training_survey_type", "=", "subscribe"),
+        ],
         ondelete="restrict",
     )
     aeci_survey_id = fields.Many2one(
-        comodel_name="survey.survey", string="AECI", ondelete="restrict"
+        comodel_name="survey.survey",
+        string="AECI",
+        domain=[
+            ("survey_type", "=", "training"),
+            ("training_survey_type", "=", "aeci"),
+        ],
+        ondelete="restrict",
     )
     aect_survey_id = fields.Many2one(
-        comodel_name="survey.survey", string="AECT", ondelete="restrict"
+        comodel_name="survey.survey",
+        string="AECT",
+        domain=[
+            ("survey_type", "=", "training"),
+            ("training_survey_type", "=", "aect"),
+        ],
+        ondelete="restrict",
     )
 
     # ------------------------------------------------------
     # Compute
     # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Inherit parent
+    # ------------------------------------------------------
+    def action_valid(self):
+        if not self.registration_survey_id or not self.aeci_survey_id:
+            raise UserError(
+                _(
+                    "Les questionnaires d'inscription/positionnement et AECI "
+                    "doivent être renseignés."
+                )
+            )
+        super(TrainingProgram, self).action_valid()
diff --git a/views/menus.xml b/views/menus.xml
new file mode 100644
index 0000000..9ec86be
--- /dev/null
+++ b/views/menus.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<odoo>
+    <!-- MENU -->
+    <menuitem
+        id="menu_training_survey"
+        name="Questionnaires"
+        sequence="30"
+        parent="training.menu_training"
+        action="action_training_survey"
+    />
+
+</odoo>
diff --git a/views/survey.xml b/views/survey.xml
new file mode 100644
index 0000000..8689d4f
--- /dev/null
+++ b/views/survey.xml
@@ -0,0 +1,138 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!-- Copyright 2024 Le Filament (<https://le-filament.com>)
+     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
+<odoo>
+    <!-- Form -->
+    <record id="training_survey_form" model="ir.ui.view">
+        <field name="name">training.survey.form</field>
+        <field name="model">survey.survey</field>
+        <field name="inherit_id" ref="survey.survey_survey_view_form" />
+        <field name="arch" type="xml">
+            <xpath expr="//field[@name='user_id']" position="before">
+                <field name="survey_type" invisible="1" />
+                <field
+                    name="training_survey_type"
+                    attrs="{'invisible': [('survey_type', '!=', 'training')], 'required': [('survey_type', '=', 'training')]}"
+                />
+            </xpath>
+            <xpath expr="//notebook" position="inside">
+                <page
+                    name="trainings"
+                    string="Formations"
+                    attrs="{'invisible': ['|', ('survey_type', '!=', 'training'), ('training_survey_type', '!=', 'satisfaction'), ]}"
+                >
+                    <field name="training_ids" />
+                </page>
+                <page
+                    name="programs"
+                    strong="Programmes"
+                    attrs="{'invisible': ['|', ('survey_type', '!=', 'training'), ('training_survey_type', 'not in', ['aeci', 'aect', 'subscribe']), ]}"
+                >
+                    <field name="program_ids" />
+                </page>
+            </xpath>
+        </field>
+    </record>
+
+    <!-- Tree -->
+    <record id="training_survey_tree" model="ir.ui.view">
+        <field name="name">training.survey.tree</field>
+        <field name="model">survey.survey</field>
+        <field name="inherit_id" ref="survey.survey_survey_view_tree" />
+        <field name="arch" type="xml">
+            <xpath expr="//field[@name='user_id']" position="after">
+                <field name="survey_type" invisible="1" />
+                <field
+                    name="training_survey_type"
+                    readonly="1"
+                    optional="show"
+                    attrs="{'column_invisible': [('survey_type', '!=', 'training')]}"
+                />
+            </xpath>
+        </field>
+    </record>
+
+    <!-- Kanban -->
+    <record id="training_survey_kanban" model="ir.ui.view">
+        <field name="name">training.survey.kanban</field>
+        <field name="model">survey.survey</field>
+        <field name="inherit_id" ref="survey.survey_survey_view_kanban" />
+        <field name="arch" type="xml">
+            <xpath expr="//field[@name='success_ratio']" position="after">
+                <field name="survey_type" />
+                <field name="training_survey_type" />
+            </xpath>
+            <xpath
+                expr="//div[hasclass('o_survey_kanban_card_ungrouped')]/div/div"
+                position="inside"
+            >
+                <div
+                    class="text-center mt-3"
+                    t-if="record.survey_type.raw_value == 'training'"
+                >
+                    <field name="training_survey_type" widget="badge" />
+                </div>
+            </xpath>
+        </field>
+    </record>
+
+    <!-- Search -->
+    <record id="training_survey_search" model="ir.ui.view">
+        <field name="name">training.survey.search</field>
+        <field name="model">survey.survey</field>
+        <field name="mode">primary</field>
+        <field name="inherit_id" ref="survey.survey_survey_view_search" />
+        <field name="arch" type="xml">
+            <xpath expr="//filter[@name='inactive']" position="before">
+                <field name="training_survey_type" />
+            </xpath>
+            <xpath expr="//filter[@name='inactive']" position="before">
+                <filter
+                    string="Inscription/Positionnement"
+                    name="subscribe"
+                    domain="[('training_survey_type', '=', 'subscribe')]"
+                />
+                <filter
+                    string="AECI"
+                    name="aeci"
+                    domain="[('training_survey_type', '=', 'aeci')]"
+                />
+                <filter
+                    string="AECT"
+                    name="aect"
+                    domain="[('training_survey_type', '=', 'aect')]"
+                />
+                <filter
+                    string="Satisfaction"
+                    name="satisfaction"
+                    domain="[('training_survey_type', '=', 'satisfaction')]"
+                />
+                <separator />
+            </xpath>
+            <xpath expr="//filter[@name='group_by_responsible']" position="before">
+                <filter
+                    string="Type de questionnaire"
+                    name="group_by_training_survey_type"
+                    context="{'group_by': 'training_survey_type'}"
+                />
+            </xpath>
+        </field>
+    </record>
+
+
+    <!-- Actions -->
+    <!-- Inherit parent action for domain -->
+    <record model="ir.actions.act_window" id="survey.action_survey_form">
+        <field name="domain">[("survey_type", "=", False)]</field>
+    </record>
+    <!-- New Actions -->
+    <record model="ir.actions.act_window" id="action_training_survey">
+        <field name="name">Questionnaires formation</field>
+        <field name="res_model">survey.survey</field>
+        <field name="domain">[("survey_type", "=", "training")]</field>
+        <field name="view_mode">kanban,tree,form,activity</field>
+        <field name="context">{"default_survey_type": "training"}</field>
+        <field name="search_view_id" ref="training_survey_search" />
+    </record>
+
+</odoo>
diff --git a/views/training.xml b/views/training.xml
index 5e8ed8c..0a4ca85 100644
--- a/views/training.xml
+++ b/views/training.xml
@@ -13,7 +13,7 @@
                         <group>
                             <field
                                 name="satisfaction_survey_id"
-                                option="{'no_create': 1, 'no_edit': 1}"
+                                options="{'no_create': 1, 'no_edit': 1}"
                             />
                         </group>
                     </group>
diff --git a/views/training_program.xml b/views/training_program.xml
index 5d428b8..b4d0bfd 100644
--- a/views/training_program.xml
+++ b/views/training_program.xml
@@ -11,20 +11,18 @@
             <xpath expr="//notebook" position="inside">
                 <page string="Questionnaires" name="survey">
                     <group>
-                        <group>
-                            <field
-                                name="registration_survey_id"
-                                option="{'no_create': 1, 'no_edit': 1}"
-                            />
-                            <field
-                                name="aect_survey_id"
-                                option="{'no_create': 1, 'no_edit': 1}"
-                            />
-                            <field
-                                name="aeci_survey_id"
-                                option="{'no_create': 1, 'no_edit': 1}"
-                            />
-                        </group>
+                        <field
+                            name="registration_survey_id"
+                            options="{'no_create': 1, 'no_edit': 1}"
+                        />
+                        <field
+                            name="aect_survey_id"
+                            options="{'no_create': 1, 'no_edit': 1}"
+                        />
+                        <field
+                            name="aeci_survey_id"
+                            options="{'no_create': 1, 'no_edit': 1}"
+                        />
                     </group>
                 </page>
             </xpath>
-- 
GitLab