improve event deletion and event registration cancelation
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user