mein-sterntours/app/Http/Controllers/BookingController.php
2021-03-31 17:55:02 +02:00

289 lines
11 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use App\Models\BookingDraftItem;
use App\Models\BookingFile;
use App\Models\Customer;
use App\Repositories\BookingFileRepository;
use App\Repositories\BookingRepository;
use App\Repositories\CustomerMailRepository;
use App\Repositories\DraftRepository;
use Request;
class BookingController extends Controller
{
protected $bookingRepo;
public function __construct(BookingRepository $bookingRepo)
{
$this->middleware('admin');
$this->bookingRepo = $bookingRepo;
}
public function index($step = false)
{
$data = [
//'bookings' => Booking::all()->sortByDesc("id"),
'step' => $step
];
return view('booking.index', $data);
}
public function detail($id)
{
if($id == "new") {
$booking = new Booking();
$id = 'new';
}else{
$booking = Booking::findOrFail($id);
$booking->getPassolutionPDF(true);
$id = $booking->id;
}
$data = [
'booking' => $booking,
'id' => $id,
];
return view('booking.detail', $data);
}
public function store($id)
{
// \Session()->flash('alert-save', '1');
$data = Request::all();
if($data['action'] === 'save_notice'){
$booking = $this->bookingRepo->updateNotice($id, $data);
\Session()->flash('alert-save', '1');
return redirect(route('booking_detail', [$booking->id])."#collapseBookingNotice");
}
if($data['action'] === 'update_booking'){
$booking = $this->bookingRepo->updateBooking($id, $data);
\Session()->flash('alert-save', '1');
return redirect(route('booking_detail', [$booking->id])."#collapseBookingBooking");
}
if($id === "new") {
$booking = new Booking();
}else{
$booking = Booking::findOrFail($id);
}
$booking->merlin_order_number = $data['merlin_order_number'];
$booking->save();
$i = 1;
if($data['action'] === 'addItemUp'){
$travel_program_id = null;
$request_date = null;
$comfort = 0;
if(count($booking->booking_draft_items)){
$first_booking_draft_item = $booking->booking_draft_items()->first();
$travel_program_id = $first_booking_draft_item->travel_program_id;
$request_date = $first_booking_draft_item->request_date;
$comfort = $first_booking_draft_item->comfort;
}
$booking->booking_draft_items()->create([
'booking_id' => $booking->id,
'travel_program_id' => $travel_program_id,
'fewo_lodging_id' => null,
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => null,
'request_date' => $request_date,
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => '',
'price_adult' => null,
'adult' => null ,
'price_children' => 0,
'children' => 0,
'price' => 0,
'pos' => $i,
'in_pdf' => true,
'comfort' => $comfort
]);
$i++;
}
if(isset($data['draft_item'])){
foreach ($data['draft_item'] as $booking_draft_item_id => $draft_item){
$BookingDraftItem = BookingDraftItem::findOrFail($booking_draft_item_id);
$draft_item['price_adult'] = isset($draft_item['price_adult']) ? $draft_item['price_adult'] : null;
$draft_item['adult'] = isset($draft_item['adult']) ? $draft_item['adult'] : null;
$draft_item['price_children'] = isset($draft_item['price_children']) ? $draft_item['price_children'] : null;
$draft_item['children'] = isset($draft_item['children']) ? $draft_item['children'] : null;
$draft_item['price'] = isset($draft_item['price']) ? $draft_item['price'] : null;
$draft_item['pos'] = $i++;
$draft_item['in_pdf'] = isset($draft_item['in_pdf']) ? true : false;
$BookingDraftItem->fill($draft_item);
$BookingDraftItem->save();
}
}
if($data['action'] === 'addItemDown'){
$travel_program_id = null;
$request_date = null;
$comfort = 0;
if(count($booking->booking_draft_items)){
$first_booking_draft_item = $booking->booking_draft_items()->first();
$travel_program_id = $first_booking_draft_item->travel_program_id;
$request_date = $first_booking_draft_item->request_date;
$comfort = $first_booking_draft_item->comfort;
}
$booking->booking_draft_items()->create([
'booking_id' => $booking->id,
'travel_program_id' => $travel_program_id,
'fewo_lodging_id' => null,
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => null,
'request_date' => $request_date,
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => '',
'price_adult' => null,
'adult' => null ,
'price_children' => 0,
'children' => 0,
'price' => 0,
'pos' => $i,
'in_pdf' => true,
'comfort' => $comfort
]);
}
$booking->calculate_price_total();
if(strpos($data['action'], 'up_') !== false) {
$reId = intval(str_replace('up_', '', $data['action']));
$d_from = BookingDraftItem::findOrFail($reId);
$d_to = $booking->findBeforeDraftItemRelation($reId);
if($d_to) {
$t_pos = $d_from->pos;
$d_from->pos = $d_to->pos;
$d_to->pos = $t_pos;
$d_from->save();
$d_to->save();
}
}
if(strpos($data['action'], 'down_') !== false) {
$reId = intval(str_replace('down_', '', $data['action']));
$d_from = BookingDraftItem::findOrFail($reId);
$d_to = $booking->findAfterDraftItemRelation($reId);
if($d_to) {
$t_pos = $d_from->pos;
$d_from->pos = $d_to->pos;
$d_to->pos = $t_pos;
$d_from->save();
$d_to->save();
}
}
\Session()->flash('alert-save', '1');
return redirect(route('booking_detail', [$booking->id]));
}
public function loadModal(){
$data = Request::all();
$ret = "";
if(Request::ajax()) {
if ($data['action'] === "new-customer-mail" || $data['action'] === "reply-customer-mail" || $data['action'] === "show-customer-mail" || $data['action'] === "edit-customer-mail"){
$data['customers'] = [];
if ($data['action'] === "new-customer-mail" && isset($data['booking_id']) && $booking = Booking::find($data['booking_id'])) {
$tmp = [];
$tmp['email'] = $booking->customer ? $booking->customer->email : "";
$tmp['name'] = $booking->customer ? $booking->customer->firstname . " " . $booking->customer->name . " | " : "- | ";
$tmp['name'] .= $booking->travel_country_id ? $booking->travel_country->name . " | " : "- | ";
$tmp['name'] .= $booking->travelagenda_id ? $booking->travel_agenda->name . "" : "-";
$data['customers'][$booking->id] = $tmp;
}
$ret = CustomerMailRepository::loadModal($data);
}
if($data['action'] === "modal-upload-booking-file") {
$ret = view("booking.upload_modal", compact('data'))->render();
}
if($data['action'] === "upload-booking-file"){
if($data['booking_id']){
$bookingFileRepo = new BookingFileRepository(new BookingFile());
$bookingFileRepo->_set('disk', 'booking');
$bookingFileRepo->_set('booking_id', $data['booking_id']);
$bookingFileRepo->_set('dir', '/files/'.date('Y/m').'/');
$bookingFileRepo->_set('identifier', 'booking');
return $bookingFileRepo->uploadFile(Request::all());
}
}
}
return response()->json(['response' => $data, 'html'=>$ret]);
}
public function draftItemDelete($id){
$boking_draft_item = BookingDraftItem::findOrFail($id);
$booking = $boking_draft_item->booking;
$boking_draft_item->delete();
$booking->calculate_price_total();
\Session()->flash('alert-success', __('Eintrag gelöscht'));
return redirect(route('booking_detail', [$booking->id]));
}
public function action($action, $id=false){
if($action === 'change_travel_dates'){
if($booking = Booking::find($id)){
$draftRepo = new DraftRepository($booking);
$draftRepo->change_dates_drafts_from_booking(Request::get('change_travel_start_date'));
\Session()->flash('alert-success', __('Datum der Reise wurde geändert'));
return redirect(route('booking_detail', [$booking->id]));
}
}
}
public function delete($id, $del="booking"){
if($del === 'booking') {
//$model = Booking::findOrFail($id);
//$model->delete();
\Session()->flash('alert-success', __('Buchung gelöscht sowie die Reservierung im Sterntrous Kalender'));
}
if($del === 'booking_file'){
$booking_file = BookingFile::findOrFail($id);
$booking = $booking_file->booking;
$fileRepo = new BookingFileRepository($booking_file);
$fileRepo->_set('disk', 'booking');
$fileRepo->delete();
$booking_file->delete();
\Session()->flash('alert-success', 'Datei gelöscht');
return redirect(route('booking_detail', [$booking->id]));
}
if($del === 'passolution_file'){
$booking = Booking::findOrFail($id);
$booking->resyncPassolutionPDF();
\Session()->flash('alert-success', 'Passolution erneuert');
return redirect(route('booking_detail', [$booking->id]));
}
return redirect(route('requests'));
}
}