Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +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 | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 6 | from frappe.utils import nowdate, add_months, get_date_str |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 7 | from frappe import _ |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 8 | from erpnext.accounts.utils import get_fiscal_year, get_account_name |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 9 | |
| 10 | def get_company_for_dashboards(): |
| 11 | company = frappe.defaults.get_defaults().company |
| 12 | if company: |
| 13 | return company |
| 14 | else: |
| 15 | company_list = frappe.get_list("Company") |
| 16 | if company_list: |
| 17 | return company_list[0].name |
| 18 | return None |
| 19 | |
| 20 | def get_data(): |
| 21 | return frappe._dict({ |
| 22 | "dashboards": get_dashboards(), |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 23 | "charts": get_charts(), |
| 24 | "number_cards": get_number_cards() |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 25 | }) |
| 26 | |
| 27 | def get_dashboards(): |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 28 | return [{ |
rohitwaghchaure | 65c9095 | 2020-05-19 15:54:24 +0530 | [diff] [blame] | 29 | "name": "Accounts", |
| 30 | "dashboard_name": "Accounts", |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 31 | "doctype": "Dashboard", |
| 32 | "charts": [ |
| 33 | { "chart": "Profit and Loss" , "width": "Full"}, |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 34 | { "chart": "Incoming Bills (Purchase Invoice)", "width": "Half"}, |
| 35 | { "chart": "Outgoing Bills (Sales Invoice)", "width": "Half"}, |
| 36 | { "chart": "Accounts Receivable Ageing", "width": "Half"}, |
| 37 | { "chart": "Accounts Payable Ageing", "width": "Half"}, |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 38 | { "chart": "Budget Variance", "width": "Full"}, |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 39 | { "chart": "Bank Balance", "width": "Full"} |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 40 | ], |
| 41 | "cards": [ |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 42 | {"card": "Total Outgoing Bills"}, |
| 43 | {"card": "Total Incoming Bills"}, |
| 44 | {"card": "Total Incoming Payment"}, |
| 45 | {"card": "Total Outgoing Payment"} |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 46 | ] |
| 47 | }] |
| 48 | |
| 49 | def get_charts(): |
| 50 | company = frappe.get_doc("Company", get_company_for_dashboards()) |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 51 | bank_account = company.default_bank_account or get_account_name("Bank", company=company.name) |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 52 | fiscal_year = get_fiscal_year(date=nowdate()) |
| 53 | default_cost_center = company.cost_center |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 54 | |
| 55 | return [ |
| 56 | { |
| 57 | "doctype": "Dashboard Charts", |
| 58 | "name": "Profit and Loss", |
| 59 | "owner": "Administrator", |
| 60 | "report_name": "Profit and Loss Statement", |
| 61 | "filters_json": json.dumps({ |
| 62 | "company": company.name, |
| 63 | "filter_based_on": "Date Range", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 64 | "period_start_date": get_date_str(fiscal_year[1]), |
| 65 | "period_end_date": get_date_str(fiscal_year[2]), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 66 | "periodicity": "Monthly", |
| 67 | "include_default_book_entries": 1 |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 68 | }), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 69 | "type": "Bar", |
| 70 | 'timeseries': 0, |
| 71 | "chart_type": "Report", |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 72 | "chart_name": _("Profit and Loss"), |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 73 | "is_custom": 1, |
| 74 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 75 | }, |
| 76 | { |
| 77 | "doctype": "Dashboard Chart", |
| 78 | "time_interval": "Monthly", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 79 | "name": "Incoming Bills (Purchase Invoice)", |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 80 | "chart_name": _("Incoming Bills (Purchase Invoice)"), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 81 | "timespan": "Last Year", |
| 82 | "color": "#a83333", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 83 | "value_based_on": "base_net_total", |
Nabin Hait | 8b686a5 | 2020-05-19 19:08:30 +0530 | [diff] [blame] | 84 | "filters_json": json.dumps([["Purchase Invoice", "docstatus", "=", 1]]), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 85 | "chart_type": "Sum", |
| 86 | "timeseries": 1, |
| 87 | "based_on": "posting_date", |
| 88 | "owner": "Administrator", |
| 89 | "document_type": "Purchase Invoice", |
| 90 | "type": "Bar", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 91 | "width": "Half", |
| 92 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | "doctype": "Dashboard Chart", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 96 | "name": "Outgoing Bills (Sales Invoice)", |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 97 | "time_interval": "Monthly", |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 98 | "chart_name": _("Outgoing Bills (Sales Invoice)"), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 99 | "timespan": "Last Year", |
| 100 | "color": "#7b933d", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 101 | "value_based_on": "base_net_total", |
Nabin Hait | 8b686a5 | 2020-05-19 19:08:30 +0530 | [diff] [blame] | 102 | "filters_json": json.dumps([["Sales Invoice", "docstatus", "=", 1]]), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 103 | "chart_type": "Sum", |
| 104 | "timeseries": 1, |
| 105 | "based_on": "posting_date", |
| 106 | "owner": "Administrator", |
| 107 | "document_type": "Sales Invoice", |
| 108 | "type": "Bar", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 109 | "width": "Half", |
| 110 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 111 | }, |
| 112 | { |
| 113 | "doctype": "Dashboard Charts", |
| 114 | "name": "Accounts Receivable Ageing", |
| 115 | "owner": "Administrator", |
| 116 | "report_name": "Accounts Receivable", |
| 117 | "filters_json": json.dumps({ |
| 118 | "company": company.name, |
| 119 | "report_date": nowdate(), |
| 120 | "ageing_based_on": "Due Date", |
| 121 | "range1": 30, |
| 122 | "range2": 60, |
| 123 | "range3": 90, |
| 124 | "range4": 120 |
| 125 | }), |
| 126 | "type": "Donut", |
| 127 | 'timeseries': 0, |
| 128 | "chart_type": "Report", |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 129 | "chart_name": _("Accounts Receivable Ageing"), |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 130 | "is_custom": 1, |
| 131 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 132 | }, |
| 133 | { |
| 134 | "doctype": "Dashboard Charts", |
| 135 | "name": "Accounts Payable Ageing", |
| 136 | "owner": "Administrator", |
| 137 | "report_name": "Accounts Payable", |
| 138 | "filters_json": json.dumps({ |
| 139 | "company": company.name, |
| 140 | "report_date": nowdate(), |
| 141 | "ageing_based_on": "Due Date", |
| 142 | "range1": 30, |
| 143 | "range2": 60, |
| 144 | "range3": 90, |
| 145 | "range4": 120 |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 146 | }), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 147 | "type": "Donut", |
| 148 | 'timeseries': 0, |
| 149 | "chart_type": "Report", |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 150 | "chart_name": _("Accounts Payable Ageing"), |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 151 | "is_custom": 1, |
| 152 | "is_public": 1 |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 153 | }, |
| 154 | { |
| 155 | "doctype": "Dashboard Charts", |
| 156 | "name": "Budget Variance", |
| 157 | "owner": "Administrator", |
| 158 | "report_name": "Budget Variance Report", |
| 159 | "filters_json": json.dumps({ |
| 160 | "company": company.name, |
| 161 | "from_fiscal_year": fiscal_year[0], |
| 162 | "to_fiscal_year": fiscal_year[0], |
| 163 | "period": "Monthly", |
| 164 | "budget_against": "Cost Center" |
| 165 | }), |
| 166 | "type": "Bar", |
| 167 | "timeseries": 0, |
| 168 | "chart_type": "Report", |
| 169 | "chart_name": _("Budget Variance"), |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 170 | "is_custom": 1, |
| 171 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 172 | }, |
| 173 | { |
| 174 | "doctype": "Dashboard Charts", |
| 175 | "name": "Bank Balance", |
| 176 | "time_interval": "Quarterly", |
| 177 | "chart_name": "Bank Balance", |
| 178 | "timespan": "Last Year", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 179 | "filters_json": json.dumps({ |
| 180 | "company": company.name, |
| 181 | "account": bank_account |
| 182 | }), |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 183 | "source": "Account Balance Timeline", |
| 184 | "chart_type": "Custom", |
| 185 | "timeseries": 1, |
| 186 | "owner": "Administrator", |
| 187 | "type": "Line", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 188 | "width": "Half", |
| 189 | "is_public": 1 |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 190 | }, |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 191 | ] |
Deepesh Garg | 0defefd | 2020-05-11 12:14:46 +0530 | [diff] [blame] | 192 | |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 193 | def get_number_cards(): |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 194 | fiscal_year = get_fiscal_year(date=nowdate()) |
| 195 | year_start_date = get_date_str(fiscal_year[1]) |
| 196 | year_end_date = get_date_str(fiscal_year[2]) |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 197 | return [ |
| 198 | { |
| 199 | "doctype": "Number Card", |
| 200 | "document_type": "Payment Entry", |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 201 | "name": "Total Incoming Payment", |
| 202 | "filters_json": json.dumps([ |
| 203 | ['Payment Entry', 'docstatus', '=', 1], |
| 204 | ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]], |
| 205 | ['Payment Entry', 'payment_type', '=', 'Receive'] |
| 206 | ]), |
| 207 | "label": _("Total Incoming Payment"), |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 208 | "function": "Sum", |
| 209 | "aggregate_function_based_on": "base_received_amount", |
| 210 | "is_public": 1, |
| 211 | "is_custom": 1, |
| 212 | "show_percentage_stats": 1, |
Nabin Hait | 9aae6f4 | 2020-05-18 18:03:11 +0530 | [diff] [blame] | 213 | "stats_time_interval": "Monthly" |
| 214 | }, |
| 215 | { |
| 216 | "doctype": "Number Card", |
| 217 | "document_type": "Payment Entry", |
| 218 | "name": "Total Outgoing Payment", |
| 219 | "filters_json": json.dumps([ |
| 220 | ['Payment Entry', 'docstatus', '=', 1], |
| 221 | ['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]], |
| 222 | ['Payment Entry', 'payment_type', '=', 'Pay'] |
| 223 | ]), |
| 224 | "label": _("Total Outgoing Payment"), |
| 225 | "function": "Sum", |
| 226 | "aggregate_function_based_on": "base_paid_amount", |
| 227 | "is_public": 1, |
| 228 | "is_custom": 1, |
| 229 | "show_percentage_stats": 1, |
| 230 | "stats_time_interval": "Monthly" |
| 231 | }, |
| 232 | { |
| 233 | "doctype": "Number Card", |
| 234 | "document_type": "Sales Invoice", |
| 235 | "name": "Total Outgoing Bills", |
| 236 | "filters_json": json.dumps([ |
| 237 | ['Sales Invoice', 'docstatus', '=', 1], |
| 238 | ['Sales Invoice', 'posting_date', 'between', [year_start_date, year_end_date]] |
| 239 | ]), |
| 240 | "label": _("Total Outgoing Bills"), |
| 241 | "function": "Sum", |
| 242 | "aggregate_function_based_on": "base_net_total", |
| 243 | "is_public": 1, |
| 244 | "is_custom": 1, |
| 245 | "show_percentage_stats": 1, |
| 246 | "stats_time_interval": "Monthly" |
| 247 | }, |
| 248 | { |
| 249 | "doctype": "Number Card", |
| 250 | "document_type": "Purchase Invoice", |
| 251 | "name": "Total Incoming Bills", |
| 252 | "filters_json": json.dumps([ |
| 253 | ['Purchase Invoice', 'docstatus', '=', 1], |
| 254 | ['Purchase Invoice', 'posting_date', 'between', [year_start_date, year_end_date]] |
| 255 | ]), |
| 256 | "label": _("Total Incoming Bills"), |
| 257 | "function": "Sum", |
| 258 | "aggregate_function_based_on": "base_net_total", |
| 259 | "is_public": 1, |
| 260 | "is_custom": 1, |
| 261 | "show_percentage_stats": 1, |
| 262 | "stats_time_interval": "Monthly" |
Deepesh Garg | 8bbac6d | 2020-05-14 22:57:11 +0530 | [diff] [blame] | 263 | } |
Chinmay Pai | 30f26b4 | 2020-05-11 19:54:46 +0530 | [diff] [blame] | 264 | ] |