فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(A01): enforce reason and explicit approval on return cash settlements
davoodmehraban89 committed
Oct 21, 2026 at 08:44 UTC
90698ac5a885421501f2ef62798863eecb9144f1
1 file changed
+89
-16
tests/retail-phase2.test.mjs
+89
-16
@@ -546,7 +546,8 @@ test('A01: a return offsets receivables before any cash refund and caps later re
546
const before=(await db.getAll('journal_entries')).length;
547
await assert.rejects(retail.postTransaction({
548
id:'a01-invalid-refund',type:'payment',date,amount:1,
549
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
549
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
550
+ reasonCode:'goods_return',explicitlyApproved:true
551
}),/بازپرداخت مجاز/);
552
await assert.rejects(retail.postTransaction({
553
id:'a01-overcollect',type:'receipt',date,amount:201,
@@ -577,21 +578,25 @@ test('A01: only genuinely paid customer credit may be refunded, once, even with
578
}));
579
const attempts=await Promise.allSettled([
580
retail.postTransaction({id:'refund-a',type:'payment',date,amount:70,
580
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'}),
581
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
582
+ reasonCode:'goods_return',explicitlyApproved:true}),
583
retail.postTransaction({id:'refund-b',type:'payment',date,amount:70,
582
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'})
584
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
585
+ reasonCode:'goods_return',explicitlyApproved:true})
586
]);
587
assert.equal(attempts.filter(x=>x.status==='fulfilled').length,1);
588
assert.equal(attempts.filter(x=>x.status==='rejected').length,1);
589
assert.equal((await db.get('invoices',returned.id)).paidAmount,70);
590
const finish=await retail.postTransaction({
591
id:'refund-final',type:'payment',date,amount:30,
589
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
592
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
593
+ reasonCode:'goods_return',explicitlyApproved:true
594
});
595
assert.equal(finish.amount,30);
596
await assert.rejects(retail.postTransaction({
597
id:'refund-excess',type:'payment',date,amount:1,
594
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
598
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
599
+ reasonCode:'goods_return',explicitlyApproved:true
600
}),/بازپرداخت مجاز/);
601
assert.equal((await db.get('invoices',returned.id)).paidAmount,100);
602
assert.equal((await db.getAll('treasury')).filter(t=>
@@ -611,7 +616,8 @@ test('A01: customer credit refund requires administrator and rejected attempt le
616
Auth.currentUser={id:'phase2-clerk',role:'user',username:'clerk',companyIds:[company]};
617
await assert.rejects(retail.postTransaction({
618
id:'unauthorized-return-refund',type:'payment',date,amount:25,
614
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
619
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
620
+ reasonCode:'goods_return',explicitlyApproved:true
621
}),/فقط توسط مدیر/);
622
assert.equal(await db.get('treasury','unauthorized-return-refund'),undefined);
623
assert.equal((await db.get('invoices',returned.id)).paidAmount,0);
@@ -620,7 +626,8 @@ test('A01: customer credit refund requires administrator and rejected attempt le
626
Auth.currentUser={id:'phase2-admin',role:'admin',username:'admin'};
627
const approved=await retail.postTransaction({
628
id:'approved-return-refund',type:'payment',date,amount:25,
623
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
629
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
630
+ reasonCode:'goods_return',explicitlyApproved:true
631
});
632
assert.equal(approved.amount,25);
633
assert.equal((await db.get('invoices',returned.id)).paidAmount,25);
@@ -636,16 +643,19 @@ test('A01: supplier cash recovery cannot exceed previously paid credit after pur
643
}));
644
await assert.rejects(retail.postTransaction({
645
id:'supplier-refund-over',type:'receipt',date,amount:101,
639
- refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
646
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1',
647
+ reasonCode:'goods_return',explicitlyApproved:true
648
}),/بازپرداخت مجاز/);
649
const cash=await retail.postTransaction({
650
id:'supplier-refund-valid',type:'receipt',date,amount:100,
643
- refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
651
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1',
652
+ reasonCode:'goods_return',explicitlyApproved:true
653
});
654
assert.equal(cash.amount,100);
655
await assert.rejects(retail.postTransaction({
656
id:'supplier-refund-excess',type:'receipt',date,amount:1,
648
- refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
657
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1',
658
+ reasonCode:'goods_return',explicitlyApproved:true
659
}),/بازپرداخت مجاز/);
660
const trial=await accounts.trialBalance();
661
assert.equal(trial.totals.debit,trial.totals.credit);
@@ -660,7 +670,8 @@ test('A01: authorized customer credit refund creates one linked correction, sett
670
const before=(await db.getAll('document_corrections')).length;
671
const refund=await retail.postTransaction({
672
id:'a01-refund-trace',type:'payment',date,amount:100,
663
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
673
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
674
+ reasonCode:'goods_return',explicitlyApproved:true
675
});
676
const records=(await db.getAll('document_corrections')).filter(c=>
677
c.correctionType==='settlement_refund');
@@ -670,7 +681,11 @@ test('A01: authorized customer credit refund creates one linked correction, sett
681
assert.equal(records[0].resultType,'treasury');
682
assert.equal(records[0].resultId,refund.id);
683
assert.equal(records[0].correlationId,refund.journalId);
673
- assert.equal(records[0].reasonCode,'customer_credit_refund');
684
+ assert.equal(records[0].reasonCode,'goods_return');
685
+ assert.equal(records[0].actionCode,'customer_credit_refund');
686
+ assert.equal(records[0].approvedBy,'phase2-admin');
687
+ assert.equal(records[0].approvalMethod,'explicit_confirmation');
688
+ assert.ok(records[0].approvedAtUtc);
689
assert.equal(records[0].amount,100);
690
assert.equal(records[0].actorId,'phase2-admin');
691
assert.equal((await db.getAll('settlements')).filter(s=>
@@ -680,7 +695,8 @@ test('A01: authorized customer credit refund creates one linked correction, sett
695
assert.ok(audit);
696
await assert.rejects(retail.postTransaction({
697
id:refund.id,type:'payment',date,amount:100,
683
- refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
698
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1',
699
+ reasonCode:'goods_return',explicitlyApproved:true
700
}),/تکراری/);
701
assert.equal((await db.getAll('document_corrections')).length,before+1);
702
assert.equal((await db.get('invoices',sold.id)).paidAmount,500);
@@ -698,13 +714,15 @@ test('A01: supplier recovery trace is linked; rejected over-recovery adds no cor
714
const base=(await db.getAll('document_corrections')).length;
715
await assert.rejects(retail.postTransaction({
716
id:'a01-supplier-over',type:'receipt',date,amount:101,
701
- refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
717
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1',
718
+ reasonCode:'goods_return',explicitlyApproved:true
719
}),/بازپرداخت مجاز/);
720
assert.equal((await db.getAll('document_corrections')).length,base);
721
assert.equal(await db.get('treasury','a01-supplier-over'),undefined);
722
const recovered=await retail.postTransaction({
723
id:'a01-supplier-trace',type:'receipt',date,amount:100,
707
- refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
724
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1',
725
+ reasonCode:'goods_return',explicitlyApproved:true
726
});
727
const records=(await db.getAll('document_corrections')).filter(c=>
728
c.correctionType==='settlement_refund');
@@ -712,7 +730,9 @@ test('A01: supplier recovery trace is linked; rejected over-recovery adds no cor
730
assert.equal(records[0].sourceId,returned.id);
731
assert.equal(records[0].resultId,recovered.id);
732
assert.equal(records[0].correlationId,recovered.journalId);
715
- assert.equal(records[0].reasonCode,'supplier_credit_recovery');
733
+ assert.equal(records[0].reasonCode,'goods_return');
734
+ assert.equal(records[0].actionCode,'supplier_credit_recovery');
735
+ assert.equal(records[0].approvedBy,'phase2-admin');
736
assert.equal(records[0].amount,100);
737
assert.equal((await db.get('invoices',purchased.id)).paidAmount,700);
738
const trial=await accounts.trialBalance();
@@ -755,3 +775,56 @@ test('A01: late correction idempotency collision rolls back cash, journal, settl
775
const trial=await accounts.trialBalance();
776
assert.equal(trial.totals.debit,trial.totals.credit);
777
});
778
+
779
+test('A01: return cash flow requires structured reason and explicit approval before writes',async()=>{
780
+ await setup();await purchase10();
781
+ const sold=await retail.issueInvoice(invoice('sale',3,200,{paidAmount:500}));
782
+ const returned=await retail.issueInvoice(invoice('sale_return',1,200,{
783
+ originalInvoiceId:sold.id
784
+ }));
785
+ const before={
786
+ treasury:(await db.getAll('treasury')).length,
787
+ journals:(await db.getAll('journal_entries')).length,
788
+ audit:(await db.getAll('audit_events')).length,
789
+ corrections:(await db.getAll('document_corrections')).length,
790
+ settlements:(await db.getAll('settlements')).length
791
+ };
792
+ const base={
793
+ type:'payment',date,amount:50,refInvoiceId:returned.id,
794
+ contactId:'contact-1',fromAccountId:'cash-1'
795
+ };
796
+ await assert.rejects(retail.postTransaction({
797
+ ...base,id:'no-reason',explicitlyApproved:true
798
+ }),/دلیل ساختاریافته/);
799
+ await assert.rejects(retail.postTransaction({
800
+ ...base,id:'invalid-reason',reasonCode:'unauthorized-text',explicitlyApproved:true
801
+ }),/دلیل ساختاریافته/);
802
+ await assert.rejects(retail.postTransaction({
803
+ ...base,id:'unconfirmed',reasonCode:'quality_issue'
804
+ }),/تأیید صریح/);
805
+ await assert.rejects(retail.postTransaction({
806
+ ...base,id:'wrong-confirmation',reasonCode:'quality_issue',explicitlyApproved:'true'
807
+ }),/تأیید صریح/);
808
+ for(const id of ['no-reason','invalid-reason','unconfirmed','wrong-confirmation']){
809
+ assert.equal(await db.get('treasury',id),undefined);
810
+ }
811
+ assert.equal((await db.get('invoices',returned.id)).paidAmount,0);
812
+ assert.deepEqual({
813
+ treasury:(await db.getAll('treasury')).length,
814
+ journals:(await db.getAll('journal_entries')).length,
815
+ audit:(await db.getAll('audit_events')).length,
816
+ corrections:(await db.getAll('document_corrections')).length,
817
+ settlements:(await db.getAll('settlements')).length
818
+ },before);
819
+ const approved=await retail.postTransaction({
820
+ ...base,id:'approved-reason',reasonCode:'quality_issue',explicitlyApproved:true
821
+ });
822
+ const correction=(await db.getAll('document_corrections')).find(x=>
823
+ x.resultId===approved.id);
824
+ assert.equal(correction?.reasonCode,'quality_issue');
825
+ assert.equal(correction?.actionCode,'customer_credit_refund');
826
+ assert.equal(correction?.approvedBy,'phase2-admin');
827
+ assert.equal(correction?.approvalMethod,'explicit_confirmation');
828
+ assert.equal((await accounts.trialBalance()).totals.debit,
829
+ (await accounts.trialBalance()).totals.credit);
830
+});