Shivam Mishra | 28436d2 | 2020-05-06 15:19:10 +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 | |
| 7 | |
| 8 | def get_data(): |
| 9 | return frappe._dict({ |
| 10 | "dashboards": get_dashboards(), |
| 11 | "charts": get_charts(), |
| 12 | }) |
| 13 | |
| 14 | def get_dashboards(): |
| 15 | return [{ |
| 16 | "name": "Accounts", |
| 17 | "dashboard_name": "Accounts", |
| 18 | "charts": [ |
| 19 | { "chart": "Outgoing Bills (Sales Invoice)" }, |
| 20 | { "chart": "Incoming Bills (Purchase Invoice)" }, |
| 21 | { "chart": "Bank Balance" }, |
| 22 | { "chart": "Income" }, |
| 23 | { "chart": "Expenses" } |
| 24 | ] |
| 25 | }] |
| 26 | |
| 27 | def get_charts(): |
| 28 | company = frappe.get_doc("Company", get_company_for_dashboards()) |
| 29 | income_account = company.default_income_account or get_account("Income Account", company.name) |
| 30 | expense_account = company.default_expense_account or get_account("Expense Account", company.name) |
| 31 | bank_account = company.default_bank_account or get_account("Bank", company.name) |
| 32 | |
| 33 | return [ |
| 34 | { |
| 35 | "doctype": "Dashboard Chart", |
| 36 | "time_interval": "Quarterly", |
| 37 | "name": "Income", |
| 38 | "chart_name": "Income", |
| 39 | "timespan": "Last Year", |
| 40 | "color": None, |
| 41 | "filters_json": json.dumps({"company": company.name, "account": income_account}), |
| 42 | "source": "Account Balance Timeline", |
| 43 | "chart_type": "Custom", |
| 44 | "timeseries": 1, |
| 45 | "owner": "Administrator", |
| 46 | "type": "Line" |
| 47 | }, |
| 48 | { |
| 49 | "doctype": "Dashboard Chart", |
| 50 | "time_interval": "Quarterly", |
| 51 | "name": "Expenses", |
| 52 | "chart_name": "Expenses", |
| 53 | "timespan": "Last Year", |
| 54 | "color": None, |
| 55 | "filters_json": json.dumps({"company": company.name, "account": expense_account}), |
| 56 | "source": "Account Balance Timeline", |
| 57 | "chart_type": "Custom", |
| 58 | "timeseries": 1, |
| 59 | "owner": "Administrator", |
| 60 | "type": "Line" |
| 61 | }, |
| 62 | { |
| 63 | "doctype": "Dashboard Chart", |
| 64 | "time_interval": "Quarterly", |
| 65 | "name": "Bank Balance", |
| 66 | "chart_name": "Bank Balance", |
| 67 | "timespan": "Last Year", |
| 68 | "color": "#ffb868", |
| 69 | "filters_json": json.dumps({"company": company.name, "account": bank_account}), |
| 70 | "source": "Account Balance Timeline", |
| 71 | "chart_type": "Custom", |
| 72 | "timeseries": 1, |
| 73 | "owner": "Administrator", |
| 74 | "type": "Line" |
| 75 | }, |
| 76 | { |
| 77 | "doctype": "Dashboard Chart", |
| 78 | "time_interval": "Monthly", |
| 79 | "name": "Incoming Bills (Purchase Invoice)", |
| 80 | "chart_name": "Incoming Bills (Purchase Invoice)", |
| 81 | "timespan": "Last Year", |
| 82 | "color": "#a83333", |
| 83 | "value_based_on": "base_grand_total", |
| 84 | "filters_json": json.dumps({}), |
| 85 | "chart_type": "Sum", |
| 86 | "timeseries": 1, |
| 87 | "based_on": "posting_date", |
| 88 | "owner": "Administrator", |
| 89 | "document_type": "Purchase Invoice", |
| 90 | "type": "Bar" |
| 91 | }, |
| 92 | { |
| 93 | "doctype": "Dashboard Chart", |
| 94 | "time_interval": "Monthly", |
| 95 | "name": "Outgoing Bills (Sales Invoice)", |
| 96 | "chart_name": "Outgoing Bills (Sales Invoice)", |
| 97 | "timespan": "Last Year", |
| 98 | "color": "#7b933d", |
| 99 | "value_based_on": "base_grand_total", |
| 100 | "filters_json": json.dumps({}), |
| 101 | "chart_type": "Sum", |
| 102 | "timeseries": 1, |
| 103 | "based_on": "posting_date", |
| 104 | "owner": "Administrator", |
| 105 | "document_type": "Sales Invoice", |
| 106 | "type": "Bar" |
| 107 | } |
| 108 | ] |
| 109 | |
| 110 | |
| 111 | def get_company_for_dashboards(): |
| 112 | company = frappe.defaults.get_defaults().company |
| 113 | if company: |
| 114 | return company |
| 115 | else: |
| 116 | company_list = frappe.get_list("Company") |
| 117 | if company_list: |
| 118 | return company_list[0].name |
| 119 | return None |