From 578dacc3af04996754d1ec67108106379a9c3c38 Mon Sep 17 00:00:00 2001
From: jvm-odoo <jvm@odoo.com>
Date: Tue, 12 Nov 2019 07:27:13 +0000
Subject: [PATCH] [FIX] sale: fix sales team goal not updated in CRM pipelines

Reproduce this issue

    - Install CRM & Sales
    - Create 2 sales team in CRM settings
    - Create some invoices and register payments for the 2 teams
    - Go in CRM > Sales > Team pipelines

    The invoicing amount is still 0 for both teams.

Cause

    - The `_compute_invoiced` method in `CrmTeam` models use a SQL
    query to retrieve the invoices lines but it returns an empty
    recordset because the dates are not right ordered.

    - The line balance are negatives values, doing a SUM() will always
      returns 0

This commit re-order the dates and invert the sign of line.balance.

OPW-2119819

closes odoo/odoo#40093

Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
---
 addons/sale/models/sales_team.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/addons/sale/models/sales_team.py b/addons/sale/models/sales_team.py
index 8c2ce776507b..8bec5cbf10b3 100644
--- a/addons/sale/models/sales_team.py
+++ b/addons/sale/models/sales_team.py
@@ -76,7 +76,7 @@ class CrmTeam(models.Model):
         query = '''
             SELECT
                 move.team_id         AS team_id,
-                SUM(line.balance)    AS amount_untaxed_signed
+                SUM(-line.balance)   AS amount_untaxed_signed
             FROM account_move move
             LEFT JOIN account_move_line line ON line.move_id = move.id
             WHERE move.type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund')
@@ -90,7 +90,7 @@ class CrmTeam(models.Model):
             GROUP BY move.team_id
         '''
         today = fields.Date.today()
-        params = [tuple(self.ids), fields.Date.to_string(today), fields.Date.to_string(today.replace(day=1))]
+        params = [tuple(self.ids), fields.Date.to_string(today.replace(day=1)), fields.Date.to_string(today)]
         self._cr.execute(query, params)
 
         data_map = dict((v[0], v[1]) for v in self._cr.fetchall())
-- 
GitLab