fix: show latest invoice in billing trend

This commit is contained in:
2026-06-25 11:50:24 +01:00
parent 3642ecfba6
commit 44165e39b1
3 changed files with 20 additions and 11 deletions
+12 -9
View File
@@ -52,7 +52,7 @@ export const load: PageServerLoad = async ({ locals }) => {
const soonDate = dateOnly(addDays(today, 60)); const soonDate = dateOnly(addDays(today, 60));
const monthStart = dateOnly(startOfMonth(today)); const monthStart = dateOnly(startOfMonth(today));
const monthEnd = dateOnly(endOfMonth(today)); const monthEnd = dateOnly(endOfMonth(today));
const sixMonthsAgo = startOfMonth(addMonths(today, -5)); const currentTrendStart = startOfMonth(addMonths(today, -5));
const activeContractCondition = and( const activeContractCondition = and(
eq(contracts.organizationId, activeOrganizationId), eq(contracts.organizationId, activeOrganizationId),
@@ -202,13 +202,7 @@ export const load: PageServerLoad = async ({ locals }) => {
totalGbp: invoices.totalGbp totalGbp: invoices.totalGbp
}) })
.from(invoices) .from(invoices)
.where( .where(and(eq(invoices.organizationId, activeOrganizationId), isNull(invoices.archivedAt)))
and(
eq(invoices.organizationId, activeOrganizationId),
gte(invoices.issueDate, dateOnly(sixMonthsAgo)),
isNull(invoices.archivedAt)
)
)
]); ]);
const activeContractIds = activeContractRows.map((contract) => contract.id); const activeContractIds = activeContractRows.map((contract) => contract.id);
@@ -292,9 +286,17 @@ export const load: PageServerLoad = async ({ locals }) => {
roomTypes.set(room.type, summary); 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<string, { month: string; label: string; totalGbp: number }>(); const billingTrendMap = new Map<string, { month: string; label: string; totalGbp: number }>();
for (let index = 0; index < 6; index += 1) { for (let index = 0; index < 6; index += 1) {
const month = addMonths(sixMonthsAgo, index); const month = addMonths(trendStart, index);
billingTrendMap.set(monthKey(month), { billingTrendMap.set(monthKey(month), {
month: monthKey(month), month: monthKey(month),
label: monthLabel(month), label: monthLabel(month),
@@ -341,6 +343,7 @@ export const load: PageServerLoad = async ({ locals }) => {
recentInvoices: recentInvoiceRows, recentInvoices: recentInvoiceRows,
meta: { meta: {
monthLabel: monthLabel(today), monthLabel: monthLabel(today),
billingTrendDescription: `Issued invoice totals from ${monthLabel(trendStart)} to ${monthLabel(trendEnd)}.`,
overdueInvoiceBasis: overdueInvoiceBasis:
'Invoices have no payment status yet, so this shows invoices past due date.' 'Invoices have no payment status yet, so this shows invoices past due date.'
} }
+5 -1
View File
@@ -24,7 +24,11 @@
<MetricCards metrics={data.metrics} monthLabel={data.meta.monthLabel} {formatMoney} /> <MetricCards metrics={data.metrics} monthLabel={data.meta.monthLabel} {formatMoney} />
<div class="grid gap-4 xl:grid-cols-2"> <div class="grid gap-4 xl:grid-cols-2">
<BillingTrend records={data.billingTrend} {formatMoney} /> <BillingTrend
records={data.billingTrend}
description={data.meta.billingTrendDescription}
{formatMoney}
/>
<RoomSummary metrics={data.metrics} summary={data.roomSummary} /> <RoomSummary metrics={data.metrics} summary={data.roomSummary} />
</div> </div>
+3 -1
View File
@@ -6,9 +6,11 @@
let { let {
records, records,
description,
formatMoney formatMoney
}: { }: {
records: PageData['billingTrend']; records: PageData['billingTrend'];
description: string;
formatMoney: (value: unknown) => string; formatMoney: (value: unknown) => string;
} = $props(); } = $props();
@@ -19,7 +21,7 @@
<Card.Root> <Card.Root>
<Card.Header> <Card.Header>
<Card.Title>Billing trend</Card.Title> <Card.Title>Billing trend</Card.Title>
<Card.Description>Issued invoice totals over the last six months.</Card.Description> <Card.Description>{description}</Card.Description>
</Card.Header> </Card.Header>
<Card.Content> <Card.Content>
{#if hasValues} {#if hasValues}