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

fix(A01): reject monetary over-return against referenced invoices

* fix(A01): cap cumulative reference-linked return value as well as quantity * test(A01): block over-credit on inflated or cumulative returns without partial writes

davoodmehraban89 committed Oct 15, 2026 at 20:57 UTC 3be5875af6855f50480b9bc6f79d49dc956c61c6
2 files changed +40
src/core/RetailPostingService.js
+10
@@ -247,6 +247,16 @@ class RetailPostingServiceImpl {
247 if(origin.contactId!==invoice.contactId)
248 throw Error('طرف حساب مرجوعی با فاکتور اولیه مطابقت ندارد');
249 const otherReturns=existing.filter(x=>x.kind===invoice.kind&&x.originalInvoiceId===origin.id);
250 + // Quantity alone is not a sufficient return cap: a changed unit price,
251 + // discount or VAT could credit more money than the original invoice.
252 + // Check the entire history with bigint arithmetic to avoid overflow.
253 + let returnedGross = 0n;
254 + for(const prior of otherReturns) {
255 + returnedGross += BigInt(amount(prior.grandTotal));
256 + }
257 + if(returnedGross + BigInt(amount(invoice.grandTotal)) > BigInt(amount(origin.grandTotal))) {
258 + throw Error('مجموع مبلغ مرجوعی از مبلغ فاکتور اولیه بیشتر است');
259 + }
260 const returnedRows=otherReturns.flatMap(x=>x.items||[]);
261 const ids=new Set(invoice.items.map(x=>x.productId||x.productName));
262 for(const id of ids){
tests/retail-phase2.test.mjs
+30
@@ -454,3 +454,33 @@ test('A01: customer returns reject non-posted legacy origin without partial writ
454 assert.equal((await db.getAll('journal_entries')).length, countBefore);
455 assert.equal((await db.getAll('document_corrections')).length, 0);
456 });
457 +
458 +test('A01: inflated return price cannot credit more than the original sale total', async () => {
459 + await setup();
460 + await purchase10();
461 + const original = await sale3({ paidAmount: 200 });
462 + const before = {
463 + stock: await db.getAll('stock_movements'),
464 + journals: await db.getAll('journal_entries'),
465 + settlements: await db.getAll('settlements'),
466 + invoices: await db.getAll('invoices')
467 + };
468 + const inflated = invoice('sale_return', 1, 650, { originalInvoiceId: original.id });
469 + await assert.rejects(retail.issueInvoice(inflated), /مجموع مبلغ مرجوعی/);
470 + assert.deepEqual(await db.getAll('stock_movements'), before.stock);
471 + assert.deepEqual(await db.getAll('journal_entries'), before.journals);
472 + assert.deepEqual(await db.getAll('settlements'), before.settlements);
473 + assert.deepEqual(await db.getAll('invoices'), before.invoices);
474 +});
475 +
476 +test('A01: successive returns cannot exceed original gross even if total quantities fit', async () => {
477 + await setup();
478 + await purchase10();
479 + const original = await sale3();
480 + await retail.issueInvoice(invoice('sale_return', 1, 400, {
481 + originalInvoiceId: original.id
482 + }));
483 + const excessive = invoice('sale_return', 1, 300, { originalInvoiceId: original.id });
484 + await assert.rejects(retail.issueInvoice(excessive), /مجموع مبلغ مرجوعی/);
485 + assert.equal((await db.getAll('document_corrections')).length, 1);
486 +});