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 |
| 6 | from frappe.utils import nowdate, add_months |
| 7 | |
| 8 | def get_company_for_dashboards(): |
| 9 | company = frappe.defaults.get_defaults().company |
| 10 | if company: |
| 11 | return company |
| 12 | else: |
| 13 | company_list = frappe.get_list("Company") |
| 14 | if company_list: |
| 15 | return company_list[0].name |
| 16 | return None |
| 17 | |
| 18 | def get_data(): |
| 19 | return frappe._dict({ |
| 20 | "dashboards": get_dashboards(), |
| 21 | "charts": get_charts() |
| 22 | }) |
| 23 | |
| 24 | def get_dashboards(): |
| 25 | |
| 26 | return [{ |
| 27 | "name": "Accounts Dashboard", |
| 28 | "dashboard_name": "Accounts", |
| 29 | "doctype": "Dashboard", |
| 30 | "charts": [ |
| 31 | { "chart": "Profit and Loss" , "width": "Full"}, |
| 32 | { "chart": "Incoming Bills"}, |
| 33 | { "chart": "Outgoing Bills"}, |
| 34 | { "chart": "Accounts Receivable Ageing"}, |
| 35 | { "chart": "Accounts Payable Ageing"}, |
| 36 | { "chart": "Bank Balance", "width": "Full"}, |
| 37 | ] |
| 38 | }] |
| 39 | |
| 40 | def get_charts(): |
| 41 | company = frappe.get_doc("Company", get_company_for_dashboards()) |
| 42 | bank_account = company.default_bank_account or get_account("Bank", company.name) |
| 43 | |
| 44 | return [ |
| 45 | { |
| 46 | "doctype": "Dashboard Charts", |
| 47 | "name": "Profit and Loss", |
| 48 | "owner": "Administrator", |
| 49 | "report_name": "Profit and Loss Statement", |
| 50 | "filters_json": json.dumps({ |
| 51 | "company": company.name, |
| 52 | "filter_based_on": "Date Range", |
| 53 | "period_start_date": add_months(nowdate(), -4), |
| 54 | "period_end_date": nowdate(), |
| 55 | "periodicity": "Monthly", |
| 56 | "include_default_book_entries": 1 |
| 57 | }), |
| 58 | "type": "Bar", |
| 59 | 'timeseries': 0, |
| 60 | "chart_type": "Report", |
| 61 | "chart_name": "Profit and Loss", |
| 62 | "is_custom": 1 |
| 63 | }, |
| 64 | { |
| 65 | "doctype": "Dashboard Chart", |
| 66 | "time_interval": "Monthly", |
| 67 | "name": "Incoming Bills", |
| 68 | "chart_name": "Incoming Bills (Purchase Invoice)", |
| 69 | "timespan": "Last Year", |
| 70 | "color": "#a83333", |
| 71 | "value_based_on": "base_grand_total", |
| 72 | "filters_json": json.dumps({}), |
| 73 | "chart_type": "Sum", |
| 74 | "timeseries": 1, |
| 75 | "based_on": "posting_date", |
| 76 | "owner": "Administrator", |
| 77 | "document_type": "Purchase Invoice", |
| 78 | "type": "Bar", |
| 79 | "width": "Half" |
| 80 | }, |
| 81 | { |
| 82 | "doctype": "Dashboard Chart", |
| 83 | "name": "Outgoing Bills", |
| 84 | "time_interval": "Monthly", |
| 85 | "chart_name": "Outgoing Bills (Sales Invoice)", |
| 86 | "timespan": "Last Year", |
| 87 | "color": "#7b933d", |
| 88 | "value_based_on": "base_grand_total", |
| 89 | "filters_json": json.dumps({}), |
| 90 | "chart_type": "Sum", |
| 91 | "timeseries": 1, |
| 92 | "based_on": "posting_date", |
| 93 | "owner": "Administrator", |
| 94 | "document_type": "Sales Invoice", |
| 95 | "type": "Bar", |
| 96 | "width": "Half" |
| 97 | }, |
| 98 | { |
| 99 | "doctype": "Dashboard Charts", |
| 100 | "name": "Accounts Receivable Ageing", |
| 101 | "owner": "Administrator", |
| 102 | "report_name": "Accounts Receivable", |
| 103 | "filters_json": json.dumps({ |
| 104 | "company": company.name, |
| 105 | "report_date": nowdate(), |
| 106 | "ageing_based_on": "Due Date", |
| 107 | "range1": 30, |
| 108 | "range2": 60, |
| 109 | "range3": 90, |
| 110 | "range4": 120 |
| 111 | }), |
| 112 | "type": "Donut", |
| 113 | 'timeseries': 0, |
| 114 | "chart_type": "Report", |
| 115 | "chart_name": "Accounts Receivable Ageing", |
| 116 | "is_custom": 1 |
| 117 | }, |
| 118 | { |
| 119 | "doctype": "Dashboard Charts", |
| 120 | "name": "Accounts Payable Ageing", |
| 121 | "owner": "Administrator", |
| 122 | "report_name": "Accounts Payable", |
| 123 | "filters_json": json.dumps({ |
| 124 | "company": company.name, |
| 125 | "report_date": nowdate(), |
| 126 | "ageing_based_on": "Due Date", |
| 127 | "range1": 30, |
| 128 | "range2": 60, |
| 129 | "range3": 90, |
| 130 | "range4": 120 |
| 131 | }), |
| 132 | "type": "Donut", |
| 133 | 'timeseries': 0, |
| 134 | "chart_type": "Report", |
| 135 | "chart_name": "Accounts Payable Ageing", |
| 136 | "is_custom": 1 |
| 137 | }, |
| 138 | { |
| 139 | "doctype": "Dashboard Charts", |
| 140 | "name": "Bank Balance", |
| 141 | "time_interval": "Quarterly", |
| 142 | "chart_name": "Bank Balance", |
| 143 | "timespan": "Last Year", |
| 144 | "filters_json": json.dumps({"company": company.name, "account": bank_account}), |
| 145 | "source": "Account Balance Timeline", |
| 146 | "chart_type": "Custom", |
| 147 | "timeseries": 1, |
| 148 | "owner": "Administrator", |
| 149 | "type": "Line", |
| 150 | "width": "Half" |
| 151 | }, |
| 152 | |
| 153 | ] |