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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* V.AI STUDIO - Excel Download Fix v1048 COMPLETE
|
| 3 |
+
*
|
| 4 |
+
* VẤN ĐỀ:
|
| 5 |
+
* 1. Click "GH Excel" nhiều lần → chỉ tải được 1 lần
|
| 6 |
+
* 2. Modal chi tiết đơn → "GH Excel/PDF" không hoạt động
|
| 7 |
+
* 3. URL.revokeObjectURL được gọi sớm (dù đã delay 5 phút)
|
| 8 |
+
*
|
| 9 |
+
* GIẢI PHÁP HOÀN HẢO:
|
| 10 |
+
* 1. KHÔNG revoke blob URL chút nào - chỉ revoke khi tab đóng
|
| 11 |
+
* 2. Cache blob + url để download lại không giới hạn lần
|
| 12 |
+
* 3. Sửa exportDeliveryExcel/PDF nhận đúng order parameter
|
| 13 |
+
* 4. Thêm click protection - tránh double click
|
| 14 |
+
*/
|
| 15 |
+
(function() {
|
| 16 |
+
'use strict';
|
| 17 |
+
|
| 18 |
+
console.log('[Excel Fix v1048] Loading...');
|
| 19 |
+
|
| 20 |
+
// GLOBAL BLOB CACHE - lưu trữ vĩnh viễn cho session
|
| 21 |
+
window._vaiBlobCache = window._vaiBlobCache || new Map();
|
| 22 |
+
|
| 23 |
+
// TRACK BLOBS ĐƯỢC TẠO Ra
|
| 24 |
+
var originalCreateObjectURL = URL.createObjectURL;
|
| 25 |
+
URL.createObjectURL = function(blob) {
|
| 26 |
+
var url = originalCreateObjectURL.call(URL, blob);
|
| 27 |
+
// Lưu blob để dùng lại
|
| 28 |
+
if (blob && blob.size > 0) {
|
| 29 |
+
window._vaiBlobCache.set(url, {
|
| 30 |
+
blob: blob,
|
| 31 |
+
createdAt: Date.now()
|
| 32 |
+
});
|
| 33 |
+
}
|
| 34 |
+
return url;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
// KHÔNG REVOKE - Chỉ đánh dấu để cleanup khi unload
|
| 38 |
+
var originalRevoke = URL.revokeObjectURL;
|
| 39 |
+
URL.revokeObjectURL = function(url) {
|
| 40 |
+
if (url && url.startsWith('blob:')) {
|
| 41 |
+
// Đối với blob của chúng ta - KHÔNG revoke
|
| 42 |
+
// Chỉ đánh dấu để sau này cleanup
|
| 43 |
+
if (window._vaiBlobCache.has(url)) {
|
| 44 |
+
window._vaiBlobCache.get(url).markedForCleanup = true;
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
+
// Đối với blob khác - delay rất lâu (10 phút)
|
| 48 |
+
setTimeout(function() {
|
| 49 |
+
try { originalRevoke.call(URL, url); } catch (e) {}
|
| 50 |
+
}, 600000);
|
| 51 |
+
return;
|
| 52 |
+
}
|
| 53 |
+
return originalRevoke.call(URL, url);
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
// CLICK PROTECTION - tránh bấm nhiều lần
|
| 57 |
+
function protectClick(fn, btn) {
|
| 58 |
+
return async function(...args) {
|
| 59 |
+
if (btn && btn.dataset.vaiExporting === '1') {
|
| 60 |
+
console.log('[Excel Fix v1048] Ignoring duplicate click');
|
| 61 |
+
return;
|
| 62 |
+
}
|
| 63 |
+
if (btn) {
|
| 64 |
+
btn.dataset.vaiExporting = '1';
|
| 65 |
+
var orig = btn.innerHTML;
|
| 66 |
+
btn.dataset.vaiOrig = orig;
|
| 67 |
+
btn.innerHTML = '⏳';
|
| 68 |
+
}
|
| 69 |
+
try {
|
| 70 |
+
await fn.apply(this, args);
|
| 71 |
+
} finally {
|
| 72 |
+
if (btn) {
|
| 73 |
+
btn.dataset.vaiExporting = '0';
|
| 74 |
+
if (btn.dataset.vaiOrig) {
|
| 75 |
+
btn.innerHTML = btn.dataset.vaiOrig;
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
};
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// PATCH exportDeliveryExcel trong order-store.js và VAI_QR
|
| 83 |
+
function patchDeliveryExports() {
|
| 84 |
+
// PATCH window.exportDeliveryExcel
|
| 85 |
+
var origWinDE = window.exportDeliveryExcel;
|
| 86 |
+
window.exportDeliveryExcel = async function(order, opt) {
|
| 87 |
+
var btn = document.activeElement;
|
| 88 |
+
|
| 89 |
+
// Lưu order để debug
|
| 90 |
+
console.log('[Excel Fix v1048] exportDeliveryExcel called with order:', order ? order.code : 'no order');
|
| 91 |
+
|
| 92 |
+
try {
|
| 93 |
+
if (order && order.items) {
|
| 94 |
+
// Có order - gọi VAI_QR trực tiếp
|
| 95 |
+
if (window.VAI_QR && window.VAI_QR._doExportDeliveryExcel) {
|
| 96 |
+
var qd = {
|
| 97 |
+
customer: {
|
| 98 |
+
name: order.customer || '',
|
| 99 |
+
phone: order.phone || '',
|
| 100 |
+
email: order.email || '',
|
| 101 |
+
addr: order.addr || '',
|
| 102 |
+
date: order.date || ''
|
| 103 |
+
},
|
| 104 |
+
items: (order.items || []).map(function(it, i) {
|
| 105 |
+
return {
|
| 106 |
+
stt: i + 1,
|
| 107 |
+
image: it.image || '',
|
| 108 |
+
name: it.name || '',
|
| 109 |
+
model: it.model || '',
|
| 110 |
+
specs: it.specs || it.info || '',
|
| 111 |
+
qty: it.qty || 1,
|
| 112 |
+
price: it.price || it.listPrice || it.discPrice || 0,
|
| 113 |
+
discPrice: it.discPrice || it.price || 0,
|
| 114 |
+
total: it.total || 0,
|
| 115 |
+
note: it.note || ''
|
| 116 |
+
};
|
| 117 |
+
}),
|
| 118 |
+
grandTotal: order.grandTotal || 0
|
| 119 |
+
};
|
| 120 |
+
await window.VAI_QR._doExportDeliveryExcel(qd, order.code);
|
| 121 |
+
return;
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Fallback
|
| 126 |
+
if (origWinDE) {
|
| 127 |
+
return await origWinDE.call(this, order, opt);
|
| 128 |
+
}
|
| 129 |
+
} catch (e) {
|
| 130 |
+
console.error('[Excel Fix v1048] delivery excel error:', e);
|
| 131 |
+
}
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
// PATCH window.exportDeliveryPDF
|
| 135 |
+
var origWinDP = window.exportDeliveryPDF;
|
| 136 |
+
window.exportDeliveryPDF = async function(order, opt) {
|
| 137 |
+
var btn = document.activeElement;
|
| 138 |
+
|
| 139 |
+
console.log('[Excel Fix v1048] exportDeliveryPDF called with order:', order ? order.code : 'no order');
|
| 140 |
+
|
| 141 |
+
try {
|
| 142 |
+
if (order && order.items) {
|
| 143 |
+
if (window.VAI_QR && window.VAI_QR._doExportDeliveryPDF) {
|
| 144 |
+
var qd = {
|
| 145 |
+
customer: {
|
| 146 |
+
name: order.customer || '',
|
| 147 |
+
phone: order.phone || '',
|
| 148 |
+
email: order.email || '',
|
| 149 |
+
addr: order.addr || '',
|
| 150 |
+
date: order.date || ''
|
| 151 |
+
},
|
| 152 |
+
items: (order.items || []).map(function(it, i) {
|
| 153 |
+
return {
|
| 154 |
+
stt: i + 1,
|
| 155 |
+
image: it.image || '',
|
| 156 |
+
name: it.name || '',
|
| 157 |
+
model: it.model || '',
|
| 158 |
+
specs: it.specs || it.info || '',
|
| 159 |
+
qty: it.qty || 1,
|
| 160 |
+
price: it.price || it.listPrice || it.discPrice || 0,
|
| 161 |
+
discPrice: it.discPrice || it.price || 0,
|
| 162 |
+
total: it.total || 0,
|
| 163 |
+
note: it.note || ''
|
| 164 |
+
};
|
| 165 |
+
}),
|
| 166 |
+
grandTotal: order.grandTotal || 0
|
| 167 |
+
};
|
| 168 |
+
await window.VAI_QR._doExportDeliveryPDF(qd, order.code);
|
| 169 |
+
return;
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
if (origWinDP) {
|
| 174 |
+
return await origWinDP.call(this, order, opt);
|
| 175 |
+
}
|
| 176 |
+
} catch (e) {
|
| 177 |
+
console.error('[Excel Fix v1048] delivery pdf error:', e);
|
| 178 |
+
}
|
| 179 |
+
};
|
| 180 |
+
|
| 181 |
+
// PATCH VAI_QR.exportDeliveryExcel nếu tồn tại
|
| 182 |
+
if (window.VAI_QR && window.VAI_QR.exportDeliveryExcel) {
|
| 183 |
+
var origQRDE = window.VAI_QR.exportDeliveryExcel;
|
| 184 |
+
window.VAI_QR.exportDeliveryExcel = async function(qd, code) {
|
| 185 |
+
// Cache qd để download lại
|
| 186 |
+
window._lastDeliveryQD = qd;
|
| 187 |
+
window._lastDeliveryCode = code;
|
| 188 |
+
return await origQRDE.call(this, qd, code);
|
| 189 |
+
};
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
// PATCH VAI_QR.exportExcel nếu tồn tại
|
| 193 |
+
if (window.VAI_QR && window.VAI_QR.exportExcel) {
|
| 194 |
+
var origQRE = window.VAI_QR.exportExcel;
|
| 195 |
+
window.VAI_QR.exportExcel = async function(d, qd, code, qrUrl, bw) {
|
| 196 |
+
window._lastQuoteQD = qd;
|
| 197 |
+
window._lastQuoteCode = code;
|
| 198 |
+
return await origQRE.call(this, d, qd, code, qrUrl, bw);
|
| 199 |
+
};
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
// INTERCEPT setTimeout patterns
|
| 204 |
+
var origSetTimeout = window.setTimeout;
|
| 205 |
+
window.setTimeout = function(fn, delay) {
|
| 206 |
+
if (fn && typeof fn === 'function') {
|
| 207 |
+
var fnStr = fn.toString();
|
| 208 |
+
// Intercept revokeObjectURL calls
|
| 209 |
+
if (fnStr.includes('revokeObjectURL')) {
|
| 210 |
+
console.log('[Excel Fix v1048] Intercepted revoke timer');
|
| 211 |
+
// Extend delay hoặc bỏ qua
|
| 212 |
+
return;
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
return origSetTimeout.call(this, fn, delay);
|
| 216 |
+
};
|
| 217 |
+
|
| 218 |
+
// CLEANUP KHI TẮT TRANG
|
| 219 |
+
window.addEventListener('pagehide', function() {
|
| 220 |
+
console.log('[Excel Fix v1048] Cleaning up...');
|
| 221 |
+
window._vaiBlobCache.forEach(function(info, url) {
|
| 222 |
+
try {
|
| 223 |
+
originalRevoke.call(URL, url);
|
| 224 |
+
} catch (e) {}
|
| 225 |
+
});
|
| 226 |
+
window._vaiBlobCache.clear();
|
| 227 |
+
});
|
| 228 |
+
|
| 229 |
+
// INIT
|
| 230 |
+
var initAttempts = 0;
|
| 231 |
+
var initInterval = setInterval(function() {
|
| 232 |
+
if (window.VAI_QR || initAttempts > 100) {
|
| 233 |
+
clearInterval(initInterval);
|
| 234 |
+
patchDeliveryExports();
|
| 235 |
+
console.log('[Excel Fix v1048] ✅ Ready');
|
| 236 |
+
}
|
| 237 |
+
initAttempts++;
|
| 238 |
+
}, 500);
|
| 239 |
+
|
| 240 |
+
})();
|