240 lines
9.0 KiB
PHP
240 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Concession;
|
|
use App\CulturecardTicket;
|
|
use App\Http\Controllers\BookControllerHelper\ShowBookController;
|
|
use App\Http\Controllers\BookControllerHelper\StoreBookController;
|
|
use App\Order;
|
|
use App\Paymentmethod;
|
|
use App\Seat;
|
|
use App\SingleSeat;
|
|
use App\SingleSeatsUser;
|
|
use App\Traits\CulturecardTrait;
|
|
use App\Traits\MoneyTrait;
|
|
use DateTime;
|
|
use Faker\Provider\Payment;
|
|
use Illuminate\Http\Request as IRequest;
|
|
use Illuminate\Support\Facades\Cookie;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Event;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Request;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Traits\EventTrait;
|
|
|
|
class BookController extends Controller
|
|
{
|
|
use EventTrait;
|
|
use MoneyTrait;
|
|
use CulturecardTrait;
|
|
|
|
|
|
public function getActiveCultureCardId($userid){
|
|
$activeCultureCard = User::find($userid)->culturecard_user()->orderBy('created_at', 'asc')->get();
|
|
|
|
foreach($activeCultureCard as $singleCultureCard){
|
|
if($this->verifySingleCultureCard($singleCultureCard->culturecard()->first())){
|
|
return $singleCultureCard->culturecard()->first()->id;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
protected function verifySingleCultureCard($activeCultureCard, $amountTicketsToBuy, $event)
|
|
{
|
|
$now = new DateTime();
|
|
$cardValidUntil = DateTime::createFromFormat('Y-m-d', $activeCultureCard->renewal_date);
|
|
if ($now > $cardValidUntil) {
|
|
return false;
|
|
}
|
|
|
|
|
|
//check whether user has enough free cards left
|
|
$amountAlreadyBought = $activeCultureCard->get_amount_bought_tickets();
|
|
if ($amountAlreadyBought + $amountTicketsToBuy > 5) {
|
|
return false;
|
|
}
|
|
|
|
//check whether user has bought tickets of this event with culturecard already
|
|
$ticketsThisEventBoughtWithCultureCard = 0;
|
|
foreach ($activeCultureCard->culturecard_ticket()->get() as $ticket) {
|
|
if ( $ticket->singleSeat()->first() == null || $event["event_id"] == $ticket->singleSeat()->first()->seat()->first()->event_id) {
|
|
$ticketsThisEventBoughtWithCultureCard++;
|
|
}
|
|
}
|
|
if ($ticketsThisEventBoughtWithCultureCard + $amountTicketsToBuy > 2) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
return (new ShowBookController())->show($id);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$book = Request::all();
|
|
if (Auth::check()) {
|
|
return $this->doBooking($book);
|
|
} else {
|
|
$book["usedLogin"] = "1";
|
|
Cookie::queue('booking', serialize($book), 60 * 24); //3. argument -> time in minutes
|
|
return redirect('/login')->with(['showSteps']);
|
|
}
|
|
}
|
|
|
|
public function storeBooking(IRequest $request)
|
|
{
|
|
$book = unserialize(Cookie::get('booking'));
|
|
Cookie::queue(Cookie::forget('booking'));
|
|
if ($request->input('paywithculturecard') != null) {
|
|
$book["payWithCultureCard"] = $request->input('paywithculturecard');
|
|
}
|
|
if ($request->input('useculturecardconc') != null) {
|
|
$book["useculturecardconc"] = $request->input('useculturecardconc');
|
|
}
|
|
return $this->doBooking($book);
|
|
}
|
|
|
|
|
|
private function doBooking($book)
|
|
{
|
|
|
|
if(isset($book["bookingConfirmed"])) {
|
|
foreach($book as $key => $b){
|
|
if($key != '_token' && $key != 'bookingConfirmed')
|
|
$book[$key] = unserialize($book[$key]);
|
|
}
|
|
return (new StoreBookController())->doBooking($book);
|
|
}else {
|
|
if(!isset($book["payWithCultureCard"]) && !isset($book["useculturecardconc"])) {
|
|
$event = Event::find($book["event_id"]);
|
|
if($event["payableWithCultureCard"] == "1"){
|
|
$userid = $this->getUserIdForBooking($book);
|
|
$returnResult = $this->checkCultureCardForBooking($book, $event, $userid);
|
|
if ($returnResult) return $returnResult;
|
|
}
|
|
}
|
|
|
|
$priceAfterConc = array();
|
|
if(isset($book["payWithCultureCard"]) || isset($book["useculturecardconc"])){
|
|
foreach($book as $key => $b){
|
|
if($key != '_token')
|
|
$book[$key] = unserialize($book[$key]);
|
|
}
|
|
if(isset($book["useculturecardconc"])){
|
|
$cultureCardConcession = Concession::where('name', 'Kulturkarte')->first();
|
|
|
|
$book["concession"][0] = "".$cultureCardConcession->id;
|
|
if(sizeof($book["concession"]) > 1) $book["concession"][1] = "".$cultureCardConcession->id;
|
|
}
|
|
}
|
|
if(isset($book["seat"])) {
|
|
foreach ($book["seat"] as $key => $s) {
|
|
$splitted = explode("_", $s);
|
|
$singleSeat = SingleSeat::where('seat_id', $book["seat_id"])->where('x', $splitted[0])->where('y', $splitted[1])->first();
|
|
$curConc = $book["concession"][$key];
|
|
$price = $this->calcPriceTicketFromSingleSeat($singleSeat, $curConc, -1);
|
|
array_push($priceAfterConc, $price);
|
|
}
|
|
}
|
|
if(isset($book["amountSeatsToBook"])){
|
|
// dd($book);
|
|
for($i=0; $i < $book["amountSeatsToBook"]; $i++){
|
|
$curConc = $book["concession"][$i];
|
|
$singleSeat = SingleSeat::where('seat_id', $book["seat_id"])->first();
|
|
$price = $this->calcPriceTicketFromSingleSeat($singleSeat, $curConc, -1);
|
|
array_push($priceAfterConc, $price);
|
|
}
|
|
}
|
|
|
|
return view('book.confirmation', compact('book', 'priceAfterConc'));
|
|
}
|
|
}
|
|
|
|
private function checkCultureCardForBooking($book, $event, $userid){
|
|
$amountSeats = $this->getAmountSeatsForBooking($book);
|
|
if ($this->verifyCultureCard($event, User::find($userid), 1)) {
|
|
//user can buy event with culture card
|
|
if (!isset($book["payWithCultureCard"])) {
|
|
//ask user whether he wanna pay with culture card
|
|
|
|
foreach($book as $key => $b){
|
|
if($key != '_token' && $key != 'bookingConfirmed')
|
|
$book[$key] = serialize($book[$key]);
|
|
}
|
|
|
|
Cookie::queue('booking', serialize($book), 60 * 24);
|
|
|
|
$cultureCardConcessionAvailableOnACard = $this->cultureCardConcessionAvailableOnACard(User::find($userid));
|
|
|
|
$possibleToPay = true;
|
|
return view("/paywithculturecard", compact('cultureCardConcessionAvailableOnACard', 'possibleToPay'));
|
|
} else if (isset($book["payWithCultureCard"]) && $book["payWithCultureCard"] == "1") {
|
|
if (!$this->verifyCultureCard($event,User::find($userid),1)) {
|
|
return redirect('/book/' . $book["event_id"] . '-0');
|
|
}
|
|
}
|
|
}else if($this->cultureCardConcessionAvailableOnACard(User::find($userid))){
|
|
$possibleToPay = false;
|
|
|
|
foreach($book as $key => $b){
|
|
if($key != '_token' && $key != 'bookingConfirmed')
|
|
$book[$key] = serialize($book[$key]);
|
|
}
|
|
|
|
Cookie::queue('booking', serialize($book), 60 * 24);
|
|
|
|
$cultureCardConcessionAvailableOnACard = $this->cultureCardConcessionAvailableOnACard(User::find($userid));
|
|
return view("/paywithculturecard", compact('cultureCardConcessionAvailableOnACard', 'possibleToPay'));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function getAmountSeatsForBooking($book){
|
|
//keine platzwahl
|
|
if(isset($book["amountSeatsToBook"])) return $book["amountSeatsToBook"];
|
|
|
|
//weihnachtsevent
|
|
if(isset($book["amountBuffett"]) && isset($book["seat"])) return $book["amountBuffett"] + count($book["seat"]);
|
|
if(isset($book["amountBuffett"])) return $book["amountBuffett"];
|
|
|
|
//platzwahl
|
|
return sizeof($book["seat"]);
|
|
}
|
|
|
|
private function getUserIdForBooking($book){
|
|
//normal user buys a ticket
|
|
$userid = Auth::user()->id;
|
|
|
|
//admin buys ticket for an existing user
|
|
if (!empty($book["user"]) > 0 && Auth::check() && Auth::user()->isAdmin()) {
|
|
$userid = $book["user"];
|
|
}
|
|
|
|
//admin buys ticket for an existing user
|
|
if(!empty($book["newLastname"]) > 0){
|
|
$newUser = new User;
|
|
$newUser->firstname = $book["newFirstname"];
|
|
$newUser->lastname = $book["newLastname"];
|
|
$newUser->street = $book["newStreet"];
|
|
$newUser->location = $book["newLocation"];
|
|
$newUser->email = $book["newLastname"]."-".$book["newFirstname"]."-".rand(100000,999999)."@inserted.user";
|
|
$newUser->password = "automaticallyGeneratedPwjiaghaighö";
|
|
$newUser->save();
|
|
$userid = $newUser->id;
|
|
}
|
|
return $userid;
|
|
}
|
|
|
|
}
|