Hide Budget Watch card when no dept budget split is configured

Previously showed a false "all departments within budget" message even
when Settings has no per-department split defined, since every share
was 0/undefined and nothing was actually being evaluated.
This commit is contained in:
jtricerolph 2026-07-25 20:49:51 +00:00
parent d0cfc39012
commit fcc1ab9e51

View file

@ -71,6 +71,7 @@ export default function Dashboard() {
const [week, setWeek] = useState<PeriodStats | null>(null)
const [month, setMonth] = useState<PeriodStats | null>(null)
const [overBudget, setOverBudget] = useState<DeptOverBudget[]>([])
const [hasDeptSplit, setHasDeptSplit] = useState(false)
const [weekLabel, setWeekLabel] = useState('')
const [monthLabel, setMonthLabel] = useState('')
const [statsLoading, setStatsLoading] = useState(true)
@ -169,8 +170,11 @@ export default function Dashboard() {
const monthNetSalesFull = monthSalesRes.days.reduce((s, d) => s + d.net_sales, 0)
// ── Budget-watch: departments forecast to finish the month over their budget share ──
// Only meaningful once a per-department split is actually configured (Settings) —
// otherwise every dept_pcts share is 0/undefined and there's nothing to evaluate.
const deptSplitConfigured = Object.values(monthActRes.dept_pcts).some(pct => pct > 0)
const overBudgetList: DeptOverBudget[] = []
if (monthBudgetFull != null) {
if (monthBudgetFull != null && deptSplitConfigured) {
for (const dep of monthDeptRows) {
const share = monthActRes.dept_pcts[dep.id] ?? 0
if (share <= 0) continue
@ -203,6 +207,7 @@ export default function Dashboard() {
pctSalesForecast: monthNetSalesFull > 0 ? (monthForecast / monthNetSalesFull) * 100 : null,
})
setOverBudget(overBudgetList.slice(0, 3))
setHasDeptSplit(deptSplitConfigured)
setWeekLabel(`${fmtDisplay(weekFrom)} ${fmtDisplay(weekTo)}`)
setMonthLabel(new Date(monthFrom + 'T00:00:00').toLocaleDateString('en-GB', { month: 'long', year: 'numeric' }))
} catch (e: unknown) {
@ -284,7 +289,7 @@ export default function Dashboard() {
{!statsLoading && !statsError && week && renderPeriodGrid(`This Week (${weekLabel})`, 'Actual WTD', 'Forecast Full Week', week)}
{!statsLoading && !statsError && month && renderPeriodGrid(`This Month (${monthLabel})`, 'Actual MTD', 'Forecast EOM', month)}
{!statsLoading && !statsError && month?.budgetFull != null && (
{!statsLoading && !statsError && month?.budgetFull != null && hasDeptSplit && (
<div className="card">
<div className="card-title" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<AlertTriangle size={16} strokeWidth={1.75} color={overBudget.length ? '#dc2626' : 'var(--app-primary)'} />