blob: c6e2ffa634fd1da9cbe307ad82f0b5fac8b41955 [file] [log] [blame]
marinationd99f85b2020-05-12 13:16:05 +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 Haite0050782020-05-19 20:22:25 +05306from frappe import _
marinationa74d4332020-05-15 23:37:48 +05307from frappe.utils import nowdate
Deepesh Garg9df45322020-06-11 21:33:43 +05308from erpnext.accounts.dashboard_fixtures import _get_fiscal_year
marinationd99f85b2020-05-12 13:16:05 +05309
10def get_data():
Deepesh Garg9df45322020-06-11 21:33:43 +053011
12 fiscal_year = _get_fiscal_year(nowdate())
13
14 if not fiscal_year:
15 return frappe._dict()
16
17 company = frappe.get_doc("Company", get_company_for_dashboards())
18 fiscal_year_name = fiscal_year.get("name")
19 start_date = str(fiscal_year.get("year_start_date"))
20 end_date = str(fiscal_year.get("year_end_date"))
21
marinationd99f85b2020-05-12 13:16:05 +053022 return frappe._dict({
23 "dashboards": get_dashboards(),
Deepesh Garg9df45322020-06-11 21:33:43 +053024 "charts": get_charts(company, fiscal_year_name, start_date, end_date),
25 "number_cards": get_number_cards(company, fiscal_year_name, start_date, end_date),
marinationd99f85b2020-05-12 13:16:05 +053026 })
27
marination354b0162020-05-12 17:35:17 +053028def get_company_for_dashboards():
29 company = frappe.defaults.get_defaults().company
30 if company:
31 return company
32 else:
33 company_list = frappe.get_list("Company")
34 if company_list:
35 return company_list[0].name
36 return None
37
marinationd99f85b2020-05-12 13:16:05 +053038def get_dashboards():
39 return [{
marination354b0162020-05-12 17:35:17 +053040 "name": "Buying",
41 "dashboard_name": "Buying",
marinationd99f85b2020-05-12 13:16:05 +053042 "charts": [
marination50d4bf52020-05-19 17:02:47 +053043 { "chart": "Purchase Order Trends", "width": "Full"},
marinationa74d4332020-05-15 23:37:48 +053044 { "chart": "Material Request Analysis", "width": "Half"},
marination354b0162020-05-12 17:35:17 +053045 { "chart": "Purchase Order Analysis", "width": "Half"},
marination50d4bf52020-05-19 17:02:47 +053046 { "chart": "Top Suppliers", "width": "Full"}
marinationa74d4332020-05-15 23:37:48 +053047 ],
48 "cards": [
Maricaa49f0452020-05-27 20:28:37 +053049 { "card": "Annual Purchase"},
marinationa74d4332020-05-15 23:37:48 +053050 { "card": "Purchase Orders to Receive"},
Nabin Haite0050782020-05-19 20:22:25 +053051 { "card": "Purchase Orders to Bill"},
52 { "card": "Active Suppliers"}
marinationd99f85b2020-05-12 13:16:05 +053053 ]
54 }]
55
Deepesh Garg9df45322020-06-11 21:33:43 +053056def get_charts(company, fiscal_year_name, start_date, end_date):
marination354b0162020-05-12 17:35:17 +053057 return [
58 {
marination354b0162020-05-12 17:35:17 +053059 "name": "Purchase Order Analysis",
Nabin Haite0050782020-05-19 20:22:25 +053060 "chart_name": _("Purchase Order Analysis"),
marinationa74d4332020-05-15 23:37:48 +053061 "chart_type": "Report",
marination354b0162020-05-12 17:35:17 +053062 "custom_options": json.dumps({
63 "type": "donut",
64 "height": 300,
65 "axisOptions": {"shortenYAxisNumbers": 1}
marinationa74d4332020-05-15 23:37:48 +053066 }),
marination354b0162020-05-12 17:35:17 +053067 "doctype": "Dashboard Chart",
marination354b0162020-05-12 17:35:17 +053068 "filters_json": json.dumps({
69 "company": company.name,
marinationa74d4332020-05-15 23:37:48 +053070 "from_date": start_date,
71 "to_date": end_date
marination354b0162020-05-12 17:35:17 +053072 }),
73 "is_custom": 1,
marinationa74d4332020-05-15 23:37:48 +053074 "is_public": 1,
75 "owner": "Administrator",
76 "report_name": "Purchase Order Analysis",
77 "type": "Donut"
78 },
79 {
80 "name": "Material Request Analysis",
Nabin Haite0050782020-05-19 20:22:25 +053081 "chart_name": _("Material Request Analysis"),
marinationa74d4332020-05-15 23:37:48 +053082 "chart_type": "Group By",
83 "custom_options": json.dumps({"height": 300}),
84 "doctype": "Dashboard Chart",
85 "document_type": "Material Request",
86 "filters_json": json.dumps(
87 [["Material Request", "status", "not in", ["Draft", "Cancelled", "Stopped", None], False],
88 ["Material Request", "material_request_type", "=", "Purchase", False],
marination50d4bf52020-05-19 17:02:47 +053089 ["Material Request", "company", "=", company.name, False],
Nabin Haite0050782020-05-19 20:22:25 +053090 ["Material Request", "docstatus", "=", 1, False],
91 ["Material Request", "transaction_date", "Between", [start_date, end_date], False]]
marinationa74d4332020-05-15 23:37:48 +053092 ),
93 "group_by_based_on": "status",
94 "group_by_type": "Count",
95 "is_custom": 0,
96 "is_public": 1,
97 "number_of_groups": 0,
98 "owner": "Administrator",
99 "type": "Donut"
100 },
101 {
102 "name": "Purchase Order Trends",
Nabin Haite0050782020-05-19 20:22:25 +0530103 "chart_name": _("Purchase Order Trends"),
marination354b0162020-05-12 17:35:17 +0530104 "chart_type": "Report",
marination354b0162020-05-12 17:35:17 +0530105 "custom_options": json.dumps({
marinationa74d4332020-05-15 23:37:48 +0530106 "type": "line",
marinationa74d4332020-05-15 23:37:48 +0530107 "axisOptions": {"shortenYAxisNumbers": 1},
marination50d4bf52020-05-19 17:02:47 +0530108 "tooltipOptions": {},
109 "lineOptions": {
110 "regionFill": 1
111 }
marinationa74d4332020-05-15 23:37:48 +0530112 }),
113 "doctype": "Dashboard Chart",
114 "filters_json": json.dumps({
115 "company": company.name,
116 "period": "Monthly",
117 "fiscal_year": fiscal_year_name,
118 "period_based_on": "posting_date",
119 "based_on": "Item"
120 }),
121 "is_custom": 1,
122 "is_public": 1,
123 "owner": "Administrator",
124 "report_name": "Purchase Order Trends",
125 "type": "Line"
126 },
127 {
128 "name": "Top Suppliers",
Nabin Haite0050782020-05-19 20:22:25 +0530129 "chart_name": _("Top Suppliers"),
marinationa74d4332020-05-15 23:37:48 +0530130 "chart_type": "Report",
131 "doctype": "Dashboard Chart",
132 "filters_json": json.dumps({
133 "company": company.name,
134 "period": "Monthly",
135 "fiscal_year": fiscal_year_name,
136 "period_based_on": "posting_date",
137 "based_on": "Supplier"
138 }),
139 "is_custom": 1,
140 "is_public": 1,
141 "owner": "Administrator",
142 "report_name": "Purchase Receipt Trends",
143 "type": "Bar"
marination354b0162020-05-12 17:35:17 +0530144 }
145 ]
marinationd99f85b2020-05-12 13:16:05 +0530146
Deepesh Garg9df45322020-06-11 21:33:43 +0530147def get_number_cards(company, fiscal_year_name, start_date, end_date):
marinationa74d4332020-05-15 23:37:48 +0530148 return [
149 {
Maricaa49f0452020-05-27 20:28:37 +0530150 "name": "Annual Purchase",
Nabin Haite0050782020-05-19 20:22:25 +0530151 "aggregate_function_based_on": "base_net_total",
marinationa74d4332020-05-15 23:37:48 +0530152 "doctype": "Number Card",
153 "document_type": "Purchase Order",
Nabin Haite0050782020-05-19 20:22:25 +0530154 "filters_json": json.dumps([
155 ["Purchase Order", "transaction_date", "Between", [start_date, end_date], False],
156 ["Purchase Order", "status", "not in", ["Draft", "Cancelled", "Closed", None], False],
157 ["Purchase Order", "docstatus", "=", 1, False],
Marica383807f2020-06-19 15:33:21 +0530158 ["Purchase Order", "company", "=", company.name, False]
Nabin Haite0050782020-05-19 20:22:25 +0530159 ]),
marinationa74d4332020-05-15 23:37:48 +0530160 "function": "Sum",
161 "is_public": 1,
Maricaa49f0452020-05-27 20:28:37 +0530162 "label": _("Annual Purchase"),
marinationa74d4332020-05-15 23:37:48 +0530163 "owner": "Administrator",
164 "show_percentage_stats": 1,
165 "stats_time_interval": "Monthly"
166 },
167 {
168 "name": "Purchase Orders to Receive",
169 "doctype": "Number Card",
170 "document_type": "Purchase Order",
Nabin Haite0050782020-05-19 20:22:25 +0530171 "filters_json": json.dumps([
172 ["Purchase Order", "status", "in", ["To Receive and Bill", "To Receive", None], False],
173 ["Purchase Order", "docstatus", "=", 1, False],
174 ["Purchase Order", "company", "=", company.name, False]
175 ]),
marinationa74d4332020-05-15 23:37:48 +0530176 "function": "Count",
177 "is_public": 1,
Nabin Haite0050782020-05-19 20:22:25 +0530178 "label": _("Purchase Orders to Receive"),
179 "owner": "Administrator",
180 "show_percentage_stats": 1,
181 "stats_time_interval": "Weekly"
182 },
183 {
184 "name": "Purchase Orders to Bill",
185 "doctype": "Number Card",
186 "document_type": "Purchase Order",
187 "filters_json": json.dumps([
188 ["Purchase Order", "status", "in", ["To Receive and Bill", "To Bill", None], False],
189 ["Purchase Order", "docstatus", "=", 1, False],
190 ["Purchase Order", "company", "=", company.name, False]
191 ]),
192 "function": "Count",
193 "is_public": 1,
194 "label": _("Purchase Orders to Bill"),
marinationa74d4332020-05-15 23:37:48 +0530195 "owner": "Administrator",
196 "show_percentage_stats": 1,
197 "stats_time_interval": "Weekly"
198 },
199 {
200 "name": "Active Suppliers",
201 "doctype": "Number Card",
202 "document_type": "Supplier",
203 "filters_json": json.dumps([["Supplier", "disabled", "=", "0"]]),
204 "function": "Count",
205 "is_public": 1,
206 "label": "Active Suppliers",
207 "owner": "Administrator",
208 "show_percentage_stats": 1,
209 "stats_time_interval": "Monthly"
marinationa74d4332020-05-15 23:37:48 +0530210 }
211 ]