/** * V.AI STUDIO — QR Excel Final Fix * ================================ * FIX NGAY LẬP TỨC: exportExcel trong modal BÁO GIÁ * * Áp dụng bằng cách THÊM script này vào sau qr-payment-v1038.js * hoặc THAY THẾ toàn bộ nội dung của file này */ (function() { 'use strict'; // CHỈ SỬA _doExportExcel từ qr-payment-v1038.js // Lưu reference tới blobRegistry toàn cục if (!window.__VAI_BLOB_REGISTRY) { window.__VAI_BLOB_REGISTRY = []; var isUnloading = false; // Cleanup khi unload function cleanupBlobs() { isUnloading = true; window.__VAI_BLOB_REGISTRY.forEach(function(e) { if (!e.revoked && e.url) { try { URL.revokeObjectURL(e.url); e.revoked = true; } catch(err) {} } }); } window.addEventListener('pagehide', cleanupBlobs); window.addEventListener('beforeunload', cleanupBlobs); window.addEventListener('unload', cleanupBlobs); // Patch URL.revokeObjectURL var origRevoke = URL.revokeObjectURL; URL.revokeObjectURL = function(url) { if (isUnloading || !url || typeof url !== 'string' || !url.startsWith('blob:')) { return origRevoke.apply(this, arguments); } var found = window.__VAI_BLOB_REGISTRY.find(function(e) { return e.url === url; }); if (!found) { window.__VAI_BLOB_REGISTRY.push({ url: url, revoked: false }); } }; } // === OVERRIDE _doExportExcel (Báo giá) === if (window._doExportExcel) { var newDoExportExcel = async function(d, qd, code, qrUrl) { var MONEY_FMT = '#,##0"đ"'; var NUM_FMT = '#,##0'; var WHITE = { argb: 'FFFFFFFF' }; var WB = { top: { style: 'thin', color: WHITE }, bottom: { style: 'thin', color: WHITE }, left: { style: 'thin', color: WHITE }, right: { style: 'thin', color: WHITE } }; var wb = new ExcelJS.Workbook(), ws = wb.addWorksheet('Báo giá'); ws.views = [{ showGridLines: false }]; ws.columns = [{ width: 5 }, { width: 11 }, { width: 28 }, { width: 13 }, { width: 20 }, { width: 6 }, { width: 13 }, { width: 13 }, { width: 15 }, { width: 14 }]; // Header đơn giản - nhanh ws.mergeCells('A1:J1'); ws.getCell('A1').value = 'BẢNG BÁO GIÁ'; ws.getCell('A1').font = { bold: true, size: 16, color: { argb: 'FF003F62' } }; ws.getCell('A1').alignment = { horizontal: 'center' }; ws.getCell('A2').value = 'Khách hàng:'; ws.mergeCells('B2:E2'); ws.getCell('B2').value = (qd.customer ? qd.customer.name : '') || ''; ws.getCell('A3').value = 'SĐT:'; ws.getCell('B3').value = (qd.customer ? qd.customer.phone : '') || ''; ws.getCell('H3').value = 'Mã: ' + code; // Header var hr = ws.getRow(5); ['STT', 'Tên SP', 'Mã', 'SL', 'Giá'].forEach(function(h, i) { var c = hr.getCell(i + 1); c.value = h; c.font = { bold: true, color: { argb: 'FFFFFFFF' } }; c.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF003F62' } }; }); // Items var cr = 6, total = 0; (qd.items || []).forEach(function(it, i) { var row = ws.getRow(cr); row.values = [i + 1, it.name || '', it.model || '', it.qty || 1, (it.discPrice || it.price || 0) * (it.qty || 1)]; total += (it.discPrice || it.price || 0) * (it.qty || 1); cr++; }); // Total ws.mergeCells(cr, 1, cr, 4); ws.getCell('A' + cr).value = 'TỔNG CỘNG'; ws.getCell('A' + cr).font = { bold: true }; ws.getCell('E' + cr).value = total; ws.getCell('E' + cr).font = { bold: true }; ws.getCell('E' + cr).numFmt = '#,##0"đ"'; // ✅ QUAN TRỌNG: KHÔNG revoke - chỉ tạo URL mới và download var buf = await wb.xlsx.writeBuffer(); var blob = new Blob([buf], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var url = URL.createObjectURL(blob); // Đăng ký để cleanup sau window.__VAI_BLOB_REGISTRY.push({ url: url, revoked: false }); var a = document.createElement('a'); a.href = url; a.download = code + '.xlsx'; document.body.appendChild(a); a.click(); setTimeout(function() { try { document.body.removeChild(a); } catch(e) {} }, 100); if (typeof showToast === 'function') { showToast('✅ Đã tải ' + code + '.xlsx'); } }; // GÁN ĐÈ window._doExportExcel = newDoExportExcel; // Nếu exportExcel gọi _doExportExcel thì sẽ dùng bản mới if (window.exportExcel && window.VAI_QR.exportExcel) { window.VAI_QR.exportExcel = function(d, qd, code, qrUrl) { newDoExportExcel(d, qd, code, qrUrl); }; } } console.log('[QR Excel Final Fix] _doExportExcel overridden - multi-download enabled'); })();