2018-10-03 05:11:41 +00:00
|
|
|
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-10-04 15:49:30 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2022-04-06 10:10:41 +00:00
|
|
|
from itertools import groupby
|
2013-10-04 15:49:30 +00:00
|
|
|
|
2016-01-04 10:27:01 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2022-04-06 10:10:41 +00:00
|
|
|
from frappe.utils import flt
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
from erpnext.accounts.report.utils import convert
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-01-04 10:27:01 +00:00
|
|
|
|
2020-05-18 09:08:57 +00:00
|
|
|
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"))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2018-10-03 05:11:41 +00:00
|
|
|
def get_funnel_data(from_date, to_date, company):
|
2020-05-18 09:08:57 +00:00
|
|
|
validate_filters(from_date, to_date, company)
|
|
|
|
|
2016-11-21 15:34:47 +00:00
|
|
|
active_leads = frappe.db.sql(
|
|
|
|
"""select count(*) from `tabLead`
|
2020-09-02 14:31:14 +00:00
|
|
|
where (date(`creation`) between %s and %s)
|
|
|
|
and company=%s""",
|
|
|
|
(from_date, to_date, company),
|
|
|
|
)[0][0]
|
2014-04-28 10:13:32 +00:00
|
|
|
|
2016-11-21 15:34:47 +00:00
|
|
|
opportunities = frappe.db.sql(
|
|
|
|
"""select count(*) from `tabOpportunity`
|
2016-02-24 13:55:35 +00:00
|
|
|
where (date(`creation`) between %s and %s)
|
2020-09-02 14:31:14 +00:00
|
|
|
and opportunity_from='Lead' and company=%s""",
|
|
|
|
(from_date, to_date, company),
|
|
|
|
)[0][0]
|
2014-04-28 10:13:32 +00:00
|
|
|
|
2016-11-21 15:34:47 +00:00
|
|
|
quotations = frappe.db.sql(
|
|
|
|
"""select count(*) from `tabQuotation`
|
2013-10-31 13:36:11 +00:00
|
|
|
where docstatus = 1 and (date(`creation`) between %s and %s)
|
2020-09-02 14:31:14 +00:00
|
|
|
and (opportunity!="" or quotation_to="Lead") and company=%s""",
|
|
|
|
(from_date, to_date, company),
|
|
|
|
)[0][0]
|
|
|
|
|
|
|
|
converted = frappe.db.sql(
|
|
|
|
"""select count(*) from `tabCustomer`
|
|
|
|
JOIN `tabLead` ON `tabLead`.name = `tabCustomer`.lead_name
|
|
|
|
WHERE (date(`tabCustomer`.creation) between %s and %s)
|
|
|
|
and `tabLead`.company=%s""",
|
|
|
|
(from_date, to_date, company),
|
|
|
|
)[0][0]
|
2014-04-28 10:13:32 +00:00
|
|
|
|
2013-10-04 15:49:30 +00:00
|
|
|
return [
|
2020-09-02 14:31:14 +00:00
|
|
|
{"title": _("Active Leads"), "value": active_leads, "color": "#B03B46"},
|
2016-01-04 10:27:01 +00:00
|
|
|
{"title": _("Opportunities"), "value": opportunities, "color": "#F09C00"},
|
|
|
|
{"title": _("Quotations"), "value": quotations, "color": "#006685"},
|
2020-09-02 14:31:14 +00:00
|
|
|
{"title": _("Converted"), "value": converted, "color": "#00AD65"},
|
2014-04-28 10:13:32 +00:00
|
|
|
]
|
2018-10-03 05:11:41 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_opp_by_lead_source(from_date, to_date, company):
|
2020-05-18 09:08:57 +00:00
|
|
|
validate_filters(from_date, to_date, company)
|
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
opportunities = frappe.get_all(
|
|
|
|
"Opportunity",
|
|
|
|
filters=[
|
|
|
|
["status", "in", ["Open", "Quotation", "Replied"]],
|
|
|
|
["company", "=", company],
|
|
|
|
["transaction_date", "Between", [from_date, to_date]],
|
2022-03-28 13:22:46 +00:00
|
|
|
],
|
2018-10-03 05:11:41 +00:00
|
|
|
fields=["currency", "sales_stage", "opportunity_amount", "probability", "source"],
|
|
|
|
)
|
|
|
|
|
|
|
|
if opportunities:
|
|
|
|
default_currency = frappe.get_cached_value("Global Defaults", "None", "default_currency")
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
cp_opportunities = [
|
|
|
|
dict(
|
|
|
|
x,
|
|
|
|
**{
|
|
|
|
"compound_amount": (
|
|
|
|
convert(x["opportunity_amount"], x["currency"], default_currency, to_date)
|
|
|
|
* x["probability"]
|
|
|
|
/ 100
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-10-03 05:11:41 +00:00
|
|
|
for x in opportunities
|
2022-03-28 13:22:46 +00:00
|
|
|
]
|
|
|
|
|
2022-04-06 10:10:41 +00:00
|
|
|
summary = {}
|
|
|
|
sales_stages = set()
|
|
|
|
group_key = lambda o: (o["source"], o["sales_stage"]) # noqa
|
|
|
|
for (source, sales_stage), rows in groupby(cp_opportunities, group_key):
|
|
|
|
summary.setdefault(source, {})[sales_stage] = sum(r["compound_amount"] for r in rows)
|
|
|
|
sales_stages.add(sales_stage)
|
|
|
|
|
|
|
|
pivot_table = []
|
|
|
|
for sales_stage in sales_stages:
|
|
|
|
row = []
|
|
|
|
for source, sales_stage_values in summary.items():
|
|
|
|
row.append(flt(sales_stage_values.get(sales_stage)))
|
|
|
|
pivot_table.append({"chartType": "bar", "name": sales_stage, "values": row})
|
|
|
|
|
|
|
|
result = {"datasets": pivot_table, "labels": list(summary.keys())}
|
2018-10-03 05:11:41 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
else:
|
|
|
|
return "empty"
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_pipeline_data(from_date, to_date, company):
|
2020-05-18 09:08:57 +00:00
|
|
|
validate_filters(from_date, to_date, company)
|
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
opportunities = frappe.get_all(
|
|
|
|
"Opportunity",
|
|
|
|
filters=[
|
|
|
|
["status", "in", ["Open", "Quotation", "Replied"]],
|
|
|
|
["company", "=", company],
|
|
|
|
["transaction_date", "Between", [from_date, to_date]],
|
2022-03-28 13:22:46 +00:00
|
|
|
],
|
2018-10-03 05:11:41 +00:00
|
|
|
fields=["currency", "sales_stage", "opportunity_amount", "probability"],
|
|
|
|
)
|
|
|
|
|
|
|
|
if opportunities:
|
|
|
|
default_currency = frappe.get_cached_value("Global Defaults", "None", "default_currency")
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-10-03 05:11:41 +00:00
|
|
|
cp_opportunities = [
|
|
|
|
dict(
|
|
|
|
x,
|
|
|
|
**{
|
|
|
|
"compound_amount": (
|
|
|
|
convert(x["opportunity_amount"], x["currency"], default_currency, to_date)
|
|
|
|
* x["probability"]
|
|
|
|
/ 100
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-10-03 05:11:41 +00:00
|
|
|
for x in opportunities
|
2022-03-28 13:22:46 +00:00
|
|
|
]
|
|
|
|
|
2022-04-06 10:10:41 +00:00
|
|
|
summary = {}
|
|
|
|
for sales_stage, rows in groupby(cp_opportunities, lambda o: o["sales_stage"]):
|
|
|
|
summary[sales_stage] = sum(flt(r["compound_amount"]) for r in rows)
|
2018-10-03 05:11:41 +00:00
|
|
|
|
2022-04-06 10:10:41 +00:00
|
|
|
result = {
|
|
|
|
"labels": list(summary.keys()),
|
|
|
|
"datasets": [{"name": _("Total Amount"), "values": list(summary.values()), "chartType": "bar"}],
|
|
|
|
}
|
2018-10-03 05:11:41 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
else:
|
|
|
|
return "empty"
|