From d543830a59d1a87bf6de83244303347ac168e769 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Sat, 9 May 2020 21:56:53 +0530 Subject: [PATCH 01/11] feat(Selling): Added Territory wise treeview to 'Customer Acquistion and Loyalty' report --- .../customer_acquisition_and_loyalty.js | 20 +- .../customer_acquisition_and_loyalty.py | 239 ++++++++++++++---- erpnext/setup/doctype/territory/territory.py | 5 +- 3 files changed, 212 insertions(+), 52 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js index a854fa9969..654614b4bb 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js @@ -3,6 +3,13 @@ frappe.query_reports["Customer Acquisition and Loyalty"] = { "filters": [ + { + "fieldname":"view_type", + "label": __("View Type"), + "fieldtype": "Select", + "default": "Time Series", + "options": ["Time Series", "Territory Tree"] + }, { "fieldname":"company", "label": __("Company"), @@ -24,6 +31,13 @@ frappe.query_reports["Customer Acquisition and Loyalty"] = { "fieldtype": "Date", "default": frappe.defaults.get_user_default("year_end_date"), "reqd": 1 - }, - ] -} + } + ], + 'formatter': function(value, row, column, data, default_formatter) { + value = default_formatter(value, row, column, data); + if (data && data.bold) { + value = value.bold(); + } + return value + } +} \ No newline at end of file diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index aa57665a81..f2033f1fff 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -2,65 +2,210 @@ # License: GNU General Public License v3. See license.txt from __future__ import unicode_literals +import calendar import frappe from frappe import _ -from frappe.utils import getdate, cint, cstr -import calendar +from frappe.utils import cint, cstr def execute(filters=None): - # key yyyy-mm - new_customers_in = {} - repeat_customers_in = {} - customers = [] - company_condition = "" + common_columns = [ + { + 'label': _('New Customers'), + 'fieldname': 'new_customers', + 'fieldtype': 'Int', + 'default': 0, + 'width': 100 + }, + { + 'label': _('Repeat Customers'), + 'fieldname': 'repeat_customers', + 'fieldtype': 'Int', + 'default': 0, + 'width': 100 + }, + { + 'label': _('Total'), + 'fieldname': 'total', + 'fieldtype': 'Int', + 'default': 0, + 'width': 100 + }, + { + 'label': _('New Customer Revenue'), + 'fieldname': 'new_customer_revenue', + 'fieldtype': 'Currency', + 'default': 0.0, + 'width': 150 + }, + { + 'label': _('Repeat Customer Revenue'), + 'fieldname': 'repeat_customer_revenue', + 'fieldtype': 'Currency', + 'default': 0.0, + 'width': 150 + }, + { + 'label': _('Total Revenue'), + 'fieldname': 'total_revenue', + 'fieldtype': 'Currency', + 'default': 0.0, + 'width': 150 + } + ] + if filters.get('view_type') == 'Territory Tree': + return get_data_by_territory(filters, common_columns) + else: + return get_data_by_time(filters, common_columns) - if filters.get("company"): - company_condition = ' and company=%(company)s' +def get_data_by_time(filters, common_columns): + # key yyyy-mm + columns = [ + { + 'label': 'Year', + 'fieldname': 'year', + 'fieldtype': 'Data', + 'width': 100 + }, + { + 'label': 'Month', + 'fieldname': 'month', + 'fieldtype': 'Data', + 'width': 100 + }, + ] + columns += common_columns - for si in frappe.db.sql("""select posting_date, customer, base_grand_total from `tabSales Invoice` - where docstatus=1 and posting_date <= %(to_date)s - {company_condition} order by posting_date""".format(company_condition=company_condition), - filters, as_dict=1): + customers_in = get_customer_stats(filters) - key = si.posting_date.strftime("%Y-%m") - if not si.customer in customers: - new_customers_in.setdefault(key, [0, 0.0]) - new_customers_in[key][0] += 1 - new_customers_in[key][1] += si.base_grand_total - customers.append(si.customer) - else: - repeat_customers_in.setdefault(key, [0, 0.0]) - repeat_customers_in[key][0] += 1 - repeat_customers_in[key][1] += si.base_grand_total + # time series + from_year, from_month, temp = filters.get('from_date').split('-') + to_year, to_month, temp = filters.get('to_date').split('-') - # time series - from_year, from_month, temp = filters.get("from_date").split("-") - to_year, to_month, temp = filters.get("to_date").split("-") + from_year, from_month, to_year, to_month = \ + cint(from_year), cint(from_month), cint(to_year), cint(to_month) - from_year, from_month, to_year, to_month = \ - cint(from_year), cint(from_month), cint(to_year), cint(to_month) + out = [] + for year in range(from_year, to_year+1): + for month in range(from_month if year==from_year else 1, (to_month+1) if year==to_year else 13): + key = '{year}-{month:02d}'.format(year=year, month=month) + data = customers_in.get(key) + new = data['new'] if data else [0, 0.0] + repeat = data['repeat'] if data else [0, 0.0] + out.append({ + 'year': cstr(year), + 'month': calendar.month_name[month], + 'new_customers': new[0], + 'repeat_customers': repeat[0], + 'total': new[0] + repeat[0], + 'new_customer_revenue': new[1], + 'repeat_customer_revenue': repeat[1], + 'total_revenue': new[1] + repeat[1] + }) + return columns, out - out = [] - for year in range(from_year, to_year+1): - for month in range(from_month if year==from_year else 1, (to_month+1) if year==to_year else 13): - key = "{year}-{month:02d}".format(year=year, month=month) +def get_data_by_territory(filters, common_columns): + columns = [{ + 'label': 'Territory', + 'fieldname': 'territory', + 'fieldtype': 'Link', + 'options': 'Territory', + 'width': 150 + }] + columns += common_columns - new = new_customers_in.get(key, [0,0.0]) - repeat = repeat_customers_in.get(key, [0,0.0]) + customers_in = get_customer_stats(filters, tree_view=True) - out.append([cstr(year), calendar.month_name[month], - new[0], repeat[0], new[0] + repeat[0], - new[1], repeat[1], new[1] + repeat[1]]) + territory_dict = {} + for t in frappe.db.sql('''SELECT name, lft, parent_territory, is_group FROM `tabTerritory` ORDER BY lft''', as_dict=1): + territory_dict.update({ + t.name: { + 'parent': t.parent_territory, + 'is_group': t.is_group + } + }) - return [ - _("Year") + "::100", - _("Month") + "::100", - _("New Customers") + ":Int:100", - _("Repeat Customers") + ":Int:100", - _("Total") + ":Int:100", - _("New Customer Revenue") + ":Currency:150", - _("Repeat Customer Revenue") + ":Currency:150", - _("Total Revenue") + ":Currency:150" - ], out + depth_map = frappe._dict() + for name, info in territory_dict.items(): + default = depth_map.get(info['parent']) + 1 if info['parent'] else 0 + depth_map.setdefault(name, default) + data = [] + for name, indent in depth_map.items(): + condition = customers_in.get(name) + new = customers_in[name]['new'] if condition else [0, 0.0] + repeat = customers_in[name]['repeat'] if condition else [0, 0.0] + temp = { + 'territory': name, + 'indent': indent, + 'new_customers': new[0], + 'repeat_customers': repeat[0], + 'total': new[0] + repeat[0], + 'new_customer_revenue': new[1], + 'repeat_customer_revenue': repeat[1], + 'total_revenue': new[1] + repeat[1], + 'bold': 0 if condition else 1 + } + data.append(temp) + node_list = [x for x in territory_dict.keys() if territory_dict[x]['is_group'] == 0] + root_node = [x for x in territory_dict.keys() if territory_dict[x]['parent'] is None][0] + for node in node_list: + data = update_groups(node, data, root_node, territory_dict) + + for group in [x for x in territory_dict.keys() if territory_dict[x]['parent'] == root_node]: + group_data = [x for x in data if x['territory'] == group][0] + root_data = [x for x in data if x['territory'] == root_node][0] + for key in group_data.keys(): + if key not in ['indent', 'territory', 'bold']: + root_data[key] += group_data[key] + + return columns, data, None, None, None, 1 + +def update_groups(node, data, root_node, territory_dict): + ''' Adds values of child territories to parent node except root ''' + parent_node = territory_dict[node]['parent'] + if parent_node != root_node and parent_node: + node_data = [x for x in data if x['territory'] == node][0] + parent_data = [x for x in data if x['territory'] == parent_node][0] + for key in parent_data.keys(): + if key not in ['indent', 'territory', 'bold']: + parent_data[key] += node_data[key] + return update_groups(parent_node, data, root_node, territory_dict) + else: + return data + +def get_customer_stats(filters, tree_view=False): + ''' Calculates number of new and repeated customers ''' + company_condition = '' + if filters.get('company'): + company_condition = ' and company=%(company)s' + + customers = [] + customers_in = {} + new_customers_in = {} + repeat_customers_in = {} + + for si in frappe.db.sql('''select territory, posting_date, customer, base_grand_total from `tabSales Invoice` + where docstatus=1 and posting_date <= %(to_date)s and posting_date >= %(from_date)s + {company_condition} order by posting_date'''.format(company_condition=company_condition), + filters, as_dict=1): + if tree_view: + key = si.territory + else: + key = si.posting_date.strftime('%Y-%m') + if not si.customer in customers: + new_customers_in.setdefault(key, [0, 0.0]) + new_customers_in[key][0] += 1 + new_customers_in[key][1] += si.base_grand_total + customers.append(si.customer) + else: + repeat_customers_in.setdefault(key, [0, 0.0]) + repeat_customers_in[key][0] += 1 + repeat_customers_in[key][1] += si.base_grand_total + customers_in.update({ + key: { + 'new': new_customers_in[key] if new_customers_in.get(key) else [0, 0.0], + 'repeat': repeat_customers_in[key] if repeat_customers_in.get(key) else [0, 0.0], + } + }) + return customers_in diff --git a/erpnext/setup/doctype/territory/territory.py b/erpnext/setup/doctype/territory/territory.py index 095bd1c179..4f2ab70b2c 100644 --- a/erpnext/setup/doctype/territory/territory.py +++ b/erpnext/setup/doctype/territory/territory.py @@ -3,8 +3,6 @@ from __future__ import unicode_literals import frappe - - from frappe.utils import flt from frappe import _ @@ -14,6 +12,9 @@ class Territory(NestedSet): nsm_parent_field = 'parent_territory' def validate(self): + if frappe.db.sql("SELECT COUNT(name) FROM `tabTerritory` WHERE parent IS NULL")[0][0] > 1: + frappe.throw('Only one Root Territory is allowed, please select a Parent Territory!') + for d in self.get('targets') or []: if not flt(d.target_qty) and not flt(d.target_amount): frappe.throw(_("Either target qty or target amount is mandatory")) From b59f2780ebf974779b4b0236e67e3f1bbee7781a Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Sat, 9 May 2020 22:09:02 +0530 Subject: [PATCH 02/11] fix: renamed view types, added default --- .../customer_acquisition_and_loyalty.js | 7 ++++--- .../customer_acquisition_and_loyalty.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js index 654614b4bb..c24d2e2bdd 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js @@ -4,11 +4,12 @@ frappe.query_reports["Customer Acquisition and Loyalty"] = { "filters": [ { - "fieldname":"view_type", + "fieldname": "view_type", "label": __("View Type"), "fieldtype": "Select", - "default": "Time Series", - "options": ["Time Series", "Territory Tree"] + "options": ["Monthly", "Territory Wise"], + "default": "Monthly", + "reqd": 1 }, { "fieldname":"company", diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index f2033f1fff..d8cc763ed9 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -52,7 +52,7 @@ def execute(filters=None): 'width': 150 } ] - if filters.get('view_type') == 'Territory Tree': + if filters.get('view_type') == 'Territory Wise': return get_data_by_territory(filters, common_columns) else: return get_data_by_time(filters, common_columns) From 7cbc902d90a3f43b985c9c12abbf0ab8caf82dc2 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Sat, 9 May 2020 22:35:28 +0530 Subject: [PATCH 03/11] removed validation for root node in territory, codacy recommended changed --- .../customer_acquisition_and_loyalty.js | 2 +- .../customer_acquisition_and_loyalty.py | 4 ++-- erpnext/setup/doctype/territory/territory.py | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js index c24d2e2bdd..d93ffb7266 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.js @@ -39,6 +39,6 @@ frappe.query_reports["Customer Acquisition and Loyalty"] = { if (data && data.bold) { value = value.bold(); } - return value + return value; } } \ No newline at end of file diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index d8cc763ed9..0121a8267f 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -162,7 +162,7 @@ def get_data_by_territory(filters, common_columns): return columns, data, None, None, None, 1 def update_groups(node, data, root_node, territory_dict): - ''' Adds values of child territories to parent node except root ''' + ''' Adds values of child territories to parent node except root. ''' parent_node = territory_dict[node]['parent'] if parent_node != root_node and parent_node: node_data = [x for x in data if x['territory'] == node][0] @@ -175,7 +175,7 @@ def update_groups(node, data, root_node, territory_dict): return data def get_customer_stats(filters, tree_view=False): - ''' Calculates number of new and repeated customers ''' + ''' Calculates number of new and repeated customers. ''' company_condition = '' if filters.get('company'): company_condition = ' and company=%(company)s' diff --git a/erpnext/setup/doctype/territory/territory.py b/erpnext/setup/doctype/territory/territory.py index 4f2ab70b2c..808b5386ab 100644 --- a/erpnext/setup/doctype/territory/territory.py +++ b/erpnext/setup/doctype/territory/territory.py @@ -12,8 +12,6 @@ class Territory(NestedSet): nsm_parent_field = 'parent_territory' def validate(self): - if frappe.db.sql("SELECT COUNT(name) FROM `tabTerritory` WHERE parent IS NULL")[0][0] > 1: - frappe.throw('Only one Root Territory is allowed, please select a Parent Territory!') for d in self.get('targets') or []: if not flt(d.target_qty) and not flt(d.target_amount): From 5ec5584319e18341e839d354bd251b9b7a382ca7 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Sun, 10 May 2020 00:24:43 +0530 Subject: [PATCH 04/11] In treeview, bold only for root territory, looks cleaner --- .../customer_acquisition_and_loyalty.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index 0121a8267f..b7bb021056 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -143,7 +143,7 @@ def get_data_by_territory(filters, common_columns): 'new_customer_revenue': new[1], 'repeat_customer_revenue': repeat[1], 'total_revenue': new[1] + repeat[1], - 'bold': 0 if condition else 1 + 'bold': 0 if indent else 1 } data.append(temp) node_list = [x for x in territory_dict.keys() if territory_dict[x]['is_group'] == 0] From 500dff63e764d7a679747546f15f1ee671a2fdc8 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Sun, 10 May 2020 14:53:59 +0530 Subject: [PATCH 05/11] fix: adjusted width of colums to see full column names, also fixes #21556 --- .../customer_acquisition_and_loyalty.py | 12 ++++---- .../territory_wise_sales.py | 30 +++++++++++-------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index b7bb021056..6e3f397fd6 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -14,21 +14,21 @@ def execute(filters=None): 'fieldname': 'new_customers', 'fieldtype': 'Int', 'default': 0, - 'width': 100 + 'width': 150 }, { 'label': _('Repeat Customers'), 'fieldname': 'repeat_customers', 'fieldtype': 'Int', 'default': 0, - 'width': 100 + 'width': 150 }, { 'label': _('Total'), 'fieldname': 'total', 'fieldtype': 'Int', 'default': 0, - 'width': 100 + 'width': 150 }, { 'label': _('New Customer Revenue'), @@ -52,10 +52,10 @@ def execute(filters=None): 'width': 150 } ] - if filters.get('view_type') == 'Territory Wise': - return get_data_by_territory(filters, common_columns) - else: + if filters.get('view_type') == 'Monthly': return get_data_by_time(filters, common_columns) + else: + return get_data_by_territory(filters, common_columns) def get_data_by_time(filters, common_columns): # key yyyy-mm 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 f2db478686..e883500170 100644 --- a/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py +++ b/erpnext/selling/report/territory_wise_sales/territory_wise_sales.py @@ -20,31 +20,36 @@ def get_columns(): "label": _("Territory"), "fieldname": "territory", "fieldtype": "Link", - "options": "Territory" + "options": "Territory", + "width": 150 }, { "label": _("Opportunity Amount"), "fieldname": "opportunity_amount", "fieldtype": "Currency", - "options": currency + "options": currency, + "width": 150 }, { "label": _("Quotation Amount"), "fieldname": "quotation_amount", "fieldtype": "Currency", - "options": currency + "options": currency, + "width": 150 }, { "label": _("Order Amount"), "fieldname": "order_amount", "fieldtype": "Currency", - "options": currency + "options": currency, + "width": 150 }, { "label": _("Billing Amount"), "fieldname": "billing_amount", "fieldtype": "Currency", - "options": currency + "options": currency, + "width": 150 } ] @@ -62,8 +67,7 @@ def get_data(filters=None): territory_opportunities = list(filter(lambda x: x.territory == territory.name, opportunities)) t_opportunity_names = [] if territory_opportunities: - t_opportunity_names = [t.name for t in territory_opportunities] - + t_opportunity_names = [t.name for t in territory_opportunities] territory_quotations = [] if t_opportunity_names and quotations: territory_quotations = list(filter(lambda x: x.opportunity in t_opportunity_names, quotations)) @@ -76,7 +80,7 @@ def get_data(filters=None): list(filter(lambda x: x.quotation in t_quotation_names, sales_orders)) t_order_names = [] if territory_orders: - t_order_names = [t.name for t in territory_orders] + t_order_names = [t.name for t in territory_orders] territory_invoices = list(filter(lambda x: x.sales_order in t_order_names, sales_invoices)) if t_order_names and sales_invoices else [] @@ -96,12 +100,12 @@ def get_opportunities(filters): if filters.get('transaction_date'): conditions = " WHERE transaction_date between {0} and {1}".format( - frappe.db.escape(filters['transaction_date'][0]), + frappe.db.escape(filters['transaction_date'][0]), frappe.db.escape(filters['transaction_date'][1])) - + if filters.company: if conditions: - conditions += " AND" + conditions += " AND" else: conditions += " WHERE" conditions += " company = %(company)s" @@ -115,7 +119,7 @@ def get_opportunities(filters): def get_quotations(opportunities): if not opportunities: return [] - + opportunity_names = [o.name for o in opportunities] return frappe.db.sql(""" @@ -155,5 +159,5 @@ def _get_total(doclist, amount_field="base_grand_total"): total = 0 for doc in doclist: total += doc.get(amount_field, 0) - + return total From 87776c335beb693c471608fa318a4997171f2dbd Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Mon, 11 May 2020 15:18:40 +0530 Subject: [PATCH 06/11] code improvements --- .../customer_acquisition_and_loyalty.py | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index 6e3f397fd6..38fbd60008 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -14,42 +14,42 @@ def execute(filters=None): 'fieldname': 'new_customers', 'fieldtype': 'Int', 'default': 0, - 'width': 150 + 'width': 125 }, { 'label': _('Repeat Customers'), 'fieldname': 'repeat_customers', 'fieldtype': 'Int', 'default': 0, - 'width': 150 + 'width': 125 }, { 'label': _('Total'), 'fieldname': 'total', 'fieldtype': 'Int', 'default': 0, - 'width': 150 + 'width': 100 }, { 'label': _('New Customer Revenue'), 'fieldname': 'new_customer_revenue', 'fieldtype': 'Currency', 'default': 0.0, - 'width': 150 + 'width': 175 }, { 'label': _('Repeat Customer Revenue'), 'fieldname': 'repeat_customer_revenue', 'fieldtype': 'Currency', 'default': 0.0, - 'width': 150 + 'width': 175 }, { 'label': _('Total Revenue'), 'fieldname': 'total_revenue', 'fieldtype': 'Currency', 'default': 0.0, - 'width': 150 + 'width': 175 } ] if filters.get('view_type') == 'Monthly': @@ -148,13 +148,13 @@ def get_data_by_territory(filters, common_columns): data.append(temp) node_list = [x for x in territory_dict.keys() if territory_dict[x]['is_group'] == 0] root_node = [x for x in territory_dict.keys() if territory_dict[x]['parent'] is None][0] + root_data = [x for x in data if x['territory'] == root_node][0] for node in node_list: data = update_groups(node, data, root_node, territory_dict) for group in [x for x in territory_dict.keys() if territory_dict[x]['parent'] == root_node]: group_data = [x for x in data if x['territory'] == group][0] - root_data = [x for x in data if x['territory'] == root_node][0] for key in group_data.keys(): if key not in ['indent', 'territory', 'bold']: root_data[key] += group_data[key] @@ -162,7 +162,7 @@ def get_data_by_territory(filters, common_columns): return columns, data, None, None, None, 1 def update_groups(node, data, root_node, territory_dict): - ''' Adds values of child territories to parent node except root. ''' + """ Adds values of child territories to parent node except root. """ parent_node = territory_dict[node]['parent'] if parent_node != root_node and parent_node: node_data = [x for x in data if x['territory'] == node][0] @@ -175,37 +175,28 @@ def update_groups(node, data, root_node, territory_dict): return data def get_customer_stats(filters, tree_view=False): - ''' Calculates number of new and repeated customers. ''' + """ Calculates number of new and repeated customers. """ company_condition = '' if filters.get('company'): company_condition = ' and company=%(company)s' customers = [] customers_in = {} - new_customers_in = {} - repeat_customers_in = {} for si in frappe.db.sql('''select territory, posting_date, customer, base_grand_total from `tabSales Invoice` where docstatus=1 and posting_date <= %(to_date)s and posting_date >= %(from_date)s {company_condition} order by posting_date'''.format(company_condition=company_condition), filters, as_dict=1): - if tree_view: - key = si.territory - else: - key = si.posting_date.strftime('%Y-%m') + + key = si.territory if tree_view else si.posting_date.strftime('%Y-%m') + customers_in.setdefault(key, {'new': [0, 0.0], 'repeat': [0, 0.0]}) + if not si.customer in customers: - new_customers_in.setdefault(key, [0, 0.0]) - new_customers_in[key][0] += 1 - new_customers_in[key][1] += si.base_grand_total + customers_in[key]['new'][0] += 1 + customers_in[key]['new'][1] += si.base_grand_total customers.append(si.customer) else: - repeat_customers_in.setdefault(key, [0, 0.0]) - repeat_customers_in[key][0] += 1 - repeat_customers_in[key][1] += si.base_grand_total - customers_in.update({ - key: { - 'new': new_customers_in[key] if new_customers_in.get(key) else [0, 0.0], - 'repeat': repeat_customers_in[key] if repeat_customers_in.get(key) else [0, 0.0], - } - }) + customers_in[key]['repeat'][0] += 1 + customers_in[key]['repeat'][1] += si.base_grand_total + return customers_in From 411b12590648e602565829d6e5f26b57ce56b62c Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Tue, 12 May 2020 15:25:35 +0530 Subject: [PATCH 07/11] new parent updating logic, made requested changes --- .../customer_acquisition_and_loyalty.py | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py index 38fbd60008..88bd9c135d 100644 --- a/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py +++ b/erpnext/selling/report/customer_acquisition_and_loyalty/customer_acquisition_and_loyalty.py @@ -61,13 +61,13 @@ def get_data_by_time(filters, common_columns): # key yyyy-mm columns = [ { - 'label': 'Year', + 'label': _('Year'), 'fieldname': 'year', 'fieldtype': 'Data', 'width': 100 }, { - 'label': 'Month', + 'label': _('Month'), 'fieldname': 'month', 'fieldtype': 'Data', 'width': 100 @@ -136,6 +136,7 @@ def get_data_by_territory(filters, common_columns): repeat = customers_in[name]['repeat'] if condition else [0, 0.0] temp = { 'territory': name, + 'parent_territory': territory_dict[name]['parent'], 'indent': indent, 'new_customers': new[0], 'repeat_customers': repeat[0], @@ -146,34 +147,18 @@ def get_data_by_territory(filters, common_columns): 'bold': 0 if indent else 1 } data.append(temp) - node_list = [x for x in territory_dict.keys() if territory_dict[x]['is_group'] == 0] - root_node = [x for x in territory_dict.keys() if territory_dict[x]['parent'] is None][0] - root_data = [x for x in data if x['territory'] == root_node][0] - for node in node_list: - data = update_groups(node, data, root_node, territory_dict) + loop_data = sorted(data, key=lambda k: k['indent'], reverse=True) - for group in [x for x in territory_dict.keys() if territory_dict[x]['parent'] == root_node]: - group_data = [x for x in data if x['territory'] == group][0] - for key in group_data.keys(): - if key not in ['indent', 'territory', 'bold']: - root_data[key] += group_data[key] + for ld in loop_data: + if ld['parent_territory']: + parent_data = [x for x in data if x['territory'] == ld['parent_territory']][0] + for key in parent_data.keys(): + if key not in ['indent', 'territory', 'parent_territory', 'bold']: + parent_data[key] += ld[key] return columns, data, None, None, None, 1 -def update_groups(node, data, root_node, territory_dict): - """ Adds values of child territories to parent node except root. """ - parent_node = territory_dict[node]['parent'] - if parent_node != root_node and parent_node: - node_data = [x for x in data if x['territory'] == node][0] - parent_data = [x for x in data if x['territory'] == parent_node][0] - for key in parent_data.keys(): - if key not in ['indent', 'territory', 'bold']: - parent_data[key] += node_data[key] - return update_groups(parent_node, data, root_node, territory_dict) - else: - return data - def get_customer_stats(filters, tree_view=False): """ Calculates number of new and repeated customers. """ company_condition = '' From 673e704bb5daefbc3c128bafee2f7051eb60d7cb Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Tue, 12 May 2020 19:09:27 +0530 Subject: [PATCH 08/11] typo in error message in loan_security_pledge.py --- .../doctype/loan_security_pledge/loan_security_pledge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py b/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py index f97e5965a5..961c05c9c1 100644 --- a/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py +++ b/erpnext/loan_management/doctype/loan_security_pledge/loan_security_pledge.py @@ -38,7 +38,7 @@ class LoanSecurityPledge(Document): for pledge in self.securities: if not pledge.qty and not pledge.amount: - frappe.throw(_("Qty or Amount is mandatroy for loan security")) + frappe.throw(_("Qty or Amount is mandatory for loan security!")) if not (self.loan_application and pledge.loan_security_price): pledge.loan_security_price = get_loan_security_price(pledge.loan_security) From bd7e5358857b66f5025e38ab3cf82d589dec6506 Mon Sep 17 00:00:00 2001 From: Afshan <33727827+AfshanKhan@users.noreply.github.com> Date: Wed, 13 May 2020 19:48:42 +0530 Subject: [PATCH 09/11] Fix: Set General Ledger 'Group By' filter as 'Group by Voucher(Consolidated)' when opened from Invoice (#21673) * fix for issue #21419 * changing group by filter to default Group by Voucher (Consolidated) --- erpnext/accounts/report/general_ledger/general_ledger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/report/general_ledger/general_ledger.js b/erpnext/accounts/report/general_ledger/general_ledger.js index 1188beaa0f..2aecd6b717 100644 --- a/erpnext/accounts/report/general_ledger/general_ledger.js +++ b/erpnext/accounts/report/general_ledger/general_ledger.js @@ -53,7 +53,7 @@ frappe.query_reports["General Ledger"] = { "label": __("Voucher No"), "fieldtype": "Data", on_change: function() { - frappe.query_report.set_filter_value('group_by', ""); + frappe.query_report.set_filter_value('group_by', "Group by Voucher (Consolidated)"); } }, { From dde39c3d1aa323072de5703b8c30eabe07b3960c Mon Sep 17 00:00:00 2001 From: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Date: Thu, 14 May 2020 12:09:36 +0530 Subject: [PATCH 10/11] chore: Drop Python2 support (#21704) * chore: Drop Python2 support * test: Fix test redundancy by removing countries 3 countries seems ennough to test coa template feature --- .travis.yml | 17 ++--------------- erpnext/setup/doctype/company/test_company.py | 4 +--- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 213445b806..77d427e5a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ -dist: trusty - language: python +dist: trusty git: depth: 1 @@ -14,21 +13,10 @@ addons: jobs: include: - - name: "Python 2.7 Server Side Test" - python: 2.7 - script: bench --site test_site run-tests --app erpnext --coverage - - name: "Python 3.6 Server Side Test" python: 3.6 script: bench --site test_site run-tests --app erpnext --coverage - - name: "Python 2.7 Patch Test" - python: 2.7 - before_script: - - wget http://build.erpnext.com/20171108_190013_955977f8_database.sql.gz - - bench --site test_site --force restore ~/frappe-bench/20171108_190013_955977f8_database.sql.gz - script: bench --site test_site migrate - - name: "Python 3.6 Patch Test" python: 3.6 before_script: @@ -40,8 +28,7 @@ install: - cd ~ - nvm install 10 - - git clone https://github.com/frappe/bench --depth 1 - - pip install -e ./bench + - pip install frappe-bench - git clone https://github.com/frappe/frappe --branch $TRAVIS_BRANCH --depth 1 - bench init --skip-assets --frappe-path ~/frappe --python $(which python) frappe-bench diff --git a/erpnext/setup/doctype/company/test_company.py b/erpnext/setup/doctype/company/test_company.py index b37cc17ba9..29f6c3731d 100644 --- a/erpnext/setup/doctype/company/test_company.py +++ b/erpnext/setup/doctype/company/test_company.py @@ -47,9 +47,7 @@ class TestCompany(unittest.TestCase): frappe.delete_doc("Company", "COA from Existing Company") def test_coa_based_on_country_template(self): - countries = ["India", "Brazil", "United Arab Emirates", "Canada", "Germany", "France", - "Guatemala", "Indonesia", "Italy", "Mexico", "Nicaragua", "Netherlands", "Singapore", - "Brazil", "Argentina", "Hungary", "Taiwan"] + countries = ["Canada", "Germany", "France"] for country in countries: templates = get_charts_for_country(country) From 39cb749f955f0fe40dc2289cdbc32bcb7f9ba939 Mon Sep 17 00:00:00 2001 From: prssanna Date: Thu, 14 May 2020 18:25:58 +0530 Subject: [PATCH 11/11] fix: add heatmap_year parameter to get --- .../account_balance_timeline/account_balance_timeline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/dashboard_chart_source/account_balance_timeline/account_balance_timeline.py b/erpnext/accounts/dashboard_chart_source/account_balance_timeline/account_balance_timeline.py index 5decccb486..39bf4b053a 100644 --- a/erpnext/accounts/dashboard_chart_source/account_balance_timeline/account_balance_timeline.py +++ b/erpnext/accounts/dashboard_chart_source/account_balance_timeline/account_balance_timeline.py @@ -14,7 +14,7 @@ from frappe.utils.nestedset import get_descendants_of @frappe.whitelist() @cache_source def get(chart_name = None, chart = None, no_cache = None, filters = None, from_date = None, - to_date = None, timespan = None, time_interval = None): + to_date = None, timespan = None, time_interval = None, heatmap_year = None): if chart_name: chart = frappe.get_doc('Dashboard Chart', chart_name) else: