blob: 0e2f78f3479ff783c9f052eb1f2cf869fa8dca8a [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
8from erpnext.accounts.utils import get_fiscal_year
marinationd99f85b2020-05-12 13:16:05 +05309
10def get_data():
11 return frappe._dict({
12 "dashboards": get_dashboards(),
13 "charts": get_charts(),
14 "number_cards": get_number_cards(),
15 })
16
marination354b0162020-05-12 17:35:17 +053017def 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())
marinationa74d4332020-05-15 23:37:48 +053028fiscal_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"))
marination354b0162020-05-12 17:35:17 +053032
marinationd99f85b2020-05-12 13:16:05 +053033def get_dashboards():
34 return [{
marination354b0162020-05-12 17:35:17 +053035 "name": "Buying",
36 "dashboard_name": "Buying",
marinationd99f85b2020-05-12 13:16:05 +053037 "charts": [
marination50d4bf52020-05-19 17:02:47 +053038 { "chart": "Purchase Order Trends", "width": "Full"},
marinationa74d4332020-05-15 23:37:48 +053039 { "chart": "Material Request Analysis", "width": "Half"},
marination354b0162020-05-12 17:35:17 +053040 { "chart": "Purchase Order Analysis", "width": "Half"},
marination50d4bf52020-05-19 17:02:47 +053041 { "chart": "Top Suppliers", "width": "Full"}
marinationa74d4332020-05-15 23:37:48 +053042 ],
43 "cards": [
Nabin Haite0050782020-05-19 20:22:25 +053044 { "card": "This Year Purchases"},
marinationa74d4332020-05-15 23:37:48 +053045 { "card": "Purchase Orders to Receive"},
Nabin Haite0050782020-05-19 20:22:25 +053046 { "card": "Purchase Orders to Bill"},
47 { "card": "Active Suppliers"}
marinationd99f85b2020-05-12 13:16:05 +053048 ]
49 }]
50
51def get_charts():
marination354b0162020-05-12 17:35:17 +053052 return [
53 {
marination354b0162020-05-12 17:35:17 +053054 "name": "Purchase Order Analysis",
Nabin Haite0050782020-05-19 20:22:25 +053055 "chart_name": _("Purchase Order Analysis"),
marinationa74d4332020-05-15 23:37:48 +053056 "chart_type": "Report",
marination354b0162020-05-12 17:35:17 +053057 "custom_options": json.dumps({
58 "type": "donut",
59 "height": 300,
60 "axisOptions": {"shortenYAxisNumbers": 1}
marinationa74d4332020-05-15 23:37:48 +053061 }),
marination354b0162020-05-12 17:35:17 +053062 "doctype": "Dashboard Chart",
marination354b0162020-05-12 17:35:17 +053063 "filters_json": json.dumps({
64 "company": company.name,
marinationa74d4332020-05-15 23:37:48 +053065 "from_date": start_date,
66 "to_date": end_date
marination354b0162020-05-12 17:35:17 +053067 }),
68 "is_custom": 1,
marinationa74d4332020-05-15 23:37:48 +053069 "is_public": 1,
70 "owner": "Administrator",
71 "report_name": "Purchase Order Analysis",
72 "type": "Donut"
73 },
74 {
75 "name": "Material Request Analysis",
Nabin Haite0050782020-05-19 20:22:25 +053076 "chart_name": _("Material Request Analysis"),
marinationa74d4332020-05-15 23:37:48 +053077 "chart_type": "Group By",
78 "custom_options": json.dumps({"height": 300}),
79 "doctype": "Dashboard Chart",
80 "document_type": "Material Request",
81 "filters_json": json.dumps(
82 [["Material Request", "status", "not in", ["Draft", "Cancelled", "Stopped", None], False],
83 ["Material Request", "material_request_type", "=", "Purchase", False],
marination50d4bf52020-05-19 17:02:47 +053084 ["Material Request", "company", "=", company.name, False],
Nabin Haite0050782020-05-19 20:22:25 +053085 ["Material Request", "docstatus", "=", 1, False],
86 ["Material Request", "transaction_date", "Between", [start_date, end_date], False]]
marinationa74d4332020-05-15 23:37:48 +053087 ),
88 "group_by_based_on": "status",
89 "group_by_type": "Count",
90 "is_custom": 0,
91 "is_public": 1,
92 "number_of_groups": 0,
93 "owner": "Administrator",
94 "type": "Donut"
95 },
96 {
97 "name": "Purchase Order Trends",
Nabin Haite0050782020-05-19 20:22:25 +053098 "chart_name": _("Purchase Order Trends"),
marination354b0162020-05-12 17:35:17 +053099 "chart_type": "Report",
marination354b0162020-05-12 17:35:17 +0530100 "custom_options": json.dumps({
marinationa74d4332020-05-15 23:37:48 +0530101 "type": "line",
marinationa74d4332020-05-15 23:37:48 +0530102 "axisOptions": {"shortenYAxisNumbers": 1},
marination50d4bf52020-05-19 17:02:47 +0530103 "tooltipOptions": {},
104 "lineOptions": {
105 "regionFill": 1
106 }
marinationa74d4332020-05-15 23:37:48 +0530107 }),
108 "doctype": "Dashboard Chart",
109 "filters_json": json.dumps({
110 "company": company.name,
111 "period": "Monthly",
112 "fiscal_year": fiscal_year_name,
113 "period_based_on": "posting_date",
114 "based_on": "Item"
115 }),
116 "is_custom": 1,
117 "is_public": 1,
118 "owner": "Administrator",
119 "report_name": "Purchase Order Trends",
120 "type": "Line"
121 },
122 {
123 "name": "Top Suppliers",
Nabin Haite0050782020-05-19 20:22:25 +0530124 "chart_name": _("Top Suppliers"),
marinationa74d4332020-05-15 23:37:48 +0530125 "chart_type": "Report",
126 "doctype": "Dashboard Chart",
127 "filters_json": json.dumps({
128 "company": company.name,
129 "period": "Monthly",
130 "fiscal_year": fiscal_year_name,
131 "period_based_on": "posting_date",
132 "based_on": "Supplier"
133 }),
134 "is_custom": 1,
135 "is_public": 1,
136 "owner": "Administrator",
137 "report_name": "Purchase Receipt Trends",
138 "type": "Bar"
marination354b0162020-05-12 17:35:17 +0530139 }
140 ]
marinationd99f85b2020-05-12 13:16:05 +0530141
142def get_number_cards():
marinationa74d4332020-05-15 23:37:48 +0530143 return [
144 {
Nabin Haite0050782020-05-19 20:22:25 +0530145 "name": "This Year Purchases",
146 "aggregate_function_based_on": "base_net_total",
marinationa74d4332020-05-15 23:37:48 +0530147 "doctype": "Number Card",
148 "document_type": "Purchase Order",
Nabin Haite0050782020-05-19 20:22:25 +0530149 "filters_json": json.dumps([
150 ["Purchase Order", "transaction_date", "Between", [start_date, end_date], False],
151 ["Purchase Order", "status", "not in", ["Draft", "Cancelled", "Closed", None], False],
152 ["Purchase Order", "docstatus", "=", 1, False],
marination50d4bf52020-05-19 17:02:47 +0530153 ["Purchase Order", "company", "=", company.name, False],
Nabin Haite0050782020-05-19 20:22:25 +0530154 ["Purchase Order", "transaction_date", "Between", [start_date,end_date], False]
155 ]),
marinationa74d4332020-05-15 23:37:48 +0530156 "function": "Sum",
157 "is_public": 1,
Nabin Haite0050782020-05-19 20:22:25 +0530158 "label": _("This Year Purchases"),
marinationa74d4332020-05-15 23:37:48 +0530159 "owner": "Administrator",
160 "show_percentage_stats": 1,
161 "stats_time_interval": "Monthly"
162 },
163 {
164 "name": "Purchase Orders to Receive",
165 "doctype": "Number Card",
166 "document_type": "Purchase Order",
Nabin Haite0050782020-05-19 20:22:25 +0530167 "filters_json": json.dumps([
168 ["Purchase Order", "status", "in", ["To Receive and Bill", "To Receive", None], False],
169 ["Purchase Order", "docstatus", "=", 1, False],
170 ["Purchase Order", "company", "=", company.name, False]
171 ]),
marinationa74d4332020-05-15 23:37:48 +0530172 "function": "Count",
173 "is_public": 1,
Nabin Haite0050782020-05-19 20:22:25 +0530174 "label": _("Purchase Orders to Receive"),
175 "owner": "Administrator",
176 "show_percentage_stats": 1,
177 "stats_time_interval": "Weekly"
178 },
179 {
180 "name": "Purchase Orders to Bill",
181 "doctype": "Number Card",
182 "document_type": "Purchase Order",
183 "filters_json": json.dumps([
184 ["Purchase Order", "status", "in", ["To Receive and Bill", "To Bill", None], False],
185 ["Purchase Order", "docstatus", "=", 1, False],
186 ["Purchase Order", "company", "=", company.name, False]
187 ]),
188 "function": "Count",
189 "is_public": 1,
190 "label": _("Purchase Orders to Bill"),
marinationa74d4332020-05-15 23:37:48 +0530191 "owner": "Administrator",
192 "show_percentage_stats": 1,
193 "stats_time_interval": "Weekly"
194 },
195 {
196 "name": "Active Suppliers",
197 "doctype": "Number Card",
198 "document_type": "Supplier",
199 "filters_json": json.dumps([["Supplier", "disabled", "=", "0"]]),
200 "function": "Count",
201 "is_public": 1,
202 "label": "Active Suppliers",
203 "owner": "Administrator",
204 "show_percentage_stats": 1,
205 "stats_time_interval": "Monthly"
marinationa74d4332020-05-15 23:37:48 +0530206 }
207 ]