KeyDoorManager.php changes
This commit is contained in:
parent
2d53992a42
commit
0591f3780c
@ -17,6 +17,7 @@ use common\models\Log;
|
|||||||
use common\models\Ticket;
|
use common\models\Ticket;
|
||||||
use common\models\VirtualKey;
|
use common\models\VirtualKey;
|
||||||
use frontend\models\KeyToggleForm;
|
use frontend\models\KeyToggleForm;
|
||||||
|
use Yii;
|
||||||
use yii\base\BaseObject;
|
use yii\base\BaseObject;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\db\Exception;
|
use yii\db\Exception;
|
||||||
@ -40,6 +41,11 @@ class KeyDoorManager extends BaseObject
|
|||||||
* @param $createdAt number unix timestamp , override createdAt for doorLogs
|
* @param $createdAt number unix timestamp , override createdAt for doorLogs
|
||||||
* @param $date number unix timestamp, override date for validation check 'now'
|
* @param $date number unix timestamp, override date for validation check 'now'
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws BadRequestHttpException
|
||||||
|
* @throws Exception
|
||||||
|
* @throws FitnessException
|
||||||
|
* @throws InvalidConfigException
|
||||||
|
* @throws ServerErrorHttpException
|
||||||
*/
|
*/
|
||||||
public function move($identifier, $device, $direction, $verifyOnly, $createdAt = null, $date = null)
|
public function move($identifier, $device, $direction, $verifyOnly, $createdAt = null, $date = null)
|
||||||
{
|
{
|
||||||
@ -57,13 +63,13 @@ class KeyDoorManager extends BaseObject
|
|||||||
|
|
||||||
// for testing purposes
|
// for testing purposes
|
||||||
if (Helper::isRestAllowVerifyOnly() === false) {
|
if (Helper::isRestAllowVerifyOnly() === false) {
|
||||||
\Yii::info("$requestId: verifyonly not allowed");
|
Yii::info("$requestId: verifyonly not allowed");
|
||||||
$verifyOnly = false;
|
$verifyOnly = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
\Yii::info("$requestId: move with next parameers:" . ";identifier" . $identifier . ";device" . $device . ";direction" . $direction . ";verifyOnly" . $verifyOnly . ";createdAt" . print_r($createdAt, true) . ";date" . print_r($date, true));
|
Yii::info("$requestId: move with next parameers:" . ";identifier" . $identifier . ";device" . $device . ";direction" . $direction . ";verifyOnly" . $verifyOnly . ";createdAt" . print_r($createdAt, true) . ";date" . print_r($date, true));
|
||||||
\Yii::info("$requestId: move get request: " . print_r($_GET, true));
|
Yii::info("$requestId: move get request: " . print_r($_GET, true));
|
||||||
\Yii::info("$requestId: move post request: " . print_r($_GET, true));
|
Yii::info("$requestId: move post request: " . print_r($_GET, true));
|
||||||
|
|
||||||
if (isset($createdAt)) {
|
if (isset($createdAt)) {
|
||||||
$createdAt = DateUtil::parseDateTime($createdAt);
|
$createdAt = DateUtil::parseDateTime($createdAt);
|
||||||
@ -86,9 +92,7 @@ class KeyDoorManager extends BaseObject
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$cardNumber = $identifier;
|
$cardNumber = $identifier;
|
||||||
$virtualKey = null;
|
|
||||||
|
|
||||||
switch ($direction) {
|
switch ($direction) {
|
||||||
case 'IN':
|
case 'IN':
|
||||||
@ -109,9 +113,9 @@ class KeyDoorManager extends BaseObject
|
|||||||
|
|
||||||
// if device is qr code
|
// if device is qr code
|
||||||
if ($device == 'Q') {
|
if ($device == 'Q') {
|
||||||
// allow only virtual key
|
|
||||||
$virtualKey = VirtualKey::findOne(['number' => $identifier]);
|
|
||||||
|
|
||||||
|
// load virtual key by virtual_key.number
|
||||||
|
$virtualKey = VirtualKey::findOne(['number' => $identifier]);
|
||||||
if (!isset($virtualKey)) {
|
if (!isset($virtualKey)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"Virtual Key Not Found",
|
"Virtual Key Not Found",
|
||||||
@ -120,11 +124,10 @@ class KeyDoorManager extends BaseObject
|
|||||||
$context
|
$context
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load card by virutal key
|
||||||
$card = Card::findOne($virtualKey->id_card);
|
$card = Card::findOne($virtualKey->id_card);
|
||||||
if ($card != null) {
|
if (!isset($card)) {
|
||||||
$card = Card::readCard($card->number);
|
|
||||||
}
|
|
||||||
if ($card == null) {
|
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"Virtual Key Not Found",
|
"Virtual Key Not Found",
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
@ -133,11 +136,11 @@ class KeyDoorManager extends BaseObject
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
$cardNumber = $card->number;
|
$cardNumber = $card->number;
|
||||||
\Yii::info("$requestId: virtual key and card loaded in sec " . $stopWatch->split());
|
Yii::info("$requestId: virtual key and card loaded in sec " . $stopWatch->split());
|
||||||
} else {
|
} else {
|
||||||
// load by rfid or card number
|
// load card by rfid
|
||||||
$card = Card::readCard(Helper::fixAsciiChars($identifier));
|
$card = Card::readCard(Helper::fixAsciiChars($identifier));
|
||||||
\Yii::info("$requestId: Card loaded in sec " . $stopWatch->split());
|
Yii::info("$requestId: Card loaded in sec " . $stopWatch->split());
|
||||||
if (!isset($card)) {
|
if (!isset($card)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"Virtual Key Not Found",
|
"Virtual Key Not Found",
|
||||||
@ -146,27 +149,48 @@ class KeyDoorManager extends BaseObject
|
|||||||
$context
|
$context
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
\Yii::info("$requestId: card loaded in sec " . $stopWatch->split());
|
Yii::info("$requestId: card loaded in sec " . $stopWatch->split());
|
||||||
|
|
||||||
|
// load virtual key by card
|
||||||
$virtualKey = VirtualKey::findOne(['id_card' => $card->id_card]);
|
$virtualKey = VirtualKey::findOne(['id_card' => $card->id_card]);
|
||||||
\Yii::info("$requestId: virtual key for card loaded in sec " . $stopWatch->split());
|
if (!isset($virtualKey)) {
|
||||||
|
throw new FitnessException(
|
||||||
|
"Virtual Key Not Found",
|
||||||
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
|
"VIRTUAL_KEY_NOT_FOUND",
|
||||||
|
$context
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Yii::info("$requestId: virtual key for card loaded in sec " . $stopWatch->split());
|
||||||
}
|
}
|
||||||
|
|
||||||
$context->virtualKey = $virtualKey;
|
$context->virtualKey = $virtualKey;
|
||||||
$context->card = $card;
|
$context->card = $card;
|
||||||
|
$context->cardNumber = $cardNumber;
|
||||||
|
Yii::info("$requestId: Card number " . $card->number);
|
||||||
|
|
||||||
|
// check card status
|
||||||
|
if ($card->status !== Card::STATUS_ACTIVE) {
|
||||||
|
throw new FitnessException(
|
||||||
|
"Card Status is inactive",
|
||||||
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
|
"CARD_STATUS_INACTIVE",
|
||||||
|
$context
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// load assigned key
|
||||||
$keyAssignment = CardKeyAssignment::findOne(['id_card' => $card->id_card]);
|
$keyAssignment = CardKeyAssignment::findOne(['id_card' => $card->id_card]);
|
||||||
if (isset($keyAssignment)) {
|
if (isset($keyAssignment)) {
|
||||||
$context->key = Key::findOne($keyAssignment->id_key);
|
$context->key = Key::findOne($keyAssignment->id_key);
|
||||||
}
|
}
|
||||||
$context->cardNumber = $cardNumber;
|
|
||||||
|
|
||||||
\Yii::info("$requestId: Card number " . $card->number);
|
|
||||||
|
|
||||||
if ($card->type == Card::TYPE_EMPLOYEE) {
|
if ($card->type == Card::TYPE_EMPLOYEE) {
|
||||||
$this->moveEmployee($context /*$identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey*/);
|
$this->moveEmployee($context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->moveCustomer($context /* $requestId, $identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey*/);
|
$this->moveCustomer($context);
|
||||||
|
|
||||||
} catch (FitnessException $e) {
|
} catch (FitnessException $e) {
|
||||||
$context->error = true;
|
$context->error = true;
|
||||||
@ -197,7 +221,8 @@ class KeyDoorManager extends BaseObject
|
|||||||
* @param $ctx DoorMoveContext
|
* @param $ctx DoorMoveContext
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function logContext($ctx){
|
function logContext($ctx)
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$result = [
|
$result = [
|
||||||
//datetime $updated_at --
|
//datetime $updated_at --
|
||||||
@ -254,26 +279,26 @@ class KeyDoorManager extends BaseObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
\Yii::info("door log: " . implode(";", $result));
|
Yii::info("door log: " . implode(";", $result));
|
||||||
|
|
||||||
\Yii::$app->db->beginTransaction();
|
Yii::$app->db->beginTransaction();
|
||||||
$log = new DoorManagerLog($result);
|
$log = new DoorManagerLog($result);
|
||||||
$log->save(false);
|
$log->save(false);
|
||||||
\Yii::$app->db->transaction->commit();
|
Yii::$app->db->transaction->commit();
|
||||||
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\Yii::error("Failed to log door context:". $e->getMessage());
|
Yii::error("Failed to log door context:" . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveEmergency($ctx)
|
function moveEmergency($ctx)
|
||||||
{
|
{
|
||||||
$ctx->kind = "EMERGENCY";
|
$ctx->kind = "EMERGENCY";
|
||||||
\Yii::info("$ctx->requestId: emergency move");
|
Yii::info("$ctx->requestId: emergency move");
|
||||||
try {
|
try {
|
||||||
$createdAtStr = DateUtil::formatDateTimeUtc($ctx->createdAt);
|
$createdAtStr = DateUtil::formatDateTimeUtc($ctx->createdAt);
|
||||||
\Yii::$app->db->beginTransaction();
|
Yii::$app->db->beginTransaction();
|
||||||
|
|
||||||
$doorLog = new DoorLog();
|
$doorLog = new DoorLog();
|
||||||
$doorLog->version = 2;
|
$doorLog->version = 2;
|
||||||
@ -292,9 +317,9 @@ class KeyDoorManager extends BaseObject
|
|||||||
|
|
||||||
$ctx->actions[] = "EMERGENCY_MOVE";
|
$ctx->actions[] = "EMERGENCY_MOVE";
|
||||||
|
|
||||||
\Yii::$app->db->transaction->commit();
|
Yii::$app->db->transaction->commit();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\Yii::$app->db->transaction->rollBack();
|
Yii::$app->db->transaction->rollBack();
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"unknowon error",
|
"unknowon error",
|
||||||
FitnessException::UNKNOWN_ERROR,
|
FitnessException::UNKNOWN_ERROR,
|
||||||
@ -312,44 +337,7 @@ class KeyDoorManager extends BaseObject
|
|||||||
* if the card type is employee, neither customer nor ticket is needed.
|
* if the card type is employee, neither customer nor ticket is needed.
|
||||||
* Free to enter/leave
|
* Free to enter/leave
|
||||||
*/
|
*/
|
||||||
\Yii::info("employee move");
|
Yii::info("employee move");
|
||||||
try {
|
|
||||||
$createdAtStr = DateUtil::formatDateTimeUtc($ctx->createdAt);
|
|
||||||
\Yii::$app->db->beginTransaction();
|
|
||||||
|
|
||||||
$doorLog = new DoorLog();
|
|
||||||
$doorLog->version = 2;
|
|
||||||
$doorLog->direction = $ctx->direction;
|
|
||||||
$doorLog->source_app = $ctx->device;
|
|
||||||
$doorLog->created_at = $createdAtStr;
|
|
||||||
$doorLog->id_card = $ctx->card->id_card;
|
|
||||||
$doorLog->card_flag = $ctx->card->flag;
|
|
||||||
|
|
||||||
if (!$ctx->verifyOnly) {
|
|
||||||
|
|
||||||
$doorLog->save(false);
|
|
||||||
Log::log(
|
|
||||||
[
|
|
||||||
'type' => Log::$TYPE_INFO,
|
|
||||||
'message' => 'Ajtó nyitás: munkatárs',
|
|
||||||
'id_door_log' => $doorLog->id_door_log
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$ctx->actions[] = "EMPLOYEE_" . $ctx->originalDirection;
|
|
||||||
|
|
||||||
\Yii::$app->db->transaction->commit();
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
\Yii::$app->db->transaction->rollBack();
|
|
||||||
throw new FitnessException(
|
|
||||||
"unknowon error",
|
|
||||||
FitnessException::UNKNOWN_ERROR,
|
|
||||||
"EMPLOYEE_FAILED",
|
|
||||||
$ctx,
|
|
||||||
$e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,39 +345,26 @@ class KeyDoorManager extends BaseObject
|
|||||||
* @param $ctx
|
* @param $ctx
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
* @throws BadRequestHttpException
|
* @throws FitnessException
|
||||||
* @throws ServerErrorHttpException
|
|
||||||
* @throws InvalidConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
function moveCustomer($ctx/* $requestId, $identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey*/)
|
function moveCustomer($ctx/* $requestId, $identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey*/)
|
||||||
{
|
{
|
||||||
$ctx->kind = "CUSTOMER";
|
$ctx->kind = "CUSTOMER";
|
||||||
\Yii::info("$ctx->requestId: move customer");
|
Yii::info("$ctx->requestId: move customer");
|
||||||
$stopWatch = new StopWatch();
|
$stopWatch = new StopWatch();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$createdAtStr = DateUtil::formatDateTimeUtc($ctx->createdAt);
|
Yii::$app->db->beginTransaction();
|
||||||
\Yii::info("$ctx->requestId: crated at str: " . $createdAtStr);
|
|
||||||
\Yii::$app->db->beginTransaction();
|
|
||||||
|
|
||||||
$doorLog = new DoorLog();
|
|
||||||
$doorLog->version = 2;
|
|
||||||
$doorLog->direction = $ctx->direction;
|
|
||||||
$doorLog->source_app = $ctx->device;
|
|
||||||
$doorLog->created_at = $createdAtStr;
|
|
||||||
$doorLog->id_card = $ctx->card->id_card;
|
|
||||||
$doorLog->card_flag = $ctx->card->flag;
|
|
||||||
|
|
||||||
|
|
||||||
|
// load active ticket
|
||||||
$activeTickets = Ticket::readActive($ctx->card, clone $ctx->date);
|
$activeTickets = Ticket::readActive($ctx->card, clone $ctx->date);
|
||||||
\Yii::info("$ctx->requestId: active ticket count:" . count($activeTickets));
|
Yii::info("$ctx->requestId: active ticket count:" . count($activeTickets));
|
||||||
/** @var Ticket $ticket */
|
/** @var Ticket $ticket */
|
||||||
$ticket = null;
|
$ticket = null;
|
||||||
if (isset($activeTickets) && count($activeTickets) > 0) {
|
if (isset($activeTickets) && count($activeTickets) > 0) {
|
||||||
$ticket = $activeTickets[0];
|
$ticket = $activeTickets[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($ticket)) {
|
if (!isset($ticket)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"$ctx->requestId: No active ticket found for:" . $ctx->card->number,
|
"$ctx->requestId: No active ticket found for:" . $ctx->card->number,
|
||||||
@ -399,12 +374,9 @@ class KeyDoorManager extends BaseObject
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
$ctx->ticket = $ticket;
|
$ctx->ticket = $ticket;
|
||||||
|
Yii::info("$ctx->requestId: ticket {$ticket->id_ticket} loaded in sec " . $stopWatch->split());
|
||||||
|
|
||||||
\Yii::info("$ctx->requestId: ticket {$ticket->id_ticket} loaded in sec " . $stopWatch->split());
|
// load customer
|
||||||
|
|
||||||
$doorLog->id_ticket_current = $ticket->id_ticket;
|
|
||||||
|
|
||||||
// customer is also required
|
|
||||||
$customer = $ctx->card->customer;
|
$customer = $ctx->card->customer;
|
||||||
if (!isset($customer)) {
|
if (!isset($customer)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
@ -414,31 +386,15 @@ class KeyDoorManager extends BaseObject
|
|||||||
$ctx
|
$ctx
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ctx->customer = $customer;
|
$ctx->customer = $customer;
|
||||||
|
Yii::info("$ctx->requestId: customer {$customer->id_customer} loaded in sec " . $stopWatch->split());
|
||||||
|
|
||||||
$doorLog->id_customer = $customer->id_customer;
|
|
||||||
\Yii::info("$ctx->requestId: customer {$customer->id_customer} loaded in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
if (!$ctx->verifyOnly) {
|
|
||||||
// save the door log
|
|
||||||
$doorLog->save(false);
|
|
||||||
}
|
|
||||||
\Yii::info("$ctx->requestId: door log {$doorLog->id_door_log} saved in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
// if direction is in, check if usage count must be increased
|
// if direction is in, check if usage count must be increased
|
||||||
if ($ctx->direction == DoorLog::$DIRECTION_IN) {
|
if ($ctx->direction == DoorLog::$DIRECTION_IN) {
|
||||||
|
|
||||||
if ($ctx->card->isFlagDoor()) {
|
// Key required
|
||||||
throw new FitnessException(
|
if (!isset($ctx->key)) {
|
||||||
"$ctx->requestId: Card already 'IN': " . $ctx->card->id_card,
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"ALREADY_IN",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($ctx->card->isFlagKey()) {
|
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"$ctx->requestId: Key required: " . $ctx->card->id_card,
|
"$ctx->requestId: Key required: " . $ctx->card->id_card,
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
@ -447,17 +403,7 @@ class KeyDoorManager extends BaseObject
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ctx->card->isFlagStatus()) {
|
// virtual_key without entry required
|
||||||
throw new FitnessException(
|
|
||||||
"$ctx->requestId: Card has no active status: " . $ctx->card->id_card,
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"CARD_STATUS_NOT_ACTIVE",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($ctx->virtualKey)) {
|
|
||||||
|
|
||||||
if (isset($ctx->virtualKey->direction_in_at)) {
|
if (isset($ctx->virtualKey->direction_in_at)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"$ctx->requestId: Virtual key - already moved in: " . $ctx->identifier . '/' . $ctx->virtualKey->id_card,
|
"$ctx->requestId: Virtual key - already moved in: " . $ctx->identifier . '/' . $ctx->virtualKey->id_card,
|
||||||
@ -466,96 +412,57 @@ class KeyDoorManager extends BaseObject
|
|||||||
$ctx
|
$ctx
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ctx->virtualKey->direction_in_at = Helper::getDateTimeString();
|
$ctx->virtualKey->direction_in_at = Helper::getDateTimeString();
|
||||||
\Yii::info("$ctx->requestId: Setting virtual key direction_in_at");
|
Yii::info("$ctx->requestId: Setting virtual key direction_in_at");
|
||||||
|
|
||||||
$ctx->actions[] = "VIRTUAL_KEY_MOVE_IN";
|
$ctx->actions[] = "VIRTUAL_KEY_MOVE_IN";
|
||||||
|
|
||||||
if (!$ctx->verifyOnly) {
|
if (!$ctx->verifyOnly) {
|
||||||
\Yii::info("$ctx->requestId: Updating virtual key");
|
Yii::info("$ctx->requestId: Updating virtual key");
|
||||||
$ctx->virtualKey->save(false);
|
$ctx->virtualKey->save(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// detect if need to increase usage count for ticket
|
|
||||||
if (!$ctx->verifyOnly) {
|
|
||||||
// if not verifyonly, check, if ticket usage count must be increased
|
// if not verifyonly, check, if ticket usage count must be increased
|
||||||
$countDoorLogsForTicketSince = $this->getCountDoorLogsForTicketSince($ticket->id_ticket, DateUtil::utcDate(clone $ctx->date));
|
$allDoorLogToday = DoorManagerLog::findAllEntryForTicketFromTime($ctx->ticket->id_ticket);
|
||||||
\Yii::info("$ctx->requestId: getCountDoorLogsForTicketSince: " . $countDoorLogsForTicketSince);
|
$countDoorLogsForTicketSince = count($allDoorLogToday);
|
||||||
|
|
||||||
if (!isset($countDoorLogsForTicketSince)) {
|
$ctx->increasedTicketUsageCount = false;
|
||||||
$countDoorLogsForTicketSince = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
\Yii::info("$ctx->requestId: count of door logs '{$countDoorLogsForTicketSince}' loaded in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
// if the current event is the first door log today
|
// if the current event is the first door log today
|
||||||
if ($countDoorLogsForTicketSince == 1) {
|
if ($countDoorLogsForTicketSince == 0) {
|
||||||
// increase the ticket usage count with 1
|
|
||||||
$usageCount = $ticket->usage_count;
|
|
||||||
$ctx->increasedTicketUsageCount = true;
|
$ctx->increasedTicketUsageCount = true;
|
||||||
$ticket->usage_count += 1;
|
// $usageCount = $ticket->usage_count;
|
||||||
$ticket->save(false);
|
// $ticket->usage_count += 1;
|
||||||
\Yii::info("$ctx->requestId: First ticket usage today, increasing usage count for card: " . $ctx->card->id_card);
|
// $ticket->save(false);
|
||||||
Log::log(
|
// \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());
|
||||||
'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";
|
$ctx->actions[] = "TICKET_FIRST_USAGE_TODAY";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
\Yii::info("$ctx->requestId: more then one door log today for card: " . $ctx->card->id_card);
|
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
|
// 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.
|
// 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
|
// If the actual event happens in an interval, in which still now doorlog event happend, we increase
|
||||||
// the usage count with 1
|
// the usage count with 1
|
||||||
// 3 óránként 1-et levonunk
|
// 3 óránként 1-et levonunk
|
||||||
|
|
||||||
$startOfDay = DateUtil::utcDate(clone $ctx->date);
|
|
||||||
$startOfTomorrow = DateUtil::tomorrowStart(clone $ctx->date);
|
|
||||||
|
|
||||||
$allDoorLogToday = DoorLog::find()
|
|
||||||
->andWhere(['>=', 'door_log.created_at', DateUtil::formatDateUtc($startOfDay)])
|
|
||||||
->andWhere(['<', 'door_log.created_at', DateUtil::formatDateUtc($startOfTomorrow)])
|
|
||||||
->andWhere(['id_ticket_current' => $ticket->id_ticket])
|
|
||||||
->andWhere(['in', 'direction', [DoorLog::$DIRECTION_IN_WITHOUT_MOVE, DoorLog::$DIRECTION_IN]])
|
|
||||||
->orderBy(['door_log.created_at' => SORT_ASC])
|
|
||||||
->all();
|
|
||||||
\Yii::info("$ctx->requestId: All door logs for today loaded in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
\Yii::info("$ctx->requestId: allDoorLogToday", print_r($allDoorLogToday, true));
|
|
||||||
|
|
||||||
|
|
||||||
if (isset($allDoorLogToday) && count($allDoorLogToday) > 0) {
|
|
||||||
$firstInToday = $allDoorLogToday[0];
|
$firstInToday = $allDoorLogToday[0];
|
||||||
}
|
Yii::info("$ctx->requestId: first in today for card: " . $ctx->card->id_card . " was at " . $firstInToday->created_at);
|
||||||
|
|
||||||
if (isset($firstInToday)) {
|
|
||||||
|
|
||||||
\Yii::info("$ctx->requestId: first in today for card: " . $ctx->card->id_card . " was at " . $firstInToday->created_at);
|
|
||||||
|
|
||||||
$firstEntryDateTimeToday = DateUtil::parseDateTime($firstInToday->created_at);
|
$firstEntryDateTimeToday = DateUtil::parseDateTime($firstInToday->created_at);
|
||||||
|
|
||||||
$interval = \DateInterval::createFromDateString('3 hours');
|
$interval = \DateInterval::createFromDateString('3 hours');
|
||||||
|
$startOfTomorrow = DateUtil::tomorrowStart(clone $ctx->date);
|
||||||
$daterange = new \DatePeriod($firstEntryDateTimeToday, $interval, $startOfTomorrow);
|
$daterange = new \DatePeriod($firstEntryDateTimeToday, $interval, $startOfTomorrow);
|
||||||
|
|
||||||
$intervals = [];
|
$intervals = [];
|
||||||
$intervalStart = null;
|
$intervalStart = null;
|
||||||
foreach ($daterange as $intervalEnd) {
|
foreach ($daterange as $intervalEnd) {
|
||||||
if (isset($intervalStart)) {
|
if (isset($intervalStart)) {
|
||||||
$intervals[] = $this->createTicketUsageInterval($intervalStart, $intervalEnd, $allDoorLogToday, $doorLog);
|
$intervals[] = $this->createTicketUsageInterval($intervalStart, $intervalEnd, $allDoorLogToday);
|
||||||
}
|
}
|
||||||
$intervalStart = clone $intervalEnd;
|
$intervalStart = clone $intervalEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($intervalStart < $startOfTomorrow) {
|
if ($intervalStart < $startOfTomorrow) {
|
||||||
$intervals[] = $this->createTicketUsageInterval($intervalStart, $startOfTomorrow, $allDoorLogToday, $doorLog);
|
$intervals[] = $this->createTicketUsageInterval($intervalStart, $startOfTomorrow, $allDoorLogToday);
|
||||||
}
|
}
|
||||||
|
|
||||||
$activeInterval = $this->getActiveInterval($intervals, $ctx->createdAt);
|
$activeInterval = $this->getActiveInterval($intervals, $ctx->createdAt);
|
||||||
@ -565,23 +472,56 @@ class KeyDoorManager extends BaseObject
|
|||||||
|
|
||||||
$logCountInActiveInterval = count($activeInterval['logs']);
|
$logCountInActiveInterval = count($activeInterval['logs']);
|
||||||
|
|
||||||
if ($logCountInActiveInterval == 1) {
|
if ($logCountInActiveInterval == 0) {
|
||||||
$ctx->increasedTicketUsageCount = true;
|
$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";
|
$ctx->actions[] = "TICKET_INCREASE_USAGE";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($ctx->increasedTicketUsageCount === true) {
|
||||||
|
$ticket->usage_count = $ticket->usage_count + 1;
|
||||||
|
if ($ticket->usage_count > $ticket->max_usage_count) {
|
||||||
|
throw new FitnessException(
|
||||||
|
"$ctx->requestId: Ticket usage count exceeded",
|
||||||
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
|
"TICKET_MAX_USAGE_COUNT_EXCEEDED",
|
||||||
|
$ctx
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
if (!$ctx->verifyOnly) {
|
||||||
|
$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[] = "MOVE_IN";
|
$ctx->actions[] = "MOVE_IN";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ctx->direction == DoorLog::$DIRECTION_OUT || $ctx->direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE) {
|
if ($ctx->direction == DoorLog::$DIRECTION_OUT || $ctx->direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE) {
|
||||||
|
|
||||||
if ($ctx->card->isFlagOutKey()) {
|
// virtual key with entry required
|
||||||
|
if (!isset($ctx->virtualKey->direction_in_at)) {
|
||||||
|
throw new FitnessException(
|
||||||
|
"$ctx->requestId: Can't exit without move ind",
|
||||||
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
|
"VIRTUAL_KEY_MOVE_OUT_WITHOUT_MOVE_IN",
|
||||||
|
$ctx
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtual_key: only one move out is allowed
|
||||||
|
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,
|
||||||
|
"VIRTUAL_KEY_ALREADY_OUT",
|
||||||
|
$ctx
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// key must not be unassigned
|
||||||
|
if (isset($ctx->key)) {
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"$ctx->requestId: Can't exit with card has a key assigned",
|
"$ctx->requestId: Can't exit with card has a key assigned",
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
FitnessException::TYPE_BAD_REQUEST,
|
||||||
@ -590,93 +530,35 @@ class KeyDoorManager extends BaseObject
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ctx->card->isFlagStatus()) {
|
// set move_out datetime
|
||||||
throw new FitnessException(
|
|
||||||
"$ctx->requestId: Can't exit with card has inactive status",
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"CARD_STATUS_INACTIVE",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$keyAssigned = CardKeyAssignment::findOne(['id_card' => $ctx->card->id_card]);
|
|
||||||
|
|
||||||
if (isset($keyAssigned)) {
|
|
||||||
throw new FitnessException(
|
|
||||||
"$ctx->requestId: Can't exit with card has a key assigned",
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"CARD_LOCKER_KEY_ASSIGNED_KEY",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (isset($ctx->virtualKey)) {
|
|
||||||
\Yii::info("Move OUT: Virtual key set");
|
|
||||||
if (!isset($ctx->virtualKey->direction_in_at)) {
|
|
||||||
throw new FitnessException(
|
|
||||||
"$ctx->requestId: Virtual key: move out without move in",
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"VIRTUAL_KEY_MOVE_OUT_WITHOUT_MOVE_IN",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($ctx->virtualKey->direction_out_at)) {
|
|
||||||
throw new FitnessException(
|
|
||||||
"$ctx->requestId: Virtual key: already move out",
|
|
||||||
FitnessException::TYPE_BAD_REQUEST,
|
|
||||||
"VIRTUAL_KEY_ALREADY_OUT",
|
|
||||||
$ctx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$ctx->virtualKey->direction_out_at = Helper::getDateTimeString();
|
$ctx->virtualKey->direction_out_at = Helper::getDateTimeString();
|
||||||
|
// save virtual key
|
||||||
if (!$ctx->verifyOnly) {
|
if (!$ctx->verifyOnly) {
|
||||||
$ctx->virtualKey->save(false);
|
$ctx->virtualKey->save(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// set ticket move out counter
|
||||||
$ticket->count_move_out = $ticket->usage_count;
|
$ticket->count_move_out = $ticket->usage_count;
|
||||||
if (!$ctx->verifyOnly) {
|
if (!$ctx->verifyOnly) {
|
||||||
$ticket->save(false);
|
$ticket->save(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ctx->actions[] = "MOVE_OUT";
|
$ctx->actions[] = "MOVE_OUT";
|
||||||
\Yii::info("$ctx->requestId: direction_out: ticket count_move_out set after direction_out in sec " . $stopWatch->split());
|
Yii::info("$ctx->requestId: direction_out: ticket count_move_out set after direction_out in sec " . $stopWatch->split());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$ctx->verifyOnly) {
|
|
||||||
Card::updateCardFlagTicket($ticket->id_ticket);
|
|
||||||
\Yii::info("$ctx->requestId: updateCardFlagTicket: card flag updated in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
// reload card after flag is set
|
|
||||||
$ctx->card = Card::readCard($ctx->cardNumber);
|
|
||||||
if ($ctx->direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE || $ctx->direction == DoorLog::$DIRECTION_OUT) {
|
|
||||||
$ctx->card->flag_out = Helper::setBit($ctx->card->flag_out, Card::$FLAG_DOOR, true);
|
|
||||||
$ctx->card->flag = Helper::setBit($ctx->card->flag, Card::$FLAG_DOOR, false);
|
|
||||||
$ctx->card->save(false);
|
|
||||||
\Yii::info("$ctx->requestId: direction_out: Door flag updated in sec " . $stopWatch->split());
|
|
||||||
|
|
||||||
} else if ($ctx->direction == DoorLog::$DIRECTION_IN || $ctx->direction == DoorLog::$DIRECTION_IN_WITHOUT_MOVE) {
|
|
||||||
$ctx->card->flag_out = Helper::setBit($ctx->card->flag_out, Card::$FLAG_DOOR, false);
|
|
||||||
$ctx->card->flag = Helper::setBit($ctx->card->flag, Card::$FLAG_DOOR, true);
|
|
||||||
$ctx->card->save(false);
|
|
||||||
\Yii::info("$ctx->requestId: direction_in: Card flag updated in sec " . $stopWatch->split());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$stopWatch->stop();
|
$stopWatch->stop();
|
||||||
\Yii::info("$ctx->requestId: finished in sec " . $stopWatch->getTotal());
|
Yii::info("$ctx->requestId: finished in sec " . $stopWatch->getTotal());
|
||||||
\Yii::$app->db->transaction->commit();
|
Yii::$app->db->transaction->commit();
|
||||||
\Yii::info("$ctx->requestId: Commited");
|
Yii::info("$ctx->requestId: Commited");
|
||||||
} catch (FitnessException $fe) {
|
} catch (FitnessException $fe) {
|
||||||
$this->doRollback();
|
$this->doRollback();
|
||||||
\Yii::info("$ctx->requestId: rollbacked");
|
Yii::info("$ctx->requestId: rollbacked");
|
||||||
throw $fe;
|
throw $fe;
|
||||||
} catch (\Exception $e) {
|
} catch
|
||||||
|
(\Exception $e) {
|
||||||
$this->doRollback();
|
$this->doRollback();
|
||||||
\Yii::info("$ctx->requestId: rollbacked");
|
Yii::info("$ctx->requestId: rollbacked");
|
||||||
throw new FitnessException(
|
throw new FitnessException(
|
||||||
"UNKNOWN_ERROR",
|
"UNKNOWN_ERROR",
|
||||||
FitnessException::UNKNOWN_ERROR,
|
FitnessException::UNKNOWN_ERROR,
|
||||||
@ -688,9 +570,10 @@ class KeyDoorManager extends BaseObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function doRollback(){
|
function doRollback()
|
||||||
if ( isset(\Yii::$app->db->transaction ) && \Yii::$app->db->transaction->isActive ){
|
{
|
||||||
\Yii::$app->db->transaction->rollBack();
|
if (isset(Yii::$app->db->transaction) && Yii::$app->db->transaction->isActive) {
|
||||||
|
Yii::$app->db->transaction->rollBack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -706,7 +589,7 @@ class KeyDoorManager extends BaseObject
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createTicketUsageInterval($start, $end, $allLogs, $actualDoorLog)
|
function createTicketUsageInterval($start, $end, $allLogs)
|
||||||
{
|
{
|
||||||
$result = ['start' => $start, 'end' => $end, 'logs' => []];
|
$result = ['start' => $start, 'end' => $end, 'logs' => []];
|
||||||
foreach ($allLogs as $log) {
|
foreach ($allLogs as $log) {
|
||||||
@ -718,23 +601,6 @@ class KeyDoorManager extends BaseObject
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCountDoorLogsForTicketSince($idTicket, $since)
|
|
||||||
{
|
|
||||||
\Yii::info("getting door log count for today");
|
|
||||||
return DoorLog::find()
|
|
||||||
->innerJoinWith('card')
|
|
||||||
->andWhere(['card.id_ticket_current' => $idTicket])
|
|
||||||
->andWhere(['in', 'door_log.direction', [DoorLog::$DIRECTION_IN, DoorLog::$DIRECTION_IN_WITHOUT_MOVE]])
|
|
||||||
->andWhere(['>=', 'door_log.created_at', DateUtil::formatDateUtc($since)])
|
|
||||||
->count();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function readActive($cardNumber)
|
|
||||||
{
|
|
||||||
$card = Card::readCard($cardNumber);
|
|
||||||
return Ticket::readActive($card);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function resetLogs($cardNumber)
|
public function resetLogs($cardNumber)
|
||||||
{
|
{
|
||||||
$card = Card::readCard($cardNumber);
|
$card = Card::readCard($cardNumber);
|
||||||
@ -749,13 +615,6 @@ class KeyDoorManager extends BaseObject
|
|||||||
$this->revokeKey($cardNumber, "f100");
|
$this->revokeKey($cardNumber, "f100");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLogs($cardNumber)
|
|
||||||
{
|
|
||||||
return DoorLog::findAll(
|
|
||||||
['id_card' => $cardNumber]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getInfo($cardNumber)
|
public function getInfo($cardNumber)
|
||||||
{
|
{
|
||||||
$card = Card::readCard($cardNumber);
|
$card = Card::readCard($cardNumber);
|
||||||
@ -774,19 +633,19 @@ class KeyDoorManager extends BaseObject
|
|||||||
|
|
||||||
public function createLog()
|
public function createLog()
|
||||||
{
|
{
|
||||||
\Yii::info("Post create log:" . \Yii::$app->request->method);
|
Yii::info("Post create log:" . Yii::$app->request->method);
|
||||||
if (\Yii::$app->request->isPost) {
|
if (Yii::$app->request->isPost) {
|
||||||
$log = new DoorLogForTest();
|
$log = new DoorLogForTest();
|
||||||
if ($log->load(\Yii::$app->request->post(), "")) {
|
if ($log->load(Yii::$app->request->post(), "")) {
|
||||||
if ($log->validate()) {
|
if ($log->validate()) {
|
||||||
\Yii::info("Door log saving:" . $log->created_at);
|
Yii::info("Door log saving:" . $log->created_at);
|
||||||
$log->save(false);
|
$log->save(false);
|
||||||
return $log;
|
return $log;
|
||||||
} else {
|
} else {
|
||||||
throw new BadRequestHttpException(print_r($log->getErrors(), true));
|
throw new BadRequestHttpException(print_r($log->getErrors(), true));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
\Yii::info("validated" . print_r($log->errors, true));
|
Yii::info("validated" . print_r($log->errors, true));
|
||||||
throw new BadRequestHttpException();
|
throw new BadRequestHttpException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user