536
const balance=await accounts.trialBalance();
537
assert.equal(balance.totals.debit,balance.totals.credit);
538
});
539
+
540
+test('A01: a return offsets receivables before any cash refund and caps later receipts to net amount',async()=>{
541
+ await setup();await purchase10();
542
+ const sold=await retail.issueInvoice(invoice('sale',3,200,{paidAmount:200}));
543
+ const returned=await retail.issueInvoice(invoice('sale_return',1,200,{
544
+ originalInvoiceId:sold.id
545
+ }));
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'
550
+ }),/بازپرداخت مجاز/);
551
+ await assert.rejects(retail.postTransaction({
552
+ id:'a01-overcollect',type:'receipt',date,amount:201,
553
+ refInvoiceId:sold.id,contactId:'contact-1',toAccountId:'cash-1'
554
+ }),/مانده خالص/);
555
+ assert.equal((await db.getAll('journal_entries')).length,before);
556
+ assert.equal(await db.get('treasury','a01-invalid-refund'),undefined);
557
+ assert.equal(await db.get('treasury','a01-overcollect'),undefined);
558
+ const receipt=await retail.postTransaction({
559
+ id:'a01-valid-remaining',type:'receipt',date,amount:200,
560
+ refInvoiceId:sold.id,contactId:'contact-1',toAccountId:'cash-1'
561
+ });
562
+ assert.equal(receipt.amount,200);
563
+ assert.equal((await db.get('invoices',sold.id)).paidAmount,400);
564
+ await assert.rejects(retail.postTransaction({
565
+ id:'a01-extra-collection',type:'receipt',date,amount:1,
566
+ refInvoiceId:sold.id,contactId:'contact-1',toAccountId:'cash-1'
567
+ }),/مانده خالص/);
568
+ const trial=await accounts.trialBalance();
569
+ assert.equal(trial.totals.debit,trial.totals.credit);
570
+});
571
+
572
+test('A01: only genuinely paid customer credit may be refunded, once, even with concurrent requests',async()=>{
573
+ await setup();await purchase10();
574
+ const sold=await retail.issueInvoice(invoice('sale',3,200,{paidAmount:500}));
575
+ const returned=await retail.issueInvoice(invoice('sale_return',1,200,{
576
+ originalInvoiceId:sold.id
577
+ }));
578
+ const attempts=await Promise.allSettled([
579
+ retail.postTransaction({id:'refund-a',type:'payment',date,amount:70,
580
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'}),
581
+ retail.postTransaction({id:'refund-b',type:'payment',date,amount:70,
582
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'})
583
+ ]);
584
+ assert.equal(attempts.filter(x=>x.status==='fulfilled').length,1);
585
+ assert.equal(attempts.filter(x=>x.status==='rejected').length,1);
586
+ assert.equal((await db.get('invoices',returned.id)).paidAmount,70);
587
+ const finish=await retail.postTransaction({
588
+ id:'refund-final',type:'payment',date,amount:30,
589
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
590
+ });
591
+ assert.equal(finish.amount,30);
592
+ await assert.rejects(retail.postTransaction({
593
+ id:'refund-excess',type:'payment',date,amount:1,
594
+ refInvoiceId:returned.id,contactId:'contact-1',fromAccountId:'cash-1'
595
+ }),/بازپرداخت مجاز/);
596
+ assert.equal((await db.get('invoices',returned.id)).paidAmount,100);
597
+ assert.equal((await db.getAll('treasury')).filter(t=>
598
+ t.refInvoiceId===returned.id&&t.recordType==='transaction').length,2);
599
+ const balance=await accounts.trialBalance();
600
+ assert.equal(balance.totals.debit,balance.totals.credit);
601
+});
602
+
603
+test('A01: supplier cash recovery cannot exceed previously paid credit after purchase return',async()=>{
604
+ await setup();
605
+ const purchased=await retail.issueInvoice(invoice('purchase',10,100,{paidAmount:700}));
606
+ const returned=await retail.issueInvoice(invoice('purchase_return',4,100,{
607
+ originalInvoiceId:purchased.id
608
+ }));
609
+ await assert.rejects(retail.postTransaction({
610
+ id:'supplier-refund-over',type:'receipt',date,amount:101,
611
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
612
+ }),/بازپرداخت مجاز/);
613
+ const cash=await retail.postTransaction({
614
+ id:'supplier-refund-valid',type:'receipt',date,amount:100,
615
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
616
+ });
617
+ assert.equal(cash.amount,100);
618
+ await assert.rejects(retail.postTransaction({
619
+ id:'supplier-refund-excess',type:'receipt',date,amount:1,
620
+ refInvoiceId:returned.id,contactId:'contact-1',toAccountId:'cash-1'
621
+ }),/بازپرداخت مجاز/);
622
+ const trial=await accounts.trialBalance();
623
+ assert.equal(trial.totals.debit,trial.totals.credit);
624
+});