151 lines
4.0 KiB
TypeScript
151 lines
4.0 KiB
TypeScript
import "server-only";
|
|
|
|
import { desc, eq } from "drizzle-orm";
|
|
|
|
import { db } from "@/db/client";
|
|
import { billingReports, editorialAccounts } from "@/db/schema";
|
|
import { queries } from "@/modules/billing/queries";
|
|
import { monthPeriod, quarterPeriod, type BillingPeriod } from "@/modules/billing/period";
|
|
import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries";
|
|
|
|
export type FinalizedReportSummary = {
|
|
id: string;
|
|
reportNumber: number;
|
|
invoiceNumber: string | null;
|
|
netTotalCents: number;
|
|
status: string;
|
|
createdAt: Date;
|
|
};
|
|
|
|
export type BillingSelectionData = {
|
|
editorial: { id: string; name: string };
|
|
period: BillingPeriod;
|
|
periodLabel: string;
|
|
isQuarter: boolean;
|
|
year: number;
|
|
month: number | null;
|
|
quarter: number | null;
|
|
positions: BillingPosition[];
|
|
repeatDownloads: RepeatDownloadPosition[];
|
|
reports: FinalizedReportSummary[];
|
|
};
|
|
|
|
export async function loadBillingSelection(
|
|
editorialId: string,
|
|
searchParams: Record<string, string | string[] | undefined>,
|
|
): Promise<BillingSelectionData | null> {
|
|
const [editorial] = await db
|
|
.select({ id: editorialAccounts.id, name: editorialAccounts.name })
|
|
.from(editorialAccounts)
|
|
.where(eq(editorialAccounts.id, editorialId))
|
|
.limit(1);
|
|
if (!editorial) return null;
|
|
|
|
const { period, isQuarter, year, month, quarter, periodLabel } = resolvePeriod(searchParams);
|
|
|
|
const [positions, repeatDownloads, reportRows] = await Promise.all([
|
|
queries.listBillablePositions(editorialId, period),
|
|
queries.listNonBillableDownloads(editorialId, period),
|
|
db
|
|
.select({
|
|
id: billingReports.id,
|
|
reportNumber: billingReports.reportNumber,
|
|
invoiceNumber: billingReports.invoiceNumber,
|
|
netTotalCents: billingReports.netTotalCents,
|
|
status: billingReports.status,
|
|
createdAt: billingReports.createdAt,
|
|
})
|
|
.from(billingReports)
|
|
.where(eq(billingReports.editorialAccountId, editorialId))
|
|
.orderBy(desc(billingReports.createdAt)),
|
|
]);
|
|
|
|
return {
|
|
editorial,
|
|
period,
|
|
periodLabel,
|
|
isQuarter,
|
|
year,
|
|
month,
|
|
quarter,
|
|
positions,
|
|
repeatDownloads,
|
|
reports: reportRows.map((row) => ({
|
|
id: row.id,
|
|
reportNumber: row.reportNumber,
|
|
invoiceNumber: row.invoiceNumber,
|
|
netTotalCents: row.netTotalCents,
|
|
status: row.status,
|
|
createdAt: row.createdAt,
|
|
})),
|
|
};
|
|
}
|
|
|
|
export function resolvePeriod(searchParams: Record<string, string | string[] | undefined>): {
|
|
period: BillingPeriod;
|
|
isQuarter: boolean;
|
|
year: number;
|
|
month: number | null;
|
|
quarter: number | null;
|
|
periodLabel: string;
|
|
} {
|
|
const now = new Date();
|
|
const defaultYear = now.getUTCFullYear();
|
|
const defaultMonth = now.getUTCMonth() + 1;
|
|
|
|
const year = parseNumberParam(searchParams.year, defaultYear);
|
|
const quarterParam = firstParam(searchParams.quarter);
|
|
|
|
if (quarterParam !== undefined) {
|
|
const quarter = parseNumberParam(searchParams.quarter, Math.floor((defaultMonth - 1) / 3) + 1);
|
|
return {
|
|
period: quarterPeriod(year, quarter),
|
|
isQuarter: true,
|
|
year,
|
|
month: null,
|
|
quarter,
|
|
periodLabel: `Q${quarter} ${year}`,
|
|
};
|
|
}
|
|
|
|
const month = parseNumberParam(searchParams.month, defaultMonth);
|
|
return {
|
|
period: monthPeriod(year, month),
|
|
isQuarter: false,
|
|
year,
|
|
month,
|
|
quarter: null,
|
|
periodLabel: monthLabel(year, month),
|
|
};
|
|
}
|
|
|
|
function monthLabel(year: number, month: number): string {
|
|
const names = [
|
|
"Januar",
|
|
"Februar",
|
|
"März",
|
|
"April",
|
|
"Mai",
|
|
"Juni",
|
|
"Juli",
|
|
"August",
|
|
"September",
|
|
"Oktober",
|
|
"November",
|
|
"Dezember",
|
|
];
|
|
return `${names[month - 1]} ${year}`;
|
|
}
|
|
|
|
function parseNumberParam(
|
|
value: string | string[] | undefined,
|
|
fallback: number,
|
|
): number {
|
|
const parsed = Number.parseInt(firstParam(value) ?? "", 10);
|
|
return Number.isFinite(parsed) ? parsed : fallback;
|
|
}
|
|
|
|
function firstParam(value: string | string[] | undefined): string | undefined {
|
|
if (Array.isArray(value)) return value[0];
|
|
return value;
|
|
} |