This commit is contained in:
2024-11-19 12:58:08 +01:00
parent 44fe789f5d
commit 7b07f0861a
3 changed files with 59 additions and 10 deletions
@@ -24,6 +24,7 @@ export default defineComponent({
const accountService = inject<AccountService>('accountService');
const isFetching = ref(false);
const sums: Ref<number[]> = ref([]);
const clear = () => {};
// console.log(props.userAccountId);
@@ -34,6 +35,7 @@ export default defineComponent({
const res = await transactionService().retrieve();
// const res = await transactionService().retrieveForAccount(props.userAccountId);
transactions.value = res.data;
sums.value = sumAmountsPerColumn(transactions.value);
} catch (err) {
alertService.showHttpError(err.response);
} finally {
@@ -71,12 +73,32 @@ export default defineComponent({
}
};
function sumAmountsPerColumn(transactions: ITransaction[]): number[] {
// Jeżeli nie ma transakcji, zwracamy pustą tablicę
if (transactions.length === 0) return [];
// Liczba kolumn to liczba transactionItems w pierwszej transakcji
const columnCount = transactions[0].items.length;
// Inicjalizacja tablicy sum z zerami dla każdej kolumny
const sums = new Array(columnCount).fill(0);
// Iteracja przez każdą transakcję
for (const transaction of transactions) {
for (let i = 0; i < columnCount; i++) {
// Dodajemy amount z każdego transactionItem do odpowiedniej kolumny
sums[i] += transaction.items[i].amount;
}
}
return sums;
}
return {
transactions,
handleSyncList,
isFetching,
retrieveTransactions,
clear,
sums,
removeId,
accountService,
hasAnyAuthorityValues,