mein-sterntours/app/Http/Controllers/BookingController.php
2020-04-15 12:11:42 +02:00

229 lines
8 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use App\Models\BookingDraftItem;
use App\Models\Customer;
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);
$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($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()){
$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);
}
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]));
}
}
}
}