Skip to content

Commit 914eae6

Browse files
committed
wip
1 parent 115a9bc commit 914eae6

File tree

6 files changed

+62
-41
lines changed

6 files changed

+62
-41
lines changed

src/lib/server/db/getTours.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ export const getToursWithRequests = async () => {
8888
eb
8989
.selectFrom('request')
9090
.whereRef('tour.id', '=', 'request.tour')
91-
.select((eb) => [
92-
'request.luggage',
93-
'request.passengers',
94-
'request.ticketChecked'
95-
])
91+
.select((eb) => ['request.luggage', 'request.passengers', 'request.ticketChecked'])
9692
).as('requests')
9793
])
9894
.where('tour.fare', 'is not', null)

src/lib/testHelpers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ export const setRequest = async (
7575
tour: number,
7676
customer: number,
7777
ticketCode: string,
78-
passengers?: number
78+
passengers?: number,
79+
ticketChecked?: boolean
7980
) => {
8081
return await db
8182
.insertInto('request')
@@ -87,7 +88,7 @@ export const setRequest = async (
8788
tour,
8889
customer,
8990
ticketCode,
90-
ticketChecked: false,
91+
ticketChecked: ticketChecked == undefined ? false : ticketChecked,
9192
cancelled: false
9293
})
9394
.returning('id')

src/routes/admin/accounting/+page.svelte

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@
4848
acc.verifiedCustomerCount += current.verifiedCustomerCount;
4949
return acc;
5050
},
51-
{ capped: 0, uncapped: 0, taxameter: 0, availabilityDuration: 0, customerCount: 0, verifiedCustomerCount: 0 }
51+
{
52+
capped: 0,
53+
uncapped: 0,
54+
taxameter: 0,
55+
availabilityDuration: 0,
56+
customerCount: 0,
57+
verifiedCustomerCount: 0
58+
}
5259
);
5360
};
5461
const costsPerCompany = groupBy(

src/routes/admin/accounting/getCompanyCosts.test.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('test accounting', () => {
4444
const v1 = await addTaxi(c1, dummyCapacities);
4545
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
4646
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 1000))!.id;
47-
await setRequest(t1, u.id, '');
47+
await setRequest(t1, u.id, '', 1, true);
4848
const { companyCostsPerDay } = await getCompanyCosts();
4949
expect(companyCostsPerDay).toHaveLength(1);
5050
expect(companyCostsPerDay[0].taxameter).toBe(1000);
@@ -60,8 +60,8 @@ describe('test accounting', () => {
6060
const v1 = await addTaxi(c1, dummyCapacities);
6161
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
6262
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 400))!.id;
63-
await setRequest(t1, u.id, '');
64-
await setRequest(t1, u.id, '');
63+
await setRequest(t1, u.id, '', 1, true);
64+
await setRequest(t1, u.id, '', 1, true);
6565
const { companyCostsPerDay } = await getCompanyCosts();
6666
expect(companyCostsPerDay).toHaveLength(1);
6767
expect(companyCostsPerDay[0].taxameter).toBe(400);
@@ -77,7 +77,7 @@ describe('test accounting', () => {
7777
const v1 = await addTaxi(c1, dummyCapacities);
7878
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
7979
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5000))!.id;
80-
await setRequest(t1, u.id, '');
80+
await setRequest(t1, u.id, '', 1, true);
8181
const { companyCostsPerDay } = await getCompanyCosts();
8282
expect(companyCostsPerDay).toHaveLength(1);
8383
expect(companyCostsPerDay[0].taxameter).toBe(5000);
@@ -94,8 +94,8 @@ describe('test accounting', () => {
9494
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
9595
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2200))!.id;
9696
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
97-
await setRequest(t1, u.id, '');
98-
await setRequest(t2, u.id, '');
97+
await setRequest(t1, u.id, '', 1, true);
98+
await setRequest(t2, u.id, '', 1, true);
9999
const { companyCostsPerDay } = await getCompanyCosts();
100100
expect(companyCostsPerDay).toHaveLength(1);
101101
expect(companyCostsPerDay[0].taxameter).toBe(5600);
@@ -116,8 +116,8 @@ describe('test accounting', () => {
116116
await setAvailability(v1, testDays[0] + HOUR * 14, testDays[0] + HOUR * 14 + 30 * MINUTE);
117117
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 4250))!.id;
118118
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
119-
await setRequest(t1, u.id, '');
120-
await setRequest(t2, u.id, '');
119+
await setRequest(t1, u.id, '', 1, true);
120+
await setRequest(t2, u.id, '', 1, true);
121121
const { companyCostsPerDay } = await getCompanyCosts();
122122
expect(companyCostsPerDay).toHaveLength(1);
123123
expect(companyCostsPerDay[0].taxameter).toBe(7650);
@@ -133,7 +133,7 @@ describe('test accounting', () => {
133133
const v1 = await addTaxi(c1, dummyCapacities);
134134
await setAvailability(v1, midnight + 30 * MINUTE, midnight + 90 * MINUTE);
135135
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2600))!.id;
136-
await setRequest(t1, u.id, '');
136+
await setRequest(t1, u.id, '', 1, true);
137137
const { companyCostsPerDay } = await getCompanyCosts();
138138
expect(companyCostsPerDay).toHaveLength(1);
139139
expect(companyCostsPerDay[0].taxameter).toBe(2600);
@@ -149,8 +149,8 @@ describe('test accounting', () => {
149149
const v1 = await addTaxi(c1, dummyCapacities);
150150
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2600))!.id;
151151
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3000))!.id;
152-
await setRequest(t1, u.id, '');
153-
await setRequest(t2, u.id, '');
152+
await setRequest(t1, u.id, '', 1, true);
153+
await setRequest(t2, u.id, '', 1, true);
154154
const { companyCostsPerDay } = await getCompanyCosts();
155155
expect(companyCostsPerDay).toHaveLength(1);
156156
expect(companyCostsPerDay[0].taxameter).toBe(5600);
@@ -169,8 +169,8 @@ describe('test accounting', () => {
169169
await setAvailability(v2, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
170170
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
171171
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
172-
await setRequest(t1, u.id, '');
173-
await setRequest(t2, u.id, '');
172+
await setRequest(t1, u.id, '', 1, true);
173+
await setRequest(t2, u.id, '', 1, true);
174174
const { companyCostsPerDay } = await getCompanyCosts();
175175
expect(companyCostsPerDay).toHaveLength(1);
176176
expect(companyCostsPerDay[0].taxameter).toBe(10200);
@@ -186,7 +186,7 @@ describe('test accounting', () => {
186186
const v1 = await addTaxi(c1, dummyCapacities);
187187
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
188188
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5700))!.id;
189-
await setRequest(t1, u.id, '', 2);
189+
await setRequest(t1, u.id, '', 2, true);
190190
const { companyCostsPerDay } = await getCompanyCosts();
191191
expect(companyCostsPerDay).toHaveLength(1);
192192
expect(companyCostsPerDay[0].taxameter).toBe(5700);
@@ -206,8 +206,8 @@ describe('test accounting', () => {
206206
await setAvailability(v2, testDays[0] + 5 * HOUR, testDays[0] + 7 * HOUR);
207207
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
208208
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 35300))!.id;
209-
await setRequest(t1, u.id, '');
210-
await setRequest(t2, u.id, '');
209+
await setRequest(t1, u.id, '', 1, true);
210+
await setRequest(t2, u.id, '', 1, true);
211211
const { companyCostsPerDay } = await getCompanyCosts();
212212
expect(companyCostsPerDay).toHaveLength(1);
213213
expect(companyCostsPerDay[0].taxameter).toBe(40400);
@@ -227,8 +227,8 @@ describe('test accounting', () => {
227227
await setAvailability(v2, testDays[0] + 2 * HOUR, testDays[0] + 4 * HOUR);
228228
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
229229
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 9200))!.id;
230-
await setRequest(t1, u.id, '');
231-
await setRequest(t2, u.id, '', 2);
230+
await setRequest(t1, u.id, '', 1, true);
231+
await setRequest(t2, u.id, '', 2 ,true);
232232
const { companyCostsPerDay } = await getCompanyCosts();
233233
expect(companyCostsPerDay).toHaveLength(2);
234234
expect(companyCostsPerDay[0].timestamp).toBe(companyCostsPerDay[1].timestamp);

src/routes/admin/accounting/getCompanyCosts.ts

+31-14
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { DAY, HOUR } from '$lib/util/time';
88
import type { UnixtimeMs } from '$lib/util/UnixtimeMs';
99

1010
export async function getCompanyCosts() {
11-
const tours: (TourWithRequests & { interval: Interval })[] = (
12-
await getToursWithRequests()
13-
).map((t) => {
14-
return {
15-
...t,
16-
interval: new Interval(t.startTime, t.endTime)
17-
};
18-
});
11+
const tours: (TourWithRequests & { interval: Interval })[] = (await getToursWithRequests()).map(
12+
(t) => {
13+
return {
14+
...t,
15+
interval: new Interval(t.startTime, t.endTime)
16+
};
17+
}
18+
);
1919
tours.sort((t1, t2) => t1.startTime - t2.startTime);
2020
if (tours.length === 0) {
2121
return {
@@ -88,12 +88,25 @@ export async function getCompanyCosts() {
8888

8989
// cumulate the total taxameter readings on every relevant day for each vehicle
9090
const taxameterPerDayAndVehicle = new Array<
91-
Map<number, { taxameter: number; customerCount: number; timestamp: UnixtimeMs, verifiedCustomerCount: number }>
91+
Map<
92+
number,
93+
{
94+
taxameter: number;
95+
customerCount: number;
96+
timestamp: UnixtimeMs;
97+
verifiedCustomerCount: number;
98+
}
99+
>
92100
>(days.length);
93101
days.forEach((day, dayIdx) => {
94102
taxameterPerDayAndVehicle[dayIdx] = new Map<
95103
number,
96-
{ taxameter: number; customerCount: number; timestamp: UnixtimeMs, verifiedCustomerCount: number }
104+
{
105+
taxameter: number;
106+
customerCount: number;
107+
timestamp: UnixtimeMs;
108+
verifiedCustomerCount: number;
109+
}
97110
>();
98111
tours.forEach((tour) => {
99112
if (day.overlaps(tour.interval)) {
@@ -105,9 +118,12 @@ export async function getCompanyCosts() {
105118
(taxameterPerDayAndVehicle[dayIdx].get(tour.vehicleId)?.customerCount ?? 0) +
106119
tour.requests.reduce((acc, current) => current.passengers + acc, 0),
107120
timestamp: tour.startTime,
108-
verifiedCustomerCount:
121+
verifiedCustomerCount:
109122
(taxameterPerDayAndVehicle[dayIdx].get(tour.vehicleId)?.verifiedCustomerCount ?? 0) +
110-
tour.requests.reduce((acc, current) => (current.ticketChecked ? current.passengers : 0) + acc, 0)
123+
tour.requests.reduce(
124+
(acc, current) => (current.ticketChecked ? current.passengers : 0) + acc,
125+
0
126+
)
111127
});
112128
}
113129
});
@@ -143,7 +159,8 @@ export async function getCompanyCosts() {
143159
taxameterPerDayAndVehicle[d].forEach((taxameter, vehicle) => {
144160
const costCap = ((availabilitiesPerDayAndVehicle[d]?.get(vehicle) ?? 0) * CAP) / HOUR;
145161
const uncapped = taxameter.taxameter - taxameter.verifiedCustomerCount * FIXED_PRICE;
146-
const capped = Math.min(costCap, uncapped) + Math.max(uncapped - costCap, 0) * OVER_CAP_FACTOR;
162+
const capped =
163+
Math.min(costCap, uncapped) + Math.max(uncapped - costCap, 0) * OVER_CAP_FACTOR;
147164
costPerDayAndVehicle[d].set(vehicle, {
148165
uncapped,
149166
taxameter: taxameter.taxameter,
@@ -204,7 +221,7 @@ export async function getCompanyCosts() {
204221
customerCount:
205222
(companyCostsPerDay[d].get(company.id)?.customerCount ?? 0) +
206223
(costPerDayAndVehicle[d].get(vehicle)?.customerCount ?? 0),
207-
verifiedCustomerCount:
224+
verifiedCustomerCount:
208225
(companyCostsPerDay[d].get(company.id)?.verifiedCustomerCount ?? 0) +
209226
(costPerDayAndVehicle[d].get(vehicle)?.verifiedCustomerCount ?? 0),
210227
taxameter: (companyCostsPerDay[d].get(company.id)?.taxameter ?? 0) + cost.taxameter,

src/routes/admin/accounting/tableData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const getEuroString = (price: number | null) => {
2929
const getCustomerCount = (tour: TourWithRequests, countOnlyVerified: boolean) => {
3030
let customers = 0;
3131
tour.requests.forEach((r) => {
32-
if(!countOnlyVerified || r.ticketChecked) {
32+
if (!countOnlyVerified || r.ticketChecked) {
3333
customers += r.passengers;
3434
}
3535
});

0 commit comments

Comments
 (0)