blob: 889cb88dce18e060eecd0f9f6263519e305b966e [file] [log] [blame]
Marica383807f2020-06-19 15:33:21 +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
6from frappe import _
7from frappe.utils import nowdate
8from erpnext.accounts.utils import get_fiscal_year
9
10def get_data():
11 return frappe._dict({
12 "dashboards": get_dashboards(),
13 "charts": get_charts(),
14 "number_cards": get_number_cards(),
15 })
16
17def get_company_for_dashboards():
18 company = frappe.defaults.get_defaults().company
19 if company:
20 return company
21 else:
22 company_list = frappe.get_list("Company")
23 if company_list:
24 return company_list[0].name
25 return None
26
27company = frappe.get_doc("Company", get_company_for_dashboards())
28fiscal_year = get_fiscal_year(nowdate(), as_dict=1)
29fiscal_year_name = fiscal_year.get("name")
30start_date = str(fiscal_year.get("year_start_date"))
31end_date = str(fiscal_year.get("year_end_date"))
32
33def get_dashboards():
34 return [{
35 "name": "Selling",
36 "dashboard_name": "Selling",
37 "charts": [
38 { "chart": "Sales Order Trends", "width": "Full"},
39 { "chart": "Top Customers", "width": "Half"},
40 { "chart": "Sales Order Analysis", "width": "Half"},
41 { "chart": "Item-wise Annual Sales", "width": "Full"}
42 ],
43 "cards": [
44 { "card": "Annual Sales"},
45 { "card": "Sales Orders to Deliver"},
46 { "card": "Sales Orders to Bill"},
47 { "card": "Active Customers"}
48 ]
49 }]
50
51def get_charts():
52 return [
53 {
54 "name": "Sales Order Analysis",
55 "chart_name": _("Sales Order Analysis"),
56 "chart_type": "Report",
57 "custom_options": json.dumps({
58 "type": "donut",
59 "height": 300,
60 "axisOptions": {"shortenYAxisNumbers": 1}
61 }),
62 "doctype": "Dashboard Chart",
63 "filters_json": json.dumps({
64 "company": company.name,
65 "from_date": start_date,
66 "to_date": end_date
67 }),
68 "is_custom": 1,
69 "is_public": 1,
70 "owner": "Administrator",
71 "report_name": "Sales Order Analysis",
72 "type": "Donut"
73 },
74 {
75 "name": "Item-wise Annual Sales",
76 "chart_name": _("Item-wise Annual Sales"),
77 "chart_type": "Report",
78 "doctype": "Dashboard Chart",
79 "filters_json": json.dumps({
80 "company": company.name,
81 "from_date": start_date,
82 "to_date": end_date
83 }),
84 "is_custom": 1,
85 "is_public": 1,
86 "owner": "Administrator",
87 "report_name": "Item-wise Sales History",
88 "type": "Bar"
89 },
90 {
91 "name": "Sales Order Trends",
92 "chart_name": _("Sales Order Trends"),
93 "chart_type": "Report",
94 "custom_options": json.dumps({
95 "type": "line",
96 "axisOptions": {"shortenYAxisNumbers": 1},
97 "tooltipOptions": {},
98 "lineOptions": {
99 "regionFill": 1
100 }
101 }),
102 "doctype": "Dashboard Chart",
103 "filters_json": json.dumps({
104 "company": company.name,
105 "period": "Monthly",
106 "fiscal_year": fiscal_year_name,
107 "based_on": "Item"
108 }),
109 "is_custom": 1,
110 "is_public": 1,
111 "owner": "Administrator",
112 "report_name": "Sales Order Trends",
113 "type": "Line"
114 },
115 {
116 "name": "Top Customers",
117 "chart_name": _("Top Customers"),
118 "chart_type": "Report",
119 "doctype": "Dashboard Chart",
120 "filters_json": json.dumps({
121 "company": company.name,
122 "period": "Monthly",
123 "fiscal_year": fiscal_year_name,
124 "based_on": "Customer"
125 }),
126 "is_custom": 1,
127 "is_public": 1,
128 "owner": "Administrator",
129 "report_name": "Delivery Note Trends",
130 "type": "Bar"
131 }
132 ]
133
134def get_number_cards():
135 return [
136 {
137 "name": "Annual Sales",
138 "aggregate_function_based_on": "base_net_total",
139 "doctype": "Number Card",
140 "document_type": "Sales Order",
141 "filters_json": json.dumps([
142 ["Sales Order", "transaction_date", "Between", [start_date, end_date], False],
143 ["Sales Order", "status", "not in", ["Draft", "Cancelled", "Closed", None], False],
144 ["Sales Order", "docstatus", "=", 1, False],
145 ["Sales Order", "company", "=", company.name, False]
146 ]),
147 "function": "Sum",
148 "is_public": 1,
149 "label": _("Annual Sales"),
150 "owner": "Administrator",
151 "show_percentage_stats": 1,
152 "stats_time_interval": "Monthly"
153 },
154 {
155 "name": "Sales Orders to Deliver",
156 "doctype": "Number Card",
157 "document_type": "Sales Order",
158 "filters_json": json.dumps([
159 ["Sales Order", "status", "in", ["To Deliver and Bill", "To Deliver", None], False],
160 ["Sales Order", "docstatus", "=", 1, False],
161 ["Sales Order", "company", "=", company.name, False]
162 ]),
163 "function": "Count",
164 "is_public": 1,
165 "label": _("Sales Orders to Deliver"),
166 "owner": "Administrator",
167 "show_percentage_stats": 1,
168 "stats_time_interval": "Weekly"
169 },
170 {
171 "name": "Sales Orders to Bill",
172 "doctype": "Number Card",
173 "document_type": "Sales Order",
174 "filters_json": json.dumps([
175 ["Sales Order", "status", "in", ["To Deliver and Bill", "To Bill", None], False],
176 ["Sales Order", "docstatus", "=", 1, False],
177 ["Sales Order", "company", "=", company.name, False]
178 ]),
179 "function": "Count",
180 "is_public": 1,
181 "label": _("Sales Orders to Bill"),
182 "owner": "Administrator",
183 "show_percentage_stats": 1,
184 "stats_time_interval": "Weekly"
185 },
186 {
187 "name": "Active Customers",
188 "doctype": "Number Card",
189 "document_type": "Customer",
190 "filters_json": json.dumps([["Customer", "disabled", "=", "0"]]),
191 "function": "Count",
192 "is_public": 1,
193 "label": "Active Customers",
194 "owner": "Administrator",
195 "show_percentage_stats": 1,
196 "stats_time_interval": "Monthly"
197 }
198 ]