From dd3babac3f1ff1470ec15d087a6098ed7ffb5573 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Mon, 15 Jul 2024 14:37:20 +0200
Subject: [PATCH] [FIX] wrong values for employee query

---
 models/lefilament_tdb.py | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/models/lefilament_tdb.py b/models/lefilament_tdb.py
index 3a94c3b..e6f6a8e 100644
--- a/models/lefilament_tdb.py
+++ b/models/lefilament_tdb.py
@@ -444,11 +444,11 @@ class LeFilamentTdb(models.Model):
         return result
 
     def _employee_time(self, date_start=None, date_end=None):
-        clause = "where 1=1 "
+        clause = ""
         if date_start:
-            clause += f"and line_date >= '{date_start}' "
+            clause += f" and aal.date >= '{date_start}'"
         if date_end:
-            clause += f"and line_date <= '{date_end}' "
+            clause += f" and aal.date <= '{date_end}'"
 
         query = """
         select
@@ -456,14 +456,13 @@ class LeFilamentTdb(models.Model):
             sum(production) as "Prod",
             sum(internal) as "Interne",
             sum(revenue) as "CA"
-            from
-            (
+
+        from (
             select
                 p.name as employee,
                 0 as production,
                 0 as internal,
-                aal.amount as revenue,
-                aal.date as line_date
+                sum(aal.amount) as revenue
             from
                 account_analytic_line aal
             left join
@@ -474,15 +473,16 @@ class LeFilamentTdb(models.Model):
                 res_partner p on aa.partner_id = p.id
             where
                 aap.id = 3
+                %s
+            group by p.name
 
             union
 
             select
                 e.name as employee,
-                case when aal.partner_id != 1 or aal.partner_id is null and aal.holiday_id is null then unit_amount else 0 end as production,
-                case when aal.partner_id = 1 and aal.holiday_id is null then unit_amount else 0 end as internal,
-                0 as revenue,
-                aal.date as line_date
+                sum(case when aal.partner_id != 1 or aal.partner_id is null and aal.holiday_id is null then unit_amount else 0 end) as production,
+                sum(case when aal.partner_id = 1 and aal.holiday_id is null then unit_amount else 0 end) as internal,
+                0 as revenue
             from
                 account_analytic_line aal
             left join
@@ -493,13 +493,13 @@ class LeFilamentTdb(models.Model):
                 hr_leave_type lt on l.holiday_status_id = lt.id
             where
                 aal.project_id is not null
-                and (lt.active is true or lt.active is null)
+                %s
+            group by e.name
             ) query
-        %s
         group by employee
         order by sum(production) desc
         """
-        self.env.cr.execute(query, (AsIs(clause),))
+        self.env.cr.execute(query, (AsIs(clause), AsIs(clause),))
 
         result = self.env.cr.dictfetchall()
         return result
-- 
GitLab