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

test(A01): cover linked return corrections after treasury safety guard

davoodmehraban89 committed Oct 14, 2026 at 23:46 UTC 05e3fb354ab57d0402050e7f8fbc0288f59cb3f7
1 file changed +51
tests/retail-phase2.test.mjs
+51
@@ -403,3 +403,54 @@ test('A01: sales and purchase returns cannot silently refund or collect cash', a
403 assert.equal((await db.getAll('settlements')).length, original.settlements);
404 }
405 });
406 +
407 +test('A01/A03: a referenced customer return creates one atomic correction and correlated audit record', async () => {
408 + await setup();
409 + await purchase10();
410 + const sold = await sale3({ paidAmount: 200 });
411 + const returned = await retail.issueInvoice(invoice('sale_return', 1, 200, {
412 + originalInvoiceId: sold.id
413 + }));
414 + const corrections = await db.getAll('document_corrections');
415 + assert.equal(corrections.length, 1);
416 + const correction = corrections[0];
417 + assert.equal(correction.companyId, company);
418 + assert.equal(correction.sourceId, sold.id);
419 + assert.equal(correction.resultId, returned.id);
420 + assert.equal(correction.correctionType, 'return');
421 + assert.equal(correction.reasonCode, 'customer_return');
422 + assert.equal(correction.actorId, 'phase2-admin');
423 + assert.ok(correction.idempotencyKey.includes(returned.id));
424 + const audits = await db.getAll('audit_events');
425 + assert.ok(audits.some(a => a.sourceId === returned.journalId &&
426 + a.correlationId === correction.correlationId &&
427 + a.eventType === 'commercial_journal_posted'));
428 + assert.equal((await db.get('invoices', sold.id)).grandTotal, 600);
429 + assert.equal((await db.get('invoices', sold.id)).paidAmount, 200);
430 + assert.equal((await db.getAll('settlements')).length, 1);
431 + assert.equal((await db.getAll('stock_movements')).filter(x => x.refId === returned.id).length, 1);
432 + await assert.rejects(retail.issueInvoice(invoice('sale_return', 3, 200, {
433 + originalInvoiceId: sold.id
434 + })), /بیشتر/);
435 + assert.equal((await db.getAll('document_corrections')).length, 1);
436 +});
437 +
438 +test('A01: customer returns reject non-posted legacy origin without partial writes', async () => {
439 + await setup();
440 + await purchase10();
441 + await db.put('invoices', {
442 + id: 'old-unposted', companyId: company, kind: 'sale',
443 + financePosted: false, isPreInvoice: false,
444 + contactId: 'contact-1', items: [{
445 + productId: 'product-1', qty: 2, price: 200,
446 + lineTotal: 400, lineSubtotal: 400
447 + }],
448 + grandTotal: 400, subtotal: 400, totalDiscount: 0, vatAmount: 0, number: 'legacy'
449 + });
450 + const countBefore = (await db.getAll('journal_entries')).length;
451 + const target = invoice('sale_return', 1, 200, { originalInvoiceId: 'old-unposted' });
452 + await assert.rejects(retail.issueInvoice(target), /سازگار نیست/);
453 + assert.equal(await db.get('invoices', target.id), undefined);
454 + assert.equal((await db.getAll('journal_entries')).length, countBefore);
455 + assert.equal((await db.getAll('document_corrections')).length, 0);
456 +});