projects
This commit is contained in:
@@ -5,7 +5,7 @@ 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 { getShipmentDocumentData, getShipmentPackingSlipData } from "../shipping/service.js";
|
||||
import { getActiveCompanyProfile } from "../settings/service.js";
|
||||
|
||||
export const documentsRouter = Router();
|
||||
@@ -136,6 +136,205 @@ function renderCommercialDocumentPdf(options: {
|
||||
`);
|
||||
}
|
||||
|
||||
function buildShippingLabelPdf(options: {
|
||||
company: Awaited<ReturnType<typeof getActiveCompanyProfile>>;
|
||||
shipment: Awaited<ReturnType<typeof getShipmentDocumentData>>;
|
||||
}) {
|
||||
const { company, shipment } = options;
|
||||
if (!shipment) {
|
||||
throw new Error("Shipment data is required.");
|
||||
}
|
||||
|
||||
const shipToLines = buildAddressLines(shipment.customer);
|
||||
const topLine = shipment.lines[0];
|
||||
|
||||
return renderPdf(`
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
@page { size: 4in 6in; margin: 8mm; }
|
||||
body { font-family: ${company.theme.fontFamily}, Arial, sans-serif; color: #111827; font-size: 11px; }
|
||||
.label { border: 2px solid #111827; border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 12px; min-height: calc(6in - 16mm); box-sizing: border-box; }
|
||||
.row { display: flex; justify-content: space-between; gap: 12px; }
|
||||
.muted { font-size: 9px; text-transform: uppercase; letter-spacing: 0.08em; color: #4b5563; }
|
||||
.brand { border-bottom: 2px solid ${company.theme.primaryColor}; padding-bottom: 10px; }
|
||||
.brand h1 { margin: 0; font-size: 18px; color: ${company.theme.primaryColor}; }
|
||||
.block { border: 1px solid #d1d5db; border-radius: 10px; padding: 10px; }
|
||||
.stack { display: flex; flex-direction: column; gap: 4px; }
|
||||
.barcode { border: 2px solid #111827; border-radius: 10px; padding: 8px; text-align: center; font-family: monospace; font-size: 18px; letter-spacing: 0.18em; }
|
||||
.strong { font-weight: 700; }
|
||||
.big { font-size: 16px; font-weight: 700; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="label">
|
||||
<div class="brand">
|
||||
<div class="row">
|
||||
<div>
|
||||
<div class="muted">From</div>
|
||||
<h1>${escapeHtml(company.companyName)}</h1>
|
||||
</div>
|
||||
<div style="text-align:right;">
|
||||
<div class="muted">Shipment</div>
|
||||
<div class="big">${escapeHtml(shipment.shipmentNumber)}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="block">
|
||||
<div class="muted">Ship To</div>
|
||||
<div class="stack" style="margin-top:8px;">
|
||||
${shipToLines.map((line) => `<div class="${line === shipment.customer.name ? "strong" : ""}">${escapeHtml(line)}</div>`).join("")}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="block" style="flex:1;">
|
||||
<div class="muted">Service</div>
|
||||
<div class="big" style="margin-top:6px;">${escapeHtml(shipment.serviceLevel || "GROUND")}</div>
|
||||
</div>
|
||||
<div class="block" style="width:90px;">
|
||||
<div class="muted">Pkgs</div>
|
||||
<div class="big" style="margin-top:6px;">${shipment.packageCount}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="block" style="flex:1;">
|
||||
<div class="muted">Sales Order</div>
|
||||
<div class="strong" style="margin-top:6px;">${escapeHtml(shipment.salesOrderNumber)}</div>
|
||||
</div>
|
||||
<div class="block" style="width:110px;">
|
||||
<div class="muted">Ship Date</div>
|
||||
<div class="strong" style="margin-top:6px;">${escapeHtml(formatDate(shipment.shipDate))}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="block">
|
||||
<div class="muted">Reference</div>
|
||||
<div style="margin-top:6px;">${escapeHtml(topLine ? `${topLine.itemSku} · ${topLine.itemName}` : "Shipment record")}</div>
|
||||
</div>
|
||||
<div class="barcode">
|
||||
*${escapeHtml(shipment.trackingNumber || shipment.shipmentNumber)}*
|
||||
</div>
|
||||
<div style="text-align:center; font-size:10px; color:#4b5563;">${escapeHtml(shipment.carrier || "Carrier pending")} · ${escapeHtml(shipment.trackingNumber || "Tracking pending")}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
|
||||
function buildBillOfLadingPdf(options: {
|
||||
company: Awaited<ReturnType<typeof getActiveCompanyProfile>>;
|
||||
shipment: Awaited<ReturnType<typeof getShipmentDocumentData>>;
|
||||
}) {
|
||||
const { company, shipment } = options;
|
||||
if (!shipment) {
|
||||
throw new Error("Shipment data is required.");
|
||||
}
|
||||
|
||||
const shipperLines = [
|
||||
company.companyName,
|
||||
company.addressLine1,
|
||||
company.addressLine2,
|
||||
`${company.city}, ${company.state} ${company.postalCode}`.trim(),
|
||||
company.country,
|
||||
company.phone,
|
||||
company.email,
|
||||
].filter((line) => line.trim().length > 0);
|
||||
const consigneeLines = [
|
||||
shipment.customer.name,
|
||||
shipment.customer.addressLine1,
|
||||
shipment.customer.addressLine2,
|
||||
`${shipment.customer.city}, ${shipment.customer.state} ${shipment.customer.postalCode}`.trim(),
|
||||
shipment.customer.country,
|
||||
shipment.customerPhone,
|
||||
shipment.customerEmail,
|
||||
].filter((line) => line.trim().length > 0);
|
||||
const totalQuantity = shipment.lines.reduce((sum, line) => sum + line.quantity, 0);
|
||||
const rows = shipment.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>
|
||||
</tr>
|
||||
`).join("");
|
||||
|
||||
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; }
|
||||
.meta { min-width: 320px; 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: 1fr 1fr; 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 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
|
||||
.summary-card { border: 1px solid #d7deeb; border-radius: 14px; padding: 12px 14px; }
|
||||
.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>Bill of Lading</p>
|
||||
</div>
|
||||
<div class="meta">
|
||||
<div><div class="label">Shipment</div><div class="value">${escapeHtml(shipment.shipmentNumber)}</div></div>
|
||||
<div><div class="label">Sales Order</div><div class="value">${escapeHtml(shipment.salesOrderNumber)}</div></div>
|
||||
<div><div class="label">Ship Date</div><div class="value">${escapeHtml(formatDate(shipment.shipDate))}</div></div>
|
||||
<div><div class="label">Status</div><div class="value">${escapeHtml(shipment.status)}</div></div>
|
||||
<div><div class="label">Carrier</div><div class="value">${escapeHtml(shipment.carrier || "Not set")}</div></div>
|
||||
<div><div class="label">Tracking</div><div class="value">${escapeHtml(shipment.trackingNumber || "Not set")}</div></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<div class="card-title">Shipper</div>
|
||||
<div class="stack">${shipperLines.map((line) => `<div>${escapeHtml(line)}</div>`).join("")}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="card-title">Consignee</div>
|
||||
<div class="stack">${consigneeLines.map((line) => `<div>${escapeHtml(line)}</div>`).join("")}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="summary">
|
||||
<div class="summary-card"><div class="label">Packages</div><div class="value">${shipment.packageCount}</div></div>
|
||||
<div class="summary-card"><div class="label">Line Count</div><div class="value">${shipment.lines.length}</div></div>
|
||||
<div class="summary-card"><div class="label">Total Qty</div><div class="value">${totalQuantity}</div></div>
|
||||
<div class="summary-card"><div class="label">Service</div><div class="value">${escapeHtml(shipment.serviceLevel || "Not set")}</div></div>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 18%;">SKU</th>
|
||||
<th>Description</th>
|
||||
<th style="width: 12%;" class="number">Qty</th>
|
||||
<th style="width: 10%;" class="number">UOM</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>${rows}</tbody>
|
||||
</table>
|
||||
<div class="notes"><div class="card-title">Logistics Notes</div>${escapeHtml(shipment.notes || "No shipment notes recorded.")}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
|
||||
documentsRouter.get("/company-profile-preview.pdf", requirePermissions([permissions.companyRead]), async (_request, response) => {
|
||||
const profile = await getActiveCompanyProfile();
|
||||
const pdf = await renderPdf(`
|
||||
@@ -471,3 +670,49 @@ documentsRouter.get(
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
documentsRouter.get(
|
||||
"/shipping/shipments/:shipmentId/shipping-label.pdf",
|
||||
requirePermissions([permissions.shippingRead]),
|
||||
async (request, response) => {
|
||||
const shipmentId = typeof request.params.shipmentId === "string" ? request.params.shipmentId : null;
|
||||
if (!shipmentId) {
|
||||
response.status(400);
|
||||
return response.send("Invalid shipment id.");
|
||||
}
|
||||
|
||||
const [profile, shipment] = await Promise.all([getActiveCompanyProfile(), getShipmentDocumentData(shipmentId)]);
|
||||
if (!shipment) {
|
||||
response.status(404);
|
||||
return response.send("Shipment was not found.");
|
||||
}
|
||||
|
||||
const pdf = await buildShippingLabelPdf({ company: profile, shipment });
|
||||
response.setHeader("Content-Type", "application/pdf");
|
||||
response.setHeader("Content-Disposition", `inline; filename=${shipment.shipmentNumber.toLowerCase()}-label.pdf`);
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
documentsRouter.get(
|
||||
"/shipping/shipments/:shipmentId/bill-of-lading.pdf",
|
||||
requirePermissions([permissions.shippingRead]),
|
||||
async (request, response) => {
|
||||
const shipmentId = typeof request.params.shipmentId === "string" ? request.params.shipmentId : null;
|
||||
if (!shipmentId) {
|
||||
response.status(400);
|
||||
return response.send("Invalid shipment id.");
|
||||
}
|
||||
|
||||
const [profile, shipment] = await Promise.all([getActiveCompanyProfile(), getShipmentDocumentData(shipmentId)]);
|
||||
if (!shipment) {
|
||||
response.status(404);
|
||||
return response.send("Shipment was not found.");
|
||||
}
|
||||
|
||||
const pdf = await buildBillOfLadingPdf({ company: profile, shipment });
|
||||
response.setHeader("Content-Type", "application/pdf");
|
||||
response.setHeader("Content-Disposition", `inline; filename=${shipment.shipmentNumber.toLowerCase()}-bill-of-lading.pdf`);
|
||||
return response.send(pdf);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user