diff --git a/src/routes/dashboard/+page.server.ts b/src/routes/dashboard/+page.server.ts index 927d5ff..0e8042c 100644 --- a/src/routes/dashboard/+page.server.ts +++ b/src/routes/dashboard/+page.server.ts @@ -52,7 +52,7 @@ export const load: PageServerLoad = async ({ locals }) => { const soonDate = dateOnly(addDays(today, 60)); const monthStart = dateOnly(startOfMonth(today)); const monthEnd = dateOnly(endOfMonth(today)); - const sixMonthsAgo = startOfMonth(addMonths(today, -5)); + const currentTrendStart = startOfMonth(addMonths(today, -5)); const activeContractCondition = and( eq(contracts.organizationId, activeOrganizationId), @@ -202,13 +202,7 @@ export const load: PageServerLoad = async ({ locals }) => { totalGbp: invoices.totalGbp }) .from(invoices) - .where( - and( - eq(invoices.organizationId, activeOrganizationId), - gte(invoices.issueDate, dateOnly(sixMonthsAgo)), - isNull(invoices.archivedAt) - ) - ) + .where(and(eq(invoices.organizationId, activeOrganizationId), isNull(invoices.archivedAt))) ]); const activeContractIds = activeContractRows.map((contract) => contract.id); @@ -292,9 +286,17 @@ export const load: PageServerLoad = async ({ locals }) => { roomTypes.set(room.type, summary); } + const hasCurrentTrendInvoices = billingTrendInvoiceRows.some( + (invoice) => parseISO(invoice.issueDate).getTime() >= currentTrendStart.getTime() + ); + const trendEnd = + hasCurrentTrendInvoices || recentInvoiceRows.length === 0 + ? today + : parseISO(recentInvoiceRows[0].issueDate); + const trendStart = startOfMonth(addMonths(trendEnd, -5)); const billingTrendMap = new Map(); for (let index = 0; index < 6; index += 1) { - const month = addMonths(sixMonthsAgo, index); + const month = addMonths(trendStart, index); billingTrendMap.set(monthKey(month), { month: monthKey(month), label: monthLabel(month), @@ -341,6 +343,7 @@ export const load: PageServerLoad = async ({ locals }) => { recentInvoices: recentInvoiceRows, meta: { monthLabel: monthLabel(today), + billingTrendDescription: `Issued invoice totals from ${monthLabel(trendStart)} to ${monthLabel(trendEnd)}.`, overdueInvoiceBasis: 'Invoices have no payment status yet, so this shows invoices past due date.' } diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index 106f758..b4f0826 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -24,7 +24,11 @@
- +
diff --git a/src/routes/dashboard/billing-trend.svelte b/src/routes/dashboard/billing-trend.svelte index 8726b50..cb4499f 100644 --- a/src/routes/dashboard/billing-trend.svelte +++ b/src/routes/dashboard/billing-trend.svelte @@ -6,9 +6,11 @@ let { records, + description, formatMoney }: { records: PageData['billingTrend']; + description: string; formatMoney: (value: unknown) => string; } = $props(); @@ -19,7 +21,7 @@ Billing trend - Issued invoice totals over the last six months. + {description} {#if hasValues}