فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(reports): net customer sales against returns and disclose balances
davoodmehraban89 committed
Nov 1, 2026 at 15:34 UTC
c4c3da45197328edd3dd7e0c0ea19f1bbf5ae37f
3 files changed
+142
-70
src/controllers/ReportController.js
+40
-31
@@ -372,7 +372,7 @@ class ReportControllerImpl {
372
}
373
374
async getSalesAnalysis(filter = {}, data = null) {
375
- const { invoices, products, contacts, stockMovements = [] } = data || await this._loadData();
375
+ const { invoices, products, stockMovements = [] } = data || await this._loadData();
376
const { from, to } = this._getRange(filter);
377
const rows = invoices.filter(invoice =>
378
['sale', 'sale_return'].includes(invoice.kind) &&
@@ -387,34 +387,42 @@ class ReportControllerImpl {
387
.sort((a, b) => b.qty - a.qty)
388
.slice(0, 10);
389
390
- const salesInRange = rows.filter(invoice => invoice.kind === 'sale');
390
const contactStats = {};
392
- salesInRange.forEach(invoice => {
391
+ rows.forEach(invoice => {
392
const key = invoice.contactId || 'unknown';
393
if (!contactStats[key]) {
394
contactStats[key] = {
395
contactId: invoice.contactId,
396
contactName: invoice.contactName || 'ناشناس',
397
invoiceCount: 0,
398
+ returnCount: 0,
399
totalSales: 0,
400
+ totalReturns: 0,
401
+ netSales: 0,
402
totalPaid: 0,
403
totalRemaining: 0
404
};
405
}
404
- contactStats[key].invoiceCount += 1;
405
- contactStats[key].totalSales += Number(invoice.grandTotal) || 0;
406
- contactStats[key].totalPaid += Number(invoice.paidAmount) || 0;
407
- contactStats[key].totalRemaining += Number(invoice.remaining) || 0;
406
+ if (invoice.kind === 'sale') {
407
+ contactStats[key].invoiceCount += 1;
408
+ contactStats[key].totalSales += Number(invoice.grandTotal) || 0;
409
+ contactStats[key].totalPaid += Number(invoice.paidAmount) || 0;
410
+ contactStats[key].totalRemaining += Number(invoice.remaining) || 0;
411
+ } else {
412
+ contactStats[key].returnCount += 1;
413
+ contactStats[key].totalReturns += Number(invoice.grandTotal) || 0;
414
+ }
415
});
416
417
const topCustomers = Object.values(contactStats)
411
- .sort((a, b) => b.totalSales - a.totalSales)
418
+ .map(customer => ({ ...customer, netSales: customer.totalSales - customer.totalReturns }))
419
+ .sort((a, b) => b.netSales - a.netSales)
420
.slice(0, 10);
421
return {
422
topProductsBySales,
423
topProductsByQty,
424
topCustomers,
417
- invoiceCount: salesInRange.length
425
+ invoiceCount: rows.filter(invoice => invoice.kind === 'sale').length
426
};
427
}
428
@@ -433,45 +441,46 @@ class ReportControllerImpl {
441
async getCustomerReport(filter = {}, data = null) {
442
const { invoices, contacts } = data || await this._loadData();
443
const { from, to } = this._getRange(filter);
436
-
444
const contactMap = {};
438
- contacts.forEach(c => contactMap[c.id] = c);
445
+ contacts.forEach(contact => { contactMap[contact.id] = contact; });
446
447
const stats = {};
441
- invoices.forEach(inv => {
442
- if (inv.isPreInvoice) return;
443
- if (!this._inRange(inv.date, from, to)) return;
444
- const key = inv.contactId || 'unknown';
448
+ invoices.forEach(invoice => {
449
+ if (!['sale', 'sale_return'].includes(invoice.kind) || invoice.isPreInvoice) return;
450
+ if (!this._inRange(invoice.date, from, to)) return;
451
+ const key = invoice.contactId || 'unknown';
452
if (!stats[key]) {
446
- const c = contactMap[inv.contactId];
453
+ const contact = contactMap[invoice.contactId];
454
stats[key] = {
448
- contactId: inv.contactId,
449
- contactName: inv.contactName || 'ناشناس',
450
- entityType: c?.entityType || 'natural',
455
+ contactId: invoice.contactId,
456
+ contactName: invoice.contactName || contact?.name || 'ناشناس',
457
+ entityType: contact?.entityType || 'natural',
458
totalInvoices: 0,
459
+ returnCount: 0,
460
totalSales: 0,
461
totalReturns: 0,
462
+ netSales: 0,
463
totalPaid: 0,
464
totalRemaining: 0,
465
lastInvoiceDate: ''
466
};
467
}
459
- const isSale = inv.kind === 'sale';
460
- const isReturn = inv.kind === 'sale_return';
461
- if (isSale) {
468
+ if (invoice.kind === 'sale') {
469
stats[key].totalInvoices += 1;
463
- stats[key].totalSales += Number(inv.grandTotal) || 0;
464
- stats[key].totalPaid += Number(inv.paidAmount) || 0;
465
- stats[key].totalRemaining += Number(inv.remaining) || 0;
466
- } else if (isReturn) {
467
- stats[key].totalReturns += Number(inv.grandTotal) || 0;
468
- }
469
- if (inv.date > (stats[key].lastInvoiceDate || '')) {
470
- stats[key].lastInvoiceDate = inv.date;
470
+ stats[key].totalSales += Number(invoice.grandTotal) || 0;
471
+ stats[key].totalPaid += Number(invoice.paidAmount) || 0;
472
+ stats[key].totalRemaining += Number(invoice.remaining) || 0;
473
+ } else {
474
+ stats[key].returnCount += 1;
475
+ stats[key].totalReturns += Number(invoice.grandTotal) || 0;
476
}
477
+ if (invoice.date > (stats[key].lastInvoiceDate || ''))
478
+ stats[key].lastInvoiceDate = invoice.date;
479
});
480
474
- return Object.values(stats).sort((a, b) => b.totalSales - a.totalSales);
481
+ return Object.values(stats)
482
+ .map(customer => ({ ...customer, netSales: customer.totalSales - customer.totalReturns }))
483
+ .sort((a, b) => b.netSales - a.netSales);
484
}
485
486
// Load one company-scoped snapshot and reuse it across all report calculations.
src/views/ReportsView.js
+55
-39
@@ -360,7 +360,7 @@ class ReportsViewImpl {
360
</div>
361
362
<div class="card">
363
- <h3 style="font-size:15px;font-weight:700;margin-bottom:14px">👥 ۱۰ مشتری برتر (به ترتیب مبلغ خرید)</h3>
363
+ <h3 style="font-size:15px;font-weight:700;margin-bottom:14px">👥 ۱۰ مشتری برتر (به ترتیب خرید خالص پس از مرجوعی)</h3>
364
<div class="table-wrap">
365
<table>
366
<thead>
@@ -368,18 +368,22 @@ class ReportsViewImpl {
368
<th style="width:30px">#</th>
369
<th>مشتری</th>
370
<th style="width:100px">تعداد فاکتور</th>
371
- <th style="width:150px">مجموع خرید</th>
372
- <th style="width:150px">مجموع پرداخت</th>
373
- <th style="width:150px">مانده</th>
371
+ <th style="width:130px">فروش ناخالص</th>
372
+ <th style="width:120px">مرجوعی</th>
373
+ <th style="width:130px">فروش خالص</th>
374
+ <th style="width:130px">دریافت از فروش</th>
375
+ <th style="width:160px">مانده فاکتور فروش</th>
376
</tr>
377
</thead>
378
<tbody>
377
- ${topCustomers.length === 0 ? '<tr><td colspan="6" style="text-align:center;padding:20px;color:var(--text-muted)">دادهای نیست</td></tr>' : topCustomers.map((c, i) => `
379
+ ${topCustomers.length === 0 ? '<tr><td colspan="8" style="text-align:center;padding:20px;color:var(--text-muted)">دادهای نیست</td></tr>' : topCustomers.map((c, i) => `
380
<tr>
381
<td><strong>${Formatters.toPersianDigits(i + 1)}</strong></td>
382
<td>${this._esc(c.contactName)}</td>
383
<td>${Formatters.number(c.invoiceCount)}</td>
384
<td style="font-weight:600;color:var(--primary)">${Formatters.money(c.totalSales)}</td>
385
+ <td style="color:var(--danger)">${Formatters.money(c.totalReturns)}</td>
386
+ <td style="font-weight:700;color:${c.netSales < 0 ? 'var(--danger)' : 'var(--primary)'}">${Formatters.money(c.netSales)}</td>
387
<td style="color:var(--success)">${Formatters.money(c.totalPaid)}</td>
388
<td style="font-weight:600;color:${c.totalRemaining > 0 ? 'var(--danger)' : 'var(--success)'}">${Formatters.money(c.totalRemaining)}</td>
389
</tr>
@@ -387,6 +391,7 @@ class ReportsViewImpl {
391
</tbody>
392
</table>
393
</div>
394
+ <p role="note" style="font-size:12px;line-height:1.8;color:var(--text-muted);margin:10px 0 0">فروش خالص، مرجوعی را از فروش ناخالص کم میکند. مانده فقط جمع مانده فاکتورهای فروش است و اعتبار مرجوعی یا بازپرداخت را از دفتر کل محاسبه نمیکند.</p>
395
</div>
396
`;
397
}
@@ -497,36 +502,39 @@ class ReportsViewImpl {
502
// ============================================================
503
renderCustomersTab() {
504
const list = cache.customers || [];
500
- const totalSales = list.reduce((s, c) => s + c.totalSales, 0);
501
- const totalRemaining = list.reduce((s, c) => s + c.totalRemaining, 0);
505
+ const totalNetSales = list.reduce((sum, customer) => sum + customer.netSales, 0);
506
+ const totalRemaining = list.reduce((sum, customer) => sum + customer.totalRemaining, 0);
507
508
return `
509
<div class="grid-kpi">
510
<div class="card kpi-card" style="margin:0">
511
<div class="kpi-card-icon" style="background:#eff6ff;color:var(--primary)">👥</div>
512
<div style="flex:1">
508
- <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">تعداد مشتریان فعال</div>
513
+ <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">تعداد مشتریان دارای فروش یا مرجوعی</div>
514
<div style="font-size:16px;font-weight:800;color:var(--primary)">${Formatters.number(list.length)}</div>
515
</div>
516
</div>
517
<div class="card kpi-card" style="margin:0">
518
<div class="kpi-card-icon" style="background:#ecfdf5;color:var(--success)">💰</div>
519
<div style="flex:1">
515
- <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">مجموع فروش</div>
516
- <div style="font-size:16px;font-weight:800;color:var(--success)">${Formatters.money(totalSales)}</div>
520
+ <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">خرید خالص پس از مرجوعی</div>
521
+ <div style="font-size:16px;font-weight:800;color:var(--success)">${Formatters.money(totalNetSales)}</div>
522
</div>
523
</div>
524
<div class="card kpi-card" style="margin:0">
525
<div class="kpi-card-icon" style="background:#fef2f2;color:var(--danger)">⏳</div>
526
<div style="flex:1">
522
- <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">مجموع مطالبات</div>
527
+ <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">مانده فاکتورهای فروش قبل از مرجوعی</div>
528
<div style="font-size:16px;font-weight:800;color:var(--danger)">${Formatters.money(totalRemaining)}</div>
529
</div>
530
</div>
531
</div>
532
533
<div class="card">
529
- <h3 style="font-size:15px;font-weight:700;margin-bottom:14px">👥 گزارش تفصیلی مشتریان</h3>
534
+ <h3 style="font-size:15px;font-weight:700;margin-bottom:8px">👥 گزارش تفصیلی مشتریان</h3>
535
+ <p role="note" style="font-size:12px;line-height:1.8;color:var(--text-muted);margin:0 0 12px">
536
+ فروش خالص، مرجوعی را از فروش ناخالص کم میکند. مانده این جدول فقط جمع مانده فاکتورهای فروش است و اعتبار مرجوعی یا بازپرداخت را از دفتر کل محاسبه نمیکند؛ برای مانده قطعی حساب، دفتر کل مبناست.
537
+ </p>
538
<div class="table-wrap">
539
<table>
540
<thead>
@@ -534,24 +542,28 @@ class ReportsViewImpl {
542
<th style="width:30px">#</th>
543
<th>نام مشتری</th>
544
<th style="width:100px">نوع</th>
537
- <th style="width:110px">تعداد فاکتور</th>
538
- <th style="width:140px">مجموع خرید</th>
539
- <th style="width:140px">پرداختشده</th>
540
- <th style="width:140px">مانده</th>
541
- <th style="width:120px">آخرین فاکتور</th>
545
+ <th style="width:110px">تعداد فاکتور فروش</th>
546
+ <th style="width:130px">فروش ناخالص</th>
547
+ <th style="width:120px">مرجوعی</th>
548
+ <th style="width:130px">فروش خالص</th>
549
+ <th style="width:130px">دریافت از فروش</th>
550
+ <th style="width:160px">مانده فاکتور فروش</th>
551
+ <th style="width:120px">آخرین سند</th>
552
</tr>
553
</thead>
554
<tbody>
545
- ${list.length === 0 ? '<tr><td colspan="8" style="text-align:center;padding:30px;color:var(--text-muted)">دادهای نیست</td></tr>' : list.map((c, i) => `
555
+ ${list.length === 0 ? '<tr><td colspan="10" style="text-align:center;padding:30px;color:var(--text-muted)">دادهای نیست</td></tr>' : list.map((customer, i) => `
556
<tr>
557
<td><strong>${Formatters.toPersianDigits(i + 1)}</strong></td>
548
- <td>${this._esc(c.contactName)}</td>
549
- <td><span class="badge ${c.entityType === 'legal' ? 'badge-warning' : 'badge-success'}">${c.entityType === 'legal' ? 'حقوقی' : 'حقیقی'}</span></td>
550
- <td>${Formatters.number(c.totalInvoices)}</td>
551
- <td style="font-weight:600;color:var(--primary)">${Formatters.money(c.totalSales)}</td>
552
- <td style="color:var(--success)">${Formatters.money(c.totalPaid)}</td>
553
- <td style="font-weight:700;color:${c.totalRemaining > 0 ? 'var(--danger)' : 'var(--success)'}">${Formatters.money(c.totalRemaining)}</td>
554
- <td>${c.lastInvoiceDate ? Formatters.toPersianDigits(c.lastInvoiceDate) : '—'}</td>
558
+ <td>${this._esc(customer.contactName)}</td>
559
+ <td><span class="badge ${customer.entityType === 'legal' ? 'badge-warning' : 'badge-success'}">${customer.entityType === 'legal' ? 'حقوقی' : 'حقیقی'}</span></td>
560
+ <td>${Formatters.number(customer.totalInvoices)}</td>
561
+ <td style="font-weight:600;color:var(--primary)">${Formatters.money(customer.totalSales)}</td>
562
+ <td style="color:var(--danger)">${Formatters.money(customer.totalReturns)}</td>
563
+ <td style="font-weight:700;color:${customer.netSales < 0 ? 'var(--danger)' : 'var(--primary)'}">${Formatters.money(customer.netSales)}</td>
564
+ <td style="color:var(--success)">${Formatters.money(customer.totalPaid)}</td>
565
+ <td style="font-weight:700;color:${customer.totalRemaining > 0 ? 'var(--danger)' : 'var(--success)'}">${Formatters.money(customer.totalRemaining)}</td>
566
+ <td>${customer.lastInvoiceDate ? Formatters.toPersianDigits(customer.lastInvoiceDate) : '—'}</td>
567
</tr>
568
`).join('')}
569
</tbody>
@@ -661,18 +673,21 @@ class ReportsViewImpl {
673
if (currentTab === 'customers') {
674
const list = cache.customers || [];
675
return `
664
- <h2>گزارش مشتریان (${Formatters.number(list.length)} مشتری)</h2>
676
+ <h2>گزارش مشتریان پس از کسر مرجوعی</h2>
677
+ <p>مانده، جمع مانده فاکتورهای فروش قبل از تعدیل مرجوعی است؛ مانده قطعی حساب از دفتر کل خوانده شود.</p>
678
<table>
666
- <thead><tr><th>#</th><th>نام مشتری</th><th>تعداد فاکتور</th><th>مجموع خرید</th><th>پرداختشده</th><th>مانده</th></tr></thead>
679
+ <thead><tr><th>#</th><th>نام مشتری</th><th>فاکتور فروش</th><th>فروش ناخالص</th><th>مرجوعی</th><th>فروش خالص</th><th>دریافت از فروش</th><th>مانده فاکتور فروش</th></tr></thead>
680
<tbody>
668
- ${list.map((c, i) => `
681
+ ${list.map((customer, i) => `
682
<tr>
683
<td>${Formatters.toPersianDigits(i + 1)}</td>
671
- <td>${this._esc(c.contactName)}</td>
672
- <td>${Formatters.number(c.totalInvoices)}</td>
673
- <td>${Formatters.money(c.totalSales)}</td>
674
- <td>${Formatters.money(c.totalPaid)}</td>
675
- <td>${Formatters.money(c.totalRemaining)}</td>
684
+ <td>${this._esc(customer.contactName)}</td>
685
+ <td>${Formatters.number(customer.totalInvoices)}</td>
686
+ <td>${Formatters.money(customer.totalSales)}</td>
687
+ <td>${Formatters.money(customer.totalReturns)}</td>
688
+ <td>${Formatters.money(customer.netSales)}</td>
689
+ <td>${Formatters.money(customer.totalPaid)}</td>
690
+ <td>${Formatters.money(customer.totalRemaining)}</td>
691
</tr>
692
`).join('')}
693
</tbody>
@@ -684,21 +699,22 @@ class ReportsViewImpl {
699
const topP = cache.sales?.topProductsBySales || [];
700
const topC = cache.sales?.topCustomers || [];
701
return `
687
- <h2>۱۰ کالای پرفروش</h2>
702
+ <h2>۱۰ کالای پرفروش (خالص از مرجوعی)</h2>
703
<table>
689
- <thead><tr><th>#</th><th>نام کالا</th><th>تعداد</th><th>مبلغ فروش</th></tr></thead>
704
+ <thead><tr><th>#</th><th>نام کالا</th><th>تعداد خالص</th><th>مبلغ خالص فروش</th></tr></thead>
705
<tbody>
706
${topP.map((p, i) => `
707
<tr><td>${Formatters.toPersianDigits(i + 1)}</td><td>${this._esc(p.productName)}</td><td>${Formatters.number(p.qty)}</td><td>${Formatters.money(p.totalSales)}</td></tr>
708
`).join('')}
709
</tbody>
710
</table>
696
- <h2>۱۰ مشتری برتر</h2>
711
+ <h2>۱۰ مشتری برتر (خرید خالص)</h2>
712
+ <p>مانده، جمع مانده فاکتورهای فروش قبل از تعدیل مرجوعی است.</p>
713
<table>
698
- <thead><tr><th>#</th><th>نام مشتری</th><th>تعداد فاکتور</th><th>مجموع خرید</th></tr></thead>
714
+ <thead><tr><th>#</th><th>نام مشتری</th><th>تعداد فاکتور</th><th>فروش ناخالص</th><th>مرجوعی</th><th>فروش خالص</th><th>دریافت از فروش</th><th>مانده فاکتور فروش</th></tr></thead>
715
<tbody>
700
- ${topC.map((c, i) => `
701
- <tr><td>${Formatters.toPersianDigits(i + 1)}</td><td>${this._esc(c.contactName)}</td><td>${Formatters.number(c.invoiceCount)}</td><td>${Formatters.money(c.totalSales)}</td></tr>
716
+ ${topC.map((customer, i) => `
717
+ <tr><td>${Formatters.toPersianDigits(i + 1)}</td><td>${this._esc(customer.contactName)}</td><td>${Formatters.number(customer.invoiceCount)}</td><td>${Formatters.money(customer.totalSales)}</td><td>${Formatters.money(customer.totalReturns)}</td><td>${Formatters.money(customer.netSales)}</td><td>${Formatters.money(customer.totalPaid)}</td><td>${Formatters.money(customer.totalRemaining)}</td></tr>
718
`).join('')}
719
</tbody>
720
</table>
tests/report-controller-bundle.test.mjs
+47
@@ -55,6 +55,7 @@ test('report bundle reads one company-scoped snapshot and preserves all four rep
55
assert.equal(bundle.sales.invoiceCount, 1);
56
assert.equal(bundle.products.length, 1);
57
assert.equal(bundle.customers[0].totalReturns, 10);
58
+ assert.equal(bundle.customers[0].netSales, 80);
59
assert.equal(bundle.pl.costBasis, 'estimated');
60
assert.equal(bundle.products[0].totalSales, 80);
61
assert.equal(bundle.products[0].cogs, 20);
@@ -223,3 +224,49 @@ test('a missing historical and current product cost is explicitly classified as
224
ReportController._loadData = originalLoad;
225
}
226
});
227
+
228
+
229
+test('customer reports subtract returns from net sales, include return-only customers and disclose gross invoice balances', async () => {
230
+ const originalLoad = ReportController._loadData;
231
+ const data = {
232
+ invoices: [
233
+ { id:'customer-sale', kind:'sale', isPreInvoice:false, date:'1405/01/02',
234
+ contactId:'customer-1', contactName:'Customer one', grandTotal:1000,
235
+ paidAmount:400, remaining:600 },
236
+ { id:'customer-return', kind:'sale_return', isPreInvoice:false, date:'1405/01/03',
237
+ contactId:'customer-1', contactName:'Customer one', grandTotal:250 },
238
+ { id:'return-only', kind:'sale_return', isPreInvoice:false, date:'1405/01/04',
239
+ contactId:'customer-2', contactName:'Customer two', grandTotal:50 },
240
+ { id:'supplier-purchase', kind:'purchase', isPreInvoice:false, date:'1405/01/05',
241
+ contactId:'supplier-1', contactName:'Supplier only', grandTotal:9999 }
242
+ ],
243
+ contacts: [
244
+ { id:'customer-1', name:'Customer one', entityType:'natural' },
245
+ { id:'customer-2', name:'Customer two', entityType:'legal' },
246
+ { id:'supplier-1', name:'Supplier only', entityType:'legal' }
247
+ ],
248
+ products: [], expenses: [], stockMovements: [], cheques: [], treasury: []
249
+ };
250
+ ReportController._loadData = async () => data;
251
+ try {
252
+ const customers = await ReportController.getCustomerReport(filter);
253
+ assert.equal(customers.length, 2, 'purchase-only suppliers must not appear in customer sales reports');
254
+ assert.deepEqual(
255
+ { gross: customers[0].totalSales, returns: customers[0].totalReturns,
256
+ net: customers[0].netSales, paid: customers[0].totalPaid,
257
+ invoiceBalanceBeforeReturns: customers[0].totalRemaining },
258
+ { gross: 1000, returns: 250, net: 750, paid: 400, invoiceBalanceBeforeReturns: 600 }
259
+ );
260
+ assert.deepEqual(
261
+ { name: customers[1].contactName, gross: customers[1].totalSales,
262
+ returns: customers[1].totalReturns, net: customers[1].netSales },
263
+ { name: 'Customer two', gross: 0, returns: 50, net: -50 }
264
+ );
265
+ const sales = await ReportController.getSalesAnalysis(filter);
266
+ assert.equal(sales.topCustomers[0].netSales, 750);
267
+ assert.equal(sales.topCustomers[0].totalReturns, 250);
268
+ assert.equal(sales.topCustomers[1].netSales, -50);
269
+ } finally {
270
+ ReportController._loadData = originalLoad;
271
+ }
272
+});