diff --git a/backend/controllers/CardController.php b/backend/controllers/CardController.php index 78b57de..d3c9ce9 100644 --- a/backend/controllers/CardController.php +++ b/backend/controllers/CardController.php @@ -391,13 +391,13 @@ class CardController extends \backend\controllers\BackendController { \Yii::info("Tickets updated: " . $result ); \Yii::$app->session->setFlash('success', 'Módosított bérletek száma: ' . $result); - $command = $connection->createCommand( Card::$SQL_CLEARS_STATUS_DOOR ); - $result = $command->execute(); - \Yii::info("Door flag cleared: " . $result ); - - $command = $connection->createCommand( Card::$SQL_FIX_KEY_STATUS ); - $result = $command->execute(); - \Yii::info("Key flag fixed: " . $result ); +// $command = $connection->createCommand( Card::$SQL_CLEARS_STATUS_DOOR ); +// $result = $command->execute(); +// \Yii::info("Door flag cleared: " . $result ); +// +// $command = $connection->createCommand( Card::$SQL_FIX_KEY_STATUS ); +// $result = $command->execute(); +// \Yii::info("Key flag fixed: " . $result ); diff --git a/changelog.txt b/changelog.txt index c623f54..558a8ca 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,5 @@ +-0.0.91 + - invalidate door log changes -0.0.90 - add new trigger, flag/flag_out changes changes - flag state page for reception diff --git a/common/config/params.php b/common/config/params.php index c61348d..e59c152 100644 --- a/common/config/params.php +++ b/common/config/params.php @@ -5,7 +5,7 @@ return [ 'supportEmail' => 'rocho02@gmail.com', 'infoEmail' => 'info@rocho-net.hu', 'user.passwordResetTokenExpire' => 3600, - 'version' => 'v0.0.90', + 'version' => 'v0.0.91', 'company' => 'movar',//gyor 'company_name' => "Freimann Kft.", 'product_visiblity' => 'account',// on reception which products to display. account or global diff --git a/common/models/Card.php b/common/models/Card.php index 7b87adc..cbec3cb 100644 --- a/common/models/Card.php +++ b/common/models/Card.php @@ -225,9 +225,9 @@ class Card extends \common\models\BaseFitnessActiveRecord public function setFlagsHasKey($hasKey){ $pos = Card::$FLAG_KEY; - $this->flag = $hasKey ? ( $this->flag & ~(1 << $pos) ) : ( $this->flag | 1 << $pos ); - $this->flag_out = $hasKey ? ( $this->flag_out | 1 << $pos ) : ( $this->flag_out & ~(1 << $pos) ); - \Yii::info("flag has key: ".$this->flag .";".$this->flag_out); +// $this->flag = $hasKey ? ( $this->flag & ~(1 << $pos) ) : ( $this->flag | 1 << $pos ); +// $this->flag_out = $hasKey ? ( $this->flag_out | 1 << $pos ) : ( $this->flag_out & ~(1 << $pos) ); +// \Yii::info("flag has key: ".$this->flag .";".$this->flag_out); } diff --git a/console/controllers/TicketController.php b/console/controllers/TicketController.php index a063c97..0df5eef 100644 --- a/console/controllers/TicketController.php +++ b/console/controllers/TicketController.php @@ -25,13 +25,13 @@ class TicketController extends Controller { $result = $command->execute (); \Yii::info ( "Tickets updated flag door: " . $result ); - $command = $connection->createCommand ( Ticket::$SQL_UPDATE ); - $result = $command->execute (); - \Yii::info ( "Tickets updated: " . $result ); - - - $command = $connection->createCommand ( Card::$SQL_FIX_KEY_STATUS ); - $result = $command->execute (); +// $command = $connection->createCommand ( Ticket::$SQL_UPDATE ); +// $result = $command->execute (); +// \Yii::info ( "Tickets updated: " . $result ); +// +// +// $command = $connection->createCommand ( Card::$SQL_FIX_KEY_STATUS ); +// $result = $command->execute (); } /** diff --git a/trigger_door_log.sql b/trigger_door_log.sql new file mode 100644 index 0000000..db39a9e --- /dev/null +++ b/trigger_door_log.sql @@ -0,0 +1,182 @@ +DROP TRIGGER IF EXISTS trigger_inc_ticket_usage_count; +DELIMITER $$ +CREATE TRIGGER trigger_inc_ticket_usage_count +AFTER INSERT ON `door_log` FOR EACH ROW +begin + /*DECLARE p_usage_count Integer;*/ + DECLARE p_count_all Integer; + DECLARE p_count_all_2 Integer; + DECLARE p_from DATETIME; + DECLARE p_usage_count Integer; + DECLARE p_max_usage_count Integer; + DECLARE p_mo_ticket_id Integer; -- ticket id at moving out + DECLARE p_mo_ticket_max_usage_count Integer; -- ticket max usage count at moving out + DECLARE p_allow_multiple_enter boolean; -- flag, if we allow multiple enter per day + DECLARE p_allow_enter boolean; + -- fejlesztői log törlése + delete from devlog; + + /*van vendég és kártya*/ + IF NEW.id_customer is not null and NEW.id_card is not null + THEN + -- BEMOZÁG folyamatban - érvényes bérlettel + IF (NEW.direction = 7 or New.direction = 3 ) and NEW.id_ticket_current is not null + then + INSERT INTO devlog ( msg) values('belepes feldoglozas indit'); + /*Mai belépések számának betöltése */ + 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); + INSERT INTO devlog ( msg) values( concat( 'count all' ,@p_count_all ) ); + + /*Ha a main napon ez az első belépés*/ + IF @p_count_all = 1 + THEN + /*Beolvasás usage_count és max_usage_count */ + select usage_count, max_usage_count into @p_usage_count ,@p_max_usage_count from ticket where id_ticket = NEW.id_ticket_current; + + /*Növeljük a usage count-ot a bérleten*/ + update ticket set usage_count = usage_count +1 where id_ticket = NEW.id_ticket_current; + + /*Log írása*/ + INSERT INTO log (type,message, app, id_ticket, id_door_log,created_at, updated_at) + values( + 30, concat('Bérlet haszná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()); + else + /* + Innentől kezdve biztos, hogy nem ez az első belépés az aktuális napon. + Ki kell számolnunk hányadik 3 órás intervallumban vagyunk az első belépéstől számítva. + Pl. ha 06:00 kor volt az első belépés, akkor 07: 30 még nem von le újabb használatot, + de a 09:00 már igen + + ( HOUR( TIMEDIFF( min(created_at) , now() ) ) /3 ) -> napi első b + */ + select min(created_at) + INTERVAL (3 * FLOOR( ( ( HOUR( TIMEDIFF( min(created_at) , now() ) ) /3 ) ) ) ) hour as last_date + into @p_from + 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); + + -- Belépések számának betöltése az aktuális 3 órás intervalumban + 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épések száma az aktuális 3 órás intervalumban: ', @p_count_all_2) ); + + -- Ha az első belépéstől számítot aktuális 3 órás intervallumban ez az elős belépés, akkor növeljük a használatot + IF @p_count_all_2 = 1 + THEN + INSERT INTO devlog ( msg) values( 'Az aktuális intervallumban ez az első belépés, usage_count növelése' ); + -- A bérlet usage_count és max_usage_count betöltése + select usage_count, max_usage_count into @p_usage_count ,@p_max_usage_count from ticket where id_ticket = NEW.id_ticket_current; + -- A bérlet usage_count növelése + update ticket set usage_count = usage_count +1 where id_ticket = New.id_ticket_current; + -- log írása + INSERT INTO log (type,message, app, id_ticket, id_door_log,created_at, updated_at) + values( + 40, concat('Bérlet haszná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()); + END IF; -- end @p_count_all_2 = 1 + END IF; -- @p_count_all > 1 + End IF; -- vége bemozgás érvényes bérlettel end type == 7 or 3 ( move in ) + + -- KILÉPÉS -- count_move_out beállítása usage_count-ra + IF NEW.direction = 5 or New.direction = 1 -- type move out + then + 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; + END IF; -- end type move out + + INSERT INTO devlog ( msg) values( 'Kártya validáció módosítása' ); + -- KÁRTYA VALIDÁCIÓ - validity [0|1] , flag | 1 << [0|1] + UPDATE card as c1 + left JOIN ( select ticket.id_card as id_card , max(ticket.id_ticket) as id_ticket + from ticket + where ticket.start <= CURDATE() + and ticket.end >= curdate() + and ticket.status = 10 + and ticket.count_move_out < ticket.max_usage_count + and ticket.id_card = New.id_card + group by id_card + order by id_card desc ) as t + 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 + , 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 + WHERE c1.type <> 50 and c1.id_card = New.id_card; + + + -- KILÉPÉS van folyamatban - kilépés flag_out kikapcsolása, belépés flag bekapcsolása , ha van érvényes bérlet + IF NEW.direction = 5 or New.direction = 1 -- type move out + then + + -- AKTUÁLIS ÉRVÉNYES BÉRLET AZONOSÍTÓ BETÖLTÉSE + -- meg kell néznünk, hogy engedélyezett -e a napi többszöri belépés + -- ezt a aktuális bérlet max_usage_count mezője állapítja meg + select max(ticket.id_ticket) into @p_mo_ticket_id + from ticket + where ticket.start <= CURDATE() + and ticket.end >= curdate() + and ticket.status = 10 + and ticket.count_move_out < ticket.max_usage_count + and ticket.id_card = New.id_card + group by id_card + order by id_card desc; + + -- alapertelmezesben a belépés engedélyezve van + set @p_allow_enter = true; + + + /* + -- NAPI TÖBSZÖRI BELÉPÉS FIGYELÉSE ITT - CSAK MÉSGEM KELLETT + + set @p_allow_multiple_enter = true; + + set @p_count_all = 0; + set @p_mo_ticket_max_usage_count = 9999; + IF @p_mo_ticket_id IS NOT NULL + THEN + select max_usage_count into @p_mo_ticket_max_usage_count from ticket where id_ticket = @p_mo_ticket_id; + + select coalesce(count(*),0) into @p_count_all from door_log where created_at >= CURDATE() and id_ticket_current = @p_mo_ticket_id and ( direction = 7 or direction = 3); + + IF @p_mo_ticket_max_usage_count > 30 THEN + set @p_allow_multiple_enter = false; + END IF; -- end allow mulitple enter + else + set @p_allow_multiple_enter = true; + END IF; -- END TICKET IS THERE + + -- Ha nincs engedélyezve a napi többszöri belépés, és már egyszer beléptünk + -- akkor további belépés nem lesz lehetséges + IF @p_allow_multiple_enter = false AND @p_count_all >= 1 + THEN + set @p_allow_enter = false; + END IF; + */ + + /* + INSERT INTO devlog ( msg) values( concat( 'Kilépés: Aktuális bérlet/belépések száma/többszire belépés on/belépés on: ' + , coalesce(@p_mo_ticket_id,''),'/', coalesce(@p_count_all,'') , '/', coalesce(@p_allow_multiple_enter,''),'/', coalesce(@p_allow_enter,'' ) ) ); + + update card set + flag_out = ( flag_out | 1 << 1 ) , -- nem léphet ki ( 1. bit beállíátsa 1-re ) + 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; + + -- BELÉPÉS van folyamatban - kilépés flag_out bekapcsolása, belépés flag kikapcsolása + /* + IF (NEW.direction = 7 or New.direction = 3 ) and NEW.id_ticket_current is not null + THEN + INSERT INTO devlog ( msg) values( 'kilépés flag_out bekapcsolása, belépés flag kikapcsolása' ); + update card set + flag_out = ( flag_out & ~(1 << 1 ) ) , -- kilpés engedélyezése ( 1. bit beállítása 0-ra ) + flag = ( flag | 1 << 1 ) -- belépés nem lehetséges ( 1.bit beállítsa 1-re ) + WHERE type <> 50 and id_card = New.id_card; + END IF; -- END - BELÉPÉS - KILÉPÉS KAPCSOLÓ BEKPACSOLÁSA + */ + + END IF; -- VAN VENDÉG , VAN KÁRTYA +END; +$$ +DELIMITER ; + + +