next slice
This commit is contained in:
@@ -3,6 +3,8 @@ import { Router } from "express";
|
||||
|
||||
import { renderPdf } from "../../lib/pdf.js";
|
||||
import { requirePermissions } from "../../lib/rbac.js";
|
||||
import { getPurchaseOrderPdfData } from "../purchasing/service.js";
|
||||
import { getSalesDocumentPdfData } from "../sales/service.js";
|
||||
import { getShipmentPackingSlipData } from "../shipping/service.js";
|
||||
import { getActiveCompanyProfile } from "../settings/service.js";
|
||||
|
||||
@@ -17,6 +19,123 @@ function escapeHtml(value: string) {
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function formatDate(value: string | null | undefined) {
|
||||
return value ? new Date(value).toLocaleDateString() : "N/A";
|
||||
}
|
||||
|
||||
function buildAddressLines(record: {
|
||||
name: string;
|
||||
addressLine1: string;
|
||||
addressLine2: string;
|
||||
city: string;
|
||||
state: string;
|
||||
postalCode: string;
|
||||
country: string;
|
||||
}) {
|
||||
return [
|
||||
record.name,
|
||||
record.addressLine1,
|
||||
record.addressLine2,
|
||||
`${record.city}, ${record.state} ${record.postalCode}`.trim(),
|
||||
record.country,
|
||||
].filter((line) => line.trim().length > 0);
|
||||
}
|
||||
|
||||
function renderCommercialDocumentPdf(options: {
|
||||
company: Awaited<ReturnType<typeof getActiveCompanyProfile>>;
|
||||
title: string;
|
||||
documentNumber: string;
|
||||
issueDate: string;
|
||||
status: string;
|
||||
partyTitle: string;
|
||||
partyLines: string[];
|
||||
partyMeta: Array<{ label: string; value: string }>;
|
||||
documentMeta: Array<{ label: string; value: string }>;
|
||||
rows: string;
|
||||
totalsRows: string;
|
||||
notes: string;
|
||||
}) {
|
||||
const { company } = options;
|
||||
|
||||
return renderPdf(`
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
@page { margin: 16mm; }
|
||||
body { font-family: ${company.theme.fontFamily}, Arial, sans-serif; color: #1b1f29; font-size: 12px; }
|
||||
.page { display: flex; flex-direction: column; gap: 16px; }
|
||||
.header { display: flex; justify-content: space-between; gap: 24px; border-bottom: 2px solid ${company.theme.primaryColor}; padding-bottom: 16px; }
|
||||
.brand h1 { margin: 0; font-size: 24px; color: ${company.theme.primaryColor}; }
|
||||
.brand p { margin: 6px 0 0; color: #5a6a85; line-height: 1.45; }
|
||||
.document-meta { min-width: 280px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 18px; }
|
||||
.label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: #5a6a85; }
|
||||
.value { margin-top: 4px; font-size: 13px; font-weight: 600; }
|
||||
.grid { display: grid; grid-template-columns: 1.05fr 0.95fr; gap: 16px; }
|
||||
.card { border: 1px solid #d7deeb; border-radius: 14px; padding: 14px 16px; }
|
||||
.card-title { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: #5a6a85; margin-bottom: 8px; }
|
||||
.stack { display: flex; flex-direction: column; gap: 4px; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
thead th { text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: #5a6a85; background: #f4f7fb; padding: 10px 12px; border-bottom: 1px solid #d7deeb; }
|
||||
tbody td { padding: 12px; border-bottom: 1px solid #e6ebf3; vertical-align: top; }
|
||||
.number { text-align: right; white-space: nowrap; }
|
||||
.item-name { font-weight: 600; }
|
||||
.item-desc { margin-top: 4px; color: #5a6a85; font-size: 11px; }
|
||||
.summary { margin-left: auto; width: 320px; border: 1px solid #d7deeb; border-radius: 14px; overflow: hidden; }
|
||||
.summary table tbody td { padding: 10px 12px; }
|
||||
.summary table tbody tr:last-child td { font-size: 14px; font-weight: 700; background: #f4f7fb; }
|
||||
.notes { border: 1px solid #d7deeb; border-radius: 14px; padding: 14px 16px; min-height: 72px; white-space: pre-line; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="page">
|
||||
<div class="header">
|
||||
<div class="brand">
|
||||
<h1>${escapeHtml(company.companyName)}</h1>
|
||||
<p>${escapeHtml(company.addressLine1)}${company.addressLine2 ? `<br/>${escapeHtml(company.addressLine2)}` : ""}<br/>${escapeHtml(company.city)}, ${escapeHtml(company.state)} ${escapeHtml(company.postalCode)}<br/>${escapeHtml(company.country)}</p>
|
||||
</div>
|
||||
<div class="document-meta">
|
||||
<div><div class="label">Document</div><div class="value">${escapeHtml(options.title)}</div></div>
|
||||
<div><div class="label">Number</div><div class="value">${escapeHtml(options.documentNumber)}</div></div>
|
||||
<div><div class="label">Issue Date</div><div class="value">${escapeHtml(formatDate(options.issueDate))}</div></div>
|
||||
<div><div class="label">Status</div><div class="value">${escapeHtml(options.status)}</div></div>
|
||||
${options.documentMeta.map((entry) => `<div><div class="label">${escapeHtml(entry.label)}</div><div class="value">${escapeHtml(entry.value)}</div></div>`).join("")}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<div class="card-title">${escapeHtml(options.partyTitle)}</div>
|
||||
<div class="stack">${options.partyLines.map((line) => `<div>${escapeHtml(line)}</div>`).join("")}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="card-title">Contact</div>
|
||||
<div class="stack">${options.partyMeta.map((entry) => `<div><strong>${escapeHtml(entry.label)}:</strong> ${escapeHtml(entry.value)}</div>`).join("")}</div>
|
||||
</div>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 16%;">SKU</th>
|
||||
<th>Description</th>
|
||||
<th style="width: 9%;" class="number">Qty</th>
|
||||
<th style="width: 9%;" class="number">UOM</th>
|
||||
<th style="width: 13%;" class="number">Unit</th>
|
||||
<th style="width: 14%;" class="number">Line Total</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>${options.rows}</tbody>
|
||||
</table>
|
||||
<div class="summary">
|
||||
<table>
|
||||
<tbody>${options.totalsRows}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="notes"><div class="card-title">Notes</div>${escapeHtml(options.notes || "No notes recorded for this document.")}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
|
||||
documentsRouter.get("/company-profile-preview.pdf", requirePermissions([permissions.companyRead]), async (_request, response) => {
|
||||
const profile = await getActiveCompanyProfile();
|
||||
const pdf = await renderPdf(`
|
||||
@@ -58,6 +177,180 @@ documentsRouter.get("/company-profile-preview.pdf", requirePermissions([permissi
|
||||
return response.send(pdf);
|
||||
});
|
||||
|
||||
documentsRouter.get(
|
||||
"/sales/quotes/:quoteId/document.pdf",
|
||||
requirePermissions([permissions.salesRead]),
|
||||
async (request, response) => {
|
||||
const quoteId = typeof request.params.quoteId === "string" ? request.params.quoteId : null;
|
||||
if (!quoteId) {
|
||||
response.status(400);
|
||||
return response.send("Invalid quote id.");
|
||||
}
|
||||
|
||||
const [profile, quote] = await Promise.all([getActiveCompanyProfile(), getSalesDocumentPdfData("QUOTE", quoteId)]);
|
||||
if (!quote) {
|
||||
response.status(404);
|
||||
return response.send("Quote was not found.");
|
||||
}
|
||||
|
||||
const rows = quote.lines.map((line) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(line.itemSku)}</td>
|
||||
<td><div class="item-name">${escapeHtml(line.itemName)}</div><div class="item-desc">${escapeHtml(line.description || "")}</div></td>
|
||||
<td class="number">${line.quantity}</td>
|
||||
<td class="number">${escapeHtml(line.unitOfMeasure)}</td>
|
||||
<td class="number">$${line.unitPrice.toFixed(2)}</td>
|
||||
<td class="number">$${line.lineTotal.toFixed(2)}</td>
|
||||
</tr>
|
||||
`).join("");
|
||||
|
||||
const pdf = await renderCommercialDocumentPdf({
|
||||
company: profile,
|
||||
title: "Sales Quote",
|
||||
documentNumber: quote.documentNumber,
|
||||
issueDate: quote.issueDate,
|
||||
status: quote.status,
|
||||
partyTitle: "Bill To",
|
||||
partyLines: buildAddressLines(quote.customer),
|
||||
partyMeta: [
|
||||
{ label: "Email", value: quote.customer.email || "Not set" },
|
||||
{ label: "Phone", value: quote.customer.phone || "Not set" },
|
||||
],
|
||||
documentMeta: [
|
||||
{ label: "Expires", value: formatDate(quote.expiresAt) },
|
||||
],
|
||||
rows,
|
||||
totalsRows: `
|
||||
<tr><td>Subtotal</td><td class="number">$${quote.subtotal.toFixed(2)}</td></tr>
|
||||
<tr><td>Discount (${quote.discountPercent.toFixed(2)}%)</td><td class="number">-$${quote.discountAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Tax (${quote.taxPercent.toFixed(2)}%)</td><td class="number">$${quote.taxAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Freight</td><td class="number">$${quote.freightAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Total</td><td class="number">$${quote.total.toFixed(2)}</td></tr>
|
||||
`,
|
||||
notes: quote.notes,
|
||||
});
|
||||
|
||||
response.setHeader("Content-Type", "application/pdf");
|
||||
response.setHeader("Content-Disposition", `inline; filename=${quote.documentNumber.toLowerCase()}-quote.pdf`);
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
documentsRouter.get(
|
||||
"/sales/orders/:orderId/document.pdf",
|
||||
requirePermissions([permissions.salesRead]),
|
||||
async (request, response) => {
|
||||
const orderId = typeof request.params.orderId === "string" ? request.params.orderId : null;
|
||||
if (!orderId) {
|
||||
response.status(400);
|
||||
return response.send("Invalid sales order id.");
|
||||
}
|
||||
|
||||
const [profile, order] = await Promise.all([getActiveCompanyProfile(), getSalesDocumentPdfData("ORDER", orderId)]);
|
||||
if (!order) {
|
||||
response.status(404);
|
||||
return response.send("Sales order was not found.");
|
||||
}
|
||||
|
||||
const rows = order.lines.map((line) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(line.itemSku)}</td>
|
||||
<td><div class="item-name">${escapeHtml(line.itemName)}</div><div class="item-desc">${escapeHtml(line.description || "")}</div></td>
|
||||
<td class="number">${line.quantity}</td>
|
||||
<td class="number">${escapeHtml(line.unitOfMeasure)}</td>
|
||||
<td class="number">$${line.unitPrice.toFixed(2)}</td>
|
||||
<td class="number">$${line.lineTotal.toFixed(2)}</td>
|
||||
</tr>
|
||||
`).join("");
|
||||
|
||||
const pdf = await renderCommercialDocumentPdf({
|
||||
company: profile,
|
||||
title: "Sales Order",
|
||||
documentNumber: order.documentNumber,
|
||||
issueDate: order.issueDate,
|
||||
status: order.status,
|
||||
partyTitle: "Bill To",
|
||||
partyLines: buildAddressLines(order.customer),
|
||||
partyMeta: [
|
||||
{ label: "Email", value: order.customer.email || "Not set" },
|
||||
{ label: "Phone", value: order.customer.phone || "Not set" },
|
||||
],
|
||||
documentMeta: [],
|
||||
rows,
|
||||
totalsRows: `
|
||||
<tr><td>Subtotal</td><td class="number">$${order.subtotal.toFixed(2)}</td></tr>
|
||||
<tr><td>Discount (${order.discountPercent.toFixed(2)}%)</td><td class="number">-$${order.discountAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Tax (${order.taxPercent.toFixed(2)}%)</td><td class="number">$${order.taxAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Freight</td><td class="number">$${order.freightAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Total</td><td class="number">$${order.total.toFixed(2)}</td></tr>
|
||||
`,
|
||||
notes: order.notes,
|
||||
});
|
||||
|
||||
response.setHeader("Content-Type", "application/pdf");
|
||||
response.setHeader("Content-Disposition", `inline; filename=${order.documentNumber.toLowerCase()}-sales-order.pdf`);
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
documentsRouter.get(
|
||||
"/purchasing/orders/:orderId/document.pdf",
|
||||
requirePermissions([permissions.purchasingRead]),
|
||||
async (request, response) => {
|
||||
const orderId = typeof request.params.orderId === "string" ? request.params.orderId : null;
|
||||
if (!orderId) {
|
||||
response.status(400);
|
||||
return response.send("Invalid purchase order id.");
|
||||
}
|
||||
|
||||
const [profile, order] = await Promise.all([getActiveCompanyProfile(), getPurchaseOrderPdfData(orderId)]);
|
||||
if (!order) {
|
||||
response.status(404);
|
||||
return response.send("Purchase order was not found.");
|
||||
}
|
||||
|
||||
const rows = order.lines.map((line) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(line.itemSku)}</td>
|
||||
<td><div class="item-name">${escapeHtml(line.itemName)}</div><div class="item-desc">${escapeHtml(line.description || "")}</div></td>
|
||||
<td class="number">${line.quantity}</td>
|
||||
<td class="number">${escapeHtml(line.unitOfMeasure)}</td>
|
||||
<td class="number">$${line.unitCost.toFixed(2)}</td>
|
||||
<td class="number">$${line.lineTotal.toFixed(2)}</td>
|
||||
</tr>
|
||||
`).join("");
|
||||
|
||||
const pdf = await renderCommercialDocumentPdf({
|
||||
company: profile,
|
||||
title: "Purchase Order",
|
||||
documentNumber: order.documentNumber,
|
||||
issueDate: order.issueDate,
|
||||
status: order.status,
|
||||
partyTitle: "Vendor",
|
||||
partyLines: buildAddressLines(order.vendor),
|
||||
partyMeta: [
|
||||
{ label: "Email", value: order.vendor.email || "Not set" },
|
||||
{ label: "Phone", value: order.vendor.phone || "Not set" },
|
||||
{ label: "Terms", value: order.vendor.paymentTerms || "Not set" },
|
||||
{ label: "Currency", value: order.vendor.currencyCode || "USD" },
|
||||
],
|
||||
documentMeta: [],
|
||||
rows,
|
||||
totalsRows: `
|
||||
<tr><td>Subtotal</td><td class="number">$${order.subtotal.toFixed(2)}</td></tr>
|
||||
<tr><td>Tax (${order.taxPercent.toFixed(2)}%)</td><td class="number">$${order.taxAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Freight</td><td class="number">$${order.freightAmount.toFixed(2)}</td></tr>
|
||||
<tr><td>Total</td><td class="number">$${order.total.toFixed(2)}</td></tr>
|
||||
`,
|
||||
notes: order.notes,
|
||||
});
|
||||
|
||||
response.setHeader("Content-Type", "application/pdf");
|
||||
response.setHeader("Content-Disposition", `inline; filename=${order.documentNumber.toLowerCase()}-purchase-order.pdf`);
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
documentsRouter.get(
|
||||
"/shipping/shipments/:shipmentId/packing-slip.pdf",
|
||||
requirePermissions([permissions.shippingRead]),
|
||||
|
||||
Reference in New Issue
Block a user