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

fix(treasury): stage reversal and corrected payment as one debounced sync batch with local exception rollback

davoodmehraban89 committed Oct 14, 2026 at 10:55 UTC 3bd9ee191b1cabcd59335714728013723a3f1920
1 file changed +34 -14
js/payments.js
+34 -14
@@ -67,20 +67,40 @@ function paymentPreflight(patch,p){
67 async function paymentAmendPosted(id,patch){
68 const {record:p,voucher:v}=paymentEligibility(id);
69 paymentPreflight(patch,p);
70 - // The original voucher is not reversed on form OPEN/CANCEL, only on SAVE.
71 - const reversal=jeReverse(v.id,getJalaliNumeric(),'اصلاح دریافت/پرداخت دوره پایلوت');
72 - await invoicePilotAwaitSync(['journalVouchers|'+v.id,'journalVouchers|'+reversal.id]);
73 - const now=new Date().toISOString();
74 - const newer={...p,id:afId('PAY'),amount:num(patch.amount),method:patch.method,
75 - reference:String(patch.reference||''),note:String(patch.note||''),
76 - createdAt:now,updatedAt:now,accountingStatus:'draft',
77 - correctsPaymentId:p.id,correctionReversalId:reversal.id,
78 - accountingVersion:1};
79 - delete newer.journalVoucherId;delete newer.accountingPostedAt;
80 - datastore.payments.push(newer);saveDatastore();
81 - await invoicePilotAwaitSync(['payments|'+newer.id]);
82 - const posted=jePostSourceRecord(p.direction==='outbound'?'payment':'receipt',newer);
83 - await invoicePilotAwaitSync(['payments|'+newer.id,'journalVouchers|'+posted.id]);
70 + // Compose compensator + new finalized source synchronously. The debounced
71 + // finora_sync_records call receives one diff, not an intermediate reversal.
72 + // PostgreSQL RPC errors never leave a partly acknowledged accounting chain.
73 + const knownVoucherIds=new Set(datastore.journalVouchers.map(x=>x.id));
74 + const knownLineIds=new Set(datastore.journalLines.map(x=>x.id));
75 + const knownDimensionIds=new Set(datastore.journalLineDimensions.map(x=>x.id));
76 + const knownProfileIds=new Set(datastore.postingProfiles.map(x=>x.id));
77 + const oldVoucher={...v};
78 + let newer,posted,reversal;
79 + try{
80 + reversal=jeReverse(v.id,getJalaliNumeric(),'اصلاح دریافت/پرداخت دوره پایلوت');
81 + const now=new Date().toISOString();
82 + newer={...p,id:afId('PAY'),amount:num(patch.amount),method:patch.method,
83 + reference:String(patch.reference||''),note:String(patch.note||''),
84 + createdAt:now,updatedAt:now,accountingStatus:'draft',
85 + correctsPaymentId:p.id,correctionReversalId:reversal.id,accountingVersion:1};
86 + delete newer.journalVoucherId;delete newer.accountingPostedAt;
87 + datastore.payments.push(newer);
88 + posted=jePostSourceRecord(p.direction==='outbound'?'payment':'receipt',newer);
89 + saveDatastore();
90 + }catch(e){
91 + // Before the first async boundary, the batch has not been dispatched.
92 + // Undo local staging if a validation/posting exception occurs.
93 + Object.assign(v,oldVoucher);
94 + datastore.journalVouchers=datastore.journalVouchers.filter(x=>knownVoucherIds.has(x.id));
95 + datastore.journalLines=datastore.journalLines.filter(x=>knownLineIds.has(x.id));
96 + datastore.journalLineDimensions=datastore.journalLineDimensions.filter(x=>knownDimensionIds.has(x.id));
97 + datastore.postingProfiles=datastore.postingProfiles.filter(x=>knownProfileIds.has(x.id));
98 + if(newer)datastore.payments=datastore.payments.filter(x=>x.id!==newer.id);
99 + saveDatastore();
100 + throw e;
101 + }
102 + await invoicePilotAwaitSync(['journalVouchers|'+v.id,'journalVouchers|'+reversal.id,
103 + 'journalVouchers|'+posted.id,'payments|'+newer.id]);
104 return newer.id;
105 }
106 async function paymentCancelPosted(id,reason){