blob: 1eed5a0f9ca18b0ae73fa300726cbafd9111ddd1 [file] [log] [blame]
Deepesh Garg0defefd2020-05-11 12:14:46 +05301# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4import frappe
5import json
Nabin Hait9aae6f42020-05-18 18:03:11 +05306from frappe.utils import nowdate, add_months, get_date_str
Deepesh Garg8bbac6d2020-05-14 22:57:11 +05307from frappe import _
Nabin Hait9aae6f42020-05-18 18:03:11 +05308from erpnext.accounts.utils import get_fiscal_year, get_account_name
Deepesh Garg0defefd2020-05-11 12:14:46 +05309
10def get_company_for_dashboards():
11 company = frappe.defaults.get_defaults().company
12 if company:
13 return company
14 else:
15 company_list = frappe.get_list("Company")
16 if company_list:
17 return company_list[0].name
18 return None
19
20def get_data():
21 return frappe._dict({
22 "dashboards": get_dashboards(),
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053023 "charts": get_charts(),
24 "number_cards": get_number_cards()
Deepesh Garg0defefd2020-05-11 12:14:46 +053025 })
26
27def get_dashboards():
Deepesh Garg0defefd2020-05-11 12:14:46 +053028 return [{
rohitwaghchaure65c90952020-05-19 15:54:24 +053029 "name": "Accounts",
30 "dashboard_name": "Accounts",
Deepesh Garg0defefd2020-05-11 12:14:46 +053031 "doctype": "Dashboard",
32 "charts": [
33 { "chart": "Profit and Loss" , "width": "Full"},
Nabin Hait9aae6f42020-05-18 18:03:11 +053034 { "chart": "Incoming Bills (Purchase Invoice)", "width": "Half"},
35 { "chart": "Outgoing Bills (Sales Invoice)", "width": "Half"},
36 { "chart": "Accounts Receivable Ageing", "width": "Half"},
37 { "chart": "Accounts Payable Ageing", "width": "Half"},
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053038 { "chart": "Budget Variance", "width": "Full"},
Nabin Hait9aae6f42020-05-18 18:03:11 +053039 { "chart": "Bank Balance", "width": "Full"}
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053040 ],
41 "cards": [
Nabin Hait9aae6f42020-05-18 18:03:11 +053042 {"card": "Total Outgoing Bills"},
43 {"card": "Total Incoming Bills"},
44 {"card": "Total Incoming Payment"},
45 {"card": "Total Outgoing Payment"}
Deepesh Garg0defefd2020-05-11 12:14:46 +053046 ]
47 }]
48
49def get_charts():
50 company = frappe.get_doc("Company", get_company_for_dashboards())
Nabin Hait9aae6f42020-05-18 18:03:11 +053051 bank_account = company.default_bank_account or get_account_name("Bank", company=company.name)
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053052 fiscal_year = get_fiscal_year(date=nowdate())
53 default_cost_center = company.cost_center
Deepesh Garg0defefd2020-05-11 12:14:46 +053054
55 return [
56 {
57 "doctype": "Dashboard Charts",
58 "name": "Profit and Loss",
59 "owner": "Administrator",
60 "report_name": "Profit and Loss Statement",
61 "filters_json": json.dumps({
62 "company": company.name,
63 "filter_based_on": "Date Range",
Nabin Hait9aae6f42020-05-18 18:03:11 +053064 "period_start_date": get_date_str(fiscal_year[1]),
65 "period_end_date": get_date_str(fiscal_year[2]),
Deepesh Garg0defefd2020-05-11 12:14:46 +053066 "periodicity": "Monthly",
67 "include_default_book_entries": 1
Nabin Hait9aae6f42020-05-18 18:03:11 +053068 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +053069 "type": "Bar",
70 'timeseries': 0,
71 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053072 "chart_name": _("Profit and Loss"),
Nabin Hait9aae6f42020-05-18 18:03:11 +053073 "is_custom": 1,
74 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +053075 },
76 {
77 "doctype": "Dashboard Chart",
78 "time_interval": "Monthly",
Nabin Hait9aae6f42020-05-18 18:03:11 +053079 "name": "Incoming Bills (Purchase Invoice)",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053080 "chart_name": _("Incoming Bills (Purchase Invoice)"),
Deepesh Garg0defefd2020-05-11 12:14:46 +053081 "timespan": "Last Year",
82 "color": "#a83333",
Nabin Hait9aae6f42020-05-18 18:03:11 +053083 "value_based_on": "base_net_total",
Nabin Hait8b686a52020-05-19 19:08:30 +053084 "filters_json": json.dumps([["Purchase Invoice", "docstatus", "=", 1]]),
Deepesh Garg0defefd2020-05-11 12:14:46 +053085 "chart_type": "Sum",
86 "timeseries": 1,
87 "based_on": "posting_date",
88 "owner": "Administrator",
89 "document_type": "Purchase Invoice",
90 "type": "Bar",
Nabin Hait9aae6f42020-05-18 18:03:11 +053091 "width": "Half",
92 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +053093 },
94 {
95 "doctype": "Dashboard Chart",
Nabin Hait9aae6f42020-05-18 18:03:11 +053096 "name": "Outgoing Bills (Sales Invoice)",
Deepesh Garg0defefd2020-05-11 12:14:46 +053097 "time_interval": "Monthly",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053098 "chart_name": _("Outgoing Bills (Sales Invoice)"),
Deepesh Garg0defefd2020-05-11 12:14:46 +053099 "timespan": "Last Year",
100 "color": "#7b933d",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530101 "value_based_on": "base_net_total",
Nabin Hait8b686a52020-05-19 19:08:30 +0530102 "filters_json": json.dumps([["Sales Invoice", "docstatus", "=", 1]]),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530103 "chart_type": "Sum",
104 "timeseries": 1,
105 "based_on": "posting_date",
106 "owner": "Administrator",
107 "document_type": "Sales Invoice",
108 "type": "Bar",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530109 "width": "Half",
110 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530111 },
112 {
113 "doctype": "Dashboard Charts",
114 "name": "Accounts Receivable Ageing",
115 "owner": "Administrator",
116 "report_name": "Accounts Receivable",
117 "filters_json": json.dumps({
118 "company": company.name,
119 "report_date": nowdate(),
120 "ageing_based_on": "Due Date",
121 "range1": 30,
122 "range2": 60,
123 "range3": 90,
124 "range4": 120
125 }),
126 "type": "Donut",
127 'timeseries': 0,
128 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530129 "chart_name": _("Accounts Receivable Ageing"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530130 "is_custom": 1,
131 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530132 },
133 {
134 "doctype": "Dashboard Charts",
135 "name": "Accounts Payable Ageing",
136 "owner": "Administrator",
137 "report_name": "Accounts Payable",
138 "filters_json": json.dumps({
139 "company": company.name,
140 "report_date": nowdate(),
141 "ageing_based_on": "Due Date",
142 "range1": 30,
143 "range2": 60,
144 "range3": 90,
145 "range4": 120
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530146 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530147 "type": "Donut",
148 'timeseries': 0,
149 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530150 "chart_name": _("Accounts Payable Ageing"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530151 "is_custom": 1,
152 "is_public": 1
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530153 },
154 {
155 "doctype": "Dashboard Charts",
156 "name": "Budget Variance",
157 "owner": "Administrator",
158 "report_name": "Budget Variance Report",
159 "filters_json": json.dumps({
160 "company": company.name,
161 "from_fiscal_year": fiscal_year[0],
162 "to_fiscal_year": fiscal_year[0],
163 "period": "Monthly",
164 "budget_against": "Cost Center"
165 }),
166 "type": "Bar",
167 "timeseries": 0,
168 "chart_type": "Report",
169 "chart_name": _("Budget Variance"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530170 "is_custom": 1,
171 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530172 },
173 {
174 "doctype": "Dashboard Charts",
175 "name": "Bank Balance",
176 "time_interval": "Quarterly",
177 "chart_name": "Bank Balance",
178 "timespan": "Last Year",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530179 "filters_json": json.dumps({
180 "company": company.name,
181 "account": bank_account
182 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530183 "source": "Account Balance Timeline",
184 "chart_type": "Custom",
185 "timeseries": 1,
186 "owner": "Administrator",
187 "type": "Line",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530188 "width": "Half",
189 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530190 },
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530191 ]
Deepesh Garg0defefd2020-05-11 12:14:46 +0530192
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530193def get_number_cards():
Nabin Hait9aae6f42020-05-18 18:03:11 +0530194 fiscal_year = get_fiscal_year(date=nowdate())
195 year_start_date = get_date_str(fiscal_year[1])
196 year_end_date = get_date_str(fiscal_year[2])
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530197 return [
198 {
199 "doctype": "Number Card",
200 "document_type": "Payment Entry",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530201 "name": "Total Incoming Payment",
202 "filters_json": json.dumps([
203 ['Payment Entry', 'docstatus', '=', 1],
204 ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
205 ['Payment Entry', 'payment_type', '=', 'Receive']
206 ]),
207 "label": _("Total Incoming Payment"),
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530208 "function": "Sum",
209 "aggregate_function_based_on": "base_received_amount",
210 "is_public": 1,
211 "is_custom": 1,
212 "show_percentage_stats": 1,
Nabin Hait9aae6f42020-05-18 18:03:11 +0530213 "stats_time_interval": "Monthly"
214 },
215 {
216 "doctype": "Number Card",
217 "document_type": "Payment Entry",
218 "name": "Total Outgoing Payment",
219 "filters_json": json.dumps([
220 ['Payment Entry', 'docstatus', '=', 1],
221 ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
222 ['Payment Entry', 'payment_type', '=', 'Pay']
223 ]),
224 "label": _("Total Outgoing Payment"),
225 "function": "Sum",
226 "aggregate_function_based_on": "base_paid_amount",
227 "is_public": 1,
228 "is_custom": 1,
229 "show_percentage_stats": 1,
230 "stats_time_interval": "Monthly"
231 },
232 {
233 "doctype": "Number Card",
234 "document_type": "Sales Invoice",
235 "name": "Total Outgoing Bills",
236 "filters_json": json.dumps([
237 ['Sales Invoice', 'docstatus', '=', 1],
238 ['Sales Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
239 ]),
240 "label": _("Total Outgoing Bills"),
241 "function": "Sum",
242 "aggregate_function_based_on": "base_net_total",
243 "is_public": 1,
244 "is_custom": 1,
245 "show_percentage_stats": 1,
246 "stats_time_interval": "Monthly"
247 },
248 {
249 "doctype": "Number Card",
250 "document_type": "Purchase Invoice",
251 "name": "Total Incoming Bills",
252 "filters_json": json.dumps([
253 ['Purchase Invoice', 'docstatus', '=', 1],
254 ['Purchase Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
255 ]),
256 "label": _("Total Incoming Bills"),
257 "function": "Sum",
258 "aggregate_function_based_on": "base_net_total",
259 "is_public": 1,
260 "is_custom": 1,
261 "show_percentage_stats": 1,
262 "stats_time_interval": "Monthly"
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530263 }
Chinmay Pai30f26b42020-05-11 19:54:46 +0530264 ]