From 83c8ed082737e73071073838bb42d9825064d5c6 Mon Sep 17 00:00:00 2001 From: pawan Date: Mon, 17 Apr 2017 01:02:56 +0530 Subject: [PATCH 1/4] =?UTF-8?q?=E2=80=9D[fix]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/campaign_efficiency/__init__.py | 0 .../campaign_efficiency.js | 21 ++++++ .../campaign_efficiency.json | 30 ++++++++ .../campaign_efficiency.py | 69 +++++++++++++++++++ .../report/lead_owner_efficiency/__init__.py | 0 .../lead_owner_efficiency.js | 21 ++++++ .../lead_owner_efficiency.json | 30 ++++++++ .../lead_owner_efficiency.py | 69 +++++++++++++++++++ 8 files changed, 240 insertions(+) create mode 100644 erpnext/crm/report/campaign_efficiency/__init__.py create mode 100644 erpnext/crm/report/campaign_efficiency/campaign_efficiency.js create mode 100644 erpnext/crm/report/campaign_efficiency/campaign_efficiency.json create mode 100644 erpnext/crm/report/campaign_efficiency/campaign_efficiency.py create mode 100644 erpnext/crm/report/lead_owner_efficiency/__init__.py create mode 100644 erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js create mode 100644 erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.json create mode 100644 erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py diff --git a/erpnext/crm/report/campaign_efficiency/__init__.py b/erpnext/crm/report/campaign_efficiency/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js new file mode 100644 index 0000000000..74a18512bc --- /dev/null +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js @@ -0,0 +1,21 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +frappe.require("assets/erpnext/js/financial_statements.js", function() { + frappe.query_reports["Campaign Efficiency"] = { + "filters": [ + { + "fieldname": "from_date", + "label": __("From Date"), + "fieldtype": "Date", + "default": frappe.defaults.get_user_default("year_start_date"), + }, + { + "fieldname": "to_date", + "label": __("To Date"), + "fieldtype": "Date", + "default": frappe.defaults.get_user_default("year_end_date"), + } + ] + } +}); + diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.json b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.json new file mode 100644 index 0000000000..986d9f3518 --- /dev/null +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.json @@ -0,0 +1,30 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "creation": "2017-04-17 00:20:27.248275", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "letter_head": "", + "modified": "2017-04-17 00:20:27.248275", + "modified_by": "Administrator", + "module": "CRM", + "name": "Campaign Efficiency", + "owner": "Administrator", + "ref_doctype": "Lead", + "report_name": "Campaign Efficiency", + "report_type": "Script Report", + "roles": [ + { + "role": "Sales User" + }, + { + "role": "Sales Manager" + }, + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py new file mode 100644 index 0000000000..6bf8bd8a66 --- /dev/null +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py @@ -0,0 +1,69 @@ +# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +import frappe +from frappe import _ +from frappe.utils import flt,cstr +from erpnext.accounts.report.financial_statements import get_period_list + +def execute(filters=None): + columns, data = [], [] + columns=get_columns() + data=get_lead_data(filters) + return columns, data + +def get_columns(): + columns = [_("Campaign Name") + ":data:130", _("Lead Count") + ":Int:80", + _("Opp Count") + ":Int:80", + _("Quot Count") + ":Int:80", _("Order Count") + ":Int:100", + _("Order Value") + ":Float:100",_("Opp/Lead %") + ":Float:100", + _("Quot/Lead %") + ":Float:100",_("Order/Quot %") + ":Float:100" + ] + return columns + +def get_lead_data(filters): + conditions="" + if filters.from_date: + conditions += " and date(creation) >= %(from_date)s" + if filters.to_date: + conditions += " and date(creation) <= %(to_date)s" + data = frappe.db.sql("""select campaign_name as "Campaign Name", count(name) as "Lead Count" from `tabLead` where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) + dl=list(data) + for row in dl: + is_quot_count_zero = False + row["Quot Count"]= get_lead_quotation_count(row["Campaign Name"]) + row["Opp Count"] = get_lead_opp_count(row["Campaign Name"]) + row["Order Count"] = get_quotation_ordered_count(row["Campaign Name"]) + row["Order Value"] = get_order_amount(row["Campaign Name"]) + row["Opp/Lead %"] = row["Opp Count"] / row["Lead Count"] * 100 + row["Quot/Lead %"] = row["Quot Count"] / row["Lead Count"] * 100 + #Handle div by zero and reset count to zero + if row["Quot Count"] == 0: + row["Quot Count"] = 1 + is_quot_count_zero = True + row["Order/Quot %"] = row["Order Count"] / row["Quot Count"] * 100 + if is_quot_count_zero == True: + row["Quot Count"] = 0 + return dl + +def get_lead_quotation_count(campaign): + quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` + where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + return flt(quotation_count[0][0]) if quotation_count else 0 + +def get_lead_opp_count(campaign): + opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` + where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + return flt(opportunity_count[0][0]) if opportunity_count else 0 + +def get_quotation_ordered_count(campaign): + quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` + where status = 'Ordered' and lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 + +def get_order_amount(campaign): + ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` + where prevdoc_docname in (select name from `tabQuotation` + where status = 'Ordered' and lead in (select name from `tabLead` where campaign_name = %s))""",campaign) + return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file diff --git a/erpnext/crm/report/lead_owner_efficiency/__init__.py b/erpnext/crm/report/lead_owner_efficiency/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js new file mode 100644 index 0000000000..217070b28e --- /dev/null +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js @@ -0,0 +1,21 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +frappe.require("assets/erpnext/js/financial_statements.js", function() { + frappe.query_reports["Lead Owner Efficiency"] = { + "filters": [ + { + "fieldname": "from_date", + "label": __("From Date"), + "fieldtype": "Date", + "default": frappe.defaults.get_user_default("year_start_date"), + }, + { + "fieldname": "to_date", + "label": __("To Date"), + "fieldtype": "Date", + "default": frappe.defaults.get_user_default("year_end_date"), + } + ] + } +}); + diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.json b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.json new file mode 100644 index 0000000000..b6dadef4c4 --- /dev/null +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.json @@ -0,0 +1,30 @@ +{ + "add_total_row": 0, + "apply_user_permissions": 1, + "creation": "2017-04-17 00:39:39.885905", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "letter_head": "", + "modified": "2017-04-17 00:45:10.139004", + "modified_by": "Administrator", + "module": "CRM", + "name": "Lead Owner Efficiency", + "owner": "Administrator", + "ref_doctype": "Lead", + "report_name": "Lead Owner Efficiency", + "report_type": "Script Report", + "roles": [ + { + "role": "Sales User" + }, + { + "role": "Sales Manager" + }, + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py new file mode 100644 index 0000000000..0c63c951a4 --- /dev/null +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py @@ -0,0 +1,69 @@ +# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +import frappe +from frappe import _ +from frappe.utils import flt,cstr +from erpnext.accounts.report.financial_statements import get_period_list + +def execute(filters=None): + columns, data = [], [] + columns=get_columns() + data=get_lead_data(filters) + return columns, data + +def get_columns(): + columns = [_("Lead Owner") + ":data:130", _("Lead Count") + ":Int:80", + _("Opp Count") + ":Int:80", + _("Quot Count") + ":Int:80", _("Order Count") + ":Int:100", + _("Order Value") + ":Float:100",_("Opp/Lead %") + ":Float:100", + _("Quot/Lead %") + ":Float:100",_("Order/Quot %") + ":Float:100" + ] + return columns + +def get_lead_data(filters): + conditions="" + if filters.from_date: + conditions += " and date(creation) >= %(from_date)s" + if filters.to_date: + conditions += " and date(creation) <= %(to_date)s" + data = frappe.db.sql("""select lead_owner as "Lead Owner", count(name) as "Lead Count" from `tabLead` where 1 = 1 %s group by lead_owner""" % (conditions,),filters, as_dict=1) + dl=list(data) + for row in dl: + is_quot_count_zero = False + row["Quot Count"]= get_lead_quotation_count(row["Lead Owner"]) + row["Opp Count"] = get_lead_opp_count(row["Lead Owner"]) + row["Order Count"] = get_quotation_ordered_count(row["Lead Owner"]) + row["Order Value"] = get_order_amount(row["Lead Owner"]) + row["Opp/Lead %"] = row["Opp Count"] / row["Lead Count"] * 100 + row["Quot/Lead %"] = row["Quot Count"] / row["Lead Count"] * 100 + #Handle div by zero and reset count to zero + if row["Quot Count"] == 0: + row["Quot Count"] = 1 + is_quot_count_zero = True + row["Order/Quot %"] = row["Order Count"] / row["Quot Count"] * 100 + if is_quot_count_zero == True: + row["Quot Count"] = 0 + return dl + +def get_lead_quotation_count(leadowner): + quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` + where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + return flt(quotation_count[0][0]) if quotation_count else 0 + +def get_lead_opp_count(leadowner): + opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` + where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + return flt(opportunity_count[0][0]) if opportunity_count else 0 + +def get_quotation_ordered_count(leadowner): + quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` + where status = 'Ordered' and lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 + +def get_order_amount(leadowner): + ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` + where prevdoc_docname in (select name from `tabQuotation` + where status = 'Ordered' and lead in (select name from `tabLead` where lead_owner = %s))""",leadowner) + return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file From 7bd7df374281645b2fd19505813dec87e0f7ee50 Mon Sep 17 00:00:00 2001 From: pawan Date: Mon, 24 Apr 2017 13:19:22 +0530 Subject: [PATCH 2/4] Changes after review --- .../campaign_efficiency/campaign_efficiency.js | 6 ++---- .../campaign_efficiency/campaign_efficiency.py | 14 +++++++++----- .../lead_owner_efficiency/lead_owner_efficiency.js | 6 +----- .../lead_owner_efficiency/lead_owner_efficiency.py | 10 ++++++---- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js index 74a18512bc..2b25f1dfba 100644 --- a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.js @@ -1,7 +1,6 @@ // Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt -frappe.require("assets/erpnext/js/financial_statements.js", function() { - frappe.query_reports["Campaign Efficiency"] = { +frappe.query_reports["Campaign Efficiency"] = { "filters": [ { "fieldname": "from_date", @@ -16,6 +15,5 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "default": frappe.defaults.get_user_default("year_end_date"), } ] - } -}); + }; diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py index 6bf8bd8a66..aad64ff592 100644 --- a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py @@ -28,7 +28,8 @@ def get_lead_data(filters): conditions += " and date(creation) >= %(from_date)s" if filters.to_date: conditions += " and date(creation) <= %(to_date)s" - data = frappe.db.sql("""select campaign_name as "Campaign Name", count(name) as "Lead Count" from `tabLead` where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) + data = frappe.db.sql("""select campaign_name as "Campaign Name", count(name) as "Lead Count" from `tabLead` + where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) dl=list(data) for row in dl: is_quot_count_zero = False @@ -54,16 +55,19 @@ def get_lead_quotation_count(campaign): def get_lead_opp_count(campaign): opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) return flt(opportunity_count[0][0]) if opportunity_count else 0 def get_quotation_ordered_count(campaign): quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` - where status = 'Ordered' and lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + where status = 'Ordered' and lead in + (select name from `tabLead` where campaign_name = %s)""",campaign) return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 def get_order_amount(campaign): ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in (select name from `tabQuotation` - where status = 'Ordered' and lead in (select name from `tabLead` where campaign_name = %s))""",campaign) + where prevdoc_docname in + (select name from `tabQuotation` + where status = 'Ordered' and lead in + (select name from `tabLead` where campaign_name = %s))""",campaign) return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js index 217070b28e..bbfd6ac9ff 100644 --- a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.js @@ -1,6 +1,5 @@ // Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt -frappe.require("assets/erpnext/js/financial_statements.js", function() { frappe.query_reports["Lead Owner Efficiency"] = { "filters": [ { @@ -15,7 +14,4 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "fieldtype": "Date", "default": frappe.defaults.get_user_default("year_end_date"), } - ] - } -}); - + ]}; diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py index 0c63c951a4..7ac55180f2 100644 --- a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py @@ -54,16 +54,18 @@ def get_lead_quotation_count(leadowner): def get_lead_opp_count(leadowner): opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) return flt(opportunity_count[0][0]) if opportunity_count else 0 def get_quotation_ordered_count(leadowner): quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` - where status = 'Ordered' and lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + where status = 'Ordered' and lead in + (select name from `tabLead` where lead_owner = %s)""",leadowner) return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 def get_order_amount(leadowner): ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in (select name from `tabQuotation` - where status = 'Ordered' and lead in (select name from `tabLead` where lead_owner = %s))""",leadowner) + where prevdoc_docname in (select name from `tabQuotation` + where status = 'Ordered' and lead in + (select name from `tabLead` where lead_owner = %s))""",leadowner) return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file From 134487ab2aa7a0d67307796462a389cb516f5c23 Mon Sep 17 00:00:00 2001 From: pawan Date: Mon, 24 Apr 2017 13:48:40 +0530 Subject: [PATCH 3/4] Changes after review --- .../campaign_efficiency.py | 19 ++++++++----------- .../lead_owner_efficiency.py | 17 +++++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py index aad64ff592..3088332351 100644 --- a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py @@ -29,7 +29,7 @@ def get_lead_data(filters): if filters.to_date: conditions += " and date(creation) <= %(to_date)s" data = frappe.db.sql("""select campaign_name as "Campaign Name", count(name) as "Lead Count" from `tabLead` - where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) + where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) dl=list(data) for row in dl: is_quot_count_zero = False @@ -50,24 +50,21 @@ def get_lead_data(filters): def get_lead_quotation_count(campaign): quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` - where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) return flt(quotation_count[0][0]) if quotation_count else 0 def get_lead_opp_count(campaign): opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) + where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) return flt(opportunity_count[0][0]) if opportunity_count else 0 def get_quotation_ordered_count(campaign): - quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where campaign_name = %s)""",campaign) + quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` where status = 'Ordered' + and lead in (select name from `tabLead` where campaign_name = %s)""",campaign) return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 def get_order_amount(campaign): - ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in - (select name from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where campaign_name = %s))""",campaign) + ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` + where prevdoc_docname in (select name from `tabQuotation` where status = 'Ordered' and + lead in (select name from `tabLead` where campaign_name = %s))""",campaign) return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py index 7ac55180f2..1560f869d9 100644 --- a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py @@ -28,7 +28,8 @@ def get_lead_data(filters): conditions += " and date(creation) >= %(from_date)s" if filters.to_date: conditions += " and date(creation) <= %(to_date)s" - data = frappe.db.sql("""select lead_owner as "Lead Owner", count(name) as "Lead Count" from `tabLead` where 1 = 1 %s group by lead_owner""" % (conditions,),filters, as_dict=1) + data = frappe.db.sql("""select lead_owner as "Lead Owner", count(name) as "Lead Count" + from `tabLead` where 1 = 1 %s group by lead_owner""" % (conditions,),filters, as_dict=1) dl=list(data) for row in dl: is_quot_count_zero = False @@ -49,23 +50,23 @@ def get_lead_data(filters): def get_lead_quotation_count(leadowner): quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` - where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) return flt(quotation_count[0][0]) if quotation_count else 0 def get_lead_opp_count(leadowner): opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) + where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) return flt(opportunity_count[0][0]) if opportunity_count else 0 def get_quotation_ordered_count(leadowner): quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where lead_owner = %s)""",leadowner) + where status = 'Ordered' and lead in + (select name from `tabLead` where lead_owner = %s)""",leadowner) return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 def get_order_amount(leadowner): ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in (select name from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where lead_owner = %s))""",leadowner) + where prevdoc_docname in (select name from `tabQuotation` + where status = 'Ordered' and lead in + (select name from `tabLead` where lead_owner = %s))""",leadowner) return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file From 020dedd00e14e53eb6998ba8700500eeb276b306 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 25 Apr 2017 16:05:01 +0530 Subject: [PATCH 4/4] Cleaned up and commonified the campaign efficiency and lead owner efficiency report --- .../campaign_efficiency.py | 113 ++++++++++-------- .../lead_owner_efficiency.py | 72 ++--------- 2 files changed, 79 insertions(+), 106 deletions(-) diff --git a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py index 3088332351..b20fe15ce2 100644 --- a/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py +++ b/erpnext/crm/report/campaign_efficiency/campaign_efficiency.py @@ -4,67 +4,86 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import flt,cstr -from erpnext.accounts.report.financial_statements import get_period_list def execute(filters=None): columns, data = [], [] columns=get_columns() - data=get_lead_data(filters) + data=get_lead_data(filters, "Campaign Name") return columns, data def get_columns(): - columns = [_("Campaign Name") + ":data:130", _("Lead Count") + ":Int:80", - _("Opp Count") + ":Int:80", - _("Quot Count") + ":Int:80", _("Order Count") + ":Int:100", - _("Order Value") + ":Float:100",_("Opp/Lead %") + ":Float:100", - _("Quot/Lead %") + ":Float:100",_("Order/Quot %") + ":Float:100" + return [ + _("Campaign Name") + ":data:130", + _("Lead Count") + ":Int:80", + _("Opp Count") + ":Int:80", + _("Quot Count") + ":Int:80", + _("Order Count") + ":Int:100", + _("Order Value") + ":Float:100", + _("Opp/Lead %") + ":Float:100", + _("Quot/Lead %") + ":Float:100", + _("Order/Quot %") + ":Float:100" ] - return columns - -def get_lead_data(filters): + +def get_lead_data(filters, based_on): + based_on_field = frappe.scrub(based_on) + conditions = get_filter_conditions(filters) + + lead_details = frappe.db.sql(""" + select {based_on_field}, name + from `tabLead` + where {based_on_field} is not null and {based_on_field} != '' {conditions} + """.format(based_on_field=based_on_field, conditions=conditions), filters, as_dict=1) + + lead_map = frappe._dict() + for d in lead_details: + lead_map.setdefault(d.get(based_on_field), []).append(d.name) + + data = [] + for based_on_value, leads in lead_map.items(): + row = { + based_on: based_on_value, + "Lead Count": len(leads) + } + row["Quot Count"]= get_lead_quotation_count(leads) + row["Opp Count"] = get_lead_opp_count(leads) + row["Order Count"] = get_quotation_ordered_count(leads) + row["Order Value"] = get_order_amount(leads) + + row["Opp/Lead %"] = row["Opp Count"] / row["Lead Count"] * 100 + row["Quot/Lead %"] = row["Quot Count"] / row["Lead Count"] * 100 + + row["Order/Quot %"] = row["Order Count"] / (row["Quot Count"] or 1) * 100 + + data.append(row) + + return data + +def get_filter_conditions(filters): conditions="" if filters.from_date: conditions += " and date(creation) >= %(from_date)s" if filters.to_date: conditions += " and date(creation) <= %(to_date)s" - data = frappe.db.sql("""select campaign_name as "Campaign Name", count(name) as "Lead Count" from `tabLead` - where 1 = 1 %s group by campaign_name""" % (conditions,),filters, as_dict=1) - dl=list(data) - for row in dl: - is_quot_count_zero = False - row["Quot Count"]= get_lead_quotation_count(row["Campaign Name"]) - row["Opp Count"] = get_lead_opp_count(row["Campaign Name"]) - row["Order Count"] = get_quotation_ordered_count(row["Campaign Name"]) - row["Order Value"] = get_order_amount(row["Campaign Name"]) - row["Opp/Lead %"] = row["Opp Count"] / row["Lead Count"] * 100 - row["Quot/Lead %"] = row["Quot Count"] / row["Lead Count"] * 100 - #Handle div by zero and reset count to zero - if row["Quot Count"] == 0: - row["Quot Count"] = 1 - is_quot_count_zero = True - row["Order/Quot %"] = row["Order Count"] / row["Quot Count"] * 100 - if is_quot_count_zero == True: - row["Quot Count"] = 0 - return dl -def get_lead_quotation_count(campaign): - quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` - where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) - return flt(quotation_count[0][0]) if quotation_count else 0 + return conditions -def get_lead_opp_count(campaign): - opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where campaign_name = %s)""",campaign) - return flt(opportunity_count[0][0]) if opportunity_count else 0 +def get_lead_quotation_count(leads): + return frappe.db.sql("""select count(name) from `tabQuotation` + where lead in (%s)""" % ', '.join(["%s"]*len(leads)), tuple(leads))[0][0] -def get_quotation_ordered_count(campaign): - quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` where status = 'Ordered' - and lead in (select name from `tabLead` where campaign_name = %s)""",campaign) - return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 +def get_lead_opp_count(leads): + return frappe.db.sql("""select count(name) from `tabOpportunity` + where lead in (%s)""" % ', '.join(["%s"]*len(leads)), tuple(leads))[0][0] -def get_order_amount(campaign): - ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in (select name from `tabQuotation` where status = 'Ordered' and - lead in (select name from `tabLead` where campaign_name = %s))""",campaign) - return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file +def get_quotation_ordered_count(leads): + return frappe.db.sql("""select count(name) + from `tabQuotation` where status = 'Ordered' + and lead in (%s)""" % ', '.join(["%s"]*len(leads)), tuple(leads))[0][0] + +def get_order_amount(leads): + return frappe.db.sql("""select sum(base_net_amount) + from `tabSales Order Item` + where prevdoc_docname in ( + select name from `tabQuotation` where status = 'Ordered' + and lead in (%s) + )""" % ', '.join(["%s"]*len(leads)), tuple(leads))[0][0] \ No newline at end of file diff --git a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py index 1560f869d9..8134bc2003 100644 --- a/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py +++ b/erpnext/crm/report/lead_owner_efficiency/lead_owner_efficiency.py @@ -4,69 +4,23 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import flt,cstr -from erpnext.accounts.report.financial_statements import get_period_list +from erpnext.crm.report.campaign_efficiency.campaign_efficiency import get_lead_data def execute(filters=None): columns, data = [], [] columns=get_columns() - data=get_lead_data(filters) + data=get_lead_data(filters, "Lead Owner") return columns, data def get_columns(): - columns = [_("Lead Owner") + ":data:130", _("Lead Count") + ":Int:80", - _("Opp Count") + ":Int:80", - _("Quot Count") + ":Int:80", _("Order Count") + ":Int:100", - _("Order Value") + ":Float:100",_("Opp/Lead %") + ":Float:100", - _("Quot/Lead %") + ":Float:100",_("Order/Quot %") + ":Float:100" - ] - return columns - -def get_lead_data(filters): - conditions="" - if filters.from_date: - conditions += " and date(creation) >= %(from_date)s" - if filters.to_date: - conditions += " and date(creation) <= %(to_date)s" - data = frappe.db.sql("""select lead_owner as "Lead Owner", count(name) as "Lead Count" - from `tabLead` where 1 = 1 %s group by lead_owner""" % (conditions,),filters, as_dict=1) - dl=list(data) - for row in dl: - is_quot_count_zero = False - row["Quot Count"]= get_lead_quotation_count(row["Lead Owner"]) - row["Opp Count"] = get_lead_opp_count(row["Lead Owner"]) - row["Order Count"] = get_quotation_ordered_count(row["Lead Owner"]) - row["Order Value"] = get_order_amount(row["Lead Owner"]) - row["Opp/Lead %"] = row["Opp Count"] / row["Lead Count"] * 100 - row["Quot/Lead %"] = row["Quot Count"] / row["Lead Count"] * 100 - #Handle div by zero and reset count to zero - if row["Quot Count"] == 0: - row["Quot Count"] = 1 - is_quot_count_zero = True - row["Order/Quot %"] = row["Order Count"] / row["Quot Count"] * 100 - if is_quot_count_zero == True: - row["Quot Count"] = 0 - return dl - -def get_lead_quotation_count(leadowner): - quotation_count = frappe.db.sql("""select count(name) from `tabQuotation` - where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) - return flt(quotation_count[0][0]) if quotation_count else 0 - -def get_lead_opp_count(leadowner): - opportunity_count = frappe.db.sql("""select count(name) from `tabOpportunity` - where lead in (select name from `tabLead` where lead_owner = %s)""",leadowner) - return flt(opportunity_count[0][0]) if opportunity_count else 0 - -def get_quotation_ordered_count(leadowner): - quotation_ordered_count = frappe.db.sql("""select count(name) from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where lead_owner = %s)""",leadowner) - return flt(quotation_ordered_count[0][0]) if quotation_ordered_count else 0 - -def get_order_amount(leadowner): - ordered_count_amount = frappe.db.sql("""select sum(base_net_amount) from `tabSales Order Item` - where prevdoc_docname in (select name from `tabQuotation` - where status = 'Ordered' and lead in - (select name from `tabLead` where lead_owner = %s))""",leadowner) - return flt(ordered_count_amount[0][0]) if ordered_count_amount else 0 \ No newline at end of file + return [ + _("Lead Owner") + ":Data:130", + _("Lead Count") + ":Int:80", + _("Opp Count") + ":Int:80", + _("Quot Count") + ":Int:80", + _("Order Count") + ":Int:100", + _("Order Value") + ":Float:100", + _("Opp/Lead %") + ":Float:100", + _("Quot/Lead %") + ":Float:100", + _("Order/Quot %") + ":Float:100" + ] \ No newline at end of file