فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(finance): reconcile multi-return payable and journal balance

Davood Mehraban committed Nov 23, 2026 at 09:18 UTC 3519fb0e01f8da7e2a93703478fb6f72bd286efb
1 file changed +32
tests/g317-return-credit-allocation.test.mjs
+32
@@ -53,6 +53,17 @@ async function allocationRows(targetId){
53 return (await db.getAll('settlements')).filter(x=>
54 x.settlementType==='credit_allocation'&&x.targetInvoiceId===targetId);
55 }
56 +async function assertBalancedJournalLedger(){
57 + const entries=await db.getAll('journal_entries');
58 + const lines=await db.getAll('journal_lines');
59 + for(const entry of entries){
60 + const related=lines.filter(row=>row.journalId===entry.id);
61 + assert.ok(related.length>=2,'posted journal must have multiple lines: '+entry.id);
62 + const debit=related.reduce((sum,row)=>sum+Number(row.debit||0),0);
63 + const credit=related.reduce((sum,row)=>sum+Number(row.credit||0),0);
64 + assert.equal(debit,credit,'journal debit/credit imbalance: '+entry.id);
65 + }
66 +}
67
68 test('G317 purchase return allocates credit to open supplier payable before cash recovery',async()=>{
69 await setup();
@@ -163,6 +174,27 @@ test('G317 multiple partial purchase returns allocate the original payable only
174 assert.equal(rows.reduce((n,x)=>n+x.amount,0),300);
175 });
176
177 +
178 +test('G317 multi-return supplier settlement keeps every posted journal balanced and reconciles payable',async()=>{
179 + await setup();
180 + const original=await retail.issueInvoice(invoice('purchase',10,100));
181 + await retail.postTransaction({id:'g317-balance-payment',type:'payment',date,amount:700,
182 + contactId:'g317-supplier',refInvoiceId:original.id,fromAccountId:'g317-cash',method:'cash'});
183 + const first=await retail.issueInvoice(invoice('purchase_return',2,100,{originalInvoiceId:original.id}));
184 + const second=await retail.issueInvoice(invoice('purchase_return',2,100,{originalInvoiceId:original.id}));
185 + assert.equal((await db.get('invoices',first.id)).remaining,0);
186 + assert.equal((await db.get('invoices',second.id)).remaining,100);
187 + assert.equal(await controlBalance('201','g317-supplier','credit'),-100);
188 + await assertBalancedJournalLedger();
189 + await retail.postTransaction({id:'g317-balance-recovery',type:'receipt',date,amount:100,
190 + contactId:'g317-supplier',refInvoiceId:second.id,toAccountId:'g317-cash',method:'cash',
191 + reasonCode:'goods_return',explicitlyApproved:true});
192 + assert.equal(await controlBalance('201','g317-supplier','credit'),0);
193 + assert.equal((await db.get('invoices',original.id)).remaining,0);
194 + assert.equal((await db.get('invoices',second.id)).remaining,0);
195 + await assertBalancedJournalLedger();
196 +});
197 +
198 test('G317 concurrent idempotent return issuance creates one return and one credit allocation',async()=>{
199 await setup();
200 const original=await retail.issueInvoice(invoice('purchase',10,100));