bep40 commited on
Commit
bba996c
·
verified ·
1 Parent(s): 9186b1d

Upload final-gh-fix-inject.js

Browse files
Files changed (1) hide show
  1. final-gh-fix-inject.js +133 -0
final-gh-fix-inject.js ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * V.AI STUDIO - Final GH Excel/PDF Fix Inject v1047
3
+ *
4
+ * TỰ ĐỘNG INJECT script vào <head> mà không cần chỉnh sửa index.html
5
+ * Chỉ cần thêm: <script src="final-gh-fix-inject.js"></script> vào index.html
6
+ */
7
+ (function() {
8
+ 'use strict';
9
+
10
+ function injectFix() {
11
+ if (document.getElementById('vai-gh-fix-v1047')) return;
12
+
13
+ var s = document.createElement('script');
14
+ s.id = 'vai-gh-fix-v1047';
15
+ s.src = 'auto-patch-gh-excel.js?v=1047';
16
+ document.head.appendChild(s);
17
+ console.log('[Inject] GH fix injected');
18
+ }
19
+
20
+ if (document.readyState === 'loading') {
21
+ document.addEventListener('DOMContentLoaded', injectFix);
22
+ } else {
23
+ injectFix();
24
+ }
25
+
26
+ // Inline fallback after 3s
27
+ setTimeout(function() {
28
+ if (typeof window.VAI_QR === 'undefined' || !window.exportDeliveryExcel._patched) {
29
+ var inlineFix = document.createElement('script');
30
+ inlineFix.textContent = `(function(){
31
+ 'use strict';
32
+
33
+ function orderToVAIData(order){
34
+ if(!order||!order.items)return null;
35
+ return{
36
+ qd:{
37
+ customer:{name:order.customer||'',phone:order.phone||'',email:order.email||'',addr:order.addr||'',date:order.date||''},
38
+ items:(order.items||[]).map(function(it,i){
39
+ return{stt:i+1,image:it.image||'',name:it.name||'',model:it.model||'',specs:it.specs||it.info||'',qty:it.qty||1,price:it.price||it.listPrice||it.discPrice||0,discPrice:it.discPrice||it.price||0,total:it.total||0,note:it.note||''};
40
+ }),
41
+ grandTotal:order.grandTotal||0
42
+ }
43
+ };
44
+ }
45
+
46
+ function setBtnState(btn,exporting){
47
+ if(!btn)return;
48
+ if(exporting){
49
+ btn.disabled=true;btn.dataset.vaiExporting='1';btn._origHtml=btn.innerHTML;btn.innerHTML='<span style="opacity:0.7">⏳</span>';
50
+ }else{
51
+ btn.disabled=false;btn.dataset.vaiExporting='0';if(btn._origHtml)btn.innerHTML=btn._origHtml;
52
+ }
53
+ }
54
+
55
+ window.exportDeliveryExcel._patched=true;
56
+ window.exportDeliveryPDF._patched=true;
57
+
58
+ var origDE=window.exportDeliveryExcel;
59
+ var origDP=window.exportDeliveryPDF;
60
+
61
+ window.exportDeliveryExcel=async function(order,opt){
62
+ var btn=document.activeElement;
63
+ if(btn&&btn.dataset.vaiExporting==='1')return;
64
+ if(btn)setBtnState(btn,true);
65
+ try{
66
+ if(order&&order.items&&order.items.length){
67
+ var vaData=orderToVAIData(order);var code=order.code||'GH-'+Date.now();
68
+ if(window.VAI_QR&&window.VAI_QR._doExportDeliveryExcel){
69
+ await window.VAI_QR._doExportDeliveryExcel(vaData.qd,code);return;
70
+ }
71
+ if(window.ExcelJS){
72
+ var wb=new ExcelJS.Workbook(),ws=wb.addWorksheet('Giao hàng');
73
+ ws.views=[{showGridLines:false}];ws.columns=[{width:5},{width:34},{width:14},{width:8},{width:20}];
74
+ ws.getCell('A1').value='PHIẾU GIAO HÀNG';
75
+ ws.getCell('A2').value='KH: '+(vaData.qd.customer.name||'');
76
+ ws.getCell('D2').value='Mã: '+code;
77
+ ws.getCell('A3').value='SĐT: '+(vaData.qd.customer.phone||'');
78
+ ws.getCell('A4').value='Địa chỉ: '+(vaData.qd.customer.addr||'');
79
+ var hr=ws.getRow(6);['STT','Tên sản phẩm','Mã SP','SL','Ghi chú'].forEach(function(h,i){
80
+ var c=hr.getCell(i+1);c.value=h;c.fill={type:'pattern',pattern:'solid',fgColor:{argb:'FF003F62'}};c.font={bold:true,color:{argb:'FFFFFFFF'}};
81
+ });
82
+ var cr=7;
83
+ (vaData.qd.items||[]).forEach(function(it){
84
+ var row=ws.getRow(cr);row.getCell(1).value=it.stt;row.getCell(2).value=it.name;row.getCell(3).value=it.model;row.getCell(4).value=it.qty;row.getCell(5).value=it.note;cr++;
85
+ });
86
+ var buf=await wb.xlsx.writeBuffer();var blob=new Blob([buf],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
87
+ var url=URL.createObjectURL(blob);var a=document.createElement('a');a.href=url;a.download='GH-'+code+'.xlsx';a.click();
88
+ setTimeout(function(){URL.revokeObjectURL(url);},300000);
89
+ return;
90
+ }
91
+ }
92
+ if(origDE)await origDE.call(this,order,opt);
93
+ }catch(e){console.error('[Fix] Error:',e);if(typeof showToast==='function')showToast('❌ Xuất file thất bại');}
94
+ finally{setTimeout(function(){if(btn)setBtnState(btn,false);},1000);}
95
+ };
96
+
97
+ window.exportDeliveryPDF=async function(order,opt){
98
+ var btn=document.activeElement;
99
+ if(btn&&btn.dataset.vaiExporting==='1')return;
100
+ if(btn)setBtnState(btn,true);
101
+ try{
102
+ if(order&&order.items&&order.items.length){
103
+ var vaData=orderToVAIData(order);var code=order.code||'GH-'+Date.now();
104
+ if(window.VAI_QR&&window.VAI_QR._doExportDeliveryPDF){
105
+ await window.VAI_QR._doExportDeliveryPDF(vaData.qd,code);return;
106
+ }
107
+ if(window.html2canvas&&window.jspdf){
108
+ var html='<div style="padding:30px;font-family:Inter,Arial,sans-serif"><div style="font-size:20px;font-weight:800;color:#003f62;margin-bottom:10px">PHIẾU GIAO HÀNG</div><div style="margin-bottom:10px"><b>KH:</b> '+(vaData.qd.customer.name||'')+' | <b>SĐT:</b> '+(vaData.qd.customer.phone||'')+'<br><b>Mã:</b> '+code+'</div><table style="width:100%;border-collapse:collapse"><thead><tr style="background:#003f62;color:#fff"><th style="padding:6px">STT</th><th style="padding:6px">Tên</th><th style="padding:6px">Mã</th><th style="padding:6px">SL</th><th style="padding:6px">Ghi chú</th></tr></thead><tbody>';
109
+ (vaData.qd.items||[]).forEach(function(it){html+='<tr><td style="padding:6px;text-align:center">'+it.stt+'</td><td style="padding:6px">'+it.name+'</td><td style="padding:6px">'+it.model+'</td><td style="padding:6px;text-align:center">'+it.qty+'</td><td style="padding:6px">'+it.note+'</td></tr>';});
110
+ html+='</tbody></table></div>';
111
+ var div=document.createElement('div');div.style.cssText='position:absolute;left:-9999px;top:0;width:700px;background:#fff;padding:28px';div.innerHTML=html;document.body.appendChild(div);
112
+ await new Promise(r=>setTimeout(r,300));
113
+ var canvas=await html2canvas(div,{scale:2,useCORS:true,backgroundColor:'#fff'});
114
+ var imgData=canvas.toDataURL('image/png');
115
+ var jsPDF=window.jspdf.jsPDF;
116
+ var pdf=new jsPDF({orientation:'p',unit:'pt',format:'a4'});
117
+ var margin=25,usableW=595-margin*2,ratio=canvas.height/canvas.width,imgH=usableW*ratio;
118
+ pdf.addImage(imgData,'PNG',margin,margin,usableW,imgH>842-margin*2?842-margin*2:imgH);
119
+ pdf.save('GH-'+code+'.pdf');div.remove();return;
120
+ }
121
+ }
122
+ if(origDP)await origDP.call(this,order,opt);
123
+ }catch(e){console.error('[Fix] Error:',e);if(typeof showToast==='function')showToast('❌ Xuất file thất bại');}
124
+ finally{setTimeout(function(){if(btn)setBtnState(btn,false);},1000);}
125
+ };
126
+
127
+ console.log('[Fix v1047] GH export ready');
128
+ })();`;
129
+ document.head.appendChild(inlineFix);
130
+ }
131
+ }, 3000);
132
+
133
+ })();