-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransaction-mapper.ts
183 lines (171 loc) · 6.06 KB
/
transaction-mapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the transaction-mapper.
*
* @module helpers
*/
import SubTransactionRow from '../entity/transactions/sub-transaction-row';
import Transaction from '../entity/transactions/transaction';
import {
TransactionReportCategoryEntry,
TransactionReportEntry, TransactionReportVatEntry,
} from '../controller/response/transaction-report-response';
import Dinero from 'dinero.js';
import SubTransaction from '../entity/transactions/sub-transaction';
/**
* Applies a function to all SubTransactionRows of the given transactions.
* @param transactions
* @param callbackfn
*/
export function transactionMapper(transactions: Transaction[], callbackfn: (subTransactionRow: SubTransactionRow) => any) {
transactions.forEach((t) => {
t.subTransactions.forEach((tSub) => {
tSub.subTransactionRows.forEach(callbackfn);
});
});
}
/**
* Function that collects items based on a given getKey function
* @param map
* @param item
* @param getKey
*/
export function collectByKey<T>(map: Map<string | number, T[]>, item: T, getKey: (item: T) => string | number) {
const key = getKey(item);
if (map.has(key)) {
map.get(key).push(item);
} else {
map.set(key, [item]);
}
}
/**
* Function that collects transactions by products based on their revision
* @param productMap
* @param tSubRow
*/
export function collectProductsByRevision(productMap: Map<string, SubTransactionRow[]>, tSubRow: SubTransactionRow) {
const getKey = (item: SubTransactionRow) => JSON.stringify({
revision: item.product.revision,
id: item.product.product.id,
});
collectByKey<SubTransactionRow>(productMap, tSubRow, getKey);
}
/**
* Function that collects transactions by product based on their category id
* @param categoryMap
* @param tSubRow
*/
export function collectProductsByCategory(categoryMap: Map<number, SubTransactionRow[]>, tSubRow: SubTransactionRow) {
const getKey = (item: SubTransactionRow) => item.product.category.id;
collectByKey<SubTransactionRow>(categoryMap, tSubRow, getKey);
}
/**
* Function that collects transactions by product based on their Vat group
* @param vatMap
* @param tSubRow
*/
export function collectProductsByVat(vatMap: Map<number, SubTransactionRow[]>, tSubRow: SubTransactionRow) {
const getKey = (item: SubTransactionRow) => item.product.vat.id;
collectByKey<SubTransactionRow>(vatMap, tSubRow, getKey);
}
/**
* Function that collects transaction by user ToId
* @param toIdMap
* @param tSub
*/
export function collectByToId(toIdMap: Map<number, SubTransaction[]>, tSub: SubTransaction) {
const getKey = (item: SubTransaction) => item.to.id;
collectByKey<SubTransaction>(toIdMap, tSub, getKey);
}
/**
* Transforms an array of SubTransactionRows of a single product to TransactionReportEntries
* @param productMap
*/
export function reduceMapToReportEntries(productMap: Map<string, SubTransactionRow[]>): TransactionReportEntry[] {
const transactionReportEntries: TransactionReportEntry[] = [];
productMap.forEach((value) => {
const count = value.reduce((sum, current) => {
return sum + current.amount;
}, 0);
const entry: TransactionReportEntry = {
count,
product: value[0].product,
};
transactionReportEntries.push(entry);
});
return transactionReportEntries;
}
/**
* Extracts the total including and excluding vat from the given transactions
* @param subTransactionRows
*/
function extractVatInfoTransactions(subTransactionRows: SubTransactionRow[]) {
let totalInclVat = 0;
let totalExclVat = 0;
subTransactionRows.forEach((row) => {
const amountInclVat = row.product.priceInclVat.getAmount() * row.amount;
totalInclVat += amountInclVat;
const amountExclVat: number = amountInclVat / (1 + (row.product.vat.percentage / 100));
totalExclVat += amountExclVat;
});
return {
totalInclVat,
totalExclVat,
};
}
/**
* Transforms an array of SubTransactionRows with the same VAT group into TransactionReportVatEntries
* @param vatMap
*/
export function reduceMapToVatEntries(vatMap: Map<number, SubTransactionRow[]>): TransactionReportVatEntry[] {
const transactionReportEntries: TransactionReportVatEntry[] = [];
vatMap.forEach((value) => {
const info = extractVatInfoTransactions(value);
const totalInclVat = info.totalInclVat;
const totalExclVat = info.totalExclVat;
const entry: TransactionReportVatEntry = {
vat: value[0].product.vat,
totalExclVat,
totalInclVat: Dinero({ amount: totalInclVat }),
};
transactionReportEntries.push(entry);
});
return transactionReportEntries;
}
/**
* Transforms an array of SubTransactionRows with the same product category into TransactionReportCategoryEntries
* @param categoryMap
*/
export function reduceMapToCategoryEntries(categoryMap: Map<number, SubTransactionRow[]>): TransactionReportCategoryEntry[] {
const transactionReportEntries: TransactionReportCategoryEntry[] = [];
categoryMap.forEach((value) => {
const info = extractVatInfoTransactions(value);
const totalInclVat = info.totalInclVat;
const totalExclVat = info.totalExclVat;
const entry: TransactionReportCategoryEntry = {
category: value[0].product.category,
totalExclVat,
totalInclVat: Dinero({ amount: totalInclVat }),
};
transactionReportEntries.push(entry);
});
return transactionReportEntries;
}