Mobile friendly, lint correction, typescript error clear
This commit is contained in:
262
app/api/financial/costs/route.ts
Normal file
262
app/api/financial/costs/route.ts
Normal file
@@ -0,0 +1,262 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function stripUndefined<T extends Record<string, unknown>>(input: T) {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
if (value !== undefined) out[key] = value;
|
||||
}
|
||||
return out as T;
|
||||
}
|
||||
|
||||
function normalizeCurrency(value?: string | null) {
|
||||
if (!value) return null;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
return trimmed.toUpperCase();
|
||||
}
|
||||
|
||||
const numberField = z.preprocess(
|
||||
(value) => {
|
||||
if (value === "" || value === null || value === undefined) return null;
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : value;
|
||||
},
|
||||
z.number().finite().nullable()
|
||||
);
|
||||
|
||||
const numericFields = {
|
||||
machineCostPerMin: numberField.optional(),
|
||||
operatorCostPerMin: numberField.optional(),
|
||||
ratedRunningKw: numberField.optional(),
|
||||
idleKw: numberField.optional(),
|
||||
kwhRate: numberField.optional(),
|
||||
energyMultiplier: numberField.optional(),
|
||||
energyCostPerMin: numberField.optional(),
|
||||
scrapCostPerUnit: numberField.optional(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
};
|
||||
|
||||
const orgSchema = z
|
||||
.object({
|
||||
defaultCurrency: z.string().trim().min(1).max(8).optional(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const locationSchema = z
|
||||
.object({
|
||||
location: z.string().trim().min(1).max(80),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const machineSchema = z
|
||||
.object({
|
||||
machineId: z.string().uuid(),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const productSchema = z
|
||||
.object({
|
||||
sku: z.string().trim().min(1).max(64),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
const payloadSchema = z
|
||||
.object({
|
||||
org: orgSchema.optional(),
|
||||
locations: z.array(locationSchema).optional(),
|
||||
machines: z.array(machineSchema).optional(),
|
||||
products: z.array(productSchema).optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
async function ensureOrgFinancialProfile(
|
||||
tx: Prisma.TransactionClient,
|
||||
orgId: string,
|
||||
userId: string
|
||||
) {
|
||||
const existing = await tx.orgFinancialProfile.findUnique({ where: { orgId } });
|
||||
if (existing) return existing;
|
||||
return tx.orgFinancialProfile.create({
|
||||
data: {
|
||||
orgId,
|
||||
defaultCurrency: "USD",
|
||||
energyMultiplier: 1.0,
|
||||
updatedBy: userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFinancialConfig(orgId: string) {
|
||||
const [org, locations, machines, products] = await Promise.all([
|
||||
prisma.orgFinancialProfile.findUnique({ where: { orgId } }),
|
||||
prisma.locationFinancialOverride.findMany({ where: { orgId }, orderBy: { location: "asc" } }),
|
||||
prisma.machineFinancialOverride.findMany({ where: { orgId }, orderBy: { createdAt: "desc" } }),
|
||||
prisma.productCostOverride.findMany({ where: { orgId }, orderBy: { sku: "asc" } }),
|
||||
]);
|
||||
|
||||
return { org, locations, machines, products };
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.$transaction((tx) => ensureOrgFinancialProfile(tx, session.orgId, session.userId));
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = payloadSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await ensureOrgFinancialProfile(tx, session.orgId, session.userId);
|
||||
|
||||
if (data.org) {
|
||||
const updateData = stripUndefined({
|
||||
defaultCurrency: data.org.defaultCurrency?.trim().toUpperCase(),
|
||||
machineCostPerMin: data.org.machineCostPerMin,
|
||||
operatorCostPerMin: data.org.operatorCostPerMin,
|
||||
ratedRunningKw: data.org.ratedRunningKw,
|
||||
idleKw: data.org.idleKw,
|
||||
kwhRate: data.org.kwhRate,
|
||||
energyMultiplier: data.org.energyMultiplier == null ? undefined : data.org.energyMultiplier,
|
||||
energyCostPerMin: data.org.energyCostPerMin,
|
||||
scrapCostPerUnit: data.org.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: data.org.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
await tx.orgFinancialProfile.update({
|
||||
where: { orgId: session.orgId },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const machineIds = new Set((data.machines ?? []).map((m) => m.machineId));
|
||||
const validMachineIds = new Set<string>();
|
||||
if (machineIds.size > 0) {
|
||||
const rows = await tx.machine.findMany({
|
||||
where: { orgId: session.orgId, id: { in: Array.from(machineIds) } },
|
||||
select: { id: true },
|
||||
});
|
||||
rows.forEach((m) => validMachineIds.add(m.id));
|
||||
}
|
||||
|
||||
for (const loc of data.locations ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(loc.currency),
|
||||
machineCostPerMin: loc.machineCostPerMin,
|
||||
operatorCostPerMin: loc.operatorCostPerMin,
|
||||
ratedRunningKw: loc.ratedRunningKw,
|
||||
idleKw: loc.idleKw,
|
||||
kwhRate: loc.kwhRate,
|
||||
energyMultiplier: loc.energyMultiplier,
|
||||
energyCostPerMin: loc.energyCostPerMin,
|
||||
scrapCostPerUnit: loc.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: loc.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.locationFinancialOverride.upsert({
|
||||
where: { orgId_location: { orgId: session.orgId, location: loc.location } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
location: loc.location,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const machine of data.machines ?? []) {
|
||||
if (!validMachineIds.has(machine.machineId)) continue;
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(machine.currency),
|
||||
machineCostPerMin: machine.machineCostPerMin,
|
||||
operatorCostPerMin: machine.operatorCostPerMin,
|
||||
ratedRunningKw: machine.ratedRunningKw,
|
||||
idleKw: machine.idleKw,
|
||||
kwhRate: machine.kwhRate,
|
||||
energyMultiplier: machine.energyMultiplier,
|
||||
energyCostPerMin: machine.energyCostPerMin,
|
||||
scrapCostPerUnit: machine.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: machine.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.machineFinancialOverride.upsert({
|
||||
where: { orgId_machineId: { orgId: session.orgId, machineId: machine.machineId } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
machineId: machine.machineId,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const product of data.products ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(product.currency),
|
||||
rawMaterialCostPerUnit: product.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.productCostOverride.upsert({
|
||||
where: { orgId_sku: { orgId: session.orgId, sku: product.sku } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
sku: product.sku,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
156
app/api/financial/export/excel/route.ts
Normal file
156
app/api/financial/export/excel/route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function csvValue(value: string | number | null | undefined) {
|
||||
if (value === null || value === undefined) return "";
|
||||
const text = String(value);
|
||||
if (/[",\n]/.test(text)) {
|
||||
return `"${text.replace(/"/g, "\"\"")}"`;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null) {
|
||||
if (value == null || !Number.isFinite(value)) return "";
|
||||
return value.toFixed(4);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const header = [
|
||||
"org_name",
|
||||
"range_start",
|
||||
"range_end",
|
||||
"event_id",
|
||||
"event_ts",
|
||||
"event_type",
|
||||
"status",
|
||||
"severity",
|
||||
"category",
|
||||
"machine_id",
|
||||
"machine_name",
|
||||
"location",
|
||||
"work_order_id",
|
||||
"sku",
|
||||
"duration_sec",
|
||||
"cost_machine",
|
||||
"cost_operator",
|
||||
"cost_energy",
|
||||
"cost_scrap",
|
||||
"cost_raw_material",
|
||||
"cost_total",
|
||||
"currency",
|
||||
];
|
||||
|
||||
const rows = impact.events.map((event) => [
|
||||
orgName,
|
||||
start.toISOString(),
|
||||
end.toISOString(),
|
||||
event.id,
|
||||
event.ts.toISOString(),
|
||||
event.eventType,
|
||||
event.status,
|
||||
event.severity,
|
||||
event.category,
|
||||
event.machineId,
|
||||
event.machineName ?? "",
|
||||
event.location ?? "",
|
||||
event.workOrderId ?? "",
|
||||
event.sku ?? "",
|
||||
formatNumber(event.durationSec),
|
||||
formatNumber(event.costMachine),
|
||||
formatNumber(event.costOperator),
|
||||
formatNumber(event.costEnergy),
|
||||
formatNumber(event.costScrap),
|
||||
formatNumber(event.costRawMaterial),
|
||||
formatNumber(event.costTotal),
|
||||
event.currency,
|
||||
]);
|
||||
|
||||
const lines = [header, ...rows].map((row) => row.map(csvValue).join(","));
|
||||
const csv = lines.join("\n");
|
||||
|
||||
const fileName = `financial_events_${slugify(orgName)}.csv`;
|
||||
return new NextResponse(csv, {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
246
app/api/financial/export/pdf/route.ts
Normal file
246
app/api/financial/export/pdf/route.ts
Normal file
@@ -0,0 +1,246 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function escapeHtml(value: string) {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/\"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function formatMoney(value: number, currency: string) {
|
||||
if (!Number.isFinite(value)) return "--";
|
||||
try {
|
||||
return new Intl.NumberFormat("en-US", { style: "currency", currency, maximumFractionDigits: 2 }).format(value);
|
||||
} catch {
|
||||
return `${value.toFixed(2)} ${currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null, digits = 2) {
|
||||
if (value == null || !Number.isFinite(value)) return "--";
|
||||
return value.toFixed(digits);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const summaryBlocks = impact.currencySummaries
|
||||
.map(
|
||||
(summary) => `
|
||||
<div class="card">
|
||||
<div class="card-title">${escapeHtml(summary.currency)}</div>
|
||||
<div class="card-value">${escapeHtml(formatMoney(summary.totals.total, summary.currency))}</div>
|
||||
<div class="card-sub">Slow: ${escapeHtml(formatMoney(summary.totals.slowCycle, summary.currency))}</div>
|
||||
<div class="card-sub">Micro: ${escapeHtml(formatMoney(summary.totals.microstop, summary.currency))}</div>
|
||||
<div class="card-sub">Macro: ${escapeHtml(formatMoney(summary.totals.macrostop, summary.currency))}</div>
|
||||
<div class="card-sub">Scrap: ${escapeHtml(formatMoney(summary.totals.scrap, summary.currency))}</div>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const dailyTables = impact.currencySummaries
|
||||
.map((summary) => {
|
||||
const rows = summary.byDay
|
||||
.map(
|
||||
(row) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(row.day)}</td>
|
||||
<td>${escapeHtml(formatMoney(row.total, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.slowCycle, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.microstop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.macrostop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.scrap, summary.currency))}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `
|
||||
<section class="section">
|
||||
<h3>${escapeHtml(summary.currency)} Daily Breakdown</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Day</th>
|
||||
<th>Total</th>
|
||||
<th>Slow</th>
|
||||
<th>Micro</th>
|
||||
<th>Macro</th>
|
||||
<th>Scrap</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows || "<tr><td colspan=\"6\">No data</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
const eventRows = impact.events
|
||||
.map(
|
||||
(e) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(e.ts.toISOString())}</td>
|
||||
<td>${escapeHtml(e.eventType)}</td>
|
||||
<td>${escapeHtml(e.category)}</td>
|
||||
<td>${escapeHtml(e.machineName ?? "-")}</td>
|
||||
<td>${escapeHtml(e.location ?? "-")}</td>
|
||||
<td>${escapeHtml(e.sku ?? "-")}</td>
|
||||
<td>${escapeHtml(e.workOrderId ?? "-")}</td>
|
||||
<td>${escapeHtml(formatNumber(e.durationSec))}</td>
|
||||
<td>${escapeHtml(formatMoney(e.costTotal, e.currency))}</td>
|
||||
<td>${escapeHtml(e.currency)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const html = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Financial Impact Report</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #0f172a; margin: 32px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.muted { color: #64748b; font-size: 12px; }
|
||||
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 20px 0; }
|
||||
.card { border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px; }
|
||||
.card-title { font-size: 12px; text-transform: uppercase; color: #64748b; }
|
||||
.card-value { font-size: 20px; font-weight: 700; margin: 8px 0; }
|
||||
.card-sub { font-size: 12px; color: #475569; }
|
||||
.section { margin-top: 24px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 8px; }
|
||||
th, td { border: 1px solid #e2e8f0; padding: 8px; font-size: 12px; text-align: left; }
|
||||
th { background: #f8fafc; }
|
||||
footer { margin-top: 32px; text-align: right; font-size: 11px; color: #94a3b8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Financial Impact Report</h1>
|
||||
<div class="muted">${escapeHtml(orgName)} | ${escapeHtml(start.toISOString())} - ${escapeHtml(end.toISOString())}</div>
|
||||
</header>
|
||||
|
||||
<section class="cards">
|
||||
${summaryBlocks || "<div class=\"muted\">No totals yet.</div>"}
|
||||
</section>
|
||||
|
||||
${dailyTables}
|
||||
|
||||
<section class="section">
|
||||
<h3>Event Details</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Timestamp</th>
|
||||
<th>Event</th>
|
||||
<th>Category</th>
|
||||
<th>Machine</th>
|
||||
<th>Location</th>
|
||||
<th>SKU</th>
|
||||
<th>Work Order</th>
|
||||
<th>Duration (sec)</th>
|
||||
<th>Cost</th>
|
||||
<th>Currency</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${eventRows || "<tr><td colspan=\"10\">No events</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<footer>Power by MaliounTech</footer>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const fileName = `financial_report_${slugify(orgName)}.html`;
|
||||
return new NextResponse(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
71
app/api/financial/impact/route.ts
Normal file
71
app/api/financial/impact/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const result = await computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: false,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, ...result });
|
||||
}
|
||||
Reference in New Issue
Block a user