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

fix(P0): honor zero-rial discounted invoice lines in sales reporting

davoodmehraban89 committed Nov 1, 2026 at 06:10 UTC c61d85550e6897f8b0e799936057c61256fbf70c
3 files changed +86 -2
src/controllers/ReportController.js
+3 -2
@@ -6,6 +6,7 @@ import { StorageService } from '../core/StorageService.js';
6 import { Auth } from '../core/Auth.js';
7 import { DataScope } from '../core/DataScope.js';
8 import { Jalali } from '../utils/Jalali.js';
9 +import { reportLineRevenue } from '../core/ReportLineRevenue.mjs';
10
11 class ReportControllerImpl {
12 async _loadData() {
@@ -218,7 +219,7 @@ class ReportControllerImpl {
219 };
220 }
221 const qty = Number(it.qty) || 0;
221 - const lineTotal = Number(it.lineTotal) || (qty * (Number(it.price) || 0));
222 + const lineTotal = reportLineRevenue(it);
223 const p = productMap[it.productId];
224 const buyPrice = p ? (Number(p.buyPrice) || 0) : 0;
225 productStats[key].qty += qty;
@@ -303,7 +304,7 @@ class ReportControllerImpl {
304 };
305 }
306 const qty = Number(it.qty) || 0;
306 - const lineTotal = Number(it.lineTotal) || (qty * (Number(it.price) || 0));
307 + const lineTotal = reportLineRevenue(it);
308 const p = productMap[it.productId];
309 const buyPrice = p ? (Number(p.buyPrice) || 0) : 0;
310 stats[key].qty += qty;
src/core/ReportLineRevenue.mjs new
+21
@@ -0,0 +1,21 @@
1 +// Reports must distinguish a missing legacy field from a legitimate zero-rial
2 +// invoice row (e.g. a fully discounted line). Never treat zero as missing.
3 +export function reportLineRevenue(item) {
4 + if (!item || typeof item !== 'object' || Array.isArray(item))
5 + throw new TypeError('ردیف فاکتور برای گزارش نامعتبر است');
6 + if (item.lineTotal !== null && item.lineTotal !== undefined) {
7 + const net = Number(item.lineTotal);
8 + if (!Number.isSafeInteger(net) || net < 0)
9 + throw new Error('مبلغ خالص ردیف فاکتور برای گزارش نامعتبر است');
10 + return net;
11 + }
12 + // Older imported invoice lines may lack lineTotal entirely. Preserve the
13 + // existing gross-price fallback only for those legacy records.
14 + const qty = Number(item.qty ?? 0);
15 + const price = Number(item.price ?? 0);
16 + const gross = Math.round(qty * price);
17 + if (!Number.isFinite(qty) || qty < 0 || !Number.isFinite(price) ||
18 + price < 0 || !Number.isSafeInteger(gross))
19 + throw new Error('مبلغ ردیف قدیمی فاکتور برای گزارش نامعتبر است');
20 + return gross;
21 +}
tests/report-zero-discount-integrity.test.mjs new
+62
@@ -0,0 +1,62 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { StorageService } from '../src/core/StorageService.js';
5 +import { CompanyService } from '../src/core/CompanyService.js';
6 +import { Auth } from '../src/core/Auth.js';
7 +import { ReportController } from '../src/controllers/ReportController.js';
8 +import { reportLineRevenue } from '../src/core/ReportLineRevenue.mjs';
9 +
10 +const companyId='synthetic-zero-discount-report-company';
11 +test('100% discount must have exactly zero revenue, not the gross sale price',()=>{
12 + assert.equal(reportLineRevenue({qty:2,price:150,lineTotal:0,discount:300}),0);
13 + assert.equal(reportLineRevenue({qty:2,price:150,lineTotal:'0'}),0);
14 + assert.equal(reportLineRevenue({qty:2,price:150,lineTotal:60}),60);
15 + assert.equal(reportLineRevenue({qty:2,price:150}),300,'legacy missing value retains historical fallback');
16 + assert.equal(reportLineRevenue({qty:0,price:150}),0);
17 +});
18 +
19 +test('corrupt persisted line totals are rejected rather than silently inflating revenue',()=>{
20 + for(const invalid of [-1,1.5,NaN,Infinity,'not-a-number',Number.MAX_SAFE_INTEGER+1]){
21 + assert.throws(()=>reportLineRevenue({qty:1,price:100,lineTotal:invalid}),/نامعتبر/);
22 + }
23 + for(const invalid of [Infinity,-1,NaN]){
24 + assert.throws(()=>reportLineRevenue({qty:invalid,price:100}),/نامعتبر/);
25 + }
26 + assert.throws(()=>reportLineRevenue(null),/نامعتبر/);
27 +});
28 +
29 +test('real indexedDB sales and profitability reports preserve discounted zero and legacy lines',async()=>{
30 + await StorageService.clearAll();
31 + Auth.currentUser={id:'report-synthetic-admin',username:'admin',role:'admin'};
32 + await StorageService.put('companies',{id:companyId,name:'Synthetic report company'});
33 + CompanyService.activeCompanyId=companyId;
34 + await StorageService.put('products',{
35 + id:'report-product',companyId,ownerUserId:'report-synthetic-admin',
36 + name:'Synthetic product',buyPrice:50,sellPrice:100
37 + });
38 + await StorageService.put('contacts',{
39 + id:'report-contact',companyId,ownerUserId:'report-synthetic-admin',
40 + name:'Synthetic customer',role:'customer'
41 + });
42 + await StorageService.put('invoices',{
43 + id:'report-invoice',companyId,ownerUserId:'report-synthetic-admin',
44 + number:'1',date:'1405/07/18',kind:'sale',isPreInvoice:false,
45 + contactId:'report-contact',contactName:'Synthetic customer',
46 + grandTotal:260,subtotal:260,paidAmount:0,remaining:260,
47 + items:[
48 + {productId:'report-product',productName:'Synthetic product',qty:2,price:100,discount:200,lineTotal:0},
49 + {productId:'report-product',productName:'Synthetic product',qty:1,price:100,discount:40,lineTotal:60},
50 + {productId:'report-product',productName:'Synthetic product',qty:2,price:100}
51 + ]
52 + });
53 + const analysis=await ReportController.getSalesAnalysis({preset:'all'});
54 + assert.equal(analysis.topProductsBySales[0].totalSales,260);
55 + assert.equal(analysis.topProductsBySales[0].qty,5);
56 + const profitability=await ReportController.getProductProfitability({preset:'all'});
57 + assert.equal(profitability.length,1);
58 + assert.deepEqual([profitability[0].totalSales,profitability[0].cogs,profitability[0].profit],
59 + [260,250,10]);
60 + assert.equal((await StorageService.get('invoices','report-invoice')).items[0].lineTotal,0,
61 + 'read-only reporting must not alter persisted issued invoices');
62 +});