From 75adb808e5dc4baf5c704be1facf5c2fad87c9c8 Mon Sep 17 00:00:00 2001 From: 0Pranav Date: Fri, 31 Jan 2020 10:35:47 +0530 Subject: [PATCH] various fixes from review - Rename the report to title case - Added company filter - fixed checks in filter and += operator for total --- .../territory_wise_sales.js | 8 +++++- .../territory_wise_sales.json | 18 ++++++++----- .../territory_wise_sales.py | 26 ++++++++++++------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.js b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.js index 12f53048134..767d5290ebe 100644 --- a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.js +++ b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.js @@ -10,7 +10,13 @@ frappe.query_reports["Territory wise Sales"] = { fieldname:"expected_closing_date", label: __("Expected Closing Date"), fieldtype: "DateRange", - default: [frappe.datetime.add_months(frappe.datetime.get_today(),-1), frappe.datetime.get_today()] + default: [frappe.datetime.add_months(frappe.datetime.get_today(),-1), frappe.datetime.get_today()], + }, + { + fieldname: "company", + label: __("Company"), + fieldtype: "Link", + options: "Company", } ] }; diff --git a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.json b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.json index 88dfe8a1298..b98d1a2f329 100644 --- a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.json +++ b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.json @@ -1,21 +1,27 @@ { "add_total_row": 0, - "creation": "2020-01-10 13:02:23.312515", + "creation": "2020-01-31 10:34:33.319047", "disable_prepared_report": 0, "disabled": 0, "docstatus": 0, "doctype": "Report", "idx": 0, "is_standard": "Yes", - "modified": "2020-01-14 14:50:33.863423", + "modified": "2020-01-31 10:34:33.319047", "modified_by": "Administrator", "module": "Selling", - "name": "Territory wise Sales", + "name": "Territory-wise Sales", "owner": "Administrator", "prepared_report": 0, - "query": "", "ref_doctype": "Opportunity", - "report_name": "Territory wise Sales", + "report_name": "Territory-wise Sales", "report_type": "Script Report", - "roles": [] + "roles": [ + { + "role": "Sales User" + }, + { + "role": "Sales Manager" + } + ] } \ No newline at end of file diff --git a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py index 12582a6e95a..c8a63ee29f1 100644 --- a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py +++ b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py @@ -57,16 +57,16 @@ def get_data(filters=None): sales_invoices = get_sales_invoice(sales_orders) for territory in frappe.get_all("Territory"): - territory_opportunities = list(filter(lambda x: x.territory == territory.name, opportunities)) if opportunities and opportunities else None + territory_opportunities = list(filter(lambda x: x.territory == territory.name, opportunities)) if opportunities and opportunities else [] t_opportunity_names = [t.name for t in territory_opportunities] if territory_opportunities else None - territory_quotations = list(filter(lambda x: x.opportunity in t_opportunity_names, quotations)) if t_opportunity_names and quotations else None + territory_quotations = list(filter(lambda x: x.opportunity in t_opportunity_names, quotations)) if t_opportunity_names and quotations else [] t_quotation_names = [t.name for t in territory_quotations] if territory_quotations else None - territory_orders = list(filter(lambda x: x.quotation in t_quotation_names, sales_orders)) if t_quotation_names and sales_orders else None + territory_orders = list(filter(lambda x: x.quotation in t_quotation_names, sales_orders)) if t_quotation_names and sales_orders else [] t_order_names = [t.name for t in territory_orders] if territory_orders else None - territory_invoices = list(filter(lambda x: x.sales_order in t_order_names, sales_invoices)) if t_order_names and sales_invoices else None + territory_invoices = list(filter(lambda x: x.sales_order in t_order_names, sales_invoices)) if t_order_names and sales_invoices else [] territory_data = { "territory": territory.name, @@ -84,11 +84,19 @@ def get_opportunities(filters): if filters.from_date and filters.to_date: conditions = " WHERE expected_closing between %(from_date)s and %(to_date)s" + + if filters.company: + if conditions: + conditions += " AND" + else: + conditions += " WHERE" + conditions += " company = %(company)s" + return frappe.db.sql(""" SELECT name, territory, opportunity_amount FROM `tabOpportunity` {0} - """.format(conditions), filters, as_dict=1) + """.format(conditions), filters, as_dict=1) #nosec def get_quotations(opportunities): if not opportunities: @@ -100,7 +108,7 @@ def get_quotations(opportunities): SELECT `name`,`base_grand_total`, `opportunity` FROM `tabQuotation` WHERE docstatus=1 AND opportunity in ({0}) - """.format(', '.join(["%s"]*len(opportunity_names))), tuple(opportunity_names), as_dict=1) + """.format(', '.join(["%s"]*len(opportunity_names))), tuple(opportunity_names), as_dict=1) #nosec def get_sales_orders(quotations): if not quotations: @@ -112,7 +120,7 @@ def get_sales_orders(quotations): SELECT so.`name`, so.`base_grand_total`, soi.prevdoc_docname as quotation FROM `tabSales Order` so, `tabSales Order Item` soi WHERE so.docstatus=1 AND so.name = soi.parent AND soi.prevdoc_docname in ({0}) - """.format(', '.join(["%s"]*len(quotation_names))), tuple(quotation_names), as_dict=1) + """.format(', '.join(["%s"]*len(quotation_names))), tuple(quotation_names), as_dict=1) #nosec def get_sales_invoice(sales_orders): if not sales_orders: @@ -124,7 +132,7 @@ def get_sales_invoice(sales_orders): SELECT si.name, si.base_grand_total, sii.sales_order FROM `tabSales Invoice` si, `tabSales Invoice Item` sii WHERE si.docstatus=1 AND si.name = sii.parent AND sii.sales_order in ({0}) - """.format(', '.join(["%s"]*len(so_names))), tuple(so_names), as_dict=1) + """.format(', '.join(["%s"]*len(so_names))), tuple(so_names), as_dict=1) #nosec def _get_total(doclist, amount_field="base_grand_total"): if not doclist: @@ -132,6 +140,6 @@ def _get_total(doclist, amount_field="base_grand_total"): total = 0 for doc in doclist: - total = total + doc.get(amount_field, 0) + total += doc.get(amount_field, 0) return total