@davoodmehraban89 / Factor-easy / commits / ab5c306
Fork 0

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

feat: delete invoice drafts through atomic lifecycle RPC

davoodmehraban89 committed Nov 18, 2026 at 09:00 UTC ab5c306aea0d9a0f7b342187f70055393c383eaf
1 file changed +8 -93
js/invoices.js
+8 -93
@@ -497,16 +497,18 @@ async function cancelPostedInvoicePilot(invoiceId,reason){
497 await invoicePilotAwaitSync(['journalVouchers|'+v.id,'journalVouchers|'+reversal.id]);
498 refreshAllSurfaces();return reversal.id;
499 }
500 -function deleteInvoicePilotDraft(invoiceId){
500 +async async function deleteInvoicePilotDraft(invoiceId){
501 if(!requireWrite())throw new Error('مجوز حذف ندارید.');
502 const inv=getMyInvoices().find(x=>x.id===invoiceId);
503 - if(!inv||inv.ownerUserId!==currentUser?.id||inv.companyId!==afCompanyId()||invoiceDocumentStatus(inv)!=='draft'||inv.journalVoucherId||inv.accountingPostingPending)
504 - throw new Error('فقط پیش‌نویس ثبت‌نشده همین شرکت قابل حذف است.');
503 + if(!inv||inv.ownerUserId!==currentUser?.id||inv.companyId!==afCompanyId()||invoiceDocumentStatus(inv)!=='draft')
504 + throw new Error('فقط پیش‌نویس همین شرکت قابل حذف است.');
505 + if(inv.accountingPostingPending)throw new Error('نتیجه ثبت حسابداری این فاکتور هنوز مشخص نیست؛ ابتدا وضعیت را تطبیق دهید.');
506 if(inv.electronicDoc)throw new Error('سند الکترونیکی مرتبط ابتدا باید بررسی شود.');
507 if(typeof getMyPayments==='function'&&getMyPayments().some(p=>p.invoiceId===inv.id||p.sourceInvoiceId===inv.id||p.relatedInvoiceId===inv.id))
508 throw new Error('این فاکتور ارجاع دریافت/پرداخت دارد و قابل حذف نیست.');
508 - datastore.invoices=datastore.invoices.filter(x=>x.id!==inv.id);
509 - saveDatastore();refreshAllSurfaces();return true;
509 + const result=await jeRpcDeleteSourceDraft('invoices',invoiceId);
510 + refreshAllSurfaces();
511 + return result;
512 }
513 function commitSaveInvoice(){
514 if(!requireWrite())return;
@@ -647,91 +649,4 @@ 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;
650 - document.getElementById('invoice-date-input').value=inv.date;
651 - 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||'';
652 - document.getElementById('invoice-discount-type').value=inv.discountType||'fixed';
653 - document.getElementById('label-invoice-discount-value').innerText=(inv.discountType==='percent')?'درصد تخفیف (٪)':`مبلغ تخفیف (${getCurrencyLabel()})`;
654 - document.getElementById('invoice-discount-fixed').value=(inv.discountType==='percent')?(inv.discountInput||0):(inv.discount||0);
655 - document.getElementById('invoice-desc-input').value=inv.description||'';
656 - const contractInput=document.getElementById('invoice-contract-number');
657 - if(contractInput)contractInput.value=inv.contractNumber||'';
658 - handleInvoiceKindChange();
659 - if(typeof handleInvoiceContactChange==='function'){
660 - handleInvoiceContactChange();
661 - const projSel=document.getElementById('invoice-project-id');
662 - if(projSel&&inv.projectId)projSel.value=inv.projectId;
663 - }
664 - // بارگذاری سند الکترونیکی در صورت وجود
665 - window.pendingElectronicDoc=inv.electronicDoc||null;
666 - const cb=document.getElementById('enable-electronic-doc');
667 - if(cb){
668 - if(inv.kind==='formal'&&inv.electronicDoc){
669 - cb.checked=true;
670 - document.getElementById('edoc-status').innerText='✓ اطلاعات سند الکترونیکی قبلاً ذخیره شده است.';
671 - }else{
672 - cb.checked=false;
673 - document.getElementById('edoc-status').innerText='';
674 - }
675 - }
676 - const tbody=document.getElementById('invoice-items-table-body');
677 - tbody.innerHTML='';
678 - inv.items.forEach(it=>{
679 - addInvoiceItemRow(it.prodId,it.qty,it.price);
680 - const lastRow=tbody.lastElementChild;
681 - if(lastRow)lastRow.querySelector('.row-unit-input').value=it.unit||'عدد';
682 - });
683 - updateInvoiceItemRowNumbers();
684 - recomputeTotals();
685 -}
686 -function cancelInvoiceEdit(){
687 - finoraSaleAmendmentId='';
688 - document.getElementById('edit-invoice-id').value='';
689 - document.getElementById('invoice-form-title').innerText='صدور فاکتور جدید';
690 - document.getElementById('btn-cancel-edit').style.display='none';
691 - document.getElementById('btn-save-invoice').innerText='💾 ثبت فاکتور';
692 - document.getElementById('invoice-items-table-body').innerHTML='';
693 - document.getElementById('invoice-number').value='';
694 - document.getElementById('invoice-discount-type').value='fixed';
695 - document.getElementById('label-invoice-discount-value').innerText=`مبلغ تخفیف (${getCurrencyLabel()})`;
696 - document.getElementById('invoice-discount-fixed').value=0;
697 - document.getElementById('invoice-vat-type').value='percent';
698 - document.getElementById('invoice-desc-input').value='';
699 - if(document.getElementById('invoice-payment-method'))document.getElementById('invoice-payment-method').value='cash';
700 - const contractInput=document.getElementById('invoice-contract-number');
701 - if(contractInput)contractInput.value='';
702 - const contractBox=document.getElementById('box-invoice-contract-number');
703 - if(contractBox)contractBox.style.display='none';
704 - const projectRow=document.getElementById('box-invoice-project-row');
705 - if(projectRow)projectRow.style.display='none';
706 - window.pendingElectronicDoc=null;
707 - const cb=document.getElementById('enable-electronic-doc');
708 - if(cb)cb.checked=false;
709 - const st=document.getElementById('edoc-status');
710 - if(st)st.innerText='';
711 - addInvoiceItemRow();
712 - applyDefaultSettingsToForm();
713 -}
714 -function renderInvoices(){
715 - const tbody=document.getElementById('invoice-archive-table-body');
716 - if(!tbody)return;
717 - // Filtering is authoritative from Settings; no independent ALL/stale filter.
718 - const list=invoiceVisibleRecords();
719 - if(list.length===0){tbody.innerHTML='<tr><td colspan="7" style="text-align:center;color:var(--text-muted);padding:20px">هیچ فاکتوری ثبت نشده است.</td></tr>';return;}
720 - tbody.innerHTML=list.map(inv=>`
721 - <tr>
722 - <td><strong>#${esc(invoiceVisibleNumber(inv))}</strong></td>
723 - <td>${esc(inv.companyName||'—')}</td>
724 - <td>${esc(inv.contactName)}${inv.projectName?' <span class="badge badge-info">'+esc(inv.projectName)+'</span>':''}</td>
725 - <td><span class="badge ${inv.kind==='formal'?'badge-danger':(inv.kind==='contract_statement'?'badge-warning':'badge-success')}">${esc(inv.kindLabel)}</span></td>
726 - <td>${esc(inv.date)}</td>
727 - <td>${inv.grandTotal.toLocaleString('fa-IR')}<div class="af-hint">${esc(invoiceDocumentStatusLabel(inv))}</div></td>
728 - <td>
729 - <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>`:'')}
730 - <button class="btn btn-secondary btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="renderAndPrintDirect('${inv.id}')">🖨️ چاپ</button>
731 - ${inv.electronicDoc?`<button class="btn btn-primary btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="printElectronicDoc('${inv.id}')">📄 سند مودیان</button>`:''}
732 - </td>
733 - </tr>`).join('');
734 -}
735 -
736 -function finoraEnsureInitialInvoiceRow(){const body=document.getElementById('invoice-items-table-body');if(body&&!body.querySelector('tr')&&!document.getElementById('edit-invoice-id')?.value)addInvoiceItemRow()}
737 -if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',finoraEnsureInitialInvoiceRow,{once:true});else finoraEnsureInitialInvoiceRow();
652 + document.getElementById('invoice-date-input').value=inv.date;
\ No newline at end of file