feat: new Report "Lost Quotations" (#38309)

(cherry picked from commit 477d9fa87e)
This commit is contained in:
Raffael Meyer
2023-11-24 22:27:01 +01:00
committed by Mergify
parent 64d9c5d61c
commit ea2c3487a3
6 changed files with 228 additions and 76 deletions

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
frappe.query_reports["Lost Quotations"] = {
filters: [
{
fieldname: "company",
label: __("Company"),
fieldtype: "Link",
options: "Company",
default: frappe.defaults.get_user_default("Company"),
},
{
label: "Timespan",
fieldtype: "Select",
fieldname: "timespan",
options: [
"Last Week",
"Last Month",
"Last Quarter",
"Last 6 months",
"Last Year",
"This Week",
"This Month",
"This Quarter",
"This Year",
],
default: "This Year",
reqd: 1,
},
{
fieldname: "group_by",
label: __("Group By"),
fieldtype: "Select",
options: ["Lost Reason", "Competitor"],
default: "Lost Reason",
reqd: 1,
},
],
};

View File

@@ -0,0 +1,30 @@
{
"add_total_row": 0,
"columns": [],
"creation": "2023-11-23 18:00:19.141922",
"disabled": 0,
"docstatus": 0,
"doctype": "Report",
"filters": [],
"idx": 0,
"is_standard": "Yes",
"letter_head": null,
"letterhead": null,
"modified": "2023-11-23 19:27:28.854108",
"modified_by": "Administrator",
"module": "Selling",
"name": "Lost Quotations",
"owner": "Administrator",
"prepared_report": 0,
"ref_doctype": "Quotation",
"report_name": "Lost Quotations",
"report_type": "Script Report",
"roles": [
{
"role": "Sales User"
},
{
"role": "Sales Manager"
}
]
}

View File

@@ -0,0 +1,98 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from typing import Literal
import frappe
from frappe import _
from frappe.model.docstatus import DocStatus
from frappe.query_builder.functions import Coalesce, Count, Round, Sum
from frappe.utils.data import get_timespan_date_range
def execute(filters=None):
columns = get_columns(filters.get("group_by"))
from_date, to_date = get_timespan_date_range(filters.get("timespan").lower())
data = get_data(filters.get("company"), from_date, to_date, filters.get("group_by"))
return columns, data
def get_columns(group_by: Literal["Lost Reason", "Competitor"]):
return [
{
"fieldname": "lost_reason" if group_by == "Lost Reason" else "competitor",
"label": _("Lost Reason") if group_by == "Lost Reason" else _("Competitor"),
"fieldtype": "Link",
"options": "Quotation Lost Reason" if group_by == "Lost Reason" else "Competitor",
"width": 200,
},
{
"filedname": "lost_quotations",
"label": _("Lost Quotations"),
"fieldtype": "Int",
"width": 150,
},
{
"filedname": "lost_quotations_pct",
"label": _("Lost Quotations %"),
"fieldtype": "Percent",
"width": 200,
},
{
"fieldname": "lost_value",
"label": _("Lost Value"),
"fieldtype": "Currency",
"width": 150,
},
{
"filedname": "lost_value_pct",
"label": _("Lost Value %"),
"fieldtype": "Percent",
"width": 200,
},
]
def get_data(
company: str, from_date: str, to_date: str, group_by: Literal["Lost Reason", "Competitor"]
):
"""Return quotation value grouped by lost reason or competitor"""
if group_by == "Lost Reason":
fieldname = "lost_reason"
dimension = frappe.qb.DocType("Quotation Lost Reason Detail")
elif group_by == "Competitor":
fieldname = "competitor"
dimension = frappe.qb.DocType("Competitor Detail")
else:
frappe.throw(_("Invalid Group By"))
q = frappe.qb.DocType("Quotation")
lost_quotation_condition = (
(q.status == "Lost")
& (q.docstatus == DocStatus.submitted())
& (q.transaction_date >= from_date)
& (q.transaction_date <= to_date)
& (q.company == company)
)
from_lost_quotations = frappe.qb.from_(q).where(lost_quotation_condition)
total_quotations = from_lost_quotations.select(Count(q.name))
total_value = from_lost_quotations.select(Sum(q.base_net_total))
query = (
frappe.qb.from_(q)
.select(
Coalesce(dimension[fieldname], _("Not Specified")),
Count(q.name).distinct(),
Round((Count(q.name).distinct() / total_quotations * 100), 2),
Sum(q.base_net_total),
Round((Sum(q.base_net_total) / total_value * 100), 2),
)
.left_join(dimension)
.on(dimension.parent == q.name)
.where(lost_quotation_condition)
.groupby(dimension[fieldname])
)
return query.run()