mein-sterntours/app/Repositories/BookingRepository.php

449 lines
18 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\Booking;
use App\Models\BookingCompanyService;
use App\Models\BookingCountryService;
use App\Models\BookingDraftItem;
use App\Models\BookingNotice;
use App\Models\BookingProviderService;
use App\Models\BookingServiceItem;
use App\Models\Lead;
use App\Models\Participant;
use App\Models\ServiceProviderEntry;
use App\Services\Util;
use Auth;
class BookingRepository extends BaseRepository
{
public function __construct(Booking $model)
{
$this->model = $model;
}
public function update($data)
{
return $this->model;
}
public function updateNotice($id, $data)
{
$this->model = Booking::findOrFail($id);
if ($data['action'] === 'edit_notice' && isset($data['notice_id'])) {
$BookingNotice = BookingNotice::findOrFail($data['notice_id']);
$BookingNotice->message = isset($data['booking_notice']) ? $data['booking_notice'] : "";
$BookingNotice->edit_at = now();
$BookingNotice->save();
} else {
//save_notice
BookingNotice::create(
[
'booking_id' => $this->model->id,
'from_user_id' => Auth::user()->id,
'to_user_id' => isset($this->model->sf_guard_user->user_id) ? $this->model->sf_guard_user->user_id : null,
'message' => isset($data['booking_notice']) ? $data['booking_notice'] : "",
]
);
}
return $this->model;
}
public function updateLeadStatus($id, $data)
{
$this->model = Booking::findOrFail($id);
if (isset($data['lead'])) {
$lead = $this->model->lead;
if ($lead->id != $data['lead']['id']) {
abort(500);
}
$lead->status_id = $data['lead']['status_id'];
$lead->is_rebook = isset($data['lead']['is_rebook']) ? true : false;
$lead->is_closed = isset($data['lead']['is_closed']) ? true : false;
$lead->save();
}
return $this->model;
}
public function updateBooking($id, $data)
{
$this->model = Booking::findOrFail($id);
$fill = [
'sf_guard_user_id' => $data['sf_guard_user_id'],
'booking_date' => $data['booking_date'] ? _reformat_date($data['booking_date']) : null,
'travel_number' => $data['travel_number'],
'travel_country_id' => $data['travel_country_id'] ? $data['travel_country_id'] : null,
'travelagenda_id' => $data['travelagenda_id'] ? $data['travelagenda_id'] : null,
'travel_category_id' => $data['travel_category_id'] ? $data['travel_category_id'] : null,
'comfort' => isset($data['travel_comfort']) ? true : false,
'start_date' => $data['start_date'] ? _reformat_date($data['start_date']) : null,
'end_date' => $data['end_date'] ? _reformat_date($data['end_date']) : null,
'title' => $data['title'],
'pax' => $data['pax'],
'travel_documents' => $data['travel_documents'],
'paying_out' => $data['paying_out'],
'paying_out_status' => $data['paying_out_status'],
'branch_id' => isset($data['branch_id']) ? $data['branch_id'] : 4,
'airport_id' => (isset($data['airport_id']) && $data['airport_id'] > 0) ? $data['airport_id'] : null,
'travel_company_id' => $data['travel_company_id'],
//'airline_id' => $data['airline_id'],
'airline_ids' => isset($data['airline_ids']) ? $data['airline_ids'] : null,
'refund' => $data['refund'],
'refund_date' => _reformat_date($data['refund_date']),
'lawyer_date' => _reformat_date($data['lawyer_date']),
'hold' => $data['hold'],
'xx_tkt' => $data['xx_tkt'],
'xx_tkt_date' => _reformat_date($data['xx_tkt_date']),
'filekey' => $data['filekey'],
'is_rail_fly' => isset($data['is_rail_fly']) ? true : false,
'notice' => $data['notice'],
'ev_number' => $data['ev_number'],
'merlin_order_number' => $data['merlin_order_number'],
];
$this->model->fill($fill);
$this->model->save();
if ($this->model->booking_draft_items) {
foreach ($this->model->booking_draft_items as $booking_draft_item) {
$booking_draft_item->comfort = isset($data['travel_comfort']) ? true : false;
$booking_draft_item->save();
}
}
return $this->model;
}
public function updateBookingServices($id, $data)
{
$this->model = Booking::findOrFail($id);
if (isset($data['country_service'])) {
$this->updateCountryService($data['country_service']);
}
if (isset($data['provider_service'])) {
$this->updateProviderService($data['provider_service']);
}
if (isset($data['company_service'])) {
$this->updateCompanyService($data['company_service']);
}
return $this->model;
}
public function updateBookingNumber($id, $data)
{
$this->model = Booking::findOrFail($id);
$fill = [
'ev_number' => $data['ev_number'],
'merlin_order_number' => $data['merlin_order_number'],
];
$this->model->fill($fill);
$this->model->save();
return $this->model;
}
public function updateBookingPrice($id, $data)
{
$this->model = Booking::findOrFail($id);
$fill = [
'deposit_total' => $data['deposit_total'] ? Util::_clean_float($data['deposit_total']) : 0,
'final_payment' => $data['final_payment'] ? Util::_clean_float($data['final_payment']) : 0,
'final_payment_date' => $data['final_payment_date'] ? _reformat_date($data['final_payment_date']) : null,
'price_total' => ($this->model->getPriceRaw() + $this->model->getServiceTotal(true)),
];
$this->model->fill($fill);
$this->model->save();
return $this->model;
}
public function updateServiceProviderEntry($id, $data)
{
$this->model = Booking::findOrFail($id);
if (isset($data['service_provider_entry'])) {
foreach ($data['service_provider_entry'] as $spe_id => $fill) {
$ServiceProviderEntry = ServiceProviderEntry::findOrFail($spe_id);
if ($ServiceProviderEntry->booking_id !== $this->model->id) {
abort(500);
}
$fill['is_cleared'] = isset($fill['is_cleared']) ? true : false;
$fill['payment_date'] = isset($fill['payment_date']) ? _reformat_date($fill['payment_date']) : null;
$ServiceProviderEntry->fill($fill);
$ServiceProviderEntry->save();
}
}
return $this->model;
}
public function updateBookingServiceItem($id, $data)
{
$this->model = Booking::findOrFail($id);
if (isset($data['booking_service_item'])) {
foreach ($data['booking_service_item'] as $bsi_id => $fill) {
$BookingServiceItem = BookingServiceItem::findOrFail($bsi_id);
if ($BookingServiceItem->booking_id !== $this->model->id) {
abort(500);
}
$fill['is_commission_locked'] = isset($fill['is_commission_locked']) ? true : false;
$fill['travel_date'] = isset($fill['travel_date']) ? _reformat_date($fill['travel_date']) : now();
$BookingServiceItem->fill($fill);
$BookingServiceItem->save();
if ($fill['is_commission_locked'] === true) {
$service_price_refund = 0;
if ($BookingServiceItem->getServicePriceRaw() > 0) {
$service_price_refund = $BookingServiceItem->getServicePriceRaw() / 100 * $BookingServiceItem->travel_company->getPercentageRaw();
}
$BookingServiceItem->setServicePriceRefundRaw($service_price_refund);
$BookingServiceItem->save();
}
}
}
$this->model->price_total = ($this->model->getPriceRaw() + $this->model->getServiceTotal(true));
$this->model->save();
return $this->model;
}
public function updateBookingParticipant($id, $data)
{
$this->model = Booking::findOrFail($id);
if (isset($data['participant'])) {
foreach ($data['participant'] as $p_id => $fill) {
$Participant = Participant::findOrFail($p_id);
if ($Participant->booking_id !== $this->model->id) {
abort(500);
}
$fill['participant_pass'] = isset($fill['participant_pass']) ? true : false;
$fill['participant_storno'] = isset($fill['participant_storno']) ? true : false;
$fill['participant_child'] = isset($fill['participant_child']) ? true : false;
$fill['participant_birthdate'] = isset($fill['participant_birthdate']) ? _reformat_date($fill['participant_birthdate']) : null;
$Participant->fill($fill);
$Participant->save();
}
}
//main
$this->model->participant_salutation_id = isset($data['participant_salutation_id']) ? $data['participant_salutation_id'] : null;
$this->model->participant_name = isset($data['participant_name']) ? $data['participant_name'] : null;
$this->model->participant_firstname = isset($data['participant_firstname']) ? $data['participant_firstname'] : null;
$this->model->nationality_id = isset($data['nationality_id']) ? $data['nationality_id'] : null;
$this->model->participant_birthdate = isset($data['participant_birthdate']) ? _reformat_date($data['participant_birthdate']) : null;
$this->model->participant_pass = isset($data['participant_pass']) ? true : false;
//update pax
if ($this->model->participants->count() > 0) {
$this->model->pax = $this->model->participants->where('participant_storno', false)->count();
}
$this->model->save();
return $this->model;
}
private function updateCountryService($country_services)
{
foreach ($country_services as $country_service_id => $val) {
$booking_country_service = BookingCountryService::where('travel_country_service_id', '=', $country_service_id)
->where('booking_id', '=', $this->model->id)->first();
if (!$booking_country_service) {
BookingCountryService::create([
'travel_country_service_id' => $country_service_id,
'booking_id' => $this->model->id,
'status' => $val
]);
} else {
$booking_country_service->fill([
'status' => $val
]);
$booking_country_service->save();
}
}
}
private function updateProviderService($provider_service)
{
foreach ($provider_service as $provider_service_id => $val) {
$booking_provider_service = BookingProviderService::where('service_provider_service_id', '=', $provider_service_id)
->where('booking_id', '=', $this->model->id)->first();
if (!$booking_provider_service) {
BookingProviderService::create([
'service_provider_service_id' => $provider_service_id,
'booking_id' => $this->model->id,
'status' => $val
]);
} else {
$booking_provider_service->fill([
'status' => $val
]);
$booking_provider_service->save();
}
}
}
private function updateCompanyService($company_service)
{
foreach ($company_service as $company_service_id => $val) {
$booking_company_service = BookingCompanyService::where('travel_company_service_id', '=', $company_service_id)
->where('booking_id', '=', $this->model->id)->first();
if (!$booking_company_service) {
BookingCompanyService::create([
'travel_company_service_id' => $company_service_id,
'booking_id' => $this->model->id,
'status' => $val
]);
} else {
$booking_company_service->fill([
'status' => $val
]);
$booking_company_service->save();
}
}
}
public function convertArrangementsToDrafts(Booking $booking)
{
if (!$booking->arrangements || $booking->arrangements->count() == 0) {
return;
}
$nextPos = 1;
if ($booking->booking_draft_items && $booking->booking_draft_items->count() > 0) {
$nextPos = $booking->booking_draft_items->max('pos') + 1;
}
foreach ($booking->arrangements->sortByDesc('view_position') as $arrangement) {
$data = $arrangement->getDataAsMap();
// Erstelle neues BookingDraftItem aus Arrangement
$draftItem = new BookingDraftItem();
// Mappe die grundlegenden Daten
$draftItem->booking_id = $booking->id;
$draftItem->pos = $nextPos++;
// Daten von Arrangement übertragen
if ($arrangement->begin) {
$draftItem->start_date = $arrangement->begin->format('Y-m-d');
}
if ($arrangement->end) {
$draftItem->end_date = $arrangement->end->format('Y-m-d');
}
// Service/Beschreibung aus data_s generieren
$serviceText = $arrangement->getDataS();
$draftItem->service = $serviceText;
$draftItem->adult = $data['Teilnehmer'] ?? $booking->pax;
$draftItem->price_adult = $data['Preis'] ?? 0;
// PDF-Einstellung übernehmen
$draftItem->in_pdf = $arrangement->in_pdf ? 1 : 0;
// Versuche DraftType zu finden basierend auf ArrangementType
if ($arrangement->arrangement_type && isset($arrangement->arrangement_type->name)) {
$draftType = \App\Models\DraftType::where('name', 'like', '%' . $arrangement->arrangement_type->name . '%')->first();
if ($draftType) {
$draftItem->draft_type_id = $draftType->id;
}
} else {
// Fallback: Suche nach einem passenden DraftType basierend auf dem Namen
$draftType = \App\Models\DraftType::where('name', 'like', '%' . $arrangement->type_s . '%')->first();
if ($draftType) {
$draftItem->draft_type_id = $draftType->id;
}
}
// Standardwerte für neue Felder setzen
$draftItem->comfort = 0;
$draftItem->price = 0;
$draftItem->adult = 0;
$draftItem->price_children = 0;
$draftItem->children = 0;
$draftItem->save();
// Arrangement als konvertiert markieren oder löschen
// Hier löschen wir das Arrangement nach erfolgreicher Konvertierung
$arrangement->delete();
}
// Stelle sicher, dass new_drafts aktiviert ist
if (!$booking->new_drafts) {
$booking->new_drafts = true;
$booking->save();
}
// Preise neu berechnen
$booking->calculate_price_total();
}
public function loadDraftToBooking($bookingId, $draftId)
{
$booking = Booking::findOrFail($bookingId);
$draft = \App\Models\Draft::findOrFail($draftId);
if (!$booking->new_drafts) {
$booking->new_drafts = true;
$booking->save();
}
// Lösche bestehende Draft Items falls gewünscht
if ($booking->booking_draft_items && $booking->booking_draft_items->count() > 0) {
foreach ($booking->booking_draft_items as $existingItem) {
$existingItem->delete();
}
}
$pos = 1;
foreach ($draft->draft_items as $draftItem) {
$bookingDraftItem = new \App\Models\BookingDraftItem();
// Kopiere alle Daten aus der Draft Vorlage
$bookingDraftItem->booking_id = $booking->id;
$bookingDraftItem->draft_type_id = $draftItem->draft_type_id;
$bookingDraftItem->travel_program_id = null; // Wird später gesetzt wenn nötig
$bookingDraftItem->fewo_lodging_id = null;
$bookingDraftItem->travel_class_id = null;
$bookingDraftItem->draft_item_id = $draftItem->id;
$bookingDraftItem->request_date = null;
$bookingDraftItem->days_start = $draftItem->days_start;
$bookingDraftItem->days_duration = $draftItem->days_duration;
$bookingDraftItem->start_date = null; // Wird später basierend auf Reisebeginn gesetzt
$bookingDraftItem->end_date = null; // Wird später basierend auf Reiseende gesetzt
$bookingDraftItem->service = $draftItem->service;
$bookingDraftItem->price_adult = $draftItem->price_adult;
$bookingDraftItem->adult = $draftItem->adult;
$bookingDraftItem->price_children = $draftItem->price_children;
$bookingDraftItem->children = $draftItem->children;
$bookingDraftItem->price = 0; // Wird später berechnet
$bookingDraftItem->pos = $pos++;
$bookingDraftItem->in_pdf = $draftItem->in_pdf;
$bookingDraftItem->comfort = 0;
$bookingDraftItem->status = 1;
$bookingDraftItem->save();
}
// Preise neu berechnen
$booking->calculate_price_total();
return $booking;
}
}