import {validateBidRows,quantityParts,parseBidPrice,bidRowTotal,roundMoney} from './bidValidation.js'; export async function createBidWorkbook(form,rows) { const errors=validateBidRows(rows); if(errors.length) throw new Error(errors.join(' ')); const {default:ExcelJS}=await import('exceljs'); const book=new ExcelJS.Workbook(); book.creator='İhaleist'; const sheet=book.addWorksheet('Teklif Cetveli'); sheet.columns=[{width:8},{width:55},{width:16},{width:18},{width:22},{width:22}]; sheet.addRow(['BİRİM FİYAT TEKLİF CETVELİ — TASLAK']);sheet.mergeCells('A1:F1'); sheet.addRow(['İKN',form.ikn || '']); sheet.addRow(['Firma',form.firmaUnvan || '']); sheet.addRow(['Kısım',form.kisim || 'Tüm kapsam']); sheet.addRow(['Resmi standart formun yerine geçmez. Kaynak dokümanla karşılaştırın.']);sheet.mergeCells('A5:F5'); sheet.addRow(['Sıra','Mal / hizmet adı','Miktar','Birim','Birim fiyat (TL)','Tutar (TL)']); rows.forEach((r,i)=>{ const {miktar,birim}=quantityParts(r.quantity); const number=7+i; sheet.addRow([i+1,r.name,miktar,birim,parseBidPrice(r.birimFiyat),{formula:`ROUND(C${number}*E${number},2)`,result:bidRowTotal(r)}]); }); const total=roundMoney(rows.reduce((sum,row)=>sum+bidRowTotal(row),0)); sheet.addRow(['','GENEL TOPLAM (KDV hariç)','','','',{formula:`SUM(F7:F${rows.length+6})`,result:total}]); sheet.getColumn(3).numFmt='#,##0.########';sheet.getColumn(5).numFmt='#,##0.00######';sheet.getColumn(6).numFmt='#,##0.00'; sheet.views=[{state:'frozen',ySplit:6}]; sheet.autoFilter={from:'A6',to:`F${rows.length+6}`}; sheet.pageSetup={paperSize:9,orientation:'landscape',fitToPage:true,fitToWidth:1,fitToHeight:0,printTitlesRow:'1:6'}; sheet.eachRow(row=>{row.alignment={vertical:'top',wrapText:true};}); for(const n of [1,6,sheet.rowCount])sheet.getRow(n).font={bold:true}; return book.xlsx.writeBuffer(); } export function downloadBidWorkbook(buffer,ikn) { const url=URL.createObjectURL(new Blob([buffer],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})); const link=document.createElement('a');link.href=url;link.download=`teklif_${String(ikn || 'taslak').replace(/[^\w-]/g,'_')}.xlsx`; document.body.appendChild(link);link.click();link.remove();setTimeout(()=>URL.revokeObjectURL(url),5000); }