code improvements

This commit is contained in:
Abhishek Balam 2020-05-11 15:18:40 +05:30
parent 500dff63e7
commit 87776c335b

View File

@ -14,42 +14,42 @@ def execute(filters=None):
'fieldname': 'new_customers', 'fieldname': 'new_customers',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 150 'width': 125
}, },
{ {
'label': _('Repeat Customers'), 'label': _('Repeat Customers'),
'fieldname': 'repeat_customers', 'fieldname': 'repeat_customers',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 150 'width': 125
}, },
{ {
'label': _('Total'), 'label': _('Total'),
'fieldname': 'total', 'fieldname': 'total',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 150 'width': 100
}, },
{ {
'label': _('New Customer Revenue'), 'label': _('New Customer Revenue'),
'fieldname': 'new_customer_revenue', 'fieldname': 'new_customer_revenue',
'fieldtype': 'Currency', 'fieldtype': 'Currency',
'default': 0.0, 'default': 0.0,
'width': 150 'width': 175
}, },
{ {
'label': _('Repeat Customer Revenue'), 'label': _('Repeat Customer Revenue'),
'fieldname': 'repeat_customer_revenue', 'fieldname': 'repeat_customer_revenue',
'fieldtype': 'Currency', 'fieldtype': 'Currency',
'default': 0.0, 'default': 0.0,
'width': 150 'width': 175
}, },
{ {
'label': _('Total Revenue'), 'label': _('Total Revenue'),
'fieldname': 'total_revenue', 'fieldname': 'total_revenue',
'fieldtype': 'Currency', 'fieldtype': 'Currency',
'default': 0.0, 'default': 0.0,
'width': 150 'width': 175
} }
] ]
if filters.get('view_type') == 'Monthly': if filters.get('view_type') == 'Monthly':
@ -148,13 +148,13 @@ def get_data_by_territory(filters, common_columns):
data.append(temp) data.append(temp)
node_list = [x for x in territory_dict.keys() if territory_dict[x]['is_group'] == 0] 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_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: for node in node_list:
data = update_groups(node, data, root_node, territory_dict) 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]: 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] 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(): for key in group_data.keys():
if key not in ['indent', 'territory', 'bold']: if key not in ['indent', 'territory', 'bold']:
root_data[key] += group_data[key] 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 return columns, data, None, None, None, 1
def update_groups(node, data, root_node, territory_dict): 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'] parent_node = territory_dict[node]['parent']
if parent_node != root_node and parent_node: if parent_node != root_node and parent_node:
node_data = [x for x in data if x['territory'] == node][0] 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 return data
def get_customer_stats(filters, tree_view=False): def get_customer_stats(filters, tree_view=False):
''' Calculates number of new and repeated customers. ''' """ Calculates number of new and repeated customers. """
company_condition = '' company_condition = ''
if filters.get('company'): if filters.get('company'):
company_condition = ' and company=%(company)s' company_condition = ' and company=%(company)s'
customers = [] customers = []
customers_in = {} 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` 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 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), {company_condition} order by posting_date'''.format(company_condition=company_condition),
filters, as_dict=1): filters, as_dict=1):
if tree_view:
key = si.territory key = si.territory if tree_view else si.posting_date.strftime('%Y-%m')
else: customers_in.setdefault(key, {'new': [0, 0.0], 'repeat': [0, 0.0]})
key = si.posting_date.strftime('%Y-%m')
if not si.customer in customers: if not si.customer in customers:
new_customers_in.setdefault(key, [0, 0.0]) customers_in[key]['new'][0] += 1
new_customers_in[key][0] += 1 customers_in[key]['new'][1] += si.base_grand_total
new_customers_in[key][1] += si.base_grand_total
customers.append(si.customer) customers.append(si.customer)
else: else:
repeat_customers_in.setdefault(key, [0, 0.0]) customers_in[key]['repeat'][0] += 1
repeat_customers_in[key][0] += 1 customers_in[key]['repeat'][1] += si.base_grand_total
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 return customers_in