Skip to content

Commit c923397

Browse files
committed
v1.22.0: optimize exports to fetch ledgers in batch instead of one-by-one
1 parent ce7626a commit c923397

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

index.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,12 @@ <h4>Pending UPI payments <span id="pendingCount" class="pending-badge">0</span><
737737
for(const id of ids){ recalcTotals(data.citizens[id]); if((++done%50)===0&&onProgress) onProgress(done, ids.length); }
738738
persist(); notify(); if(onProgress) onProgress(ids.length, ids.length); return ids.length;
739739
},
740-
async migrateEntries(){ return 0; } // no-op for local
740+
async migrateEntries(){ return 0; }, // no-op for local
741+
async getAllLedgers(){
742+
const out={};
743+
for(const [id,c] of Object.entries(data.citizens)){ out[id]=[...(c.ledger||[])]; }
744+
return out;
745+
}
741746
};
742747
}
743748

@@ -1141,6 +1146,15 @@ <h4>Pending UPI payments <span id="pendingCount" class="pending-badge">0</span><
11411146
await flush();
11421147
if(onProgress) onProgress(citizenIds.length, citizenIds.length);
11431148
return citizenIds.length;
1149+
},
1150+
async getAllLedgers(){
1151+
const out={};
1152+
const cSnap=await fs.getDocs(fs.collection(db,"citizens"));
1153+
for(const d of cSnap.docs){
1154+
const ledSnap=await fs.getDocs(fs.collection(db,"citizens",d.id,"ledger"));
1155+
out[d.id]=ledSnap.docs.map(x=>x.data());
1156+
}
1157+
return out;
11441158
}
11451159
};
11461160
}
@@ -2315,9 +2329,10 @@ <h3 style="font-size:12.5px;text-transform:uppercase;letter-spacing:1px;color:va
23152329
const btn=$("exportAllBtn"); if(btn){ btn.disabled=true; btn.textContent="Preparing…"; }
23162330
try{
23172331
const citizens = allCitizens;
2332+
const ledgers = await store.getAllLedgers();
23182333
let csv = "Name,Job Card,Total Charged (₹),Total Paid (₹),Balance (₹),Status,Payment History\n";
23192334
for(const c of citizens){
2320-
const entries = await store.getLedger(c.id);
2335+
const entries = ledgers[c.id] || [];
23212336
const payments = entries.filter(e=>e.type==="payment").map(e=>`${entryLabel(e)} ${fmt(e.amount)}`).join("; ");
23222337
const status = statusOf(c);
23232338
csv += `"${esc(c.name)}","${esc(c.jobCard)}",${c.totalCharged||0},${c.totalPaid||0},${c.balance||0},"${statusLabel[status]}","${esc(payments)}"\n`;
@@ -2333,9 +2348,10 @@ <h3 style="font-size:12.5px;text-transform:uppercase;letter-spacing:1px;color:va
23332348
}
23342349

23352350
async function openExportPeriod(){
2351+
const ledgers = await store.getAllLedgers();
23362352
const periods = [];
23372353
for(const c of allCitizens){
2338-
const entries = await store.getLedger(c.id);
2354+
const entries = ledgers[c.id] || [];
23392355
entries.forEach(e => { if(e.type==="charge" && e.note) periods.push(e.note); });
23402356
}
23412357
const uniquePeriods = [...new Set(periods)].sort();
@@ -2360,9 +2376,10 @@ <h3 style="font-size:12.5px;text-transform:uppercase;letter-spacing:1px;color:va
23602376
async function doExportPeriod(period){
23612377
if(exportBusy) return; exportBusy=true;
23622378
try{
2379+
const ledgers = await store.getAllLedgers();
23632380
let csv = "Name,Job Card,Amount (₹),Status,Payment History\n";
23642381
for(const c of allCitizens){
2365-
const entries = await store.getLedger(c.id);
2382+
const entries = ledgers[c.id] || [];
23662383
const charge = entries.find(e => e.type==="charge" && e.note===period);
23672384
if(charge){
23682385
const payments = entries.filter(e=>e.type==="payment").map(e=>`${entryLabel(e)} ${fmt(e.amount)}`).join("; ");

0 commit comments

Comments
 (0)