Fix current-period % Budget bugs on Rolling 12 Weeks/Months tables

Rolling12Months: current month's budget wasn't prorated at all, so
%Budget compared MTD actual against the FULL monthly budget — always
looked artificially favorable. Now prorates by elapsed PY DOW-matched
sales share (flat day-count fallback), reusing already-fetched data.

Rolling12Weeks: budget was prorated, but the elapsed-day count mixed a
real timestamp with Math.ceil(...)+1, always overstating elapsed days
by one and inflating the budget denominator. Rewrote to sum per-day
budgets by date string (no fractional-time bug), and applied the same
PY DOW-weighting as the Weekly page, sourcing each day from its own
calendar month so weeks spanning a month boundary split correctly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-23 21:18:30 +00:00
parent 47e899f636
commit a1a8900196
2 changed files with 72 additions and 14 deletions

View file

@ -90,8 +90,19 @@ export default function Rolling12Months() {
if (date >= monthFrom && date <= effectiveTo) { sales += val.sales; pySales += val.py }
}
const monKey = `${monthFrom}`
const budget = budgetMap[monKey] ?? null
// Budget: prorated to elapsed-to-date by each day's share of the month's PY (DOW-matched)
// sales — falls back to flat day-count when PY data is missing. For complete past months
// effectiveTo === monthTo, so this always resolves to the full budget (fraction = 1).
let monthPyTotal = 0
for (const [date, val] of Object.entries(salesByDate)) {
if (date >= monthFrom && date <= monthTo) monthPyTotal += val.py
}
const monKey = `${monthFrom}`
const budgetRaw = budgetMap[monKey] ?? null
const mtdFrac = monthPyTotal > 0
? pySales / monthPyTotal
: parseInt(effectiveTo.slice(8), 10) / dim
const budget = budgetRaw != null ? budgetRaw * mtdFrac : null
const label = `${MONTH_LABELS[month - 1]}-${String(year).slice(2)}`
tableRows.push({ label, wages, budget, sales, pySales, partial: isCurrentMonth })