diff --git a/common/manager/EventRegistrationManager.php b/common/manager/EventRegistrationManager.php index b965b25..c55c095 100644 --- a/common/manager/EventRegistrationManager.php +++ b/common/manager/EventRegistrationManager.php @@ -194,12 +194,11 @@ class EventRegistrationManager extends BaseObject */ public function loadRegistration($idRegistration, $idCustomer) { - - $query = EventRegistration::find() + $query = EventRegistration::find() ->andWhere(['id' => $idRegistration]); - if ( isset($idCustomer)){ - $query->andWhere(['id_customer' => $idCustomer]) - } + if (isset($idCustomer)) { + $query->andWhere(['id_customer' => $idCustomer]); + } $registration = $query->one(); if ($registration === null) { throw new NotFoundHttpException('The requested registration does not exist.'); @@ -207,6 +206,18 @@ class EventRegistrationManager extends BaseObject return $registration; } + public function cancelRegistrationTX($registration, $idCustomer, $reason) + { + $tx = \Yii::$app->db->beginTransaction(); + try { + $this->cancelRegistration($registration, $idCustomer, $reason); + $tx->commit(); + } catch (\Exception $e) { + $tx->rollBack(); + throw $e; + } + } + /** * @param EventRegistration $registration * @throws ServerErrorHttpException @@ -230,7 +241,7 @@ class EventRegistrationManager extends BaseObject if (!isset($event)) { throw new BadRequestHttpException('The reservation is already deleted', self::EVENT_NOT_FOUND); } - $tx = \Yii::$app->db->beginTransaction(); + $now = strtotime("now UTC"); if ($reason != EventRegistration::CANCEL_REASON_CUSTOMER) { @@ -240,51 +251,43 @@ class EventRegistrationManager extends BaseObject } } - try { - + $now = strtotime("now"); + // if user cancels then cancel_at will be set + if ( $reason == EventRegistration::CANCEL_REASON_CUSTOMER){ $registration->canceled_at = date('Y-m-d H:i:s', $now); - $registration->save(false); - - $ticket = Ticket::findOne($registration->id_ticket); - - if (!isset($ticket)) { - throw new BadRequestHttpException('The ticket is not found'); - } - - $ticket->restoreReservationCount(1); - $ticket->save(false); - - $tx->commit(); - } catch (\Exception $e) { - $tx->rollBack(); - throw $e; + }else{ + // otherwise ( e.g. event is deleted ) the delete_at will be set + $registration->deleted_at = date('Y-m-d H:i:s', $now); } + $registration->save(false); + $ticket = Ticket::findOne($registration->id_ticket); + + if (!isset($ticket)) { + throw new BadRequestHttpException('The ticket is not found'); + } + + $ticket->restoreReservationCount(1); + $ticket->save(false); + } -// /** -// * @param EventRegistration $registration -// * @return bool -// * @throws \yii\base\Exception -// */ -// public function deleteRegistration($registration) -// { -// if (isset($registration->deleted_at)) { -// return false; -// } -// -//// $ticket = Ticket::findOne(['id_ticket' => $registration->id_ticket]); -//// if( !isset($ticket ) ) { -//// throw new \yii\base\Exception('Ticket not found: ' . $registration->id_ticket); -//// } -//// -//// $ticket->restoreReservationCount(1); -//// $ticket->save(false); -// -// $registration->deleted_at = date('Y-m-d H:i:s'); -// return $registration->save(false); -// -// } + public function findAllRegistrations($idEvent, $idCustomer = null) + { + $registrations = + EventRegistration::find() + ->andWhere(['id_event' => $idEvent])->all(); + if (isset($idCustomer)) { + $registrations = EventRegistration::filterForCustomer($registrations, $idCustomer); + } + return $registrations; + } + + public function findActiveRegistrations($idEvent, $idCustomer = null) + { + $registrations = $this->findAllRegistrations($idEvent, $idCustomer); + return EventRegistration::filterActive($registrations); + } /** * Delete an event @@ -298,12 +301,15 @@ class EventRegistrationManager extends BaseObject $tx = $db->beginTransaction(); try { - $registrations = $event->getEventRegistrations()->all(); + $eventRegistrationManager = new EventRegistrationManager(); + $allRegistrations = $eventRegistrationManager->findAllRegistrations($event->id); + $activeRegistrations = $eventRegistrationManager->findActiveRegistrations($event->id); + // //////////////////////////////// // if event has no registrations // we can simply delete it from db // //////////////////////////////// - if (count($registrations) === 0) { + if (count($allRegistrations) === 0) { $event->delete(); } else { // ///////////////////////////// @@ -312,7 +318,7 @@ class EventRegistrationManager extends BaseObject // ///////////////////////////// $event->deleted_at = date('Y-m-d H:i:s'); $event->save(false); - foreach ($registrations as $registration) { + foreach ($activeRegistrations as $registration) { if (!isset($registration->deleted_at)) { /** @var EventRegistration $registration */ $this->cancelRegistration($registration, null, EventRegistration::CANCEL_REASON_EVENT); diff --git a/common/modules/event/controllers/EventController.php b/common/modules/event/controllers/EventController.php index b8a5d9b..623c7c0 100644 --- a/common/modules/event/controllers/EventController.php +++ b/common/modules/event/controllers/EventController.php @@ -230,7 +230,7 @@ class EventController extends Controller $tx = $db->beginTransaction(); try { $registration = $eventRegistrationManager->loadRegistration($id,null); - $eventRegistrationManager->cancelRegistration($registration,null,EventRegistration::CANCEL_REASON_CUSTOMER); + $eventRegistrationManager->cancelRegistrationTX($registration,null,EventRegistration::CANCEL_REASON_CUSTOMER); $tx->commit(); return $this->redirect(['view', 'id' => $registration->id_event]); } catch (Exception $ex) { diff --git a/common/modules/event/views/event/index.php b/common/modules/event/views/event/index.php index 186c6a2..8b7e45b 100644 --- a/common/modules/event/views/event/index.php +++ b/common/modules/event/views/event/index.php @@ -153,6 +153,9 @@ $indexTableTemplateButtons[] = '{reserve-card}'; 'aria-label' => Yii::t('yii', 'Register'), 'data-pjax' => '0', ]; + if ( isset($model['event_deleted_at']) ){ + return ""; + } return Html::a('', $url, $options); }, // 'equipment-types-assignment' => function ($url, $model, $key) { diff --git a/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts b/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts index 01b0886..8d4d2be 100644 --- a/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts +++ b/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts @@ -1,6 +1,6 @@ import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Event} from "../../services/event.service"; -import { MonthCalendarEvent} from "../month-calendar/month-calendar.component"; +import {MonthCalendarEvent} from "../month-calendar/month-calendar.component"; @Component({ selector: 'app-month-calendar-event', @@ -38,7 +38,7 @@ export class MonthCalendarEventComponent implements OnInit { } maySelect() { - return this.isRegistered() ? true : this.event.hasFreeSeats; + return this.isRegistered() ? true : this.event.hasFreeSeats; } isRegistered() { @@ -46,27 +46,32 @@ export class MonthCalendarEventComponent implements OnInit { } isDisabled() { - return !this.isRegistered() && !this.event.hasFreeSeats; + return !this.isRegistered() && !this.event.hasFreeSeats; } getEventThemeClass(): string { const theme = this.event?.eventType?.theme; let styleClass = "bg-dark"; - if (this.isDisabled()) { + if (this.event.deleted_at) { styleClass = "bg-secondary"; } else { - if (theme != null && theme.length > 0) { - if (this.isRegistered()) { - styleClass += " event-theme-active-" + theme; - } else { - styleClass += " event-theme-normal-" + theme; - } + + if (this.isDisabled()) { + styleClass = "bg-secondary"; } else { - if (this.isRegistered()) { - styleClass += " bg-primary"; + if (theme != null && theme.length > 0) { + if (this.isRegistered()) { + styleClass += " event-theme-active-" + theme; + } else { + styleClass += " event-theme-normal-" + theme; + } } else { - styleClass += " bg-dark"; + if (this.isRegistered()) { + styleClass += " bg-primary"; + } else { + styleClass += " bg-dark"; + } } } } diff --git a/customer/app/src/app/pages/event-details/event-details.component.html b/customer/app/src/app/pages/event-details/event-details.component.html index 2b15954..3e8f610 100644 --- a/customer/app/src/app/pages/event-details/event-details.component.html +++ b/customer/app/src/app/pages/event-details/event-details.component.html @@ -29,6 +29,10 @@