-
Notifications
You must be signed in to change notification settings - Fork 3
/
report-service.ts
395 lines (353 loc) · 17.3 KB
/
report-service.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* 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 report-service.
*
* @module internal/reports
*/
import { SelectQueryBuilder } from 'typeorm';
import ProductRevision from '../entity/product/product-revision';
import Dinero from 'dinero.js';
import VatGroup from '../entity/vat-group';
import ProductCategory from '../entity/product/product-category';
import { toMySQLString } from '../helpers/timestamps';
import SubTransactionRow from '../entity/transactions/sub-transaction-row';
import SubTransaction from '../entity/transactions/sub-transaction';
import { asDinero, asNumber } from '../helpers/validators';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
import Transaction from '../entity/transactions/transaction';
import ContainerRevision from '../entity/container/container-revision';
import {
ReportCategoryEntryResponse,
ReportContainerEntryResponse,
ReportDataResponse,
ReportEntryResponse,
ReportPosEntryResponse,
ReportProductEntryResponse,
ReportResponse,
ReportVatEntryResponse,
} from '../controller/response/report-response';
import ProductService from './product-service';
import VatGroupService from './vat-group-service';
import ProductCategoryService from './product-category-service';
import PointOfSaleService from './point-of-sale-service';
import ContainerService from './container-service';
import {
BuyerReport, IReport,
Report,
ReportCategoryEntry,
ReportContainerEntry,
ReportEntry,
ReportPosEntry,
ReportProductEntry,
ReportVatEntry,
SalesReport,
} from '../entity/report/report';
import WithManager from '../database/with-manager';
export interface ReportParameters {
fromDate: Date,
tillDate: Date,
forId: number,
}
export default abstract class ReportService extends WithManager {
private static reportEntryToResponse(entry: ReportEntry): ReportEntryResponse {
return {
totalInclVat: entry.totalInclVat.toObject(),
totalExclVat: entry.totalExclVat.toObject(),
};
}
private static productEntryToResponse(entry: ReportProductEntry): ReportProductEntryResponse {
return {
...entry,
...ReportService.reportEntryToResponse(entry),
product: ProductService.revisionToBaseResponse(entry.product),
};
}
private static vatEntryToResponse(entry: ReportVatEntry): ReportVatEntryResponse {
return {
...ReportService.reportEntryToResponse(entry),
vat: VatGroupService.toResponse(entry.vat),
};
}
private static categoryEntryToResponse(entry: ReportCategoryEntry): ReportCategoryEntryResponse {
return {
...ReportService.reportEntryToResponse(entry),
category: ProductCategoryService.asProductCategoryResponse(entry.category),
};
}
private static posEntryToResponse(entry: ReportPosEntry): ReportPosEntryResponse {
return {
...ReportService.reportEntryToResponse(entry),
pos: PointOfSaleService.revisionToBaseResponse(entry.pos),
};
}
private static containerEntryToResponse(entry: ReportContainerEntry): ReportContainerEntryResponse {
return {
...ReportService.reportEntryToResponse(entry),
container: ContainerService.revisionToBaseResponse(entry.container),
};
}
public static reportToResponse(report: Report): ReportResponse {
const data: ReportDataResponse = {};
if (report.data.categories) data.categories = report.data.categories.map((c) => ReportService.categoryEntryToResponse(c));
if (report.data.pos) data.pos = report.data.pos.map((p) => ReportService.posEntryToResponse(p));
if (report.data.containers) data.containers = report.data.containers.map((c) => ReportService.containerEntryToResponse(c));
if (report.data.products) data.products = report.data.products.map((p) => ReportService.productEntryToResponse(p));
if (report.data.vat) data.vat = report.data.vat.map((v) => ReportService.vatEntryToResponse(v));
return {
forId: report.forId,
fromDate: report.fromDate.toISOString(),
tillDate: report.tillDate.toISOString(),
data,
totalExclVat: report.totalExclVat.toObject(),
totalInclVat: report.totalInclVat.toObject(),
};
}
protected abstract addSubTransactionRowFilter<T>(query: SelectQueryBuilder<T>, forId: number, fromDate: Date, tillDate: Date): SelectQueryBuilder<T>;
/**
* Adds the totals to the given query
* Note that the VAT is rounded per product, not per transaction.
* @param query - The query to add the totals to
* @private
*/
private addSelectTotals<T>(query: SelectQueryBuilder<T>): SelectQueryBuilder<T> {
return query
.addSelect('sum(subTransactionRow.amount * ROUND(productRevision.priceInclVat / (1 + (vatGroup.percentage / 100))))', 'total_excl_vat')
.addSelect('sum(subTransactionRow.amount * productRevision.priceInclVat)', 'total_incl_vat');
}
/**
* Gets the product report for the given user
* @param forId - The user ID to get the entries for
* @param fromDate - The from date to get the entries for (inclusive)
* @param tillDate - The till date to get the entries for (exclusive)
* @returns {Promise<ReportProductEntry[]>} - The product report
*/
public async getProductEntries(forId: number, fromDate: Date, tillDate: Date): Promise<ReportProductEntry[]> {
const query = this.manager.createQueryBuilder(ProductRevision, 'productRevision')
.innerJoinAndSelect('productRevision.vat', 'vatGroup')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.productProductId = productRevision.productId AND subTransactionRow.productRevision = productRevision.revision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.id = subTransactionRow.subTransactionId')
.innerJoin(Transaction, 'transaction', 'transaction.id = subTransaction.transactionId')
.addSelect('sum(subTransactionRow.amount)', 'sum_amount');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.groupBy('subTransactionRow.productProductId, subTransactionRow.productRevision')
.getRawAndEntities();
return data.entities.map((productRevision, index) => {
const count = asNumber(data.raw[index].sum_amount);
const totalInclVat = asDinero(data.raw[index].total_incl_vat);
const totalExclVat = asDinero(data.raw[index].total_excl_vat);
return {
count,
product: productRevision,
totalInclVat,
totalExclVat,
};
});
}
/**
* Gets the VAT report for the given user
* @param forId - The user ID to get the entries for
* @param fromDate - The from date to get the entries for (inclusive)
* @param tillDate - The till date to get the entries for (exclusive)
* @returns {Promise<ReportVatEntry[]>} - The VAT report
*/
public async getVatEntries(forId: number, fromDate: Date, tillDate: Date): Promise<ReportVatEntry[]> {
const query = this.manager.createQueryBuilder(VatGroup, 'vatGroup')
.innerJoin(ProductRevision, 'productRevision', 'productRevision.vat = vatGroup.id')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.productProductId = productRevision.productId AND subTransactionRow.productRevision = productRevision.revision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.id = subTransactionRow.subTransactionId')
.innerJoin(Transaction, 'transaction', 'transaction.id = subTransaction.transactionId');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.groupBy('vatGroup.id')
.getRawAndEntities();
return data.entities.map((vatGroup, index) => {
const totalInclVat = asDinero(data.raw[index].total_incl_vat);
const totalExclVat = asDinero(data.raw[index].total_excl_vat);
return {
vat: vatGroup,
totalExclVat,
totalInclVat,
};
});
}
/**
* Gets the category report for the given user
* @param forId - The user ID to get the entries for
* @param fromDate - The from date to get the entries for (inclusive)
* @param tillDate - The till date to get the entries for (exclusive)
* @returns {Promise<ReportCategoryEntry[]>} - The category report
*/
public async getCategoryEntries(forId: number, fromDate: Date, tillDate: Date): Promise<ReportCategoryEntry[]> {
const query = this.manager.createQueryBuilder(ProductCategory, 'productCategory')
.innerJoin(ProductRevision, 'productRevision', 'productRevision.category = productCategory.id')
.innerJoin(VatGroup, 'vatGroup', 'vatGroup.id = productRevision.vat')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.productProductId = productRevision.productId AND subTransactionRow.productRevision = productRevision.revision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.id = subTransactionRow.subTransactionId')
.innerJoin(Transaction, 'transaction', 'transaction.id = subTransaction.transactionId');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.groupBy('productCategory.id')
.getRawAndEntities();
return data.entities.map((productCategory, index) => {
const totalInclVat = asDinero(data.raw[index].total_incl_vat);
const totalExclVat = asDinero(data.raw[index].total_excl_vat);
return {
category: productCategory,
totalExclVat,
totalInclVat,
};
});
}
/**
* Gets the POS report for the given user
* @param forId - The user ID to get the entries for
* @param fromDate - The from date to get the entries for (inclusive)
* @param tillDate - The till date to get the entries for (exclusive)
* @returns {Promise<ReportPosEntry[]>} - The POS report
*/
public async getPosEntries(forId: number, fromDate: Date, tillDate: Date): Promise<ReportPosEntry[]> {
const query = this.manager.createQueryBuilder(PointOfSaleRevision, 'pointOfSaleRevision')
.innerJoin(Transaction, 'transaction', 'transaction.pointOfSalePointOfSaleId = pointOfSaleRevision.pointOfSaleId AND transaction.pointOfSaleRevision = pointOfSaleRevision.revision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.transactionId = transaction.id')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.subTransactionId = subTransaction.id')
.innerJoin(ProductRevision, 'productRevision', 'productRevision.productId = subTransactionRow.productProductId AND productRevision.revision = subTransactionRow.productRevision')
.innerJoin(VatGroup, 'vatGroup', 'vatGroup.id = productRevision.vat');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.groupBy('pointOfSaleRevision.pointOfSaleId')
.getRawAndEntities();
return data.entities.map((pos, index) => {
const totalInclVat = asDinero(data.raw[index].total_incl_vat);
const totalExclVat = asDinero(data.raw[index].total_excl_vat);
return {
pos,
totalExclVat,
totalInclVat,
};
});
}
/**
* Gets the container report for the given user
* @param forId - The user ID to get the entries for
* @param fromDate - The from date to get the entries for (inclusive)
* @param tillDate - The till date to get the entries for (exclusive)
* @returns {Promise<ReportContainerEntry[]>} - The container report
*/
public async getContainerEntries(forId: number, fromDate: Date, tillDate: Date): Promise<ReportContainerEntry[]> {
const query = this.manager.createQueryBuilder(ContainerRevision, 'containerRevision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.containerContainerId = containerRevision.containerId and subTransaction.containerRevision = containerRevision.revision')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.subTransactionId = subTransaction.id')
.innerJoin(Transaction, 'transaction', 'transaction.id = subTransaction.transactionId')
.innerJoin(ProductRevision, 'productRevision', 'productRevision.productId = subTransactionRow.productProductId AND productRevision.revision = subTransactionRow.productRevision')
.innerJoin(VatGroup, 'vatGroup', 'vatGroup.id = productRevision.vat');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.groupBy('containerRevision.containerId')
.getRawAndEntities();
return data.entities.map((container, index) => {
const totalInclVat = asDinero(data.raw[index].total_incl_vat);
const totalExclVat = asDinero(data.raw[index].total_excl_vat);
return {
container,
totalExclVat,
totalInclVat,
};
});
}
/**
* Gets the totals for the given user
* @param forId - The user ID to get the totals for
* @param fromDate - The from date to get the totals for (inclusive)
* @param tillDate - The till date to get the totals for (exclusive)
* @returns {Promise<{ totalExclVat: Dinero.Dinero, totalInclVat: Dinero.Dinero }>} - The totals
*/
public async getTotals(forId: number, fromDate: Date, tillDate: Date): Promise<{ totalExclVat: Dinero.Dinero, totalInclVat: Dinero.Dinero }> {
const query = this.manager.createQueryBuilder(ProductRevision, 'productRevision')
.innerJoin('productRevision.vat', 'vatGroup')
.innerJoin(SubTransactionRow, 'subTransactionRow', 'subTransactionRow.productProductId = productRevision.productId AND subTransactionRow.productRevision = productRevision.revision')
.innerJoin(SubTransaction, 'subTransaction', 'subTransaction.id = subTransactionRow.subTransactionId')
.innerJoin(Transaction, 'transaction', 'transaction.id = subTransaction.transactionId');
this.addSelectTotals(query);
const data = await this.addSubTransactionRowFilter(query, forId, fromDate, tillDate)
.getRawAndEntities();
if (data.raw.length > 1) throw new Error('Multiple results when getting totals');
const totalExclVat = asDinero(data.raw[0].total_excl_vat || 0);
const totalInclVat = asDinero(data.raw[0].total_incl_vat || 0);
return {
totalExclVat,
totalInclVat,
};
}
async fetchReportData(parameters: ReportParameters): Promise<IReport> {
const { fromDate, tillDate, forId } = parameters;
const productEntries = await this.getProductEntries(forId, fromDate, tillDate);
const vatEntries = await this.getVatEntries(forId, fromDate, tillDate);
const categoryEntries = await this.getCategoryEntries(forId, fromDate, tillDate);
const getPosEntries = await this.getPosEntries(forId, fromDate, tillDate);
const getContainerEntries = await this.getContainerEntries(forId, fromDate, tillDate);
const totals = await this.getTotals(forId, fromDate, tillDate);
return {
forId,
fromDate,
tillDate,
totalExclVat: totals.totalExclVat,
totalInclVat: totals.totalInclVat,
data: {
products: productEntries,
vat: vatEntries,
categories: categoryEntries,
pos: getPosEntries,
containers: getContainerEntries,
},
};
}
/**
* Gets the report for the given user
* @param parameters - The parameters to get the report for
* @returns {Promise<Report>} - The report
*/
abstract getReport(parameters: ReportParameters): Promise<Report>;
}
export class SalesReportService extends ReportService {
protected addSubTransactionRowFilter<T>(query: SelectQueryBuilder<T>, forId: number, fromDate: Date, tillDate: Date): SelectQueryBuilder<T> {
return query
.where('subTransaction.toId = :userId', { userId: forId })
.andWhere('subTransaction.createdAt >= :fromDate', { fromDate: toMySQLString(fromDate) })
.andWhere('subTransaction.createdAt < :tillDate', { tillDate: toMySQLString(tillDate) });
}
async getReport(parameters: ReportParameters): Promise<SalesReport> {
const report = await super.fetchReportData(parameters);
return new SalesReport(report);
}
}
export class BuyerReportService extends ReportService {
protected addSubTransactionRowFilter<T>(query: SelectQueryBuilder<T>, forId: number, fromDate: Date, tillDate: Date): SelectQueryBuilder<T> {
return query
.where('transaction.fromId = :userId', { userId: forId })
.andWhere('subTransaction.createdAt >= :fromDate', { fromDate: toMySQLString(fromDate) })
.andWhere('subTransaction.createdAt < :tillDate', { tillDate: toMySQLString(tillDate) });
}
async getReport(parameters: ReportParameters): Promise<BuyerReport> {
const report = await super.fetchReportData(parameters);
return new BuyerReport(report);
}
}