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