/** * V.AI STUDIO - Final GH Excel/PDF Fix Inject v1047 * * TỰ ĐỘNG INJECT script vào
mà không cần chỉnh sửa index.html * Chỉ cần thêm: 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='⏳'; }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='| STT | Tên | Mã | SL | Ghi chú |
|---|---|---|---|---|
| '+it.stt+' | '+it.name+' | '+it.model+' | '+it.qty+' | '+it.note+' |