@davoodmehraban89 / Factor-easy / commits / 9b96b20
Fork 0

سامانه صدور فاکتور و حسابداری

Phase 6: add journal-derived professional reports

davoodmehraban89 committed Sep 29, 2026 at 21:19 UTC 9b96b2062d7fcc7751b3ad3bb27c70cbcd3187f6
1 file changed +20
js/phase6-reporting.js new
+20
@@ -0,0 +1,20 @@
1 +/* Phase 6 — professional journal-derived reporting */
2 +(function(){
3 + const n=v=>Number(v)||0, rows=()=>typeof jeLedgerRows==='function'?jeLedgerRows():[];
4 + const voucherDate=r=>String(r.voucher?.date||'');
5 + const accountMap=()=>new Map((typeof getMyAccounts==='function'?getMyAccounts():[]).map(a=>[a.id,a]));
6 + function inRange(r,from,to){const d=voucherDate(r);return(!from||d>=from)&&(!to||d<=to)}
7 + function scoped(from,to){return rows().filter(r=>inRange(r,from,to))}
8 + function rollup(from,to){
9 + const m=new Map();scoped(from,to).forEach(r=>{const a=r.account||accountMap().get(r.accountId)||{},x=m.get(r.accountId)||{accountId:r.accountId,code:a.code||'',title:a.title||'',debit:0,credit:0};x.debit+=n(r.debit);x.credit+=n(r.credit);m.set(r.accountId,x)});
10 + return [...m.values()].map(x=>({...x,balance:x.debit-x.credit})).sort((a,b)=>String(a.code).localeCompare(String(b.code),undefined,{numeric:true}));
11 + }
12 + function classify(a){const s=((a.code||'')+' '+(a.title||'')).toLowerCase();if(/فروش|درآمد/.test(s))return'revenue';if(/هزینه|بهای تمام|خرید/.test(s))return'expense';if(/دریافتنی|دارایی|موجودی|بانک|صندوق/.test(s))return'asset';if(/پرداختنی|بدهی|مالیات پرداختنی/.test(s))return'liability';if(/سرمایه|حقوق صاحبان/.test(s))return'equity';return'other'}
13 + function financials(from,to){const tb=rollup(from,to);let revenue=0,expense=0,assets=0,liabilities=0,equity=0;tb.forEach(x=>{const k=classify(x);if(k==='revenue')revenue+=x.credit-x.debit;else if(k==='expense')expense+=x.debit-x.credit;else if(k==='asset')assets+=x.debit-x.credit;else if(k==='liability')liabilities+=x.credit-x.debit;else if(k==='equity')equity+=x.credit-x.debit});return{trial:tb,revenue,expense,profit:revenue-expense,assets,liabilities,equity}}
14 + function aging(asOf){const cutoff=asOf||getJalaliNumeric(),bucket=x=>x<=30?'0-30':x<=60?'31-60':x<=90?'61-90':'90+';const out={receivable:{},payable:{}};['receivable','payable'].forEach(k=>['0-30','31-60','61-90','90+'].forEach(b=>out[k][b]=0));const fy=String(cutoff).split('/').map(Number);const approx=d=>{const p=String(d||cutoff).split('/').map(Number);return Math.max(0,(fy[0]-p[0])*365+(fy[1]-p[1])*30+(fy[2]-p[2]))};scoped('',cutoff).forEach(r=>{const a=r.account||{},s=(a.code||'')+' '+(a.title||''),b=bucket(approx(r.voucher?.date));if(/دریافتنی|بدهکاران/.test(s))out.receivable[b]+=n(r.debit)-n(r.credit);if(/پرداختنی|بستانکاران/.test(s))out.payable[b]+=n(r.credit)-n(r.debit)});return out}
15 + function render(){
16 + const root=document.getElementById('p6-reporting-root');if(!root)return;const from=document.getElementById('p6-from')?.value||'',to=document.getElementById('p6-to')?.value||'',f=financials(from,to),fmt=v=>Math.round(v).toLocaleString('fa-IR'),tr=f.trial.map(x=>'<tr><td>'+esc(x.code)+'</td><td>'+esc(x.title)+'</td><td>'+fmt(x.debit)+'</td><td>'+fmt(x.credit)+'</td><td>'+fmt(x.balance)+'</td></tr>').join('');const dr=f.trial.reduce((s,x)=>s+x.debit,0),cr=f.trial.reduce((s,x)=>s+x.credit,0),a=aging(to||getJalaliNumeric());
17 + root.innerHTML='<div class="grid-kpi"><div class="kpi-unit"><div><div class="kpi-lbl">جمع بدهکار</div><div class="kpi-val">'+fmt(dr)+'</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">جمع بستانکار</div><div class="kpi-val">'+fmt(cr)+'</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">سود/زیان دفتر</div><div class="kpi-val">'+fmt(f.profit)+'</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">کنترل تراز</div><div class="kpi-val">'+(Math.abs(dr-cr)<.001?'تراز':'نامتوازن')+'</div></div></div></div><div class="card"><h3>تراز آزمایشی</h3><div class="table-responsive"><table><thead><tr><th>کد</th><th>حساب</th><th>گردش بدهکار</th><th>گردش بستانکار</th><th>مانده</th></tr></thead><tbody>'+tr+'</tbody></table></div></div><div class="card"><h3>صورت سود و زیان مبتنی بر دفتر</h3><div class="table-responsive"><table><tbody><tr><td>درآمد</td><td>'+fmt(f.revenue)+'</td></tr><tr><td>هزینه و بهای تمام‌شده</td><td>'+fmt(f.expense)+'</td></tr><tr><td><strong>سود/زیان</strong></td><td><strong>'+fmt(f.profit)+'</strong></td></tr></tbody></table></div></div><div class="card"><h3>وضعیت مالی خلاصه</h3><div class="table-responsive"><table><tbody><tr><td>دارایی</td><td>'+fmt(f.assets)+'</td></tr><tr><td>بدهی</td><td>'+fmt(f.liabilities)+'</td></tr><tr><td>حقوق مالکانه</td><td>'+fmt(f.equity)+'</td></tr></tbody></table></div></div><div class="card"><h3>سن مطالبات / بدهی‌ها</h3><div class="table-responsive"><table><thead><tr><th>نوع</th><th>۰–۳۰</th><th>۳۱–۶۰</th><th>۶۱–۹۰</th><th>۹۰+</th></tr></thead><tbody><tr><td>مطالبات</td><td>'+fmt(a.receivable['0-30'])+'</td><td>'+fmt(a.receivable['31-60'])+'</td><td>'+fmt(a.receivable['61-90'])+'</td><td>'+fmt(a.receivable['90+'])+'</td></tr><tr><td>بدهی</td><td>'+fmt(a.payable['0-30'])+'</td><td>'+fmt(a.payable['31-60'])+'</td><td>'+fmt(a.payable['61-90'])+'</td><td>'+fmt(a.payable['90+'])+'</td></tr></tbody></table></div></div>';
18 + }
19 + window.P6Reports={rollup,financials,aging,render};
20 +})();