Improve Event management
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace common\manager;
|
||||
|
||||
use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use common\models\Event;
|
||||
@@ -16,7 +18,6 @@ use yii\web\ServerErrorHttpException;
|
||||
* Date: 2018.12.17.
|
||||
* Time: 6:12
|
||||
*/
|
||||
|
||||
class EventRegistrationManager extends \yii\base\Object
|
||||
{
|
||||
const CARD_NOT_FOUND = 1;
|
||||
@@ -27,9 +28,10 @@ class EventRegistrationManager extends \yii\base\Object
|
||||
const TICKET_INSUFFICIENT = 6;
|
||||
const UNKNOWN_ERROR = 7;
|
||||
const MAX_SEAT_COUNT_EXCEEDED = 8;
|
||||
const EVENT_UNAVAILABLE = 9;
|
||||
|
||||
public static $STATES = [
|
||||
self::CARD_NOT_FOUND => "CARD_NOT_FOUND",
|
||||
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",
|
||||
@@ -37,87 +39,99 @@ class EventRegistrationManager extends \yii\base\Object
|
||||
self::TICKET_INSUFFICIENT => "TICKET_INSUFFICIENT",
|
||||
self::UNKNOWN_ERROR => "UNKNOWN_ERROR",
|
||||
self::MAX_SEAT_COUNT_EXCEEDED => "MAX_SEAT_COUNT_EXCEEDED",
|
||||
self::EVENT_UNAVAILABLE => "EVENT_UNAVAILABLE",
|
||||
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* @param \common\models\CardEventRegistrationForm $cardEventForm
|
||||
* @throws NotFoundHttpException
|
||||
* @throws BadRequestHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
* @throws \yii\web\HttpException
|
||||
* @throws \yii\db\Exception
|
||||
*/
|
||||
public function registerCard($cardEventForm){
|
||||
public function registerCard($cardEventForm)
|
||||
{
|
||||
$db = \Yii::$app->db;
|
||||
$tx = $db->beginTransaction();
|
||||
try {
|
||||
if ($cardEventForm->validate()) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/** @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 ( isset($event->deleted_at)){
|
||||
throw new BadRequestHttpException("Event deleted", self::EVENT_UNAVAILABLE);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if ($selectedTicket->hasOpenReservationCount()) {
|
||||
$selectedTicket->consumeReservationCount(1);
|
||||
}
|
||||
$selectedTicket->save();
|
||||
|
||||
$registration = new EventRegistration();
|
||||
$registration->id_event = $event->id;
|
||||
$registration->id_card = $card->id_card;
|
||||
$registration->id_ticket = $selectedTicket->id_ticket;
|
||||
try {
|
||||
$registration->save(false);
|
||||
} catch (\Throwable $exception) {
|
||||
throw new ServerErrorHttpException("Failed to save", self::UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
$cardEventForm->registration = $registration;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if ( $selectedTicket->hasOpenReservationCount() ){
|
||||
$selectedTicket->consumeReservationCount(1);
|
||||
}
|
||||
$selectedTicket->save();
|
||||
|
||||
$registration = new EventRegistration();
|
||||
$registration->id_event = $event->id;
|
||||
$registration->id_card = $card->id_card;
|
||||
$registration->id_ticket = $selectedTicket->id_ticket;
|
||||
try{
|
||||
$registration->save(false);
|
||||
}catch (\Throwable $exception){
|
||||
throw new ServerErrorHttpException("Failed to save", self::UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
$cardEventForm->registration = $registration;
|
||||
$tx->commit();
|
||||
} catch (\Exception $exception) {
|
||||
$tx->rollBack();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function createFindRegistrationsQuery($idEvent){
|
||||
public function createFindRegistrationsQuery($idEvent)
|
||||
{
|
||||
$query = new Query();
|
||||
$query->select([
|
||||
"event_registration.id as event_registration_id",
|
||||
"event_registration.created_at as event_registration_created_at",
|
||||
"event_registration.canceled_at as event_registration_canceled_at",
|
||||
"event_registration.deleted_at as event_registration_deleted_at",
|
||||
"card.id_card as card_id_card",
|
||||
"card.number as card_number",
|
||||
"card.number as card_number",
|
||||
"customer.id_customer as customer_id_customer",
|
||||
"customer.name as customer_name",
|
||||
"customer.email as customer_email",
|
||||
]);
|
||||
@@ -125,7 +139,7 @@ class EventRegistrationManager extends \yii\base\Object
|
||||
$query->innerJoin(Event::tableName(), "event_registration.id_event = event.id");
|
||||
$query->innerJoin(Card::tableName(), "event_registration.id_card = card.id_card");
|
||||
$query->innerJoin(Customer::tableName(), "customer.id_customer_card = card.id_card");
|
||||
$query->andWhere(["event_registration.id_event" => $idEvent ]);
|
||||
$query->andWhere(["event_registration.id_event" => $idEvent]);
|
||||
|
||||
return $query;
|
||||
}
|
||||
@@ -136,9 +150,10 @@ class EventRegistrationManager extends \yii\base\Object
|
||||
* @return array|EventRegistration|\yii\db\ActiveRecord|null
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function loadRegistration($idRegistration) {
|
||||
public function loadRegistration($idRegistration)
|
||||
{
|
||||
|
||||
$registration = EventRegistration::find()->andWhere(['id' => $idRegistration ])->one();
|
||||
$registration = EventRegistration::find()->andWhere(['id' => $idRegistration])->one();
|
||||
if ($registration == null) {
|
||||
throw new NotFoundHttpException('The requested registration does not exist.');
|
||||
}
|
||||
@@ -149,21 +164,77 @@ class EventRegistrationManager extends \yii\base\Object
|
||||
* @param \common\models\EventRegistration $registration
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function cancelRegistration($registration){
|
||||
if ( isset($registration->canceled_at)){
|
||||
public function cancelRegistration($registration)
|
||||
{
|
||||
if (isset($registration->canceled_at)) {
|
||||
throw new ServerErrorHttpException('The registration is already canceled');
|
||||
}
|
||||
|
||||
$ticket = Ticket::findOne(['id' => $registration->id_ticket ]);
|
||||
if (isset($registration->deleted_at)) {
|
||||
throw new ServerErrorHttpException('The reservation is already deleted');
|
||||
}
|
||||
|
||||
$ticket->restoreReservationCount(1);
|
||||
$ticket->save(false);
|
||||
|
||||
$registration->canceled_at = date('Y-m-d H:i:s' );
|
||||
$registration->canceled_at = date('Y-m-d H:i:s');
|
||||
$registration->save(false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \common\models\EventRegistration $registration
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function deleteRegistration($registration)
|
||||
{
|
||||
if (isset($registration->deleted_at)) {
|
||||
throw new ServerErrorHttpException('The reservation is already deleted');
|
||||
}
|
||||
|
||||
$ticket = Ticket::findOne(['id_ticket' => $registration->id_ticket]);
|
||||
|
||||
$ticket->restoreReservationCount(1);
|
||||
$ticket->save(false);
|
||||
|
||||
$registration->deleted_at = date('Y-m-d H:i:s');
|
||||
$registration->save(false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an event
|
||||
* @param \common\models\Event $event
|
||||
* @throws \yii\db\Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function deleteEvent($event)
|
||||
{
|
||||
$db = \Yii::$app->db;
|
||||
$tx = $db->beginTransaction();
|
||||
try {
|
||||
|
||||
$registrations = $event->getEventRegistrations()->all();
|
||||
// ////////////////////////////////
|
||||
// if even has no registrations
|
||||
// we can simply delete it from db
|
||||
// ////////////////////////////////
|
||||
if (count($registrations) == 0) {
|
||||
$event->delete();
|
||||
} else {
|
||||
$event->deleted_at = date('Y-m-d H:i:s');
|
||||
$event->save(false);
|
||||
foreach ($registrations as $registration) {
|
||||
if (!isset($registration->deleted_at)) {
|
||||
/** @var \common\models\EventRegistration $registration */
|
||||
$this->deleteRegistration($registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
$tx->commit();
|
||||
} catch (\Exception $e) {
|
||||
$tx->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -39,5 +39,12 @@ return [
|
||||
'Canceled At' => 'Sztornó dátum, idő',
|
||||
'Register' => 'Regisztráció',
|
||||
'Event Registration Created At' => 'Regisztráció dátum, idő',
|
||||
|
||||
'Event Registration Count' => 'Foglalások száma',
|
||||
'Open Seat Count' => 'Szabad helyek száma',
|
||||
'Cancel' => 'Lemond',
|
||||
'Deleted At' => 'Törlés dátum, idő',
|
||||
'Are you sure you want to cancel this item? Usage count won\'t be restored!' => 'Biztos benne, hogy lemondja ezt az foglalást? A foglalás nem kerül vissza a bérletre!',
|
||||
'Are you sure you want to delete this item? Usage count will be restored!' => 'Biztos benne, hogy törli ezt az foglalást? A foglalás vissza fog kerülni a bérletre!',
|
||||
'Reservation' => 'Foglalás',
|
||||
'Event Details' => "Esemény részletei"
|
||||
];
|
||||
@@ -18,6 +18,7 @@ use yii\helpers\ArrayHelper;
|
||||
* @property integer $seat_count
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property string $deleted_at
|
||||
* @property \common\models\EventType $eventType
|
||||
* @property \common\models\Trainer $trainer
|
||||
* @property \common\models\Room $room
|
||||
@@ -67,6 +68,7 @@ class Event extends \yii\db\ActiveRecord
|
||||
'id_event_type' => Yii::t('event', 'Id Event Type'),
|
||||
'created_at' => Yii::t('event', 'Created At'),
|
||||
'updated_at' => Yii::t('event', 'Updated At'),
|
||||
'deleted_at' => Yii::t('event', 'Deleted At'),
|
||||
'trainerName' => Yii::t('event', 'Trainer Name'),
|
||||
'roomName' => Yii::t('event', 'Room Name'),
|
||||
'eventTypeName' => Yii::t('event', 'Típus'),
|
||||
@@ -77,6 +79,8 @@ class Event extends \yii\db\ActiveRecord
|
||||
'endDateString' => Yii::t('event', 'End'),
|
||||
'status' => Yii::t('event', 'Status'),
|
||||
'seat_count' => Yii::t('event', 'Seat Count'),
|
||||
'eventRegistrationCount' => Yii::t('event', 'Event Registration Count'),
|
||||
'openSeatCount' => Yii::t('event', 'Open Seat Count'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -132,7 +136,16 @@ class Event extends \yii\db\ActiveRecord
|
||||
* @return \common\models\EventRegistration[]|\yii\db\ActiveQuery
|
||||
*/
|
||||
public function getEventRegistrationCount(){
|
||||
return sizeof($this->eventRegistrations);
|
||||
return sizeof($this->getEventRegistrations()
|
||||
->andWhere( [
|
||||
'canceled_at' => null,
|
||||
'deleted_at' => null,
|
||||
])
|
||||
->all());
|
||||
}
|
||||
|
||||
public function getOpenSeatCount(){
|
||||
return $this->seat_count - $this->getEventRegistrationCount();
|
||||
}
|
||||
|
||||
public function hasFreeSeats(){
|
||||
@@ -146,4 +159,21 @@ class Event extends \yii\db\ActiveRecord
|
||||
return $registrationCount < $seatCount ;
|
||||
}
|
||||
|
||||
public function canReserve(){
|
||||
if ( isset($this->deleted_at)){
|
||||
return false;
|
||||
}
|
||||
if ( !$this->hasFreeSeats()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canDelete(){
|
||||
if ( isset($this->deleted_at)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ use yii\helpers\ArrayHelper;
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property string $canceled_at
|
||||
* @property string $deleted_at
|
||||
*/
|
||||
class EventRegistration extends \yii\db\ActiveRecord
|
||||
{
|
||||
@@ -50,6 +51,7 @@ class EventRegistration extends \yii\db\ActiveRecord
|
||||
'created_at' => Yii::t('event-registration', 'Created At'),
|
||||
'updated_at' => Yii::t('event-registration', 'Updated At'),
|
||||
'canceled_at' => Yii::t('event-registration', 'Canceled At'),
|
||||
'deleted_at' => Yii::t('event-registration', 'Deleted At'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user