improve cutlergyor move

This commit is contained in:
Schneider Roland
2023-05-11 18:20:56 +02:00
parent 0591f3780c
commit 0d7002b173
6 changed files with 22 additions and 88 deletions

View File

@@ -29,7 +29,7 @@ use yii\web\ServerErrorHttpException;
* Date: 2018.12.17.
* Time: 6:12
*/
class DoorCardPassDoorManager extends BaseObject
class CutlerMovarDoorManager extends BaseObject
{
/**
@@ -139,7 +139,9 @@ class DoorCardPassDoorManager extends BaseObject
);
}
\Yii::info("$requestId: card loaded in sec " . $stopWatch->split());
$virtualKey = VirtualKey::findOne(['id_card' => $card->id_card]);
$virtualKey = VirtualKey::find()
->andWhere(['id_card' => $card->id_card])
->orderBy(['id' => SORT_DESC])->one();
\Yii::info("$requestId: virtual key for card loaded in sec " . $stopWatch->split());
}
@@ -454,87 +456,18 @@ class DoorCardPassDoorManager extends BaseObject
// detect if need to increase usage count for ticket
if (!$ctx->verifyOnly) {
$ctx->virtualKey->direction_in_at = Helper::getDateTimeString();
\Yii::info("$ctx->requestId: Setting virtual key direction_in_at");
\Yii::info("$ctx->requestId: Updating virtual key");
$ctx->virtualKey->save(false);
$allDoorLogToday = DoorManagerLog::findAllEntryForTicketFromTime($ctx->ticket->id_ticket);
$countDoorLogsForTicketSince = count($allDoorLogToday);
// if the current event is the first door log today
if ($countDoorLogsForTicketSince == 0) {
// increase the ticket usage count with 1
$usageCount = $ticket->usage_count;
$ctx->increasedTicketUsageCount = true;
$ticket->usage_count += 1;
$ticket->save(false);
\Yii::info("$ctx->requestId: First ticket usage today, increasing usage count for card: " . $ctx->card->id_card);
Log::log(
[
'type' => Log::$TYPE_TICKET_USAGE_FIRST,
'message' => 'Bérlet használat(előtte: ' . $usageCount . ' -> utána: ' . $ticket->usage_count,
'id_ticket' => $ticket->id_ticket,
'id_door_log' => $doorLog->id_door_log
]
);
\Yii::info("$ctx->requestId: Ticket usage count increased after first doorlog of day in sec " . $stopWatch->split());
$ctx->actions[] = "TICKET_FIRST_USAGE_TODAY";
} else {
\Yii::info("$ctx->requestId: more then one door log today for card: " . $ctx->card->id_card);
// we have already a door log for today, other than this
// Now we split the day into 3hour intervalls, starting with the createdAt value of the first event.
// If the actual event happens in an interval, in which still now doorlog event happend, we increase
// the usage count with 1
// 3 óránként 1-et levonunk
if (count($allDoorLogToday) == 0) {
throw new FitnessException(
"$ctx->requestId: Tried to inc ticket usage without prev entry: " . $ctx->identifier . '/' . $ctx->virtualKey->id_card,
FitnessException::TYPE_BAD_REQUEST,
"INVALID_TICKET_USAGE_INCREMENT",
$ctx
);
}
$firstInToday = $allDoorLogToday[0];
\Yii::info("$ctx->requestId: first in today for card: " . $ctx->card->id_card . " was at " . $firstInToday->created_at);
$firstEntryDateTimeToday = DateUtil::parseDateTime($firstInToday->created_at);
$interval = \DateInterval::createFromDateString('3 hours');
$startOfTomorrow = DateUtil::tomorrowStart(clone $ctx->date);
$daterange = new \DatePeriod($firstEntryDateTimeToday, $interval, $startOfTomorrow);
$intervals = [];
$intervalStart = null;
foreach ($daterange as $intervalEnd) {
if (isset($intervalStart)) {
$intervals[] = $this->createTicketUsageInterval($intervalStart, $intervalEnd, $allDoorLogToday, $doorLog);
}
$intervalStart = clone $intervalEnd;
}
if ($intervalStart < $startOfTomorrow) {
$intervals[] = $this->createTicketUsageInterval($intervalStart, $startOfTomorrow, $allDoorLogToday, $doorLog);
}
$activeInterval = $this->getActiveInterval($intervals, $ctx->createdAt);
if (!isset($activeInterval)) {
throw new ServerErrorHttpException("$ctx->requestId: Active Interval not found");
}
$logCountInActiveInterval = count($activeInterval['logs']);
if ($logCountInActiveInterval == 1) {
$ctx->increasedTicketUsageCount = true;
$ticket->usage_count = $ticket->usage_count + 1;
$ticket->save(false);
\Yii::info("$ctx->requestId: Ticket usage count increased after first IN after first door_log in interval in sec " . $stopWatch->split());
$ctx->actions[] = "TICKET_INCREASE_USAGE";
}
}
// increase the ticket usage count with 1
$usageCount = $ticket->usage_count;
$ctx->increasedTicketUsageCount = true;
$ticket->usage_count += 1;
$ticket->save(false);
\Yii::info("$ctx->requestId: First ticket usage today, increasing usage count for card: " . $ctx->card->id_card);
\Yii::info("$ctx->requestId: Ticket usage count increased after first doorlog of day in sec " . $stopWatch->split());
}
$ctx->actions[] = "MOVE_IN";
}

View File

@@ -511,7 +511,7 @@ class KeyDoorManager extends BaseObject
}
// virtual_key: only one move out is allowed
if (!isset($ctx->virtualKey->direction_out_at)) {
if (isset($ctx->virtualKey->direction_out_at)) {
throw new FitnessException(
"$ctx->requestId: virtual key already out. Only one move out allowed.",
FitnessException::TYPE_BAD_REQUEST,
@@ -559,6 +559,7 @@ class KeyDoorManager extends BaseObject
(\Exception $e) {
$this->doRollback();
Yii::info("$ctx->requestId: rollbacked");
Yii::error($e->getMessage());
throw new FitnessException(
"UNKNOWN_ERROR",
FitnessException::UNKNOWN_ERROR,

View File

@@ -74,7 +74,7 @@ class DoorManagerLog extends \yii\db\ActiveRecord
static function findAllEntryForTicketFromTime($idTicket, $datetime = null){
return DoorManagerLog::find()
->andWhere(['id_ticket' =>$idTicket])
->andWhere(['ticket_id_ticket' =>$idTicket])
->andWhere(['verify_only' => false])
->andWhere(['direction' => 'IN'])
->andWhere(['>=' ,'created_at', isset( $datetime ) ? $datetime : date('Y-m-d H:i:s')])