فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A01): cap cumulative reference-linked return value as well as quantity
davoodmehraban89 committed
Oct 15, 2026 at 20:50 UTC
86861171dfbff3351927047ccd0cda6ccf7840ad
1 file changed
+10
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){