From cc7432b2534aa1c98b51eb5b311dbaa9088a2214 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 10 Dec 2024 10:38:43 +0100 Subject: [PATCH] refactor: refs #7936 simplify getTotal fn --- src/composables/getTotal.js | 5 ++--- src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue | 2 +- test/vitest/__tests__/composables/getTotal.spec.js | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/composables/getTotal.js b/src/composables/getTotal.js index 91b884bda..24ac3aa27 100644 --- a/src/composables/getTotal.js +++ b/src/composables/getTotal.js @@ -1,11 +1,10 @@ import { toCurrency } from 'src/filters'; 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 decimals = int ? 0 : decimalPlaces ?? 2; return currency ? toCurrency(total, currency == 'default' ? undefined : currency) - : parseFloat(total).toFixed(decimals); + : parseFloat(total).toFixed(decimalPlaces ?? 2); } diff --git a/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue b/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue index fee65daaa..1c4091169 100644 --- a/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue +++ b/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue @@ -146,7 +146,7 @@ const columns = computed(() => [ {{ getTotal(rows, 'net') }} - {{ getTotal(rows, 'stems', { int: true }) }} + {{ getTotal(rows, 'stems', { decimalPlaces: 0 }) }} diff --git a/test/vitest/__tests__/composables/getTotal.spec.js b/test/vitest/__tests__/composables/getTotal.spec.js index 7081c4334..789e3fbcf 100644 --- a/test/vitest/__tests__/composables/getTotal.spec.js +++ b/test/vitest/__tests__/composables/getTotal.spec.js @@ -33,7 +33,7 @@ describe('getTotal()', () => { }); 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'); });