fix: distribute non-monthly budgets across months when creating budget map

This commit is contained in:
khushi8112
2025-12-30 16:46:24 +05:30
parent e3fb7f4c47
commit c57a43b3f4

View File

@@ -81,26 +81,48 @@ def build_budget_map(budget_records, filters):
actual_amt = get_actual_details(budget.dimension, filters) actual_amt = get_actual_details(budget.dimension, filters)
budget_map.setdefault(budget.dimension, {}) budget_map.setdefault(budget.dimension, {})
budget_map[budget.dimension].setdefault(budget.account, {}) budget_map[budget.dimension].setdefault(budget.account, {})
budget_distributions = get_budget_distributions(budget) budget_distributions = get_budget_distributions(budget)
for row in budget_distributions: for row in budget_distributions:
fiscal_year = get_fiscal_year(row.start_date)[0] months = get_months_in_range(row.start_date, row.end_date)
month = row.start_date.strftime("%B") monthly_budget = flt(row.amount) / len(months)
budget_map[budget.dimension][budget.account].setdefault(fiscal_year, {})
budget_map[budget.dimension][budget.account][fiscal_year].setdefault(month, {}) for month_date in months:
budget_map[budget.dimension][budget.account][fiscal_year][month] = { fiscal_year = get_fiscal_year(month_date)[0]
"budget": row.amount, month = month_date.strftime("%B")
"actual": 0,
} budget_map[budget.dimension][budget.account].setdefault(fiscal_year, {})
for ad in actual_amt.get(budget.account, []): budget_map[budget.dimension][budget.account][fiscal_year].setdefault(
if ad.month_name == month and ad.fiscal_year == fiscal_year: month,
budget_map[budget.dimension][budget.account][fiscal_year][month]["actual"] += flt( {
ad.debit "budget": 0,
) - flt(ad.credit) "actual": 0,
},
)
budget_map[budget.dimension][budget.account][fiscal_year][month]["budget"] += monthly_budget
for ad in actual_amt.get(budget.account, []):
if ad.month_name == month and ad.fiscal_year == fiscal_year:
budget_map[budget.dimension][budget.account][fiscal_year][month]["actual"] += flt(
ad.debit
) - flt(ad.credit)
return budget_map return budget_map
def get_months_in_range(start_date, end_date):
months = []
current = start_date
while current <= end_date:
months.append(current)
current = add_months(current, 1)
return months
def get_budget_distributions(budget): def get_budget_distributions(budget):
return frappe.db.sql( return frappe.db.sql(
""" """