refactor: implement safe currency formatting utility to ensure consistent currency rendering in POS closing receipts

This commit is contained in:
Suherdy Yacob 2026-06-11 14:35:03 +07:00
parent 733d52e2d7
commit 43f92c39a2

View File

@ -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;