From f910bd7bb83ef8a2e1f9b87efaaf33bc8df45cbc Mon Sep 17 00:00:00 2001
From: Benjamin <Benjamin@MBP-de-Benj.(none)>
Date: Fri, 20 Oct 2017 18:03:20 +0200
Subject: [PATCH] fix erreurs requetes _delay et _total_days

---
 models/lefilament_projets.py | 42 +++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/models/lefilament_projets.py b/models/lefilament_projets.py
index 97f5c9c..56b8df3 100644
--- a/models/lefilament_projets.py
+++ b/models/lefilament_projets.py
@@ -86,14 +86,27 @@ class FilamentProjet(models.Model):
 	@api.one
 	def _delay(self):
 		account_id = self.analytic_account_id.id
-		self._cr.execute("""
-			SELECT 
-			date(d_invoice) - date(d_cost) as datediff
-			from 
-			(select date FROM account_analytic_line where account_id=%s and amount < 0 order by date limit 1) as d_cost,
-			(select date FROM account_analytic_line where account_id=%s and amount > 0 order by date limit 1) as d_invoice
-			;""", (account_id, account_id ) )
-		self.lf_delay = self._cr.fetchone()[0]
+		self.env.cr.execute("""
+			SELECT date 
+			FROM account_analytic_line 
+			where account_id=%s and amount < 0 order by date limit 1
+			;""", (account_id, ) )
+		d_cost = self.env.cr.fetchone()
+
+		self.env.cr.execute("""
+			SELECT date 
+			FROM account_analytic_line 
+			where account_id=%s and amount > 0 order by date limit 1
+			;""", (account_id, ) )
+		d_invoice = self.env.cr.fetchone()
+
+		if d_cost:
+			if d_invoice:
+				self.lf_delay = (datetime.strptime(d_invoice[0], "%Y-%m-%d").date() - datetime.strptime(d_cost[0], "%Y-%m-%d").date()).days
+			else:
+				self.lf_delay = (datetime.now().date() - datetime.strptime(d_cost[0], "%Y-%m-%d").date()).days
+		else:
+			self.lf_delay = 0
 
 	## Total Days
 	@api.one
@@ -106,7 +119,12 @@ class FilamentProjet(models.Model):
 			(select date FROM account_analytic_line where account_id=%s order by date limit 1) as d_first,
 			(select date FROM account_analytic_line where account_id=%s order by date desc limit 1) as d_last
 			;""", (account_id, account_id, ) )
-		self.lf_total_days = self._cr.fetchone()[0]
+		lf_total_days = self._cr.fetchone()
+		
+		if lf_total_days:
+			self.lf_total_days = lf_total_days[0]
+		else:
+			self.lf_total_days = 0
 
 	## Balance
 	@api.one
@@ -127,10 +145,10 @@ class FilamentProjet(models.Model):
 		self.env.cr.execute("""
 			SELECT sum(amount) as Total
 			FROM account_analytic_line
-			WHERE account_id=31
+			WHERE account_id=%s
 			and date < (case when
-			(select date FROM account_analytic_line where account_id=31 and amount > 0 order by date limit 1) IS NOT NULL then
-			(select date FROM account_analytic_line where account_id=31 and amount > 0 order by date limit 1) else current_date end
+			(select date FROM account_analytic_line where account_id=%s and amount > 0 order by date limit 1) IS NOT NULL then
+			(select date FROM account_analytic_line where account_id=%s and amount > 0 order by date limit 1) else current_date end
 			);""", (account_id, account_id, account_id, ) )
 		self.lf_advance = self._cr.fetchone()[0]
 
-- 
GitLab