Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ frappe.query_reports["Budget Variance Report"] = {
return value;
},
};

frappe.query_reports["Budget Variance Report"].onload = function(report) {
let company = frappe.query_report.get_fileter_value("company");
if (!company) return;

frappe.call({
method: "frappe.client.get_value",
args: {
doctype: "Company",
filters: { name: company },
fieldname: "is_group",
},
callback: function(r) {
const filter = frappe.query_report.get_filter("accumulated_in_group_company");
if (!filter) return;

if (r.message && r.message.is_group) {
filter.df.hidden = 0;
} else {
filter.df.hidden = 1;
frappe.query_report.set_filter_value("accumulated_in_group_company", 0);
}
filter.refresh();
},
});
};

function get_filters() {
function get_dimensions() {
let result = [];
Expand Down Expand Up @@ -107,6 +134,13 @@ function get_filters() {
fieldtype: "Check",
default: 0,
},
{
fieldname: "accumulated_in_group_company",
label: __("Accumulated Values in Group Company"),
fieldtype: "Check",
default: 0,
hidden: 1,
},
];

return filters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@

from erpnext.controllers.trends import get_period_date_ranges, get_period_month_ranges

def get_companies_for_report(filters):
"""Return list of companies for consolidated reporting."""
company = filters.get("company")

# If not consolidating -> return only the selected company
if not filters.get("accumulated_in_group_company"):
return [company]

# Consolidated -> get all child companies + parent
companies = frappe.get_all(
"Company",
filters={"parent_company": company},
pluck="name"
)
# Always include parent company
if company not in companies:
companies.append(company)

return companies


def execute(filters=None):
if not filters:
Expand All @@ -24,6 +44,36 @@ def execute(filters=None):
period_month_ranges = get_period_month_ranges(filters["period"], filters["from_fiscal_year"])
cam_map = get_dimension_account_month_map(filters)

# consolidate dimension_account_month map accross companies
companies = get_companies_for_report(filters)
if filters.get("accumulated_in_group_company"):
consolidated_cam_map = {}
for comp in companies:
filters["company"] = comp
company_map = get_dimension_account_month_map(filters)

for dim, accounts in company_map.items():
if dim not in consolidated_cam_map:
consolidated_cam_map[dim] = {}
for acc, months in accounts.items():
if acc not in consolidated_cam_map[dim]:
consolidated_cam_map[dim][acc] = months
else:
# accumulate monthwise data
for year, mdata in months.items():
consolidated_cam_map[dim][acc].setdefault(year, {})
for month, vals in mdata.items():
consolidated_cam_map[dim][acc][year].setdefault(month, {})
for key in ("target", "actual"):
consolidated_cam_map[dim][acc][year][month][key] = \
consolidated_cam_map[dim][acc][year][month].get(key, 0) + vals.get(key, 0)
consolidated_cam_map[dim][acc][year][month]["variance"] = \
consolidated_cam_map[dim][acc][year][month]["target"] - consolidated_cam_map[dim][acc][year][month]["actual"]

cam_map = consolidated_cam_map
else:
cam_map = get_dimension_account_month_map(filters)

data = []
for dimension in dimensions:
dimension_items = cam_map.get(dimension)
Expand Down