@davoodmehraban89 / Factor-easy / commits / 876593f
Fork 0

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

Add account balances and report drill-down details

davoodmehraban89 committed Sep 28, 2026 at 13:42 UTC 876593fda78c0175385ed3dd3f0d180375deb483
1 file changed +32 -3
js/drilldown.js
+32 -3
@@ -7,11 +7,40 @@ function rowsTable(rows){return `<div class="table-responsive"><table><thead><tr
7 function invAction(i){return `<button class="btn btn-secondary btn-inline" onclick="closeFinoraDrill();editInvoice('${i.id}')">باز کردن</button> <button class="btn btn-secondary btn-inline" onclick="renderAndPrintDirect('${i.id}')">چاپ</button>`;}
8 function purAction(p){return `<button class="btn btn-secondary btn-inline" onclick="closeFinoraDrill();purEdit('${p.id}')">باز کردن</button>`;}
9 function chqAction(q){return `<button class="btn btn-secondary btn-inline" onclick="closeFinoraDrill();editCheque('${q.id}')">باز کردن</button>`;}
10 -window.openContactLedger=function(contactId){const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;const rows=[];getMyInvoices().filter(i=>i.contactId===contactId).forEach(i=>rows.push({date:i.date,type:'فروش',ref:i.number,name:(i.projectName?i.projectName+' — ':'')+(i.description||c.name),amount:i.grandTotal,action:invAction(i)}));(datastore.purchases||[]).filter(p=>p.ownerUserId===currentUser.id&&p.supplierId===contactId).forEach(p=>rows.push({date:p.date,type:'خرید',ref:p.number,name:(p.costCenterLabel?p.costCenterLabel+' — ':'')+(p.description||c.name),amount:p.grandTotal,action:purAction(p)}));getMyCheques().filter(q=>q.contactId===contactId||(!q.contactId&&q.contactName===c.name)).forEach(q=>rows.push({date:q.due_date,type:q.direction==='inbound'?'چک دریافتی':'چک پرداختی',ref:q.sayad_id,name:q.bank||c.name,amount:q.amount,action:chqAction(q)}));const sales=rows.filter(r=>r.type==='فروش').reduce((a,r)=>a+r.amount,0),purchases=rows.filter(r=>r.type==='خرید').reduce((a,r)=>a+r.amount,0);show('مرور حساب — '+c.name,`<div class="grid-kpi" style="margin-bottom:12px"><div class="kpi-unit"><div><div class="kpi-lbl">فروش</div><div class="kpi-val">${fmt(sales)}</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">خرید</div><div class="kpi-val">${fmt(purchases)}</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">تعداد اسناد مرتبط</div><div class="kpi-val">${rows.length.toLocaleString('fa-IR')}</div></div></div></div>${rowsTable(rows)}`);}
10 +window.openContactLedger=function(contactId){
11 + const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;
12 + const rows=[];
13 + getMyInvoices().filter(i=>i.contactId===contactId).forEach(i=>rows.push({date:i.date,type:'فروش '+(i.paymentMethod==='credit'?'نسیه':'نقدی'),ref:i.number,name:(i.projectName?i.projectName+' — ':'')+(i.description||c.name),amount:i.grandTotal,action:invAction(i)}));
14 + (datastore.purchases||[]).filter(p=>p.ownerUserId===currentUser.id&&p.supplierId===contactId).forEach(p=>rows.push({date:p.date,type:'خرید '+(p.paymentMethod==='credit'?'نسیه':'نقدی'),ref:p.number,name:(p.costCenterLabel?p.costCenterLabel+' — ':'')+(p.description||c.name),amount:p.grandTotal,action:purAction(p)}));
15 + getMyCheques().filter(q=>q.contactId===contactId||(!q.contactId&&q.contactName===c.name)).forEach(q=>rows.push({date:q.due_date,type:q.direction==='inbound'?'چک دریافتی':'چک پرداختی',ref:q.sayad_id,name:q.bank||c.name,amount:q.amount,action:chqAction(q)}));
16 + const sales=rows.filter(r=>r.type.startsWith('فروش')).reduce((a,r)=>a+r.amount,0);
17 + const purchases=rows.filter(r=>r.type.startsWith('خرید')).reduce((a,r)=>a+r.amount,0);
18 + const receivable=getMyInvoices().filter(i=>i.contactId===contactId&&i.paymentMethod==='credit').reduce((a,i)=>a+Number(i.grandTotal||0),0);
19 + const payable=(datastore.purchases||[]).filter(p=>p.ownerUserId===currentUser.id&&p.supplierId===contactId&&p.paymentMethod==='credit').reduce((a,p)=>a+Number(p.grandTotal||0),0);
20 + const net=receivable-payable;
21 + show('مرور حساب — '+c.name,`<div class="grid-kpi" style="margin-bottom:12px"><div class="kpi-unit"><div><div class="kpi-lbl">فروش</div><div class="kpi-val">${fmt(sales)}</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">خرید</div><div class="kpi-val">${fmt(purchases)}</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">مطالبات</div><div class="kpi-val">${fmt(receivable)}</div></div></div><div class="kpi-unit"><div><div class="kpi-lbl">بدهی</div><div class="kpi-val">${fmt(payable)}</div></div></div></div><div style="padding:10px 12px;margin-bottom:12px;border:1px solid var(--border);border-radius:10px"><b>مانده خالص:</b> ${fmt(Math.abs(net))} — ${net>0?'بستانکار از طرف حساب':net<0?'بدهکار به طرف حساب':'تسویه'}</div>${rowsTable(rows)}`);
22 +}
23 window.openDashboardDetail=function(kind){let title='',rows=[];const inv=getMyInvoices();if(kind==='sales'||kind==='settled'||kind==='debtors'){title=kind==='sales'?'جزئیات فروش':kind==='settled'?'فروش/دریافت وصول‌شده':'مطالبات نسیه';inv.filter(i=>kind==='sales'||(kind==='settled'&&i.paymentMethod!=='credit')||(kind==='debtors'&&i.paymentMethod==='credit')).forEach(i=>rows.push({date:i.date,type:i.kindLabel||'فاکتور',ref:i.number,name:(i.contactName||'')+(i.projectName?' — '+i.projectName:''),amount:i.grandTotal,action:invAction(i)}));}else{title='چک‌های باز صیادی';getMyCheques().filter(q=>q.status==='registered').forEach(q=>rows.push({date:q.due_date,type:q.direction==='inbound'?'دریافتی':'پرداختی',ref:q.sayad_id,name:q.contactName,amount:q.amount,action:chqAction(q)}));}show(title,rowsTable(rows));}
12 -function wire(){[['kpi-sales-sum','sales'],['kpi-settled-sum','settled'],['kpi-debtors-sum','debtors'],['kpi-cheques-count','cheques']].forEach(([id,k])=>{const card=$(id)?.closest('.kpi-unit');if(card){card.style.cursor='pointer';card.title='مشاهده جزئیات';card.onclick=()=>openDashboardDetail(k);}});document.querySelectorAll('#report-top-customers tr').forEach(tr=>{const name=tr.cells?.[0]?.textContent;if(!name)return;const c=getMyContacts().find(x=>x.name===name);if(c){tr.style.cursor='pointer';tr.title='مرور حساب';tr.onclick=()=>openContactLedger(c.id);}});}
24 +window.openReportDetail=function(kind){
25 + const y=parseInt($('report-year-select')?.value||0);
26 + const parsed=getMyInvoices().map(i=>{const d=String(i.date||'').replace(/[۰-۹]/g,x=>'۰۱۲۳۴۵۶۷۸۹'.indexOf(x));const p=d.split('/');return {i,year:parseInt(p[0]),month:parseInt(p[1])};}).filter(x=>x.year===y);
27 + let list=parsed,title='جزئیات گزارش';
28 + if(kind==='best'&&parsed.length){
29 + const sums=Array(12).fill(0);parsed.forEach(x=>{if(x.month>=1&&x.month<=12)sums[x.month-1]+=Number(x.i.grandTotal||0);});
30 + const m=sums.indexOf(Math.max(...sums))+1;list=parsed.filter(x=>x.month===m);title='فاکتورهای پرفروش‌ترین ماه';
31 + }else if(kind==='year')title='فاکتورهای سال انتخاب‌شده';
32 + else if(kind==='avg')title='اسناد تشکیل‌دهنده میانگین ماهانه';
33 + else if(kind==='count')title='فهرست فاکتورهای سال';
34 + const rows=list.map(x=>({date:x.i.date,type:x.i.kindLabel||'فاکتور',ref:x.i.number,name:(x.i.contactName||'')+(x.i.projectName?' — '+x.i.projectName:''),amount:x.i.grandTotal,action:invAction(x.i)}));
35 + show(title,rowsTable(rows));
36 +};
37 +function wire(){
38 + [['kpi-sales-sum','sales'],['kpi-settled-sum','settled'],['kpi-debtors-sum','debtors'],['kpi-cheques-count','cheques']].forEach(([id,k])=>{const card=$(id)?.closest('.kpi-unit');if(card){card.style.cursor='pointer';card.title='مشاهده جزئیات';card.onclick=()=>openDashboardDetail(k);}});
39 + [['rep-year-total','year'],['rep-year-avg','avg'],['rep-best-month','best'],['rep-invoice-count','count']].forEach(([id,k])=>{const card=$(id)?.closest('.kpi-unit');if(card){card.style.cursor='pointer';card.title='مشاهده اسناد تشکیل‌دهنده';card.onclick=()=>openReportDetail(k);}});
40 + document.querySelectorAll('#report-top-customers tr').forEach(tr=>{const name=tr.cells?.[0]?.textContent;if(!name)return;const c=getMyContacts().find(x=>x.name===name);if(c){tr.style.cursor='pointer';tr.title='مرور حساب';tr.onclick=()=>openContactLedger(c.id);}});
41 +}
42 const rd=window.renderDashboard;window.renderDashboard=function(){rd();wire();const tb=$('dashboard-recent-table');if(tb)tb.querySelectorAll('tr').forEach(tr=>{const num=(tr.cells?.[0]?.textContent||'').replace(/^#/,'');const i=getMyInvoices().find(x=>String(x.number)===num);if(i&&tr.lastElementChild&&!tr.lastElementChild.querySelector('.dash-open')){const b=document.createElement('button');b.className='btn btn-secondary btn-inline dash-open';b.style.cssText='padding:4px 8px;font-size:11px;margin-left:4px';b.textContent='جزئیات';b.onclick=()=>editInvoice(i.id);tr.lastElementChild.prepend(b);}});};
43 const rr=window.renderFinancialReports;if(typeof rr==='function')window.renderFinancialReports=function(){rr();wire();};
15 -const rc=window.renderContacts;window.renderContacts=function(){rc();const cs=getMyContacts(),trs=document.querySelectorAll('#contacts-ledger-table-body tr');trs.forEach((tr,i)=>{const c=cs[i];if(!c||!tr.cells||tr.cells.length<7)return;tr.cells[0].style.cursor='pointer';tr.cells[0].title='مرور حساب';tr.cells[0].onclick=()=>openContactLedger(c.id);tr.cells[6].innerHTML=`<button class="btn btn-secondary btn-inline" style="padding:2px 7px;font-size:11px" onclick="openContactLedger('${c.id}')">مرور حساب</button>`;});};
44 +const rc=window.renderContacts;window.renderContacts=function(){rc();const cs=getMyContacts(),trs=document.querySelectorAll('#contacts-ledger-table-body tr');trs.forEach((tr,i)=>{const c=cs[i];if(!c||!tr.cells||tr.cells.length<7)return;tr.cells[0].style.cursor='pointer';tr.cells[0].title='مرور حساب';tr.cells[0].onclick=()=>openContactLedger(c.id);const rec=getMyInvoices().filter(i=>i.contactId===c.id&&i.paymentMethod==='credit').reduce((a,i)=>a+Number(i.grandTotal||0),0);const pay=(datastore.purchases||[]).filter(p=>p.ownerUserId===currentUser.id&&p.supplierId===c.id&&p.paymentMethod==='credit').reduce((a,p)=>a+Number(p.grandTotal||0),0);const net=rec-pay;tr.cells[6].innerHTML=`<div style="font-weight:bold">${fmt(Math.abs(net))}</div><button class="btn btn-secondary btn-inline" style="padding:2px 7px;font-size:11px;margin-top:3px" onclick="openContactLedger('${c.id}')">${net>0?'مطالبات':net<0?'بدهی':'مرور حساب'} ↗</button>`;});};
45 try{if(currentUser)refreshAllSurfaces();}catch(e){console.error('drilldown init',e);}
46 })();
\ No newline at end of file