Bill-payment checks: per-element positioning + visibility

The bill-payment check generator (checkPdf.ts) now supports per-field
position offsets (X/Y inches) and show/hide for every element — check
number, return address, bank block, date, pay-to, amounts, payee address,
memo, signature, and MICR — layered on the existing layout (defaults render
identically). Edited in Settings → Check Setup ("Element positions").
Stored in accounting.check_settings.field_positions (jsonb). Also replaced a
"→" with ">" in the MICR placeholder to avoid the UTF-16 spacing artifact.

Migration applied: check_settings.field_positions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-03 01:05:10 -04:00
parent c3d1d86b07
commit 7ccfc133f8
3 changed files with 261 additions and 155 deletions
+215 -152
View File
@@ -55,6 +55,28 @@ export type CheckPrintOptions = {
micrGap1?: number;
/** Spaces between MICR routing and account segments (default 1) */
micrGap2?: number;
/** Per-field position offsets (inches) + visibility, keyed by field. */
fieldPositions?: Record<string, { dx?: number; dy?: number; hidden?: boolean }>;
};
/** Field keys for per-element check positioning (bill-payment check face). */
export const CHECK_FIELD_KEYS = [
"check_number", "company", "bank", "date", "pay_to",
"amount_box", "amount_words", "address_block", "memo", "signature", "micr",
] as const;
export const CHECK_FIELD_LABELS: Record<string, string> = {
check_number: "Check Number",
company: "Company / Return Address",
bank: "Bank Name & Address",
date: "Date",
pay_to: "Pay to the Order Of",
amount_box: "Numeric Amount ($)",
amount_words: "Written Amount",
address_block: "Payee Address Block",
memo: "Memo",
signature: "Signature Line & Label",
micr: "MICR Line",
};
// ── Constants ────────────────────────────────────────────────────────────────
@@ -110,10 +132,26 @@ function hline(doc: jsPDF, x1: number, y: number, x2: number, w = 0.006, gray =
// ── Check face ───────────────────────────────────────────────────────────────
function drawCheck(doc: jsPDF, originY: number, c: CheckData, ox = 0, micrOy = 0, micrGap1 = 1, micrGap2 = 1) {
function drawCheck(
doc: jsPDF,
originY: number,
c: CheckData,
ox = 0,
micrOy = 0,
micrGap1 = 1,
micrGap2 = 1,
fields: Record<string, { dx?: number; dy?: number; hidden?: boolean }> = {}
) {
const y = (dy: number) => originY + dy;
const x = (dx: number) => dx + ox; // Apply global X offset to every horizontal coordinate
// Per-field positioning: each labeled element can be nudged (dx/dy in inches)
// or hidden. Layered on top of the default layout, so {} renders identically.
const F = (k: string): { dx?: number; dy?: number; hidden?: boolean } => (fields && fields[k]) || {};
const hid = (k: string) => F(k).hidden === true;
const ax = (k: string) => Number(F(k).dx) || 0;
const ay = (k: string) => Number(F(k).dy) || 0;
// VOID watermark
if (c.voided) {
doc.saveGraphicsState();
@@ -125,175 +163,199 @@ function drawCheck(doc: jsPDF, originY: number, c: CheckData, ox = 0, micrOy = 0
doc.setTextColor(0);
}
// ── Top row ──────────────────────────────────────────────────────────────
doc.setFont("helvetica", "bold");
doc.setFontSize(12);
doc.setTextColor(0);
doc.text(String(c.checkNumber), x(RIGHT), y(0.18), { align: "right" });
// ── Check number (top-right) ──
if (!hid("check_number")) {
doc.setFont("helvetica", "bold");
doc.setFontSize(12);
doc.setTextColor(0);
doc.text(String(c.checkNumber), x(RIGHT + ax("check_number")), y(0.18 + ay("check_number")), { align: "right" });
}
doc.setFont("helvetica", "bold");
doc.setFontSize(8.5);
doc.text(c.companyName, x(ML), y(0.18));
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
const companyAddrLines = (c.companyAddress ?? "").split(/\n/).filter(Boolean);
companyAddrLines.slice(0, 4).forEach((line, i) => {
doc.text(line, x(ML), y(0.32 + i * 0.135));
});
// ── Company / return address ──
if (!hid("company")) {
const dx = ax("company"), dy = ay("company");
doc.setFont("helvetica", "bold");
doc.setFontSize(8.5);
doc.setTextColor(0);
doc.text(c.companyName, x(ML + dx), y(0.18 + dy));
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
const companyAddrLines = (c.companyAddress ?? "").split(/\n/).filter(Boolean);
companyAddrLines.slice(0, 4).forEach((line, i) => {
doc.text(line, x(ML + dx), y(0.32 + i * 0.135 + dy));
});
}
// ── Bank name / address ──
const bankX = ML + CW * 0.52;
doc.setFont("helvetica", "bold");
doc.setFontSize(9);
doc.text(c.bankName ?? "", x(bankX), y(0.18));
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
if (c.bankAddress) doc.text(c.bankAddress, x(bankX), y(0.32));
if (!hid("bank")) {
const dx = ax("bank"), dy = ay("bank");
doc.setFont("helvetica", "bold");
doc.setFontSize(9);
doc.setTextColor(0);
doc.text(c.bankName ?? "", x(bankX + dx), y(0.18 + dy));
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
if (c.bankAddress) doc.text(c.bankAddress, x(bankX + dx), y(0.32 + dy));
}
// ── Date line ────────────────────────────────────────────────────────────
const dateLineY = y(0.78);
doc.setFont("helvetica", "normal");
doc.setFontSize(9);
doc.text(c.date, x(RIGHT), dateLineY - 0.04, { align: "right" });
hline(doc, x(ML + CW * 0.62), dateLineY, x(RIGHT));
// ── Date + date line ──
if (!hid("date")) {
const dx = ax("date");
const dateLineY = y(0.78 + ay("date"));
doc.setFont("helvetica", "normal");
doc.setFontSize(9);
doc.setTextColor(0);
doc.text(c.date, x(RIGHT + dx), dateLineY - 0.04, { align: "right" });
hline(doc, x(ML + CW * 0.62 + dx), dateLineY, x(RIGHT + dx));
}
// ── Pay to the order of ──────────────────────────────────────────────────
const payY = y(1.10);
// ── Pay to the order of ──
const payeeX = ML + 0.78;
if (!hid("pay_to")) {
const dx = ax("pay_to");
const payY = y(1.10 + ay("pay_to"));
doc.setFont("helvetica", "normal");
doc.setFontSize(7);
doc.setTextColor(0);
doc.text("PAY TO THE", x(ML + dx), payY - 0.10);
doc.text("ORDER OF", x(ML + dx), payY + 0.03);
doc.setFont("helvetica", "bold");
doc.setFontSize(10);
doc.text(c.payee, x(payeeX + dx), payY);
hline(doc, x(payeeX - 0.02 + dx), payY + 0.06, x(RIGHT - 1.55 + dx));
}
doc.setFont("helvetica", "normal");
doc.setFontSize(7);
doc.text("PAY TO THE", x(ML), payY - 0.10);
doc.text("ORDER OF", x(ML), payY + 0.03);
// ── Numeric amount box ──
if (!hid("amount_box")) {
const dx = ax("amount_box");
const amtY = y(1.10 + ay("amount_box"));
doc.setFont("helvetica", "bold");
doc.setFontSize(11);
doc.setTextColor(0);
doc.text(`$${c.amount.toFixed(2)}`, x(RIGHT - 0.08 + dx), amtY, { align: "right" });
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
doc.setTextColor(80);
doc.text("DOLLARS", x(RIGHT - 0.08 + dx), amtY + 0.17, { align: "right" });
doc.setTextColor(0);
}
doc.setFont("helvetica", "bold");
doc.setFontSize(10);
doc.text(c.payee, x(payeeX), payY);
hline(doc, x(payeeX - 0.02), payY + 0.06, x(RIGHT - 1.55));
// ── Written amount ──
if (!hid("amount_words")) {
const dx = ax("amount_words");
const writtenY = y(1.40 + ay("amount_words"));
const amountWords = numberToWords(c.amount).toUpperCase();
doc.setFont("helvetica", "normal");
doc.setFontSize(8.5);
doc.setTextColor(0);
const wordsW = doc.getTextWidth(amountWords);
const starW = doc.getTextWidth("*");
const starsTotal = Math.max(0, Math.floor((CW * 0.90 - wordsW) / starW) - 2);
const leftStars = Math.max(4, Math.floor(starsTotal * 0.18));
const rightStars = Math.max(0, starsTotal - leftStars);
doc.text("*".repeat(leftStars) + " " + amountWords + " " + "*".repeat(rightStars),
x(ML + dx), writtenY, { maxWidth: CW });
hline(doc, x(ML + dx), writtenY + 0.06, x(RIGHT + dx), 0.008, 0);
}
// Amount box
doc.setFont("helvetica", "bold");
doc.setFontSize(11);
doc.text(`$${c.amount.toFixed(2)}`, x(RIGHT - 0.08), payY, { align: "right" });
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
doc.setTextColor(80);
doc.text("DOLLARS", x(RIGHT - 0.08), payY + 0.17, { align: "right" });
doc.setTextColor(0);
// ── Written amount — with more breathing room after the pay-to line ────────
// numberToWords() already ends with "Dollars" — no suffix needed
const writtenY = payY + 0.30; // extra gap below pay-to line
const amountWords = numberToWords(c.amount).toUpperCase();
doc.setFont("helvetica", "normal");
doc.setFontSize(8.5);
const wordsW = doc.getTextWidth(amountWords);
const starW = doc.getTextWidth("*");
const starsTotal = Math.max(0, Math.floor((CW * 0.90 - wordsW) / starW) - 2);
const leftStars = Math.max(4, Math.floor(starsTotal * 0.18));
const rightStars = Math.max(0, starsTotal - leftStars);
doc.text("*".repeat(leftStars) + " " + amountWords + " " + "*".repeat(rightStars),
x(ML), writtenY, { maxWidth: CW });
// Underline the written amount
hline(doc, x(ML), writtenY + 0.06, x(RIGHT), 0.008, 0);
// ── Envelope address block — more space below written amount ─────────────
// Layout: payee name (bold) on left | "Authorized Signer" on right (same Y)
// address line 1 (indented)
// address line 2 (indented)
// ── Envelope address block ──
const addrLines = c.payeeAddress
? c.payeeAddress.split(/\n/).map(s => s.trim()).filter(Boolean)
: [];
const addrBlockY = writtenY + 0.28; // extra gap below written amount
const addrIndentX = ML + 0.35;
// Payee name bold (envelope window — first line)
doc.setFont("helvetica", "bold");
doc.setFontSize(9);
doc.text(c.payee, x(addrIndentX), addrBlockY);
// "Authorized Signer" on the RIGHT, same Y as payee name
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
doc.setTextColor(80);
doc.text("Authorized Signer", x(RIGHT), addrBlockY, { align: "right" });
doc.setTextColor(0);
// Address lines below, slightly indented
if (addrLines.length > 0) {
doc.setFont("helvetica", "normal");
doc.setFontSize(8.5);
doc.setTextColor(30);
addrLines.slice(0, 3).forEach((line, i) => {
doc.text(line, x(addrIndentX + 0.05), addrBlockY + 0.155 + i * 0.145);
});
if (!hid("address_block")) {
const dx = ax("address_block");
const addrBlockY = y(1.68 + ay("address_block"));
doc.setFont("helvetica", "bold");
doc.setFontSize(9);
doc.setTextColor(0);
}
// ── Memo / Authorized Signature line — no grey divider, just space ────────
const addrEndY = addrBlockY + 0.155 + Math.max(addrLines.length, 0) * 0.145;
const bottomLineY = addrEndY + 0.30; // generous gap, no grey line between
const sigLabelX = PW / 2 + 0.2;
// Memo (left side)
doc.setFont("helvetica", "normal");
doc.setFontSize(8);
doc.setTextColor(0);
doc.text("MEMO", x(ML), bottomLineY);
hline(doc, x(ML + 0.5), bottomLineY, x(ML + 2.8));
if (c.memo) {
doc.text(c.payee, x(addrIndentX + dx), addrBlockY);
doc.setFont("helvetica", "normal");
doc.setFontSize(7.5);
doc.text(c.memo, x(ML + 0.55), bottomLineY - 0.04);
}
// "AUTHORIZED SIGNATURE" label centered above the right-side line
doc.setFontSize(7);
doc.setTextColor(80);
const sigCenterX = sigLabelX + (RIGHT - sigLabelX) / 2;
doc.text("AUTHORIZED SIGNATURE", x(sigCenterX), bottomLineY - 0.36, { align: "center" });
doc.setTextColor(0);
// Signature image — natural aspect ratio, bottom edge sits ON the line
if (c.printSignature && c.signatureDataUrl) {
try {
const props = doc.getImageProperties(c.signatureDataUrl);
const maxW = RIGHT - sigLabelX - 0.1;
const maxH = 0.42;
const ratio = props.width / props.height;
let sigW = maxW;
let sigH = sigW / ratio;
if (sigH > maxH) { sigH = maxH; sigW = sigH * ratio; }
const sigX = x(sigCenterX - sigW / 2);
const sigY = bottomLineY - sigH;
doc.addImage(c.signatureDataUrl, "PNG", sigX, sigY, sigW, sigH);
} catch { /* silent */ }
}
// Signature LINE
hline(doc, x(sigLabelX), bottomLineY, x(RIGHT));
// ── MICR line (X offset + separate MICR Y fine-tune) ─────────────────────
const micrY = y(CHECK_H - 0.22) + micrOy;
const micr = buildMicr(
(c.routingNumber ?? "").replace(/\D/g, ""),
(c.accountNumber ?? "").replace(/\D/g, ""),
c.checkNumber,
micrGap1,
micrGap2
);
if (micr) {
ensureMicrFont(doc);
doc.setFontSize(11);
doc.setTextColor(80);
doc.text("Authorized Signer", x(RIGHT + dx), addrBlockY, { align: "right" });
doc.setTextColor(0);
doc.text(micr, x(PW / 2), micrY, { align: "center" });
if (addrLines.length > 0) {
doc.setFont("helvetica", "normal");
doc.setFontSize(8.5);
doc.setTextColor(30);
addrLines.slice(0, 3).forEach((line, i) => {
doc.text(line, x(addrIndentX + 0.05 + dx), addrBlockY + 0.155 + i * 0.145);
});
doc.setTextColor(0);
}
}
// ── Memo + signature (flow below the address block) ──
const addrEndDy = 1.68 + 0.155 + Math.max(addrLines.length, 0) * 0.145;
const bottomDy = addrEndDy + 0.30;
const sigLabelX = PW / 2 + 0.2;
const sigCenterX = sigLabelX + (RIGHT - sigLabelX) / 2;
if (!hid("memo")) {
const dx = ax("memo");
const bottomLineY = y(bottomDy + ay("memo"));
doc.setFont("helvetica", "normal");
} else {
doc.setFontSize(8);
doc.setTextColor(0);
doc.text("MEMO", x(ML + dx), bottomLineY);
hline(doc, x(ML + 0.5 + dx), bottomLineY, x(ML + 2.8 + dx));
if (c.memo) {
doc.setFontSize(7.5);
doc.text(c.memo, x(ML + 0.55 + dx), bottomLineY - 0.04);
}
}
if (!hid("signature")) {
const dx = ax("signature");
const bottomLineY = y(bottomDy + ay("signature"));
doc.setFont("helvetica", "normal");
doc.setFontSize(7);
doc.setTextColor(160);
doc.text("[ Configure routing & account numbers in Settings → Check Setup ]", x(PW / 2), micrY, { align: "center" });
doc.setTextColor(80);
doc.text("AUTHORIZED SIGNATURE", x(sigCenterX + dx), bottomLineY - 0.36, { align: "center" });
doc.setTextColor(0);
if (c.printSignature && c.signatureDataUrl) {
try {
const props = doc.getImageProperties(c.signatureDataUrl);
const maxW = RIGHT - sigLabelX - 0.1;
const maxH = 0.42;
const ratio = props.width / props.height;
let sigW = maxW;
let sigH = sigW / ratio;
if (sigH > maxH) { sigH = maxH; sigW = sigH * ratio; }
const sigX = x(sigCenterX + dx - sigW / 2);
const sigY = bottomLineY - sigH;
doc.addImage(c.signatureDataUrl, "PNG", sigX, sigY, sigW, sigH);
} catch { /* silent */ }
}
hline(doc, x(sigLabelX + dx), bottomLineY, x(RIGHT + dx));
}
// ── MICR line (X offset + separate MICR Y fine-tune) ──
if (!hid("micr")) {
const dx = ax("micr");
const micrY = y(CHECK_H - 0.22 + ay("micr")) + micrOy;
const micr = buildMicr(
(c.routingNumber ?? "").replace(/\D/g, ""),
(c.accountNumber ?? "").replace(/\D/g, ""),
c.checkNumber,
micrGap1,
micrGap2
);
if (micr) {
ensureMicrFont(doc);
doc.setFontSize(11);
doc.setTextColor(0);
doc.text(micr, x(PW / 2 + dx), micrY, { align: "center" });
doc.setFont("helvetica", "normal");
} else {
doc.setFont("helvetica", "normal");
doc.setFontSize(7);
doc.setTextColor(160);
doc.text("[ Configure routing & account numbers in Settings > Check Setup ]", x(PW / 2 + dx), micrY, { align: "center" });
doc.setTextColor(0);
}
}
}
@@ -362,19 +424,20 @@ export function generateCheckPDF(checks: CheckData[], opts: CheckPrintOptions):
const micrOy = opts.micrOffsetY ?? 0;
const g1 = opts.micrGap1 ?? 1;
const g2 = opts.micrGap2 ?? 1;
const fp = opts.fieldPositions || {};
checks.forEach((c, idx) => {
if (idx > 0) doc.addPage();
if (opts.style === "voucher") {
drawCheck(doc, 0 + oy, c, ox, micrOy, g1, g2);
drawCheck(doc, 0 + oy, c, ox, micrOy, g1, g2, fp);
drawStub(doc, SECTION_H + oy, c, ox);
drawStub(doc, SECTION_H * 2 + oy, c, ox);
} else {
let posY = 0;
if (opts.position === "middle") posY = PH / 2 - CHECK_H / 2;
if (opts.position === "bottom") posY = PH - CHECK_H;
drawCheck(doc, posY + oy, c, ox, micrOy, g1, g2);
drawCheck(doc, posY + oy, c, ox, micrOy, g1, g2, fp);
}
});