fix: Validate Filters in Sales Funnel. (#21761)

* fix: Validate Filters in Sales Funnel.

* fix: Style fixes
This commit is contained in:
Marica 2020-05-18 14:38:57 +05:30 committed by GitHub
parent d94a38eb51
commit c734db5d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -90,6 +90,10 @@ erpnext.SalesFunnel = class SalesFunnel {
get_data(btn) {
var me = this;
if (!this.company) {
frappe.throw(__("Please Select a Company."));
}
const method_map = {
"sales_funnel": "erpnext.selling.page.sales_funnel.sales_funnel.get_funnel_data",
"opp_by_lead_source": "erpnext.selling.page.sales_funnel.sales_funnel.get_opp_by_lead_source",

View File

@ -8,14 +8,23 @@ from frappe import _
from erpnext.accounts.report.utils import convert
import pandas as pd
def validate_filters(from_date, to_date, company):
if from_date and to_date and (from_date >= to_date):
frappe.throw(_("To Date must be greater than From Date"))
if not company:
frappe.throw(_("Please Select a Company"))
@frappe.whitelist()
def get_funnel_data(from_date, to_date, company):
validate_filters(from_date, to_date, company)
active_leads = frappe.db.sql("""select count(*) from `tabLead`
where (date(`modified`) between %s and %s)
and status != "Do Not Contact" and company=%s""", (from_date, to_date, company))[0][0]
active_leads += frappe.db.sql("""select count(distinct contact.name) from `tabContact` contact
left join `tabDynamic Link` dl on (dl.parent=contact.name) where dl.link_doctype='Customer'
left join `tabDynamic Link` dl on (dl.parent=contact.name) where dl.link_doctype='Customer'
and (date(contact.modified) between %s and %s) and status != "Passive" """, (from_date, to_date))[0][0]
opportunities = frappe.db.sql("""select count(*) from `tabOpportunity`
@ -38,6 +47,8 @@ def get_funnel_data(from_date, to_date, company):
@frappe.whitelist()
def get_opp_by_lead_source(from_date, to_date, company):
validate_filters(from_date, to_date, company)
opportunities = frappe.get_all("Opportunity", filters=[['status', 'in', ['Open', 'Quotation', 'Replied']], ['company', '=', company], ['transaction_date', 'Between', [from_date, to_date]]], fields=['currency', 'sales_stage', 'opportunity_amount', 'probability', 'source'])
if opportunities:
@ -68,11 +79,13 @@ def get_opp_by_lead_source(from_date, to_date, company):
@frappe.whitelist()
def get_pipeline_data(from_date, to_date, company):
validate_filters(from_date, to_date, company)
opportunities = frappe.get_all("Opportunity", filters=[['status', 'in', ['Open', 'Quotation', 'Replied']], ['company', '=', company], ['transaction_date', 'Between', [from_date, to_date]]], fields=['currency', 'sales_stage', 'opportunity_amount', 'probability'])
if opportunities:
default_currency = frappe.get_cached_value('Global Defaults', 'None', 'default_currency')
cp_opportunities = [dict(x, **{'compound_amount': (convert(x['opportunity_amount'], x['currency'], default_currency, to_date) * x['probability']/100)}) for x in opportunities]
df = pd.DataFrame(cp_opportunities).groupby(['sales_stage'], as_index=True).agg({'compound_amount': 'sum'}).to_dict()