سامانه صدور فاکتور و حسابداری
fix: restore invoice module and route draft deletion atomically
davoodmehraban89 committed
Nov 18, 2026 at 09:04 UTC
effef1ae7c6cd24f559d19aa9bbeb01b63a7295d
1 file changed
+88
-1
js/invoices.js
+88
-1
@@ -649,4 +649,91 @@ function editInvoice(invId,allowPostedCorrection=false){
649
document.getElementById('invoice-vat-rate-input').value=inv.vatRate||10;
650
document.getElementById('invoice-number').value=invoiceVisibleNumber(inv);
651
document.getElementById('invoice-contact-id').value=inv.contactId;
652
- document.getElementById('invoice-date-input').value=inv.date;
\ No newline at end of file
652
+ document.getElementById('invoice-date-input').value=inv.date;
653
+ if(document.getElementById('invoice-payment-method'))document.getElementById('invoice-payment-method').value=inv.paymentMethod||'cash';if(document.getElementById('invoice-warehouse-id'))document.getElementById('invoice-warehouse-id').value=inv.warehouseId||p5DefaultWarehouse()?.id||'';
654
+ document.getElementById('invoice-discount-type').value=inv.discountType||'fixed';
655
+ document.getElementById('label-invoice-discount-value').innerText=(inv.discountType==='percent')?'درصد تخفیف (٪)':`مبلغ تخفیف (${getCurrencyLabel()})`;
656
+ document.getElementById('invoice-discount-fixed').value=(inv.discountType==='percent')?(inv.discountInput||0):(inv.discount||0);
657
+ document.getElementById('invoice-desc-input').value=inv.description||'';
658
+ const contractInput=document.getElementById('invoice-contract-number');
659
+ if(contractInput)contractInput.value=inv.contractNumber||'';
660
+ handleInvoiceKindChange();
661
+ if(typeof handleInvoiceContactChange==='function'){
662
+ handleInvoiceContactChange();
663
+ const projSel=document.getElementById('invoice-project-id');
664
+ if(projSel&&inv.projectId)projSel.value=inv.projectId;
665
+ }
666
+ // بارگذاری سند الکترونیکی در صورت وجود
667
+ window.pendingElectronicDoc=inv.electronicDoc||null;
668
+ const cb=document.getElementById('enable-electronic-doc');
669
+ if(cb){
670
+ if(inv.kind==='formal'&&inv.electronicDoc){
671
+ cb.checked=true;
672
+ document.getElementById('edoc-status').innerText='✓ اطلاعات سند الکترونیکی قبلاً ذخیره شده است.';
673
+ }else{
674
+ cb.checked=false;
675
+ document.getElementById('edoc-status').innerText='';
676
+ }
677
+ }
678
+ const tbody=document.getElementById('invoice-items-table-body');
679
+ tbody.innerHTML='';
680
+ inv.items.forEach(it=>{
681
+ addInvoiceItemRow(it.prodId,it.qty,it.price);
682
+ const lastRow=tbody.lastElementChild;
683
+ if(lastRow)lastRow.querySelector('.row-unit-input').value=it.unit||'عدد';
684
+ });
685
+ updateInvoiceItemRowNumbers();
686
+ recomputeTotals();
687
+}
688
+function cancelInvoiceEdit(){
689
+ finoraSaleAmendmentId='';
690
+ document.getElementById('edit-invoice-id').value='';
691
+ document.getElementById('invoice-form-title').innerText='صدور فاکتور جدید';
692
+ document.getElementById('btn-cancel-edit').style.display='none';
693
+ document.getElementById('btn-save-invoice').innerText='💾 ثبت فاکتور';
694
+ document.getElementById('invoice-items-table-body').innerHTML='';
695
+ document.getElementById('invoice-number').value='';
696
+ document.getElementById('invoice-discount-type').value='fixed';
697
+ document.getElementById('label-invoice-discount-value').innerText=`مبلغ تخفیف (${getCurrencyLabel()})`;
698
+ document.getElementById('invoice-discount-fixed').value=0;
699
+ document.getElementById('invoice-vat-type').value='percent';
700
+ document.getElementById('invoice-desc-input').value='';
701
+ if(document.getElementById('invoice-payment-method'))document.getElementById('invoice-payment-method').value='cash';
702
+ const contractInput=document.getElementById('invoice-contract-number');
703
+ if(contractInput)contractInput.value='';
704
+ const contractBox=document.getElementById('box-invoice-contract-number');
705
+ if(contractBox)contractBox.style.display='none';
706
+ const projectRow=document.getElementById('box-invoice-project-row');
707
+ if(projectRow)projectRow.style.display='none';
708
+ window.pendingElectronicDoc=null;
709
+ const cb=document.getElementById('enable-electronic-doc');
710
+ if(cb)cb.checked=false;
711
+ const st=document.getElementById('edoc-status');
712
+ if(st)st.innerText='';
713
+ addInvoiceItemRow();
714
+ applyDefaultSettingsToForm();
715
+}
716
+function renderInvoices(){
717
+ const tbody=document.getElementById('invoice-archive-table-body');
718
+ if(!tbody)return;
719
+ // Filtering is authoritative from Settings; no independent ALL/stale filter.
720
+ const list=invoiceVisibleRecords();
721
+ if(list.length===0){tbody.innerHTML='<tr><td colspan="7" style="text-align:center;color:var(--text-muted);padding:20px">هیچ فاکتوری ثبت نشده است.</td></tr>';return;}
722
+ tbody.innerHTML=list.map(inv=>`
723
+ <tr>
724
+ <td><strong>#${esc(invoiceVisibleNumber(inv))}</strong></td>
725
+ <td>${esc(inv.companyName||'—')}</td>
726
+ <td>${esc(inv.contactName)}${inv.projectName?' <span class="badge badge-info">'+esc(inv.projectName)+'</span>':''}</td>
727
+ <td><span class="badge ${inv.kind==='formal'?'badge-danger':(inv.kind==='contract_statement'?'badge-warning':'badge-success')}">${esc(inv.kindLabel)}</span></td>
728
+ <td>${esc(inv.date)}</td>
729
+ <td>${inv.grandTotal.toLocaleString('fa-IR')}<div class="af-hint">${esc(invoiceDocumentStatusLabel(inv))}</div></td>
730
+ <td>
731
+ <button class="btn btn-secondary btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="navigateShell('accounting','view-journal','documents')">▤ مدیریت سند</button>${invoiceMayEdit(inv)?` <button class="btn btn-secondary btn-inline" onclick="editInvoice('${inv.id}')">✏️ ویرایش</button>` : (invoiceDocumentStatus(inv)==='posted'?` <button class="btn btn-secondary btn-inline" onclick="beginInvoiceAmendment('${inv.id}')">✏️ اصلاح</button>`:'')}
732
+ <button class="btn btn-secondary btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="renderAndPrintDirect('${inv.id}')">🖨️ چاپ</button>
733
+ ${inv.electronicDoc?`<button class="btn btn-primary btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="printElectronicDoc('${inv.id}')">📄 سند مودیان</button>`:''}
734
+ </td>
735
+ </tr>`).join('');
736
+}
737
+
738
+function finoraEnsureInitialInvoiceRow(){const body=document.getElementById('invoice-items-table-body');if(body&&!body.querySelector('tr')&&!document.getElementById('edit-invoice-id')?.value)addInvoiceItemRow()}
739
+if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',finoraEnsureInitialInvoiceRow,{once:true});else finoraEnsureInitialInvoiceRow();
\ No newline at end of file