سامانه صدور فاکتور و حسابداری
Add dashboard and account drill-down navigation
davoodmehraban89 committed
Sep 28, 2026 at 12:49 UTC
93c23dcf72c7c669be65eb9799251047379a6a5e
1 file changed
+17
js/drilldown.js
new
+17
@@ -0,0 +1,17 @@
1
+(function(){
2
+const $=id=>document.getElementById(id),fmt=n=>Number(n||0).toLocaleString('fa-IR');
3
+function modal(){if($('finora-drill-modal'))return;const d=document.createElement('div');d.id='finora-drill-modal';d.className='modal-backdrop';d.innerHTML='<div class="modal-card" style="max-width:1000px;max-height:85vh;overflow:auto"><div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px"><h3 id="finora-drill-title"></h3><button class="btn btn-secondary btn-inline" onclick="closeFinoraDrill()">✕ بستن</button></div><div id="finora-drill-body"></div></div>';document.body.appendChild(d);}
4
+window.closeFinoraDrill=function(){if($('finora-drill-modal'))$('finora-drill-modal').classList.remove('active');};
5
+function show(title,html){modal();$('finora-drill-title').innerText=title;$('finora-drill-body').innerHTML=html;$('finora-drill-modal').classList.add('active');}
6
+function rowsTable(rows){return `<div class="table-responsive"><table><thead><tr><th>تاریخ</th><th>نوع</th><th>مرجع</th><th>طرف حساب / پروژه</th><th>مبلغ</th><th>عملیات</th></tr></thead><tbody>${rows.length?rows.map(r=>`<tr><td>${esc(r.date||'—')}</td><td>${esc(r.type||'—')}</td><td>${esc(r.ref||'—')}</td><td>${esc(r.name||'—')}</td><td>${fmt(r.amount)}</td><td>${r.action||''}</td></tr>`).join(''):'<tr><td colspan="6" style="text-align:center;padding:18px">موردی وجود ندارد.</td></tr>'}</tbody></table></div>`;}
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)}`);}
11
+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);}});}
13
+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);}});};
14
+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>`;});};
16
+try{if(currentUser)refreshAllSurfaces();}catch(e){console.error('drilldown init',e);}
17
+})();
\ No newline at end of file