Skip to content

Commit 75cbd7d

Browse files
committed
wip
1 parent 11c7922 commit 75cbd7d

File tree

3 files changed

+142
-47
lines changed

3 files changed

+142
-47
lines changed

src/lib/testHelpers.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ export const setTour = async (
7171
.executeTakeFirst();
7272
};
7373

74-
export const setRequest = async (tour: number, customer: number, ticketCode: string) => {
74+
export const setRequest = async (
75+
tour: number,
76+
customer: number,
77+
ticketCode: string,
78+
passengers?: number
79+
) => {
7580
return await db
7681
.insertInto('request')
7782
.values({
78-
passengers: 1,
83+
passengers: passengers ?? 1,
7984
bikes: 0,
8085
luggage: 0,
8186
wheelchairs: 0,

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

+110-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
addTestUser,
66
clearDatabase,
77
setAvailability,
8-
setEvent,
98
setRequest,
109
setTour
1110
} from '$lib/testHelpers';
@@ -49,8 +48,7 @@ describe('test accounting', () => {
4948
const v1 = await addTaxi(c1, dummyCapacities);
5049
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
5150
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 1000))!.id;
52-
const r1 = await setRequest(t1, u.id, '');
53-
await setEvent(r1.id, 1, true, 1, 1);
51+
await setRequest(t1, u.id, '');
5452
const { companyCostsPerDay } = await getCompanyCosts();
5553
expect(companyCostsPerDay).toHaveLength(1);
5654
expect(companyCostsPerDay[0].taxameter).toBe(1000);
@@ -66,10 +64,8 @@ describe('test accounting', () => {
6664
const v1 = await addTaxi(c1, dummyCapacities);
6765
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
6866
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 1000))!.id;
69-
const r1 = await setRequest(t1, u.id, '');
70-
const r2 = await setRequest(t1, u.id, '');
71-
await setEvent(r1.id, 1, true, 1, 1);
72-
await setEvent(r2.id, 1, true, 1, 1);
67+
await setRequest(t1, u.id, '');
68+
await setRequest(t1, u.id, '');
7369
const { companyCostsPerDay } = await getCompanyCosts();
7470
expect(companyCostsPerDay).toHaveLength(1);
7571
expect(companyCostsPerDay[0].taxameter).toBe(1000);
@@ -85,8 +81,7 @@ describe('test accounting', () => {
8581
const v1 = await addTaxi(c1, dummyCapacities);
8682
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
8783
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5000))!.id;
88-
const r1 = await setRequest(t1, u.id, '');
89-
await setEvent(r1.id, 1, true, 1, 1);
84+
await setRequest(t1, u.id, '');
9085
const { companyCostsPerDay } = await getCompanyCosts();
9186
expect(companyCostsPerDay).toHaveLength(1);
9287
expect(companyCostsPerDay[0].taxameter).toBe(5000);
@@ -103,10 +98,8 @@ describe('test accounting', () => {
10398
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
10499
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2200))!.id;
105100
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
106-
const r1 = await setRequest(t1, u.id, '');
107-
await setEvent(r1.id, 1, true, 1, 1);
108-
const r2 = await setRequest(t2, u.id, '');
109-
await setEvent(r2.id, 1, true, 1, 1);
101+
await setRequest(t1, u.id, '');
102+
await setRequest(t2, u.id, '');
110103
const { companyCostsPerDay } = await getCompanyCosts();
111104
expect(companyCostsPerDay).toHaveLength(1);
112105
expect(companyCostsPerDay[0].taxameter).toBe(5600);
@@ -127,10 +120,8 @@ describe('test accounting', () => {
127120
await setAvailability(v1, testDays[0] + HOUR * 14, testDays[0] + HOUR * 14 + 10 * MINUTE);
128121
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2200))!.id;
129122
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
130-
const r1 = await setRequest(t1, u.id, '');
131-
await setEvent(r1.id, 1, true, 1, 1);
132-
const r2 = await setRequest(t2, u.id, '');
133-
await setEvent(r2.id, 1, true, 1, 1);
123+
await setRequest(t1, u.id, '');
124+
await setRequest(t2, u.id, '');
134125
const { companyCostsPerDay } = await getCompanyCosts();
135126
expect(companyCostsPerDay).toHaveLength(1);
136127
expect(companyCostsPerDay[0].taxameter).toBe(5600);
@@ -140,37 +131,123 @@ describe('test accounting', () => {
140131
expect(companyCostsPerDay[0].availabilityDuration).toBe(70 * MINUTE);
141132
});
142133

143-
it('availabilitiy crosses midnight', async () => {
134+
it('availabilitiy on insignificant day', async () => {
144135
const u = await addTestUser();
145136
const c1 = await addCompany(1, dummyCoordinates);
146137
const v1 = await addTaxi(c1, dummyCapacities);
147-
await setAvailability(v1, testDays[0] - 30 * DAY - 30 * MINUTE, testDays[0] + 30 * MINUTE);
148-
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2630))!.id;
149-
const r1 = await setRequest(t1, u.id, '');
150-
await setEvent(r1.id, 1, true, 1, 1);
138+
await setAvailability(v1, midnight + 30 * MINUTE, midnight + 90 * MINUTE);
139+
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2600))!.id;
140+
await setRequest(t1, u.id, '');
151141
const { companyCostsPerDay } = await getCompanyCosts();
152142
expect(companyCostsPerDay).toHaveLength(1);
153-
expect(companyCostsPerDay[0].taxameter).toBe(2630);
154-
expect(companyCostsPerDay[0].uncapped).toBe(2030);
155-
expect(companyCostsPerDay[0].capped).toBe(1820);
143+
expect(companyCostsPerDay[0].taxameter).toBe(2600);
144+
expect(companyCostsPerDay[0].uncapped).toBe(2000);
145+
expect(companyCostsPerDay[0].capped).toBe(500);
146+
expect(companyCostsPerDay[0].availabilityDuration).toBe(0);
156147
expect(companyCostsPerDay[0].customerCount).toBe(1);
157-
expect(companyCostsPerDay[0].availabilityDuration).toBe(30 * MINUTE);
158148
});
159149

160-
it('availabilitiy on insignificant day', async () => {
150+
it('two tours same vehicle', async () => {
161151
const u = await addTestUser();
162152
const c1 = await addCompany(1, dummyCoordinates);
163153
const v1 = await addTaxi(c1, dummyCapacities);
164-
await setAvailability(v1, midnight + 30 * MINUTE, midnight + 90 * MINUTE);
165154
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2600))!.id;
166-
const r1 = await setRequest(t1, u.id, '');
167-
await setEvent(r1.id, 1, true, 1, 1);
155+
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3000))!.id;
156+
await setRequest(t1, u.id, '');
157+
await setRequest(t2, u.id, '');
168158
const { companyCostsPerDay } = await getCompanyCosts();
169159
expect(companyCostsPerDay).toHaveLength(1);
170-
expect(companyCostsPerDay[0].taxameter).toBe(2600);
171-
expect(companyCostsPerDay[0].uncapped).toBe(2000);
172-
expect(companyCostsPerDay[0].capped).toBe(500);
160+
expect(companyCostsPerDay[0].taxameter).toBe(5600);
161+
expect(companyCostsPerDay[0].uncapped).toBe(4400);
162+
expect(companyCostsPerDay[0].capped).toBe(1100);
173163
expect(companyCostsPerDay[0].availabilityDuration).toBe(0);
164+
expect(companyCostsPerDay[0].customerCount).toBe(2);
165+
});
166+
167+
it('two tours different vehicles', async () => {
168+
const u = await addTestUser();
169+
const c1 = await addCompany(1, dummyCoordinates);
170+
const v1 = await addTaxi(c1, dummyCapacities);
171+
const v2 = await addTaxi(c1, dummyCapacities);
172+
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
173+
await setAvailability(v2, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
174+
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
175+
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
176+
await setRequest(t1, u.id, '');
177+
await setRequest(t2, u.id, '');
178+
const { companyCostsPerDay } = await getCompanyCosts();
179+
expect(companyCostsPerDay).toHaveLength(1);
180+
expect(companyCostsPerDay[0].taxameter).toBe(10200);
181+
expect(companyCostsPerDay[0].uncapped).toBe(9000);
182+
expect(companyCostsPerDay[0].capped).toBe(7500);
183+
expect(companyCostsPerDay[0].availabilityDuration).toBe(2 * HOUR);
184+
expect(companyCostsPerDay[0].customerCount).toBe(2);
185+
});
186+
187+
it('tours with 2 customers', async () => {
188+
const u = await addTestUser();
189+
const c1 = await addCompany(1, dummyCoordinates);
190+
const v1 = await addTaxi(c1, dummyCapacities);
191+
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
192+
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5700))!.id;
193+
await setRequest(t1, u.id, '', 2);
194+
const { companyCostsPerDay } = await getCompanyCosts();
195+
expect(companyCostsPerDay).toHaveLength(1);
196+
expect(companyCostsPerDay[0].taxameter).toBe(5700);
197+
expect(companyCostsPerDay[0].uncapped).toBe(4500);
198+
expect(companyCostsPerDay[0].capped).toBe(3750);
199+
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
200+
expect(companyCostsPerDay[0].customerCount).toBe(2);
201+
});
202+
203+
it('overlapping availabilities', async () => {
204+
const u = await addTestUser();
205+
const c1 = await addCompany(1, dummyCoordinates);
206+
const v1 = await addTaxi(c1, dummyCapacities);
207+
const v2 = await addTaxi(c1, dummyCapacities);
208+
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 6 * HOUR);
209+
await setAvailability(v2, testDays[0] + 2 * HOUR, testDays[0] + 4 * HOUR);
210+
await setAvailability(v2, testDays[0] + 5 * HOUR, testDays[0] + 7 * HOUR);
211+
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
212+
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 35600))!.id;
213+
await setRequest(t1, u.id, '');
214+
await setRequest(t2, u.id, '');
215+
const { companyCostsPerDay } = await getCompanyCosts();
216+
expect(companyCostsPerDay).toHaveLength(1);
217+
expect(companyCostsPerDay[0].taxameter).toBe(40700);
218+
expect(companyCostsPerDay[0].uncapped).toBe(39500);
219+
expect(companyCostsPerDay[0].capped).toBe(23750);
220+
expect(companyCostsPerDay[0].availabilityDuration).toBe(7 * HOUR);
221+
expect(companyCostsPerDay[0].customerCount).toBe(2);
222+
});
223+
224+
it('2 companies', async () => {
225+
const u = await addTestUser();
226+
const c1 = await addCompany(1, dummyCoordinates);
227+
const c2 = await addCompany(1, dummyCoordinates);
228+
const v1 = await addTaxi(c1, dummyCapacities);
229+
const v2 = await addTaxi(c2, dummyCapacities);
230+
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
231+
await setAvailability(v2, testDays[0] + 2 * HOUR, testDays[0] + 4 * HOUR);
232+
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
233+
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 9200))!.id;
234+
await setRequest(t1, u.id, '');
235+
await setRequest(t2, u.id, '', 2);
236+
const { companyCostsPerDay } = await getCompanyCosts();
237+
expect(companyCostsPerDay).toHaveLength(2);
238+
expect(companyCostsPerDay[0].timestamp).toBe(companyCostsPerDay[1].timestamp);
239+
companyCostsPerDay.sort((c1, c2) => c1.companyId - c2.companyId);
240+
241+
expect(companyCostsPerDay[0].taxameter).toBe(5100);
242+
expect(companyCostsPerDay[0].uncapped).toBe(4500);
243+
expect(companyCostsPerDay[0].capped).toBe(3750);
244+
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
174245
expect(companyCostsPerDay[0].customerCount).toBe(1);
246+
247+
expect(companyCostsPerDay[1].taxameter).toBe(9200);
248+
expect(companyCostsPerDay[1].uncapped).toBe(8000);
249+
expect(companyCostsPerDay[1].capped).toBe(7250);
250+
expect(companyCostsPerDay[1].availabilityDuration).toBe(2 * HOUR);
251+
expect(companyCostsPerDay[1].customerCount).toBe(2);
175252
});
176253
});

src/routes/admin/accounting/getCompanyCosts.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,42 @@ export async function getCompanyCosts() {
5959
const availabilitiesPerDayAndVehicle = new Array<Map<number, number>>(days.length);
6060
days.forEach((day, dayIdx) => {
6161
availabilitiesPerDayAndVehicle[dayIdx] = new Map<number, number>();
62-
availabilitiesPerVehicle.forEach((availabilities, vehicle) => availabilities.forEach((availability) => {
63-
if(day.overlaps(availability)){
64-
availabilitiesPerDayAndVehicle[dayIdx].set(vehicle, (availabilitiesPerDayAndVehicle[dayIdx].get(vehicle) ?? 0) + availability.getDurationMs());
65-
}
66-
}));
62+
availabilitiesPerVehicle.forEach((availabilities, vehicle) =>
63+
availabilities.forEach((availability) => {
64+
if (day.overlaps(availability)) {
65+
availabilitiesPerDayAndVehicle[dayIdx].set(
66+
vehicle,
67+
(availabilitiesPerDayAndVehicle[dayIdx].get(vehicle) ?? 0) +
68+
availability.getDurationMs()
69+
);
70+
}
71+
})
72+
);
6773
});
6874

6975
// cumulate the total taxameter readings on every relevant day for each vehicle
70-
const taxameterPerDayAndVehicle = new Array<Map<number, { taxameter: number; customerCount: number; timestamp: UnixtimeMs }>>(days.length);
76+
const taxameterPerDayAndVehicle = new Array<
77+
Map<number, { taxameter: number; customerCount: number; timestamp: UnixtimeMs }>
78+
>(days.length);
7179
days.forEach((day, dayIdx) => {
72-
taxameterPerDayAndVehicle[dayIdx] = new Map<number, { taxameter: number; customerCount: number; timestamp: UnixtimeMs }>
80+
taxameterPerDayAndVehicle[dayIdx] = new Map<
81+
number,
82+
{ taxameter: number; customerCount: number; timestamp: UnixtimeMs }
83+
>();
7384
tours.forEach((tour) => {
74-
if(day.overlaps(tour.interval)) {
85+
if (day.overlaps(tour.interval)) {
7586
taxameterPerDayAndVehicle[dayIdx].set(tour.vehicleId, {
76-
taxameter: (taxameterPerDayAndVehicle[dayIdx].get(tour.vehicleId)?.taxameter ?? 0) + (tour.fare ?? 0),
87+
taxameter:
88+
(taxameterPerDayAndVehicle[dayIdx].get(tour.vehicleId)?.taxameter ?? 0) +
89+
(tour.fare ?? 0),
7790
customerCount:
7891
(taxameterPerDayAndVehicle[dayIdx].get(tour.vehicleId)?.customerCount ?? 0) +
7992
tour.requests.reduce((acc, current) => current.passengers + acc, 0),
8093
timestamp: tour.startTime
81-
})
94+
});
8295
}
83-
})
84-
})
96+
});
97+
});
8598
const costPerDayAndVehicle = new Array<
8699
Map<
87100
VehicleId,

0 commit comments

Comments
 (0)