marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 1 | # Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | import frappe |
| 5 | import json |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 6 | from frappe import _ |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 7 | from frappe.utils import nowdate |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 8 | from erpnext.accounts.dashboard_fixtures import _get_fiscal_year |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 9 | |
| 10 | def get_data(): |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 11 | |
| 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 | |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 22 | return frappe._dict({ |
| 23 | "dashboards": get_dashboards(), |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 24 | "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), |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 26 | }) |
| 27 | |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 28 | def 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 | |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 38 | def get_dashboards(): |
| 39 | return [{ |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 40 | "name": "Buying", |
| 41 | "dashboard_name": "Buying", |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 42 | "charts": [ |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 43 | { "chart": "Purchase Order Trends", "width": "Full"}, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 44 | { "chart": "Material Request Analysis", "width": "Half"}, |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 45 | { "chart": "Purchase Order Analysis", "width": "Half"}, |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 46 | { "chart": "Top Suppliers", "width": "Full"} |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 47 | ], |
| 48 | "cards": [ |
Marica | a49f045 | 2020-05-27 20:28:37 +0530 | [diff] [blame] | 49 | { "card": "Annual Purchase"}, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 50 | { "card": "Purchase Orders to Receive"}, |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 51 | { "card": "Purchase Orders to Bill"}, |
| 52 | { "card": "Active Suppliers"} |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 53 | ] |
| 54 | }] |
| 55 | |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 56 | def get_charts(company, fiscal_year_name, start_date, end_date): |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 57 | return [ |
| 58 | { |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 59 | "name": "Purchase Order Analysis", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 60 | "chart_name": _("Purchase Order Analysis"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 61 | "chart_type": "Report", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 62 | "custom_options": json.dumps({ |
| 63 | "type": "donut", |
| 64 | "height": 300, |
| 65 | "axisOptions": {"shortenYAxisNumbers": 1} |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 66 | }), |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 67 | "doctype": "Dashboard Chart", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 68 | "filters_json": json.dumps({ |
| 69 | "company": company.name, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 70 | "from_date": start_date, |
| 71 | "to_date": end_date |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 72 | }), |
| 73 | "is_custom": 1, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 74 | "is_public": 1, |
| 75 | "owner": "Administrator", |
| 76 | "report_name": "Purchase Order Analysis", |
| 77 | "type": "Donut" |
| 78 | }, |
| 79 | { |
| 80 | "name": "Material Request Analysis", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 81 | "chart_name": _("Material Request Analysis"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 82 | "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], |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 89 | ["Material Request", "company", "=", company.name, False], |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 90 | ["Material Request", "docstatus", "=", 1, False], |
| 91 | ["Material Request", "transaction_date", "Between", [start_date, end_date], False]] |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 92 | ), |
| 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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 103 | "chart_name": _("Purchase Order Trends"), |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 104 | "chart_type": "Report", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 105 | "custom_options": json.dumps({ |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 106 | "type": "line", |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 107 | "axisOptions": {"shortenYAxisNumbers": 1}, |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 108 | "tooltipOptions": {}, |
| 109 | "lineOptions": { |
| 110 | "regionFill": 1 |
| 111 | } |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 112 | }), |
| 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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 129 | "chart_name": _("Top Suppliers"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 130 | "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" |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 144 | } |
| 145 | ] |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 146 | |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 147 | def get_number_cards(company, fiscal_year_name, start_date, end_date): |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 148 | return [ |
| 149 | { |
Marica | a49f045 | 2020-05-27 20:28:37 +0530 | [diff] [blame] | 150 | "name": "Annual Purchase", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 151 | "aggregate_function_based_on": "base_net_total", |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 152 | "doctype": "Number Card", |
| 153 | "document_type": "Purchase Order", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 154 | "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], |
Marica | 383807f | 2020-06-19 15:33:21 +0530 | [diff] [blame^] | 158 | ["Purchase Order", "company", "=", company.name, False] |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 159 | ]), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 160 | "function": "Sum", |
| 161 | "is_public": 1, |
Marica | a49f045 | 2020-05-27 20:28:37 +0530 | [diff] [blame] | 162 | "label": _("Annual Purchase"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 163 | "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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 171 | "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 | ]), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 176 | "function": "Count", |
| 177 | "is_public": 1, |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 178 | "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"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 195 | "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" |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 210 | } |
| 211 | ] |