From b21ef91f5947b0965ca132b51606da50f249c374 Mon Sep 17 00:00:00 2001
From: Benjamin <benjamin@le-filament.com>
Date: Tue, 28 Apr 2020 17:24:44 +0200
Subject: [PATCH] [update] ajout champs + modifs programme + update maj
 sessions

---
 models/training_course.py        |  5 ++-
 models/training_session.py       | 10 ++++--
 models/training_student.py       | 22 +++++++++++--
 models/training_training.py      | 53 ++++++++++++++++++++++++--------
 report/report_program.xml        | 20 ++++++------
 views/training_course_view.xml   | 14 ++++-----
 views/training_session_view.xml  | 12 +++++---
 views/training_training_view.xml | 28 +++++++++--------
 8 files changed, 111 insertions(+), 53 deletions(-)

diff --git a/models/training_course.py b/models/training_course.py
index 3661d45..6b3b267 100644
--- a/models/training_course.py
+++ b/models/training_course.py
@@ -32,11 +32,14 @@ class TrainingProgram(models.Model):
         default=0,
         store=True)
     session_count = fields.Integer(
-        string="Nbre Sessions",
+        string="Nb Sessions",
         compute='_compute_sessions',
         default=0,
         store=True)
 
+    # ------------------------------------------------------
+    # Compute
+    # ------------------------------------------------------
     @api.depends('session_ids')
     def _compute_sessions(self):
         self.session_count = len(self.session_ids)
diff --git a/models/training_session.py b/models/training_session.py
index 5ebbc5a..8b91594 100644
--- a/models/training_session.py
+++ b/models/training_session.py
@@ -14,11 +14,17 @@ class TrainingSession(models.Model):
     session_id = fields.Many2one(
         comodel_name='training.course.session',
         string='Session',
-        ondelete='cascade',
         required=True)
     training_id = fields.Many2one(
         comodel_name='training.training',
-        string='Formation')
+        string='Formation',
+        ondelete='cascade',
+        required=True)
+    customer_id = fields.Many2one(
+        comodel_name='res.partner',
+        string='Client',
+        related='training_id.customer_id',
+        store=True)
     date = fields.Datetime('Date',)
     user_id = fields.Many2one(
         comodel_name='res.users',
diff --git a/models/training_student.py b/models/training_student.py
index 00207a5..20314b0 100644
--- a/models/training_student.py
+++ b/models/training_student.py
@@ -2,7 +2,7 @@
 # © 2019 Le Filament (<http://www.le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
-from odoo import models, fields
+from odoo import models, fields, api
 
 
 class TrainingStudent(models.Model):
@@ -17,9 +17,25 @@ class TrainingStudent(models.Model):
     parent_id = fields.Many2one(
         comodel_name='res.partner',
         string='Entreprise',
-        readonly=True,
-        related='student_id.parent_id')
+        domain=[('is_company', '=', True)])
     training_id = fields.Many2one(
         comodel_name='training.training',
         string='Formation')
     certificate = fields.Binary(string="Attestation")
+
+    _sql_constraints = [ (
+        'course_unique','UNIQUE(student_id, training_id)',
+        "Un stagiaire ne peut être inscrit qu'une fois") ]
+
+    # ------------------------------------------------------
+    # Onchange
+    # ------------------------------------------------------
+    @api.onchange('parent_id')
+    def _onchange_student_id(self):
+        vals = {}
+        vals['domain'] = {
+            'student_id': [
+                ('is_company', '=', False),
+                ('parent_id', '=', self.parent_id.id)],
+        }
+        return vals
diff --git a/models/training_training.py b/models/training_training.py
index 2223b24..1377059 100644
--- a/models/training_training.py
+++ b/models/training_training.py
@@ -46,6 +46,8 @@ class Training(models.Model):
     payment = fields.Boolean(string="Subrogation de paiement")
     date_begin = fields.Date(string='Début de la formation')
     date_end = fields.Date(string='Fin de la formation')
+    session_hours = fields.Char(string='Horaires')
+    students_nb_prev = fields.Char(string="Nb Stagiaires Prévisionnel")
     convention = fields.Binary(string="Convention")
     date_convention = fields.Date(string="Date Convention")
     date_attestation = fields.Date(string="Date Attestation")
@@ -105,6 +107,42 @@ class Training(models.Model):
     file_number = fields.Char(string='N° Dossier')
     plan = fields.Char(string='Dispositif')
 
+    # ------------------------------------------------------
+    # Override ORM
+    # ------------------------------------------------------
+    @api.model
+    def create(self, vals):
+        record = super(Training, self).create(vals)
+        if vals.get('course_id', False):
+            for s in record.course_id.session_ids:
+                self.env['training.session'].create({
+                    'session_id': s.id,
+                    'training_id': record.id,
+                    'date_delay': s.duration,
+                    'sequence': s.sequence,
+                })
+        return record
+
+    @api.multi
+    def write(self, vals):
+        record = super(Training, self).write(vals)
+        print(self)
+        print(record)
+        print(vals)
+        if vals.get('course_id', False):
+            self.session_ids.unlink()
+            for s in self.course_id.session_ids:
+                self.env['training.session'].create({
+                    'session_id': s.id,
+                    'training_id': self.id,
+                    'date_delay': s.duration,
+                    'sequence': s.sequence,
+                })
+        return record
+
+    # ------------------------------------------------------
+    # compute
+    # ------------------------------------------------------
     @api.depends('student_ids')
     def _compute_students_count(self):
         self.students_count = len(self.student_ids)
@@ -133,18 +171,9 @@ class Training(models.Model):
     def _compute_invoiced(self):
         self.invoiced = sum(self.invoice_ids.mapped('amount_untaxed_signed'))
 
-    @api.onchange('course_id')
-    def _onchange_session_ids(self):
-        # Delete all training session
-        self.session_ids.unlink()
-        # Populate sessions from course
-        for session in self.course_id.session_ids:
-            self.env['training.session'].create({
-                'session_id': session.id,
-                'training_id': self._origin.id,
-                'date_delay': session.duration,
-            })
-
+    # ------------------------------------------------------
+    # Buttons
+    # ------------------------------------------------------
     @api.multi
     def action_valid(self):
         for training in self:
diff --git a/report/report_program.xml b/report/report_program.xml
index e544de9..598fbb3 100644
--- a/report/report_program.xml
+++ b/report/report_program.xml
@@ -30,7 +30,7 @@
                                             </div>
                                         </t>
                                     </div>
-                                    <div class="subblock">                                  
+                                    <div class="subblock">
                                         <h3>4. MÉTHODOLOGIE ET SUPPORTS PÉDAGOGIQUES</h3>
                                         <span t-field="doc.course_id.method" />
                                     </div>
@@ -46,21 +46,21 @@
                                         <h3>7. MODALITÉS D'ÉVALUATION</h3>
                                         <span t-field="doc.course_id.evaluation" />
                                     </div>
-                                    <div class="subblock">                              
+                                    <div class="subblock">
                                         <h3>8. MODALITÉS ET DURÉE DE LA FORMATION</h3>
-                                        <p>Formation dispensée en intra-entreprise par groupes de <span t-field="doc.students_count" /> personnes, et par session de demi-journées (4h), afin de s’adapter aux contraintes de disponibilité de l’entreprise.</p>
-                                        <p>Horaires : <strong>9h-13h ou 13h-17h</strong></p>
+                                        <p>Formation dispensée en intra-entreprise par groupes de <span t-field="doc.students_nb_prev" /> personnes, et par session de demi-journées, afin de s’adapter aux contraintes de disponibilité de l’entreprise.</p>
+                                        <p>Horaires : <strong><span t-field="doc.session_hours" /></strong></p>
                                         <p><strong><span t-field="doc.session_count" /> sessions</strong> de formation, soit <strong><span t-field="doc.hours" /> heures par personne</strong></p>
                                     </div>
-                                    <div class="subblock">   
+                                    <div class="subblock">
                                         <h3>9. LIEU ET DÉLAIS DE LA FORMATION</h3>
                                         <p t-field="doc.place" />
                                         <p>Du <span t-field="doc.date_begin" /> au <span t-field="doc.date_end" /></p>
                                     </div>
-                                    <div class="subblock">  
+                                    <div class="subblock">
                                         <h3>10. COÛT DE LA FORMATION</h3>
                                         <p>
-                                            <span t-field="doc.cost" /> € HT en formation INTRA de <span t-field="doc.students_count" /> personnes soit <span t-esc="round(doc.cost / doc.students_count, 2)" /> € par personne.
+                                            <span t-field="doc.cost" /> € HT en formation INTRA de <span t-field="doc.students_nb_prev" /> personnes.
                                         </p>
                                     </div>
                                     <div class="subblock">
@@ -69,9 +69,9 @@
                                     </div>
                                     <div class="text-center">
                                         <img src="/lefilament_training/static/src/img/logo-datadock.jpg" style="max-width: 80px;" class="mt64" />
-                                    </div>   
+                                    </div>
                                 </div>
-                            </div> 
+                            </div>
                       	</div>
                     </t>
                 </t>
@@ -88,4 +88,4 @@
 	    />
 
     </data>
-</odoo>
\ No newline at end of file
+</odoo>
diff --git a/views/training_course_view.xml b/views/training_course_view.xml
index 7f83162..ddb4f74 100644
--- a/views/training_course_view.xml
+++ b/views/training_course_view.xml
@@ -6,7 +6,7 @@
             <field name="name">Training Course Form View</field>
             <field name="model">training.course</field>
             <field name="arch" type="xml">
-            <form string="Fomration">
+            <form string="Formation">
                 <sheet>
                     <group>
                         <field name="name" />
@@ -40,8 +40,8 @@
                 <div class="oe_chatter">
                     <field name="message_follower_ids" widget="mail_followers" />
                     <field name="message_ids" widget="mail_thread" />
-                </div> 
-            </form> 
+                </div>
+            </form>
         </field>
         </record>
 
@@ -55,7 +55,7 @@
                 <field name="name" />
                 <field name="duration_total" />
                 <field name="session_count" />
-            </tree> 
+            </tree>
         </field>
         </record>
 
@@ -88,7 +88,7 @@
                         <field name="course_id" widget="many2many_tags"/>
                     </group>
                 </sheet>
-            </form> 
+            </form>
         </field>
         </record>
 
@@ -102,14 +102,14 @@
                   <field name="name" />
                   <field name="duration" />
                   <field name="course_id" />
-              </tree> 
+              </tree>
             </field>
         </record>
 
         <!-- ACTIONS -->
         <act_window id="action_training_course" name="Catalogue de Formations" res_model="training.course" view_mode="tree,form,pivot,graph" />
         <act_window id="action_training_course_session" name="Sessions de Formations" res_model="training.course.session" view_mode="tree,form,pivot,graph" />
-        
+
         <!-- MENU --> 
         <menuitem
           id="menu_training"
diff --git a/views/training_session_view.xml b/views/training_session_view.xml
index 96aa1f1..f2a7d9c 100644
--- a/views/training_session_view.xml
+++ b/views/training_session_view.xml
@@ -13,10 +13,11 @@
 						<field name="date" />
 						<field name="date_delay" />
 						<field name="training_id" />
+						<field name="customer_id" />
 						<field name="user_id" />
-					</group>	
+					</group>
 				</sheet>
-			</form>	
+			</form>
 		</field>
 		</record>
 
@@ -30,8 +31,9 @@
 				<field name="date" />
 				<field name="session_id" />
 				<field name="training_id" />
+				<field name="customer_id" />
 				<field name="user_id" />
-			</tree>	
+			</tree>
 		</field>
 		</record>
 
@@ -44,10 +46,10 @@
 						<!-- Champs de recherche -->
 				     	<field name="session_id"/>
 				     	<field name="user_id"/>
+				     	<field name="training_id"/>
 				     	<field name="training_id"/>
 						<!-- Filtres -->
 
-				    	
 				    	<!-- Groupes -->
 				    	<group expand="0" name="group_by" string="Group By">
 	                       <filter string="Formateur" name="group_user" domain="[]" context="{'group_by' : 'user_id'}" />
@@ -57,7 +59,7 @@
 				    </search>
 			  </field>
 		</record>
-		
+
 		<act_window id="action_training_session" name="Sessions" res_model="training.session" view_mode="tree,form,calendar,pivot,graph" />
 		<!-- MENU --> 
 	    <menuitem id="menu_training_session" name="Sessions" sequence="20" parent="menu_training_training_parent" action="action_training_session" />
diff --git a/views/training_training_view.xml b/views/training_training_view.xml
index 79a312c..a5db293 100644
--- a/views/training_training_view.xml
+++ b/views/training_training_view.xml
@@ -16,10 +16,12 @@
 				<sheet>
 					<group>
 						<group string="Formation">
-							<field name="customer_id" />
-							<field name="course_id" />
-							<field name="date_begin" widget="date" />
-							<field name="date_end" widget="date" />
+							<field name="customer_id" required="1"/>
+							<field name="course_id" required="1"/>
+							<field name="date_begin" widget="date" required="1"/>
+							<field name="date_end" widget="date" required="1"/>
+							<field name="students_nb_prev" required="1" placeholder="ex : 4, 4 à 6"/>
+							<field name="session_hours" required="1" placeholder="ex : 9h-12h ou 14h-17h"/>
 						</group>
 						<group string="OPCO">
 							<field name="opco_id" />
@@ -28,10 +30,10 @@
 							<field name="file_number" />
 							<field name="plan" />
 						</group>
-					</group>	
+					</group>
 					<group string="Infos Financières">
 						<group>
-							<field name="cost" />
+							<field name="cost" required="1"/>
 							<field name="rate" />
 							<field name="amount" />
 							<field name="invoiced" />
@@ -44,13 +46,13 @@
 						</group>
 					</group>
 					<group>
-						<field name="place" />
+						<field name="place" required="1"/>
 					</group>
 					<notebook>
 						<page name="students" string="Stagiaires">
-							<field name="student_ids" >
+							<field name="student_ids" context="{'default_parent_id': customer_id, 'default_training_id': active_id}">
 								<tree>
-									<field name="student_id" />
+									<field name="student_id" domain="[('parent_id', '=', parent_id), ]"/>
 									<field name="parent_id" />
 									<field name="certificate" />
 								</tree>
@@ -90,8 +92,8 @@
 				<div class="oe_chatter">
 					<field name="message_follower_ids" widget="mail_followers" />
 					<field name="message_ids" widget="mail_thread" />
-				</div> 
-			</form>	
+				</div>
+			</form>
 		</field>
 		</record>
 
@@ -112,7 +114,7 @@
 				<field name="invoiced" />
 				<field name="payment" />
 				<field name="state" />
-			</tree>	
+			</tree>
 		</field>
 		</record>
 
@@ -145,7 +147,7 @@
 				    </search>
 			  </field>
 		</record>
-		
+
 		<!-- ACTIONS -->
 		<act_window id="action_training_training" name="Formations" res_model="training.training" view_mode="tree,form,pivot,graph" />
 		<act_window id="action_training_session" name="Sessions" res_model="training.session" view_mode="tree,form,calendar,pivot,graph" />
-- 
GitLab