New Tax Invoice
INV
Your Business Details
Client / Bill To Details
IGST (Interstate)
Invoice Details
Total Invoiced
βΉ'+total.toLocaleString('en-IN')+'
'+historyData.length+' invoices
Amount Collected
βΉ'+paid.toLocaleString('en-IN')+'
'+historyData.filter(function(i){return i.status==='paid';}).length+' paid
Pending Collection
βΉ'+pending.toLocaleString('en-IN')+'
'+historyData.filter(function(i){return i.status==='unpaid'||i.status==='partial';}).length+' pending
Overdue Invoices
'+overdueCnt+'
Immediate action
';
html+='';
for(var i=1;i<=pages;i++){
if(pages<=7||i===1||i===pages||Math.abs(i-histPage)<=1)
html+='';
else if(Math.abs(i-histPage)===2)html+='β¦';
}
html+='
';
box.innerHTML=html;
}function goHistPage(p){
var pages=Math.ceil(histFiltered.length/HIST_PER_PAGE);
if(p<1||p>pages)return;
histPage=p;
renderHistory();
}/* BULK */
function toggleRowSel(id,cb){
if(cb.checked)selectedRows.add(id);else selectedRows.delete(id);
updateBulkBar();
}
function toggleSelAll(cb){
var visible=histFiltered.slice((histPage-1)*HIST_PER_PAGE,histPage*HIST_PER_PAGE);
visible.forEach(function(inv){if(cb.checked)selectedRows.add(inv.id);else selectedRows.delete(inv.id);});
renderHistory();updateBulkBar();
}
function updateBulkBar(){
var bar=gel('bulk-bar');
if(selectedRows.size>0){bar.classList.add('show');gel('bulk-text').textContent=selectedRows.size+' selected';}
else bar.classList.remove('show');
}
function clearBulk(){selectedRows.clear();var sa=gel('sel-all');if(sa)sa.checked=false;renderHistory();updateBulkBar();}
function bulkMarkPaid(){
selectedRows.forEach(function(id){var inv=historyData.find(function(i){return i.id===id;});if(inv)inv.status='paid';});
clearBulk();renderHistStats();renderHistory();
}
function bulkDelete(){
if(!confirm(selectedRows.size+' invoices permanently delete karein?'))return;
historyData=historyData.filter(function(i){return !selectedRows.has(i.id);});
clearBulk();renderHistStats();renderHistory();
}/* DETAIL DRAWER */
function toggleDrawer(id,e){
if(e&&e.target.tagName==='INPUT')return;
if(openDrawerId===id){closeDrawer();return;}
showDrawer(id);
}
function closeDrawer(){
openDrawerId=null;
var d=gel('detail-drawer');if(d){d.classList.remove('open');d.innerHTML='';}
}
function showDrawer(id){
var inv=historyData.find(function(i){return i.id===id;});
if(!inv)return;
openDrawerId=id;
var over=isOverdue(inv);
var d=gel('detail-drawer');
if(!d)return;
var itemsHTML=inv.items.map(function(it){
return ''+fmtDate(h.d)+'
'+escHtml(h.t)+'
'+
'
'+
''+
'
'+
''+escHtml(inv.num)+' β '+escHtml(inv.client)+'
'+
''+
''+inv.status.charAt(0).toUpperCase()+inv.status.slice(1)+''+
(over&&inv.status!=='paid'?'Overdue':'')+
''+DOC_LABELS[inv.type||'invoice']+''+
'
'+
''+
''+
''+
''+
'
'+
''+
'
';
d.classList.add('open');
d.scrollIntoView({behavior:'smooth',block:'start'});
}function editInvoice(id){
var inv=historyData.find(function(i){return i.id===id;});
if(!inv)return;
switchPage('generator',inv.type||'invoice');
sv('inv-num',inv.num);sv('cl-name',inv.client);sv('cl-gst',inv.clientGST||'');
sv('inv-date',inv.date);sv('due-date',inv.due);sv('inv-notes',inv.notes||'');
sv('pay-status',inv.status);
items=inv.items.map(function(it){return Object.assign({},it,{id:Date.now()+Math.random()});});
renderItems();updatePreview();
alert('Invoice loaded! Edit karke "Save Invoice" dabayein.');
}function deleteInvoice(id,e){
if(e)e.stopPropagation();
var inv=historyData.find(function(i){return i.id===id;});
if(!inv)return;
if(!confirm('"'+inv.num+'" permanently delete karein?'))return;
historyData=historyData.filter(function(i){return i.id!==id;});
if(openDrawerId===id)closeDrawer();
renderHistStats();renderHistory();
}function downloadHistPDF(id){
var inv=historyData.find(function(i){return i.id===id;});
if(!inv){alert('Invoice not found');return;}
editInvoice(id);
setTimeout(function(){downloadPDF();},500);
}function exportHistoryCSV(){
var headers=['Invoice #','Type','Client','Client GSTIN','Date','Due Date','Amount','GST','Status'];
var rows=histFiltered.map(function(inv){
return [inv.num,inv.type,inv.client,inv.clientGST||'',inv.date,inv.due,inv.amount,inv.gst,inv.status];
});
var csv=[headers].concat(rows).map(function(r){return r.map(function(c){return '"'+(c||'')+'"';}).join(',');}).join('\n');
var blob=new Blob([csv],{type:'text/csv;charset=utf-8;'});
var a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download='invoices_export.csv';a.click();
}/* ============================================================
INIT
============================================================ */
function initDates(){
var today=new Date();
var fmt=function(d){return d.toISOString().split('T')[0];};
sv('inv-date',fmt(today));
var due=new Date(today);due.setDate(due.getDate()+30);
sv('due-date',fmt(due));
}function init(){
initDates();
sv('inv-num','INV-2024-001');
sv('biz-name','Sharma Digital Agency');
sv('biz-gst','08AAACS0472N1Z1');
sv('biz-phone','+91 98765 43210');
sv('biz-email','hello@sharma.in');
sv('biz-addr','123, MG Road\nJaipur, Rajasthan β 302001');
sv('biz-state','Rajasthan');
sv('cl-name','Ramesh Kumar & Co.');
sv('cl-gst','07BBBCS1234M1Z5');
sv('cl-addr','456, Civil Lines\nNew Delhi β 110001');
sv('cl-state','Delhi');
sv('bank-name','State Bank of India');
sv('bank-ifsc','SBIN0001234');
sv('bank-holder','Sharma Digital Agency');
sv('upi-id','sharma@okicici');
sv('upi-name','Sharma Digital Agency');
sv('biz-avatar','SD');
sv_text('biz-display-name','Sharma Digital Agency');
detectGSTType();
addItem('Web Design & Development','998311',1,'Nos',25000,0,18);
addItem('SEO Optimization','998315',3,'Month',5000,10,18);
addItem('Content Writing','998399',5,'Nos',2000,0,18);
updatePreview();
}window.addEventListener('DOMContentLoaded', init);
'+fmtDate(inv.date)+'
'+
''+fmtDate(inv.due)+'
'+
''+escHtml(inv.clientGST||'β')+'
'+
'βΉ'+inv.gst.toLocaleString('en-IN')+'
'+
(inv.paidOn?''+fmtDate(inv.paidOn)+'
':'')+
(inv.notes?''+escHtml(inv.notes)+'
':'')+''+
''+
'
'+
'Line Items
'+
''+
''+
'
'+
'Activity Timeline
'+
''+tlHTML+'
'+
'