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 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.'
}