blob: cdd166134f9df5b12325eca064a678fef264b432 [file] [log] [blame]
Shivam Mishra28436d22020-05-06 15:19:10 +05301# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
Chinmay Pai30f26b42020-05-11 19:54:46 +05303from erpnext import get_default_company
Shivam Mishra28436d22020-05-06 15:19:10 +05304
5import frappe
6import json
7
8
9def get_data():
Chinmay Pai30f26b42020-05-11 19:54:46 +053010 data = frappe._dict({
11 "dashboards": [],
12 "charts": []
Shivam Mishra28436d22020-05-06 15:19:10 +053013 })
Chinmay Pai30f26b42020-05-11 19:54:46 +053014 company = get_company_for_dashboards()
15 if company:
16 company_doc = frappe.get_doc("Company", company)
17 data.dashboards = get_dashboards()
18 data.charts = get_charts(company_doc)
19 return data
Shivam Mishra28436d22020-05-06 15:19:10 +053020
21def get_dashboards():
22 return [{
23 "name": "Accounts",
24 "dashboard_name": "Accounts",
25 "charts": [
26 { "chart": "Outgoing Bills (Sales Invoice)" },
27 { "chart": "Incoming Bills (Purchase Invoice)" },
28 { "chart": "Bank Balance" },
29 { "chart": "Income" },
30 { "chart": "Expenses" }
31 ]
32 }]
33
Chinmay Pai30f26b42020-05-11 19:54:46 +053034def get_charts(company):
Shivam Mishra28436d22020-05-06 15:19:10 +053035 income_account = company.default_income_account or get_account("Income Account", company.name)
36 expense_account = company.default_expense_account or get_account("Expense Account", company.name)
37 bank_account = company.default_bank_account or get_account("Bank", company.name)
38
39 return [
Chinmay Pai30f26b42020-05-11 19:54:46 +053040 {
41 "doctype": "Dashboard Chart",
42 "time_interval": "Quarterly",
43 "name": "Income",
44 "chart_name": "Income",
45 "timespan": "Last Year",
46 "color": None,
47 "filters_json": json.dumps({"company": company.name, "account": income_account}),
48 "source": "Account Balance Timeline",
49 "chart_type": "Custom",
50 "timeseries": 1,
51 "owner": "Administrator",
52 "type": "Line"
53 },
54 {
55 "doctype": "Dashboard Chart",
56 "time_interval": "Quarterly",
57 "name": "Expenses",
58 "chart_name": "Expenses",
59 "timespan": "Last Year",
60 "color": None,
61 "filters_json": json.dumps({"company": company.name, "account": expense_account}),
62 "source": "Account Balance Timeline",
63 "chart_type": "Custom",
64 "timeseries": 1,
65 "owner": "Administrator",
66 "type": "Line"
67 },
68 {
69 "doctype": "Dashboard Chart",
70 "time_interval": "Quarterly",
71 "name": "Bank Balance",
72 "chart_name": "Bank Balance",
73 "timespan": "Last Year",
74 "color": "#ffb868",
75 "filters_json": json.dumps({"company": company.name, "account": bank_account}),
76 "source": "Account Balance Timeline",
77 "chart_type": "Custom",
78 "timeseries": 1,
79 "owner": "Administrator",
80 "type": "Line"
81 },
82 {
83 "doctype": "Dashboard Chart",
84 "time_interval": "Monthly",
85 "name": "Incoming Bills (Purchase Invoice)",
86 "chart_name": "Incoming Bills (Purchase Invoice)",
87 "timespan": "Last Year",
88 "color": "#a83333",
89 "value_based_on": "base_grand_total",
90 "filters_json": json.dumps({}),
91 "chart_type": "Sum",
92 "timeseries": 1,
93 "based_on": "posting_date",
94 "owner": "Administrator",
95 "document_type": "Purchase Invoice",
96 "type": "Bar"
97 },
98 {
99 "doctype": "Dashboard Chart",
100 "time_interval": "Monthly",
101 "name": "Outgoing Bills (Sales Invoice)",
102 "chart_name": "Outgoing Bills (Sales Invoice)",
103 "timespan": "Last Year",
104 "color": "#7b933d",
105 "value_based_on": "base_grand_total",
106 "filters_json": json.dumps({}),
107 "chart_type": "Sum",
108 "timeseries": 1,
109 "based_on": "posting_date",
110 "owner": "Administrator",
111 "document_type": "Sales Invoice",
112 "type": "Bar"
113 }
114 ]
Shivam Mishra28436d22020-05-06 15:19:10 +0530115
Suraj Shetty65d3ac82020-05-09 17:39:59 +0530116def get_account(account_type, company):
117 accounts = frappe.get_list("Account", filters={"account_type": account_type, "company": company})
118 if accounts:
119 return accounts[0].name
Shivam Mishra28436d22020-05-06 15:19:10 +0530120
121def get_company_for_dashboards():
Chinmay Pai30f26b42020-05-11 19:54:46 +0530122 company = get_default_company()
123 if not company:
Shivam Mishra28436d22020-05-06 15:19:10 +0530124 company_list = frappe.get_list("Company")
125 if company_list:
Chinmay Pai30f26b42020-05-11 19:54:46 +0530126 company = company_list[0].name
127 return company