File size: 7,209 Bytes
bba996c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * V.AI STUDIO - Final GH Excel/PDF Fix Inject v1047
 * 
 * TỰ ĐỘNG INJECT script vào <head> mà không cần chỉnh sửa index.html
 * Chỉ cần thêm: <script src="final-gh-fix-inject.js"></script> vào index.html
 */
(function() {
    'use strict';

    function injectFix() {
        if (document.getElementById('vai-gh-fix-v1047')) return;
        
        var s = document.createElement('script');
        s.id = 'vai-gh-fix-v1047';
        s.src = 'auto-patch-gh-excel.js?v=1047';
        document.head.appendChild(s);
        console.log('[Inject] GH fix injected');
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', injectFix);
    } else {
        injectFix();
    }

    // Inline fallback after 3s
    setTimeout(function() {
        if (typeof window.VAI_QR === 'undefined' || !window.exportDeliveryExcel._patched) {
            var inlineFix = document.createElement('script');
            inlineFix.textContent = `(function(){
'use strict';

function orderToVAIData(order){
    if(!order||!order.items)return null;
    return{
        qd:{
            customer:{name:order.customer||'',phone:order.phone||'',email:order.email||'',addr:order.addr||'',date:order.date||''},
            items:(order.items||[]).map(function(it,i){
                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||''};
            }),
            grandTotal:order.grandTotal||0
        }
    };
}

function setBtnState(btn,exporting){
    if(!btn)return;
    if(exporting){
        btn.disabled=true;btn.dataset.vaiExporting='1';btn._origHtml=btn.innerHTML;btn.innerHTML='<span style="opacity:0.7">⏳</span>';
    }else{
        btn.disabled=false;btn.dataset.vaiExporting='0';if(btn._origHtml)btn.innerHTML=btn._origHtml;
    }
}

window.exportDeliveryExcel._patched=true;
window.exportDeliveryPDF._patched=true;

var origDE=window.exportDeliveryExcel;
var origDP=window.exportDeliveryPDF;

window.exportDeliveryExcel=async function(order,opt){
    var btn=document.activeElement;
    if(btn&&btn.dataset.vaiExporting==='1')return;
    if(btn)setBtnState(btn,true);
    try{
        if(order&&order.items&&order.items.length){
            var vaData=orderToVAIData(order);var code=order.code||'GH-'+Date.now();
            if(window.VAI_QR&&window.VAI_QR._doExportDeliveryExcel){
                await window.VAI_QR._doExportDeliveryExcel(vaData.qd,code);return;
            }
            if(window.ExcelJS){
                var wb=new ExcelJS.Workbook(),ws=wb.addWorksheet('Giao hàng');
                ws.views=[{showGridLines:false}];ws.columns=[{width:5},{width:34},{width:14},{width:8},{width:20}];
                ws.getCell('A1').value='PHIẾU GIAO HÀNG';
                ws.getCell('A2').value='KH: '+(vaData.qd.customer.name||'');
                ws.getCell('D2').value='Mã: '+code;
                ws.getCell('A3').value='SĐT: '+(vaData.qd.customer.phone||'');
                ws.getCell('A4').value='Địa chỉ: '+(vaData.qd.customer.addr||'');
                var hr=ws.getRow(6);['STT','Tên sản phẩm','Mã SP','SL','Ghi chú'].forEach(function(h,i){
                    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'}};
                });
                var cr=7;
                (vaData.qd.items||[]).forEach(function(it){
                    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++;
                });
                var buf=await wb.xlsx.writeBuffer();var blob=new Blob([buf],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                var url=URL.createObjectURL(blob);var a=document.createElement('a');a.href=url;a.download='GH-'+code+'.xlsx';a.click();
                setTimeout(function(){URL.revokeObjectURL(url);},300000);
                return;
            }
        }
        if(origDE)await origDE.call(this,order,opt);
    }catch(e){console.error('[Fix] Error:',e);if(typeof showToast==='function')showToast('❌ Xuất file thất bại');}
    finally{setTimeout(function(){if(btn)setBtnState(btn,false);},1000);}
};

window.exportDeliveryPDF=async function(order,opt){
    var btn=document.activeElement;
    if(btn&&btn.dataset.vaiExporting==='1')return;
    if(btn)setBtnState(btn,true);
    try{
        if(order&&order.items&&order.items.length){
            var vaData=orderToVAIData(order);var code=order.code||'GH-'+Date.now();
            if(window.VAI_QR&&window.VAI_QR._doExportDeliveryPDF){
                await window.VAI_QR._doExportDeliveryPDF(vaData.qd,code);return;
            }
            if(window.html2canvas&&window.jspdf){
                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>';
                (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>';});
                html+='</tbody></table></div>';
                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);
                await new Promise(r=>setTimeout(r,300));
                var canvas=await html2canvas(div,{scale:2,useCORS:true,backgroundColor:'#fff'});
                var imgData=canvas.toDataURL('image/png');
                var jsPDF=window.jspdf.jsPDF;
                var pdf=new jsPDF({orientation:'p',unit:'pt',format:'a4'});
                var margin=25,usableW=595-margin*2,ratio=canvas.height/canvas.width,imgH=usableW*ratio;
                pdf.addImage(imgData,'PNG',margin,margin,usableW,imgH>842-margin*2?842-margin*2:imgH);
                pdf.save('GH-'+code+'.pdf');div.remove();return;
            }
        }
        if(origDP)await origDP.call(this,order,opt);
    }catch(e){console.error('[Fix] Error:',e);if(typeof showToast==='function')showToast('❌ Xuất file thất bại');}
    finally{setTimeout(function(){if(btn)setBtnState(btn,false);},1000);}
};

console.log('[Fix v1047] GH export ready');
})();`;
            document.head.appendChild(inlineFix);
        }
    }, 3000);

})();