341 lines
11 KiB
PHP
341 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Category;
|
|
use App\Concession;
|
|
use App\Event;
|
|
use App\Http\Controllers\EventControllerHelper\EventStoreController;
|
|
use App\Order;
|
|
use App\Seattemplate;
|
|
use App\SingleSeat;
|
|
use App\Seat;
|
|
use App\Reservation;
|
|
use App\SingleSeatsUser;
|
|
use App\Traits\MoneyTrait;
|
|
use DateInterval;
|
|
use DatePeriod;
|
|
use DateTime;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Mockery\Exception;
|
|
use Monolog\Logger;
|
|
use Request;
|
|
use App\Traits\EventTrait;
|
|
|
|
class EventController extends Controller
|
|
{
|
|
use EventTrait, MoneyTrait;
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$events=Event::orderBy('start_date', 'desc')->get();
|
|
return view('admin.events.index',compact('events'));
|
|
}
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$cats=Category::all();
|
|
$concessions=DB::table('concessions')->where('global', '=', 1)->get();
|
|
$reservations = Reservation::pluck('name','id');
|
|
$seattemplates = Seattemplate::all();
|
|
return view('admin.events.create',compact('cats', 'reservations', 'concessions', 'seattemplates'));
|
|
}
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function store()
|
|
{
|
|
return (new EventStoreController())->store();
|
|
}
|
|
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$event=Event::find($id);
|
|
|
|
$concessions = $event->concessions()->get();
|
|
|
|
$seatMap = null;
|
|
if($this->hasSeatMap($event)) {
|
|
$seat = $event->seat()->first();
|
|
$seatMap = $this->calcSeatMap($seat);
|
|
}
|
|
$cats=Category::all();
|
|
$reservations = Reservation::pluck('name','id');
|
|
$selReservation = $event->reservation->id;
|
|
|
|
$tickets = [];
|
|
try{
|
|
if($event->seat()->first() != null)
|
|
$tickets = $event->seat()->first()->singleseats()->where('booked', '1')->get();
|
|
}catch(\Exception $e){
|
|
|
|
}
|
|
return view('admin.events.edit',compact('event','cats', 'reservations','selReservation', 'seatMap', 'concessions', 'tickets'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function update($id)
|
|
{
|
|
$eventUpdate = Request::all();
|
|
|
|
if(!empty($eventUpdate["image"])){
|
|
$image = $eventUpdate["image"];
|
|
$newFileName = time()."_".$image->getClientOriginalName();
|
|
$image->move("imgs/events",$newFileName);
|
|
$eventUpdate["image"] = $newFileName;
|
|
}
|
|
|
|
$event=Event::find($id);
|
|
if(isset($eventUpdate["amountSeats"])){
|
|
if($eventUpdate["amountSeats"] > $event->amountSeats){
|
|
$diff = $eventUpdate["amountSeats"]-$event->amountSeats;
|
|
$startValue = $event->seat()->first()->singleSeats()->max('y')+1;
|
|
$seat = $event->seat()->first();
|
|
for($i=$startValue; $i<$diff+$startValue; $i++){
|
|
$s = new SingleSeat;
|
|
$s->x = 1;
|
|
$s->y = $i+1;
|
|
$s->category = "s";
|
|
$s->seat()->associate($seat);
|
|
$s->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
$event->update($eventUpdate);
|
|
if(!empty($eventUpdate["pinned"]) && $eventUpdate["pinned"] === "on"){
|
|
$event->pinned = 1;
|
|
}else{
|
|
$event->pinned = 0;
|
|
}
|
|
if(!empty($eventUpdate["hide_externally"]) && $eventUpdate["hide_externally"] === "on"){
|
|
$event->hide_externally= 1;
|
|
}else{
|
|
$event->hide_externally = 0;
|
|
}
|
|
$event->save();
|
|
|
|
if(!empty($eventUpdate["cat"]))
|
|
$event->categories()->sync($eventUpdate["cat"]);
|
|
|
|
$reservationType = $event->reservation()->first()->name; //e.g. "Ohne Sitzplatz", ...
|
|
if($reservationType == "Mit Platzwahl" || $reservationType == "Mit Tischwahl") {
|
|
$this->updateSeatMap($event->seat()->first());
|
|
}
|
|
|
|
\Session::flash('flash_message', 'Die Veranstaltung wurde erfolgreich bearbeitet!');
|
|
if(!empty($eventUpdate["cat"])){
|
|
return redirect('admin/events');
|
|
}else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
DB::transaction(function () use ($id) {
|
|
// Check if there are any rows in `single_seat_user` related to the single seats of the event
|
|
$singleSeatUserCount = DB::table('single_seat_user')
|
|
->whereIn('single_seat_id', function ($query) use ($id) {
|
|
$query->select('id')
|
|
->from('single_seats')
|
|
->whereIn('seat_id', function ($subQuery) use ($id) {
|
|
$subQuery->select('id')
|
|
->from('seats')
|
|
->where('event_id', $id);
|
|
});
|
|
})
|
|
->count();
|
|
|
|
// Proceed with deletion only if there are no related rows
|
|
if ($singleSeatUserCount === 0) {
|
|
DB::statement("DELETE FROM culturecards_tickets WHERE singleseat_id IN (
|
|
SELECT id FROM single_seats WHERE seat_id IN (
|
|
SELECT id FROM seats WHERE event_id=?
|
|
)
|
|
)", [$id]);
|
|
|
|
DB::statement("DELETE FROM single_seats WHERE seat_id IN (
|
|
SELECT id FROM seats WHERE event_id=?
|
|
)", [$id]);
|
|
|
|
DB::statement("DELETE FROM seats WHERE event_id=?", [$id]);
|
|
|
|
DB::statement("DELETE FROM event_category WHERE event_id=?", [$id]);
|
|
|
|
DB::statement("DELETE FROM event_concessions WHERE event_id=?", [$id]);
|
|
|
|
DB::statement("DELETE FROM events WHERE id=?", [$id]);
|
|
\Session::flash('flash_message', 'Die Veranstaltung wurde erfolgreich gelöscht!');
|
|
}else{
|
|
\Session::flash('flash_error', 'Veranstaltung kann nicht gelöscht werden, da bereits Karten verkauft wurden');
|
|
}
|
|
});
|
|
return redirect('admin/events');
|
|
}
|
|
|
|
public function overview($id){
|
|
$seat = Seat::find($id);
|
|
|
|
$ssuForEvent = $this->getSSUForEvent($seat);
|
|
$totalPrice = $this->getTotalPriceOfSSUs($ssuForEvent);
|
|
$portoCosts = $this->getPortoCostsOfSSUs($ssuForEvent);
|
|
|
|
$transactionPrice = $this->getPricePerPaymentmethod($seat, '1');
|
|
$barPrice = $this->getPricePerPaymentmethod($seat, '2');
|
|
$voucherPrice = $this->getPricePerPaymentmethod($seat, '3');
|
|
$atmPrice = $this->getPricePerPaymentmethod($seat, '4');
|
|
$cultureCardPrice = $this->getPricePerPaymentmethod($seat, '5');
|
|
|
|
|
|
return view('admin/events/overview', compact('seat', 'totalPrice',
|
|
'transactionPrice', 'barPrice', 'voucherPrice', 'atmPrice', 'cultureCardPrice', 'portoCosts'));
|
|
}
|
|
|
|
public function overviewList($id){
|
|
$seat = Seat::find($id);
|
|
$orders = $this->getUniqueOrdersOfSeat($seat);
|
|
|
|
echo "<table border='1'><tr><th>Rechnungsnummer</th><th>Kundenname</th><th>Betrag</th></tr>";
|
|
foreach($orders as $order){
|
|
if($order->singleseatusers->first()->paymentmethod->name === "Überweisung") {
|
|
echo "<tr>";
|
|
echo "<td>" . $order->id . "</td>";
|
|
echo "<td>" . $order->user()->withTrashed()->first()->firstname . " " . $order->user()->withTrashed()->first()->lastname . "</td>";
|
|
echo "<td>€ " . number_format($this->calcPriceOrder($order->id) / 100, 2) . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
}
|
|
}
|
|
public function overviewListAll($id){
|
|
$seat = Seat::find($id);
|
|
$orders = $this->getUniqueOrdersOfSeat($seat);
|
|
|
|
echo "<table border='1'><tr><th>Rechnungsnummer</th><th>Bezahlmethode</th><th>Sitzplatz</th><th>Kundenname</th><th>Betrag</th></tr>";
|
|
foreach($orders as $order){
|
|
echo "<tr>";
|
|
echo "<td>" . $order->id . "</td>";
|
|
echo "<td>" . $order->singleseatusers->first()->paymentmethod->name . "</td>";
|
|
echo "<td>";
|
|
foreach($order->singleseatusers()->get() as $seat){
|
|
echo $seat->calcSeatName()."; ";
|
|
}
|
|
echo "</td>";
|
|
echo "<td>" . $order->user()->withTrashed()->first()->firstname . " " . $order->user()->withTrashed()->first()->lastname . "</td>";
|
|
echo "<td>€ " . number_format($this->calcPriceOrder($order->id) / 100, 2) . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
}
|
|
|
|
private function getUniqueOrdersOfSeat($seat){
|
|
$ssus = $this->getSSUForEvent($seat);
|
|
return $this->getUniqueOrdersOfSSUs($ssus);
|
|
}
|
|
|
|
private function getUniqueOrdersOfSSUs($ssus){
|
|
$orders = array();
|
|
foreach($ssus as $ssu){
|
|
array_push($orders, $ssu->order);
|
|
}
|
|
|
|
$orders = array_unique($orders);
|
|
return $orders;
|
|
}
|
|
|
|
private function getPortoCostsOfSSUs($ssus){
|
|
$orders = $this->getUniqueOrdersOfSSUs($ssus);
|
|
$porto = 0;
|
|
foreach($orders as $order){
|
|
$porto += $order->porto;
|
|
}
|
|
|
|
return $porto;
|
|
}
|
|
|
|
private function getTotalPriceOfSSUs($ssus){
|
|
$totalPrice = 0;
|
|
|
|
foreach($ssus as $ssu){
|
|
$priceTicket = $this->calcPriceTicket($ssu);
|
|
$totalPrice += $priceTicket;
|
|
}
|
|
|
|
return $totalPrice;
|
|
}
|
|
|
|
private function getSSUForEvent($seat){
|
|
$ret = array();
|
|
foreach($seat->singleSeats()->where('booked', '1')->get() as $singleSeat){
|
|
$ssu = SingleSeatsUser::where('single_seat_id', $singleSeat->id)->get();
|
|
foreach($ssu as $s) {
|
|
array_push($ret, $s);
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
private function getPricePerPaymentmethod($seat, $paymentmethod_id){
|
|
$price = 0;
|
|
|
|
foreach($seat->singleSeats()->where('booked', '1')->get() as $singleSeat){
|
|
try{
|
|
$ssu = SingleSeatsUser::where([
|
|
['single_seat_id', $singleSeat->id]
|
|
])->get();
|
|
foreach($ssu as $s) {
|
|
if ($s->paymentmethod_id == $paymentmethod_id) {
|
|
$priceTicket = $this->calcPriceTicket($s);
|
|
$price += $priceTicket;
|
|
}
|
|
}
|
|
}catch(\Exception $e){
|
|
}
|
|
}
|
|
|
|
|
|
return $price;
|
|
}
|
|
|
|
}
|