فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(A01): verify atomic commercial correction references and audit correlation
davoodmehraban89 committed
Oct 14, 2026 at 23:36 UTC
b1730a896e18c89aeedf3d55807744fe12618e08
1 file changed
+51
tests/retail-phase2.test.mjs
+51
@@ -378,3 +378,54 @@ test('regression: non-postable 101 with other children creates valid mapped cash
378
assert.notEqual(ledger.code,'101001');
379
assert.equal(ledger.active,true);
380
});
381
+
382
+test('A01/A03: a referenced customer return creates one atomic correction and correlated audit record', async () => {
383
+ await setup();
384
+ await purchase10();
385
+ const sold = await sale3({ paidAmount: 200 });
386
+ const returned = await retail.issueInvoice(invoice('sale_return', 1, 200, {
387
+ originalInvoiceId: sold.id
388
+ }));
389
+ const corrections = await db.getAll('document_corrections');
390
+ assert.equal(corrections.length, 1);
391
+ const correction = corrections[0];
392
+ assert.equal(correction.companyId, company);
393
+ assert.equal(correction.sourceId, sold.id);
394
+ assert.equal(correction.resultId, returned.id);
395
+ assert.equal(correction.correctionType, 'return');
396
+ assert.equal(correction.reasonCode, 'customer_return');
397
+ assert.equal(correction.actorId, 'phase2-admin');
398
+ assert.ok(correction.idempotencyKey.includes(returned.id));
399
+ const audits = await db.getAll('audit_events');
400
+ assert.ok(audits.some(a => a.sourceId === returned.journalId &&
401
+ a.correlationId === correction.correlationId &&
402
+ a.eventType === 'commercial_journal_posted'));
403
+ assert.equal((await db.get('invoices', sold.id)).grandTotal, 600);
404
+ assert.equal((await db.get('invoices', sold.id)).paidAmount, 200);
405
+ assert.equal((await db.getAll('settlements')).length, 1);
406
+ assert.equal((await db.getAll('stock_movements')).filter(x => x.refId === returned.id).length, 1);
407
+ await assert.rejects(retail.issueInvoice(invoice('sale_return', 3, 200, {
408
+ originalInvoiceId: sold.id
409
+ })), /بیشتر/);
410
+ assert.equal((await db.getAll('document_corrections')).length, 1);
411
+});
412
+
413
+test('A01: customer returns reject non-posted legacy origin without partial writes', async () => {
414
+ await setup();
415
+ await purchase10();
416
+ await db.put('invoices', {
417
+ id: 'old-unposted', companyId: company, kind: 'sale',
418
+ financePosted: false, isPreInvoice: false,
419
+ contactId: 'contact-1', items: [{
420
+ productId: 'product-1', qty: 2, price: 200,
421
+ lineTotal: 400, lineSubtotal: 400
422
+ }],
423
+ grandTotal: 400, subtotal: 400, totalDiscount: 0, vatAmount: 0, number: 'legacy'
424
+ });
425
+ const countBefore = (await db.getAll('journal_entries')).length;
426
+ const target = invoice('sale_return', 1, 200, { originalInvoiceId: 'old-unposted' });
427
+ await assert.rejects(retail.issueInvoice(target), /سازگار نیست/);
428
+ assert.equal(await db.get('invoices', target.id), undefined);
429
+ assert.equal((await db.getAll('journal_entries')).length, countBefore);
430
+ assert.equal((await db.getAll('document_corrections')).length, 0);
431
+});