indent to tabs instead of spaces

This commit is contained in:
Abhishek Balam 2020-06-01 23:58:05 +05:30
parent 732f5c1867
commit 192d4c8810

View File

@ -8,179 +8,180 @@ from frappe import _
from frappe.utils import cint, cstr from frappe.utils import cint, cstr
def execute(filters=None): def execute(filters=None):
common_columns = [ common_columns = [
{ {
'label': _('New Customers'), 'label': _('New Customers'),
'fieldname': 'new_customers', 'fieldname': 'new_customers',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 125 'width': 125
}, },
{ {
'label': _('Repeat Customers'), 'label': _('Repeat Customers'),
'fieldname': 'repeat_customers', 'fieldname': 'repeat_customers',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 125 'width': 125
}, },
{ {
'label': _('Total'), 'label': _('Total'),
'fieldname': 'total', 'fieldname': 'total',
'fieldtype': 'Int', 'fieldtype': 'Int',
'default': 0, 'default': 0,
'width': 100 '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': 175 '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': 175 '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': 175 'width': 175
} }
] ]
if filters.get('view_type') == 'Monthly': if filters.get('view_type') == 'Monthly':
return get_data_by_time(filters, common_columns) return get_data_by_time(filters, common_columns)
else: else:
return get_data_by_territory(filters, common_columns) return get_data_by_territory(filters, common_columns)
def get_data_by_time(filters, common_columns): def get_data_by_time(filters, common_columns):
# key yyyy-mm # key yyyy-mm
columns = [ columns = [
{ {
'label': _('Year'), 'label': _('Year'),
'fieldname': 'year', 'fieldname': 'year',
'fieldtype': 'Data', 'fieldtype': 'Data',
'width': 100 'width': 100
}, },
{ {
'label': _('Month'), 'label': _('Month'),
'fieldname': 'month', 'fieldname': 'month',
'fieldtype': 'Data', 'fieldtype': 'Data',
'width': 100 'width': 100
}, },
] ]
columns += common_columns columns += common_columns
customers_in = get_customer_stats(filters) customers_in = get_customer_stats(filters)
# time series # time series
from_year, from_month, temp = filters.get('from_date').split('-') from_year, from_month, temp = filters.get('from_date').split('-')
to_year, to_month, temp = filters.get('to_date').split('-') to_year, to_month, temp = filters.get('to_date').split('-')
from_year, from_month, to_year, to_month = \ from_year, from_month, to_year, to_month = \
cint(from_year), cint(from_month), cint(to_year), cint(to_month) cint(from_year), cint(from_month), cint(to_year), cint(to_month)
out = [] out = []
for year in range(from_year, to_year+1): 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): 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) key = '{year}-{month:02d}'.format(year=year, month=month)
data = customers_in.get(key) data = customers_in.get(key)
new = data['new'] if data else [0, 0.0] new = data['new'] if data else [0, 0.0]
repeat = data['repeat'] if data else [0, 0.0] repeat = data['repeat'] if data else [0, 0.0]
out.append({ out.append({
'year': cstr(year), 'year': cstr(year),
'month': calendar.month_name[month], 'month': calendar.month_name[month],
'new_customers': new[0], 'new_customers': new[0],
'repeat_customers': repeat[0], 'repeat_customers': repeat[0],
'total': new[0] + repeat[0], 'total': new[0] + repeat[0],
'new_customer_revenue': new[1], 'new_customer_revenue': new[1],
'repeat_customer_revenue': repeat[1], 'repeat_customer_revenue': repeat[1],
'total_revenue': new[1] + repeat[1] 'total_revenue': new[1] + repeat[1]
}) })
return columns, out return columns, out
def get_data_by_territory(filters, common_columns): def get_data_by_territory(filters, common_columns):
columns = [{ columns = [{
'label': 'Territory', 'label': 'Territory',
'fieldname': 'territory', 'fieldname': 'territory',
'fieldtype': 'Link', 'fieldtype': 'Link',
'options': 'Territory', 'options': 'Territory',
'width': 150 'width': 150
}] }]
columns += common_columns columns += common_columns
customers_in = get_customer_stats(filters, tree_view=True) customers_in = get_customer_stats(filters, tree_view=True)
territory_dict = {} territory_dict = {}
for t in frappe.db.sql('''SELECT name, lft, parent_territory, is_group FROM `tabTerritory` ORDER BY lft''', as_dict=1): for t in frappe.db.sql('''SELECT name, lft, parent_territory, is_group FROM `tabTerritory` ORDER BY lft''', as_dict=1):
territory_dict.update({ territory_dict.update({
t.name: { t.name: {
'parent': t.parent_territory, 'parent': t.parent_territory,
'is_group': t.is_group 'is_group': t.is_group
} }
}) })
depth_map = frappe._dict() depth_map = frappe._dict()
for name, info in territory_dict.items(): for name, info in territory_dict.items():
default = depth_map.get(info['parent']) + 1 if info['parent'] else 0 default = depth_map.get(info['parent']) + 1 if info['parent'] else 0
depth_map.setdefault(name, default) depth_map.setdefault(name, default)
data = [] data = []
for name, indent in depth_map.items(): for name, indent in depth_map.items():
condition = customers_in.get(name) condition = customers_in.get(name)
new = customers_in[name]['new'] if condition else [0, 0.0] new = customers_in[name]['new'] if condition else [0, 0.0]
repeat = customers_in[name]['repeat'] if condition else [0, 0.0] repeat = customers_in[name]['repeat'] if condition else [0, 0.0]
temp = { temp = {
'territory': name, 'territory': name,
'parent_territory': territory_dict[name]['parent'], 'parent_territory': territory_dict[name]['parent'],
'indent': indent, 'indent': indent,
'new_customers': new[0], 'new_customers': new[0],
'repeat_customers': repeat[0], 'repeat_customers': repeat[0],
'total': new[0] + repeat[0], 'total': new[0] + repeat[0],
'new_customer_revenue': new[1], 'new_customer_revenue': new[1],
'repeat_customer_revenue': repeat[1], 'repeat_customer_revenue': repeat[1],
'total_revenue': new[1] + repeat[1], 'total_revenue': new[1] + repeat[1],
'bold': 0 if indent else 1 'bold': 0 if indent else 1
} }
data.append(temp) data.append(temp)
loop_data = sorted(data, key=lambda k: k['indent'], reverse=True) loop_data = sorted(data, key=lambda k: k['indent'], reverse=True)
for ld in loop_data: for ld in loop_data:
if ld['parent_territory']: if ld['parent_territory']:
parent_data = [x for x in data if x['territory'] == ld['parent_territory']][0] parent_data = [x for x in data if x['territory'] == ld['parent_territory']][0]
for key in parent_data.keys(): for key in parent_data.keys():
if key not in ['indent', 'territory', 'parent_territory', 'bold']: if key not in ['indent', 'territory', 'parent_territory', 'bold']:
parent_data[key] += ld[key] parent_data[key] += ld[key]
return columns, data, None, None, None, 1 return columns, data, None, None, None, 1
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 = {}
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 {company_condition} order by posting_date'''.format(company_condition=company_condition), where docstatus=1 and posting_date <= %(to_date)s
filters, as_dict=1): {company_condition} order by posting_date'''.format(company_condition=company_condition),
filters, as_dict=1):
key = si.territory if tree_view else 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]}) customers_in.setdefault(key, {'new': [0, 0.0], 'repeat': [0, 0.0]})
if not si.customer in customers: if not si.customer in customers:
customers_in[key]['new'][0] += 1 customers_in[key]['new'][0] += 1
customers_in[key]['new'][1] += si.base_grand_total customers_in[key]['new'][1] += si.base_grand_total
customers.append(si.customer) customers.append(si.customer)
else: else:
customers_in[key]['repeat'][0] += 1 customers_in[key]['repeat'][0] += 1
customers_in[key]['repeat'][1] += si.base_grand_total customers_in[key]['repeat'][1] += si.base_grand_total
return customers_in return customers_in