bep40 commited on
Commit
e81a9ca
·
verified ·
1 Parent(s): af4db02

Upload qr-excel-final-fix.js

Browse files
Files changed (1) hide show
  1. qr-excel-final-fix.js +143 -0
qr-excel-final-fix.js ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * V.AI STUDIO — QR Excel Final Fix
3
+ * ================================
4
+ * FIX NGAY LẬP TỨC: exportExcel trong modal BÁO GIÁ
5
+ *
6
+ * Áp dụng bằng cách THÊM script này vào sau qr-payment-v1038.js
7
+ * hoặc THAY THẾ toàn bộ nội dung của file này
8
+ */
9
+ (function() {
10
+ 'use strict';
11
+
12
+ // CHỈ SỬA _doExportExcel từ qr-payment-v1038.js
13
+
14
+ // Lưu reference tới blobRegistry toàn cục
15
+ if (!window.__VAI_BLOB_REGISTRY) {
16
+ window.__VAI_BLOB_REGISTRY = [];
17
+ var isUnloading = false;
18
+
19
+ // Cleanup khi unload
20
+ function cleanupBlobs() {
21
+ isUnloading = true;
22
+ window.__VAI_BLOB_REGISTRY.forEach(function(e) {
23
+ if (!e.revoked && e.url) {
24
+ try { URL.revokeObjectURL(e.url); e.revoked = true; } catch(err) {}
25
+ }
26
+ });
27
+ }
28
+
29
+ window.addEventListener('pagehide', cleanupBlobs);
30
+ window.addEventListener('beforeunload', cleanupBlobs);
31
+ window.addEventListener('unload', cleanupBlobs);
32
+
33
+ // Patch URL.revokeObjectURL
34
+ var origRevoke = URL.revokeObjectURL;
35
+ URL.revokeObjectURL = function(url) {
36
+ if (isUnloading || !url || typeof url !== 'string' || !url.startsWith('blob:')) {
37
+ return origRevoke.apply(this, arguments);
38
+ }
39
+ var found = window.__VAI_BLOB_REGISTRY.find(function(e) { return e.url === url; });
40
+ if (!found) {
41
+ window.__VAI_BLOB_REGISTRY.push({ url: url, revoked: false });
42
+ }
43
+ };
44
+ }
45
+
46
+ // === OVERRIDE _doExportExcel (Báo giá) ===
47
+ if (window._doExportExcel) {
48
+ var newDoExportExcel = async function(d, qd, code, qrUrl) {
49
+ var MONEY_FMT = '#,##0"đ"';
50
+ var NUM_FMT = '#,##0';
51
+ var WHITE = { argb: 'FFFFFFFF' };
52
+ var WB = {
53
+ top: { style: 'thin', color: WHITE },
54
+ bottom: { style: 'thin', color: WHITE },
55
+ left: { style: 'thin', color: WHITE },
56
+ right: { style: 'thin', color: WHITE }
57
+ };
58
+
59
+ var wb = new ExcelJS.Workbook(), ws = wb.addWorksheet('Báo giá');
60
+ ws.views = [{ showGridLines: false }];
61
+ ws.columns = [{ width: 5 }, { width: 11 }, { width: 28 }, { width: 13 },
62
+ { width: 20 }, { width: 6 }, { width: 13 }, { width: 13 },
63
+ { width: 15 }, { width: 14 }];
64
+
65
+ // Header đơn giản - nhanh
66
+ ws.mergeCells('A1:J1');
67
+ ws.getCell('A1').value = 'BẢNG BÁO GIÁ';
68
+ ws.getCell('A1').font = { bold: true, size: 16, color: { argb: 'FF003F62' } };
69
+ ws.getCell('A1').alignment = { horizontal: 'center' };
70
+
71
+ ws.getCell('A2').value = 'Khách hàng:';
72
+ ws.mergeCells('B2:E2');
73
+ ws.getCell('B2').value = (qd.customer ? qd.customer.name : '') || '';
74
+
75
+ ws.getCell('A3').value = 'SĐT:';
76
+ ws.getCell('B3').value = (qd.customer ? qd.customer.phone : '') || '';
77
+ ws.getCell('H3').value = 'Mã: ' + code;
78
+
79
+ // Header
80
+ var hr = ws.getRow(5);
81
+ ['STT', 'Tên SP', 'Mã', 'SL', 'Giá'].forEach(function(h, i) {
82
+ var c = hr.getCell(i + 1);
83
+ c.value = h;
84
+ c.font = { bold: true, color: { argb: 'FFFFFFFF' } };
85
+ c.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF003F62' } };
86
+ });
87
+
88
+ // Items
89
+ var cr = 6, total = 0;
90
+ (qd.items || []).forEach(function(it, i) {
91
+ var row = ws.getRow(cr);
92
+ row.values = [i + 1, it.name || '', it.model || '',
93
+ it.qty || 1, (it.discPrice || it.price || 0) * (it.qty || 1)];
94
+ total += (it.discPrice || it.price || 0) * (it.qty || 1);
95
+ cr++;
96
+ });
97
+
98
+ // Total
99
+ ws.mergeCells(cr, 1, cr, 4);
100
+ ws.getCell('A' + cr).value = 'TỔNG CỘNG';
101
+ ws.getCell('A' + cr).font = { bold: true };
102
+ ws.getCell('E' + cr).value = total;
103
+ ws.getCell('E' + cr).font = { bold: true };
104
+ ws.getCell('E' + cr).numFmt = '#,##0"đ"';
105
+
106
+ // ✅ QUAN TRỌNG: KHÔNG revoke - chỉ tạo URL mới và download
107
+ var buf = await wb.xlsx.writeBuffer();
108
+ var blob = new Blob([buf], {
109
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
110
+ });
111
+ var url = URL.createObjectURL(blob);
112
+
113
+ // Đăng ký để cleanup sau
114
+ window.__VAI_BLOB_REGISTRY.push({ url: url, revoked: false });
115
+
116
+ var a = document.createElement('a');
117
+ a.href = url;
118
+ a.download = code + '.xlsx';
119
+ document.body.appendChild(a);
120
+ a.click();
121
+
122
+ setTimeout(function() {
123
+ try { document.body.removeChild(a); } catch(e) {}
124
+ }, 100);
125
+
126
+ if (typeof showToast === 'function') {
127
+ showToast('✅ Đã tải ' + code + '.xlsx');
128
+ }
129
+ };
130
+
131
+ // GÁN ĐÈ
132
+ window._doExportExcel = newDoExportExcel;
133
+
134
+ // Nếu exportExcel gọi _doExportExcel thì sẽ dùng bản mới
135
+ if (window.exportExcel && window.VAI_QR.exportExcel) {
136
+ window.VAI_QR.exportExcel = function(d, qd, code, qrUrl) {
137
+ newDoExportExcel(d, qd, code, qrUrl);
138
+ };
139
+ }
140
+ }
141
+
142
+ console.log('[QR Excel Final Fix] _doExportExcel overridden - multi-download enabled');
143
+ })();