Skip to content

Commit

Permalink
Accounting für Taxiunternehmer (#237)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

	closes#235,
	closes#236

* wip

* wip

* wip

* SortableTable.svelte: button style, sortableTable tag removed

---------

Co-authored-by: Felix Gündling <felix.guendling@gmail.com>
  • Loading branch information
nilspenzel and felixguendling authored Mar 10, 2025
1 parent 04d96ae commit 954f238
Show file tree
Hide file tree
Showing 20 changed files with 615 additions and 522 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ import { groupBy } from '$lib/util/groupBy';
import { DAY, HOUR } from '$lib/util/time';
import type { UnixtimeMs } from '$lib/util/UnixtimeMs';

export async function getCompanyCosts() {
const tours: (TourWithRequests & { interval: Interval })[] = (await getToursWithRequests()).map(
(t) => {
return {
...t,
interval: new Interval(t.startTime, t.endTime)
};
}
);
export async function getCompanyCosts(companyId?: number) {
const tours: (TourWithRequests & { interval: Interval })[] = (
await getToursWithRequests(true, companyId)
).map((t) => {
return {
...t,
interval: new Interval(t.startTime, t.endTime)
};
});
tours.sort((t1, t2) => t1.startTime - t2.startTime);
if (tours.length === 0) {
return {
tours: [],
earliestTime: Date.now(),
latestTime: Date.now(),
companyCostsPerDay: []
};
}
const earliestTime =
tours.reduce((min, entry) => (entry.startTime < min.startTime ? entry : min), tours[0])
.startTime - DAY;

const today = Math.ceil(Date.now() / DAY) * DAY;
const latestTime =
tours.reduce((max, entry) => (entry.endTime > max.endTime ? entry : max), tours[0]).endTime +
DAY;
const availabilities = await db
.selectFrom('availability')
.innerJoin('vehicle', 'vehicle.id', 'availability.vehicle')
.$if(companyId != undefined, (qb) => qb.where('vehicle.company', '=', companyId!))
.where('availability.endTime', '>=', earliestTime)
.where('availability.startTime', '<=', today)
.where('availability.startTime', '<=', latestTime)
.select(['availability.vehicle as vehicleId', 'availability.startTime', 'availability.endTime'])
.orderBy('availability.endTime', 'asc')
.execute();
Expand All @@ -51,7 +55,7 @@ export async function getCompanyCosts() {
};
const firstDay = new Date(earliestTime - DAY);
const firstDayStart = Math.floor(firstDay.getTime() / DAY) * DAY;
const days = Array.from({ length: Math.ceil((today - firstDayStart) / DAY) }, (_, i) => {
const days = Array.from({ length: Math.ceil((latestTime - firstDayStart) / DAY) }, (_, i) => {
const offset = getOffset(firstDayStart + DAY * i + HOUR * 12);
const offsetNextDay = getOffset(firstDayStart + DAY * (i + 1) + HOUR * 12);
return new Interval(
Expand Down Expand Up @@ -232,6 +236,7 @@ export async function getCompanyCosts() {
return {
tours,
earliestTime,
latestTime,
companyCostsPerDay: companyCostsPerDay.flatMap((companyCosts) =>
Array.from(companyCosts).map(
([
Expand Down
58 changes: 55 additions & 3 deletions src/lib/server/db/getTours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,79 @@ export type Tours = Awaited<ReturnType<typeof getTours>>;
export type Tour = Tours[0];
export type TourEvent = Tour['events'][0];

export const getToursWithRequests = async () => {
export const getToursWithRequests = async (
selectCancelled: boolean,
companyId?: number,
timeRange?: [UnixtimeMs, UnixtimeMs]
) => {
console.log({ companyId });
return await db
.selectFrom('tour')
.innerJoin('vehicle', 'vehicle.id', 'tour.vehicle')
.innerJoin('company', 'company.id', 'vehicle.company')
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
.$if(companyId != undefined, (qb) => qb.where('company.id', '=', companyId!))
.$if(!!timeRange, (qb) =>
qb.where('tour.departure', '<', timeRange![1]).where('tour.arrival', '>', timeRange![0])
)
.select((eb) => [
'tour.fare as fare',
'tour.departure as startTime',
'tour.arrival as endTime',
'tour.cancelled',
'tour.id as tourId',
'tour.message',
'company.name as companyName',
'company.id as companyId',
'company.lat as companyLat',
'company.lng as companyLng',
'vehicle.id as vehicleId',
'vehicle.licensePlate',
jsonArrayFrom(
eb
.selectFrom('request')
.whereRef('tour.id', '=', 'request.tour')
.select(['request.luggage', 'request.passengers', 'request.ticketChecked'])
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
.select([
'request.luggage',
'request.passengers',
'request.ticketChecked',
jsonArrayFrom(
eb
.selectFrom('event')
.innerJoin('request', 'request.id', 'event.request')
.whereRef('tour.id', '=', 'request.tour')
.innerJoin('user', 'user.id', 'request.customer')
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
.select([
'tour.id as tour',
'user.name as customerName',
'user.phone as customerPhone',
'event.id',
'event.communicatedTime',
'event.address',
'event.eventGroup',
'event.isPickup',
'event.lat',
'event.lng',
'event.nextLegDuration',
'event.prevLegDuration',
'event.scheduledTimeStart',
'event.scheduledTimeEnd',
'event.cancelled',
'request.bikes',
'request.customer',
'request.luggage',
'request.passengers',
'request.wheelchairs',
'request.id as requestId'
])
.select(sql<string>`md5(request.ticket_code)`.as('ticketHash'))
.orderBy('event.scheduledTimeStart')
).as('events')
])
).as('requests')
])
.where('tour.fare', 'is not', null)
.execute();
};

Expand Down
Loading

0 comments on commit 954f238

Please sign in to comment.