import "server-only"; import { serverAuth } from "./pb"; import { requireUser } from "./auth"; import type { Asset, CostLine, FinancingTerm, InsuranceRate, Mechanic, OilItem, OilMovement, Operator, MonthlyIncome, MonthlyRunningCost, Overhead, Invoice, InvoiceLine, Site } from "./data"; export * from "./data"; async function dataPB() { await requireUser(); return serverAuth(); } export async function getAssets(): Promise { const pb = await dataPB(); return pb.collection("assets").getFullList({ sort: "category,name" }); } export async function getAssetsByCategory(category: string): Promise { const all = await getAssets(); return all.filter((a) => a.category === category); } export async function getMonthlyIncome(year = 2026): Promise { const pb = await dataPB(); return pb.collection("monthly_income").getFullList({ filter: `year = ${year}`, sort: "month", }); } // Part 1.2: YTD aggregations operate on actuals only by default. // `is_forecast` is a boolean field on monthly_income; rows where it's true are excluded. export async function getActualMonthlyIncome(year = 2026): Promise { const all = await getMonthlyIncome(year); return all.filter((r) => !(r as any).is_forecast); } export async function getRunningCosts(year = 2026): Promise { const pb = await dataPB(); return pb.collection("monthly_running_costs").getFullList({ filter: `year = ${year}`, sort: "month", }); } // Per-machine, per-month cost lines by kind — `insurance`, `instalment`, // `tracking`. This is a DIFFERENT collection from `monthly_running_costs`: the // running-cost rows are repairs & parts, while these are the fixed per-machine // charges the workbook splits out by term. Keep them apart or the /running // Overview total silently swallows an insurance figure it never showed. export async function getCostLines(kind?: string, year = 2026): Promise { const pb = await dataPB(); const filters = [`year = ${year}`]; if (kind) filters.push(`kind = "${kind}"`); return pb.collection("cost_lines").getFullList({ filter: filters.join(" && "), sort: "month", }); } /** * Insurance rate periods. The source of truth for a plant vehicle's premium — * `cost_lines` insurance rows are DERIVED from these, not the other way round. */ export async function getInsuranceRates(year = 2026): Promise { const pb = await dataPB(); return pb.collection("insurance_rates").getFullList({ filter: `year = ${year}`, sort: "month_from", }); } export async function getFinancingTerms(): Promise { const pb = await dataPB(); return pb.collection("financing_terms").getFullList({ sort: "installment" }); } export async function getOperators(): Promise { const pb = await dataPB(); return pb.collection("operators").getFullList({ sort: "name" }); } export async function getMechanics(): Promise { const pb = await dataPB(); return pb.collection("mechanics").getFullList({ sort: "name" }); } export async function getOilItems(): Promise { const pb = await dataPB(); return pb.collection("oil_items").getFullList({ sort: "code" }); } export async function getOilMovements(): Promise { const pb = await dataPB(); return pb.collection("oil_stock_movements").getFullList({ sort: "-created" }); } export async function getOverheads(): Promise { const pb = await dataPB(); return pb.collection("overheads").getFullList({ sort: "section,description" }); } export async function getOverheadsBySection(section: string): Promise { const all = await getOverheads(); return all.filter((o) => o.section === section); } export async function getInvoices(): Promise { const pb = await dataPB(); return pb.collection("invoices").getFullList({ sort: "-invoice_date,-invoice_no" }); } export async function getInvoiceLines(invoiceId?: string): Promise { const pb = await dataPB(); return pb.collection("invoice_lines").getFullList({ filter: invoiceId ? `invoice = "${invoiceId}"` : "", sort: "line_no", }); } export async function getSites(): Promise { const pb = await dataPB(); return pb.collection("sites").getFullList({ sort: "name" }); }