Upload auto-patch-gh-excel.js
Browse files- auto-patch-gh-excel.js +185 -215
auto-patch-gh-excel.js
CHANGED
|
@@ -1,238 +1,208 @@
|
|
| 1 |
/**
|
| 2 |
-
* V.AI STUDIO - Auto Patch GH Excel
|
| 3 |
*
|
| 4 |
* TỰ ĐỘNG PATCH mà không cần sửa index.html
|
| 5 |
* Chỉ cần thêm dòng: <script src="auto-patch-gh-excel.js"></script> vào index.html
|
| 6 |
*
|
| 7 |
-
* FIX:
|
| 8 |
-
* - exportDeliveryExcel
|
| 9 |
-
* - Click protection (chống bấm nhiều lần)
|
| 10 |
-
* - Blob retention
|
|
|
|
| 11 |
*/
|
| 12 |
(function() {
|
| 13 |
'use strict';
|
| 14 |
|
| 15 |
-
// ===
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
date: order.date || ''
|
| 37 |
-
},
|
| 38 |
-
items: (order.items || []).map(function(it, i) {
|
| 39 |
-
return {
|
| 40 |
-
stt: i + 1,
|
| 41 |
-
image: it.image || '',
|
| 42 |
-
name: it.name || '',
|
| 43 |
-
model: it.model || '',
|
| 44 |
-
specs: it.specs || it.info || '',
|
| 45 |
-
qty: it.qty || 1,
|
| 46 |
-
price: it.price || it.listPrice || it.discPrice || 0,
|
| 47 |
-
discPrice: it.discPrice || it.price || 0,
|
| 48 |
-
total: it.total || 0,
|
| 49 |
-
note: it.note || ''
|
| 50 |
-
};
|
| 51 |
-
}),
|
| 52 |
-
grandTotal: order.grandTotal || 0
|
| 53 |
-
}
|
| 54 |
-
};
|
| 55 |
}
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
-
//
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return;
|
| 80 |
-
}
|
| 81 |
-
if (btn) setBtnState(btn, true);
|
| 82 |
-
|
| 83 |
-
try {
|
| 84 |
-
// If order provided, use it
|
| 85 |
-
if (order && order.items && order.items.length) {
|
| 86 |
-
var vaData = orderToVAIData(order);
|
| 87 |
-
var code = order.code || 'GH-' + Date.now();
|
| 88 |
-
|
| 89 |
-
// Use qr-payment.js internal function if available
|
| 90 |
-
if (window.VAI_QR && window.VAI_QR._doExportDeliveryExcel) {
|
| 91 |
-
await window.VAI_QR._doExportDeliveryExcel(vaData.qd, code);
|
| 92 |
-
console.log('[AutoPatch] GH Excel done for order: ' + code);
|
| 93 |
-
return;
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
// Fallback: rebuild HTML manually
|
| 97 |
-
if (window.VAI_QR && window.VAI_QR.buildDeliveryHTML) {
|
| 98 |
-
let html = window.VAI_QR.buildDeliveryHTML(vaData.qd, code);
|
| 99 |
-
let div = document.createElement('div');
|
| 100 |
-
div.style.cssText = 'position:absolute;left:-9999px;top:0;width:700px;background:#fff;padding:28px';
|
| 101 |
-
div.innerHTML = html;
|
| 102 |
-
document.body.appendChild(div);
|
| 103 |
-
|
| 104 |
-
await new Promise(r => setTimeout(r, 300));
|
| 105 |
-
|
| 106 |
-
if (window.ExcelJS) {
|
| 107 |
-
let wb = new ExcelJS.Workbook();
|
| 108 |
-
let ws = wb.addWorksheet('Giao hàng');
|
| 109 |
-
ws.views = [{showGridLines: false}];
|
| 110 |
-
ws.columns = [{width: 5}, {width: 34}, {width: 14}, {width: 8}, {width: 20}];
|
| 111 |
-
|
| 112 |
-
ws.getCell('A1').value = 'PHIẾU GIAO HÀNG';
|
| 113 |
-
ws.getCell('A2').value = 'KH: ' + (vaData.qd.customer.name || '');
|
| 114 |
-
ws.getCell('D2').value = 'Mã: ' + code;
|
| 115 |
-
ws.getCell('A3').value = 'SĐT: ' + (vaData.qd.customer.phone || '');
|
| 116 |
-
ws.getCell('A4').value = 'Địa chỉ: ' + (vaData.qd.customer.addr || '');
|
| 117 |
-
|
| 118 |
-
let headerRow = ws.getRow(6);
|
| 119 |
-
['STT', 'Tên sản phẩm', 'Mã SP', 'SL', 'Ghi chú'].forEach(function(h, i) {
|
| 120 |
-
let c = headerRow.getCell(i + 1);
|
| 121 |
-
c.value = h;
|
| 122 |
-
c.fill = {type: 'pattern', pattern: 'solid', fgColor: {argb: 'FF003F62'}};
|
| 123 |
-
c.font = {bold: true, color: {argb: 'FFFFFFFF'}};
|
| 124 |
-
});
|
| 125 |
-
|
| 126 |
-
let cr = 7;
|
| 127 |
-
(vaData.qd.items || []).forEach(function(it) {
|
| 128 |
-
let row = ws.getRow(cr);
|
| 129 |
-
row.getCell(1).value = it.stt;
|
| 130 |
-
row.getCell(2).value = it.name;
|
| 131 |
-
row.getCell(3).value = it.model;
|
| 132 |
-
row.getCell(4).value = it.qty;
|
| 133 |
-
row.getCell(5).value = it.note;
|
| 134 |
-
cr++;
|
| 135 |
-
});
|
| 136 |
-
|
| 137 |
-
let buf = await wb.xlsx.writeBuffer();
|
| 138 |
-
let blob = new Blob([buf], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
|
| 139 |
-
let url = URL.createObjectURL(blob);
|
| 140 |
-
let a = document.createElement('a');
|
| 141 |
-
a.href = url;
|
| 142 |
-
a.download = 'GH-' + code + '.xlsx';
|
| 143 |
-
a.click();
|
| 144 |
-
// Keep blob for 5 minutes
|
| 145 |
-
setTimeout(() => URL.revokeObjectURL(url), 300000);
|
| 146 |
-
}
|
| 147 |
-
div.remove();
|
| 148 |
-
return;
|
| 149 |
-
}
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
// Fallback to original
|
| 153 |
-
if (origExportDE) {
|
| 154 |
-
await origExportDE.call(this, order, opt);
|
| 155 |
-
}
|
| 156 |
-
} catch (e) {
|
| 157 |
-
console.error('[AutoPatch] Export error:', e);
|
| 158 |
-
if (typeof showToast === 'function') {
|
| 159 |
-
showToast('❌ Xuất file thất bại: ' + (e.message || e));
|
| 160 |
-
}
|
| 161 |
-
} finally {
|
| 162 |
-
setTimeout(() => { if (btn) setBtnState(btn, false); }, 1000);
|
| 163 |
-
}
|
| 164 |
};
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
if (
|
| 181 |
-
|
| 182 |
-
console.log('[AutoPatch] GH PDF done for order: ' + code);
|
| 183 |
-
return;
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
if (window.VAI_QR && window.VAI_QR.buildDeliveryHTML) {
|
| 187 |
-
let html = window.VAI_QR.buildDeliveryHTML(vaData.qd, code);
|
| 188 |
-
let div = document.createElement('div');
|
| 189 |
-
div.style.cssText = 'position:absolute;left:-9999px;top:0;width:700px;background:#fff;padding:28px';
|
| 190 |
-
div.innerHTML = html;
|
| 191 |
-
document.body.appendChild(div);
|
| 192 |
-
|
| 193 |
-
await new Promise(r => setTimeout(r, 300));
|
| 194 |
-
|
| 195 |
-
if (window.html2canvas && window.jspdf) {
|
| 196 |
-
let canvas = await html2canvas(div, {scale: 2, useCORS: true, backgroundColor: '#fff'});
|
| 197 |
-
let imgData = canvas.toDataURL('image/png');
|
| 198 |
-
let {jsPDF} = window.jspdf;
|
| 199 |
-
let pdf = new jsPDF({orientation: 'p', unit: 'pt', format: 'a4'});
|
| 200 |
-
let margin = 25;
|
| 201 |
-
let usableW = 595 - margin * 2;
|
| 202 |
-
let ratio = canvas.height / canvas.width;
|
| 203 |
-
let imgH = usableW * ratio;
|
| 204 |
-
pdf.addImage(imgData, 'PNG', margin, margin, usableW, imgH > 842 - margin * 2 ? 842 - margin * 2 : imgH);
|
| 205 |
-
pdf.save('GH-' + code + '.pdf');
|
| 206 |
-
}
|
| 207 |
-
div.remove();
|
| 208 |
-
return;
|
| 209 |
-
}
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
if (origExportDP) {
|
| 213 |
-
await origExportDP.call(this, order, opt);
|
| 214 |
-
}
|
| 215 |
-
} catch (e) {
|
| 216 |
-
console.error('[AutoPatch] Export error:', e);
|
| 217 |
-
if (typeof showToast === 'function') {
|
| 218 |
-
showToast('❌ Xuất file thất bại: ' + (e.message || e));
|
| 219 |
-
}
|
| 220 |
-
} finally {
|
| 221 |
-
setTimeout(() => { if (btn) setBtnState(btn, false); }, 1000);
|
| 222 |
}
|
| 223 |
};
|
| 224 |
-
|
| 225 |
-
console.log('[AutoPatch v1046] GH Excel/PDF patched - ready!');
|
| 226 |
}
|
| 227 |
|
| 228 |
-
// Start patching
|
| 229 |
if (document.readyState === 'loading') {
|
| 230 |
-
document.addEventListener('DOMContentLoaded',
|
| 231 |
} else {
|
| 232 |
-
|
| 233 |
}
|
| 234 |
-
|
| 235 |
-
setTimeout(
|
| 236 |
-
setTimeout(initPatch, 5000);
|
| 237 |
|
| 238 |
})();
|
|
|
|
| 1 |
/**
|
| 2 |
+
* V.AI STUDIO - Auto Patch GH Excel - v2.0
|
| 3 |
*
|
| 4 |
* TỰ ĐỘNG PATCH mà không cần sửa index.html
|
| 5 |
* Chỉ cần thêm dòng: <script src="auto-patch-gh-excel.js"></script> vào index.html
|
| 6 |
*
|
| 7 |
+
* FIX HOÀN HẢO:
|
| 8 |
+
* - exportDeliveryExcel nhận order parameter đúng
|
| 9 |
+
* - Click protection (chống bấm nhiều lần)
|
| 10 |
+
* - Blob retention đến khi unload (tải nhiều lần được)
|
| 11 |
+
* - Hỗ trợ cả Quote & Order modal
|
| 12 |
*/
|
| 13 |
(function() {
|
| 14 |
'use strict';
|
| 15 |
|
| 16 |
+
// ===== GLOBAL BLOB REGISTRY =====
|
| 17 |
+
window.__VAI_BLOB_REGISTRY = window.__VAI_BLOB_REGISTRY || { blobs: [], isUnloading: false };
|
| 18 |
+
var reg = window.__VAI_BLOB_REGISTRY;
|
| 19 |
+
|
| 20 |
+
// Cleanup on unload
|
| 21 |
+
function cleanupBlobs() {
|
| 22 |
+
reg.isUnloading = true;
|
| 23 |
+
reg.blobs.forEach(function(b) {
|
| 24 |
+
if (!b.revoked) {
|
| 25 |
+
try { URL.revokeObjectURL(b.url); b.revoked = true; } catch(e) {}
|
| 26 |
+
}
|
| 27 |
+
});
|
| 28 |
+
reg.blobs = [];
|
| 29 |
+
}
|
| 30 |
+
window.addEventListener('pagehide', cleanupBlobs);
|
| 31 |
+
window.addEventListener('beforeunload', cleanupBlobs);
|
| 32 |
+
window.addEventListener('unload', cleanupBlobs);
|
| 33 |
+
|
| 34 |
+
// ===== PATCH URL.revokeObjectURL - CRITICAL =====
|
| 35 |
+
if (!window.__VAI_REVOKE_PATCHED_AUTO) {
|
| 36 |
+
var origRevoke = URL.revokeObjectURL;
|
| 37 |
+
URL.revokeObjectURL = function(url) {
|
| 38 |
+
if (reg.isUnloading || !url || typeof url !== 'string' || !url.startsWith('blob:')) {
|
| 39 |
+
return origRevoke.call(this, url);
|
| 40 |
+
}
|
| 41 |
+
var exists = reg.blobs.find(function(b) { return b.url === url; });
|
| 42 |
+
if (!exists) reg.blobs.push({ url: url, revoked: false, created: Date.now() });
|
| 43 |
+
return undefined;
|
| 44 |
+
};
|
| 45 |
+
window.__VAI_REVOKE_PATCHED_AUTO = true;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// ===== UTILITIES =====
|
| 49 |
+
function fmt(n) { return n && !isNaN(n) ? Number(n).toLocaleString('vi-VN') + 'đ' : '0đ'; }
|
| 50 |
+
function stripDiacritics(s) { return s.normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[Đđ]/g,'D'); }
|
| 51 |
+
function genCode(name) {
|
| 52 |
+
var d = new Date(), dd = String(d.getDate()).padStart(2,'0'), mm = String(d.getMonth()+1).padStart(2,'0'), yy = String(d.getFullYear()).slice(-2);
|
| 53 |
+
var ini = '';
|
| 54 |
+
if (name && name.trim()) {
|
| 55 |
+
name.trim().split(/\s+/).forEach(function(w) {
|
| 56 |
+
if (w) { var c = stripDiacritics(w.charAt(0)).toUpperCase(); if (/[A-Z]/.test(c)) ini += c; }
|
| 57 |
+
});
|
| 58 |
}
|
| 59 |
+
return 'VAS' + (ini || 'X') + dd + mm + yy;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// ===== BUTTON STATE =====
|
| 63 |
+
function setBtn(b, exporting) {
|
| 64 |
+
if (!b) return;
|
| 65 |
+
if (exporting) {
|
| 66 |
+
b.disabled = true;
|
| 67 |
+
b.dataset.vaiExporting = '1';
|
| 68 |
+
b._orig = b.innerHTML;
|
| 69 |
+
b.innerHTML = '<span style="opacity:0.7">⏳ Đang xuất...</span>';
|
| 70 |
+
} else {
|
| 71 |
+
b.disabled = false;
|
| 72 |
+
b.dataset.vaiExporting = '0';
|
| 73 |
+
if (b._orig) b.innerHTML = b._orig;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// ===== DOWNLOAD BLOB =====
|
| 78 |
+
function dlBlob(blob, filename) {
|
| 79 |
+
var url = URL.createObjectURL(blob);
|
| 80 |
+
reg.blobs.push({ url: url, revoked: false });
|
| 81 |
+
var a = document.createElement('a');
|
| 82 |
+
a.href = url; a.download = filename; a.style.cssText = 'position:absolute;left:-9999px';
|
| 83 |
+
document.body.appendChild(a); a.click();
|
| 84 |
+
setTimeout(function() { try { document.body.removeChild(a); } catch(e) {} }, 100);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// ===== EXPORT QUOTE EXCEL =====
|
| 88 |
+
async function exportQuoteExcel(d, c) {
|
| 89 |
+
if (typeof ExcelJS === 'undefined') return alert('⚠️ Chưa có ExcelJS'), false;
|
| 90 |
+
try {
|
| 91 |
+
var wb = new ExcelJS.Workbook(), ws = wb.addWorksheet('Báo giá');
|
| 92 |
+
ws.views = [{ showGridLines: false }];
|
| 93 |
+
ws.columns = [{width:5},{width:11},{width:28},{width:13},{width:20},{width:6},{width:13},{width:13},{width:15},{width:14}];
|
| 94 |
+
ws.mergeCells('A3:J3'); ws.getCell('A3').value='BẢNG BÁO GIÁ';
|
| 95 |
+
ws.getCell('A3').font = { bold:true, size:18, color:{argb:'FFDB9815'} };
|
| 96 |
+
ws.getCell('A3').alignment = { horizontal:'center' };
|
| 97 |
+
ws.getCell('A5').value='Khách hàng:'; ws.mergeCells('B5:E5'); ws.getCell('B5').value=d.customer?.name||'';
|
| 98 |
+
ws.getCell('H5').value='Mã đơn:'; ws.mergeCells('I5:J5'); ws.getCell('I5').value=c;
|
| 99 |
+
ws.getCell('A6').value='SĐT:'; ws.mergeCells('B6:E6'); ws.getCell('B6').value=d.customer?.phone||'';
|
| 100 |
+
ws.getCell('H6').value='Ngày:'; ws.mergeCells('I6:J6'); ws.getCell('I6').value=d.customer?.date||'';
|
| 101 |
+
ws.getCell('A7').value='Email:'; ws.mergeCells('B7:E7'); ws.getCell('B7').value=d.customer?.email||'';
|
| 102 |
+
ws.getCell('A8').value='Địa chỉ:'; ws.mergeCells('B8:J8'); ws.getCell('B8').value=d.customer?.addr||'';
|
| 103 |
+
|
| 104 |
+
var hr = ws.getRow(10);
|
| 105 |
+
['STT','Hình','Tên sản phẩm','Mã SP','Thông tin','SL','Đơn giá','Giá CK','Thành tiền','Ghi chú'].forEach(function(h,i){
|
| 106 |
+
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'}};
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
var cr = 11, fr = cr;
|
| 110 |
+
(d.items||[]).forEach(function(it,i){
|
| 111 |
+
var r = ws.getRow(cr), bg = i%2===0?'FFF8FAFC':'FFFFFFFF'; r.height=50;
|
| 112 |
+
r.getCell(1).value=it.stt||i+1; r.getCell(3).value=it.name||''; r.getCell(3).font={bold:true,size:9};
|
| 113 |
+
r.getCell(4).value=it.model||''; r.getCell(5).value=it.specs||''; r.getCell(5).font={size:8,color:{argb:'FF64748B'}};
|
| 114 |
+
r.getCell(6).value=Number(it.qty||1); r.getCell(6).numFmt='#,##0';
|
| 115 |
+
r.getCell(7).value=Number(it.price||0); r.getCell(7).numFmt='#,##0"đ"';
|
| 116 |
+
r.getCell(8).value=Number(it.discPrice||it.price||0); r.getCell(8).numFmt='#,##0"đ"';
|
| 117 |
+
r.getCell(8).font=(it.price||0)>(it.discPrice||0)?{bold:true,color:{argb:'FFDC3545'}}:{};
|
| 118 |
+
r.getCell(9).value=Number(it.total||0); r.getCell(9).numFmt='#,##0"đ"'; r.getCell(9).font={bold:true};
|
| 119 |
+
r.getCell(10).value=it.note||''; cr++;
|
| 120 |
+
});
|
| 121 |
+
|
| 122 |
+
ws.getRow(cr).height=28; ws.mergeCells(cr,1,cr,5); ws.getCell('A'+cr).value='TỔNG CỘNG';
|
| 123 |
+
ws.getCell('A'+cr).font={bold:true,size:13,color:{argb:'FFFFFFFF'}}; ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:'FF003F62'}};
|
| 124 |
+
ws.getCell('E'+cr).value={formula:'SUM(I'+fr+':I'+(cr-1)+')',result:Number(d.grandTotal||0)}; ws.getCell('E'+cr).numFmt='#,##0"đ"';
|
| 125 |
+
ws.getCell('E'+cr).font={bold:true,size:14,color:{argb:'FFF0B840'}};
|
| 126 |
+
wb.calcProperties.fullCalcOnLoad=true;
|
| 127 |
+
|
| 128 |
+
var buf = await wb.xlsx.writeBuffer();
|
| 129 |
+
dlBlob(new Blob([buf],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}), c+'.xlsx');
|
| 130 |
+
return true;
|
| 131 |
+
} catch(e) { console.error('[AutoPatch] exportQuoteExcel error:',e); alert('❌ Lỗi: '+e.message); return false; }
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// ===== EXPORT DELIVERY EXCEL =====
|
| 135 |
+
async function exportDeliveryExcel(d, c) {
|
| 136 |
+
if (typeof ExcelJS === 'undefined') return alert('⚠️ Chưa có ExcelJS'), false;
|
| 137 |
+
try {
|
| 138 |
+
var wb = new ExcelJS.Workbook(), ws = wb.addWorksheet('Giao hàng');
|
| 139 |
+
ws.views = [{ showGridLines: false }]; ws.columns = [{width:5},{width:34},{width:14},{width:8},{width:20}];
|
| 140 |
+
ws.mergeCells('A1:E1'); ws.getCell('A1').value='PHIẾU GIAO HÀNG'; ws.getCell('A1').font={bold:true,size:16,color:{argb:'FF003F62'}}; ws.getCell('A1').alignment={horizontal:'center'};
|
| 141 |
+
ws.getCell('A2').value='KH: '+(d.customer?.name||''); ws.getCell('D2').value='Mã: '+c;
|
| 142 |
+
ws.getCell('A3').value='SĐT: '+(d.customer?.phone||''); ws.getCell('D3').value='Ngày: '+(d.customer?.date||'');
|
| 143 |
+
ws.getCell('A4').value='Địa chỉ: '+(d.customer?.addr||'');
|
| 144 |
+
|
| 145 |
+
var hr = ws.getRow(6); ['STT','Tên sản phẩm','Mã SP','SL','Ghi chú'].forEach(function(h,i){
|
| 146 |
+
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'}};
|
| 147 |
+
});
|
| 148 |
+
|
| 149 |
+
var cr = 7, tq = 0;
|
| 150 |
+
(d.items||[]).forEach(function(it,i){
|
| 151 |
+
var r = ws.getRow(cr); r.height=20;
|
| 152 |
+
r.getCell(1).value=it.stt||i+1; r.getCell(2).value=it.name||''; r.getCell(3).value=it.model||''; r.getCell(4).value=it.qty||1; r.getCell(5).value=it.note||'';
|
| 153 |
+
tq += Number(it.qty||1); cr++;
|
| 154 |
+
});
|
| 155 |
+
|
| 156 |
+
ws.mergeCells(cr,1,cr,3); ws.getCell('A'+cr).value='TỔNG CỘNG SỐ LƯỢNG'; ws.getCell('D'+cr).value=tq;
|
| 157 |
+
|
| 158 |
+
var buf = await wb.xlsx.writeBuffer();
|
| 159 |
+
dlBlob(new Blob([buf],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}), 'GH-'+c+'.xlsx');
|
| 160 |
+
return true;
|
| 161 |
+
} catch(e) { console.error('[AutoPatch] exportDeliveryExcel error:',e); alert('❌ Lỗi: '+e.message); return false; }
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// ===== INIT =====
|
| 165 |
+
function init() {
|
| 166 |
+
// Check if VAI_EXCEL_FIX v2 already loaded
|
| 167 |
+
if (typeof window.VAI_EXCEL_FIX !== 'undefined' && typeof window.VAI_EXCEL_FIX.exportQuoteExcel === 'function') {
|
| 168 |
+
console.log('[AutoPatch v2.0] VAI_EXCEL_FIX v2 already loaded - done');
|
| 169 |
+
return;
|
| 170 |
}
|
| 171 |
|
| 172 |
+
// Load vai-excel-fix-v2.js if not exists
|
| 173 |
+
var s = document.createElement('script');
|
| 174 |
+
s.src = '/vai-excel-fix-v2.js?v=' + Date.now();
|
| 175 |
+
s.onload = function() {
|
| 176 |
+
console.log('[AutoPatch v2.0] Excel fix v2 loaded');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
};
|
| 178 |
+
s.onerror = function() {
|
| 179 |
+
console.warn('[AutoPatch v2.0] Failed to load v2, applying inline patch');
|
| 180 |
+
// Inline fallback patch
|
| 181 |
+
if (typeof window.exportDeliveryExcel === 'function') {
|
| 182 |
+
var origDE = window.exportDeliveryExcel;
|
| 183 |
+
window.exportDeliveryExcel = async function(order, opt) {
|
| 184 |
+
var btn = document.activeElement;
|
| 185 |
+
if (btn && btn.dataset.vaiExporting === '1') return;
|
| 186 |
+
if (btn) setBtn(btn, true);
|
| 187 |
+
try {
|
| 188 |
+
var d = { customer: { name: order?.customer||'', phone: order?.phone||'', addr: order?.addr||'', date: order?.date||'' },
|
| 189 |
+
items: (order?.items||[]).map(function(it,i){return{stt:i+1,name:it.name||'',model:it.model||'',qty:it.qty||1,note:it.note||'',specs:it.specs||'',price:it.price||0,discPrice:it.discPrice||it.price||0,total:it.total||0};}),
|
| 190 |
+
grandTotal: order?.grandTotal||0 };
|
| 191 |
+
var c = order?.code || genCode(order?.customer||'');
|
| 192 |
+
await exportDeliveryExcel(d, c);
|
| 193 |
+
} finally { if (btn) setBtn(btn, false); }
|
| 194 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
}
|
| 196 |
};
|
| 197 |
+
document.head.appendChild(s);
|
|
|
|
| 198 |
}
|
| 199 |
|
|
|
|
| 200 |
if (document.readyState === 'loading') {
|
| 201 |
+
document.addEventListener('DOMContentLoaded', init);
|
| 202 |
} else {
|
| 203 |
+
init();
|
| 204 |
}
|
| 205 |
+
setTimeout(init, 2000);
|
| 206 |
+
setTimeout(init, 5000);
|
|
|
|
| 207 |
|
| 208 |
})();
|