[company][dashboard] implement heatmap with linked doctypes (#14185)

* [company][dashboard] implement heatmap with linked doctypes

* [company][dashboard] cache transaction history data, schedule query daily
This commit is contained in:
Prateeksha Singh 2018-05-23 10:41:26 +05:30 committed by Nabin Hait
parent e03937fd6a
commit 2f69254e56
2 changed files with 2640 additions and 2531 deletions

View File

@ -358,6 +358,36 @@
"translatable": 0, "translatable": 0,
"unique": 0 "unique": 0
}, },
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "transactions_annual_history",
"fieldtype": "Code",
"hidden": 1,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Transactions Annual History",
"length": 0,
"no_copy": 1,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 1,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{ {
"allow_bulk_edit": 0, "allow_bulk_edit": 0,
"allow_on_submit": 0, "allow_on_submit": 0,
@ -2561,7 +2591,7 @@
"istable": 0, "istable": 0,
"max_attachments": 0, "max_attachments": 0,
"menu_index": 0, "menu_index": 0,
"modified": "2018-05-17 21:59:33.594245", "modified": "2018-05-23 03:25:15.872138",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Setup", "module": "Setup",
"name": "Company", "name": "Company",

View File

@ -2,8 +2,9 @@
# License: GNU General Public License v3. See license.txt # License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe, os import frappe, os, json
from frappe import _ from frappe import _
from frappe.utils import get_timestamp
from frappe.utils import cint, today, formatdate from frappe.utils import cint, today, formatdate
import frappe.defaults import frappe.defaults
@ -407,10 +408,18 @@ def update_company_monthly_sales(company):
frappe.db.set_value("Company", company, "sales_monthly_history", json.dumps(month_to_value_dict)) frappe.db.set_value("Company", company, "sales_monthly_history", json.dumps(month_to_value_dict))
def update_transactions_annual_history(company, commit=False):
transactions_history = get_all_transactions_annual_history(company)
frappe.db.set_value("Company", company, "transactions_annual_history", json.dumps(transactions_history))
if commit:
frappe.db.commit()
def cache_companies_monthly_sales_history(): def cache_companies_monthly_sales_history():
companies = [d['name'] for d in frappe.get_list("Company")] companies = [d['name'] for d in frappe.get_list("Company")]
for company in companies: for company in companies:
update_company_monthly_sales(company) update_company_monthly_sales(company)
update_transactions_annual_history(company)
frappe.db.commit() frappe.db.commit()
@frappe.whitelist() @frappe.whitelist()
@ -441,3 +450,73 @@ def add_node():
args.parent_company = None args.parent_company = None
frappe.get_doc(args).insert() frappe.get_doc(args).insert()
def get_all_transactions_annual_history(company):
out = {}
items = frappe.db.sql('''
select transaction_date, count(*) as count
from (
select name, transaction_date, company
from `tabQuotation`
UNION ALL
select name, transaction_date, company
from `tabSales Order`
UNION ALL
select name, posting_date as transaction_date, company
from `tabDelivery Note`
UNION ALL
select name, posting_date as transaction_date, company
from `tabSales Invoice`
UNION ALL
select name, creation as transaction_date, company
from `tabIssue`
UNION ALL
select name, creation as transaction_date, company
from `tabProject`
) t
where
company=%s
and
transaction_date > date_sub(curdate(), interval 1 year)
group by
transaction_date
''', (company), as_dict=True)
for d in items:
timestamp = get_timestamp(d["transaction_date"])
out.update({ timestamp: d["count"] })
return out
def get_timeline_data(doctype, name):
'''returns timeline data based on linked records in dashboard'''
out = {}
date_to_value_dict = {}
history = frappe.db.get_value("Company", name, "transactions_annual_history")
try:
date_to_value_dict = json.loads(history) if history and '{' in history else None
except ValueError:
date_to_value_dict = None
if date_to_value_dict is None:
update_transactions_annual_history(name, True)
history = frappe.db.get_value("Company", name, "transactions_annual_history")
return json.loads(history) if history and '{' in history else {}
return date_to_value_dict