سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 01:45 UTC
feat: route sales finalization through server autopost RPC
davoodmehraban89 committed
Nov 17, 2026 at 13:52 UTC
984c66f3f4229f2c86fd430cfa5af44773351454
1 file changed
+15
-25
js/invoices.js
+15
-25
@@ -390,31 +390,21 @@ async function finalizeInvoiceDocument(invoiceId){
390
const activeCompany=typeof afCompanyId==='function'?afCompanyId():getMySettings().default_company_id;
391
if(!activeCompany||inv.companyId!==activeCompany)throw new Error('ابتدا شرکت ثبتکننده فاکتور را در سربرگ انتخاب کنید.');
392
if(!Array.isArray(inv.items)||!inv.items.length||!Number.isFinite(inv.grandTotal)||inv.grandTotal<=0)throw new Error('ردیفها و مبلغ فاکتور معتبر نیست.');
393
- // The source remains a draft if accounting validation or posting rejects it.
394
- const v=jePostSourceRecord('sale',inv);
395
- inv.documentStatus='posted';
396
- inv.documentPostedAt=v.postedAt||new Date().toISOString();
397
- inv.documentPostedBy=currentUser.id;
398
- inv.documentVersion=Number(inv.documentVersion||1);
399
- if(typeof syncProductDefaultPrices==='function')syncProductDefaultPrices(inv.items,'sale');
400
- saveDatastore();
401
- // Only report financial finalization as successful after the atomic sync RPC has
402
- // acknowledged both the source invoice and its voucher on the server.
403
- if(typeof runSync==='function'&&typeof syncSnap!=='undefined'){
404
- await runSync();
405
- const deadline=Date.now()+20000;
406
- let saved=false;
407
- while(Date.now()<deadline){
408
- const savedInvoice=getMyInvoices().find(x=>x.id===invoiceId);
409
- saved=!syncRunning&&!syncPending&&
410
- syncSnap.has('journalVouchers|'+v.id)&&syncSnap.has('invoices|'+invoiceId)&&
411
- savedInvoice?.journalVoucherId===v.id&&invoiceDocumentStatus(savedInvoice)==='posted';
412
- if(saved)break;
413
- if(syncFailCount>0)break;
414
- await new Promise(resolve=>setTimeout(resolve,200));
415
- }
416
- if(!saved)throw new Error('تأیید ذخیره قطعی از سرور دریافت نشد. وضعیت را دوباره بارگذاری و بررسی کنید.');
417
- }
393
+ // Persist the source draft first, then let the server derive and post its
394
+ // accounting lines atomically. The journal itself never goes through generic sync.
395
+ const v=await jeRpcAutoPost('sale',invoiceId);
396
+ const savedInvoice=getMyInvoices().find(x=>x.id===invoiceId);
397
+ if(!savedInvoice)throw new Error('فاکتور پس از ثبت حسابداری از سرور بارگذاری نشد؛ پیش از تکرار وضعیت سند را بررسی کنید.');
398
+ savedInvoice.journalVoucherId=v.id;
399
+ savedInvoice.accountingVersion=Number(savedInvoice.accountingVersion||1);
400
+ savedInvoice.accountingStatus='posted';
401
+ savedInvoice.accountingPostedAt=v.postedAt||new Date().toISOString();
402
+ savedInvoice.documentStatus='posted';
403
+ savedInvoice.documentPostedAt=v.postedAt||new Date().toISOString();
404
+ savedInvoice.documentPostedBy=currentUser.id;
405
+ savedInvoice.documentVersion=Number(savedInvoice.documentVersion||1);
406
+ if(typeof syncProductDefaultPrices==='function')syncProductDefaultPrices(savedInvoice.items,'sale');
407
+ await jeRpcPersistSourceRecord('invoices',invoiceId);
408
refreshAllSurfaces();
409
return v.id;
410
}