|
|
|
@@ -1,4 +1,4 @@ |
|
|
|
import {Component, Input} from '@angular/core'; |
|
|
|
import {Component} from '@angular/core'; |
|
|
|
import {AbstractDataFormComponent} from "@app/_components/_abstract/abstract-data-form-component"; |
|
|
|
import { |
|
|
|
LocationService, TripJsonld, |
|
|
|
@@ -21,11 +21,13 @@ import {SearchSelectComponent} from "@app/_components/search-select/search-selec |
|
|
|
}) |
|
|
|
export class TripLocationFormComponent extends AbstractDataFormComponent<TripLocationJsonld> { |
|
|
|
|
|
|
|
protected trip!: TripJsonld; |
|
|
|
protected arrivalDateTime?: string; |
|
|
|
protected departureDateTime?: string; |
|
|
|
protected readonly tripLocationForm = tripLocationForm; |
|
|
|
protected readonly SearchSelectComponent = SearchSelectComponent; |
|
|
|
protected locationColDefinitions: ListColDefinition[]; |
|
|
|
protected trip!: TripJsonld; |
|
|
|
protected newArrivalDateTime?: string; |
|
|
|
protected newDepartureDateTime?: string; |
|
|
|
protected tripLocationEntries: TripLocationJsonld[]; |
|
|
|
|
|
|
|
constructor( |
|
|
|
private tripLocationService: TripLocationService, |
|
|
|
@@ -49,7 +51,7 @@ export class TripLocationFormComponent extends AbstractDataFormComponent<TripLoc |
|
|
|
translateService, |
|
|
|
router |
|
|
|
); |
|
|
|
|
|
|
|
this.tripLocationEntries = []; |
|
|
|
this.redirectAfterDelete = '/' + ROUTE_BASE_DATA; |
|
|
|
this.locationColDefinitions = SearchSelectComponent.getDefaultColDefLocations(); |
|
|
|
} |
|
|
|
@@ -58,8 +60,11 @@ export class TripLocationFormComponent extends AbstractDataFormComponent<TripLoc |
|
|
|
super.ngOnInit(); |
|
|
|
this.form.get('tripIri')?.setValue(this.trip.id); |
|
|
|
if (!this.isEditMode()) { |
|
|
|
this.form.get('arrivalDateTime')?.setValue(this.arrivalDateTime); |
|
|
|
this.form.get('departureDateTime')?.setValue(this.departureDateTime); |
|
|
|
this.form.get('newArrivalDateTime')?.setValue(this.newArrivalDateTime); |
|
|
|
this.form.get('newDepartureDateTime')?.setValue(this.newDepartureDateTime); |
|
|
|
} else { |
|
|
|
this.form.get('newArrivalDateTime')?.setValue(this.data?.arrivalDateTime); |
|
|
|
this.form.get('newDepartureDateTime')?.setValue(this.data?.departureDateTime); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -72,8 +77,115 @@ export class TripLocationFormComponent extends AbstractDataFormComponent<TripLoc |
|
|
|
} |
|
|
|
|
|
|
|
override onSubmit() { |
|
|
|
const formData = this.form.value as TripLocationJsonld; |
|
|
|
|
|
|
|
if (this.hasDateOverlap(formData)) { |
|
|
|
this.translateService?.get('trip_location.overlap_error').subscribe((error: string) => { |
|
|
|
alert(error); |
|
|
|
}); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
super.onSubmit(); |
|
|
|
} |
|
|
|
|
|
|
|
protected readonly tripLocationForm = tripLocationForm; |
|
|
|
private hasDateOverlap(formData: TripLocationJsonld): boolean { |
|
|
|
// Wenn kein Datum gesetzt ist, kann keine Überlappung stattfinden |
|
|
|
if (!formData.arrivalDateTime && !formData.departureDateTime) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// Wenn wir im Edit-Modus sind, entferne den aktuellen Eintrag aus der Überprüfung |
|
|
|
const entriesToCheck = this.isEditMode() |
|
|
|
? this.tripLocationEntries.filter(entry => entry.id !== this.data?.id) |
|
|
|
: this.tripLocationEntries; |
|
|
|
|
|
|
|
for (const entry of entriesToCheck) { |
|
|
|
// Fall 1: Nur ein Datum ist gesetzt in formData |
|
|
|
if (formData.arrivalDateTime && !formData.departureDateTime) { |
|
|
|
// Arrival date darf nicht mit anderen arrival dates übereinstimmen |
|
|
|
if (entry.arrivalDateTime === formData.arrivalDateTime) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// Überprüfe, ob arrival innerhalb eines existierenden Zeitraums liegt |
|
|
|
// ABER: arrival darf gleich mit einem departure sein |
|
|
|
if (entry.arrivalDateTime && entry.departureDateTime) { |
|
|
|
const arrivalDate = new Date(formData.arrivalDateTime); |
|
|
|
const entryArrival = new Date(entry.arrivalDateTime); |
|
|
|
const entryDeparture = new Date(entry.departureDateTime); |
|
|
|
|
|
|
|
// Liegt innerhalb des Zeitraums, aber ist NICHT gleich dem departure |
|
|
|
if (arrivalDate > entryArrival && arrivalDate < entryDeparture) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (!formData.arrivalDateTime && formData.departureDateTime) { |
|
|
|
// Departure date darf nicht mit anderen departure dates übereinstimmen |
|
|
|
if (entry.departureDateTime === formData.departureDateTime) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// Überprüfe, ob departure innerhalb eines existierenden Zeitraums liegt |
|
|
|
// ABER: departure darf gleich mit einem arrival sein |
|
|
|
if (entry.arrivalDateTime && entry.departureDateTime) { |
|
|
|
const departureDate = new Date(formData.departureDateTime); |
|
|
|
const entryArrival = new Date(entry.arrivalDateTime); |
|
|
|
const entryDeparture = new Date(entry.departureDateTime); |
|
|
|
|
|
|
|
// Liegt innerhalb des Zeitraums, aber ist NICHT gleich dem arrival |
|
|
|
if (departureDate > entryArrival && departureDate < entryDeparture) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// Fall 2: Beide Daten sind gesetzt in formData, überprüfe auf Überlappung |
|
|
|
else if (formData.arrivalDateTime && formData.departureDateTime) { |
|
|
|
const formArrival = new Date(formData.arrivalDateTime); |
|
|
|
const formDeparture = new Date(formData.departureDateTime); |
|
|
|
|
|
|
|
// Wenn der andere Eintrag beide Daten hat |
|
|
|
if (entry.arrivalDateTime && entry.departureDateTime) { |
|
|
|
const entryArrival = new Date(entry.arrivalDateTime); |
|
|
|
const entryDeparture = new Date(entry.departureDateTime); |
|
|
|
|
|
|
|
// Überprüfung auf Überlappung der Zeiträume, erlaubt grenzen zu teilen |
|
|
|
// Formular-Zeitraum darf vor oder nach dem Entry-Zeitraum liegen, |
|
|
|
// oder die Grenzen dürfen gleich sein (departure gleich arrival oder umgekehrt) |
|
|
|
if ( |
|
|
|
// Überlappung, aber nicht nur am Rand |
|
|
|
(formArrival < entryDeparture && formDeparture > entryArrival) && |
|
|
|
// Erlaubt: formDeparture === entryArrival ODER formArrival === entryDeparture |
|
|
|
!(formDeparture.getTime() === entryArrival.getTime() || |
|
|
|
formArrival.getTime() === entryDeparture.getTime()) |
|
|
|
) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
// Wenn der andere Eintrag nur arrival hat |
|
|
|
else if (entry.arrivalDateTime && !entry.departureDateTime) { |
|
|
|
const entryArrival = new Date(entry.arrivalDateTime); |
|
|
|
|
|
|
|
// Das arrival des anderen Eintrags darf nicht innerhalb des neuen Zeitraums liegen |
|
|
|
// ABER: Es darf am Rand liegen (gleich mit formDeparture) |
|
|
|
if (entryArrival > formArrival && entryArrival < formDeparture) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
// Wenn der andere Eintrag nur departure hat |
|
|
|
else if (!entry.arrivalDateTime && entry.departureDateTime) { |
|
|
|
const entryDeparture = new Date(entry.departureDateTime); |
|
|
|
|
|
|
|
// Das departure des anderen Eintrags darf nicht innerhalb des neuen Zeitraums liegen |
|
|
|
// ABER: Es darf am Rand liegen (gleich mit formArrival) |
|
|
|
if (entryDeparture > formArrival && entryDeparture < formDeparture) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |