diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index 832e656..ad5ec24 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -2,10 +2,7 @@ namespace backend\components; use Yii; -use common\models\Order; -use yii\helpers\Html; use common\components\RoleDefinition; -use common\components\Helper; class AdminMenuStructure{ @@ -23,12 +20,12 @@ class AdminMenuStructure{ } return $result; } - - - - - - protected function addUserMainMenu(){ + + + /** + * @throws \yii\base\InvalidConfigException + */ + protected function addUserMainMenu(){ $userMainMenu = null; $items = []; @@ -173,7 +170,6 @@ class AdminMenuStructure{ $items[] = ['label' => 'Termek', 'url' => ['/room' ] ]; $items[] = ['label' => 'Esemény típusok', 'url' => ['/event-type' ] ]; $items[] = ['label' => 'Események', 'url' => ['/event' ] ]; - $items[] = ['label' => 'Esemény regisztrációk', 'url' => ['/event-registration' ] ]; $this->menuItems[] = ['label' => 'Csoportos edzés', 'url' => $this->emptyUrl, 'items' => $items ]; @@ -199,7 +195,8 @@ class AdminMenuStructure{ if (Yii::$app->user->isGuest) { $mainMenuItem= ['label' => Yii::t('common/site','Login'), 'url' => ['/site/login']]; } else { - $mainMenuItem= [ + /** @noinspection PhpUndefinedFieldInspection */ + $mainMenuItem= [ 'label' => Yii::t('common/site','Logout') . '(' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post'] @@ -207,9 +204,13 @@ class AdminMenuStructure{ } $this->menuItems[] = $mainMenuItem; } - - - public function run(){ + + + /** + * @return array + * @throws \yii\base\InvalidConfigException + */ + public function run(){ $this->addUserMainMenu(); // $this->addLoginMainMenu(); return $this->menuItems; diff --git a/backend/controllers/EventController.php b/backend/controllers/EventController.php index b44505e..3945d69 100644 --- a/backend/controllers/EventController.php +++ b/backend/controllers/EventController.php @@ -7,6 +7,7 @@ use common\models\CardEventRegistrationForm; use Yii; use common\models\Event; use backend\models\EventSearch; +use yii\data\ActiveDataProvider; use yii\web\Controller; use yii\web\HttpException; use yii\web\NotFoundHttpException; @@ -48,11 +49,20 @@ class EventController extends Controller * Displays a single Event model. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionView($id) { + $eventRegistrationManager = new EventRegistrationManager(); + + + $dataProvider = new ActiveDataProvider([ + 'query' => $eventRegistrationManager->createFindRegistrationsQuery($id), + ] + ); return $this->render('view', [ 'model' => $this->findModel($id), + 'dataProvider' => $dataProvider ]); } @@ -79,6 +89,7 @@ class EventController extends Controller * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionUpdate($id) { @@ -109,28 +120,65 @@ class EventController extends Controller } - public function actionRegisterCard($id){ + /** + * @param $id + * @return \yii\web\Response + * @throws \yii\db\Exception + * @throws \Exception + */ + public function actionCancelRegistration($id) + { + $eventRegistrationManager = new EventRegistrationManager(); + $db = \Yii::$app->db; + $tx = $db->beginTransaction(); + try{ + $registration = $eventRegistrationManager->loadRegistration($id); + $eventRegistrationManager->cancelRegistration($registration); + $tx->commit(); + return $this->redirect(['view', 'id' => $registration->id_event]); + }catch (\Exception $ex){ + $tx->rollBack(); + throw $ex; + } + } + /** + * @param $id + * @return string|\yii\web\Response + * @throws NotFoundHttpException + */ + public function actionRegisterCard($id) + { + + $event = $this->findModel($id); + $model = new CardEventRegistrationForm(); $model->event_id = $id; - if ($model->load(Yii::$app->request->post()) && $model->validate() ) { - $manager = new EventRegistrationManager(); - try { - $manager->registerCard($model); - } catch (HttpException $e) { - if ( array_key_exists($e->getCode(),EventRegistrationManager::$STATES)) { - $model->addError("id_event", Yii::t('event-registration', EventRegistrationManager::$STATES[$e->getCode()])); - } else { - $model->addError("id_event", Yii::t('event-registration', "Unknown Error")); + if ($model->load(Yii::$app->request->post())) { + if ($model->validate()) { + $manager = new EventRegistrationManager(); + try { + $manager->registerCard($model); + } catch (HttpException $e) { + if (array_key_exists($e->getCode(), EventRegistrationManager::$STATES)) { + $model->addError("card_number", Yii::t('event-registration', EventRegistrationManager::$STATES[$e->getCode()])); + } else { + $model->addError("card_number", Yii::t('event-registration', "Unknown Error")); + } } } + if ($model->hasErrors()) { + return $this->render('register_card', [ + 'model' => $model, + 'event' => $event, + ]); + } else { + return $this->redirect(['view', 'id' => $id]); + } } - if ( $model->hasErrors() ){ - return $this->redirect(['view', 'id' => $model->registration->id]); - } else { - return $this->render('register_card', [ - 'model' => $model, - ]); - } + return $this->render('register_card', [ + 'model' => $model, + 'event' => $event, + ]); } /** diff --git a/backend/controllers/TicketTypeController.php b/backend/controllers/TicketTypeController.php index a1be1ab..99dbbc7 100644 --- a/backend/controllers/TicketTypeController.php +++ b/backend/controllers/TicketTypeController.php @@ -5,15 +5,13 @@ namespace backend\controllers; use Yii; use common\models\TicketType; use backend\models\TicketTypeSearch; -use yii\web\Controller; use yii\web\NotFoundHttpException; -use yii\filters\VerbFilter; use common\models\Account; /** * TicketTypeController implements the CRUD actions for TicketType model. */ -class TicketTypeController extends \backend\controllers\BackendController +class TicketTypeController extends BackendController { @@ -60,6 +58,7 @@ class TicketTypeController extends \backend\controllers\BackendController * Displays a single TicketType model. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionView($id) { @@ -82,6 +81,7 @@ class TicketTypeController extends \backend\controllers\BackendController $model->time_unit_type = TicketType::TIME_UNIT_MONTH; $model->time_unit_count = 1; $model->max_usage_count = 0; + $model->max_reservation_count = 0; $accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all(); if ($model->load(Yii::$app->request->post()) && $model->save()) { @@ -99,6 +99,7 @@ class TicketTypeController extends \backend\controllers\BackendController * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionUpdate($id) { diff --git a/backend/models/TransferSearch.php b/backend/models/TransferSearch.php index fcf1fd2..5967a85 100644 --- a/backend/models/TransferSearch.php +++ b/backend/models/TransferSearch.php @@ -8,7 +8,6 @@ use yii\data\ActiveDataProvider; use common\models\Transfer; use yii\db\Expression; use yii\db\Query; -use yii\helpers\ArrayHelper; use common\models\Account; use common\components\Helper; use common\components\RoleDefinition; @@ -167,8 +166,6 @@ class TransferSearch extends Transfer } } - ['like', 'name', ['test', 'sample']] - $query->andFilterWhere([ 'transfer.id_account' => $this->id_account, 'transfer.status' => $this->status, @@ -280,11 +277,12 @@ class TransferSearch extends Transfer } + /** + * @throws \yii\db\Exception + */ public function totalsTransfers() { - $accounts = Account::read(); - $accountMap = ArrayHelper::map($accounts, 'id_account', 'name'); $idUser = $this->id_user; /**mk totals need date time format*/ @@ -293,8 +291,7 @@ class TransferSearch extends Transfer $start .= ' 00:00'; } - - $this->totals = Transfer::mkTotals($start, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap); + $this->totals = Transfer::mkTotals($start, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts); } diff --git a/backend/views/event/_form.php b/backend/views/event/_form.php index cf5e01c..7defefb 100644 --- a/backend/views/event/_form.php +++ b/backend/views/event/_form.php @@ -9,41 +9,46 @@ use yii\widgets\ActiveForm; ?>
+
+
- + - field($model, 'startDateString')->widget(\kartik\widgets\DateTimePicker::classname(), [ - 'pluginOptions' => [ - 'autoclose'=>true, - 'format' => 'yyyy.mm.dd hh:ii' - ], - 'options' => [ - 'autocomplete' => 'off' - ] - ]); - ?> + field($model, 'startDateString')->widget(\kartik\widgets\DateTimePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose' => true, + 'format' => 'yyyy.mm.dd hh:ii' + ], + 'options' => [ + 'autocomplete' => 'off' + ] + ]); + ?> - field($model, 'endDateString')->widget(\kartik\widgets\DateTimePicker::classname(), [ - 'pluginOptions' => [ - 'autoclose'=>true, - 'format' => 'yyyy.mm.dd hh:ii' - ], - 'options' => [ - 'autocomplete' => 'off' - ] - ]) - ?> + field($model, 'endDateString')->widget(\kartik\widgets\DateTimePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose' => true, + 'format' => 'yyyy.mm.dd hh:ii' + ], + 'options' => [ + 'autocomplete' => 'off' + ] + ]) + ?> + field($model, 'seat_count')->textInput() ?> - field($model, 'id_room')->dropDownList(\common\models\Room::roomOptions(false, true) ) ?> + field($model, 'id_room')->dropDownList(\common\models\Room::roomOptions(false, true)) ?> - field($model, 'id_trainer')->dropDownList(\common\models\Trainer::trainerOptions(false, true) ) ?> + field($model, 'id_trainer')->dropDownList(\common\models\Trainer::trainerOptions(false, true)) ?> - field($model, 'id_event_type')->dropDownList(\common\models\EventType::eventTypeOptions(false, true) ) ?> + field($model, 'id_event_type')->dropDownList(\common\models\EventType::eventTypeOptions(false, true)) ?> -
- isNewRecord ? Yii::t('event', 'Create') : Yii::t('event', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ isNewRecord ? Yii::t('event', 'Create') : Yii::t('event', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
- - -
diff --git a/backend/views/event/register_card.php b/backend/views/event/register_card.php index 5e41518..9394525 100644 --- a/backend/views/event/register_card.php +++ b/backend/views/event/register_card.php @@ -14,6 +14,31 @@ $this->params['breadcrumbs'][] = $this->title;

title) ?>

+

Esemény

+ $event, + 'attributes' => [ + 'id', + 'start:datetime', + 'end:datetime', + [ + 'attribute' => 'room.name', + 'label' => $model->getAttributeLabel('id_room') + ], + [ + 'attribute' => 'trainer.name', + 'label' => $model->getAttributeLabel('id_trainer') + ], + [ + 'attribute' => 'eventType.name', + 'label' => $model->getAttributeLabel('id_event_type') + ], + 'seat_count', + ], + ]) ?> + render('_form_register_card', [ 'model' => $model, ]) ?> diff --git a/backend/views/event/view.php b/backend/views/event/view.php index f060a80..694d399 100644 --- a/backend/views/event/view.php +++ b/backend/views/event/view.php @@ -16,8 +16,9 @@ $this->params['breadcrumbs'][] = $this->title;

$model->id], ['class' => 'btn btn-primary']) ?> + $model->id], ['class' => 'btn btn-primary']) ?> $model->id], [ - 'class' => 'btn btn-danger', + 'class' => 'btn btn-danger pull-right', 'data' => [ 'confirm' => Yii::t('event', 'Are you sure you want to delete this item?'), 'method' => 'post', @@ -25,27 +26,144 @@ $this->params['breadcrumbs'][] = $this->title; ]) ?>

- $model, - 'attributes' => [ - 'id', - 'start:datetime', - 'end:datetime', - [ - 'attribute' => 'room.name', - 'label' => $model->getAttributeLabel('id_room') - ], - [ - 'attribute' => 'trainer.name', - 'label' => $model->getAttributeLabel('id_trainer') - ], - [ - 'attribute' => 'eventType.name', - 'label' => $model->getAttributeLabel('id_event_type') - ], - 'created_at:datetime', - 'updated_at:datetime', - ], - ]) ?> +
+
+ $model, + 'attributes' => [ + 'id', + 'start:datetime', + 'end:datetime', + [ + 'attribute' => 'room.name', + 'label' => $model->getAttributeLabel('id_room') + ], + [ + 'attribute' => 'trainer.name', + 'label' => $model->getAttributeLabel('id_trainer') + ], + ], + ]); + } catch (Exception $e) { + echo "failed to render event details "; + } ?> +
+
+ $model, + 'attributes' => [ + [ + 'attribute' => 'eventType.name', + 'label' => $model->getAttributeLabel('id_event_type') + ], + 'seat_count', + 'created_at:datetime', + 'updated_at:datetime', + ], + ]); + } catch (Exception $e) { + echo "Failed to render view"; + } ?> +
+
+

Regisztrációk

+ + $dataProvider, + 'columns' => [ + [ + 'attribute' => 'card_number', + 'label' => \Yii::t('event', 'Card Number'), + 'contentOptions' => function ($model ) { + $options = []; + + if (isset($model['event_registration_canceled_at'])) { + $options['style'] = 'text-decoration: line-through;'; + } + + return $options; + } + ], + [ + 'attribute' => 'customer_name', + 'label' => \Yii::t('event', 'Customer Name'), + 'contentOptions' => function ($model ) { + $options = []; + + if (isset($model['event_registration_canceled_at'])) { + $options['style'] = 'text-decoration: line-through;'; + } + + return $options; + } + ], + [ + 'attribute' => 'customer_email', + 'label' => \Yii::t('event', 'Customer Email'), + 'contentOptions' => function ($model ) { + $options = []; + + if (isset($model['event_registration_canceled_at'])) { + $options['style'] = 'text-decoration: line-through;'; + } + + return $options; + } + ], + [ + 'attribute' => 'event_registration_created_at', + 'label' => \Yii::t('event', 'Event Registration Created At'), + 'contentOptions' => function ($model) { + $options = []; + + if (isset($model['event_registration_canceled_at'])) { + $options['style'] = 'text-decoration: line-through;'; + } + + return $options; + } + ], + [ + 'attribute' => 'event_registration_canceled_at', + 'label' => \Yii::t('event', 'Canceled At'), + 'contentOptions' => function () { + $options = []; + return $options; + } + ], + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{cancel-registration}', + 'urlCreator' => function ($action, $model) { + $params = ['id' => $model['event_registration_id']]; + $params[0] = "event" . '/' . $action; + return \yii\helpers\Url::toRoute($params); + }, + 'buttons' => [ + 'cancel-registration' => function ($url, $model) { + if (isset($model['event_registration_canceled_at'])) { + return ""; + } + $options = [ + 'title' => Yii::t('yii', 'Delete'), + 'aria-label' => Yii::t('yii', 'Delete'), + 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), + 'data-method' => 'post', + 'data-pjax' => '0', + ]; + return Html::a('', $url, $options); + }, + ] + + ], + ] + ]); + } catch (Exception $e) { + echo "Failed to render registrations"; + } + ?>
diff --git a/backend/views/ticket-type/_form.php b/backend/views/ticket-type/_form.php index ea80d2d..9e2e6b8 100644 --- a/backend/views/ticket-type/_form.php +++ b/backend/views/ticket-type/_form.php @@ -33,6 +33,7 @@ use yii\helpers\ArrayHelper; field($model, 'max_usage_count')->textInput() ?> + field($model, 'max_reservation_count')->textInput() ?>
diff --git a/backend/views/ticket-type/view.php b/backend/views/ticket-type/view.php index 02fc174..1180fd9 100644 --- a/backend/views/ticket-type/view.php +++ b/backend/views/ticket-type/view.php @@ -21,49 +21,54 @@ $this->params['breadcrumbs'][] = $this->title;

- $model, - 'attributes' => [ - 'name', - [ - 'attribute' => 'type', - 'value' => $model->typeHuman - ], - 'max_usage_count', - [ - 'attribute' => 'time_unit_count', - ], - [ - 'attribute' => 'time_unit_type', - 'value' => $model->timeUnitHuman - ], - 'price_brutto', - [ - 'attribute' => 'id_account', - 'value' => $model->accountName, - ], - [ - 'attribute' => 'flag_student', - 'value' => ( $model->isStudent() ? Yii::t('common', 'Yes' ) : Yii::t('common', 'No' ) ), - ], - [ - 'attribute' => 'status', - 'value' => $model->statusHuman - ], - [ - 'attribute' => 'door_allowed', - 'value' => ( $model->isDoor() ? Yii::t('common', 'Yes' ) : Yii::t('common', 'No' ) ), - //'visible' => \common\components\Helper::isTicketTypeDoorAllowedCheckOn() + $model, + 'attributes' => [ + 'name', + [ + 'attribute' => 'type', + 'value' => $model->typeHuman + ], + 'max_usage_count', + 'max_reservation_count', + [ + 'attribute' => 'time_unit_count', + ], + [ + 'attribute' => 'time_unit_type', + 'value' => $model->timeUnitHuman + ], + 'price_brutto', + [ + 'attribute' => 'id_account', + 'value' => $model->accountName, + ], + [ + 'attribute' => 'flag_student', + 'value' => ($model->isStudent() ? Yii::t('common', 'Yes') : Yii::t('common', 'No')), + ], + [ + 'attribute' => 'status', + 'value' => $model->statusHuman + ], + [ + 'attribute' => 'door_allowed', + 'value' => ($model->isDoor() ? Yii::t('common', 'Yes') : Yii::t('common', 'No')), + //'visible' => \common\components\Helper::isTicketTypeDoorAllowedCheckOn() + ], + 'created_at:datetime', + 'updated_at:datetime', + [ + 'attribute' => 'installment_enabled', + 'value' => ($model->isInstallment() ? Yii::t('common', 'Yes') : Yii::t('common', 'No')), + ], + 'installment_money', + 'installment_count', ], - 'created_at:datetime', - 'updated_at:datetime', - [ - 'attribute' => 'installment_enabled', - 'value' => ( $model->isInstallment() ? Yii::t('common', 'Yes' ) : Yii::t('common', 'No' ) ), - ], - 'installment_money', - 'installment_count', - ], - ]) ?> + ]); + } catch (Exception $e) { + echo "Failed to render ticket type"; + } ?>
diff --git a/common/components/TicketSale.php b/common/components/TicketSale.php index e5c038c..fdf288d 100644 --- a/common/components/TicketSale.php +++ b/common/components/TicketSale.php @@ -1 +1 @@ -readMoney(); $this->readStartDate(); $this->readEndDate(); $this->mkTicket(); $this->mkTransfer(); $this->addToCustomerCart(); } protected function mkTicket( ) { $this->ticket = new Ticket (); $this->ticket->id_user = \Yii::$app->user->id; $this->ticket->id_ticket_type = $this->ticketType->id_ticket_type; // save to contract $this->ticket->id_account = $this->account->id_account; $this->ticket->id_discount = isset($this->discount) ? $this->discount->id_discount : null; // contract.id_discount $this->ticket->start = $this->start; $this->ticket->end = $this->end; $this->ticket->max_usage_count = $this->ticketType->max_usage_count; $this->ticket->usage_count = 0; $this->ticket->status = $this->transferStatus == Transfer::STATUS_PAID ? Ticket::STATUS_ACTIVE : Ticket::STATUS_INACTIVE ; $this->ticket->price_brutto = $this->money; $this->ticket->id_card = $this->card->id_card; if ( isset( $this->ticketInstallmentRequest ) ){ $this->ticket->part = $this->ticketInstallmentRequest->priority; } if (isset($this->contract)){ $this->ticket->id_contract = $this->contract->id_contract; } if ( !$this->ticket->save ( false ) ){ \Yii::error("Nem sikerült menteni a bérletet!"); throw new \Exception("Bérlet mentése nem sikerült"); } \Yii::info("Bérlet elmentve: id=" . $this->ticket->id_ticket); } protected function mkTransfer( ) { $this->transfer = new Transfer (); $this->transfer->status = $this->transferStatus; $this->transfer->type = Transfer::TYPE_TICKET; $this->transfer->direction = Transfer::DIRECTION_IN; $this->transfer->id_object = $this->ticket->id_ticket; $this->transfer->item_price = $this->ticketType->price_brutto; $this->transfer->money = $this->money; $this->transfer->id_account = $this->account->id_account; $this->transfer->count = 1; if ($this->transferStatus == Transfer::STATUS_PAID) { $this->transfer->paid_at = date ( 'Y-m-d H:i:s' ); $this->transfer->paid_by = \Yii::$app->user->id; } $this->transfer->payment_method = $this->paymentMethod; $this->transfer->comment = ""; if ( isset($this->ticketInstallmentRequest)){ $this->transfer->comment = "Csoportos megbízással"; } $this->transfer->id_user = \Yii::$app->user->id; $this->transfer->id_customer = $this->customer->id_customer; if ( !$this->transfer->save (false) ){ \Yii::error("Nem sikerült menteni a tranzakciót!"); throw new \Exception("Tranzakció mentése nem sikerült"); } \Yii::info("tranzakció elmentve: id=" . $this->transfer->id_transfer); } protected function addToCustomerCart( ) { if ( isset( $this->customer ) ){ if ( $this->transferStatus == Transfer::STATUS_NOT_PAID ){ $cartItem = new ShoppingCart(); $cartItem->id_customer = $this->customer->id_customer; $cartItem->id_transfer = $this->transfer->id_transfer; $cartItem->save(false); } } } protected function readMoney(){ if ( !isset($this->money)){ $this->money = $this->ticketType->price_brutto; if (isset($this->discount)){ $this->money = Discount::applyDiscount($this->money, $this->discount); } if ( isset($this->ticketInstallmentRequest)){ $this->money = $this->ticketInstallmentRequest->money; } } } protected function readStartDate(){ if ( !isset($this->start)){ $this->start = Helper::getDateString(); if ( isset($this->ticketInstallmentRequest)){ $this->start = $this->ticketInstallmentRequest->request_target_time_at; } } } protected function readEndDate(){ if ( !isset($this->end)){ $unit = "month"; if ( $this->ticketType->time_unit_type == TicketType::TIME_UNIT_DAY ) { $unit = "day"; } $count = $this->ticketType->time_unit_count; $this->end = date( 'Y-m-d', strtotime( $this->start . " +$count $unit -1 day")); if ( isset($this->ticketInstallmentRequest)){ $this->end = date( 'Y-m-d', strtotime( $this->ticketInstallmentRequest->request_target_time_at . " +1 month -1 day")); } } } } \ No newline at end of file +readMoney(); $this->readStartDate(); $this->readEndDate(); $this->mkTicket(); $this->mkTransfer(); $this->addToCustomerCart(); } /** * @throws \Exception */ protected function mkTicket( ) { $this->ticket = new Ticket (); $this->ticket->id_user = \Yii::$app->user->id; $this->ticket->id_ticket_type = $this->ticketType->id_ticket_type; // save to contract $this->ticket->id_account = $this->account->id_account; $this->ticket->id_discount = isset($this->discount) ? $this->discount->id_discount : null; // contract.id_discount $this->ticket->start = $this->start; $this->ticket->end = $this->end; $this->ticket->max_reservation_count = $this->ticketType->max_reservation_count; $this->ticket->max_usage_count = $this->ticketType->max_usage_count; $this->ticket->usage_count = 0; $this->ticket->reservation_count = 0; $this->ticket->status = $this->transferStatus == Transfer::STATUS_PAID ? Ticket::STATUS_ACTIVE : Ticket::STATUS_INACTIVE ; $this->ticket->price_brutto = $this->money; $this->ticket->id_card = $this->card->id_card; if ( isset( $this->ticketInstallmentRequest ) ){ $this->ticket->part = $this->ticketInstallmentRequest->priority; } if (isset($this->contract)){ $this->ticket->id_contract = $this->contract->id_contract; } if ( !$this->ticket->save ( false ) ){ \Yii::error("Nem sikerült menteni a bérletet!"); throw new \Exception("Bérlet mentése nem sikerült"); } \Yii::info("Bérlet elmentve: id=" . $this->ticket->id_ticket); } /** * @throws \Exception */ protected function mkTransfer( ) { $this->transfer = new Transfer (); $this->transfer->status = $this->transferStatus; $this->transfer->type = Transfer::TYPE_TICKET; $this->transfer->direction = Transfer::DIRECTION_IN; $this->transfer->id_object = $this->ticket->id_ticket; $this->transfer->item_price = $this->ticketType->price_brutto; $this->transfer->money = $this->money; $this->transfer->id_account = $this->account->id_account; $this->transfer->count = 1; if ($this->transferStatus == Transfer::STATUS_PAID) { $this->transfer->paid_at = date ( 'Y-m-d H:i:s' ); $this->transfer->paid_by = \Yii::$app->user->id; } $this->transfer->payment_method = $this->paymentMethod; $this->transfer->comment = ""; if ( isset($this->ticketInstallmentRequest)){ $this->transfer->comment = "Csoportos megbízással"; } $this->transfer->id_user = \Yii::$app->user->id; $this->transfer->id_customer = $this->customer->id_customer; if ( !$this->transfer->save (false) ){ \Yii::error("Nem sikerült menteni a tranzakciót!"); throw new \Exception("Tranzakció mentése nem sikerült"); } \Yii::info("tranzakció elmentve: id=" . $this->transfer->id_transfer); } protected function addToCustomerCart( ) { if ( isset( $this->customer ) ){ if ( $this->transferStatus == Transfer::STATUS_NOT_PAID ){ $cartItem = new ShoppingCart(); $cartItem->id_customer = $this->customer->id_customer; $cartItem->id_transfer = $this->transfer->id_transfer; $cartItem->save(false); } } } protected function readMoney(){ if ( !isset($this->money)){ $this->money = $this->ticketType->price_brutto; if (isset($this->discount)){ $this->money = Discount::applyDiscount($this->money, $this->discount); } if ( isset($this->ticketInstallmentRequest)){ $this->money = $this->ticketInstallmentRequest->money; } } } protected function readStartDate(){ if ( !isset($this->start)){ $this->start = Helper::getDateString(); if ( isset($this->ticketInstallmentRequest)){ $this->start = $this->ticketInstallmentRequest->request_target_time_at; } } } protected function readEndDate(){ if ( !isset($this->end)){ $unit = "month"; if ( $this->ticketType->time_unit_type == TicketType::TIME_UNIT_DAY ) { $unit = "day"; } $count = $this->ticketType->time_unit_count; $this->end = date( 'Y-m-d', strtotime( $this->start . " +$count $unit -1 day")); if ( isset($this->ticketInstallmentRequest)){ $this->end = date( 'Y-m-d', strtotime( $this->ticketInstallmentRequest->request_target_time_at . " +1 month -1 day")); } } } } \ No newline at end of file diff --git a/common/manager/EventRegistrationManager.php b/common/manager/EventRegistrationManager.php index 5ef65b5..7df85e2 100644 --- a/common/manager/EventRegistrationManager.php +++ b/common/manager/EventRegistrationManager.php @@ -1,8 +1,11 @@ "CARD_NOT_FOUND", @@ -30,6 +35,8 @@ class EventRegistrationManager extends \yii\base\Object self::NO_FREE_SEATS => "NO_FREE_SEATS", self::EVENT_TYPE_NOT_FOUND => "EVENT_TYPE_NOT_FOUND", self::TICKET_INSUFFICIENT => "TICKET_INSUFFICIENT", + self::UNKNOWN_ERROR => "UNKNOWN_ERROR", + self::MAX_SEAT_COUNT_EXCEEDED => "MAX_SEAT_COUNT_EXCEEDED", ]; @@ -39,6 +46,7 @@ class EventRegistrationManager extends \yii\base\Object * @throws NotFoundHttpException * @throws BadRequestHttpException * @throws ServerErrorHttpException + * @throws \yii\web\HttpException */ public function registerCard($cardEventForm){ @@ -77,21 +85,85 @@ class EventRegistrationManager extends \yii\base\Object $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; - $registration->save(false); + try{ + $registration->save(false); + }catch (\Throwable $exception){ + throw new ServerErrorHttpException("Failed to save", self::UNKNOWN_ERROR); + } $cardEventForm->registration = $registration; } } + 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", + "card.number as card_number", + "card.number as card_number", + "customer.name as customer_name", + "customer.email as customer_email", + ]); + $query->from(EventRegistration::tableName()); + $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 ]); + + return $query; + } + + + /** + * @param $idRegistration + * @return array|EventRegistration|\yii\db\ActiveRecord|null + * @throws NotFoundHttpException + */ + public function loadRegistration($idRegistration) { + + $registration = EventRegistration::find()->andWhere(['id' => $idRegistration ])->one(); + if ($registration == null) { + throw new NotFoundHttpException('The requested registration does not exist.'); + } + return $registration; + } + + /** + * @param \common\models\EventRegistration $registration + * @throws ServerErrorHttpException + */ + public function cancelRegistration($registration){ + if ( isset($registration->canceled_at)){ + throw new ServerErrorHttpException('The registration is already canceled'); + } + + $ticket = Ticket::findOne(['id' => $registration->id_ticket ]); + + $ticket->restoreReservationCount(1); + $ticket->save(false); + + $registration->canceled_at = date('Y-m-d H:i:s' ); + $registration->save(false); + + } + } \ No newline at end of file diff --git a/common/messages/hu/common/ticket_type.php b/common/messages/hu/common/ticket_type.php index c15f450..61bb5f1 100644 --- a/common/messages/hu/common/ticket_type.php +++ b/common/messages/hu/common/ticket_type.php @@ -29,7 +29,7 @@ return [ 'Inactive' => 'Inaktív', 'Invalid account (inactive)!' => 'Érvénytelen kassza (inaktív)!', 'Invalid account!' => 'Érvénytelen kassza!', - 'Max Usage Count' => 'Akalmak', + 'Max Usage Count' => 'Belépések száma (f. villa)', 'Month' => 'Hónap', 'Name' => 'Név', 'Normal' => 'Normál', @@ -45,4 +45,5 @@ return [ 'Type' => 'Típus', 'Update' => 'Módosítás', 'Updated At' => 'Módosítás ideje', + 'Max Reservation Count' => 'Foglalások száma (csop. edzés)', ]; diff --git a/common/messages/hu/event.php b/common/messages/hu/event.php index c7c3623..11eb796 100644 --- a/common/messages/hu/event.php +++ b/common/messages/hu/event.php @@ -32,5 +32,12 @@ return [ 'Id Room' => 'Terem', 'Id Trainer' => 'Edző', 'Id Event Type' => 'Esemény Típus', - 'Registration Count' => 'Résztvevők' + 'Registration Count' => 'Résztvevők', + 'Card Number' => 'Kártyaszám', + 'Customer Name' => 'Vendég Neve', + 'Customer Email' => 'Vendég E-Mail címe', + 'Canceled At' => 'Sztornó dátum, idő', + 'Register' => 'Regisztráció', + 'Event Registration Created At' => 'Regisztráció dátum, idő', + ]; \ No newline at end of file diff --git a/common/models/CardEventRegistrationForm.php b/common/models/CardEventRegistrationForm.php index aa4154f..1022169 100644 --- a/common/models/CardEventRegistrationForm.php +++ b/common/models/CardEventRegistrationForm.php @@ -18,6 +18,7 @@ class CardEventRegistrationForm extends \yii\base\Model public function rules() { return [ + [['card_number'], 'required' ], [['card_number'], 'validateFormat' ] ]; } @@ -29,5 +30,15 @@ class CardEventRegistrationForm extends \yii\base\Model public function getIsNewRecord(){ return true; } + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'card_number' => \Yii::t('event', 'Card Number'), + ]; + } + } \ No newline at end of file diff --git a/common/models/Collection.php b/common/models/Collection.php index a7c083d..1a6d85f 100644 --- a/common/models/Collection.php +++ b/common/models/Collection.php @@ -123,13 +123,20 @@ class Collection extends \common\models\BaseFitnessActiveRecord return $result; } - - + + /** - * - * */ + * + * @param string $mode + * @param $start + * @param $end + * @param $idUser + * @param $types + * @param $idAccount + * @return Query + */ public static function mkTotalQuery($mode = 'reception', $start,$end,$idUser,$types,$idAccount){ - + $query = new Query(); $query->innerJoin("account",'account.id_account = collection.id_account' ); @@ -168,7 +175,7 @@ public static function mkTotalQuery($mode = 'reception', $start,$end,$idUser,$ty } - public static function mkTotalsResultWithAllAvailableAccount($queryResult,$accounts,$accountMap,$idAccount){ + public static function mkTotalsResultWithAllAvailableAccount($queryResult,$accounts,$idAccount){ $totals = []; $totals['total'] = 0; @@ -228,7 +235,7 @@ public static function mkTotalQuery($mode = 'reception', $start,$end,$idUser,$ty public static function mkReceptionTotal( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){ $result = []; $queryResult = self::exTotalQuery('reception', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount); + $result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $idAccount); return $result; } } diff --git a/common/models/CollectionCreate.php b/common/models/CollectionCreate.php index 9108501..681d4dd 100644 --- a/common/models/CollectionCreate.php +++ b/common/models/CollectionCreate.php @@ -59,7 +59,7 @@ class CollectionCreate extends \common\models\Collection if (parent::beforeSave($insert)) { $this->id_user = Yii::$app->user->id; $this->created_by = Yii::$app->user->id; - $paidAt = Transfer::mkPaidAtTotals($this->timestampStart, $this->timestampEnd, $this->user->id, null, $this->account->id_account, $this->accounts, $this->accountMap); + $paidAt = Transfer::mkPaidAtTotals($this->timestampStart, $this->timestampEnd, $this->user->id, null, $this->account->id_account, $this->accounts ); $this->money = $paidAt['total']; return true; } else { diff --git a/common/models/Contract.php b/common/models/Contract.php index 0462d69..f09c564 100644 --- a/common/models/Contract.php +++ b/common/models/Contract.php @@ -19,6 +19,7 @@ use common\components\Helper; * @property integer $part_count * @property integer $part_required * @property integer $id_ticket_type + * @property integer $id_discount * @property string $expired_at * @property string $created_at * @property string $updated_at diff --git a/common/models/Event.php b/common/models/Event.php index 6fd3e46..9cb3533 100644 --- a/common/models/Event.php +++ b/common/models/Event.php @@ -18,7 +18,10 @@ use yii\helpers\ArrayHelper; * @property integer $seat_count * @property string $created_at * @property string $updated_at - * @property \common\models\EventType eventType + * @property \common\models\EventType $eventType + * @property \common\models\Trainer $trainer + * @property \common\models\Room $room + * @property \common\models\EventRegistration[] $eventRegistrations */ class Event extends \yii\db\ActiveRecord { @@ -45,7 +48,8 @@ class Event extends \yii\db\ActiveRecord return [ [['startDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'start', 'timeZone' => 'UTC'], [['endDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'end' , 'timeZone' => 'UTC'], - [['id_trainer','id_room', 'id_event_type'], 'required'], + [['id_trainer','id_room', 'id_event_type','seat_count'], 'required'], + [['id_trainer','id_room', 'id_event_type','seat_count'], 'integer'], ]; } @@ -72,6 +76,7 @@ class Event extends \yii\db\ActiveRecord 'startDateString' => Yii::t('event', 'Start'), 'endDateString' => Yii::t('event', 'End'), 'status' => Yii::t('event', 'Status'), + 'seat_count' => Yii::t('event', 'Seat Count'), ]; } @@ -127,13 +132,18 @@ class Event extends \yii\db\ActiveRecord * @return \common\models\EventRegistration[]|\yii\db\ActiveQuery */ public function getEventRegistrationCount(){ - return sizeof($this->getEventRegistrations()); + return sizeof($this->eventRegistrations); } public function hasFreeSeats(){ - $registrationCount = $this->getEventRegistrations() ; + $registrationCount = sizeof($this->eventRegistrations) ; - return $registrationCount < $this->seat_count ; + $seatCount = $this->seat_count; + if ( !isset($seatCount ) || $seatCount == 0){ + $seatCount = PHP_INT_MAX; + } + + return $registrationCount < $seatCount ; } } diff --git a/common/models/EventRegistration.php b/common/models/EventRegistration.php index ab2cfaf..4c8ed2a 100644 --- a/common/models/EventRegistration.php +++ b/common/models/EventRegistration.php @@ -3,6 +3,8 @@ namespace common\models; use Yii; +use yii\behaviors\TimestampBehavior; +use yii\helpers\ArrayHelper; /** * This is the model class for table "event_registration". @@ -50,4 +52,15 @@ class EventRegistration extends \yii\db\ActiveRecord 'canceled_at' => Yii::t('event-registration', 'Canceled At'), ]; } + + public function behaviors() + { + return ArrayHelper::merge( [ + [ + 'class' => TimestampBehavior::className(), + 'value' => function(){ return date('Y-m-d H:i:s' ); } + ] + ], + parent::behaviors()); + } } diff --git a/common/models/EventType.php b/common/models/EventType.php index 9235bd0..07f2717 100644 --- a/common/models/EventType.php +++ b/common/models/EventType.php @@ -89,7 +89,19 @@ class EventType extends \yii\db\ActiveRecord if ( sizeof($tickets) == 0 ){ return null; } + $possibleTickets = []; + + foreach ($tickets as $ticket ){ + if ( $ticket->hasOpenReservationCount() ){ + $possibleTickets[] = $ticket; + } + } + + if ( sizeof($possibleTickets) == 0 ){ + return null; + } + // TODO: implement bossiness logic to select ticket - return $tickets[0]; + return $possibleTickets[0]; } } diff --git a/common/models/Ticket.php b/common/models/Ticket.php index f87ccc7..5acdc78 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -2,10 +2,12 @@ namespace common\models; +use common\manager\EventRegistrationManager; use Yii; use yii\db\Query; use yii\db\Expression; use common\components\Helper; +use yii\web\HttpException; /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ @@ -33,6 +35,8 @@ use common\components\Helper; * @property int id_contract * @property integer $original_price * @property string $original_end; + * @property integer $reservation_count + * @property integer $max_reservation_count * * @property \common\models\Card card * @property \common\models\Ticket transfer @@ -441,7 +445,31 @@ class Ticket extends \common\models\BaseFitnessActiveRecord } } } - + + public function hasOpenReservationCount(){ + return $this->reservation_count < $this->max_reservation_count; + } + + /** + * @param $count + * @throws HttpException + */ + public function consumeReservationCount($count){ + $newReservationCount = $this->reservation_count + $count; + if ( $newReservationCount > $this->max_reservation_count ){ + throw new HttpException(406,"Max ticket seat count exceeded",EventRegistrationManager::MAX_SEAT_COUNT_EXCEEDED); + } + $this->reservation_count = $newReservationCount; + } + + public function restoreReservationCount($usagesToRestore){ + $this->reservation_count = $this->reservation_count - $usagesToRestore; + if ( $this->usage_count < 0 ){ + $this->usage_count = 0; + } + } + + public function afterSave($insert, $changedAttributes) { Card::updateCardFlagTicket($this->id_card);; } diff --git a/common/models/TicketType.php b/common/models/TicketType.php index a424676..d377ac0 100644 --- a/common/models/TicketType.php +++ b/common/models/TicketType.php @@ -24,13 +24,22 @@ use yii\helpers\ArrayHelper; * @property integer door_allowed * @property string $created_at * @property string $updated_at + * @property integer $max_reservation_count + * @property \common\models\Account $account + * @property string $typeHuman + * @property string $timeUnitHuman + * @property string $accountName */ -class TicketType extends \common\models\BaseFitnessActiveRecord { +class TicketType extends BaseFitnessActiveRecord +{ const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; - CONST TIME_UNIT_DAY = 10;//nap - CONST TIME_UNIT_MONTH = 20;//hónap - CONST TIME_UNIT_MONTH_REFERENCE = 30; //tárgy hónap + // day + CONST TIME_UNIT_DAY = 10; + // month + CONST TIME_UNIT_MONTH = 20; + // subject month + CONST TIME_UNIT_MONTH_REFERENCE = 30; const TYPE_NORMAL = 10; const TYPE_DEFAULT = self::TYPE_NORMAL; @@ -57,7 +66,7 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { public function rules() { return [ - [['name', 'id_account','time_unit_count','type' ,'time_unit_type' ,'max_usage_count','price_brutto'], 'required'], + [['name', 'id_account','time_unit_count','type' ,'time_unit_type' ,'max_usage_count','max_reservation_count','price_brutto'], 'required'], //////////////// //price brutto //////////////// @@ -76,6 +85,10 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { //////////////// [['max_usage_count',], 'integer','min' => 0 , 'max' => 10000], //////////////// + //max_reservation_count + //////////////// + [['max_reservation_count',], 'integer','min' => 0 , 'max' => 10000], + //////////////// //flag_student //////////////// [['flag_student',], 'integer'], @@ -140,6 +153,7 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { 'installment_count' => Yii::t('common/ticket_type', 'Havi részletek száma'), 'installment_money' => Yii::t('common/ticket_type', 'Havi részlet összege'), 'door_allowed' => Yii::t('common/ticket_type', 'Forgóvilla'), + 'max_reservation_count' => Yii::t('common/ticket_type', 'Max Reservation Count'), ]; } @@ -169,7 +183,7 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { } public function getTimeUnitHuman() { $result = null; - $s = self::timeUnitTypes ( $this->time_unit_type ); + $s = self::timeUnitTypes( ); if (array_key_exists ( $this->time_unit_type, $s )) { $result = $s [$this->time_unit_type]; } @@ -210,7 +224,7 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { } - public function validateIdAccount($attribute,$params){ + public function validateIdAccount($attribute){ $account = null; if ( !$this->hasErrors("id_account")){ $account = Account::findOne($this->$attribute); @@ -233,9 +247,12 @@ class TicketType extends \common\models\BaseFitnessActiveRecord { } } - /** - * $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive - * */ + /** + * $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive + * @param null $forceIncludeObjectWithId + * @param null $account + * @return array|\yii\db\ActiveRecord[]|null + */ public static function read($forceIncludeObjectWithId = null, $account = null){ $ticketTypes = null; diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 9d718ac..d128550 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -3,11 +3,8 @@ namespace common\models; use Yii; -use yii\base\Object; use yii\helpers\ArrayHelper; use yii\behaviors\TimestampBehavior; -use common\components\AccountAwareBehavior; -use common\components\UserAwareBehavior; use common\components\DiscountAwareBehavior; use common\components\CustomerAwareBehavior; use yii\db\Query; @@ -604,12 +601,17 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { } return $status; } - public function beforeDelete() { + + /** + * @return bool + * @throws \yii\db\StaleObjectException + */ + public function beforeDelete() { parent::beforeDelete (); if ($this->type == Transfer::TYPE_TICKET) { $ticket = $this->ticket; if ($ticket != null) { - $ticket->delete (); + $ticket->delete(); } } else if ($this->type == Transfer::TYPE_MONEY_MOVEMENT_OUT) { /** @noinspection PhpUndefinedFieldInspection */ @@ -816,11 +818,10 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { * an array, wchic contains items. each item has to key - value pairs: [ id_account => 0, money => 0 ] * * @param $accounts - * @param $accountMap * @param $idAccount * @return array */ - public static function mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount) { + public static function mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $idAccount) { $totals = [ ]; $totals ['total'] = 0; @@ -867,6 +868,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { * @param $types * @param $idAccount * @return array + * @throws \yii\db\Exception */ public static function exTotalQuery($mode, $start, $end, $idUser, $types, $idAccount) { $query = self::mkTotalQuery ( $mode, $start, $end, $idUser, $types, $idAccount ); @@ -885,45 +887,56 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { * @param $accounts * @param $accountMap * @return array + * @throws \yii\db\Exception */ - public static function mkPaidAtTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + public static function mkPaidAtTotals($start, $end, $idUser, $types, $idAccount, $accounts) { /** @noinspection PhpUnusedLocalVariableInspection */ $result = [ ]; $queryResult = self::exTotalQuery ( 'paid_at', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $accountMap, $idAccount ); + $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $idAccount ); return $result; } - - /** - * find all transfers in the period ( doesn't matter if paid or not ) - */ - public static function mkCreatedAtTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + + /** + * find all transfers in the period ( doesn't matter if paid or not ) + * @throws \yii\db\Exception + */ + public static function mkCreatedAtTotals($start, $end, $idUser, $types, $idAccount, $accounts) { /** @noinspection PhpUnusedLocalVariableInspection */ $result = [ ]; $queryResult = self::exTotalQuery ( 'created_at', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $accountMap, $idAccount ); + $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $idAccount ); return $result; } - - /** - * find transfers which were created but not paid in the period - */ - public static function mkCreatedAtNotPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + + /** + * find transfers which were created but not paid in the period + * @throws \yii\db\Exception + */ + public static function mkCreatedAtNotPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts) { /** @noinspection PhpUnusedLocalVariableInspection */ $result = [ ]; $queryResult = self::exTotalQuery ( 'created_at_not_paid', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $accountMap, $idAccount ); + $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $idAccount ); return $result; } - - /** - * find transfers which were created and paid in the period - */ - public static function mkCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + + /** + * find transfers which were created and paid in the period + * @param $start + * @param $end + * @param $idUser + * @param $types + * @param $idAccount + * @param $accounts + * @return array + * @throws \yii\db\Exception + */ + public static function mkCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts) { /** @noinspection PhpUnusedLocalVariableInspection */ $result = [ ]; $queryResult = self::exTotalQuery ( 'created_at_paid', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $accountMap, $idAccount ); + $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $idAccount ); return $result; } @@ -937,22 +950,35 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { * @param $accounts * @param $accountMap * @return array + * @throws \yii\db\Exception */ - public static function mkPaidAtNotCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + public static function mkPaidAtNotCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts) { /** @noinspection PhpUnusedLocalVariableInspection */ $result = [ ]; $queryResult = self::exTotalQuery ( 'paid_at_not_created_at', $start, $end, $idUser, $types, $idAccount ); - $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $accountMap, $idAccount ); + $result = self::mkTotalsResultWithAllAvailableAccount ( $queryResult, $accounts, $idAccount ); return $result; } - public static function mkTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap) { + + /** + * @param $start + * @param $end + * @param $idUser + * @param $types + * @param $idAccount + * @param $accounts + * @param $accountMap + * @return array + * @throws \yii\db\Exception + */ + public static function mkTotals($start, $end, $idUser, $types, $idAccount, $accounts) { $result = [ ]; - $result ['paid_at'] = self::mkPaidAtTotals ( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ); - $result ['created_at'] = self::mkCreatedAtTotals ( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ); - $result ['created_at_not_paid'] = self::mkCreatedAtNotPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ); - $result ['created_at_paid'] = self::mkCreatedAtPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ); - $result ['paid_at_not_created_at'] = self::mkPaidAtNotCreatedAtPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ); + $result ['paid_at'] = self::mkPaidAtTotals ( $start, $end, $idUser, $types, $idAccount, $accounts ); + $result ['created_at'] = self::mkCreatedAtTotals ( $start, $end, $idUser, $types, $idAccount, $accounts ); + $result ['created_at_not_paid'] = self::mkCreatedAtNotPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts ); + $result ['created_at_paid'] = self::mkCreatedAtPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts ); + $result ['paid_at_not_created_at'] = self::mkPaidAtNotCreatedAtPaidTotals ( $start, $end, $idUser, $types, $idAccount, $accounts ); return $result; } @@ -1220,6 +1246,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { $ticket->id_discount = null; // contract.id_discount $ticket->start = $request->request_target_time_at; $ticket->end = date ( 'Y-m-d', strtotime ( $request->request_target_time_at . " +1 month -1 day" ) ); + $ticket->max_reservation_count = $ticketType->max_reservation_count; $ticket->max_usage_count = $ticketType->max_usage_count; $ticket->usage_count = 0; $ticket->status = Ticket::STATUS_INACTIVE; diff --git a/console/migrations/m181227_210620_alter_table_event_registration_cancaled_at_allow_null.php b/console/migrations/m181227_210620_alter_table_event_registration_cancaled_at_allow_null.php new file mode 100644 index 0000000..86d5839 --- /dev/null +++ b/console/migrations/m181227_210620_alter_table_event_registration_cancaled_at_allow_null.php @@ -0,0 +1,30 @@ +execute("alter table event_registration modify canceled_at datetime null;"); + } + + public function down() + { + echo "m181227_210620_alter_table_event_registration_cancaled_at_allow_null cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m181227_215706_fix_trigger_event_seat_count_check.php b/console/migrations/m181227_215706_fix_trigger_event_seat_count_check.php new file mode 100644 index 0000000..b4cebd7 --- /dev/null +++ b/console/migrations/m181227_215706_fix_trigger_event_seat_count_check.php @@ -0,0 +1,43 @@ +execute("DROP TRIGGER IF EXISTS event_seat_count_check;"); + $this->execute("CREATE TRIGGER event_seat_count_check + AFTER INSERT ON event_registration FOR EACH ROW + BEGIN + IF (SELECT coalesce(COUNT(id),0) FROM event_registration + WHERE canceled_at is null AND id_event = NEW.id_event) + > + (SELECT coalesce(seat_count,0) from event where id = NEW.id_event) + THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'MAX_SEAT_COUNT_EXCEEDED'; + END IF; + END; + "); + } + + public function down() + { + echo "m181227_215706_fix_trigger_event_seat_count_check cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m181229_121344_alter_table_ticket_add_seat_count.php b/console/migrations/m181229_121344_alter_table_ticket_add_seat_count.php new file mode 100644 index 0000000..d7ee50a --- /dev/null +++ b/console/migrations/m181229_121344_alter_table_ticket_add_seat_count.php @@ -0,0 +1,31 @@ +addColumn("ticket","reservation_count","int default 0"); + $this->addColumn("ticket","max_reservation_count","int default 0"); + } + + public function down() + { + echo "m181229_121344_alter_table_ticket_add_seat_count cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m181230_100000_alter_table_ticket_type_add_reservation_count.php b/console/migrations/m181230_100000_alter_table_ticket_type_add_reservation_count.php new file mode 100644 index 0000000..f84d94f --- /dev/null +++ b/console/migrations/m181230_100000_alter_table_ticket_type_add_reservation_count.php @@ -0,0 +1,30 @@ +addColumn("ticket_type","max_reservation_count","int default 0"); + } + + public function down() + { + echo "m181230_100000_alter_table_ticket_type_add_reservation_count cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/frontend/controllers/CollectionController.php b/frontend/controllers/CollectionController.php index f0fa37a..24dc0d3 100644 --- a/frontend/controllers/CollectionController.php +++ b/frontend/controllers/CollectionController.php @@ -66,7 +66,7 @@ class CollectionController extends Controller $accounts = Account::read(); $accountMap = Account::toAccaountMap($accounts); - $totals = Transfer::mkTotals($model->start, $model->end, $model->id_user, null, $model->id_account, $accounts, $accountMap); + $totals = Transfer::mkTotals($model->start, $model->end, $model->id_user, null, $model->id_account, $accounts); return $this->render('view', [ 'model' => $model, @@ -106,7 +106,7 @@ class CollectionController extends Controller if ( $model->load(Yii::$app->request->post()) && $model->save() ) { return $this->redirect(['view', 'id' => $model->id_collection]); } else { - $model->totals = Transfer::mkTotals($model->timestampStart, $model->timestampEnd, $model->user->id, null, $model->account->id_account, $model->accounts, $model->accountMap); + $model->totals = Transfer::mkTotals($model->timestampStart, $model->timestampEnd, $model->user->id, null, $model->account->id_account, $model->accounts ); return $this->render('create', [ 'model' => $model, ]); diff --git a/frontend/controllers/TicketController.php b/frontend/controllers/TicketController.php index f6baaaf..2adec5d 100644 --- a/frontend/controllers/TicketController.php +++ b/frontend/controllers/TicketController.php @@ -122,6 +122,7 @@ class TicketController extends FrontendController $model->usage_count = 0; $model->id_card = $receptionForm->card->id_card; $model->id_account = Account::readDefault(); + if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', Yii::t('frontend/ticket', 'Ticket added to customer') ); @@ -139,7 +140,12 @@ class TicketController extends FrontendController 'receptionForm' => $receptionForm, ]); } - + + /** + * @param $id + * @return string|\yii\web\Response + * @throws NotFoundHttpException + */ public function actionUpdate($id){ /** @var \frontend\models\TicketUpdate $model */ @@ -156,12 +162,14 @@ class TicketController extends FrontendController return $this->render('update',['model' => $model]); } - + /** * Deletes an existing Transfer model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed + * @throws NotFoundHttpException + * @throws \yii\db\Exception */ public function actionDelete($id) { diff --git a/frontend/models/TicketCreate.php b/frontend/models/TicketCreate.php index a588ae7..0251bc1 100644 --- a/frontend/models/TicketCreate.php +++ b/frontend/models/TicketCreate.php @@ -9,7 +9,6 @@ use common\models\Discount; use common\models\Transfer; use common\models\UserSoldItem; use common\models\ShoppingCart; -use yii\base\Object; use common\models\TicketInstallmentRequest; use common\models\Contract; use common\components\Helper; @@ -20,7 +19,7 @@ use common\models\Card; * @property $userCart common\models\Transfer[] items in user cart * @property $customerCart common\models\Transfer[] items in customer cart * @property $customer common\models\Customer selected customer - * + * * */ class TicketCreate extends Ticket{ @@ -93,11 +92,11 @@ class TicketCreate extends Ticket{ } - public function validateTicketType($attribute,$params){ + public function validateTicketType($attribute){ $type = TicketType::findOne($this->id_ticket_type); $this->_ticketType = $type; if ( !isset($type)) { - $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid ticket type' )); + $this->addError($attribute,\Yii::t('frontend/ticket' , 'Invalid ticket type' )); }else{ if ( $type->isInstallment()){ $bankAccount = trim($this->customer->bank_account); @@ -124,17 +123,17 @@ class TicketCreate extends Ticket{ } } - public function validateAccount($attribute,$params){ + public function validateAccount($attribute){ $this->_account = Account::findOne($this->id_account); if ( !isset($this->_account )) { - $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid transfer' )); + $this->addError($attribute,\Yii::t('frontend/ticket' , 'Invalid transfer' )); } } - public function validateDiscount($attribute,$params){ + public function validateDiscount($attribute){ $this->_discount = Discount::findOne($this->id_discount); if ( !isset($this->_discount)) { - $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid discount' )); + $this->addError($attribute,\Yii::t('frontend/ticket' , 'Invalid discount' )); } } @@ -163,13 +162,19 @@ class TicketCreate extends Ticket{ $start = DateUtil::parseDate($this->start); $original_end = Helper::getTicketExpirationDate($start,$ticketType); $this->original_end = DateUtil::formatDateUtc($original_end); + $this->max_reservation_count = $ticketType->max_reservation_count; } } } return $result; } - - public function afterSave($insert, $changedAttributes){ + + /** + * @param $insert + * @param $changedAttributes + * @throws \Exception + */ + public function afterSave($insert, $changedAttributes){ $this->addTransfer(); $this->appendToUserCart(); $this->appendToCustomerCart(); @@ -208,7 +213,10 @@ class TicketCreate extends Ticket{ } } - protected function addTransfer(){ + /** + * @throws \Exception + */ + protected function addTransfer(){ //$transfer = Transfer::createTicketTransfer($this->_account, $this->_discount, null, 1, $this); @@ -230,16 +238,13 @@ class TicketCreate extends Ticket{ $transfer->money = $this->price_brutto; $transfer->id_account = $this->_account->id_account; - - $status = Transfer::STATUS_PAID; - + if ( !Transfer::canMarkPaidByReception( $this->payment_method ) ){ $status = Transfer::STATUS_NOT_PAID; }else if ( $this->isAppendToUserCart() ){ $status = Transfer::STATUS_NOT_PAID; }else if ( $this->isAppendToCustomerCart() ){ $status = Transfer::STATUS_NOT_PAID; - $customer = $this->customer; }else { $status = Transfer::STATUS_PAID; $transfer->paid_at = date('Y-m-d H:i:s' ) ; @@ -260,18 +265,6 @@ class TicketCreate extends Ticket{ $this->_transfer = $transfer; } - - - protected function addToCart(){ - if ( $this->isAddToCart()){ - $item = new UserSoldItem(); - $item->id_transfer = $this->_transfer->id_transfer; - $item->id_user = \Yii::$app->user->id; - $item->save(false); - } - } - - public function isAppendToUserCart(){ $result = false; if ( isset( $this->cart ) && $this->cart == 'user' ){ @@ -287,8 +280,11 @@ class TicketCreate extends Ticket{ } return $result; } - - + + + /** + * @throws \Exception + */ public function appendToUserCart(){ if ( $this->isAppendToUserCart() ){ $item = new UserSoldItem(); @@ -300,7 +296,10 @@ class TicketCreate extends Ticket{ } } } - + + /** + * @throws \Exception + */ public function appendToCustomerCart(){ if ( Transfer::canBeAddedToCart($this->payment_method)){ if ( $this->isAppendToCustomerCart() && isset($this->customer) ){ diff --git a/frontend/models/TransferSearch.php b/frontend/models/TransferSearch.php index 3a9ce72..c5d0aea 100644 --- a/frontend/models/TransferSearch.php +++ b/frontend/models/TransferSearch.php @@ -3,15 +3,10 @@ namespace frontend\models; use Yii; -use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Transfer; -use yii\base\Object; -use yii\db\Query; -use yii\db\Expression; use common\models\Account; -use yii\helpers\ArrayHelper; use common\components\Helper; /** * TransferSearch represents the model behind the search form about `common\models\Transfer`. @@ -96,23 +91,14 @@ class TransferSearch extends Transfer return $dataProvider; } - - public function totalsTransfers($params){ - $accountTotals = []; - $fullTotal = [ - 'label' => Yii::t('common/transfer', 'Total'), - 'money' => 0 - ]; - - + + /** + * @throws \yii\db\Exception + */ + public function totalsTransfers(){ $accounts = Account::find()->orderBy("name asc")->all(); - $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); $idUser = Yii::$app->user->id; - - - $this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap); - - + $this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts); } } diff --git a/frontend/models/TransferSearchToday.php b/frontend/models/TransferSearchToday.php index 8c9c0f9..b46af69 100644 --- a/frontend/models/TransferSearchToday.php +++ b/frontend/models/TransferSearchToday.php @@ -4,16 +4,9 @@ namespace frontend\models; use common\components\DateUtil; use Yii; -use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Transfer; -use yii\base\Object; -use yii\db\Query; -use yii\db\Expression; use common\models\Account; - -use yii\helpers\ArrayHelper; -use common\components\Helper; use yii\web\NotFoundHttpException; /** @@ -95,23 +88,14 @@ class TransferSearchToday extends Transfer return $dataProvider; } - - public function totalsTransfers($params){ - $accountTotals = []; - $fullTotal = [ - 'label' => Yii::t('common/transfer', 'Total'), - 'money' => 0 - ]; - - + + /** + * @throws \yii\db\Exception + */ + public function totalsTransfers(){ $accounts = Account::find()->orderBy("name asc")->all(); - $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); $idUser = Yii::$app->user->id; - - - $this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap); - - + $this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts); } }