fitness-web/common/manager/EventRegistrationManager.php
2021-09-27 20:43:58 +02:00

97 lines
3.0 KiB
PHP

<?php
namespace common\manager;
use common\models\Card;
use common\models\Event;
use common\models\EventRegistration;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.12.17.
* Time: 6:12
*/
class EventRegistrationManager extends \yii\base\Object
{
const CARD_NOT_FOUND = 1;
const CUSTOMER_NOT_FOUND = 2;
const TICKET_NOT_FOUND = 3;
const NO_FREE_SEATS = 4;
const EVENT_TYPE_NOT_FOUND = 5;
const TICKET_INSUFFICIENT = 6;
public static $STATES = [
self::CARD_NOT_FOUND => "CARD_NOT_FOUND",
self::CUSTOMER_NOT_FOUND => "CUSTOMER_NOT_FOUND",
self::TICKET_NOT_FOUND => "TICKET_NOT_FOUND",
self::NO_FREE_SEATS => "NO_FREE_SEATS",
self::EVENT_TYPE_NOT_FOUND => "EVENT_TYPE_NOT_FOUND",
self::TICKET_INSUFFICIENT => "TICKET_INSUFFICIENT",
];
/**
* @param \common\models\CardEventRegistrationForm $cardEventForm
* @throws NotFoundHttpException
* @throws BadRequestHttpException
* @throws ServerErrorHttpException
*/
public function registerCard($cardEventForm){
if ( $cardEventForm->validate() ){
/** @var \common\models\Card $card */
$card = Card::readCard($cardEventForm->card_number,false);
if ( !isset($card )){
throw new NotFoundHttpException("Card not found: " . $cardEventForm->card_number,self::CARD_NOT_FOUND);
}
if ( $card->isFree() ){
throw new NotFoundHttpException("Customer not found", self::CUSTOMER_NOT_FOUND);
}
$activeTickets = $card->getActiveTickets();
if ( sizeof($activeTickets) == 0 ){
throw new NotFoundHttpException("Ticket not found", self::TICKET_NOT_FOUND);
}
/** @var \common\models\Event $event */
$event = Event::find()->andWhere(['id' => $cardEventForm->event_id])->one();
if ( !isset($event)){
throw new NotFoundHttpException("Event not found: " . $cardEventForm->event_id);
}
if ( !$event->hasFreeSeats() ){
throw new BadRequestHttpException("No free seats", self::NO_FREE_SEATS);
}
$eventType = $event->eventType;
if ( !isset($eventType) ){
throw new ServerErrorHttpException("Event type not found", self::EVENT_TYPE_NOT_FOUND);
}
$selectedTicket = $eventType->findTicketAllowingEventType($activeTickets);
if ( !isset($selectedTicket) ){
throw new NotFoundHttpException("Ticket not found", self::TICKET_INSUFFICIENT);
}
$registration = new EventRegistration();
$registration->id_event = $event->id;
$registration->id_card = $card->id_card;
$registration->id_ticket = $selectedTicket->id_ticket;
$registration->save(false);
$cardEventForm->registration = $registration;
}
}
}