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