refactor: refs #7936 simplify getTotal fn
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-12-10 10:38:43 +01:00
parent 2939ddcfb7
commit cc7432b253
3 changed files with 4 additions and 5 deletions

View File

@ -1,11 +1,10 @@
import { toCurrency } from 'src/filters'; import { toCurrency } from 'src/filters';
export function getTotal(rows, key, opts = {}) { export function getTotal(rows, key, opts = {}) {
const { currency, cb, decimalPlaces, int } = opts; const { currency, cb, decimalPlaces } = opts;
const total = rows.reduce((acc, row) => acc + +(cb ? cb(row) : row[key] || 0), 0); const total = rows.reduce((acc, row) => acc + +(cb ? cb(row) : row[key] || 0), 0);
const decimals = int ? 0 : decimalPlaces ?? 2;
return currency return currency
? toCurrency(total, currency == 'default' ? undefined : currency) ? toCurrency(total, currency == 'default' ? undefined : currency)
: parseFloat(total).toFixed(decimals); : parseFloat(total).toFixed(decimalPlaces ?? 2);
} }

View File

@ -146,7 +146,7 @@ const columns = computed(() => [
{{ getTotal(rows, 'net') }} {{ getTotal(rows, 'net') }}
</QTd> </QTd>
<QTd> <QTd>
{{ getTotal(rows, 'stems', { int: true }) }} {{ getTotal(rows, 'stems', { decimalPlaces: 0 }) }}
</QTd> </QTd>
<QTd /> <QTd />
</QTr> </QTr>

View File

@ -33,7 +33,7 @@ describe('getTotal()', () => {
}); });
it('should calculate the total with integer formatting', () => { it('should calculate the total with integer formatting', () => {
const total = getTotal(rows, 'amount', { int: true }); const total = getTotal(rows, 'amount', { decimalPlaces: 0 });
expect(total).toBe('62'); expect(total).toBe('62');
}); });