118 lines
3.8 KiB
PHP
118 lines
3.8 KiB
PHP
<?php
|
|
namespace App\Traits;
|
|
|
|
use App\SingleSeat;
|
|
use DateTime;
|
|
use Request;
|
|
|
|
trait CulturecardTrait
|
|
{
|
|
protected function cultureCardConcessionUsed($card){
|
|
return $card->conc_ticket_event_id != null;
|
|
}
|
|
|
|
protected function cultureCardConcessionAvailableOnACard($user){
|
|
$cards = $user->culturecard_user()->get();
|
|
|
|
foreach($cards as $card){
|
|
$card = $card->culturecard()->first();
|
|
if($this->cultureCardExpired($card) && !$this->cultureCardConcessionUsed($card))
|
|
return true;
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
public function verifyCultureCard($event, $user, $amountTicketsToBuy)
|
|
{
|
|
if ($amountTicketsToBuy > 2) return false;
|
|
|
|
|
|
//check whether user has a culture card
|
|
if (count($user->culturecard_user()->get()) <= 0) {
|
|
return false;
|
|
}
|
|
|
|
//culture card still valid
|
|
$activeCultureCard = $user->culturecard_user()->get();
|
|
|
|
$verified = false;
|
|
$amountPossibleToBuy = 0;
|
|
foreach($activeCultureCard as $singleCultureCard){
|
|
$amountPossibleToBuy += $this->getAmountOfPossibleTickets($singleCultureCard->culturecard, $event);
|
|
}
|
|
|
|
return $amountPossibleToBuy >= $amountTicketsToBuy;
|
|
}
|
|
|
|
protected function getCultureCardWithConsessionAvailable($user){
|
|
$cards = $user->culturecard_user()->get();
|
|
|
|
foreach($cards as $card){
|
|
$card = $card->culturecard()->first();
|
|
if($this->cultureCardExpired($card) && !$this->cultureCardConcessionUsed($card))
|
|
return $card;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected function getAmountOfPossibleTickets($activeCultureCard, $event)
|
|
{
|
|
if(!$this->verifySingleCultureCard($activeCultureCard, 1, $event)) return 0;
|
|
|
|
//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++;
|
|
}
|
|
}
|
|
|
|
$max = 2-$ticketsThisEventBoughtWithCultureCard;
|
|
|
|
|
|
|
|
//check whether user has enough free cards left
|
|
$amountAlreadyBought = $activeCultureCard->get_amount_bought_tickets();
|
|
return min($max, 5-$amountAlreadyBought);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
private function cultureCardExpired($card){
|
|
$now = new DateTime();
|
|
$cardValidUntil = DateTime::createFromFormat('Y-m-d', $card->renewal_date);
|
|
return $now <= $cardValidUntil;
|
|
}
|
|
}
|