Skip to content

Commit ca21c4e

Browse files
committed
wip
1 parent 45d68cb commit ca21c4e

File tree

6 files changed

+75
-33
lines changed

6 files changed

+75
-33
lines changed

src/lib/server/db/getTours.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ export const getToursWithRequests = async (
7676
companyId?: number,
7777
timeRange?: [UnixtimeMs, UnixtimeMs]
7878
) => {
79+
console.log({companyId})
7980
return await db
8081
.selectFrom('tour')
8182
.innerJoin('vehicle', 'vehicle.id', 'tour.vehicle')
8283
.innerJoin('company', 'company.id', 'vehicle.company')
84+
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
8385
.$if(companyId != undefined, (qb) => qb.where('company.id', '=', companyId!))
8486
.$if(!!timeRange, (qb) =>
8587
qb.where('tour.departure', '<', timeRange![1]).where('tour.arrival', '>', timeRange![0])
@@ -101,6 +103,7 @@ export const getToursWithRequests = async (
101103
eb
102104
.selectFrom('request')
103105
.whereRef('tour.id', '=', 'request.tour')
106+
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
104107
.select([
105108
'request.luggage',
106109
'request.passengers',
@@ -111,6 +114,7 @@ export const getToursWithRequests = async (
111114
.innerJoin('request', 'request.id', 'event.request')
112115
.whereRef('tour.id', '=', 'request.tour')
113116
.innerJoin('user', 'user.id', 'request.customer')
117+
.$if(!selectCancelled, (qb) => qb.where('tour.cancelled', '=', false))
114118
.select([
115119
'tour.id as tour',
116120
'user.name as customerName',
@@ -140,7 +144,6 @@ export const getToursWithRequests = async (
140144
])
141145
).as('requests')
142146
])
143-
.where('tour.fare', 'is not', null)
144147
.execute();
145148
};
146149

src/lib/ui/AccountingView.svelte

+41-16
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
import { CalendarDate } from '@internationalized/date';
1313
import { groupBy } from '$lib/util/groupBy';
1414
import {
15-
companyCols,
15+
companyColsAdmin,
16+
companyColsCompany,
1617
getEuroString,
17-
subtractionCols,
18-
tourCols,
18+
subtractionColsAdmin,
19+
subtractionColsCompany,
20+
tourColsAdmin,
21+
tourColsCompany,
1922
type Column,
2023
type CompanyRow,
2124
type Subtractions
@@ -44,9 +47,6 @@
4447
earliestTime: UnixtimeMs;
4548
} = $props();
4649
47-
let currentRowsToursTable: TourWithRequests[] = $state(tours);
48-
let currentRowsSubtractionsTable: Subtractions[] = $state(companyCostsPerDay);
49-
5050
const updateCompanySums = (subtractionRows: Subtractions[]) => {
5151
const accumulatedCompanyRowEntries = (arr: (Subtractions | CompanyRow)[]) => {
5252
return arr.reduce(
@@ -69,6 +69,7 @@
6969
}
7070
);
7171
};
72+
7273
const costsPerCompany = groupBy(
7374
subtractionRows,
7475
(c) => c.companyId,
@@ -80,10 +81,14 @@
8081
return;
8182
}
8283
const accumulated = accumulatedCompanyRowEntries(arr);
83-
newCompanyRows.push({ ...accumulated, companyName: arr[0].companyName, companyId });
84+
if (isAdmin) {
85+
newCompanyRows.push({ ...accumulated, companyName: arr[0].companyName, companyId });
86+
} else {
87+
newCompanyRows.push({ ...accumulated, companyId });
88+
}
8489
});
8590
if (newCompanyRows.length === 0) {
86-
return;
91+
return [];
8792
}
8893
if (isAdmin) {
8994
newCompanyRows.push({
@@ -94,7 +99,10 @@
9499
}
95100
return newCompanyRows;
96101
};
97-
let currentCompanyRows: CompanyRow[] = $state(updateCompanySums(companyCostsPerDay)!);
102+
103+
let currentRowsToursTable: TourWithRequests[] = $state(tours);
104+
let currentRowsSubtractionsTable: Subtractions[] = $state(companyCostsPerDay);
105+
let currentCompanyRows: CompanyRow[] = $state(updateCompanySums(companyCostsPerDay));
98106
99107
const getNewSum = (rows: Subtractions[]) => {
100108
let newSum = 0;
@@ -135,7 +143,7 @@
135143
$effect(() => {
136144
currentRowsToursTable = getNewRows(tourFilters, tours);
137145
const subtractionRows = getNewRows(subtractionFilters, companyCostsPerDay);
138-
currentCompanyRows = updateCompanySums(subtractionRows)!;
146+
currentCompanyRows = updateCompanySums(subtractionRows);
139147
currentRowsSubtractionsTable = subtractionRows;
140148
});
141149
@@ -203,7 +211,7 @@
203211
204212
const csvExport = <T,>(rows: T[], cols: Column<T>[], filename: string, addSumRow: boolean) => {
205213
let data = [];
206-
data.push(cols.map((col) => col.text));
214+
data.push(cols.map((col) => col.text.trim()));
207215
for (let row of rows) {
208216
data.push(cols.map((col) => col.toTableEntry(row)));
209217
}
@@ -220,8 +228,13 @@
220228
saveAs(blob, filename);
221229
};
222230
223-
csvExport(tourRows, tourCols, filename + '_tour.csv', false);
224-
csvExport(subtractionRows, subtractionCols, filename + '_abzuege.csv', true);
231+
csvExport(tourRows, isAdmin ? tourColsAdmin : tourColsCompany, filename + '_tour.csv', false);
232+
csvExport(
233+
subtractionRows,
234+
isAdmin ? subtractionColsAdmin : subtractionColsCompany,
235+
filename + '_abzuege.csv',
236+
true
237+
);
225238
};
226239
227240
let tables = [
@@ -241,15 +254,27 @@
241254
</script>
242255

243256
{#snippet tourTable()}
244-
<SortableTable rows={currentRowsToursTable} cols={tourCols} {isAdmin} />
257+
<SortableTable
258+
rows={currentRowsToursTable}
259+
cols={isAdmin ? tourColsAdmin : tourColsCompany}
260+
{isAdmin}
261+
/>
245262
{/snippet}
246263

247264
{#snippet subtractionTable()}
248-
<SortableTable rows={currentRowsSubtractionsTable} cols={subtractionCols} {isAdmin} />
265+
<SortableTable
266+
rows={currentRowsSubtractionsTable}
267+
cols={isAdmin ? subtractionColsAdmin : subtractionColsCompany}
268+
{isAdmin}
269+
/>
249270
{/snippet}
250271

251272
{#snippet companyTable()}
252-
<SortableTable rows={currentCompanyRows} cols={companyCols} {isAdmin} />
273+
<SortableTable
274+
rows={currentCompanyRows}
275+
cols={isAdmin ? companyColsAdmin : companyColsCompany}
276+
{isAdmin}
277+
/>
253278
{/snippet}
254279

255280
{#snippet filterOptions()}

src/lib/ui/SortableTable.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<Table.Body>
7171
{#each rows as row}
7272
<Table.Row
73-
class={`cursor-pointer ${isTourWithRequests(row) && (row as TourWithRequests).cancelled ? 'bg-destructive' : 'bg-white-0'}`}
73+
class={`${isTourWithRequests(row) && (row as TourWithRequests).cancelled ? 'bg-destructive cursor-pointer' : 'bg-white-0'}`}
7474
onclick={() => {
7575
if (rows.length != 0 && isTourWithRequests(rows[0])) {
7676
selectedTour = {

src/lib/ui/tableData.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { UnixtimeMs } from '$lib/util/UnixtimeMs';
66
export type CompanyRow = {
77
taxameter: number;
88
companyId: number;
9-
companyName: string | null;
9+
companyName?: string | null;
1010
capped: number;
1111
uncapped: number;
1212
availabilityDuration: number;
@@ -59,12 +59,19 @@ const displayDuration = (duration: number) => {
5959
return addLeadingZero(hours) + ':' + addLeadingZero(minutes);
6060
};
6161

62-
export const tourCols: Column<TourWithRequests>[] = [
63-
{
64-
text: 'Unternehmen',
65-
sort: undefined,
66-
toTableEntry: (r: TourWithRequests) => r.companyName ?? ''
67-
},
62+
const firstTourColAdmin: Column<TourWithRequests> = {
63+
text: 'Unternehmen',
64+
sort: undefined,
65+
toTableEntry: (r: TourWithRequests) => r.companyName ?? ''
66+
};
67+
68+
const firstTourColCompany: Column<TourWithRequests> = {
69+
text: 'Fahrzeug',
70+
sort: undefined,
71+
toTableEntry: (r: TourWithRequests) => r.licensePlate ?? ''
72+
};
73+
74+
const restTourCols: Column<TourWithRequests>[] = [
6875
{
6976
text: 'Abfahrt ',
7077
sort: (t1: TourWithRequests, t2: TourWithRequests) => t1.startTime - t2.startTime,
@@ -76,7 +83,7 @@ export const tourCols: Column<TourWithRequests>[] = [
7683
toTableEntry: (r: TourWithRequests) => displayUnixtimeMs(r.endTime, true)
7784
},
7885
{
79-
text: 'Anzahl Kunden',
86+
text: 'Kunden',
8087
sort: undefined,
8188
toTableEntry: (r: TourWithRequests) => getCustomerCount(r, false)
8289
},
@@ -97,7 +104,10 @@ export const tourCols: Column<TourWithRequests>[] = [
97104
}
98105
];
99106

100-
export const subtractionCols: Column<Subtractions>[] = [
107+
export const tourColsAdmin = [firstTourColAdmin].concat(restTourCols);
108+
export const tourColsCompany = [firstTourColCompany].concat(restTourCols);
109+
110+
export const subtractionColsAdmin: Column<Subtractions>[] = [
101111
{
102112
text: 'Unternehmen',
103113
sort: undefined,
@@ -108,7 +118,7 @@ export const subtractionCols: Column<Subtractions>[] = [
108118
sort: (a: Subtractions, b: Subtractions) => a.timestamp - b.timestamp,
109119
toTableEntry: (r: Subtractions) => displayUnixtimeMs(r.timestamp)
110120
},
111-
{ text: 'Buchungen', sort: undefined, toTableEntry: (r: Subtractions) => r.customerCount },
121+
{ text: 'Kunden', sort: undefined, toTableEntry: (r: Subtractions) => r.customerCount },
112122
{
113123
text: 'erschienene Kunden ',
114124
sort: undefined,
@@ -141,13 +151,15 @@ export const subtractionCols: Column<Subtractions>[] = [
141151
}
142152
];
143153

144-
export const companyCols: Column<CompanyRow>[] = [
154+
export const subtractionColsCompany = subtractionColsAdmin.slice(1);
155+
156+
export const companyColsAdmin: Column<CompanyRow>[] = [
145157
{
146158
text: 'Unternehmen',
147159
sort: undefined,
148160
toTableEntry: (r: CompanyRow) => r.companyName ?? ''
149161
},
150-
{ text: 'Buchungen', sort: undefined, toTableEntry: (r: CompanyRow) => r.customerCount },
162+
{ text: 'Kunden', sort: undefined, toTableEntry: (r: CompanyRow) => r.customerCount },
151163
{
152164
text: 'erschienene Kunden ',
153165
sort: undefined,
@@ -179,3 +191,5 @@ export const companyCols: Column<CompanyRow>[] = [
179191
toTableEntry: (r: CompanyRow) => displayDuration(r.availabilityDuration)
180192
}
181193
];
194+
195+
export const companyColsCompany = companyColsAdmin.slice(1);

src/routes/taxi/availability/+page.server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { fail } from '@sveltejs/kit';
66
import { msg } from '$lib/msg';
77
import { readInt } from '$lib/server/util/readForm';
88

9-
export async function load(event) {
10-
const companyId = event.locals.session?.companyId;
9+
export async function load(event: RequestEvent) {
10+
const companyId = event.locals.session?.companyId!;
1111
if (!companyId) {
1212
throw 'company not defined';
1313
}

src/routes/taxi/availability/+page.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
copy.setHours(todayDay.getHours() + 8);
123123
return copy;
124124
});
125-
125+
console.log({tours:data.tours})
126126
const overlaps = (a: Range, b: Range) => a.startTime < b.endTime && a.endTime > b.startTime;
127127
128128
const hasTour = (vehicleId: number, cell: Range) =>

0 commit comments

Comments
 (0)