/** Shared receipt helpers for HAH restaurant orders (customer download + waiter print). */
window.SmOSReceipt = (function () {
function money(n) {
const v = Number(n);
if (!Number.isFinite(v)) return "—";
return `£${v.toFixed(2)}`;
}
function esc(s) {
return String(s ?? "")
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function tableLabel(b) {
const tables = Array.isArray(b.tables) ? b.tables : [];
if (tables.length) {
return tables.map((t) => t.label || t.id).filter(Boolean).join(", ");
}
const p = b.payload || {};
return p.tableId || p.table_id || "";
}
function itemsRows(b) {
const items = Array.isArray(b.items) ? b.items : [];
if (!items.length) return `
| No line items |
`;
return items
.map((i) => {
const qty = i.qty || 1;
const price = Number(i.price) || 0;
return `
| ${esc(qty)}× ${esc(i.name || "Item")} |
${esc(money(price))} |
${esc(money(qty * price))} |
`;
})
.join("");
}
function buildHtml(b, opts = {}) {
const biz = opts.businessName || "HAH restaurant";
const when = b.placedAt || b.created_at || b.updated_at || new Date().toISOString();
const whenLabel = (() => {
try {
return new Date(when).toLocaleString("en-GB", {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
});
} catch (e) {
return when;
}
})();
const table = tableLabel(b);
const service = b.service || (b.payload && b.payload.service) || "order";
const pay =
b.payment ||
(b.payload && (b.payload.paymentName || b.payload.payment)) ||
"—";
const payStatus = b.payment_status || (b.payload && b.payload.paymentStatus) || "—";
return `
Receipt · ${esc(b.ref || b.id || "")}
${esc(biz)}
9 Eskdail Court, Dalkeith · Receipt
| Item | Each | Line |
${itemsRows(b)}
Total${esc(money(b.total))}
Thank you — please keep this receipt for your records.
`;
}
function openPrint(b, opts) {
const html = buildHtml(b, opts);
const w = window.open("", "_blank", "noopener,noreferrer,width=480,height=720");
if (!w) {
alert("Please allow pop-ups to open the receipt.");
return;
}
w.document.open();
w.document.write(html);
w.document.close();
}
return { buildHtml, openPrint, money };
})();