door manager extra logging + docu improvment

This commit is contained in:
Schneider Roland 2022-11-25 19:25:29 +01:00
parent d432064b3d
commit 94364c2f56
4 changed files with 340 additions and 270 deletions

View File

@ -1,6 +1,28 @@
## Start the app
```bash
fitness_web/environments/cutlergyor/apache2
docker-compose up -d
```
Frontend
http://localhost:42001/cutler/frontend/web/index.php?r=site%2Flogin
Backend
http://localhost:42001/cutler/backend/web/index.php?r=site%2Flogin
Rest
[rest.http](rest.http)
Yii 2 Advanced Project Template Yii 2 Advanced Project Template
=============================== ===============================
Yii 2 Advanced Project Template is a skeleton [Yii 2](http://www.yiiframework.com/) application best for Yii 2 Advanced Project Template is a skeleton [Yii 2](http://www.yiiframework.com/) application best for
developing complex Web applications with multiple tiers. developing complex Web applications with multiple tiers.

View File

@ -530,4 +530,8 @@ class Helper {
return $result; return $result;
} }
public static function isRestAllowVerifyOnly() {
return \Yii::$app->params ['rest_allow_verify_only'] == true;
}
} }

View File

@ -15,6 +15,8 @@ use common\models\Ticket;
use common\models\VirtualKey; use common\models\VirtualKey;
use frontend\models\KeyToggleForm; use frontend\models\KeyToggleForm;
use yii\base\BaseObject; use yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\db\Exception;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
@ -42,11 +44,18 @@ class DoorManager extends BaseObject
*/ */
public function move($identifier, $device, $direction, $verifyOnly, $createdAt = null, $date = null) public function move($identifier, $device, $direction, $verifyOnly, $createdAt = null, $date = null)
{ {
$requestId = uniqid("",false);
$stopWatch = new StopWatch(); $stopWatch = new StopWatch();
\Yii::info("move with next parameers:" . ";identifier" . $identifier . ";device" . $device . ";direction" . $direction . ";verifyOnly" . $verifyOnly . ";createdAt" . print_r($createdAt,true) . ";date" . print_r($date,true)); // for testing purposes
\Yii::info("move get request: " . print_r($_GET, true)); if ( Helper::isRestAllowVerifyOnly() === false ){
\Yii::info("move post request: " . print_r($_GET, true)); \Yii::info("$requestId: verifyonly not allowed");
$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 get 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);
@ -61,7 +70,7 @@ class DoorManager extends BaseObject
} }
if ($device === 'E') { if ($device === 'E') {
$this->moveEmergency($identifier, $device, $direction, $verifyOnly, $createdAt, $date); $this->moveEmergency($requestId,$identifier, $device, $direction, $verifyOnly, $createdAt, $date);
return; return;
} }
@ -76,7 +85,7 @@ class DoorManager extends BaseObject
$direction = DoorLog::$DIRECTION_OUT; $direction = DoorLog::$DIRECTION_OUT;
break; break;
default: default:
throw new BadRequestHttpException("Direction not supported: " . $direction); throw new BadRequestHttpException("$requestId: Direction not supported: " . $direction);
} }
// if device is qr code // if device is qr code
@ -84,27 +93,27 @@ class DoorManager extends BaseObject
// allow only virtual key // allow only virtual key
$virtualKey = VirtualKey::findOne(['number' => $identifier]); $virtualKey = VirtualKey::findOne(['number' => $identifier]);
if (!isset($virtualKey)) { if (!isset($virtualKey)) {
throw new BadRequestHttpException("Virtual key not found: " . $identifier); throw new BadRequestHttpException("$requestId: Virtual key not found: " . $identifier);
} }
$card = Card::findOne($virtualKey->id_card); $card = Card::findOne($virtualKey->id_card);
if ($card != null) { if ($card != null) {
$card = Card::readCard($card->number); $card = Card::readCard($card->number);
} }
if ($card == null) { if ($card == null) {
throw new BadRequestHttpException("Card not found by virtual key: " . $identifier . '/' . $virtualKey->id_card); throw new BadRequestHttpException("$requestId: Card not found by virtual key: " . $identifier . '/' . $virtualKey->id_card);
} }
$cardNumber = $card->number; $cardNumber = $card->number;
\Yii::info("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 by rfid or card number
$card = Card::readCard(Helper::fixAsciiChars($identifier)); $card = Card::readCard(Helper::fixAsciiChars($identifier));
\Yii::info("Card loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: Card loaded in sec " . $stopWatch->split());
if (!isset($card)) { if (!isset($card)) {
throw new BadRequestHttpException('Card not found with number: ' . $identifier); throw new BadRequestHttpException("$requestId: Card not found with number: " . $identifier);
} }
\Yii::info("card loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: card loaded in sec " . $stopWatch->split());
$virtualKey = VirtualKey::findOne(['id_card' => $card->id_card]); $virtualKey = VirtualKey::findOne(['id_card' => $card->id_card]);
\Yii::info("virtual key for card loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: virtual key for card loaded in sec " . $stopWatch->split());
} }
@ -113,13 +122,13 @@ class DoorManager extends BaseObject
return; return;
} }
$this->moveCustomer($identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey); $this->moveCustomer($requestId, $identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey);
} }
function moveEmergency($identifier, $device, $direction, $verifyOnly, $createdAt, $date) function moveEmergency($requestId,$identifier, $device, $direction, $verifyOnly, $createdAt, $date)
{ {
\Yii::info("emergency move"); \Yii::info("$requestId: emergency move");
try { try {
$createdAtStr = DateUtil::formatDateTimeUtc($createdAt); $createdAtStr = DateUtil::formatDateTimeUtc($createdAt);
\Yii::$app->db->beginTransaction(); \Yii::$app->db->beginTransaction();
@ -188,6 +197,7 @@ class DoorManager extends BaseObject
} }
/** /**
* @param $requestId
* @param $identifier string virtual key id, card rfid or card number * @param $identifier string virtual key id, card rfid or card number
* @param $device string device * @param $device string device
* @param $direction number direction * @param $direction number direction
@ -200,19 +210,17 @@ class DoorManager extends BaseObject
* @return void * @return void
* @throws BadRequestHttpException * @throws BadRequestHttpException
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
* @throws \Throwable * @throws InvalidConfigException
* @throws \yii\base\InvalidConfigException * @throws Exception
* @throws \yii\db\Exception
* @throws \yii\db\StaleObjectException
*/ */
function moveCustomer($identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey) function moveCustomer($requestId, $identifier, $device, $direction, $verifyOnly, $createdAt, $date, $card, $cardNumber, $virtualKey)
{ {
\Yii::info("move customer"); \Yii::info("$requestId: move customer");
$stopWatch = new StopWatch(); $stopWatch = new StopWatch();
try { try {
$createdAtStr = DateUtil::formatDateTimeUtc($createdAt); $createdAtStr = DateUtil::formatDateTimeUtc($createdAt);
\Yii::info("crated at str: ". $createdAtStr); \Yii::info("$requestId: crated at str: ". $createdAtStr);
\Yii::$app->db->beginTransaction(); \Yii::$app->db->beginTransaction();
$doorLog = new DoorLog(); $doorLog = new DoorLog();
@ -225,7 +233,7 @@ class DoorManager extends BaseObject
$activeTickets = Ticket::readActive($card, clone $date); $activeTickets = Ticket::readActive($card, clone $date);
\Yii::info('active ticket count:' . count($activeTickets)); \Yii::info("$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) {
@ -233,54 +241,54 @@ class DoorManager extends BaseObject
} }
if (!isset($ticket)) { if (!isset($ticket)) {
throw new BadRequestHttpException("No active ticket found for:" . $card->number); throw new BadRequestHttpException("$requestId: No active ticket found for:" . $card->number);
} }
\Yii::info("ticket {$ticket->id_ticket} loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: ticket {$ticket->id_ticket} loaded in sec " . $stopWatch->split());
$doorLog->id_ticket_current = $ticket->id_ticket; $doorLog->id_ticket_current = $ticket->id_ticket;
// customer is also required // customer is also required
$customer = $card->customer; $customer = $card->customer;
if (!isset($customer)) { if (!isset($customer)) {
throw new BadRequestHttpException("Customer not found for:" . $card->number); throw new BadRequestHttpException("$requestId: Customer not found for:" . $card->number);
} }
$doorLog->id_customer = $customer->id_customer; $doorLog->id_customer = $customer->id_customer;
\Yii::info("customer {$customer->id_customer} loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: customer {$customer->id_customer} loaded in sec " . $stopWatch->split());
if (!$verifyOnly) { if (!$verifyOnly) {
// save the door log // save the door log
$doorLog->save(false); $doorLog->save(false);
} }
\Yii::info("door log {$doorLog->id_door_log} saved in sec " . $stopWatch->split()); \Yii::info("$requestId: door log {$doorLog->id_door_log} saved in sec " . $stopWatch->split());
// if direction is in // if direction is in
if ($direction == DoorLog::$DIRECTION_IN) { if ($direction == DoorLog::$DIRECTION_IN) {
if ($card->isFlagDoor()) { if ($card->isFlagDoor()) {
throw new BadRequestHttpException("Card already 'IN': " . $card->id_card); throw new BadRequestHttpException("$requestId: Card already 'IN': " . $card->id_card);
} }
if ($card->isFlagKey()) { if ($card->isFlagKey()) {
throw new BadRequestHttpException("Key required: " . $card->id_card); throw new BadRequestHttpException("$requestId: Key required: " . $card->id_card);
} }
if ($card->isFlagStatus()) { if ($card->isFlagStatus()) {
throw new BadRequestHttpException("Card has no active status: " . $card->id_card); throw new BadRequestHttpException("$requestId: Card has no active status: " . $card->id_card);
} }
if (isset($virtualKey)) { if (isset($virtualKey)) {
if (isset($virtualKey->direction_in_at)) { if (isset($virtualKey->direction_in_at)) {
throw new BadRequestHttpException("Virtual key - already moved in: " . $identifier . '/' . $virtualKey->id_card); throw new BadRequestHttpException("$requestId: Virtual key - already moved in: " . $identifier . '/' . $virtualKey->id_card);
} }
$virtualKey->direction_in_at = Helper::getDateTimeString(); $virtualKey->direction_in_at = Helper::getDateTimeString();
\Yii::info("Setting virtual key direction_in_at"); \Yii::info("$requestId: Setting virtual key direction_in_at");
if (!$verifyOnly) { if (!$verifyOnly) {
\Yii::info("Updating virtual key"); \Yii::info("$requestId: Updating virtual key");
$virtualKey->save(false); $virtualKey->save(false);
} }
} }
@ -289,13 +297,13 @@ class DoorManager extends BaseObject
if (!$verifyOnly) { if (!$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 $date)); $countDoorLogsForTicketSince = $this->getCountDoorLogsForTicketSince($ticket->id_ticket, DateUtil::utcDate(clone $date));
\Yii::info("getCountDoorLogsForTicketSince: " . $countDoorLogsForTicketSince); \Yii::info("$requestId: getCountDoorLogsForTicketSince: " . $countDoorLogsForTicketSince);
if (!isset($countDoorLogsForTicketSince)) { if (!isset($countDoorLogsForTicketSince)) {
$countDoorLogsForTicketSince = 0; $countDoorLogsForTicketSince = 0;
} }
\Yii::info("count of door logs '{$countDoorLogsForTicketSince}' loaded in sec " . $stopWatch->split()); \Yii::info("$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 == 1) {
@ -303,7 +311,7 @@ class DoorManager extends BaseObject
$usageCount = $ticket->usage_count; $usageCount = $ticket->usage_count;
$ticket->usage_count += 1; $ticket->usage_count += 1;
$ticket->save(false); $ticket->save(false);
\Yii::info("First ticket usage today, increasing usage count for card: " . $card->id_card); \Yii::info("$requestId: First ticket usage today, increasing usage count for card: " . $card->id_card);
Log::log( Log::log(
[ [
'type' => Log::$TYPE_TICKET_USAGE_FIRST, 'type' => Log::$TYPE_TICKET_USAGE_FIRST,
@ -312,10 +320,10 @@ class DoorManager extends BaseObject
'id_door_log' => $doorLog->id_door_log 'id_door_log' => $doorLog->id_door_log
] ]
); );
\Yii::info("Ticket usage count increased after first doorlog of day in sec " . $stopWatch->split()); \Yii::info("$requestId: Ticket usage count increased after first doorlog of day in sec " . $stopWatch->split());
} else { } else {
\Yii::info("more then one door log today for card: " . $card->id_card); \Yii::info("$requestId: more then one door log today for card: " . $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
@ -332,9 +340,9 @@ class DoorManager extends BaseObject
->andWhere(['in', 'direction', [DoorLog::$DIRECTION_IN_WITHOUT_MOVE, DoorLog::$DIRECTION_IN]]) ->andWhere(['in', 'direction', [DoorLog::$DIRECTION_IN_WITHOUT_MOVE, DoorLog::$DIRECTION_IN]])
->orderBy(['door_log.created_at' => SORT_ASC]) ->orderBy(['door_log.created_at' => SORT_ASC])
->all(); ->all();
\Yii::info("All door logs for today loaded in sec " . $stopWatch->split()); \Yii::info("$requestId: All door logs for today loaded in sec " . $stopWatch->split());
\Yii::info("allDoorLogToday", print_r($allDoorLogToday, true)); \Yii::info("$requestId: allDoorLogToday", print_r($allDoorLogToday, true));
if (isset($allDoorLogToday) && count($allDoorLogToday) > 0) { if (isset($allDoorLogToday) && count($allDoorLogToday) > 0) {
@ -343,7 +351,7 @@ class DoorManager extends BaseObject
if (isset($firstInToday)) { if (isset($firstInToday)) {
\Yii::info("first in today for card: " . $card->id_card . " was at " . $firstInToday->created_at); \Yii::info("$requestId: first in today for card: " . $card->id_card . " was at " . $firstInToday->created_at);
$firstEntryDateTimeToday = DateUtil::parseDateTime($firstInToday->created_at); $firstEntryDateTimeToday = DateUtil::parseDateTime($firstInToday->created_at);
@ -365,7 +373,7 @@ class DoorManager extends BaseObject
$activeInterval = $this->getActiveInterval($intervals, $createdAt); $activeInterval = $this->getActiveInterval($intervals, $createdAt);
if (!isset($activeInterval)) { if (!isset($activeInterval)) {
throw new ServerErrorHttpException("Active Interval not found"); throw new ServerErrorHttpException("$requestId: Active Interval not found");
} }
$logCountInActiveInterval = count($activeInterval['logs']); $logCountInActiveInterval = count($activeInterval['logs']);
@ -373,7 +381,7 @@ class DoorManager extends BaseObject
if ($logCountInActiveInterval == 1) { if ($logCountInActiveInterval == 1) {
$ticket->usage_count = $ticket->usage_count + 1; $ticket->usage_count = $ticket->usage_count + 1;
$ticket->save(false); $ticket->save(false);
\Yii::info("Ticket usage count increased after first IN after first door_log in interval in sec " . $stopWatch->split()); \Yii::info("$requestId: Ticket usage count increased after first IN after first door_log in interval in sec " . $stopWatch->split());
} }
} }
@ -385,27 +393,27 @@ class DoorManager extends BaseObject
if ($direction == DoorLog::$DIRECTION_OUT || $direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE) { if ($direction == DoorLog::$DIRECTION_OUT || $direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE) {
if ($card->isFlagOutKey()) { if ($card->isFlagOutKey()) {
throw new BadRequestHttpException("Can't exit with card has a key assigned"); throw new BadRequestHttpException("$requestId: Can't exit with card has a key assigned");
} }
if ($card->isFlagStatus()) { if ($card->isFlagStatus()) {
throw new BadRequestHttpException("Can't exit with card has inactive status"); throw new BadRequestHttpException("$requestId: Can't exit with card has inactive status");
} }
$keyAssigned = CardKeyAssignment::findOne(['id_card' => $card->id_card]); $keyAssigned = CardKeyAssignment::findOne(['id_card' => $card->id_card]);
if (isset($keyAssigned)) { if (isset($keyAssigned)) {
throw new BadRequestHttpException("Can't exit with card has a key assigned"); throw new BadRequestHttpException("$requestId: Can't exit with card has a key assigned");
} }
if (isset($virtualKey)) { if (isset($virtualKey)) {
if (!isset($virtualKey->direction_in_at)) { if (!isset($virtualKey->direction_in_at)) {
throw new BadRequestHttpException("Virtual key: move out without move in"); throw new BadRequestHttpException("$requestId: Virtual key: move out without move in");
} }
if (isset($virtualKey->direction_out_at)) { if (isset($virtualKey->direction_out_at)) {
throw new BadRequestHttpException("Virtual key: already move out"); throw new BadRequestHttpException("$requestId: Virtual key: already move out");
} }
$virtualKey->direction_out_at = Helper::getDateTimeString(); $virtualKey->direction_out_at = Helper::getDateTimeString();
@ -419,12 +427,12 @@ class DoorManager extends BaseObject
if (!$verifyOnly) { if (!$verifyOnly) {
$ticket->save(false); $ticket->save(false);
} }
\Yii::info("direction_out: ticket count_move_out set after direction_out in sec " . $stopWatch->split()); \Yii::info("$requestId: direction_out: ticket count_move_out set after direction_out in sec " . $stopWatch->split());
} }
if (!$verifyOnly) { if (!$verifyOnly) {
Card::updateCardFlagTicket($ticket->id_ticket); Card::updateCardFlagTicket($ticket->id_ticket);
\Yii::info("updateCardFlagTicket: card flag updated in sec " . $stopWatch->split()); \Yii::info("$requestId: updateCardFlagTicket: card flag updated in sec " . $stopWatch->split());
// reload card after flag is set // reload card after flag is set
$card = Card::readCard($cardNumber); $card = Card::readCard($cardNumber);
@ -432,22 +440,22 @@ class DoorManager extends BaseObject
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, true); $card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, true);
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, false); $card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, false);
$card->save(false); $card->save(false);
\Yii::info("direction_out: Door flag updated in sec " . $stopWatch->split()); \Yii::info("$requestId: direction_out: Door flag updated in sec " . $stopWatch->split());
} else if ($direction == DoorLog::$DIRECTION_IN || $direction == DoorLog::$DIRECTION_IN_WITHOUT_MOVE) { } else if ($direction == DoorLog::$DIRECTION_IN || $direction == DoorLog::$DIRECTION_IN_WITHOUT_MOVE) {
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, false); $card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, false);
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, true); $card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, true);
$card->save(false); $card->save(false);
\Yii::info("direction_in: Card flag updated in sec " . $stopWatch->split()); \Yii::info("$requestId: direction_in: Card flag updated in sec " . $stopWatch->split());
} }
} }
$stopWatch->stop(); $stopWatch->stop();
\Yii::info("finished in sec " . $stopWatch->getTotal()); \Yii::info("$requestId: finished in sec " . $stopWatch->getTotal());
\Yii::$app->db->transaction->commit(); \Yii::$app->db->transaction->commit();
\Yii::info("Commited"); \Yii::info("$requestId: Commited");
} catch (\Exception $e) { } catch (\Exception $e) {
\Yii::$app->db->transaction->rollBack(); \Yii::$app->db->transaction->rollBack();
\Yii::info("rollbacked"); \Yii::info("$requestId: rollbacked");
throw $e; throw $e;
} }
} }

View File

@ -1,4 +1,7 @@
CREATE TRIGGER trigger_inc_ticket_usage_count AFTER INSERT ON `door_log` FOR EACH ROW CREATE TRIGGER trigger_inc_ticket_usage_count
AFTER INSERT
ON `door_log`
FOR EACH ROW
begin begin
DECLARE p_count_all Integer; DECLARE p_count_all Integer;
@ -13,32 +16,42 @@ begin
delete from devlog; delete from devlog;
if NEW.version = 1
then
IF NEW.id_customer is not null and NEW.id_card is not null IF NEW.id_customer is not null and NEW.id_card is not null
THEN THEN
IF (NEW.direction = 7 or New.direction = 3 ) and NEW.id_ticket_current is not null IF (NEW.direction = 7 or New.direction = 3) and NEW.id_ticket_current is not null
then then
INSERT INTO devlog ( msg) values('belepes feldoglozas indit'); INSERT INTO devlog (msg) values ('belepes feldoglozas indit');
select count(*) into @p_count_all from door_log where created_at >= CURDATE() and id_ticket_current = New.id_ticket_current and ( direction = 7 or direction = 3); select count(*)
INSERT INTO devlog ( msg) values( concat( 'count all' ,@p_count_all ) ); into @p_count_all
from door_log
where created_at >= CURDATE()
and id_ticket_current = New.id_ticket_current
and (direction = 7 or direction = 3);
INSERT INTO devlog (msg) values (concat('count all', @p_count_all));
IF @p_count_all = 1 IF @p_count_all = 1
THEN THEN
select usage_count, max_usage_count into @p_usage_count ,@p_max_usage_count from ticket where id_ticket = NEW.id_ticket_current; select usage_count, max_usage_count
into @p_usage_count ,@p_max_usage_count
from ticket
where id_ticket = NEW.id_ticket_current;
update ticket set usage_count = usage_count +1 where id_ticket = NEW.id_ticket_current; update ticket set usage_count = usage_count + 1 where id_ticket = NEW.id_ticket_current;
INSERT INTO log (type,message, app, id_ticket, id_door_log,created_at, updated_at) INSERT INTO log (type, message, app, id_ticket, id_door_log, created_at, updated_at)
values( values (30, concat('Bérlet használat (elotte: ', @p_usage_count, ' > utana: ', @p_usage_count + 1,
30, concat('B<EFBFBD>rlet haszn<7A>lat (elotte: ',@p_usage_count, ' > utana: ' , @p_usage_count +1 , ' max: ', @p_max_usage_count, ')' ), ' trigger_inc_ticket',New.id_ticket_current, New.id_door_log,now(),now()); ' max: ', @p_max_usage_count, ')'), ' trigger_inc_ticket', New.id_ticket_current,
New.id_door_log, now(), now());
else else
-- deltaElsoBelépés a napi első door log és 'most' között eltelt órák -- deltaElsoBelépés a napi első door log és 'most' között eltelt órák
-- HOUR( TIMEDIFF( min(created_at) , now() ) ) -- HOUR( TIMEDIFF( min(created_at) , now() ) )
@ -48,28 +61,43 @@ begin
-- a napi első belépés után kiszámoljuk az aktuális n-edik 3órás intervallum kezdetét -- a napi első belépés után kiszámoljuk az aktuális n-edik 3órás intervallum kezdetét
select min(created_at) + INTERVAL (3 * FLOOR( ( ( HOUR( TIMEDIFF( min(created_at) , now() ) ) /3 ) ) ) ) hour as last_date select min(created_at) +
INTERVAL (3 * FLOOR(((HOUR(TIMEDIFF(min(created_at), now())) / 3)))) hour as last_date
into @p_from into @p_from
from door_log from door_log
where created_at > CURDATE() and id_customer is not null and id_ticket_current = NEW.id_ticket_current and ( direction = 7 or direction = 3); where created_at > CURDATE()
and id_customer is not null
and id_ticket_current = NEW.id_ticket_current
and (direction = 7 or direction = 3);
select count(*) into @p_count_all_2 from door_log where created_at >= @p_from and id_ticket_current = New.id_ticket_current and ( direction = 7 or direction = 3); select count(*)
into @p_count_all_2
from door_log
where created_at >= @p_from
and id_ticket_current = New.id_ticket_current
and (direction = 7 or direction = 3);
INSERT INTO devlog ( msg) values(CONCAT( 'Bel<EFBFBD>p<EFBFBD>sek sz<73>ma az aktu<74>lis 3 <20>r<EFBFBD>s intervalumban: ', @p_count_all_2) ); INSERT INTO devlog (msg)
values (CONCAT('Belépések száma az aktuális 3 órás intervalumban: ', @p_count_all_2));
IF @p_count_all_2 = 1 IF @p_count_all_2 = 1
THEN THEN
INSERT INTO devlog ( msg) values( 'Az aktu<74>lis intervallumban ez az els? bel<65>p<EFBFBD>s, usage_count n<>vel<65>se' ); INSERT INTO devlog (msg)
values ('Az aktuális intervallumban ez az első belépés, usage_count növelése');
select usage_count, max_usage_count into @p_usage_count ,@p_max_usage_count from ticket where id_ticket = NEW.id_ticket_current; select usage_count, max_usage_count
into @p_usage_count ,@p_max_usage_count
from ticket
where id_ticket = NEW.id_ticket_current;
update ticket set usage_count = usage_count +1 where id_ticket = New.id_ticket_current; update ticket set usage_count = usage_count + 1 where id_ticket = New.id_ticket_current;
INSERT INTO log (type,message, app, id_ticket, id_door_log,created_at, updated_at) INSERT INTO log (type, message, app, id_ticket, id_door_log, created_at, updated_at)
values( values (40, concat('Bérlet használat/egy nap tobbszori (elotte: ', @p_usage_count, ' > utana: ',
40, concat('B<EFBFBD>rlet haszn<7A>lat/egy nap tobbszori (elotte: ',@p_usage_count, ' > utana: ' , @p_usage_count +1 , ' max: ', @p_max_usage_count, ')' ), ' trigger_inc_ticket',New.id_ticket_current, New.id_door_log,now(),now()); @p_usage_count + 1, ' max: ', @p_max_usage_count, ')'), ' trigger_inc_ticket',
New.id_ticket_current, New.id_door_log, now(), now());
END IF; END IF;
END IF; END IF;
End IF; End IF;
@ -77,14 +105,14 @@ begin
IF NEW.direction = 5 or New.direction = 1 IF NEW.direction = 5 or New.direction = 1
then then
INSERT INTO devlog ( msg) values('Kil<EFBFBD>p<EFBFBD>s van folyamatban, kil<69>p<EFBFBD>sek sz<73>m<EFBFBD>nak be<62>ll<6C>t<EFBFBD>sa'); INSERT INTO devlog (msg) values ('Kilépés van folyamatban, kilépések számának beállítása');
update ticket set count_move_out = usage_count where id_ticket = NEW.id_ticket_current; update ticket set count_move_out = usage_count where id_ticket = NEW.id_ticket_current;
END IF; END IF;
INSERT INTO devlog ( msg) values( 'K<EFBFBD>rtya valid<69>ci<63> m<>dos<6F>t<EFBFBD>sa' ); INSERT INTO devlog (msg) values ('Kártya validáció módosítása');
UPDATE card as c1 UPDATE card as c1
left JOIN ( select ticket.id_card as id_card , max(ticket.id_ticket) as id_ticket left JOIN (select ticket.id_card as id_card, max(ticket.id_ticket) as id_ticket
from ticket from ticket
where ticket.start <= CURDATE() where ticket.start <= CURDATE()
and ticket.end >= curdate() and ticket.end >= curdate()
@ -92,19 +120,23 @@ begin
and ticket.count_move_out < ticket.max_usage_count and ticket.count_move_out < ticket.max_usage_count
and ticket.id_card = New.id_card and ticket.id_card = New.id_card
group by id_card group by id_card
order by id_card desc ) as t order by id_card desc) as t
on t.id_card = c1.id_card on t.id_card = c1.id_card
SET c1.validity = case when t.id_card is null then ( c1.validity | 1 << 0 ) else ( c1.validity & ~(1 << 0) ) end SET c1.validity = case
, c1.flag = case when t.id_card is null then ( c1.flag | 1 << 0 ) else ( c1.flag & ~(1 << 0) ) end when t.id_card is null then (c1.validity | 1 << 0)
else (c1.validity & ~(1 << 0)) end
, c1.flag = case when t.id_card is null then (c1.flag | 1 << 0) else (c1.flag & ~(1 << 0)) end
, c1.id_ticket_current = case when t.id_ticket is null then null else t.id_ticket end , c1.id_ticket_current = case when t.id_ticket is null then null else t.id_ticket end
WHERE c1.type <> 50 and c1.id_card = New.id_card; WHERE c1.type <> 50
and c1.id_card = New.id_card;
IF NEW.direction = 5 or New.direction = 1 IF NEW.direction = 5 or New.direction = 1
then then
/*
select max(ticket.id_ticket) into @p_mo_ticket_id
select max(ticket.id_ticket)
into @p_mo_ticket_id
from ticket from ticket
where ticket.start <= CURDATE() where ticket.start <= CURDATE()
and ticket.end >= curdate() and ticket.end >= curdate()
@ -113,26 +145,30 @@ begin
and ticket.id_card = New.id_card and ticket.id_card = New.id_card
group by id_card group by id_card
order by id_card desc; order by id_card desc;
*/
# set @p_allow_enter = true;
update card set set @p_allow_enter = true;
flag_out = ( flag_out | 1 << 1 ) ,
flag = ( flag & ~(1 << 1) )
WHERE type <> 50 and id_card = New.id_card; update card
set flag_out = (flag_out | 1 << 1),
flag = case when @p_allow_enter then (flag & ~(1 << 1)) else (flag | 1 << 1) end
WHERE type <> 50
and id_card = New.id_card;
END IF; END IF;
IF (NEW.direction = 7 or New.direction = 3 ) and NEW.id_ticket_current is not null IF (NEW.direction = 7 or New.direction = 3) and NEW.id_ticket_current is not null
THEN THEN
update card set update card
flag_out = ( flag_out & ~(1 << 1 ) ) , set flag_out = (flag_out & ~(1 << 1)),
flag = ( flag | 1 << 1 ) flag = (flag | 1 << 1)
WHERE type <> 50 and id_card = New.id_card; WHERE type <> 50
and id_card = New.id_card;
END IF; END IF;
END IF; END IF;
END IF;
END END