mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-23 15:09:20 +00:00
Merge pull request #52932 from frappe/mergify/bp/version-15-hotfix/pr-52824
refactor: separate construction of chart related data from `get_columns()` (backport #52824)
This commit is contained in:
@@ -96,7 +96,7 @@ def execute(filters=None):
|
|||||||
filters.periodicity, period_list, filters.accumulated_values, company=filters.company
|
filters.periodicity, period_list, filters.accumulated_values, company=filters.company
|
||||||
)
|
)
|
||||||
|
|
||||||
chart = get_chart_data(filters, columns, asset, liability, equity, currency)
|
chart = get_chart_data(filters, period_list, asset, liability, equity, currency)
|
||||||
|
|
||||||
report_summary, primitive_summary = get_report_summary(
|
report_summary, primitive_summary = get_report_summary(
|
||||||
period_list, asset, liability, equity, provisional_profit_loss, currency, filters
|
period_list, asset, liability, equity, provisional_profit_loss, currency, filters
|
||||||
@@ -225,18 +225,19 @@ def get_report_summary(
|
|||||||
], (net_asset - net_liability + net_equity)
|
], (net_asset - net_liability + net_equity)
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(filters, columns, asset, liability, equity, currency):
|
def get_chart_data(filters, chart_columns, asset, liability, equity, currency):
|
||||||
labels = [d.get("label") for d in columns[2:]]
|
labels = [col.get("label") for col in chart_columns]
|
||||||
|
|
||||||
asset_data, liability_data, equity_data = [], [], []
|
asset_data, liability_data, equity_data = [], [], []
|
||||||
|
|
||||||
for p in columns[2:]:
|
for col in chart_columns:
|
||||||
|
key = col.get("key") or col.get("fieldname")
|
||||||
if asset:
|
if asset:
|
||||||
asset_data.append(asset[-2].get(p.get("fieldname")))
|
asset_data.append(asset[-2].get(key))
|
||||||
if liability:
|
if liability:
|
||||||
liability_data.append(liability[-2].get(p.get("fieldname")))
|
liability_data.append(liability[-2].get(key))
|
||||||
if equity:
|
if equity:
|
||||||
equity_data.append(equity[-2].get(p.get("fieldname")))
|
equity_data.append(equity[-2].get(key))
|
||||||
|
|
||||||
datasets = []
|
datasets = []
|
||||||
if asset_data:
|
if asset_data:
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ def execute(filters=None):
|
|||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
chart = get_chart_data(columns, data, company_currency)
|
chart = get_chart_data(period_list, data, company_currency)
|
||||||
|
|
||||||
report_summary = get_report_summary(summary_data, company_currency)
|
report_summary = get_report_summary(summary_data, company_currency)
|
||||||
|
|
||||||
@@ -411,13 +411,12 @@ def get_report_summary(summary_data, currency):
|
|||||||
return report_summary
|
return report_summary
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(columns, data, currency):
|
def get_chart_data(period_list, data, currency):
|
||||||
labels = [d.get("label") for d in columns[2:]]
|
labels = [period.get("label") for period in period_list]
|
||||||
print(data)
|
|
||||||
datasets = [
|
datasets = [
|
||||||
{
|
{
|
||||||
"name": section.get("section").replace("'", ""),
|
"name": section.get("section").replace("'", ""),
|
||||||
"values": [section.get(d.get("fieldname")) for d in columns[2:]],
|
"values": [section.get(period.get("key")) for period in period_list],
|
||||||
}
|
}
|
||||||
for section in data
|
for section in data
|
||||||
if section.get("parent_section") is None and section.get("currency")
|
if section.get("parent_section") is None and section.get("currency")
|
||||||
|
|||||||
@@ -48,22 +48,25 @@ def execute(filters=None):
|
|||||||
return columns, data, message, chart
|
return columns, data, message, chart
|
||||||
|
|
||||||
fiscal_year = get_fiscal_year_data(filters.get("from_fiscal_year"), filters.get("to_fiscal_year"))
|
fiscal_year = get_fiscal_year_data(filters.get("from_fiscal_year"), filters.get("to_fiscal_year"))
|
||||||
companies_column, companies = get_companies(filters)
|
company_list, companies = get_companies(filters)
|
||||||
columns = get_columns(companies_column, filters)
|
company_columns = get_company_columns(company_list, filters)
|
||||||
|
columns = get_columns(company_columns)
|
||||||
|
|
||||||
if filters.get("report") == "Balance Sheet":
|
if filters.get("report") == "Balance Sheet":
|
||||||
data, message, chart, report_summary = get_balance_sheet_data(
|
data, message, chart, report_summary = get_balance_sheet_data(
|
||||||
fiscal_year, companies, columns, filters
|
fiscal_year, companies, company_columns, filters
|
||||||
)
|
)
|
||||||
elif filters.get("report") == "Profit and Loss Statement":
|
elif filters.get("report") == "Profit and Loss Statement":
|
||||||
data, message, chart, report_summary = get_profit_loss_data(fiscal_year, companies, columns, filters)
|
data, message, chart, report_summary = get_profit_loss_data(
|
||||||
|
fiscal_year, companies, company_columns, filters
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
data, report_summary = get_cash_flow_data(fiscal_year, companies, filters)
|
data, report_summary = get_cash_flow_data(fiscal_year, companies, filters)
|
||||||
|
|
||||||
return columns, data, message, chart, report_summary
|
return columns, data, message, chart, report_summary
|
||||||
|
|
||||||
|
|
||||||
def get_balance_sheet_data(fiscal_year, companies, columns, filters):
|
def get_balance_sheet_data(fiscal_year, companies, company_columns, filters):
|
||||||
asset = get_data(companies, "Asset", "Debit", fiscal_year, filters=filters)
|
asset = get_data(companies, "Asset", "Debit", fiscal_year, filters=filters)
|
||||||
|
|
||||||
liability = get_data(companies, "Liability", "Credit", fiscal_year, filters=filters)
|
liability = get_data(companies, "Liability", "Credit", fiscal_year, filters=filters)
|
||||||
@@ -116,7 +119,7 @@ def get_balance_sheet_data(fiscal_year, companies, columns, filters):
|
|||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
chart = get_chart_data(filters, columns, asset, liability, equity, company_currency)
|
chart = get_chart_data(filters, company_columns, asset, liability, equity, company_currency)
|
||||||
|
|
||||||
return data, message, chart, report_summary
|
return data, message, chart, report_summary
|
||||||
|
|
||||||
@@ -164,7 +167,7 @@ def get_root_account_name(root_type, company):
|
|||||||
return root_account[0][0]
|
return root_account[0][0]
|
||||||
|
|
||||||
|
|
||||||
def get_profit_loss_data(fiscal_year, companies, columns, filters):
|
def get_profit_loss_data(fiscal_year, companies, company_columns, filters):
|
||||||
income, expense, net_profit_loss = get_income_expense_data(companies, fiscal_year, filters)
|
income, expense, net_profit_loss = get_income_expense_data(companies, fiscal_year, filters)
|
||||||
company_currency = get_company_currency(filters)
|
company_currency = get_company_currency(filters)
|
||||||
|
|
||||||
@@ -174,7 +177,7 @@ def get_profit_loss_data(fiscal_year, companies, columns, filters):
|
|||||||
if net_profit_loss:
|
if net_profit_loss:
|
||||||
data.append(net_profit_loss)
|
data.append(net_profit_loss)
|
||||||
|
|
||||||
chart = get_pl_chart_data(filters, columns, income, expense, net_profit_loss, company_currency)
|
chart = get_pl_chart_data(filters, company_columns, income, expense, net_profit_loss, company_currency)
|
||||||
|
|
||||||
report_summary, primitive_summary = get_pl_summary(
|
report_summary, primitive_summary = get_pl_summary(
|
||||||
companies, "", income, expense, net_profit_loss, company_currency, filters, True
|
companies, "", income, expense, net_profit_loss, company_currency, filters, True
|
||||||
@@ -279,7 +282,30 @@ def get_account_type_based_data(account_type, companies, fiscal_year, filters):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_columns(companies, filters):
|
def get_company_columns(companies, filters):
|
||||||
|
company_columns = []
|
||||||
|
for company in companies:
|
||||||
|
apply_currency_formatter = 1 if not filters.presentation_currency else 0
|
||||||
|
currency = filters.presentation_currency
|
||||||
|
if not currency:
|
||||||
|
currency = erpnext.get_company_currency(company)
|
||||||
|
|
||||||
|
company_columns.append(
|
||||||
|
{
|
||||||
|
"fieldname": company,
|
||||||
|
"label": f"{company} ({currency})",
|
||||||
|
"fieldtype": "Currency",
|
||||||
|
"options": "currency",
|
||||||
|
"width": 150,
|
||||||
|
"apply_currency_formatter": apply_currency_formatter,
|
||||||
|
"company_name": company,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return company_columns
|
||||||
|
|
||||||
|
|
||||||
|
def get_columns(company_columns):
|
||||||
columns = [
|
columns = [
|
||||||
{
|
{
|
||||||
"fieldname": "account",
|
"fieldname": "account",
|
||||||
@@ -297,23 +323,7 @@ def get_columns(companies, filters):
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
for company in companies:
|
columns.extend(company_columns)
|
||||||
apply_currency_formatter = 1 if not filters.presentation_currency else 0
|
|
||||||
currency = filters.presentation_currency
|
|
||||||
if not currency:
|
|
||||||
currency = erpnext.get_company_currency(company)
|
|
||||||
|
|
||||||
columns.append(
|
|
||||||
{
|
|
||||||
"fieldname": company,
|
|
||||||
"label": f"{company} ({currency})",
|
|
||||||
"fieldtype": "Currency",
|
|
||||||
"options": "currency",
|
|
||||||
"width": 150,
|
|
||||||
"apply_currency_formatter": apply_currency_formatter,
|
|
||||||
"company_name": company,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return columns
|
return columns
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ def execute(filters=None):
|
|||||||
currency = filters.presentation_currency or frappe.get_cached_value(
|
currency = filters.presentation_currency or frappe.get_cached_value(
|
||||||
"Company", filters.company, "default_currency"
|
"Company", filters.company, "default_currency"
|
||||||
)
|
)
|
||||||
chart = get_chart_data(filters, columns, income, expense, net_profit_loss, currency)
|
chart = get_chart_data(filters, period_list, income, expense, net_profit_loss, currency)
|
||||||
|
|
||||||
report_summary, primitive_summary = get_report_summary(
|
report_summary, primitive_summary = get_report_summary(
|
||||||
period_list, filters.periodicity, income, expense, net_profit_loss, currency, filters
|
period_list, filters.periodicity, income, expense, net_profit_loss, currency, filters
|
||||||
@@ -158,18 +158,20 @@ def get_net_profit_loss(income, expense, period_list, company, currency=None, co
|
|||||||
return net_profit_loss
|
return net_profit_loss
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(filters, columns, income, expense, net_profit_loss, currency):
|
def get_chart_data(filters, chart_columns, income, expense, net_profit_loss, currency):
|
||||||
labels = [d.get("label") for d in columns[2:]]
|
labels = [col.get("label") for col in chart_columns]
|
||||||
|
|
||||||
income_data, expense_data, net_profit = [], [], []
|
income_data, expense_data, net_profit = [], [], []
|
||||||
|
|
||||||
for p in columns[2:]:
|
for col in chart_columns:
|
||||||
|
key = col.get("key") or col.get("fieldname")
|
||||||
|
|
||||||
if income:
|
if income:
|
||||||
income_data.append(income[-2].get(p.get("fieldname")))
|
income_data.append(income[-2].get(key))
|
||||||
if expense:
|
if expense:
|
||||||
expense_data.append(expense[-2].get(p.get("fieldname")))
|
expense_data.append(expense[-2].get(key))
|
||||||
if net_profit_loss:
|
if net_profit_loss:
|
||||||
net_profit.append(net_profit_loss.get(p.get("fieldname")))
|
net_profit.append(net_profit_loss.get(key))
|
||||||
|
|
||||||
datasets = []
|
datasets = []
|
||||||
if income_data:
|
if income_data:
|
||||||
|
|||||||
@@ -6,24 +6,25 @@ import frappe
|
|||||||
from frappe import _, scrub
|
from frappe import _, scrub
|
||||||
from frappe.utils import getdate, today
|
from frappe.utils import getdate, today
|
||||||
|
|
||||||
from erpnext.stock.report.stock_analytics.stock_analytics import get_period, get_period_date_ranges
|
from erpnext.stock.report.stock_analytics.stock_analytics import (
|
||||||
|
get_period,
|
||||||
|
get_period_columns,
|
||||||
|
get_period_date_ranges,
|
||||||
|
)
|
||||||
|
|
||||||
WORK_ORDER_STATUS_LIST = ["Not Started", "Overdue", "Pending", "Completed", "Closed", "Stopped"]
|
WORK_ORDER_STATUS_LIST = ["Not Started", "Overdue", "Pending", "Completed", "Closed", "Stopped"]
|
||||||
|
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
columns = get_columns(filters)
|
period_columns = get_period_columns(filters)
|
||||||
data, chart = get_data(filters, columns)
|
columns = get_columns(period_columns)
|
||||||
|
data, chart = get_data(filters, period_columns)
|
||||||
return columns, data, None, chart
|
return columns, data, None, chart
|
||||||
|
|
||||||
|
|
||||||
def get_columns(filters):
|
def get_columns(period_columns):
|
||||||
columns = [{"label": _("Status"), "fieldname": "status", "fieldtype": "Data", "width": 140}]
|
columns = [{"label": _("Status"), "fieldname": "status", "fieldtype": "Data", "width": 140}]
|
||||||
ranges = get_period_date_ranges(filters)
|
columns.extend(period_columns)
|
||||||
|
|
||||||
for _dummy, end_date in ranges:
|
|
||||||
period = get_period(end_date, filters)
|
|
||||||
columns.append({"label": _(period), "fieldname": scrub(period), "fieldtype": "Float", "width": 120})
|
|
||||||
|
|
||||||
return columns
|
return columns
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ def get_work_orders(filters):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_data(filters, columns):
|
def get_data(filters, period_columns):
|
||||||
ranges = build_ranges(filters)
|
ranges = build_ranges(filters)
|
||||||
period_labels = [scrub(pd) for _fd, _td, pd in ranges]
|
period_labels = [scrub(pd) for _fd, _td, pd in ranges]
|
||||||
periodic_data = {status: {pd: 0 for pd in period_labels} for status in WORK_ORDER_STATUS_LIST}
|
periodic_data = {status: {pd: 0 for pd in period_labels} for status in WORK_ORDER_STATUS_LIST}
|
||||||
@@ -84,7 +85,7 @@ def get_data(filters, columns):
|
|||||||
row[scrub(period)] = periodic_data[status].get(scrub(period), 0)
|
row[scrub(period)] = periodic_data[status].get(scrub(period), 0)
|
||||||
data.append(row)
|
data.append(row)
|
||||||
|
|
||||||
chart = get_chart_data(periodic_data, columns)
|
chart = get_chart_data(periodic_data, period_columns)
|
||||||
return data, chart
|
return data, chart
|
||||||
|
|
||||||
|
|
||||||
@@ -103,9 +104,9 @@ def build_ranges(filters):
|
|||||||
return ranges
|
return ranges
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(periodic_data, columns):
|
def get_chart_data(periodic_data, period_columns):
|
||||||
period_labels = [d.get("label") for d in columns[1:]]
|
period_labels = [col.get("label") for col in period_columns]
|
||||||
period_fieldnames = [d.get("fieldname") for d in columns[1:]]
|
period_fieldnames = [col.get("fieldname") for col in period_columns]
|
||||||
|
|
||||||
datasets = []
|
datasets = []
|
||||||
for status in WORK_ORDER_STATUS_LIST:
|
for status in WORK_ORDER_STATUS_LIST:
|
||||||
|
|||||||
@@ -17,14 +17,29 @@ from erpnext.stock.utils import is_reposting_item_valuation_in_progress
|
|||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
is_reposting_item_valuation_in_progress()
|
is_reposting_item_valuation_in_progress()
|
||||||
filters = frappe._dict(filters or {})
|
filters = frappe._dict(filters or {})
|
||||||
columns = get_columns(filters)
|
period_columns = get_period_columns(filters)
|
||||||
|
columns = get_columns(period_columns)
|
||||||
data = get_data(filters)
|
data = get_data(filters)
|
||||||
chart = get_chart_data(columns)
|
chart = get_chart_data(period_columns)
|
||||||
|
|
||||||
return columns, data, None, chart
|
return columns, data, None, chart
|
||||||
|
|
||||||
|
|
||||||
def get_columns(filters):
|
def get_period_columns(filters):
|
||||||
|
period_columns = []
|
||||||
|
ranges = get_period_date_ranges(filters)
|
||||||
|
|
||||||
|
for _dummy, end_date in ranges:
|
||||||
|
period = get_period(end_date, filters)
|
||||||
|
|
||||||
|
period_columns.append(
|
||||||
|
{"label": _(period), "fieldname": scrub(period), "fieldtype": "Float", "width": 120}
|
||||||
|
)
|
||||||
|
|
||||||
|
return period_columns
|
||||||
|
|
||||||
|
|
||||||
|
def get_columns(period_columns):
|
||||||
columns = [
|
columns = [
|
||||||
{"label": _("Item"), "options": "Item", "fieldname": "name", "fieldtype": "Link", "width": 140},
|
{"label": _("Item"), "options": "Item", "fieldname": "name", "fieldtype": "Link", "width": 140},
|
||||||
{
|
{
|
||||||
@@ -45,12 +60,7 @@ def get_columns(filters):
|
|||||||
{"label": _("UOM"), "fieldname": "uom", "fieldtype": "Data", "width": 120},
|
{"label": _("UOM"), "fieldname": "uom", "fieldtype": "Data", "width": 120},
|
||||||
]
|
]
|
||||||
|
|
||||||
ranges = get_period_date_ranges(filters)
|
columns.extend(period_columns)
|
||||||
|
|
||||||
for _dummy, end_date in ranges:
|
|
||||||
period = get_period(end_date, filters)
|
|
||||||
|
|
||||||
columns.append({"label": _(period), "fieldname": scrub(period), "fieldtype": "Float", "width": 120})
|
|
||||||
|
|
||||||
return columns
|
return columns
|
||||||
|
|
||||||
@@ -250,8 +260,8 @@ def get_data(filters):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_chart_data(columns):
|
def get_chart_data(period_columns):
|
||||||
labels = [d.get("label") for d in columns[5:]]
|
labels = [col.get("label") for col in period_columns]
|
||||||
chart = {"data": {"labels": labels, "datasets": []}}
|
chart = {"data": {"labels": labels, "datasets": []}}
|
||||||
chart["type"] = "line"
|
chart["type"] = "line"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user