265 lines
10 KiB
PHP
265 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Concession;
|
|
use App\CulturecardTicket;
|
|
use App\Order;
|
|
use App\Paymentmethod;
|
|
use App\Reservation;
|
|
use App\SingleSeatsUser;
|
|
use App\Traits\MoneyTrait;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Request;
|
|
use App\Seat;
|
|
use DateTime;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class ReservationController extends Controller
|
|
{
|
|
use MoneyTrait;
|
|
|
|
public function index()
|
|
{
|
|
$orders = Order::orderBy('id', 'desc')->get();
|
|
return view('admin.reservations.index', compact('orders'));
|
|
}
|
|
|
|
public function finance(){
|
|
//Run `update single_seat_user set paymentmethod_id=5 where single_seat_id in (select singleseat_id from culturecards_tickets) and paymentmethod_id <> 5;` to fix weird issue
|
|
DB::statement("update single_seat_user set paymentmethod_id=5 where single_seat_id in (select singleseat_id from culturecards_tickets) and paymentmethod_id <> 5;");
|
|
$kontoMapping = array();
|
|
$kontoMapping['A'] = '251009';
|
|
$kontoMapping['B'] = '252029';
|
|
$kontoMapping['C'] = '253013';
|
|
$kontoMapping['D'] = '254019';
|
|
$kontoMapping['E'] = '255015';
|
|
$kontoMapping['F'] = '256024';
|
|
$kontoMapping['G'] = '257022';
|
|
$kontoMapping['H'] = '258031';
|
|
$kontoMapping['I'] = '259016';
|
|
$kontoMapping['J'] = '259016';
|
|
$kontoMapping['K'] = '260030';
|
|
$kontoMapping['L'] = '261020';
|
|
$kontoMapping['M'] = '262028';
|
|
$kontoMapping['N'] = '263113';
|
|
$kontoMapping['O'] = '264016';
|
|
$kontoMapping['P'] = '265025';
|
|
$kontoMapping['Q'] = '266010';
|
|
$kontoMapping['R'] = '267024';
|
|
$kontoMapping['T'] = '271016';
|
|
$kontoMapping['U'] = '272011';
|
|
$kontoMapping['V'] = '273013';
|
|
$kontoMapping['W'] = '274032';
|
|
$kontoMapping['X'] = 'NONE';
|
|
$kontoMapping['Y'] = 'NONE';
|
|
$kontoMapping['Z'] = '276016';
|
|
$kontoMapping['Ö'] = $kontoMapping['O'];
|
|
$kontoMapping['Ü'] = $kontoMapping['U'];
|
|
$kontoMapping['Ä'] = $kontoMapping['A'];
|
|
|
|
|
|
|
|
$csv = "satzart;konto;gkonto;belegnr;belegdatum;buchsymbol;buchcode;prozent;steuercode;betrag;steuer;text\n";
|
|
$orders = Order::where('created_at', 'like', '2025-08-%')->get();
|
|
foreach($orders as $order){
|
|
$paymentmethod = $order->singleseatusers()->first()->paymentmethod->name;
|
|
if($paymentmethod !== "Überweisung"){
|
|
continue;
|
|
}
|
|
$satzart = "0";
|
|
$lastname = $order->user()->withTrashed()->first()->lastname;
|
|
if(starts_with($lastname, "S")){
|
|
if(starts_with($lastname, "Sch")){
|
|
$konto="269026";
|
|
}elseif(starts_with($lastname, "St")){
|
|
$konto="270019";
|
|
}else{
|
|
$konto="268022";
|
|
}
|
|
}else{
|
|
$konto = $kontoMapping[strtoupper(mb_substr($lastname, 0, 1))];
|
|
}
|
|
$event_month = date('m', strtotime($order->singleseatusers()->first()->singleSeat()->first()->seat()->first()->date));
|
|
$gkonto="39$event_month";
|
|
$event_id=$order->singleseatusers()->first()->singleSeat()->first()->seat()->first()->event()->first()->id;
|
|
if($event_id == 1608){
|
|
//fast christmas
|
|
$gkonto = 3474;
|
|
}elseif($event_id == 1615){
|
|
$gkonto = 3476;
|
|
}elseif($event_id == 1626){
|
|
$gkonto =3477;
|
|
}
|
|
$belegnr = $order->id;
|
|
$belegdatum = $order->created_at->format('d.m.Y'); //TODO: check if Y-m-D is ok
|
|
$buchsymbol = "AR";
|
|
$buchcode = "1";
|
|
$prozent="10";
|
|
$steuercode="1";
|
|
$price = $order->getPrice();
|
|
/*if($order->porto){
|
|
$price -= 100;
|
|
}*/
|
|
$betrag=number_format($price/100, 2); //TODO: Check if "." is ok as delim
|
|
$steuer = number_format(round($price-$price/1.1)/100, 2);
|
|
$firstname = $order->user()->withTrashed()->first()->firstname;
|
|
$title = $order->singleseatusers()->first()->singleSeat()->first()->seat()->first()->event()->first()->title;
|
|
$text = "$lastname $firstname $title";
|
|
|
|
if($order->getPrice() > 0){
|
|
$csv.="$satzart;$konto;$gkonto;$belegnr;$belegdatum;$buchsymbol;$buchcode;$prozent;$steuercode;$betrag;$steuer;$text\n";
|
|
|
|
if($order->porto){
|
|
$betrag = number_format(1,2);
|
|
$steuer = number_format(1-1/1.1,2);
|
|
$csv.="$satzart;$konto;4031;$belegnr;$belegdatum;$buchsymbol;$buchcode;$prozent;$steuercode;$betrag;$steuer;$text\n";
|
|
}
|
|
}
|
|
//dd($order);
|
|
}
|
|
echo nl2br($csv);
|
|
}
|
|
|
|
public function stats(){
|
|
$seats = Seat::all();
|
|
$totalPrice = 0;
|
|
$counter = 0;
|
|
foreach($seats as $seat){
|
|
foreach($seat->singleSeats()->where('booked', '1')->get() as $singleSeat){
|
|
$ssu = SingleSeatsUser::where('single_seat_id', $singleSeat->id)->first();
|
|
if($ssu != null) {
|
|
$priceTicket = $this->calcPriceTicket($ssu);
|
|
$totalPrice += $priceTicket;
|
|
if ($priceTicket > 0)
|
|
$counter++;
|
|
}
|
|
}
|
|
}
|
|
$avg = $totalPrice/$counter;
|
|
echo "Erlös aus Karten seit 16.4.: € ".($totalPrice/100)." (".$counter." x durchschn. ".$avg.")";
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$order = Order::find($id);
|
|
$concessions = Concession::where('deleted', 0)->get();
|
|
$paymentmethods = Paymentmethod::where('name', '<>', 'Kulturkarte')->get();
|
|
|
|
return view('admin.reservations.edit',compact('order', 'concessions', 'paymentmethods'));
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$order = Order::find($id);
|
|
$ssus = $order->singleseatusers()->get();
|
|
foreach($ssus as $ssu){
|
|
$singleSeat = $ssu->singleSeat()->first();
|
|
$singleSeat->booked = 0;
|
|
$singleSeat->save();
|
|
$ssu->delete();
|
|
}
|
|
$order->delete();
|
|
return redirect('admin/reservations');
|
|
}
|
|
|
|
public function update($id){
|
|
$orderUpdate = Request::all();
|
|
|
|
|
|
$order=Order::find($id);
|
|
$order->paid = Request::has('paid');
|
|
$order->porto = Request::has('porto');
|
|
$order->notes = $orderUpdate["notes"];
|
|
$order->paiddate = $orderUpdate["date"];
|
|
$order->save();
|
|
|
|
$concessions = $orderUpdate["concession"];
|
|
$counter = 0;
|
|
foreach($order->singleseatusers()->get() as $ssu){
|
|
$ssu->event_concessions_id = $concessions[$counter];
|
|
$counter++;
|
|
$ssu->save();
|
|
}
|
|
|
|
// if($orderUpdate['paymentmethod'] == 5 && $order->paymentmethod != 5){ //kulturkarte
|
|
// $event = $order->singleseatusers()->first()->singleSeat()->first()->seat()->first()->event()->first();
|
|
// if(!$this->verifyCultureCard($event, $order)){
|
|
// $eventId = $event->id;
|
|
//// return redirect('/book/'.$eventId.'-0');
|
|
// return redirect('/home');
|
|
// }
|
|
// foreach($order->singleseatusers()->get() as $ssu){
|
|
// $culturecardTicket = new CulturecardTicket();
|
|
// $culturecardTicket->culturecard_id = $order->user()->first()->culturecard_user()->orderBy('created_at', 'desc')->first()->culturecard()->first()->id;
|
|
// $culturecardTicket->singleseat_id = $ssu->singleSeat()->first()->id;
|
|
// $culturecardTicket->save();
|
|
// }
|
|
// }
|
|
|
|
foreach($order->singleseatusers()->get() as $ssu){
|
|
$culturcard = Concession::where('name', 'Kulturkarte')->first();
|
|
if($ssu->paymentmethod_id != $culturcard->id){
|
|
$ssu->paymentmethod_id = $orderUpdate['paymentmethod'];
|
|
$ssu->save();
|
|
}
|
|
}
|
|
|
|
|
|
\Session::flash('flash_message', 'Die Reservierung wurde erfolgreich bearbeitet!');
|
|
return redirect('admin/reservations?search='.$order->id);
|
|
}
|
|
|
|
private function verifyCultureCard($event, $order){
|
|
$user = $order->user()->first();
|
|
|
|
$amountTicketsToBuy = count($order->singleseatusers()->get());
|
|
if($amountTicketsToBuy > 2){
|
|
\Session::flash('flash_error','Mit der Kulturkarte können maximal 2 Tickets der selben Veranstaltung gekauft werden! Bitte wähle weniger Sitzplätze aus');
|
|
return false;
|
|
}
|
|
|
|
//check whether user has a culture card
|
|
if(count($user->culturecard_user()->get()) <= 0){
|
|
\Session::flash('flash_error', 'Als Bezahlart wurde Kulturkarte gewählt, Sie besitzen jedoch keine. Bitte kaufen Sie eine neue Kulturkarte, oder wähle eine andere Bezahlmöglichkeit.');
|
|
return false;
|
|
}
|
|
|
|
//culture card still valid
|
|
$activeCultureCard = $user->culturecard_user()->orderBy('created_at', 'desc')->first()->culturecard()->first();
|
|
$now = new DateTime();
|
|
$cardValidUntil = DateTime::createFromFormat('Y-m-d', $activeCultureCard->renewal_date);
|
|
if ($now > $cardValidUntil){
|
|
\Session::flash('flash_error', 'Ihre Kulturkarte ist bereits abgelaufen. Bitte kaufen Sie eine neue Kulturkarte, oder wähle eine andere Bezahlmöglichkeit.');
|
|
return false;
|
|
}
|
|
|
|
|
|
//check whether user has enough free cards left
|
|
$amountAlreadyBought = $activeCultureCard->get_amount_bought_tickets();
|
|
if($amountAlreadyBought + $amountTicketsToBuy > 5){
|
|
\Session::flash('flash_error', 'Sie haben bereits '.$amountAlreadyBought.' Ticket(s) mit Ihrer Kulturkarte gekauft. Sie können noch maximal '.(5-$amountAlreadyBought).' Ticket(s) mit Ihrer Kulturkarte kaufen, haben jedoch '.$amountTicketsToBuy.' ausgewählt. Bitte wählen Sie weniger Plätze aus und probieren Sie es erneut.');
|
|
return false;
|
|
}
|
|
|
|
//check whether user has bought tickets of this event with culturecard already
|
|
$ticketsThisEventBoughtWithCultureCard = 0;
|
|
foreach($activeCultureCard->culturecard_ticket()->get() as $ticket){
|
|
if($event["event_id"] == $ticket->singleSeat()->first()->seat()->first()->event_id){
|
|
$ticketsThisEventBoughtWithCultureCard++;
|
|
}
|
|
}
|
|
if($ticketsThisEventBoughtWithCultureCard + $amountTicketsToBuy > 2){
|
|
\Session::flash('flash_error', 'Sie haben bereits '.$ticketsThisEventBoughtWithCultureCard.' Karte(n) für diese Veranstaltung mit Ihrer Kulturkarte gekauft. Insgesamt können maximal 2 Karten pro Veranstaltung mit der Kulturkarte bezahlt werden.');
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|