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 |
| 8 | from erpnext.accounts.utils import get_fiscal_year |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 9 | |
| 10 | def get_data(): |
| 11 | return frappe._dict({ |
| 12 | "dashboards": get_dashboards(), |
| 13 | "charts": get_charts(), |
| 14 | "number_cards": get_number_cards(), |
| 15 | }) |
| 16 | |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 17 | def 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 | |
| 27 | company = frappe.get_doc("Company", get_company_for_dashboards()) |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 28 | fiscal_year = get_fiscal_year(nowdate(), as_dict=1) |
| 29 | fiscal_year_name = fiscal_year.get("name") |
| 30 | start_date = str(fiscal_year.get("year_start_date")) |
| 31 | end_date = str(fiscal_year.get("year_end_date")) |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 32 | |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 33 | def get_dashboards(): |
| 34 | return [{ |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 35 | "name": "Buying", |
| 36 | "dashboard_name": "Buying", |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 37 | "charts": [ |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 38 | { "chart": "Purchase Order Trends", "width": "Full"}, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 39 | { "chart": "Material Request Analysis", "width": "Half"}, |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 40 | { "chart": "Purchase Order Analysis", "width": "Half"}, |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 41 | { "chart": "Top Suppliers", "width": "Full"} |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 42 | ], |
| 43 | "cards": [ |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 44 | { "card": "This Year Purchases"}, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 45 | { "card": "Purchase Orders to Receive"}, |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 46 | { "card": "Purchase Orders to Bill"}, |
| 47 | { "card": "Active Suppliers"} |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 48 | ] |
| 49 | }] |
| 50 | |
| 51 | def get_charts(): |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 52 | return [ |
| 53 | { |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 54 | "name": "Purchase Order Analysis", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 55 | "chart_name": _("Purchase Order Analysis"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 56 | "chart_type": "Report", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 57 | "custom_options": json.dumps({ |
| 58 | "type": "donut", |
| 59 | "height": 300, |
| 60 | "axisOptions": {"shortenYAxisNumbers": 1} |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 61 | }), |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 62 | "doctype": "Dashboard Chart", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 63 | "filters_json": json.dumps({ |
| 64 | "company": company.name, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 65 | "from_date": start_date, |
| 66 | "to_date": end_date |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 67 | }), |
| 68 | "is_custom": 1, |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 69 | "is_public": 1, |
| 70 | "owner": "Administrator", |
| 71 | "report_name": "Purchase Order Analysis", |
| 72 | "type": "Donut" |
| 73 | }, |
| 74 | { |
| 75 | "name": "Material Request Analysis", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 76 | "chart_name": _("Material Request Analysis"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 77 | "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], |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 84 | ["Material Request", "company", "=", company.name, False], |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 85 | ["Material Request", "docstatus", "=", 1, False], |
| 86 | ["Material Request", "transaction_date", "Between", [start_date, end_date], False]] |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 87 | ), |
| 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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 98 | "chart_name": _("Purchase Order Trends"), |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 99 | "chart_type": "Report", |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 100 | "custom_options": json.dumps({ |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 101 | "type": "line", |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 102 | "axisOptions": {"shortenYAxisNumbers": 1}, |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 103 | "tooltipOptions": {}, |
| 104 | "lineOptions": { |
| 105 | "regionFill": 1 |
| 106 | } |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 107 | }), |
| 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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 124 | "chart_name": _("Top Suppliers"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 125 | "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" |
marination | 354b016 | 2020-05-12 17:35:17 +0530 | [diff] [blame] | 139 | } |
| 140 | ] |
marination | d99f85b | 2020-05-12 13:16:05 +0530 | [diff] [blame] | 141 | |
| 142 | def get_number_cards(): |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 143 | return [ |
| 144 | { |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 145 | "name": "This Year Purchases", |
| 146 | "aggregate_function_based_on": "base_net_total", |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 147 | "doctype": "Number Card", |
| 148 | "document_type": "Purchase Order", |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 149 | "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], |
marination | 50d4bf5 | 2020-05-19 17:02:47 +0530 | [diff] [blame] | 153 | ["Purchase Order", "company", "=", company.name, False], |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 154 | ["Purchase Order", "transaction_date", "Between", [start_date,end_date], False] |
| 155 | ]), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 156 | "function": "Sum", |
| 157 | "is_public": 1, |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 158 | "label": _("This Year Purchases"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 159 | "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 Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 167 | "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 | ]), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 172 | "function": "Count", |
| 173 | "is_public": 1, |
Nabin Hait | e005078 | 2020-05-19 20:22:25 +0530 | [diff] [blame] | 174 | "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"), |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 191 | "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" |
marination | a74d433 | 2020-05-15 23:37:48 +0530 | [diff] [blame] | 206 | } |
| 207 | ] |