From 107eeae116f153913712261b3162929c774d8dd1 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Sun, 13 Jul 2025 12:11:50 +0530 Subject: [PATCH] fix: used closed_date instead of closed_on and set closed_date if status type is Won (cherry picked from commit f82019e510612bd9803f9947240d155479d5d665) --- crm/api/dashboard.py | 26 +++++++++++++------------- crm/fcrm/doctype/crm_deal/crm_deal.py | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/crm/api/dashboard.py b/crm/api/dashboard.py index d94172c7..3a5d31b7 100644 --- a/crm/api/dashboard.py +++ b/crm/api/dashboard.py @@ -202,7 +202,7 @@ def get_won_deal_count(from_date, to_date, user="", conds="", return_result=Fals f""" SELECT COUNT(CASE - WHEN d.closed_on >= %(from_date)s AND d.closed_on < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) + WHEN d.closed_date >= %(from_date)s AND d.closed_date < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) AND s.type = 'Won' {conds} THEN d.name @@ -210,7 +210,7 @@ def get_won_deal_count(from_date, to_date, user="", conds="", return_result=Fals END) as current_month_deals, COUNT(CASE - WHEN d.closed_on >= %(prev_from_date)s AND d.closed_on < %(from_date)s + WHEN d.closed_date >= %(prev_from_date)s AND d.closed_date < %(from_date)s AND s.type = 'Won' {conds} THEN d.name @@ -218,7 +218,7 @@ def get_won_deal_count(from_date, to_date, user="", conds="", return_result=Fals END) as prev_month_deals, AVG(CASE - WHEN d.closed_on >= %(from_date)s AND d.closed_on < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) + WHEN d.closed_date >= %(from_date)s AND d.closed_date < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) AND s.type = 'Won' {conds} THEN d.deal_value * IFNULL(d.exchange_rate, 1) @@ -226,7 +226,7 @@ def get_won_deal_count(from_date, to_date, user="", conds="", return_result=Fals END) as current_month_avg_value, AVG(CASE - WHEN d.closed_on >= %(prev_from_date)s AND d.closed_on < %(from_date)s + WHEN d.closed_date >= %(prev_from_date)s AND d.closed_date < %(from_date)s AND s.type = 'Won' {conds} THEN d.deal_value * IFNULL(d.exchange_rate, 1) @@ -353,18 +353,18 @@ def get_average_time_to_close(from_date, to_date, user="", conds="", return_resu result = frappe.db.sql( f""" SELECT - AVG(CASE WHEN d.closed_on >= %(from_date)s AND d.closed_on < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) - THEN TIMESTAMPDIFF(DAY, COALESCE(l.creation, d.creation), d.closed_on) END) as current_avg_lead, - AVG(CASE WHEN d.closed_on >= %(prev_from_date)s AND d.closed_on < %(prev_to_date)s - THEN TIMESTAMPDIFF(DAY, COALESCE(l.creation, d.creation), d.closed_on) END) as prev_avg_lead, - AVG(CASE WHEN d.closed_on >= %(from_date)s AND d.closed_on < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) - THEN TIMESTAMPDIFF(DAY, d.creation, d.closed_on) END) as current_avg_deal, - AVG(CASE WHEN d.closed_on >= %(prev_from_date)s AND d.closed_on < %(prev_to_date)s - THEN TIMESTAMPDIFF(DAY, d.creation, d.closed_on) END) as prev_avg_deal + AVG(CASE WHEN d.closed_date >= %(from_date)s AND d.closed_date < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) + THEN TIMESTAMPDIFF(DAY, COALESCE(l.creation, d.creation), d.closed_date) END) as current_avg_lead, + AVG(CASE WHEN d.closed_date >= %(prev_from_date)s AND d.closed_date < %(prev_to_date)s + THEN TIMESTAMPDIFF(DAY, COALESCE(l.creation, d.creation), d.closed_date) END) as prev_avg_lead, + AVG(CASE WHEN d.closed_date >= %(from_date)s AND d.closed_date < DATE_ADD(%(to_date)s, INTERVAL 1 DAY) + THEN TIMESTAMPDIFF(DAY, d.creation, d.closed_date) END) as current_avg_deal, + AVG(CASE WHEN d.closed_date >= %(prev_from_date)s AND d.closed_date < %(prev_to_date)s + THEN TIMESTAMPDIFF(DAY, d.creation, d.closed_date) END) as prev_avg_deal FROM `tabCRM Deal` AS d JOIN `tabCRM Deal Status` s ON d.status = s.name LEFT JOIN `tabCRM Lead` l ON d.lead = l.name - WHERE d.closed_on IS NOT NULL AND s.type = 'Won' + WHERE d.closed_date IS NOT NULL AND s.type = 'Won' {conds} """, { diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index c3a8488d..f46f6475 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -25,8 +25,8 @@ class CRMDeal(Document): self.assign_agent(self.deal_owner) if self.has_value_changed("status"): add_status_change_log(self) - if self.status == "Won": - self.closed_on = frappe.utils.now_datetime() + if frappe.db.get_value("CRM Deal Status", self.status, "type") == "Won": + self.closed_date = frappe.utils.nowdate() self.validate_forcasting_fields() self.validate_lost_reason() self.update_exchange_rate()