blob: b2abffc79d7def0610412ccc1fbaaa0d2e28e3ff [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 _
Deepesh Garg9df45322020-06-11 21:33:43 +05308from erpnext.accounts.utils import get_fiscal_year, get_account_name, FiscalYearError
9
10def _get_fiscal_year(date=None):
11 try:
Deepesh Garg7e974c92020-06-12 15:29:40 +053012 fiscal_year = get_fiscal_year(date=nowdate(), as_dict=True)
13 return fiscal_year
14
Deepesh Garg9df45322020-06-11 21:33:43 +053015 except FiscalYearError:
16 #if no fiscal year for current date then get default fiscal year
17 try:
Deepesh Garg7e974c92020-06-12 15:29:40 +053018 fiscal_year = get_fiscal_year(as_dict=True)
19 return fiscal_year
20
Deepesh Garg9df45322020-06-11 21:33:43 +053021 except FiscalYearError:
22 #if still no fiscal year found then no accounting data created, return
23 return None
Deepesh Garg0defefd2020-05-11 12:14:46 +053024
25def get_company_for_dashboards():
26 company = frappe.defaults.get_defaults().company
27 if company:
28 return company
29 else:
30 company_list = frappe.get_list("Company")
31 if company_list:
32 return company_list[0].name
33 return None
34
35def get_data():
Deepesh Garg9df45322020-06-11 21:33:43 +053036
37 fiscal_year = _get_fiscal_year(nowdate())
38
39 if not fiscal_year:
40 return frappe._dict()
41
Deepesh Garg0defefd2020-05-11 12:14:46 +053042 return frappe._dict({
43 "dashboards": get_dashboards(),
Deepesh Garg9df45322020-06-11 21:33:43 +053044 "charts": get_charts(fiscal_year),
45 "number_cards": get_number_cards(fiscal_year)
Deepesh Garg0defefd2020-05-11 12:14:46 +053046 })
47
48def get_dashboards():
Deepesh Garg0defefd2020-05-11 12:14:46 +053049 return [{
rohitwaghchaure65c90952020-05-19 15:54:24 +053050 "name": "Accounts",
51 "dashboard_name": "Accounts",
Deepesh Garg0defefd2020-05-11 12:14:46 +053052 "doctype": "Dashboard",
53 "charts": [
54 { "chart": "Profit and Loss" , "width": "Full"},
Nabin Hait9aae6f42020-05-18 18:03:11 +053055 { "chart": "Incoming Bills (Purchase Invoice)", "width": "Half"},
56 { "chart": "Outgoing Bills (Sales Invoice)", "width": "Half"},
57 { "chart": "Accounts Receivable Ageing", "width": "Half"},
58 { "chart": "Accounts Payable Ageing", "width": "Half"},
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053059 { "chart": "Budget Variance", "width": "Full"},
Nabin Hait9aae6f42020-05-18 18:03:11 +053060 { "chart": "Bank Balance", "width": "Full"}
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053061 ],
62 "cards": [
Nabin Hait9aae6f42020-05-18 18:03:11 +053063 {"card": "Total Outgoing Bills"},
64 {"card": "Total Incoming Bills"},
65 {"card": "Total Incoming Payment"},
66 {"card": "Total Outgoing Payment"}
Deepesh Garg0defefd2020-05-11 12:14:46 +053067 ]
68 }]
69
Deepesh Garg9df45322020-06-11 21:33:43 +053070def get_charts(fiscal_year):
Deepesh Garg0defefd2020-05-11 12:14:46 +053071 company = frappe.get_doc("Company", get_company_for_dashboards())
Nabin Hait9aae6f42020-05-18 18:03:11 +053072 bank_account = company.default_bank_account or get_account_name("Bank", company=company.name)
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053073 default_cost_center = company.cost_center
Deepesh Garg0defefd2020-05-11 12:14:46 +053074
75 return [
76 {
77 "doctype": "Dashboard Charts",
78 "name": "Profit and Loss",
79 "owner": "Administrator",
80 "report_name": "Profit and Loss Statement",
81 "filters_json": json.dumps({
82 "company": company.name,
mergify[bot]fd351f82020-05-28 15:18:47 +053083 "filter_based_on": "Fiscal Year",
Deepesh Garg7e974c92020-06-12 15:29:40 +053084 "from_fiscal_year": fiscal_year.get('name'),
85 "to_fiscal_year": fiscal_year.get('name'),
Deepesh Garg0defefd2020-05-11 12:14:46 +053086 "periodicity": "Monthly",
87 "include_default_book_entries": 1
Nabin Hait9aae6f42020-05-18 18:03:11 +053088 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +053089 "type": "Bar",
90 'timeseries': 0,
91 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +053092 "chart_name": _("Profit and Loss"),
Nabin Hait9aae6f42020-05-18 18:03:11 +053093 "is_custom": 1,
94 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +053095 },
96 {
97 "doctype": "Dashboard Chart",
98 "time_interval": "Monthly",
Nabin Hait9aae6f42020-05-18 18:03:11 +053099 "name": "Incoming Bills (Purchase Invoice)",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530100 "chart_name": _("Incoming Bills (Purchase Invoice)"),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530101 "timespan": "Last Year",
102 "color": "#a83333",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530103 "value_based_on": "base_net_total",
Nabin Hait8b686a52020-05-19 19:08:30 +0530104 "filters_json": json.dumps([["Purchase Invoice", "docstatus", "=", 1]]),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530105 "chart_type": "Sum",
106 "timeseries": 1,
107 "based_on": "posting_date",
108 "owner": "Administrator",
109 "document_type": "Purchase Invoice",
110 "type": "Bar",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530111 "width": "Half",
112 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530113 },
114 {
115 "doctype": "Dashboard Chart",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530116 "name": "Outgoing Bills (Sales Invoice)",
Deepesh Garg0defefd2020-05-11 12:14:46 +0530117 "time_interval": "Monthly",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530118 "chart_name": _("Outgoing Bills (Sales Invoice)"),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530119 "timespan": "Last Year",
120 "color": "#7b933d",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530121 "value_based_on": "base_net_total",
Nabin Hait8b686a52020-05-19 19:08:30 +0530122 "filters_json": json.dumps([["Sales Invoice", "docstatus", "=", 1]]),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530123 "chart_type": "Sum",
124 "timeseries": 1,
125 "based_on": "posting_date",
126 "owner": "Administrator",
127 "document_type": "Sales Invoice",
128 "type": "Bar",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530129 "width": "Half",
130 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530131 },
132 {
133 "doctype": "Dashboard Charts",
134 "name": "Accounts Receivable Ageing",
135 "owner": "Administrator",
136 "report_name": "Accounts Receivable",
137 "filters_json": json.dumps({
138 "company": company.name,
139 "report_date": nowdate(),
140 "ageing_based_on": "Due Date",
141 "range1": 30,
142 "range2": 60,
143 "range3": 90,
144 "range4": 120
145 }),
146 "type": "Donut",
147 'timeseries': 0,
148 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530149 "chart_name": _("Accounts Receivable Ageing"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530150 "is_custom": 1,
151 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530152 },
153 {
154 "doctype": "Dashboard Charts",
155 "name": "Accounts Payable Ageing",
156 "owner": "Administrator",
157 "report_name": "Accounts Payable",
158 "filters_json": json.dumps({
159 "company": company.name,
160 "report_date": nowdate(),
161 "ageing_based_on": "Due Date",
162 "range1": 30,
163 "range2": 60,
164 "range3": 90,
165 "range4": 120
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530166 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530167 "type": "Donut",
168 'timeseries': 0,
169 "chart_type": "Report",
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530170 "chart_name": _("Accounts Payable Ageing"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530171 "is_custom": 1,
172 "is_public": 1
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530173 },
174 {
175 "doctype": "Dashboard Charts",
176 "name": "Budget Variance",
177 "owner": "Administrator",
178 "report_name": "Budget Variance Report",
179 "filters_json": json.dumps({
180 "company": company.name,
Deepesh Garg7e974c92020-06-12 15:29:40 +0530181 "from_fiscal_year": fiscal_year.get('name'),
182 "to_fiscal_year": fiscal_year.get('name'),
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530183 "period": "Monthly",
184 "budget_against": "Cost Center"
185 }),
186 "type": "Bar",
187 "timeseries": 0,
188 "chart_type": "Report",
189 "chart_name": _("Budget Variance"),
Nabin Hait9aae6f42020-05-18 18:03:11 +0530190 "is_custom": 1,
191 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530192 },
193 {
194 "doctype": "Dashboard Charts",
195 "name": "Bank Balance",
196 "time_interval": "Quarterly",
197 "chart_name": "Bank Balance",
198 "timespan": "Last Year",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530199 "filters_json": json.dumps({
200 "company": company.name,
201 "account": bank_account
202 }),
Deepesh Garg0defefd2020-05-11 12:14:46 +0530203 "source": "Account Balance Timeline",
204 "chart_type": "Custom",
205 "timeseries": 1,
206 "owner": "Administrator",
207 "type": "Line",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530208 "width": "Half",
209 "is_public": 1
Deepesh Garg0defefd2020-05-11 12:14:46 +0530210 },
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530211 ]
Deepesh Garg0defefd2020-05-11 12:14:46 +0530212
Deepesh Garg9df45322020-06-11 21:33:43 +0530213def get_number_cards(fiscal_year):
214
Deepesh Garg7e974c92020-06-12 15:29:40 +0530215 year_start_date = get_date_str(fiscal_year.get("year_start_date"))
216 year_end_date = get_date_str(fiscal_year.get("year_end_date"))
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530217 return [
218 {
219 "doctype": "Number Card",
220 "document_type": "Payment Entry",
Nabin Hait9aae6f42020-05-18 18:03:11 +0530221 "name": "Total Incoming Payment",
222 "filters_json": json.dumps([
223 ['Payment Entry', 'docstatus', '=', 1],
224 ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
225 ['Payment Entry', 'payment_type', '=', 'Receive']
226 ]),
227 "label": _("Total Incoming Payment"),
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530228 "function": "Sum",
229 "aggregate_function_based_on": "base_received_amount",
230 "is_public": 1,
231 "is_custom": 1,
232 "show_percentage_stats": 1,
Nabin Hait9aae6f42020-05-18 18:03:11 +0530233 "stats_time_interval": "Monthly"
234 },
235 {
236 "doctype": "Number Card",
237 "document_type": "Payment Entry",
238 "name": "Total Outgoing Payment",
239 "filters_json": json.dumps([
240 ['Payment Entry', 'docstatus', '=', 1],
241 ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
242 ['Payment Entry', 'payment_type', '=', 'Pay']
243 ]),
244 "label": _("Total Outgoing Payment"),
245 "function": "Sum",
246 "aggregate_function_based_on": "base_paid_amount",
247 "is_public": 1,
248 "is_custom": 1,
249 "show_percentage_stats": 1,
250 "stats_time_interval": "Monthly"
251 },
252 {
253 "doctype": "Number Card",
254 "document_type": "Sales Invoice",
255 "name": "Total Outgoing Bills",
256 "filters_json": json.dumps([
257 ['Sales Invoice', 'docstatus', '=', 1],
258 ['Sales Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
259 ]),
260 "label": _("Total Outgoing Bills"),
261 "function": "Sum",
262 "aggregate_function_based_on": "base_net_total",
263 "is_public": 1,
264 "is_custom": 1,
265 "show_percentage_stats": 1,
266 "stats_time_interval": "Monthly"
267 },
268 {
269 "doctype": "Number Card",
270 "document_type": "Purchase Invoice",
271 "name": "Total Incoming Bills",
272 "filters_json": json.dumps([
273 ['Purchase Invoice', 'docstatus', '=', 1],
274 ['Purchase Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
275 ]),
276 "label": _("Total Incoming Bills"),
277 "function": "Sum",
278 "aggregate_function_based_on": "base_net_total",
279 "is_public": 1,
280 "is_custom": 1,
281 "show_percentage_stats": 1,
282 "stats_time_interval": "Monthly"
Deepesh Garg8bbac6d2020-05-14 22:57:11 +0530283 }
Chinmay Pai30f26b42020-05-11 19:54:46 +0530284 ]