fix: resolve pop-up blocker issues by initializing print window synchronously before async operations

This commit is contained in:
Suherdy Yacob 2026-06-18 16:45:06 +07:00
parent 94a4072d45
commit 9bfd81b153

View File

@ -100,8 +100,30 @@ patch(ClosePosPopup.prototype, {
async closeSession() { async closeSession() {
this.pos._resetConnectedCashier(); this.pos._resetConnectedCashier();
// ── Open print window synchronously to avoid pop-up blocker ──────────
let printWindow = null;
try {
printWindow = window.open("", "_blank", "width=400,height=600");
if (!printWindow) {
const notification = this.pos?.env?.services?.notification;
if (notification) {
notification.add(
_t("Pop-up blocked. Please allow pop-ups for this site to print the closing receipt."),
{ type: "warning" }
);
} else {
alert("Pop-up blocked. Please allow pop-ups for this site to print the closing receipt.");
}
} else {
printWindow.document.write("<html><body><h3 style='font-family:sans-serif;text-align:center;margin-top:20px;'>Closing Session, please wait...</h3></body></html>");
}
} catch (e) {
console.error("[pos_closing_receipt] Failed to open print window", e);
}
const syncSuccess = await this.pos.pushOrdersWithClosingPopup(); const syncSuccess = await this.pos.pushOrdersWithClosingPopup();
if (!syncSuccess) { if (!syncSuccess) {
if (printWindow) printWindow.close();
return; return;
} }
@ -117,6 +139,7 @@ patch(ClosePosPopup.prototype, {
} }
); );
if (!response.successful) { if (!response.successful) {
if (printWindow) printWindow.close();
return this.handleClosingError(response); return this.handleClosingError(response);
} }
} }
@ -129,6 +152,7 @@ patch(ClosePosPopup.prototype, {
); );
} catch (error) { } catch (error) {
if (!error.data && error.data?.message !== "This session is already closed.") { if (!error.data && error.data?.message !== "This session is already closed.") {
if (printWindow) printWindow.close();
throw error; throw error;
} }
} }
@ -166,11 +190,12 @@ patch(ClosePosPopup.prototype, {
); );
if (!response.successful) { if (!response.successful) {
if (printWindow) printWindow.close();
return this.handleClosingError(response); return this.handleClosingError(response);
} }
// ── Print AFTER closing session on server ──────────────────────── // ── Print AFTER closing session on server ────────────────────────
await this._printClosingReceipt(receiptData); await this._printClosingReceipt(receiptData, printWindow);
// Mark session closed locally // Mark session closed locally
this.pos.session.state = "closed"; this.pos.session.state = "closed";
@ -178,6 +203,7 @@ patch(ClosePosPopup.prototype, {
// ── Navigate away ──────────────────────────────────────────────── // ── Navigate away ────────────────────────────────────────────────
this.pos.router.close(); this.pos.router.close();
} catch (error) { } catch (error) {
if (printWindow) printWindow.close();
if (error instanceof ConnectionLostError) { if (error instanceof ConnectionLostError) {
throw error; throw error;
} else { } else {
@ -188,7 +214,7 @@ patch(ClosePosPopup.prototype, {
} }
}, },
async _printClosingReceipt(receiptData) { async _printClosingReceipt(receiptData, printWindow) {
console.log("[pos_closing_receipt] Initiating closing receipt print..."); console.log("[pos_closing_receipt] Initiating closing receipt print...");
if (this.pos.env.services.ui) { if (this.pos.env.services.ui) {
console.log("[pos_closing_receipt] Blocking UI during print..."); console.log("[pos_closing_receipt] Blocking UI during print...");
@ -325,17 +351,7 @@ patch(ClosePosPopup.prototype, {
</body> </body>
</html>`; </html>`;
const printWindow = window.open("", "_blank", "width=400,height=600");
if (!printWindow) { if (!printWindow) {
const notification = this.pos?.env?.services?.notification;
if (notification) {
notification.add(
_t("Pop-up blocked. Please allow pop-ups for this site and try again to print closing receipt."),
{ type: "warning" }
);
} else {
alert("Pop-up blocked. Please allow pop-ups for this site and try again to print closing receipt.");
}
return; return;
} }
printWindow.document.open(); printWindow.document.open();
@ -435,6 +451,23 @@ patch(Navbar.prototype, {
const pos = this.pos; const pos = this.pos;
const formatCurrency = (val, hasSymbol = true) => safeFormatCurrency(val, pos, hasSymbol); const formatCurrency = (val, hasSymbol = true) => safeFormatCurrency(val, pos, hasSymbol);
// ── Open print window synchronously to avoid pop-up blocker ──────────
let printWindow = null;
try {
printWindow = window.open("", "_blank", "width=400,height=600");
if (!printWindow) {
this.notification.add(
_t("Pop-up blocked. Please allow pop-ups for this site and try again."),
{ type: "warning" }
);
return;
} else {
printWindow.document.write("<html><body><h3 style='font-family:sans-serif;text-align:center;margin-top:20px;'>Fetching Closing Summary...</h3></body></html>");
}
} catch (e) {
console.error("[pos_closing_receipt] Failed to open print window", e);
}
// ── 1. Fetch the last closed session for this config ───────────────── // ── 1. Fetch the last closed session for this config ─────────────────
let sessionData; let sessionData;
try { try {
@ -444,6 +477,7 @@ patch(Navbar.prototype, {
[this.pos.config.id] [this.pos.config.id]
); );
if (!result) { if (!result) {
if (printWindow) printWindow.close();
this.notification.add(_t("No closed session found to reprint."), { this.notification.add(_t("No closed session found to reprint."), {
type: "warning", type: "warning",
}); });
@ -451,6 +485,7 @@ patch(Navbar.prototype, {
} }
sessionData = result; sessionData = result;
} catch (err) { } catch (err) {
if (printWindow) printWindow.close();
console.error("[pos_closing_receipt] Could not fetch last session:", err); console.error("[pos_closing_receipt] Could not fetch last session:", err);
this.notification.add(_t("Failed to fetch session data."), { type: "danger" }); this.notification.add(_t("Failed to fetch session data."), { type: "danger" });
return; return;
@ -600,12 +635,7 @@ patch(Navbar.prototype, {
</body> </body>
</html>`; </html>`;
const printWindow = window.open("", "_blank", "width=400,height=600");
if (!printWindow) { if (!printWindow) {
this.notification.add(
_t("Pop-up blocked. Please allow pop-ups for this site and try again."),
{ type: "warning" }
);
return; return;
} }
printWindow.document.open(); printWindow.document.open();