diff --git a/models/lefilament_projets.py b/models/lefilament_projets.py index 97f5c9cee3147a55fd697adf34d0eb83c193d793..56b8df32082e11e64a4657e0633de972fae5c687 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]