126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
namespace App\Traits;
|
|
|
|
use App\CulturecardTicket;
|
|
use App\Order;
|
|
use App\Paymentmethod;
|
|
use App\SingleSeat;
|
|
use App\Seat;
|
|
use DateTime;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\User;
|
|
|
|
trait ReservationTrait
|
|
{
|
|
use CulturecardTrait;
|
|
protected function getPaymentMethodDependingOnTime($eventDate){
|
|
if ($this->lessThanThreeDays($eventDate)) {
|
|
$paymentMethod = Paymentmethod::find(2); //bar
|
|
} else {
|
|
$paymentMethod = Paymentmethod::find(1); //Überweisung
|
|
}
|
|
return $paymentMethod;
|
|
}
|
|
|
|
protected function setPortoCostsDependingOnTime($order, $eventDate){
|
|
if (($eventDate - time()) / (60 * 60 * 24) <= 3) {
|
|
$order->porto = 0;
|
|
} else {
|
|
$order->porto = 1;
|
|
}
|
|
$order->save();
|
|
}
|
|
|
|
private function lessThanThreeDays($eventDate){
|
|
return ($eventDate - time()) / (60 * 60 * 24) <= 3;
|
|
}
|
|
|
|
protected function createOrder($seatid, $userid){
|
|
$order = new Order();
|
|
$order->user_id = $userid;
|
|
|
|
$boughtSeat = Seat::find($seatid);
|
|
$eventDate = strtotime($boughtSeat->date);
|
|
$this->setPortoCostsDependingOnTime($order, $eventDate);
|
|
|
|
$order->save();
|
|
return $order;
|
|
}
|
|
|
|
protected function getPaymentMethodFromRequest($event){
|
|
if(!empty($event["payWithCultureCard"])>0){
|
|
return Paymentmethod::find(5); //Kulturkarte
|
|
}
|
|
$boughtSeat = Seat::find($event["seat_id"]);
|
|
$eventDate = strtotime($boughtSeat->date);
|
|
$paymentMethod = $this->getPaymentMethodDependingOnTime($eventDate);
|
|
return $paymentMethod;
|
|
}
|
|
|
|
protected function getPaymentMethodFromRequestNoCulturecard($event){
|
|
$boughtSeat = Seat::find($event["seat_id"]);
|
|
$eventDate = strtotime($boughtSeat->date);
|
|
$paymentMethod = $this->getPaymentMethodDependingOnTime($eventDate);
|
|
return $paymentMethod;
|
|
}
|
|
|
|
private function sendReservationSuccMails($order, $userid){
|
|
if(Auth::user()->admin != 1 ){
|
|
Mail::to(\App\User::find($userid))
|
|
->send(new \App\Mail\ReservationSuccessfulUser($order));
|
|
Mail::to("video.bruckmuehle@gmail.com")
|
|
->send(new \App\Mail\ReservationSuccessfulUser($order));
|
|
Mail::to("richard@maynau.com")
|
|
->send(new \App\Mail\ReservationSuccessfulUser($order));
|
|
|
|
}
|
|
}
|
|
|
|
protected function handleSsuCultureCard($paymentMethod, $ssu, $order, $userid, $singleSeatId){
|
|
if ($paymentMethod->name === "Kulturkarte") {
|
|
$culturecardTicket = new CulturecardTicket();
|
|
$culturecardTicket->culturecard_id = $this->getActiveCultureCardId($userid);
|
|
$culturecardTicket->singleseat_id = $singleSeatId;
|
|
$culturecardTicket->save();
|
|
|
|
$ssu->paymentmethod_id = $paymentMethod->id;
|
|
$ssu->save();
|
|
|
|
$order->porto = 0;
|
|
$order->save();
|
|
}
|
|
}
|
|
|
|
private function verifySingleCultureCard($activeCultureCard)
|
|
{
|
|
$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 >= 5) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected 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;
|
|
}
|
|
|
|
}
|