From 2e7fd8437c97c18043786f413135eb1027943dec Mon Sep 17 00:00:00 2001
From: "CGSCOP\\hsilvant" <hsilvant@scop.coop>
Date: Wed, 12 Mar 2025 16:02:30 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20notion=20de=20temps=20estim?=
 =?UTF-8?q?=C3=A9=20vs=20temps=20r=C3=A9alis=C3=A9=20sur=20les=20projet=20?=
 =?UTF-8?q?d'accompagnement=20+=20d=C3=A9tails=20temps=20factur=C3=A9=20sp?=
 =?UTF-8?q?=C3=A9cifique=20AURA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 models/res_partner.py |  54 ++++++++++++++++
 views/res_partner.xml | 141 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+)

diff --git a/models/res_partner.py b/models/res_partner.py
index 4fc3f33..4688e15 100644
--- a/models/res_partner.py
+++ b/models/res_partner.py
@@ -66,6 +66,27 @@ class ScopPartner(models.Model):
         comodel_name="scop.liasse.fiscale", string="Liasse fiscale adhésion"
     )
 
+    # Temps estimé/Réalisé
+    estimated_time = fields.Float("Durée estimée")
+    invoiced_creation = fields.Float("Durée facturée (création)")
+    invoiced_followup = fields.Float("Durée facturée (suivi)")
+    realized_time = fields.Float(
+        string="Durée réelle",
+        compute="_compute_real_time",
+    )
+    realized_creation = fields.Float(
+        string="Durée réelle (création)",
+        compute="_compute_real_time",
+    )
+    realized_followup = fields.Float(
+        string="Durée réelle (suivi)",
+        compute="_compute_real_time",
+    )
+    is_estimated_time_ok = fields.Boolean(
+        string="Estimation ok",
+        compute="_compute_real_time",
+    )
+
     # Changement de statuts
     list_logs = fields.Text("Liste des erreurs")
 
@@ -78,6 +99,39 @@ class ScopPartner(models.Model):
             if partner.date_first_rdv:
                 partner.year_project = str(partner.date_first_rdv.year)
 
+    @api.depends("estimated_time")
+    def _compute_real_time(self):
+        """
+        Champs calculés temps réels
+        """
+        for rec in self:
+            # Liste des lignes de temps pour ce partner
+            ldts = self.env["account.analytic.line"].sudo().search(
+            [
+                ("partner_id", "=", rec.id),
+            ]
+            )
+            rec.realized_time = 0
+            rec.realized_creation = 0
+            rec.realized_followup = 0
+            for ldt in ldts:
+                # On totalise les heures de développement
+                if (ldt.project_id.cgscop_timesheet_code_id != False and
+                    ldt.project_id.cgscop_timesheet_code_id.domain == 'D'):
+                    rec.realized_time = rec.realized_time + ldt.unit_amount
+                # On totalise les heures facturés
+                if ldt.project_id.creation_invoiced == True:
+                    if (ldt.project_id.cgscop_timesheet_code_id != False and
+                        ldt.project_id.cgscop_timesheet_code_id.domain == 'A'):
+                        rec.realized_followup = rec.realized_followup + ldt.unit_amount
+                    if (ldt.project_id.cgscop_timesheet_code_id != False and
+                        ldt.project_id.cgscop_timesheet_code_id.domain == 'D'):
+                        rec.realized_creation = rec.realized_creation + ldt.unit_amount
+
+            # On ajoute les heures de suivi facturées au titre du dev
+            rec.realized_time = rec.realized_time + rec.realized_followup
+            rec.is_estimated_time_ok = rec.realized_time <= rec.estimated_time
+
     # ------------------------------------------------------
     # Onchange function
     # ------------------------------------------------------
diff --git a/views/res_partner.xml b/views/res_partner.xml
index beefbe8..d6a1a87 100644
--- a/views/res_partner.xml
+++ b/views/res_partner.xml
@@ -28,6 +28,38 @@
                             readonly="1"
                         />
                     </div>
+                    <field name="estimated_time" invisible="1"/>
+                    <field name="is_estimated_time_ok" invisible="1"/>
+                    <field name="project_status" invisible="1"/>
+                    <div
+                        class="alert alert-danger"
+                        role="alert"
+                        attrs="{'invisible':['|', '|',
+                        ('estimated_time','&lt;=',0),
+                        ('is_estimated_time_ok','!=',False),
+                        ('project_status','in',('1_information','2_pre-diagnostic'))]}"
+                    >
+                        Heures passées :
+                        <field
+                            name="realized_time"
+                            readonly="1"
+                        />
+                    </div>
+                    <div
+                        class="alert alert-success"
+                        role="alert"
+                        attrs="{'invisible':[ '|','|',
+                        ('estimated_time','&lt;=',0),
+                        ('is_estimated_time_ok','=',False),
+                        ('project_status','in',('1_information','2_pre-diagnostic'))]}"
+                    >
+                        Heures passées :
+                        <field
+                            name="realized_time"
+                            readonly="1"
+                        />
+                    </div>
+
                 </xpath>
             </field>
         </record>
@@ -215,6 +247,57 @@
                                 <field name="date_convention" />
                                 <field name="amount_convention" widget="monetary" />
                             </group>
+                            <group
+                                string="Estimation des temps"
+                                attrs="{'invisible':[('project_status','in',('1_information','2_pre-diagnostic'))]}"
+
+                            >
+                                <table class="table table-bordered">
+                                    <thead>
+                                        <tr>
+                                            <th />
+                                            <th>(heures) </th>
+                                            <th>Réel</th>
+                                        </tr>
+                                    </thead>
+                                    <tbody>
+                                        <tr>
+                                            <th>
+                                                <label for="estimated_time"/>
+                                            </th>
+                                            <td>
+                                                <field name="estimated_time" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_time" />
+                                            </td>
+                                        </tr>
+                                        <tr attrs="{'invisible':[('ur_id','!=',%(cgscop_partner.riga_14353)d)]}">
+                                            <th>
+                                                <label for="invoiced_creation"/>
+                                            </th>
+                                            <td>
+                                                <field name="invoiced_creation" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_creation" />
+                                            </td>
+                                        </tr>
+                                        <tr attrs="{'invisible':[('ur_id','!=',%(cgscop_partner.riga_14353)d)]}">
+                                            <th>
+                                                <label for="invoiced_followup"/>
+                                            </th>
+                                            <td>
+                                                <field name="invoiced_followup" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_followup" />
+                                            </td>
+                                        </tr>
+
+                                    </tbody>
+                                </table>
+                            </group>
                         </group>
                     </page>
 
@@ -329,6 +412,58 @@
                                 <field name="date_convention" />
                                 <field name="amount_convention" widget="monetary" />
                             </group>
+                            <group
+                                string="Estimation des temps"
+                                attrs="{'invisible':[('project_status','in',('1_information','2_pre-diagnostic'))]}"
+
+                            >
+                                <table class="table table-bordered">
+                                    <thead>
+                                        <tr>
+                                            <th />
+                                            <th>(heures) </th>
+                                            <th>Réel</th>
+                                        </tr>
+                                    </thead>
+                                    <tbody>
+                                        <tr>
+                                            <th>
+                                                <label for="estimated_time"/>
+                                            </th>
+                                            <td>
+                                                <field name="estimated_time" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_time" />
+                                            </td>
+                                        </tr>
+                                        <tr attrs="{'invisible':[('ur_id','!=',%(cgscop_partner.riga_14353)d)]}">
+                                            <th>
+                                                <label for="invoiced_creation"/>
+                                            </th>
+                                            <td>
+                                                <field name="invoiced_creation" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_creation" />
+                                            </td>
+                                        </tr>
+                                        <tr attrs="{'invisible':[('ur_id','!=',%(cgscop_partner.riga_14353)d)]}">
+                                            <th>
+                                                <label for="invoiced_followup"/>
+                                            </th>
+                                            <td>
+                                                <field name="invoiced_followup" />
+                                            </td>
+                                            <td>
+                                                <field name="realized_followup" />
+                                            </td>
+                                        </tr>
+
+                                    </tbody>
+                                </table>
+                            </group>
+
                         </group>
                     </page>
                 </page>
@@ -370,6 +505,12 @@
                     <field name="date_prediag" optional="hide" />
                     <field name="date_convention" optional="hide" />
                     <field name="date_transmission_cg" optional="hide" />
+                    <field name="estimated_time" optional="hide" />
+                    <field name="realized_time" optional="hide" />
+                    <field name="invoiced_creation" optional="hide" />
+                    <field name="realized_creation" optional="hide" />
+                    <field name="invoiced_followup" optional="hide" />
+                    <field name="realized_followup" optional="hide" />
                 </field>
             </field>
         </record>
-- 
GitLab