From 43f92c39a2dfb43a67274fd76070a48b0d72caaf Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Thu, 11 Jun 2026 14:35:03 +0700 Subject: [PATCH] refactor: implement safe currency formatting utility to ensure consistent currency rendering in POS closing receipts --- static/src/app/closing_receipt_patch.js | 38 +++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/static/src/app/closing_receipt_patch.js b/static/src/app/closing_receipt_patch.js index ff4299d..b710d1b 100644 --- a/static/src/app/closing_receipt_patch.js +++ b/static/src/app/closing_receipt_patch.js @@ -10,9 +10,42 @@ import { ConnectionLostError } from "@web/core/network/rpc"; import { _t } from "@web/core/l10n/translation"; import { PrinterService } from "@point_of_sale/app/services/printer_service"; import { waitImages } from "@point_of_sale/utils"; +import { formatCurrency as webFormatCurrency } from "@web/core/currency"; ClosePosPopup.props.push("last_cashier_name?"); +const getCurrencyId = (pos) => { + if (!pos) { + return undefined; + } + if (pos.currency && typeof pos.currency === "object" && "id" in pos.currency) { + return pos.currency.id; + } + if (pos.currency && typeof pos.currency === "number") { + return pos.currency; + } + const configCurrency = pos.config?.currency_id; + if (configCurrency) { + if (typeof configCurrency === "object") { + if ("id" in configCurrency) { + return configCurrency.id; + } + if (Array.isArray(configCurrency)) { + return configCurrency[0]; + } + } else if (typeof configCurrency === "number") { + return configCurrency; + } + } + return undefined; +}; + +const safeFormatCurrency = (value, pos, hasSymbol = true) => { + return webFormatCurrency(value, getCurrencyId(pos), { + noSymbol: !hasSymbol, + }); +}; + // Patch PrinterService.printWeb to return a promise that resolves only after print dialog is closed patch(PrinterService.prototype, { @@ -308,7 +341,7 @@ patch(ClosePosPopup.prototype, { _buildReceiptData(sessionName) { const pos = this.pos; - const formatCurrency = this.env.utils.formatCurrency; + const formatCurrency = (val, hasSymbol = true) => safeFormatCurrency(val, pos, hasSymbol); let cashierName = this.props.last_cashier_name || ""; if (!cashierName) { @@ -384,7 +417,8 @@ patch(ClosePosPopup.prototype, { // ───────────────────────────────────────────────────────────────────────────── patch(Navbar.prototype, { async reprintLastClosingReceipt() { - const formatCurrency = this.env.utils.formatCurrency; + const pos = this.pos; + const formatCurrency = (val, hasSymbol = true) => safeFormatCurrency(val, pos, hasSymbol); // ── 1. Fetch the last closed session for this config ───────────────── let sessionData;