fix: show latest invoice in billing trend
This commit is contained in:
@@ -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<string, { month: string; label: string; totalGbp: number }>();
|
||||
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.'
|
||||
}
|
||||
|
||||
@@ -24,7 +24,11 @@
|
||||
<MetricCards metrics={data.metrics} monthLabel={data.meta.monthLabel} {formatMoney} />
|
||||
|
||||
<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} />
|
||||
</div>
|
||||
|
||||
|
||||
@@ -6,9 +6,11 @@
|
||||
|
||||
let {
|
||||
records,
|
||||
description,
|
||||
formatMoney
|
||||
}: {
|
||||
records: PageData['billingTrend'];
|
||||
description: string;
|
||||
formatMoney: (value: unknown) => string;
|
||||
} = $props();
|
||||
|
||||
@@ -19,7 +21,7 @@
|
||||
<Card.Root>
|
||||
<Card.Header>
|
||||
<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.Content>
|
||||
{#if hasValues}
|
||||
|
||||
Reference in New Issue
Block a user