bkper-js 2.28.0 → 2.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ import { AccountType } from '../model/Enums.js';
2
+ import { convertInMatrix } from '../utils.js';
3
+ /**
4
+ * A GroupsDataTableBuilder is used to setup and build two-dimensional arrays containing groups.
5
+ *
6
+ * @public
7
+ */
8
+ export class GroupsDataTableBuilder {
9
+ constructor(groups) {
10
+ this.COMPARATOR = (g1, g2) => {
11
+ let ret = this.getTypeIndex(this.getStringType(g1)) - this.getTypeIndex(this.getStringType(g2));
12
+ if (ret === 0) {
13
+ ret =
14
+ this.getHasChildrenIndex(g1.hasChildren()) -
15
+ this.getHasChildrenIndex(g2.hasChildren());
16
+ }
17
+ if (ret === 0) {
18
+ ret = g1.getNormalizedName().localeCompare(g2.getNormalizedName());
19
+ }
20
+ return ret;
21
+ };
22
+ this.groups = groups;
23
+ this.shouldAddProperties = false;
24
+ this.shouldAddIds = false;
25
+ this.shouldAddHiddenProperties = false;
26
+ this.shouldShowTree = false;
27
+ this.propertyKeys = [];
28
+ }
29
+ /**
30
+ * Defines whether include custom group properties.
31
+ *
32
+ * @param include - Whether to include properties
33
+ *
34
+ * @returns This builder with respective include properties option, for chaining.
35
+ */
36
+ properties(include) {
37
+ this.shouldAddProperties = include;
38
+ return this;
39
+ }
40
+ /**
41
+ * Defines whether include group ids.
42
+ *
43
+ * @param include - Whether to include ids
44
+ *
45
+ * @returns This builder with respective include ids option, for chaining.
46
+ */
47
+ ids(include) {
48
+ this.shouldAddIds = include;
49
+ return this;
50
+ }
51
+ /**
52
+ * Defines whether to include hidden properties (keys ending with underscore "_").
53
+ * Only relevant when {@link properties} is enabled.
54
+ * Default is false — hidden properties are excluded.
55
+ *
56
+ * @param include - Whether to include hidden properties
57
+ *
58
+ * @returns This builder with respective option, for chaining.
59
+ */
60
+ hiddenProperties(include) {
61
+ this.shouldAddHiddenProperties = include;
62
+ return this;
63
+ }
64
+ /**
65
+ * Defines whether to render groups as an indented tree instead of flat rows with a Parent column.
66
+ * When enabled, child group names are indented by depth level and the Parent column is removed.
67
+ * Default is false.
68
+ *
69
+ * @param enable - Whether to enable tree rendering
70
+ *
71
+ * @returns This builder with respective option, for chaining.
72
+ */
73
+ tree(enable) {
74
+ this.shouldShowTree = enable;
75
+ return this;
76
+ }
77
+ mapPropertyKeys() {
78
+ this.propertyKeys = [];
79
+ for (const group of this.groups) {
80
+ for (const key of group.getPropertyKeys()) {
81
+ if (!this.shouldAddHiddenProperties && key.endsWith('_')) {
82
+ continue;
83
+ }
84
+ if (this.propertyKeys.indexOf(key) <= -1) {
85
+ this.propertyKeys.push(key);
86
+ }
87
+ }
88
+ }
89
+ this.propertyKeys = this.propertyKeys.sort();
90
+ }
91
+ getStringType(group) {
92
+ let groupType = group.getType() ? group.getType() + '' : undefined;
93
+ if (!groupType) {
94
+ groupType = group.isPermanent()
95
+ ? AccountType.ASSET + '_' + AccountType.LIABILITY
96
+ : AccountType.INCOMING + '_' + AccountType.OUTGOING;
97
+ }
98
+ return groupType;
99
+ }
100
+ getTypeIndex(type) {
101
+ if (type === AccountType.ASSET + '_' + AccountType.LIABILITY) {
102
+ return 0;
103
+ }
104
+ if (type === AccountType.ASSET) {
105
+ return 1;
106
+ }
107
+ if (type === AccountType.LIABILITY) {
108
+ return 2;
109
+ }
110
+ if (type === AccountType.INCOMING + '_' + AccountType.OUTGOING) {
111
+ return 3;
112
+ }
113
+ if (type === AccountType.INCOMING) {
114
+ return 4;
115
+ }
116
+ return 5;
117
+ }
118
+ getHasChildrenIndex(hasChildren) {
119
+ return hasChildren ? 0 : 1;
120
+ }
121
+ /**
122
+ * Builds a two-dimensional array containing all Groups.
123
+ *
124
+ * @returns A two-dimensional array containing all Groups
125
+ */
126
+ build() {
127
+ let table = new Array();
128
+ const groups = this.groups;
129
+ groups.sort(this.COMPARATOR);
130
+ let headers = [];
131
+ if (this.shouldAddIds) {
132
+ headers.push('Group Id');
133
+ }
134
+ headers.push('Name');
135
+ headers.push('Type');
136
+ if (!this.shouldShowTree) {
137
+ headers.push('Parent');
138
+ }
139
+ if (this.shouldAddProperties) {
140
+ this.mapPropertyKeys();
141
+ }
142
+ for (const group of groups) {
143
+ if (group.isHidden() || group.getParent()) {
144
+ continue;
145
+ }
146
+ table.push(this.buildGroupLine(group, 0));
147
+ table = this.traverse(group, table, 1);
148
+ }
149
+ if (this.shouldAddProperties) {
150
+ headers = headers.concat(this.propertyKeys);
151
+ }
152
+ table.unshift(headers);
153
+ convertInMatrix(table);
154
+ return table;
155
+ }
156
+ buildGroupLine(group, depth) {
157
+ const line = [];
158
+ if (this.shouldAddIds) {
159
+ line.push(group.getId());
160
+ }
161
+ const name = this.shouldShowTree ? ' '.repeat(depth) + group.getName() : group.getName();
162
+ line.push(name);
163
+ line.push(this.getStringType(group));
164
+ if (!this.shouldShowTree) {
165
+ const parentName = group.getParent() ? group.getParent().getName() : '';
166
+ line.push(parentName);
167
+ }
168
+ if (this.shouldAddProperties) {
169
+ const properties = group.getProperties();
170
+ for (const key of this.propertyKeys) {
171
+ const propertyValue = properties[key];
172
+ if (propertyValue) {
173
+ line.push(propertyValue);
174
+ continue;
175
+ }
176
+ line.push('');
177
+ }
178
+ }
179
+ return line;
180
+ }
181
+ traverse(group, table, depth) {
182
+ const children = group.getChildren();
183
+ children.sort(this.COMPARATOR);
184
+ for (const child of children) {
185
+ table.push(this.buildGroupLine(child, depth));
186
+ if (child.hasChildren()) {
187
+ this.traverse(child, table, depth + 1);
188
+ }
189
+ }
190
+ return table;
191
+ }
192
+ }
193
+ //# sourceMappingURL=GroupsDataTableBuilder.js.map
@@ -0,0 +1,359 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { convertInMatrix, formatValue, getRepresentativeValue } from '../utils.js';
11
+ /**
12
+ * A TransactionsDataTableBuilder is used to setup and build two-dimensional arrays containing transactions.
13
+ *
14
+ * @public
15
+ */
16
+ export class TransactionsDataTableBuilder {
17
+ constructor(book, transactions, account) {
18
+ this.shouldFormatDates = false;
19
+ this.shouldFormatValues = false;
20
+ this.shouldAddUrls = false;
21
+ this.shouldAddProperties = false;
22
+ this.shouldAddIds = false;
23
+ this.shouldAddRecordedAt = true;
24
+ this.shouldAddHiddenProperties = false;
25
+ this.book = book;
26
+ this.transactions = transactions;
27
+ this.account = account;
28
+ }
29
+ /**
30
+ * Defines whether the dates should be formatted, based on date pattern of the [[Book]].
31
+ *
32
+ * @param format - Whether to format dates
33
+ *
34
+ * @returns This builder with respective formatting option, for chaining.
35
+ */
36
+ formatDates(format) {
37
+ this.shouldFormatDates = format;
38
+ return this;
39
+ }
40
+ /**
41
+ * Defines whether amounts should be formatted based on [[DecimalSeparator]] of the [[Book]].
42
+ *
43
+ * @param format - Whether to format values
44
+ *
45
+ * @returns This builder with respective formatting option, for chaining.
46
+ */
47
+ formatValues(format) {
48
+ this.shouldFormatValues = format;
49
+ return this;
50
+ }
51
+ /**
52
+ * Defines whether to include attachments and url links.
53
+ *
54
+ * @param include - Whether to include URLs
55
+ *
56
+ * @returns This builder with respective option, for chaining.
57
+ */
58
+ urls(include) {
59
+ this.shouldAddUrls = include;
60
+ return this;
61
+ }
62
+ /**
63
+ * Defines whether to include custom transaction properties.
64
+ *
65
+ * @param include - Whether to include properties
66
+ *
67
+ * @returns This builder with respective option, for chaining.
68
+ */
69
+ properties(include) {
70
+ this.shouldAddProperties = include;
71
+ return this;
72
+ }
73
+ /**
74
+ * Defines whether to include transaction ids and remote ids.
75
+ *
76
+ * @param include - Whether to include ids
77
+ *
78
+ * @returns This builder with respective option, for chaining.
79
+ */
80
+ ids(include) {
81
+ this.shouldAddIds = include;
82
+ return this;
83
+ }
84
+ /**
85
+ * Defines whether to include the "Recorded at" column.
86
+ *
87
+ * @param include - Whether to include the recorded at column
88
+ *
89
+ * @returns This builder with respective option, for chaining.
90
+ */
91
+ recordedAt(include) {
92
+ this.shouldAddRecordedAt = include;
93
+ return this;
94
+ }
95
+ /**
96
+ * Defines whether to include hidden properties (keys ending with underscore "_").
97
+ * Only relevant when {@link properties} is enabled.
98
+ * Default is false — hidden properties are excluded.
99
+ *
100
+ * @param include - Whether to include hidden properties
101
+ *
102
+ * @returns This builder with respective option, for chaining.
103
+ */
104
+ hiddenProperties(include) {
105
+ this.shouldAddHiddenProperties = include;
106
+ return this;
107
+ }
108
+ /**
109
+ * @deprecated Use {@link urls} instead.
110
+ */
111
+ includeUrls(include) {
112
+ return this.urls(include);
113
+ }
114
+ /**
115
+ * @deprecated Use {@link properties} instead.
116
+ */
117
+ includeProperties(include) {
118
+ return this.properties(include);
119
+ }
120
+ /**
121
+ * @deprecated Use {@link ids} instead.
122
+ */
123
+ includeIds(include) {
124
+ return this.ids(include);
125
+ }
126
+ /**
127
+ * Gets the account used to filter transactions, when applicable.
128
+ *
129
+ * @returns The account, when filtering by a single account.
130
+ */
131
+ getAccount() {
132
+ return this.account;
133
+ }
134
+ /**
135
+ * Builds a two-dimensional array containing all transactions.
136
+ *
137
+ * @returns A promise resolving to a two-dimensional array containing all transactions
138
+ */
139
+ build() {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ const headerLine = this.getHeaderLine();
142
+ const dataTable = yield this.get2DArray_();
143
+ if (dataTable.length > 0) {
144
+ dataTable.splice(0, 0, headerLine);
145
+ convertInMatrix(dataTable);
146
+ return dataTable;
147
+ }
148
+ else {
149
+ return [headerLine];
150
+ }
151
+ });
152
+ }
153
+ getHeaderLine() {
154
+ const headerLine = [];
155
+ if (this.shouldAddIds) {
156
+ headerLine.push('Transaction Id');
157
+ }
158
+ headerLine.push('Status');
159
+ headerLine.push('Date');
160
+ headerLine.push('Origin');
161
+ headerLine.push('Destination');
162
+ headerLine.push('Description');
163
+ headerLine.push('Amount');
164
+ if (this.shouldShowBalances()) {
165
+ headerLine.push('Balance');
166
+ }
167
+ if (this.shouldAddRecordedAt) {
168
+ headerLine.push('Recorded at');
169
+ }
170
+ if (this.shouldAddProperties) {
171
+ for (const key of this.getPropertyKeys()) {
172
+ headerLine.push(key);
173
+ }
174
+ }
175
+ if (this.shouldAddIds) {
176
+ for (const remoteIdHeader of this.getRemoteIdHeaders()) {
177
+ headerLine.push(remoteIdHeader);
178
+ }
179
+ }
180
+ if (this.shouldAddUrls) {
181
+ for (const attachmentHeader of this.getAttachmentHeaders()) {
182
+ headerLine.push(attachmentHeader);
183
+ }
184
+ }
185
+ return headerLine;
186
+ }
187
+ get2DArray_() {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ const dataTable = [];
190
+ for (const transaction of this.transactions) {
191
+ const line = [];
192
+ if (this.shouldAddIds) {
193
+ line.push(transaction.getId());
194
+ }
195
+ line.push(transaction.getStatus());
196
+ if (this.shouldFormatDates) {
197
+ line.push(transaction.getDateFormatted());
198
+ }
199
+ else {
200
+ line.push(transaction.getDateObject());
201
+ }
202
+ line.push(yield transaction.getCreditAccountName());
203
+ line.push(yield transaction.getDebitAccountName());
204
+ line.push(transaction.getDescription());
205
+ let amount = transaction.getAmount();
206
+ if (amount != null) {
207
+ if (this.shouldShowBalances() && this.account) {
208
+ const isCreditOnTx = yield this.isCreditOnTransaction_(transaction, this.account);
209
+ amount = getRepresentativeValue(amount, !isCreditOnTx);
210
+ }
211
+ if (this.shouldFormatValues) {
212
+ line.push(formatValue(amount, this.book.getDecimalSeparator(), this.book.getFractionDigits()));
213
+ }
214
+ else {
215
+ line.push(amount.toNumber());
216
+ }
217
+ }
218
+ else {
219
+ line.push('');
220
+ }
221
+ if (this.shouldShowBalances()) {
222
+ const accountBalance = yield transaction.getAccountBalance();
223
+ if (accountBalance != null) {
224
+ if (this.shouldFormatValues) {
225
+ line.push(formatValue(accountBalance, this.book.getDecimalSeparator(), this.book.getFractionDigits()));
226
+ }
227
+ else {
228
+ line.push(accountBalance.toNumber());
229
+ }
230
+ }
231
+ else {
232
+ line.push('');
233
+ }
234
+ }
235
+ if (this.shouldAddRecordedAt) {
236
+ if (this.shouldFormatDates) {
237
+ line.push(transaction.getCreatedAtFormatted());
238
+ }
239
+ else {
240
+ line.push(transaction.getCreatedAt());
241
+ }
242
+ }
243
+ if (this.shouldAddProperties) {
244
+ this.addPropertiesToLine(line, transaction);
245
+ }
246
+ if (this.shouldAddIds) {
247
+ this.addRemoteIdsToLine(line, transaction);
248
+ }
249
+ if (this.shouldAddUrls) {
250
+ this.addUrlsToLine(line, transaction);
251
+ }
252
+ dataTable.push(line);
253
+ }
254
+ return dataTable;
255
+ });
256
+ }
257
+ shouldShowBalances() {
258
+ return this.account != null && this.account.isPermanent() === true;
259
+ }
260
+ isCreditOnTransaction_(transaction, account) {
261
+ return __awaiter(this, void 0, void 0, function* () {
262
+ const creditAccount = yield transaction.getCreditAccount();
263
+ if (account == null || creditAccount == null) {
264
+ return false;
265
+ }
266
+ return creditAccount.getId() === account.getId();
267
+ });
268
+ }
269
+ getPropertyKeys() {
270
+ if (this.propertyKeys == null) {
271
+ this.propertyKeys = [];
272
+ for (const transaction of this.transactions) {
273
+ for (const key of transaction.getPropertyKeys()) {
274
+ if (!this.shouldAddHiddenProperties && key.endsWith('_')) {
275
+ continue;
276
+ }
277
+ if (this.propertyKeys.indexOf(key) <= -1) {
278
+ this.propertyKeys.push(key);
279
+ }
280
+ }
281
+ }
282
+ this.propertyKeys = this.propertyKeys.sort();
283
+ }
284
+ return this.propertyKeys;
285
+ }
286
+ getAttachmentHeaders() {
287
+ if (this.attachmentHeaders == null) {
288
+ this.attachmentHeaders = [];
289
+ for (const transaction of this.transactions) {
290
+ const urls = this.getUrls(transaction);
291
+ if (urls.length > this.attachmentHeaders.length) {
292
+ this.attachmentHeaders = [];
293
+ urls.forEach(() => this.attachmentHeaders.push('Attachment'));
294
+ }
295
+ }
296
+ }
297
+ return this.attachmentHeaders;
298
+ }
299
+ getRemoteIdHeaders() {
300
+ if (this.remoteIdHeaders == null) {
301
+ this.remoteIdHeaders = [];
302
+ for (const transaction of this.transactions) {
303
+ const remoteIds = transaction.getRemoteIds();
304
+ if (remoteIds && remoteIds.length > this.remoteIdHeaders.length) {
305
+ this.remoteIdHeaders = [];
306
+ remoteIds.forEach(() => this.remoteIdHeaders.push('Remote Id'));
307
+ }
308
+ }
309
+ }
310
+ return this.remoteIdHeaders;
311
+ }
312
+ addPropertiesToLine(line, transaction) {
313
+ const lineLength = line.length;
314
+ for (const key of this.getPropertyKeys()) {
315
+ line.push('');
316
+ }
317
+ for (const key of transaction.getPropertyKeys()) {
318
+ if (!this.shouldAddHiddenProperties && key.endsWith('_')) {
319
+ continue;
320
+ }
321
+ const index = this.getPropertyKeys().indexOf(key) + lineLength;
322
+ line[index] = transaction.getProperty(key);
323
+ }
324
+ }
325
+ addRemoteIdsToLine(line, transaction) {
326
+ const lineLength = line.length;
327
+ for (const key of this.getRemoteIdHeaders()) {
328
+ line.push('');
329
+ }
330
+ const remoteIds = transaction.getRemoteIds();
331
+ if (remoteIds) {
332
+ for (let index = lineLength; index < lineLength + remoteIds.length; index++) {
333
+ line[index] = remoteIds[index - lineLength];
334
+ }
335
+ }
336
+ }
337
+ addUrlsToLine(line, transaction) {
338
+ const lineLength = line.length;
339
+ for (const key of this.getAttachmentHeaders()) {
340
+ line.push('');
341
+ }
342
+ const urls = this.getUrls(transaction);
343
+ for (let index = lineLength; index < lineLength + urls.length; index++) {
344
+ line[index] = urls[index - lineLength];
345
+ }
346
+ }
347
+ getUrls(transaction) {
348
+ let urls = transaction.getUrls();
349
+ if (urls == null) {
350
+ urls = [];
351
+ }
352
+ const files = transaction.getFiles();
353
+ if (files != null) {
354
+ urls = urls.concat(files.map(f => f.getUrl()).filter((url) => url != null));
355
+ }
356
+ return urls;
357
+ }
358
+ }
359
+ //# sourceMappingURL=TransactionsDataTableBuilder.js.map