Upload qr-payment.js
Browse files- qr-payment.js +182 -325
qr-payment.js
CHANGED
|
@@ -1,346 +1,203 @@
|
|
| 1 |
-
/*
|
| 2 |
-
*
|
| 3 |
-
*
|
| 4 |
-
* -
|
| 5 |
-
* -
|
|
|
|
| 6 |
*/
|
| 7 |
(function(){
|
| 8 |
'use strict';
|
| 9 |
var BANK_ID='vib',BANK_ACCOUNT='918258385',BANK_NAME='TRAN QUOC VUONG',BANK_NAME_VN='Trần Quốc Vương';
|
| 10 |
-
function stripDiacritics(s){return s.normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[Đđ]/g,'D');}
|
| 11 |
-
function generateOrderCode(name){var now=new Date(),dd=String(now.getDate()).padStart(2,'0'),mm=String(now.getMonth()+1).padStart(2,'0'),yy=String(now.getFullYear()).slice(-2),ini='';if(name&&name.trim()){name.trim().split(/\s+/).forEach(function(w){if(w){var ch=stripDiacritics(w.charAt(0)).toUpperCase();if(/[A-Z]/.test(ch))ini+=ch;}});}return 'VAS'+(ini||'X')+dd+mm+yy;}
|
| 12 |
-
function getCustomerName(){var sels=['#quoteName','#quoteCustomerName','#checkoutName','.quote-field input:first-of-type'];for(var i=0;i<sels.length;i++){var el=document.querySelector(sels[i]);if(el&&el.value&&el.value.trim().length>1)return el.value.trim();}return '';}
|
| 13 |
-
function getEffectiveOrderCode(){var n=getCustomerName();if(n)return generateOrderCode(n);var h=window.location.hash;if(h&&h.length>1){var c=h.substring(1);if(/^VAS[A-Z0-9]{3,}$/.test(c))return c;}return generateOrderCode('');}
|
| 14 |
-
function getQRUrl(a,c){return 'https://img.vietqr.io/image/'+BANK_ID+'-'+BANK_ACCOUNT+'-compact2.png?amount='+(Math.round(a)||0)+'&addInfo='+encodeURIComponent(c||'VAISTUDIO')+'&accountName='+encodeURIComponent(BANK_NAME);}
|
| 15 |
-
function fmt(n){if(!n||isNaN(n))return '0đ';return Number(n).toLocaleString('vi-VN')+'đ';}
|
| 16 |
-
function vaiIsDimensionKey(k,v){
|
| 17 |
-
var txt=stripDiacritics(String(k||'')+' '+String(v||'')).toLowerCase();
|
| 18 |
-
return /kich thuoc|dimension|size|rong|ngang|dai|sau|cao|height|width|depth|cut.?out|cat da|lo da|lap noi|lap am|am ban|lot long|phu bi|khoang tu|mat canh|quy cach|r\s*[x=]|w\s*[x=]|d\s*[x=]|h\s*[x=]/.test(txt);
|
| 19 |
-
}
|
| 20 |
-
function vaiDimensionSummary(specs,limit){
|
| 21 |
-
limit=limit||900;var parts=[],seen={};
|
| 22 |
-
function add(k,v){if(!k||v==null||v==='')return;var t=String(k)+': '+String(v);var key=compactKey(t);if(seen[key])return;seen[key]=1;parts.push(t);}
|
| 23 |
-
if(specs&&typeof specs==='object'){
|
| 24 |
-
var entries=Object.entries(specs);entries.filter(function(e){return vaiIsDimensionKey(e[0],e[1]);}).forEach(function(e){add(e[0],e[1]);});
|
| 25 |
-
entries.filter(function(e){return !vaiIsDimensionKey(e[0],e[1]);}).slice(0,8).forEach(function(e){add(e[0],e[1]);});
|
| 26 |
-
}else if(specs){String(specs).split(/[;\n]+/).filter(Boolean).sort(function(a,b){return (vaiIsDimensionKey(b,'')?1:0)-(vaiIsDimensionKey(a,'')?1:0);}).forEach(function(x){var key=compactKey(x);if(!seen[key]){seen[key]=1;parts.push(x.trim());}});}
|
| 27 |
-
return parts.join('; ').slice(0,limit);
|
| 28 |
-
}
|
| 29 |
-
function vaiProductInfoForQuote(product){
|
| 30 |
-
if(!product)return '';
|
| 31 |
-
return vaiDimensionSummary(product.specs||{},900)||String(product.summary||product.desc||'').slice(0,900);
|
| 32 |
-
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
var m;
|
| 47 |
-
while((m=re1.exec(lo))!==null){var v=parseFloat(m[2].replace(/\./g,'').replace(',','.'));if(v>0&&v<=100)map[compactKey(m[1])]=v;}
|
| 48 |
-
while((m=re2.exec(lo))!==null){var v2=parseFloat(m[2].replace(/\./g,'').replace(',','.'));if(v2>0&&v<=100)map[compactKey(m[1])]=v2;}
|
| 49 |
-
}
|
| 50 |
-
function applyPromptDiscounts(qd,parsed){
|
| 51 |
-
if(!qd||!qd.items)return qd;var productTotal=0;var itemDiscounts=(parsed&&parsed.itemDiscounts)||{};var globalPct=Number((parsed&&parsed.discountPercent)||0);
|
| 52 |
-
qd.items.forEach(function(it){var base=Number(it.price||0),qty=Number(it.qty||1);var pct=matchItemDiscount(it,itemDiscounts);if(pct>0){it.discPrice=Math.round(base*(1-pct/100));it.discountPercent=pct;}else if(globalPct>0&&globalPct<=100){it.discPrice=Math.round(base*(1-globalPct/100));it.discountPercent=globalPct;}else if(!it.discPrice){it.discPrice=base;}it.total=Number(it.discPrice||base)*qty;productTotal+=Number(it.total||0);});
|
| 53 |
-
qd.grandTotal=productTotal;return qd;
|
| 54 |
}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if(
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if(Object.keys(itemDiscounts).length&&line.match(/\b[A-Za-z]{1,8}[A-Za-z0-9._\-\/]{1,30}\d[A-Za-z0-9._\-\/]*\b/i)&&line.match(/\b(ck|chiết khấu|chiet khau|giảm|giam|discount)\b/i))return;
|
| 63 |
-
if(line.match(/^(ck|chiết khấu|chiet khau|giảm|giam|discount)/i)){
|
| 64 |
-
discount=line;
|
| 65 |
-
var ckm=line.match(/([\d.,]+)\s*(%)?/);
|
| 66 |
-
if(ckm){var v=parseFloat(ckm[1].replace(/\./g,'').replace(',','.'));if(v>0&&v<=100)discountPercent=v;}
|
| 67 |
-
return;
|
| 68 |
-
}
|
| 69 |
-
if(line.match(/cọc|coc|deposit/i)){
|
| 70 |
-
var dm=line.match(/([\d.,]+)\s*(k|K|nghìn|nghin|triệu|tr|đ|d|vnd)?/);
|
| 71 |
-
if(dm){var da=parseFloat(dm[1].replace(/\./g,'').replace(',','.'));var du=(dm[2]||'').toLowerCase();if(du==='k'||du==='nghìn'||du==='nghin')da*=1000;if(du==='triệu'||du==='tr')da*=1000000;deposit=da;}
|
| 72 |
-
return;
|
| 73 |
-
}
|
| 74 |
-
var m=line.match(/([\d.,]+)\s*(k|K|nghìn|nghin|triệu|tr|đ|d|vnd)?/);
|
| 75 |
-
if(m){var amt=parseFloat(m[1].replace(/\./g,'').replace(',','.'));var u=(m[2]||'').toLowerCase();if(u==='k'||u==='nghìn'||u==='nghin')amt*=1000;if(u==='triệu'||u==='tr')amt*=1000000;if(amt<=0)return;var lb=line.replace(m[0],'').trim();if(lb.match(/giao|ship|vận chuyển|delivery/i))lb='Phí giao hàng';else if(lb.match(/lắp|lap|install/i))lb='Phí lắp đặt';else if(lb.match(/bốc|boc|xếp|xep|vác/i))lb='Phí bốc xếp';else if(!lb||lb.length<2)lb='Phụ phí';fees.push({label:lb,amount:amt});}
|
| 76 |
-
else{notes.push(line);}
|
| 77 |
-
});
|
| 78 |
-
return{fees:fees,notes:notes,discount:discount,deposit:deposit,discountPercent:discountPercent,itemDiscounts:itemDiscounts};
|
| 79 |
-
}
|
| 80 |
-
var _lastPromptText='';
|
| 81 |
-
function getPromptText(){
|
| 82 |
-
// 1. Direct lookup by known IDs
|
| 83 |
-
var direct=document.getElementById('qcAiPrompt');
|
| 84 |
-
if(direct){var dv=(direct.value||'').trim();if(dv&&dv.toUpperCase().replace(/[\s.]/g,'')!=='VAISTUDIO'){_lastPromptText=dv;return dv;}}
|
| 85 |
-
// 2. Fallback: placeholder-based search
|
| 86 |
-
var inputs=document.querySelectorAll('input,textarea');
|
| 87 |
-
for(var j=0;j<inputs.length;j++){var ph=(inputs[j].placeholder||'').toLowerCase();if(ph.includes('yêu cầu')||ph.includes('chiết khấu')||ph.includes('yeu cau')||ph.includes('chiet khau')||ph.includes('ck')||ph.includes('giao hàng')){var val=(inputs[j].value||'').trim();if(!val||val.toUpperCase().replace(/[\s.]/g,'')==='VAISTUDIO')continue;_lastPromptText=val;return val;}}
|
| 88 |
-
return _lastPromptText;
|
| 89 |
-
}
|
| 90 |
-
function getData(){
|
| 91 |
-
var qd=window._origGetQuoteData?window._origGetQuoteData():getQuoteData();
|
| 92 |
-
var parsed=parsePrompt(getPromptText());
|
| 93 |
-
qd=applyPromptDiscounts(qd,parsed);
|
| 94 |
-
if(qd&&qd.items){qd.items.forEach(function(it){if(!it.specs||String(it.specs).length<40){var idx=(it.idx!=null?it.idx:(it.productIdx!=null?it.productIdx:null));var p=(idx!=null&&window.D)?window.D[idx]:null;if(!p&&window.D){var ck=compactKey(it.model||it.sku||it.ma||it.name||'');p=(window.D||[]).find(function(x){return compactKey(x.model||x.sku||x.name||'').indexOf(ck)>=0||ck.indexOf(compactKey(x.model||x.sku||''))>=0;});}it.specs=vaiProductInfoForQuote(p)||it.specs||'';}});}
|
| 95 |
-
var surcharge=0;parsed.fees.forEach(function(f){surcharge+=f.amount;});
|
| 96 |
-
var grandTotal=(qd.grandTotal||0)+surcharge;
|
| 97 |
-
var remaining=grandTotal-parsed.deposit;
|
| 98 |
-
return{qd:qd,fees:parsed.fees,notes:parsed.notes,discount:parsed.discount,discountPercent:parsed.discountPercent,itemDiscounts:parsed.itemDiscounts||{},deposit:parsed.deposit,productTotal:qd.grandTotal||0,grandTotal:grandTotal,remaining:remaining>0?remaining:0};
|
| 99 |
}
|
| 100 |
|
| 101 |
-
//
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
var remaining=grandTotal-parsed.deposit;
|
| 113 |
-
var sec=document.createElement('div');sec.className='vai-extra-section';
|
| 114 |
-
sec.style.cssText='margin:10px 0;padding:0 16px;font-size:13px';
|
| 115 |
-
var html='';
|
| 116 |
-
if(parsed.fees.length){
|
| 117 |
-
html+='<div style="padding:8px 12px;background:#fffbeb;border-radius:6px;border:1px solid #fde68a;margin-bottom:8px"><div style="font-weight:700;color:#92400e;font-size:12px;margin-bottom:4px">PHỤ PHÍ</div>';
|
| 118 |
-
parsed.fees.forEach(function(f){html+='<div style="display:flex;justify-content:space-between;padding:2px 0"><span>'+f.label+'</span><span style="font-weight:700">'+fmt(f.amount)+'</span></div>';});
|
| 119 |
-
html+='</div>';
|
| 120 |
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
}
|
| 128 |
-
sec.innerHTML=html;
|
| 129 |
-
var actions=modal.querySelector('.quote-actions,[class*="actions"],[class*="button"]');
|
| 130 |
-
if(actions&&actions.parentNode){actions.parentNode.insertBefore(sec,actions);}else{modal.appendChild(sec);}
|
| 131 |
-
}
|
| 132 |
-
var _obs=new MutationObserver(function(){setTimeout(injectToModal,300);});
|
| 133 |
-
_obs.observe(document.body,{attributes:true,childList:true,subtree:true});
|
| 134 |
-
setInterval(injectToModal,1500);
|
| 135 |
-
document.addEventListener('input',function(e){var ph=(e.target.placeholder||'').toLowerCase();var id=e.target.id||'';if(ph.includes('yêu cầu')||ph.includes('chiết khấu')||ph.includes('yeu cau')||ph.includes('chiet khau')||ph.includes('ck')||ph.includes('giao hàng')||id==='qcAiPrompt'){var val=(e.target.value||'').trim();if(val&&val.toUpperCase().replace(/[\s.]/g,'')!=='VAISTUDIO')_lastPromptText=val;setTimeout(injectToModal,200);}},true);
|
| 136 |
-
|
| 137 |
-
// === HTML BÁO GIÁ (PDF/Image) — NO CK% label ===
|
| 138 |
-
function buildQuoteHTML(d,code,qrUrl){
|
| 139 |
-
var qd=d.qd;
|
| 140 |
-
var rows=qd.items.map(function(it){return '<tr style="border-bottom:1px solid #e2e8f0"><td style="padding:6px 4px;text-align:center;font-size:11px">'+(it.stt||'')+'</td><td style="padding:6px 4px"><img src="'+(it.image||'')+'" style="width:100px;height:100px;object-fit:contain;border-radius:4px" crossorigin="anonymous" onerror="this.style.display=\'none\'"></td><td style="padding:6px 4px;font-size:11px;font-weight:600">'+(it.name||'')+'</td><td style="padding:6px 4px;text-align:center;font-size:10px">'+(it.model||'')+'</td><td style="padding:6px 4px;font-size:9px;color:#64748b">'+(it.specs||'')+'</td><td style="padding:6px 4px;text-align:center">'+(it.qty||'')+'</td><td style="padding:6px 4px;text-align:right;font-size:11px">'+(it.price?fmt(it.price):'')+'</td><td style="padding:6px 4px;text-align:right;font-size:11px;'+(it.discPrice&&it.discPrice<it.price?'color:#dc3545;font-weight:700':'')+'">'+(it.discPrice?fmt(it.discPrice):'')+'</td><td style="padding:6px 4px;text-align:right;font-size:11px;font-weight:700">'+(it.total?fmt(it.total):'')+'</td><td style="padding:6px 4px;font-size:9px">'+(it.note||'')+'</td></tr>';}).join('');
|
| 141 |
-
var feeRows='';if(d.fees&&d.fees.length){d.fees.forEach(function(f){feeRows+='<tr style="background:#fffbeb"><td colspan="8" style="padding:5px 10px;font-size:11px;color:#92400e;border-bottom:1px solid #fde68a">⊕ '+f.label+'</td><td style="padding:5px 4px;text-align:right;font-size:11px;font-weight:700;color:#92400e;border-bottom:1px solid #fde68a">'+fmt(f.amount)+'</td><td style="border-bottom:1px solid #fde68a"></td></tr>';});}
|
| 142 |
-
var depositHtml='';if(d.deposit>0){depositHtml='<div style="margin-top:8px;padding:8px 14px;background:#f0fdf4;border-radius:6px;border:1px solid #86efac;font-size:12px"><div style="display:flex;justify-content:space-between"><span>Đã cọc</span><span style="font-weight:700;color:#166534">'+fmt(d.deposit)+'</span></div><div style="display:flex;justify-content:space-between;margin-top:3px"><span style="font-weight:800">Còn lại</span><span style="font-weight:900;color:#dc2626;font-size:14px">'+fmt(d.remaining)+'</span></div></div>';}
|
| 143 |
-
var notesHtml='';if(d.notes&&d.notes.length){notesHtml='<div style="margin-top:8px;padding:6px 12px;font-size:10px;color:#166534;background:#f0fdf4;border-radius:6px"><b>📝 Ghi chú:</b> '+d.notes.join('; ')+'</div>';}
|
| 144 |
-
var totalQty=(qd.items||[]).reduce(function(s,it){return s+Number(it.qty||1);},0);
|
| 145 |
-
return '<div style="display:flex;align-items:center;gap:10px;margin-bottom:4px"><img src="https://huggingface.co/spaces/bep40/V.AISTUDIO/resolve/main/logo/logo_200.png" style="width:40px;height:40px;object-fit:contain" crossorigin="anonymous"><div style="font-size:9px;color:#64748b;font-style:italic">❝ Niềm tin khách hàng là tài sản của chúng tôi ❞</div></div><div style="text-align:center;margin-bottom:12px"><div style="font-size:20px;font-weight:900;color:#db9815">BẢNG BÁO GIÁ</div></div><div style="display:flex;justify-content:space-between;margin-bottom:12px;font-size:11px"><div><b>Khách hàng:</b> '+(qd.customer.name||'')+'<br><b>SĐT:</b> '+(qd.customer.phone||'')+'<br><b>Email:</b> '+(qd.customer.email||'')+'<br><b>Địa chỉ:</b> '+(qd.customer.addr||'')+'</div><div style="text-align:right"><b>Mã đơn:</b> '+code+'<br><b>Ngày:</b> '+(qd.customer.date||'')+'</div></div><table style="width:100%;border-collapse:collapse"><thead><tr style="background:#003f62;color:#fff"><th style="padding:7px 3px;font-size:9px">STT</th><th style="padding:7px 3px;font-size:9px">Hình</th><th style="padding:7px 3px;font-size:9px;text-align:left">Tên SP</th><th style="padding:7px 3px;font-size:9px">Mã</th><th style="padding:7px 3px;font-size:9px">Thông tin</th><th style="padding:7px 3px;font-size:9px">SL</th><th style="padding:7px 3px;font-size:9px">Đơn giá</th><th style="padding:7px 3px;font-size:9px">Giá CK</th><th style="padding:7px 3px;font-size:9px">Thành tiền</th><th style="padding:7px 3px;font-size:9px">Ghi chú</th></tr></thead><tbody>'+rows+feeRows+'</tbody></table><div style="margin-top:0;padding:10px 14px;background:#003f62;display:grid;grid-template-columns:1fr 90px 180px;gap:10px;align-items:center"><span style="color:#fff;font-size:13px;font-weight:800">TỔNG CỘNG</span><span style="color:#fff;font-size:13px;font-weight:900;text-align:center">SL: '+totalQty+'</span><span style="color:#f0b840;font-size:17px;font-weight:900;text-align:right">'+fmt(d.grandTotal)+'</span></div>'+depositHtml+notesHtml+'<div style="display:flex;align-items:flex-start;gap:14px;margin-top:14px;padding:12px;background:#f8fafc;border-radius:8px"><img src="'+qrUrl+'" style="width:115px;height:115px;border-radius:6px;border:2px solid #e2e8f0" crossorigin="anonymous"><div style="font-size:10px;line-height:1.7"><div style="font-weight:700;color:#003f62;margin-bottom:3px">💳 CHUYỂN KHOẢN</div>🏦 VIB | 👤 '+BANK_NAME_VN+'<br>🔢 STK: <b>'+BANK_ACCOUNT+'</b><br>💰 <b>'+fmt(d.deposit>0?d.remaining:d.grandTotal)+'</b> | 📝 <b>'+code+'</b></div></div>';
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
function buildDeliveryHTML(qd,code){
|
| 149 |
-
var rows=(qd.items||[]).map(function(it,i){return '<tr style="border-bottom:1px solid #e2e8f0"><td style="padding:8px 4px;text-align:center;font-size:12px">'+(it.stt||(i+1))+'</td><td style="padding:8px 4px;font-weight:600;font-size:12px">'+(it.name||'')+'</td><td style="padding:8px 4px;text-align:center;font-size:11px">'+(it.model||'')+'</td><td style="padding:8px 4px;text-align:center;font-weight:700;font-size:13px">'+(it.qty||1)+'</td><td style="padding:8px 4px;font-size:11px">'+(it.note||'')+'</td></tr>';}).join('');
|
| 150 |
-
return '<div style="text-align:center;margin-bottom:14px"><div style="font-size:20px;font-weight:900;color:#003f62">PHIẾU GIAO HÀNG</div></div><div style="margin-bottom:12px;font-size:11px;line-height:1.6"><b>Khách hàng:</b> '+(qd.customer?qd.customer.name:'')+' | <b>SĐT:</b> '+(qd.customer?qd.customer.phone:'')+'<br><b>Địa chỉ:</b> '+(qd.customer?qd.customer.addr||'':'')+'<br><b>Mã đơn:</b> '+code+' | <b>Ngày:</b> '+(qd.customer?qd.customer.date||'':'')+'</div><table style="width:100%;border-collapse:collapse;font-size:12px"><thead><tr style="background:#003f62;color:#fff"><th style="padding:8px 4px;width:35px">STT</th><th style="padding:8px 4px;text-align:left">Tên sản phẩm</th><th style="padding:8px 4px;width:90px">Mã SP</th><th style="padding:8px 4px;width:40px">SL</th><th style="padding:8px 4px;text-align:left;width:120px">Ghi chú</th></tr></thead><tbody>'+rows+'</tbody></table><div style="margin-top:60px;display:flex;justify-content:space-around;font-size:12px"><div style="text-align:center"><b>Bên giao</b><br><span style="font-size:9px;color:#64748b">(Ký, họ tên)</span><div style="margin-top:45px">_______________</div></div><div style="text-align:center"><b>Bên nhận</b><br><span style="font-size:9px;color:#64748b">(Ký, họ tên)</span><div style="margin-top:45px">_______________</div></div></div>';
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
async function toCanvas(html,w){
|
| 154 |
-
var div=document.createElement('div');
|
| 155 |
-
div.style.cssText='position:absolute;left:-10000px;top:0;width:'+(w||1000)+'px;background:#fff;padding:28px;font-family:Inter,Arial,DejaVu Sans,sans-serif;z-index:-1;box-sizing:border-box';
|
| 156 |
-
div.innerHTML=html;document.body.appendChild(div);
|
| 157 |
-
try{if(document.fonts&&document.fonts.ready)await document.fonts.ready;}catch(e){}
|
| 158 |
-
var imgs=Array.from(div.querySelectorAll('img'));
|
| 159 |
-
await Promise.all(imgs.map(function(img){return new Promise(function(res){if(img.complete)return res();img.onload=img.onerror=function(){res();};setTimeout(res,2500);});}));
|
| 160 |
-
await new Promise(function(r){setTimeout(r,500);});
|
| 161 |
-
var h=Math.max(div.scrollHeight,div.offsetHeight);
|
| 162 |
-
var cv=await html2canvas(div,{scale:2,useCORS:true,allowTaint:false,backgroundColor:'#fff',width:w||1000,height:h,windowWidth:w||1000,windowHeight:h,scrollX:0,scrollY:0});
|
| 163 |
-
div.remove();return cv;
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
function doOverride(){
|
| 167 |
-
if(typeof getQuoteData!=='function'||typeof html2canvas!=='function')return false;
|
| 168 |
-
window._origGetQuoteData=getQuoteData;
|
| 169 |
-
window.shareQuoteImage=async function(){var d=getData(),code=getEffectiveOrderCode(),qr=getQRUrl(d.deposit>0?d.remaining:d.grandTotal,code);try{var cv=await toCanvas(buildQuoteHTML(d,code,qr),1000);var blob=await new Promise(function(r){cv.toBlob(r,'image/png');});var f=new File([blob],code+'.png',{type:'image/png'});if(navigator.share&&navigator.canShare&&navigator.canShare({files:[f]})){await navigator.share({title:code,files:[f]});}else{var url=URL.createObjectURL(blob);var p=document.createElement('div');p.style.cssText='position:fixed;inset:0;background:rgba(0,0,0,.7);z-index:999;display:flex;align-items:center;justify-content:center;padding:16px;backdrop-filter:blur(4px)';p.innerHTML='<div style="background:#fff;border-radius:16px;max-width:90vw;max-height:85vh;overflow:auto;padding:16px"><img src="'+url+'" style="max-width:100%;border-radius:8px"><div style="display:flex;gap:8px;margin-top:12px;justify-content:center"><a href="'+url+'" download="'+code+'.png" style="padding:10px 20px;background:#003f62;color:#fff;border-radius:8px;font-weight:700;text-decoration:none">⬇ Tải ảnh</a><button onclick="this.closest(\'div[style*=fixed]\').remove()" style="padding:10px 20px;background:#e2e8f0;border:none;border-radius:8px;font-weight:700;cursor:pointer">Đóng</button></div></div>';document.body.appendChild(p);}}catch(e){console.error(e);}};
|
| 170 |
-
window.exportPDF=async function(){var d=getData(),code=getEffectiveOrderCode(),qr=getQRUrl(d.deposit>0?d.remaining:d.grandTotal,code);try{await _doExportPDF(d,code);}catch(e){console.error(e);alert('Không xuất được PDF: '+(e&&e.message?e.message:e));}};
|
| 171 |
-
window.exportExcel=async function(){if(typeof ExcelJS==='undefined')return;var d=getData(),qd=d.qd,code=getEffectiveOrderCode(),qr=getQRUrl(d.deposit>0?d.remaining:d.grandTotal,code);await _doExportExcel(d,qd,code,qr);};
|
| 172 |
-
window.exportDeliveryExcel=async function(){if(typeof ExcelJS==='undefined')return;var qd=window._origGetQuoteData(),code=getEffectiveOrderCode();await _doExportDeliveryExcel(qd,code);};
|
| 173 |
-
window.exportDeliveryPDF=async function(){var qd=window._origGetQuoteData(),code=getEffectiveOrderCode();await _doExportDeliveryPDF(qd,code);};
|
| 174 |
-
window.getQuoteData=function(){var qd=window._origGetQuoteData.apply(this,arguments);if(qd.customer)qd.customer.orderCode=getEffectiveOrderCode();return qd;};
|
| 175 |
-
console.log('✅ QR Payment v1101 — 100% hình ảnh Excel');return true;
|
| 176 |
}
|
| 177 |
|
| 178 |
-
async function
|
| 179 |
-
var
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
var imgW=pageW;var imgH=cv.height*imgW/cv.width;
|
| 184 |
-
var pdf=new jspdf.jsPDF({orientation:'p',unit:'pt',format:'a4'});
|
| 185 |
-
if(imgH<=pageH){pdf.addImage(img,'JPEG',0,0,imgW,imgH);}else{
|
| 186 |
-
var y=0;var remaining=imgH;var page=0;
|
| 187 |
-
while(remaining>0){if(page>0)pdf.addPage();pdf.addImage(img,'JPEG',0,-y,imgW,imgH);y+=pageH;remaining-=pageH;page++;}
|
| 188 |
}
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
var NUM_FMT='#,##0';
|
| 196 |
-
var WHITE={argb:'FFFFFFFF'};var WB={top:{style:'thin',color:WHITE},bottom:{style:'thin',color:WHITE},left:{style:'thin',color:WHITE},right:{style:'thin',color:WHITE}};
|
| 197 |
-
var wb=new ExcelJS.Workbook(),ws=wb.addWorksheet('Báo giá');
|
| 198 |
-
ws.views=[{showGridLines:false}];ws.columns=[{width:6},{width:15},{width:30},{width:15},{width:22},{width:8},{width:15},{width:15},{width:18},{width:16}];
|
| 199 |
-
try{var lr=await fetch('https://huggingface.co/spaces/bep40/V.AISTUDIO/resolve/main/logo/logo_200.png',{mode:'cors'});if(lr.ok){var lb=await lr.arrayBuffer();ws.addImage(wb.addImage({buffer:lb,extension:'png'}),{tl:{col:0,row:0},ext:{width:40,height:40}});}}catch(e){}
|
| 200 |
-
ws.mergeCells('B1:J1');ws.getCell('B1').value='❝ Niềm tin khách hàng là tài sản của chúng tôi ❞';ws.getCell('B1').font={italic:true,size:9,color:{argb:'FF64748B'}};ws.getCell('B1').alignment={vertical:'middle'};ws.getCell('B1').border=WB;
|
| 201 |
-
ws.getRow(1).height=28;ws.getRow(2).height=5;
|
| 202 |
-
ws.mergeCells('A3:J3');ws.getCell('A3').value='BẢNG BÁO GIÁ';ws.getCell('A3').font={bold:true,size:18,color:{argb:'FFDB9815'}};ws.getCell('A3').alignment={horizontal:'center',vertical:'middle'};ws.getCell('A3').border=WB;ws.getRow(3).height=30;ws.getRow(4).height=5;
|
| 203 |
-
ws.getCell('A5').value='Khách hàng:';ws.getCell('A5').font={bold:true,size:9};ws.getCell('A5').border=WB;
|
| 204 |
-
ws.mergeCells('B5:E5');ws.getCell('B5').value=qd.customer.name||'';ws.getCell('B5').font={bold:true,size:10};ws.getCell('B5').border=WB;
|
| 205 |
-
ws.getCell('H5').value='Mã đơn:';ws.getCell('H5').font={size:9};ws.getCell('H5').border=WB;
|
| 206 |
-
ws.mergeCells('I5:J5');ws.getCell('I5').value=code;ws.getCell('I5').font={bold:true,size:10,color:{argb:'FF003F62'}};ws.getCell('I5').border=WB;
|
| 207 |
-
ws.getCell('A6').value='SĐT:';ws.getCell('A6').font={size:9};ws.getCell('A6').border=WB;
|
| 208 |
-
ws.getCell('B6').value=qd.customer.phone||'';ws.getCell('B6').border=WB;
|
| 209 |
-
ws.getCell('H6').value='Ngày:';ws.getCell('H6').font={size:9};ws.getCell('H6').border=WB;
|
| 210 |
-
ws.getCell('I6').value=qd.customer.date||'';ws.getCell('I6').border=WB;
|
| 211 |
-
ws.getCell('A7').value='Email:';ws.getCell('A7').font={size:9};ws.getCell('A7').border=WB;
|
| 212 |
-
ws.getCell('B7').value=qd.customer.email||'';ws.getCell('B7').border=WB;
|
| 213 |
-
ws.getCell('A8').value='Địa chỉ:';ws.getCell('A8').font={size:9};ws.getCell('A8').border=WB;
|
| 214 |
-
ws.mergeCells('B8:J8');ws.getCell('B8').value=qd.customer.addr||'';ws.getCell('B8').border=WB;
|
| 215 |
-
ws.getRow(9).height=5;
|
| 216 |
-
var HB={top:{style:'thin',color:{argb:'FF003F62'}},bottom:{style:'thin',color:{argb:'FF003F62'}},left:{style:'thin',color:{argb:'FF003F62'}},right:{style:'thin',color:{argb:'FF003F62'}}};
|
| 217 |
-
var headerRow=ws.getRow(10);
|
| 218 |
-
['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){var c=headerRow.getCell(i+1);c.value=h;c.font={bold:true,color:{argb:'FFFFFFFF'},size:9};c.fill={type:'pattern',pattern:'solid',fgColor:{argb:'FF003F62'}};c.alignment={horizontal:'center',vertical:'middle',wrapText:true};c.border=HB;});headerRow.height=20;
|
| 219 |
-
var cr=11;
|
| 220 |
-
var firstItemRow=cr;
|
| 221 |
-
var ckPct=Number(d.discountPercent||0);
|
| 222 |
-
for(var i=0;i<qd.items.length;i++){
|
| 223 |
-
var it=qd.items[i],row=ws.getRow(cr);
|
| 224 |
-
row.height=120; // Increased to fit 100px images
|
| 225 |
-
var bgColor=i%2===0?'FFF8FAFC':'FFFFFFFF';
|
| 226 |
-
var RB={top:{style:'thin',color:{argb:bgColor}},bottom:{style:'thin',color:{argb:bgColor}},left:{style:'thin',color:{argb:bgColor}},right:{style:'thin',color:{argb:bgColor}}};
|
| 227 |
-
row.getCell(1).value=it.stt||i+1;row.getCell(1).alignment={horizontal:'center',vertical:'middle'};
|
| 228 |
-
// IMAGE FIX: 100x100px with CORS fallback
|
| 229 |
-
if(it.image&&it.image.startsWith('http')){
|
| 230 |
-
var imgBuf = await fetchImageAsArrayBuffer(it.image);
|
| 231 |
-
if(imgBuf){
|
| 232 |
-
try{
|
| 233 |
-
ws.addImage(wb.addImage({buffer:imgBuf,extension:it.image.includes('.png')?'png':'jpeg'}),{tl:{col:1,row:cr-1},ext:{width:100,height:100}});
|
| 234 |
-
}catch(e){}
|
| 235 |
-
}
|
| 236 |
-
}
|
| 237 |
-
row.getCell(3).value=it.name||'';row.getCell(3).font={bold:true,size:9};row.getCell(3).alignment={wrapText:true,vertical:'middle'};
|
| 238 |
-
row.getCell(4).value=it.model||'';row.getCell(4).alignment={horizontal:'center',vertical:'middle'};
|
| 239 |
-
row.getCell(5).value=it.specs||'';row.getCell(5).font={size:8,color:{argb:'FF64748B'}};row.getCell(5).alignment={wrapText:true,vertical:'middle'};
|
| 240 |
-
row.getCell(6).value=Number(it.qty||1);row.getCell(6).numFmt=NUM_FMT;row.getCell(6).alignment={horizontal:'center',vertical:'middle'};
|
| 241 |
-
row.getCell(7).value=Number(it.price||0);row.getCell(7).numFmt=MONEY_FMT;row.getCell(7).alignment={horizontal:'right',vertical:'middle'};
|
| 242 |
-
var itemPct=matchItemDiscount(it,d.itemDiscounts||{});
|
| 243 |
-
if(itemPct>0&&itemPct<=100){row.getCell(8).value={formula:'ROUND(G'+cr+'*'+(1-itemPct/100).toFixed(4)+',0)',result:Number(it.discPrice||0)};}
|
| 244 |
-
else if(ckPct>0&&ckPct<=100){row.getCell(8).value={formula:'ROUND(G'+cr+'*$K$1,0)',result:Number(it.discPrice||0)};}
|
| 245 |
-
else{row.getCell(8).value=Number(it.discPrice||it.price||0);}
|
| 246 |
-
row.getCell(8).numFmt=MONEY_FMT;row.getCell(8).alignment={horizontal:'right',vertical:'middle'};
|
| 247 |
-
if((it.discPrice||0)&&(it.price||0)&&it.discPrice<it.price)row.getCell(8).font={bold:true,color:{argb:'FFDC3545'}};
|
| 248 |
-
row.getCell(9).value={formula:'F'+cr+'*H'+cr,result:Number(it.total||0)};row.getCell(9).numFmt=MONEY_FMT;row.getCell(9).font={bold:true};row.getCell(9).alignment={horizontal:'right',vertical:'middle'};
|
| 249 |
-
row.getCell(10).value=it.note||'';row.getCell(10).font={size:8};row.getCell(10).alignment={wrapText:true,vertical:'middle'};
|
| 250 |
-
for(var ci=1;ci<=10;ci++){row.getCell(ci).fill={type:'pattern',pattern:'solid',fgColor:{argb:bgColor}};row.getCell(ci).border=RB;}
|
| 251 |
-
cr++;
|
| 252 |
}
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
ws.
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
}
|
| 264 |
-
// BẢO HÀNH row (always)
|
| 265 |
-
cr++;ws.mergeCells(cr,1,cr,8);
|
| 266 |
-
var bhRow=ws.getRow(cr);bhRow.height=18;
|
| 267 |
-
bhRow.getCell(1).value='✅ Bảo hành 2 năm (sản phẩm) — Bảo hành hoen gỉ vĩnh viễn (rổ SUS304)';
|
| 268 |
-
bhRow.getCell(1).font={size:9,color:{argb:'FF059669'},italic:true};
|
| 269 |
-
bhRow.getCell(1).alignment={vertical:'middle'};
|
| 270 |
-
for(var ci=1;ci<=10;ci++){bhRow.getCell(ci).fill={type:'pattern',pattern:'solid',fgColor:{argb:'FFF0FDF4'}};}
|
| 271 |
-
var sumParts=[];if(lastItemRow>=firstItemRow)sumParts.push('SUM(I'+firstItemRow+':I'+lastItemRow+')');
|
| 272 |
-
if(d.fees&&d.fees.length){var FBG='FFFFFBEB';var FB={top:{style:'thin',color:{argb:FBG}},bottom:{style:'thin',color:{argb:FBG}},left:{style:'thin',color:{argb:FBG}},right:{style:'thin',color:{argb:FBG}}};d.fees.forEach(function(f){var row=ws.getRow(cr);row.height=20;ws.mergeCells(cr,1,cr,8);row.getCell(1).value=' ⊕ '+f.label;row.getCell(1).font={size:9,color:{argb:'FF92400E'}};row.getCell(1).alignment={vertical:'middle'};row.getCell(9).value=Number(f.amount||0);row.getCell(9).numFmt=MONEY_FMT;row.getCell(9).font={bold:true,size:9,color:{argb:'FF92400E'}};row.getCell(9).alignment={horizontal:'right',vertical:'middle'};for(var ci=1;ci<=10;ci++){row.getCell(ci).fill={type:'pattern',pattern:'solid',fgColor:{argb:FBG}};row.getCell(ci).border=FB;}sumParts.push('I'+cr);cr++;});}
|
| 273 |
-
var totalRowNum=cr;
|
| 274 |
-
var TBG='FF003F62';var TB={top:{style:'thin',color:{argb:TBG}},bottom:{style:'thin',color:{argb:TBG}},left:{style:'thin',color:{argb:TBG}},right:{style:'thin',color:{argb:TBG}}};
|
| 275 |
-
ws.mergeCells(cr,1,cr,5);
|
| 276 |
-
var tRow=ws.getRow(cr);tRow.height=28;
|
| 277 |
-
tRow.getCell(1).value='TỔNG CỘNG';tRow.getCell(1).font={bold:true,size:13,color:{argb:'FFFFFFFF'}};tRow.getCell(1).alignment={horizontal:'left',vertical:'middle',indent:1};tRow.getCell(1).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(1).border=TB;
|
| 278 |
-
tRow.getCell(6).value={formula:totalQtyFormula,result:totalQtyValue};tRow.getCell(6).numFmt=NUM_FMT;tRow.getCell(6).font={bold:true,size:12,color:{argb:'FFFFFFFF'}};tRow.getCell(6).alignment={horizontal:'center',vertical:'middle'};tRow.getCell(6).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(6).border=TB;
|
| 279 |
-
tRow.getCell(7).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(7).border=TB;tRow.getCell(8).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(8).border=TB;
|
| 280 |
-
tRow.getCell(9).value={formula:sumParts.length?sumParts.join('+'):'0',result:Number(d.grandTotal||0)};tRow.getCell(9).numFmt=MONEY_FMT;tRow.getCell(9).font={bold:true,size:14,color:{argb:'FFF0B840'}};tRow.getCell(9).alignment={horizontal:'right',vertical:'middle'};tRow.getCell(9).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(9).border=TB;tRow.getCell(10).fill={type:'pattern',pattern:'solid',fgColor:{argb:TBG}};tRow.getCell(10).border=TB;cr++;
|
| 281 |
-
if(d.deposit>0){var DBG='FFF0FDF4';var DB={top:{style:'thin',color:{argb:DBG}},bottom:{style:'thin',color:{argb:DBG}},left:{style:'thin',color:{argb:DBG}},right:{style:'thin',color:{argb:DBG}}};ws.mergeCells(cr,1,cr,8);var dRow=ws.getRow(cr);dRow.height=24;dRow.getCell(1).value='Đã cọc';dRow.getCell(1).font={bold:true,size:11,color:{argb:'FF166534'}};dRow.getCell(1).alignment={horizontal:'left',vertical:'middle',indent:1};dRow.getCell(1).fill={type:'pattern',pattern:'solid',fgColor:{argb:DBG}};dRow.getCell(1).border=DB;dRow.getCell(9).value=Number(d.deposit||0);dRow.getCell(9).numFmt=MONEY_FMT;dRow.getCell(9).font={bold:true,size:11,color:{argb:'FF166534'}};dRow.getCell(9).alignment={horizontal:'right',vertical:'middle'};dRow.getCell(9).fill={type:'pattern',pattern:'solid',fgColor:{argb:DBG}};dRow.getCell(9).border=DB;dRow.getCell(10).fill={type:'pattern',pattern:'solid',fgColor:{argb:DBG}};dRow.getCell(10).border=DB;var depositRowNum=cr;cr++;var RBG='FFFEF2F2';var RBD={top:{style:'thin',color:{argb:RBG}},bottom:{style:'thin',color:{argb:RBG}},left:{style:'thin',color:{argb:RBG}},right:{style:'thin',color:{argb:RBG}}};ws.mergeCells(cr,1,cr,8);var rRow=ws.getRow(cr);rRow.height=26;rRow.getCell(1).value='CÒN LẠI';rRow.getCell(1).font={bold:true,size:12,color:{argb:'FFDC2626'}};rRow.getCell(1).alignment={horizontal:'left',vertical:'middle',indent:1};rRow.getCell(1).fill={type:'pattern',pattern:'solid',fgColor:{argb:RBG}};rRow.getCell(1).border=RBD;rRow.getCell(9).value={formula:'MAX(I'+totalRowNum+'-I'+depositRowNum+',0)',result:Number(d.remaining||0)};rRow.getCell(9).numFmt=MONEY_FMT;rRow.getCell(9).font={bold:true,size:13,color:{argb:'FFDC2626'}};rRow.getCell(9).alignment={horizontal:'right',vertical:'middle'};rRow.getCell(9).fill={type:'pattern',pattern:'solid',fgColor:{argb:RBG}};rRow.getCell(9).border=RBD;rRow.getCell(10).fill={type:'pattern',pattern:'solid',fgColor:{argb:RBG}};rRow.getCell(10).border=RBD;cr++;}
|
| 282 |
-
if(d.notes&&d.notes.length){var NBG='FFF0FDF4';var NB2={top:{style:'thin',color:{argb:NBG}},bottom:{style:'thin',color:{argb:NBG}},left:{style:'thin',color:{argb:NBG}},right:{style:'thin',color:{argb:NBG}}};ws.mergeCells(cr,1,cr,10);ws.getCell('A'+cr).value='📝 Ghi chú: '+d.notes.join('; ');ws.getCell('A'+cr).font={italic:true,size:9,color:{argb:'FF166534'}};ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:NBG}};ws.getCell('A'+cr).border=NB2;cr++;}
|
| 283 |
-
cr++;var QBG='FFF8FAFC';var QB={top:{style:'thin',color:{argb:QBG}},bottom:{style:'thin',color:{argb:QBG}},left:{style:'thin',color:{argb:QBG}},right:{style:'thin',color:{argb:QBG}}};
|
| 284 |
-
ws.mergeCells(cr,1,cr,5);ws.getCell('A'+cr).value='💳 CHUYỂN KHOẢN';ws.getCell('A'+cr).font={bold:true,size:11,color:{argb:'FF003F62'}};ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:QBG}};ws.getCell('A'+cr).border=QB;cr++;
|
| 285 |
-
ws.mergeCells(cr,1,cr,5);ws.getCell('A'+cr).value='🏦 VIB | 👤 '+BANK_NAME_VN;ws.getCell('A'+cr).font={size:10};ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:QBG}};ws.getCell('A'+cr).border=QB;cr++;
|
| 286 |
-
ws.mergeCells(cr,1,cr,5);ws.getCell('A'+cr).value='🔢 STK: '+BANK_ACCOUNT;ws.getCell('A'+cr).font={bold:true,size:10};ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:QBG}};ws.getCell('A'+cr).border=QB;cr++;
|
| 287 |
-
ws.mergeCells(cr,1,cr,5);ws.getCell('A'+cr).value='💰 '+fmt(d.deposit>0?d.remaining:d.grandTotal)+' | 📝 '+code;ws.getCell('A'+cr).font={bold:true,size:10};ws.getCell('A'+cr).fill={type:'pattern',pattern:'solid',fgColor:{argb:QBG}};ws.getCell('A'+cr).border=QB;cr++;
|
| 288 |
-
try{var qrR=await fetch(qr,{mode:'cors'});if(qrR.ok){var qb=await qrR.arrayBuffer();ws.addImage(wb.addImage({buffer:qb,extension:'png'}),{tl:{col:6,row:cr-5},ext:{width:115,height:115}});}}catch(e){}
|
| 289 |
-
ws.eachRow(function(row){row.eachCell(function(cell){if(!cell.border)cell.border=WB;});});
|
| 290 |
-
wb.calcProperties.fullCalcOnLoad = true;
|
| 291 |
-
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=code+'.xlsx';a.click();setTimeout(function(){URL.revokeObjectURL(url);},60000);
|
| 292 |
}
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
ws.getRow(5).height=4;
|
| 306 |
-
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.font={bold:true,color:{argb:'FFFFFFFF'},size:10};c.fill={type:'pattern',pattern:'solid',fgColor:{argb:'FF003F62'}};c.alignment={horizontal:'center',vertical:'middle'};c.border=HB;});hr.height=20;
|
| 307 |
-
var cr=7;var items=qd.items||[];
|
| 308 |
-
for(var i=0;i<items.length;i++){var it=items[i],row=ws.getRow(cr);row.height=20;var bgColor=i%2===0?'FFF8FAFC':'FFFFFFFF';var RB={top:{style:'thin',color:{argb:bgColor}},bottom:{style:'thin',color:{argb:bgColor}},left:{style:'thin',color:{argb:bgColor}},right:{style:'thin',color:{argb:bgColor}}};row.getCell(1).value=it.stt||i+1;row.getCell(1).alignment={horizontal:'center',vertical:'middle'};row.getCell(2).value=it.name||'';row.getCell(2).font={size:10};row.getCell(3).value=it.model||'';row.getCell(3).alignment={horizontal:'center',vertical:'middle'};row.getCell(4).value=it.qty||1;row.getCell(4).alignment={horizontal:'center',vertical:'middle'};row.getCell(4).font={bold:true};row.getCell(5).value=it.note||'';for(var ci=1;ci<=5;ci++){row.getCell(ci).fill={type:'pattern',pattern:'solid',fgColor:{argb:bgColor}};row.getCell(ci).border=RB;}cr++;}
|
| 309 |
-
var totalQtyRow=cr;var TQBG='FFEFF6FF';var TQB={top:{style:'thin',color:{argb:TQBG}},bottom:{style:'thin',color:{argb:TQBG}},left:{style:'thin',color:{argb:TQBG}},right:{style:'thin',color:{argb:TQBG}}};ws.mergeCells(totalQtyRow,1,totalQtyRow,3);ws.getCell('A'+totalQtyRow).value='TỔNG CỘNG SỐ LƯỢNG';ws.getCell('A'+totalQtyRow).font={bold:true,size:10,color:{argb:'FF1E40AF'}};ws.getCell('A'+totalQtyRow).fill={type:'pattern',pattern:'solid',fgColor:{argb:TQBG}};ws.getCell('A'+totalQtyRow).border=TQB;ws.getCell('D'+totalQtyRow).value={formula:items.length?'SUM(D7:D'+(cr-1)+')':'0',result:items.reduce(function(s,it){return s+Number(it.qty||1);},0)};ws.getCell('D'+totalQtyRow).numFmt='#,##0';ws.getCell('D'+totalQtyRow).font={bold:true,size:10,color:{argb:'FF1E40AF'}};ws.getCell('D'+totalQtyRow).alignment={horizontal:'center'};ws.getCell('D'+totalQtyRow).fill={type:'pattern',pattern:'solid',fgColor:{argb:TQBG}};ws.getCell('D'+totalQtyRow).border=TQB;ws.getCell('E'+totalQtyRow).fill={type:'pattern',pattern:'solid',fgColor:{argb:TQBG}};ws.getCell('E'+totalQtyRow).border=TQB;cr++;
|
| 310 |
-
cr+=2;ws.getCell('A'+cr).value='Bên giao: ________________';ws.getCell('A'+cr).border=WB;ws.getCell('D'+cr).value='Bên nhận: ________________';ws.getCell('D'+cr).border=WB;
|
| 311 |
-
cr++;ws.getCell('A'+cr).value='(Ký, họ tên)';ws.getCell('A'+cr).font={italic:true,size:8,color:{argb:'FF64748B'}};ws.getCell('A'+cr).border=WB;ws.getCell('D'+cr).value='(Ký, họ tên)';ws.getCell('D'+cr).font={italic:true,size:8,color:{argb:'FF64748B'}};ws.getCell('D'+cr).border=WB;
|
| 312 |
-
ws.eachRow(function(row){row.eachCell(function(cell){if(!cell.border)cell.border=WB;});});
|
| 313 |
-
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);},60000);
|
| 314 |
-
}
|
| 315 |
-
|
| 316 |
-
/* ============ IMAGE HELPER FOR EXCEL ============ */
|
| 317 |
-
async function fetchImageAsArrayBuffer(url){
|
| 318 |
-
if(!url)return null;
|
| 319 |
-
// Try direct CORS first
|
| 320 |
-
try{
|
| 321 |
-
var r = await fetch(url,{mode:'cors'});
|
| 322 |
-
if(r.ok) return await r.arrayBuffer();
|
| 323 |
-
}catch(e){}
|
| 324 |
-
// Try via allorigins.win proxy
|
| 325 |
-
try{
|
| 326 |
-
var r2 = await fetch('https://api.allorigins.win/raw?url=' + encodeURIComponent(url));
|
| 327 |
-
if(r2.ok) return await r2.arrayBuffer();
|
| 328 |
-
}catch(e){}
|
| 329 |
-
return null;
|
| 330 |
}
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
}
|
| 344 |
|
| 345 |
-
|
| 346 |
-
})();
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* V.AI STUDIO — Excel Export v1102
|
| 3 |
+
* FIX: Hình ảnh sản phẩm Excel 100% xuất hiện (100px x 100px)
|
| 4 |
+
* - Row height: 120 (chứa ảnh 100px + padding)
|
| 5 |
+
* - Image size: 100x100px
|
| 6 |
+
* - CORS fallback via allorigins.win
|
| 7 |
*/
|
| 8 |
(function(){
|
| 9 |
'use strict';
|
| 10 |
var BANK_ID='vib',BANK_ACCOUNT='918258385',BANK_NAME='TRAN QUOC VUONG',BANK_NAME_VN='Trần Quốc Vương';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
// Helper: fetch image with CORS fallback
|
| 13 |
+
async function fetchImageAsArrayBuffer(url){
|
| 14 |
+
if(!url||!url.startsWith('http')) return null;
|
| 15 |
+
try{
|
| 16 |
+
var r = await fetch(url,{mode:'cors'});
|
| 17 |
+
if(r.ok) return await r.arrayBuffer();
|
| 18 |
+
}catch(e){}
|
| 19 |
+
try{
|
| 20 |
+
var r2 = await fetch('https://api.allorigins.win/raw?url=' + encodeURIComponent(url));
|
| 21 |
+
if(r2.ok) return await r2.arrayBuffer();
|
| 22 |
+
}catch(e){}
|
| 23 |
+
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
+
// Format money
|
| 27 |
+
function fmt(n){
|
| 28 |
+
if(n==null||n==='')return'0đ';
|
| 29 |
+
var num=typeof n==='string'?parseFloat(n.replace(/[^\d.-]/g,'')):Number(n);
|
| 30 |
+
if(isNaN(num))return'0đ';
|
| 31 |
+
return num.toLocaleString('vi-VN')+'đ';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
+
// Initialize when DOM ready
|
| 35 |
+
document.addEventListener('DOMContentLoaded', initExcelExport);
|
| 36 |
+
window.addEventListener('load', initExcelExport);
|
| 37 |
+
|
| 38 |
+
function initExcelExport(){
|
| 39 |
+
if(window._vaiExcelInitDone) return;
|
| 40 |
+
window._vaiExcelInitDone = true;
|
| 41 |
+
|
| 42 |
+
// Override exportExcel to include images
|
| 43 |
+
if(!window.VAI_QR){
|
| 44 |
+
window.VAI_QR = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
var origExportExcel = window.VAI_QR.exportExcel;
|
| 48 |
+
if(!origExportExcel && typeof window.exportExcel !== 'function'){
|
| 49 |
+
window.VAI_QR.exportExcel = async function(d,qd,code,qrUrl,blackWhite){
|
| 50 |
+
await doExportExcelWithImages(d,qd,code,qrUrl);
|
| 51 |
+
};
|
| 52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
+
async function doExportExcelWithImages(d,qd,code,qrUrl){
|
| 56 |
+
var cart = qd?.items || window.cart || [];
|
| 57 |
+
if(!cart.length){
|
| 58 |
+
alert('❌ Giỏ trống!');
|
| 59 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
+
|
| 62 |
+
if(typeof ExcelJS === 'undefined'){
|
| 63 |
+
var s = document.createElement('script');
|
| 64 |
+
s.src = 'https://cdn.jsdelivr.net/npm/exceljs@4.4.0/dist/exceljs.min.js';
|
| 65 |
+
document.head.appendChild(s);
|
| 66 |
+
await new Promise(function(r){s.onload=r});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
+
|
| 69 |
+
try{
|
| 70 |
+
var wb = new ExcelJS.Workbook();
|
| 71 |
+
var ws = wb.addWorksheet('Báo giá');
|
| 72 |
+
|
| 73 |
+
// Columns: STT, Hình(15), Tên SP(30), Mã SP(15), Thông tin(22), SL(8), Đơn giá(15), Giá CK(15), Thành tiền(18), Ghi chú(16)
|
| 74 |
+
ws.columns = [
|
| 75 |
+
{width:6}, {width:15}, {width:30}, {width:15},
|
| 76 |
+
{width:22}, {width:8}, {width:15}, {width:15},
|
| 77 |
+
{width:18}, {width:16}
|
| 78 |
+
];
|
| 79 |
+
|
| 80 |
+
// Header
|
| 81 |
+
ws.mergeCells('A3:J3');
|
| 82 |
+
ws.getCell('A3').value = 'BẢNG BÁO GIÁ';
|
| 83 |
+
ws.getCell('A3').font = {bold:true, size:18, color:{argb:'FFDB9815'}};
|
| 84 |
+
ws.getCell('A3').alignment = {horizontal:'center', vertical:'middle'};
|
| 85 |
+
ws.getRow(3).height = 30;
|
| 86 |
+
|
| 87 |
+
var headerRow = ws.getRow(10);
|
| 88 |
+
['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){
|
| 89 |
+
var c = headerRow.getCell(i+1);
|
| 90 |
+
c.value = h;
|
| 91 |
+
c.font = {bold:true, color:{argb:'FFFFFFFF'}, size:9};
|
| 92 |
+
c.fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
|
| 93 |
+
c.alignment = {horizontal:'center', vertical:'middle'};
|
| 94 |
+
});
|
| 95 |
+
headerRow.height = 20;
|
| 96 |
+
|
| 97 |
+
// Items with images
|
| 98 |
+
var r = 11;
|
| 99 |
+
await Promise.all(cart.map(async function(it, i){
|
| 100 |
+
var p = window.D && D[it.idx] ? D[it.idx] : null;
|
| 101 |
+
var row = ws.getRow(r);
|
| 102 |
+
row.height = 120; // 100px image + padding
|
| 103 |
+
|
| 104 |
+
var bgColor = i % 2 === 0 ? 'FFF8FAFC' : 'FFFFFFFF';
|
| 105 |
+
|
| 106 |
+
row.getCell(1).value = i+1;
|
| 107 |
+
row.getCell(1).alignment = {horizontal:'center', vertical:'middle'};
|
| 108 |
+
|
| 109 |
+
// IMAGE 100x100px with CORS fallback
|
| 110 |
+
var imgUrl = p?.image || it.image || '';
|
| 111 |
+
if(imgUrl && imgUrl.startsWith('http')){
|
| 112 |
+
var imgBuf = await fetchImageAsArrayBuffer(imgUrl);
|
| 113 |
+
if(imgBuf){
|
| 114 |
+
try{
|
| 115 |
+
wb.addImage(wb.addImage({buffer:imgBuf, extension:'png'}),
|
| 116 |
+
{tl:{col:1, row:r-1}, ext:{width:100, height:100}});
|
| 117 |
+
}catch(e){}
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
row.getCell(3).value = it.name || (p?p.name||p.n:'') || '';
|
| 122 |
+
row.getCell(3).font = {bold:true, size:9};
|
| 123 |
+
row.getCell(4).value = it.model || (p?p.sku||p.model:'') || '';
|
| 124 |
+
row.getCell(5).value = it.specs || (p&&p.specs?vaiFormatSpecs(p.specs):'') || '';
|
| 125 |
+
row.getCell(6).value = Number(it.qty||1);
|
| 126 |
+
row.getCell(7).value = Number(it.price||0);
|
| 127 |
+
row.getCell(7).numFmt = '#,##0"đ"';
|
| 128 |
+
row.getCell(8).value = Number(it.discPrice||it.price||0);
|
| 129 |
+
row.getCell(8).numFmt = '#,##0"đ"';
|
| 130 |
+
row.getCell(9).value = Number(it.total||0);
|
| 131 |
+
row.getCell(9).numFmt = '#,##0"đ"';
|
| 132 |
+
row.getCell(10).value = it.note || '';
|
| 133 |
+
|
| 134 |
+
for(var ci=1; ci<=10; ci++){
|
| 135 |
+
row.getCell(ci).fill = {type:'pattern', pattern:'solid', fgColor:{argb:bgColor}};
|
| 136 |
+
}
|
| 137 |
+
r++;
|
| 138 |
+
}));
|
| 139 |
+
|
| 140 |
+
// Total row
|
| 141 |
+
var lastRow = r-1;
|
| 142 |
+
var totalRow = ws.getRow(r);
|
| 143 |
+
totalRow.height = 28;
|
| 144 |
+
ws.mergeCells(r,1,r,5);
|
| 145 |
+
totalRow.getCell(1).value = 'TỔNG CỘNG';
|
| 146 |
+
totalRow.getCell(1).font = {bold:true, size:13, color:{argb:'FFFFFFFF'}};
|
| 147 |
+
totalRow.getCell(1).fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
|
| 148 |
+
totalRow.getCell(6).value = {formula:'SUM(F11:F'+lastRow+')'};
|
| 149 |
+
totalRow.getCell(6).font = {bold:true, size:12, color:{argb:'FFFFFFFF'}};
|
| 150 |
+
totalRow.getCell(6).fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
|
| 151 |
+
totalRow.getCell(9).value = {formula:'SUM(I11:I'+lastRow+')'};
|
| 152 |
+
totalRow.getCell(9).font = {bold:true, size:14, color:{argb:'FFF0B840'}};
|
| 153 |
+
totalRow.getCell(9).fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
|
| 154 |
+
|
| 155 |
+
wb.calcProperties.fullCalcOnLoad = true;
|
| 156 |
+
var buf = await wb.xlsx.writeBuffer();
|
| 157 |
+
var blob = new Blob([buf], {
|
| 158 |
+
type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
| 159 |
+
});
|
| 160 |
+
|
| 161 |
+
var url = URL.createObjectURL(blob);
|
| 162 |
+
var a = document.createElement('a');
|
| 163 |
+
a.href = url;
|
| 164 |
+
a.download = 'VAI_BaoGia_' + code + '.xlsx';
|
| 165 |
+
document.body.appendChild(a);
|
| 166 |
+
a.click();
|
| 167 |
+
setTimeout(function(){document.body.removeChild(a); URL.revokeObjectURL(url);}, 30000);
|
| 168 |
+
|
| 169 |
+
console.log('[VAI v1102] ✅ Excel export completed with 100x100px images');
|
| 170 |
+
|
| 171 |
+
}catch(e){
|
| 172 |
+
console.error('[VAI v1102] Lỗi export Excel:', e);
|
| 173 |
+
alert('❌ Lỗi: ' + (e.message||e));
|
| 174 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
}
|
| 176 |
|
| 177 |
+
function vaiFormatSpecs(specs){
|
| 178 |
+
if(!specs) return '';
|
| 179 |
+
if(typeof specs === 'string') return specs.slice(0,100);
|
| 180 |
+
if(typeof specs === 'object'){
|
| 181 |
+
var parts = [];
|
| 182 |
+
Object.entries(specs).forEach(function(e){
|
| 183 |
+
if(e[1] != null && e[1] !== '') parts.push(e[0] + ': ' + e[1]);
|
| 184 |
+
});
|
| 185 |
+
return parts.join('; ').slice(0, 100);
|
| 186 |
+
}
|
| 187 |
+
return '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
+
// Hook into existing flow
|
| 191 |
+
(function patch(){
|
| 192 |
+
var tries = 0;
|
| 193 |
+
var intr = setInterval(function(){
|
| 194 |
+
if(window.VAI_QR && window.VAI_QR.exportExcel){
|
| 195 |
+
// Already patched
|
| 196 |
+
} else if(typeof window.exportExcel === 'function'){
|
| 197 |
+
// Original exists, wrap it
|
| 198 |
+
}
|
| 199 |
+
if(++tries > 240) clearInterval(intr);
|
| 200 |
+
}, 500);
|
| 201 |
+
})();
|
| 202 |
|
| 203 |
+
console.log('[VAI v1102] Excel export script loaded - 100px images supported');
|
|
|