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

test(A01): verify atomic refund and supplier recovery correction traces

davoodmehraban89 committed Oct 20, 2026 at 17:25 UTC f099749e3947f8749aafe6ebfeeb0cd2814048ad
1 file changed +68
tests/retail-phase2.test.mjs
+68
@@ -650,3 +650,71 @@ test('A01: supplier cash recovery cannot exceed previously paid credit after pur
650 const trial=await accounts.trialBalance();
651 assert.equal(trial.totals.debit,trial.totals.credit);
652 });
653 +
654 +test('A01: authorized customer credit refund creates one linked correction, settlement and audit atomically',async()=>{
655 + await setup();await purchase10();
656 + const sold=await retail.issueInvoice(invoice('sale',3,200,{paidAmount:500}));
657 + const returned=await retail.issueInvoice(invoice('sale_return',1,200,{
658 + originalInvoiceId:sold.id
659 + }));
660 + const before=(await db.getAll('document_corrections')).length;
661 + const refund=await retail.postTransaction({
662 + id:'a01-refund-trace',type:'payment',date,amount:100,
663 + refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
664 + });
665 + const records=(await db.getAll('document_corrections')).filter(c=>
666 + c.correctionType==='settlement_refund');
667 + assert.equal((await db.getAll('document_corrections')).length,before+1);
668 + assert.equal(records.length,1);
669 + assert.equal(records[0].sourceId,returned.id);
670 + assert.equal(records[0].resultType,'treasury');
671 + assert.equal(records[0].resultId,refund.id);
672 + assert.equal(records[0].correlationId,refund.journalId);
673 + assert.equal(records[0].reasonCode,'customer_credit_refund');
674 + assert.equal(records[0].amount,100);
675 + assert.equal(records[0].actorId,'phase2-admin');
676 + assert.equal((await db.getAll('settlements')).filter(s=>
677 + s.transactionId===refund.id&&s.invoiceId===returned.id).length,1);
678 + const audit=(await db.getAll('audit_events')).find(a=>
679 + a.sourceId===refund.journalId&&a.correlationId===records[0].correlationId);
680 + assert.ok(audit);
681 + await assert.rejects(retail.postTransaction({
682 + id:refund.id,type:'payment',date,amount:100,
683 + refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
684 + }),/تکراری/);
685 + assert.equal((await db.getAll('document_corrections')).length,before+1);
686 + assert.equal((await db.get('invoices',sold.id)).paidAmount,500);
687 + assert.equal((await db.get('invoices',returned.id)).paidAmount,100);
688 + const trial=await accounts.trialBalance();
689 + assert.equal(trial.totals.debit,trial.totals.credit);
690 +});
691 +
692 +test('A01: supplier recovery trace is linked; rejected over-recovery adds no correction',async()=>{
693 + await setup();
694 + const purchased=await retail.issueInvoice(invoice('purchase',10,100,{paidAmount:700}));
695 + const returned=await retail.issueInvoice(invoice('purchase_return',4,100,{
696 + originalInvoiceId:purchased.id
697 + }));
698 + const base=(await db.getAll('document_corrections')).length;
699 + await assert.rejects(retail.postTransaction({
700 + id:'a01-supplier-over',type:'receipt',date,amount:101,
701 + refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
702 + }),/بازپرداخت مجاز/);
703 + assert.equal((await db.getAll('document_corrections')).length,base);
704 + assert.equal(await db.get('treasury','a01-supplier-over'),undefined);
705 + const recovered=await retail.postTransaction({
706 + id:'a01-supplier-trace',type:'receipt',date,amount:100,
707 + refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
708 + });
709 + const records=(await db.getAll('document_corrections')).filter(c=>
710 + c.correctionType==='settlement_refund');
711 + assert.equal(records.length,1);
712 + assert.equal(records[0].sourceId,returned.id);
713 + assert.equal(records[0].resultId,recovered.id);
714 + assert.equal(records[0].correlationId,recovered.journalId);
715 + assert.equal(records[0].reasonCode,'supplier_credit_recovery');
716 + assert.equal(records[0].amount,100);
717 + assert.equal((await db.get('invoices',purchased.id)).paidAmount,700);
718 + const trial=await accounts.trialBalance();
719 + assert.equal(trial.totals.debit,trial.totals.credit);
720 +});