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

fix(invoices): strictly scope visible invoice archive and save/edit to Settings company

davoodmehraban89 committed Oct 14, 2026 at 12:28 UTC 55832bef63188a1cb33427c5aa88f761d2c18231
1 file changed +21 -10
js/invoices.js
+21 -10
@@ -95,9 +95,10 @@ function renderDashboard(){
95 const body=document.getElementById('dashboard-recent-journals');if(body){const vs=typeof getMyJournalVouchers==='function'?getMyJournalVouchers().filter(companyOk).slice().sort((a,b)=>String(b.createdAt||b.date||'').localeCompare(String(a.createdAt||a.date||''))).slice(0,5):[];body.innerHTML=vs.length?vs.map(v=>'<tr><td>'+esc(v.number||'—')+'</td><td>'+esc(v.date||'—')+'</td><td>'+esc(v.description||'—')+'</td><td>'+Number(v.totalDebit||0).toLocaleString('fa-IR')+'</td><td>'+Number(v.totalCredit||0).toLocaleString('fa-IR')+'</td><td><span class="badge '+(v.status==='posted'?'badge-success':v.status==='reversed'?'badge-danger':'badge-warning')+'">'+esc(v.status==='posted'?'قطعی':v.status==='reversed'?'برگشت‌شده':'پیش‌نویس')+'</span></td></tr>').join(''):'<tr><td colspan="6" class="af-empty">سند حسابداری ثبت نشده است.</td></tr>'}
96 }
97 function onActiveCompanyFilterChange(){
98 - const activeCompId=document.getElementById('invoice-company-id').value;
99 - const filterSelect=document.getElementById('filter-invoice-company');
100 - if(filterSelect)filterSelect.value=activeCompId;
98 + // The invoicing seller follows Settings and cannot change invoice scope.
99 + const active=typeof afCompanyId==='function'?afCompanyId():'';
100 + const header=document.getElementById('invoice-company-id');
101 + if(header&&header.value!==active)header.value=active;
102 renderInvoices();renderDashboard();
103 }
104 function startNewInvoice(){cancelInvoiceEdit();switchView('view-invoices');}
@@ -306,8 +307,10 @@ function invoiceLogicalRoot(inv){
307 }
308 function invoiceVisibleNumber(inv){return invoiceLogicalRoot(inv)?.number||inv?.number||'';}
309 function invoiceVisibleRecords(){
310 + const active=typeof afCompanyId==='function'?afCompanyId():getMySettings().default_company_id;
311 + if(!active||!getMyCompanies().some(c=>c.id===active))return [];
312 const all=getMyInvoices(),children=new Set(all.map(x=>x.correctsInvoiceId).filter(Boolean));
310 - return all.filter(x=>!children.has(x.id));
313 + return all.filter(x=>x.companyId===active&&!children.has(x.id));
314 }
315 function invoiceCurrentVisible(invId){
316 const all=getMyInvoices();let current=all.find(x=>x.id===invId);
@@ -504,8 +507,14 @@ function commitSaveInvoice(){
507 const mySettings=getMySettings();
508 const companySelect=document.getElementById('invoice-company-id');
509 const selectedCompanyId=companySelect?companySelect.value:mySettings.default_company_id;
507 - const sellerCompany=myCompanies.find(c=>c.id===selectedCompanyId)||myCompanies.find(c=>c.id===mySettings.default_company_id)||myCompanies[0];
508 - if(!sellerCompany){alert('لطفاً ابتدا حداقل یک شرکت در بخش تنظیمات ثبت فرمایید.');return;}
510 + const activeCompanyId=typeof afCompanyId==='function'?afCompanyId():mySettings.default_company_id;
511 + const sellerCompany=myCompanies.find(c=>c.id===activeCompanyId);
512 + if(!sellerCompany){alert('ابتدا شرکت فعال را در بخش تنظیمات انتخاب کنید.');return;}
513 + if(selectedCompanyId!==sellerCompany.id){alert('شرکت فاکتور با شرکت فعال در تنظیمات یکسان نیست.');return;}
514 + const editSource=getMyInvoices().find(i=>i.id===document.getElementById('edit-invoice-id')?.value);
515 + if(editSource&&editSource.companyId!==sellerCompany.id){
516 + alert('برای اصلاح این فاکتور، شرکت مربوطه را از تنظیمات انتخاب کنید.');return;
517 + }
518 const companyId=sellerCompany.id;
519 const companyName=sellerCompany.name;
520 const contactSelect=document.getElementById('invoice-contact-id');
@@ -614,6 +623,9 @@ function commitSaveInvoice(){
623 function editInvoice(invId,allowPostedCorrection=false){
624 const inv=getMyInvoices().find(i=>i.id===invId);
625 if(!inv)return;
626 + if(!afCompanyId()||inv.companyId!==afCompanyId()){
627 + alert('برای مشاهده این فاکتور، ابتدا شرکت مربوطه را در تنظیمات فعال کنید.');return;
628 + }
629 if(!invoiceMayEdit(inv)&&!(allowPostedCorrection&&invoiceDocumentStatus(inv)==='posted')){alert('فاکتور برای اصلاح در این وضعیت قابل دسترس نیست.');return;}
630 switchView('view-invoices');
631 document.getElementById('edit-invoice-id').value=inv.id;
@@ -693,10 +705,9 @@ function cancelInvoiceEdit(){
705 }
706 function renderInvoices(){
707 const tbody=document.getElementById('invoice-archive-table-body');
696 - const filterSelect=document.getElementById('filter-invoice-company');
697 - const selectedFilter=filterSelect?.value||document.getElementById('invoice-company-id')?.value||'ALL';
698 - let list=invoiceVisibleRecords();
699 - if(selectedFilter!=='ALL')list=list.filter(i=>i.companyId===selectedFilter);
708 + if(!tbody)return;
709 + // Filtering is authoritative from Settings; no independent ALL/stale filter.
710 + const list=invoiceVisibleRecords();
711 if(list.length===0){tbody.innerHTML='<tr><td colspan="7" style="text-align:center;color:var(--text-muted);padding:20px">هیچ فاکتوری ثبت نشده است.</td></tr>';return;}
712 tbody.innerHTML=list.map(inv=>`
713 <tr>