2020-05-11 06:44:46 +00:00
|
|
|
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
import frappe
|
|
|
|
import json
|
2020-05-18 12:33:11 +00:00
|
|
|
from frappe.utils import nowdate, add_months, get_date_str
|
2020-05-14 17:27:11 +00:00
|
|
|
from frappe import _
|
2020-06-11 16:03:43 +00:00
|
|
|
from erpnext.accounts.utils import get_fiscal_year, get_account_name, FiscalYearError
|
|
|
|
|
|
|
|
def _get_fiscal_year(date=None):
|
|
|
|
try:
|
2020-06-12 09:59:40 +00:00
|
|
|
fiscal_year = get_fiscal_year(date=nowdate(), as_dict=True)
|
|
|
|
return fiscal_year
|
|
|
|
|
2020-06-11 16:03:43 +00:00
|
|
|
except FiscalYearError:
|
|
|
|
#if no fiscal year for current date then get default fiscal year
|
|
|
|
try:
|
2020-06-12 09:59:40 +00:00
|
|
|
fiscal_year = get_fiscal_year(as_dict=True)
|
|
|
|
return fiscal_year
|
|
|
|
|
2020-06-11 16:03:43 +00:00
|
|
|
except FiscalYearError:
|
|
|
|
#if still no fiscal year found then no accounting data created, return
|
|
|
|
return None
|
2020-05-11 06:44:46 +00:00
|
|
|
|
|
|
|
def get_company_for_dashboards():
|
|
|
|
company = frappe.defaults.get_defaults().company
|
|
|
|
if company:
|
|
|
|
return company
|
|
|
|
else:
|
|
|
|
company_list = frappe.get_list("Company")
|
|
|
|
if company_list:
|
|
|
|
return company_list[0].name
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_data():
|
2020-06-11 16:03:43 +00:00
|
|
|
|
|
|
|
fiscal_year = _get_fiscal_year(nowdate())
|
|
|
|
|
|
|
|
if not fiscal_year:
|
|
|
|
return frappe._dict()
|
|
|
|
|
2020-05-11 06:44:46 +00:00
|
|
|
return frappe._dict({
|
|
|
|
"dashboards": get_dashboards(),
|
2020-06-11 16:03:43 +00:00
|
|
|
"charts": get_charts(fiscal_year),
|
|
|
|
"number_cards": get_number_cards(fiscal_year)
|
2020-05-11 06:44:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
def get_dashboards():
|
|
|
|
return [{
|
2020-05-19 10:24:24 +00:00
|
|
|
"name": "Accounts",
|
|
|
|
"dashboard_name": "Accounts",
|
2020-05-11 06:44:46 +00:00
|
|
|
"doctype": "Dashboard",
|
|
|
|
"charts": [
|
|
|
|
{ "chart": "Profit and Loss" , "width": "Full"},
|
2020-05-18 12:33:11 +00:00
|
|
|
{ "chart": "Incoming Bills (Purchase Invoice)", "width": "Half"},
|
|
|
|
{ "chart": "Outgoing Bills (Sales Invoice)", "width": "Half"},
|
|
|
|
{ "chart": "Accounts Receivable Ageing", "width": "Half"},
|
|
|
|
{ "chart": "Accounts Payable Ageing", "width": "Half"},
|
2020-05-14 17:27:11 +00:00
|
|
|
{ "chart": "Budget Variance", "width": "Full"},
|
2020-05-18 12:33:11 +00:00
|
|
|
{ "chart": "Bank Balance", "width": "Full"}
|
2020-05-14 17:27:11 +00:00
|
|
|
],
|
|
|
|
"cards": [
|
2020-05-18 12:33:11 +00:00
|
|
|
{"card": "Total Outgoing Bills"},
|
|
|
|
{"card": "Total Incoming Bills"},
|
|
|
|
{"card": "Total Incoming Payment"},
|
|
|
|
{"card": "Total Outgoing Payment"}
|
2020-05-11 06:44:46 +00:00
|
|
|
]
|
|
|
|
}]
|
|
|
|
|
2020-06-11 16:03:43 +00:00
|
|
|
def get_charts(fiscal_year):
|
2020-05-11 06:44:46 +00:00
|
|
|
company = frappe.get_doc("Company", get_company_for_dashboards())
|
2020-05-18 12:33:11 +00:00
|
|
|
bank_account = company.default_bank_account or get_account_name("Bank", company=company.name)
|
2020-05-14 17:27:11 +00:00
|
|
|
default_cost_center = company.cost_center
|
2020-05-11 06:44:46 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Charts",
|
|
|
|
"name": "Profit and Loss",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"report_name": "Profit and Loss Statement",
|
|
|
|
"filters_json": json.dumps({
|
|
|
|
"company": company.name,
|
2020-05-28 09:48:47 +00:00
|
|
|
"filter_based_on": "Fiscal Year",
|
2020-06-12 09:59:40 +00:00
|
|
|
"from_fiscal_year": fiscal_year.get('name'),
|
|
|
|
"to_fiscal_year": fiscal_year.get('name'),
|
2020-05-11 06:44:46 +00:00
|
|
|
"periodicity": "Monthly",
|
|
|
|
"include_default_book_entries": 1
|
2020-05-18 12:33:11 +00:00
|
|
|
}),
|
2020-05-11 06:44:46 +00:00
|
|
|
"type": "Bar",
|
|
|
|
'timeseries': 0,
|
|
|
|
"chart_type": "Report",
|
2020-05-14 17:27:11 +00:00
|
|
|
"chart_name": _("Profit and Loss"),
|
2020-05-18 12:33:11 +00:00
|
|
|
"is_custom": 1,
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Chart",
|
|
|
|
"time_interval": "Monthly",
|
2020-05-18 12:33:11 +00:00
|
|
|
"name": "Incoming Bills (Purchase Invoice)",
|
2020-05-14 17:27:11 +00:00
|
|
|
"chart_name": _("Incoming Bills (Purchase Invoice)"),
|
2020-05-11 06:44:46 +00:00
|
|
|
"timespan": "Last Year",
|
|
|
|
"color": "#a83333",
|
2020-05-18 12:33:11 +00:00
|
|
|
"value_based_on": "base_net_total",
|
2020-05-19 13:38:30 +00:00
|
|
|
"filters_json": json.dumps([["Purchase Invoice", "docstatus", "=", 1]]),
|
2020-05-11 06:44:46 +00:00
|
|
|
"chart_type": "Sum",
|
|
|
|
"timeseries": 1,
|
|
|
|
"based_on": "posting_date",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"document_type": "Purchase Invoice",
|
|
|
|
"type": "Bar",
|
2020-05-18 12:33:11 +00:00
|
|
|
"width": "Half",
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Chart",
|
2020-05-18 12:33:11 +00:00
|
|
|
"name": "Outgoing Bills (Sales Invoice)",
|
2020-05-11 06:44:46 +00:00
|
|
|
"time_interval": "Monthly",
|
2020-05-14 17:27:11 +00:00
|
|
|
"chart_name": _("Outgoing Bills (Sales Invoice)"),
|
2020-05-11 06:44:46 +00:00
|
|
|
"timespan": "Last Year",
|
|
|
|
"color": "#7b933d",
|
2020-05-18 12:33:11 +00:00
|
|
|
"value_based_on": "base_net_total",
|
2020-05-19 13:38:30 +00:00
|
|
|
"filters_json": json.dumps([["Sales Invoice", "docstatus", "=", 1]]),
|
2020-05-11 06:44:46 +00:00
|
|
|
"chart_type": "Sum",
|
|
|
|
"timeseries": 1,
|
|
|
|
"based_on": "posting_date",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"document_type": "Sales Invoice",
|
|
|
|
"type": "Bar",
|
2020-05-18 12:33:11 +00:00
|
|
|
"width": "Half",
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Charts",
|
|
|
|
"name": "Accounts Receivable Ageing",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"report_name": "Accounts Receivable",
|
|
|
|
"filters_json": json.dumps({
|
|
|
|
"company": company.name,
|
|
|
|
"report_date": nowdate(),
|
|
|
|
"ageing_based_on": "Due Date",
|
|
|
|
"range1": 30,
|
|
|
|
"range2": 60,
|
|
|
|
"range3": 90,
|
|
|
|
"range4": 120
|
|
|
|
}),
|
|
|
|
"type": "Donut",
|
|
|
|
'timeseries': 0,
|
|
|
|
"chart_type": "Report",
|
2020-05-14 17:27:11 +00:00
|
|
|
"chart_name": _("Accounts Receivable Ageing"),
|
2020-05-18 12:33:11 +00:00
|
|
|
"is_custom": 1,
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Charts",
|
|
|
|
"name": "Accounts Payable Ageing",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"report_name": "Accounts Payable",
|
|
|
|
"filters_json": json.dumps({
|
|
|
|
"company": company.name,
|
|
|
|
"report_date": nowdate(),
|
|
|
|
"ageing_based_on": "Due Date",
|
|
|
|
"range1": 30,
|
|
|
|
"range2": 60,
|
|
|
|
"range3": 90,
|
|
|
|
"range4": 120
|
2020-05-14 17:27:11 +00:00
|
|
|
}),
|
2020-05-11 06:44:46 +00:00
|
|
|
"type": "Donut",
|
|
|
|
'timeseries': 0,
|
|
|
|
"chart_type": "Report",
|
2020-05-14 17:27:11 +00:00
|
|
|
"chart_name": _("Accounts Payable Ageing"),
|
2020-05-18 12:33:11 +00:00
|
|
|
"is_custom": 1,
|
|
|
|
"is_public": 1
|
2020-05-14 17:27:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Charts",
|
|
|
|
"name": "Budget Variance",
|
|
|
|
"owner": "Administrator",
|
|
|
|
"report_name": "Budget Variance Report",
|
|
|
|
"filters_json": json.dumps({
|
|
|
|
"company": company.name,
|
2020-06-12 09:59:40 +00:00
|
|
|
"from_fiscal_year": fiscal_year.get('name'),
|
|
|
|
"to_fiscal_year": fiscal_year.get('name'),
|
2020-05-14 17:27:11 +00:00
|
|
|
"period": "Monthly",
|
|
|
|
"budget_against": "Cost Center"
|
|
|
|
}),
|
|
|
|
"type": "Bar",
|
|
|
|
"timeseries": 0,
|
|
|
|
"chart_type": "Report",
|
|
|
|
"chart_name": _("Budget Variance"),
|
2020-05-18 12:33:11 +00:00
|
|
|
"is_custom": 1,
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Dashboard Charts",
|
|
|
|
"name": "Bank Balance",
|
|
|
|
"time_interval": "Quarterly",
|
|
|
|
"chart_name": "Bank Balance",
|
|
|
|
"timespan": "Last Year",
|
2020-05-18 12:33:11 +00:00
|
|
|
"filters_json": json.dumps({
|
|
|
|
"company": company.name,
|
|
|
|
"account": bank_account
|
|
|
|
}),
|
2020-05-11 06:44:46 +00:00
|
|
|
"source": "Account Balance Timeline",
|
|
|
|
"chart_type": "Custom",
|
|
|
|
"timeseries": 1,
|
|
|
|
"owner": "Administrator",
|
|
|
|
"type": "Line",
|
2020-05-18 12:33:11 +00:00
|
|
|
"width": "Half",
|
|
|
|
"is_public": 1
|
2020-05-11 06:44:46 +00:00
|
|
|
},
|
2020-05-14 17:27:11 +00:00
|
|
|
]
|
2020-05-11 06:44:46 +00:00
|
|
|
|
2020-06-11 16:03:43 +00:00
|
|
|
def get_number_cards(fiscal_year):
|
|
|
|
|
2020-06-12 09:59:40 +00:00
|
|
|
year_start_date = get_date_str(fiscal_year.get("year_start_date"))
|
|
|
|
year_end_date = get_date_str(fiscal_year.get("year_end_date"))
|
2020-05-14 17:27:11 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"doctype": "Number Card",
|
|
|
|
"document_type": "Payment Entry",
|
2020-05-18 12:33:11 +00:00
|
|
|
"name": "Total Incoming Payment",
|
|
|
|
"filters_json": json.dumps([
|
|
|
|
['Payment Entry', 'docstatus', '=', 1],
|
|
|
|
['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
|
|
|
|
['Payment Entry', 'payment_type', '=', 'Receive']
|
|
|
|
]),
|
|
|
|
"label": _("Total Incoming Payment"),
|
2020-05-14 17:27:11 +00:00
|
|
|
"function": "Sum",
|
|
|
|
"aggregate_function_based_on": "base_received_amount",
|
|
|
|
"is_public": 1,
|
|
|
|
"is_custom": 1,
|
|
|
|
"show_percentage_stats": 1,
|
2020-05-18 12:33:11 +00:00
|
|
|
"stats_time_interval": "Monthly"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Number Card",
|
|
|
|
"document_type": "Payment Entry",
|
|
|
|
"name": "Total Outgoing Payment",
|
|
|
|
"filters_json": json.dumps([
|
|
|
|
['Payment Entry', 'docstatus', '=', 1],
|
|
|
|
['Payment Entry', 'posting_date', 'between', [year_start_date, year_end_date]],
|
|
|
|
['Payment Entry', 'payment_type', '=', 'Pay']
|
|
|
|
]),
|
|
|
|
"label": _("Total Outgoing Payment"),
|
|
|
|
"function": "Sum",
|
|
|
|
"aggregate_function_based_on": "base_paid_amount",
|
|
|
|
"is_public": 1,
|
|
|
|
"is_custom": 1,
|
|
|
|
"show_percentage_stats": 1,
|
|
|
|
"stats_time_interval": "Monthly"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Number Card",
|
|
|
|
"document_type": "Sales Invoice",
|
|
|
|
"name": "Total Outgoing Bills",
|
|
|
|
"filters_json": json.dumps([
|
|
|
|
['Sales Invoice', 'docstatus', '=', 1],
|
|
|
|
['Sales Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
|
|
|
|
]),
|
|
|
|
"label": _("Total Outgoing Bills"),
|
|
|
|
"function": "Sum",
|
|
|
|
"aggregate_function_based_on": "base_net_total",
|
|
|
|
"is_public": 1,
|
|
|
|
"is_custom": 1,
|
|
|
|
"show_percentage_stats": 1,
|
|
|
|
"stats_time_interval": "Monthly"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"doctype": "Number Card",
|
|
|
|
"document_type": "Purchase Invoice",
|
|
|
|
"name": "Total Incoming Bills",
|
|
|
|
"filters_json": json.dumps([
|
|
|
|
['Purchase Invoice', 'docstatus', '=', 1],
|
|
|
|
['Purchase Invoice', 'posting_date', 'between', [year_start_date, year_end_date]]
|
|
|
|
]),
|
|
|
|
"label": _("Total Incoming Bills"),
|
|
|
|
"function": "Sum",
|
|
|
|
"aggregate_function_based_on": "base_net_total",
|
|
|
|
"is_public": 1,
|
|
|
|
"is_custom": 1,
|
|
|
|
"show_percentage_stats": 1,
|
|
|
|
"stats_time_interval": "Monthly"
|
2020-05-14 17:27:11 +00:00
|
|
|
}
|
2020-05-12 06:00:07 +00:00
|
|
|
]
|