From f19c60eacdc0eb6d5d3a76d439e91f0b11787192 Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Mon, 18 Apr 2016 22:14:25 +0200 Subject: [PATCH] change trigger to detect move in and move out --- changelog.txt | 2 + common/config/params.php | 2 +- common/models/DoorLog.php | 13 ++ common/models/Log.php | 1 + common/models/Ticket.php | 4 +- console/controllers/DoorlogController.php | 56 +++++++ .../m160418_172543_add_move_aout.php | 30 ++++ ...010_update_ticket_field_count_move_out.php | 30 ++++ ...34_add__trigger_move_in__and__move_out.php | 143 ++++++++++++++++++ 9 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 console/controllers/DoorlogController.php create mode 100644 console/migrations/m160418_172543_add_move_aout.php create mode 100644 console/migrations/m160418_180010_update_ticket_field_count_move_out.php create mode 100644 console/migrations/m160418_200934_add__trigger_move_in__and__move_out.php diff --git a/changelog.txt b/changelog.txt index 6d8dfe9..16c3a7f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,5 @@ +-0.0.65 + - change trigger to detect move in and move out -0.0.64 - admin add unstorno ticket - admin transfer search by customer name diff --git a/common/config/params.php b/common/config/params.php index 338a0c8..36d0fbd 100644 --- a/common/config/params.php +++ b/common/config/params.php @@ -4,7 +4,7 @@ return [ 'supportEmail' => 'rocho02@gmail.com', 'infoEmail' => 'info@rocho-net.hu', 'user.passwordResetTokenExpire' => 3600, - 'version' => 'v0.0.64', + 'version' => 'v0.0.65', 'company' => 'movar',//gyor 'company_name' => "Freimann Kft.", 'product_visiblity' => 'account',// on reception which products to display. account or global diff --git a/common/models/DoorLog.php b/common/models/DoorLog.php index 52a0e6a..069d23a 100644 --- a/common/models/DoorLog.php +++ b/common/models/DoorLog.php @@ -4,6 +4,8 @@ namespace common\models; use Yii; use common\components\Helper; +use yii\behaviors\TimestampBehavior; +use yii\helpers\ArrayHelper; /** * This is the model class for table "door_log". @@ -30,6 +32,17 @@ class DoorLog extends \yii\db\ActiveRecord { return 'door_log'; } + + public function behaviors() + { + return ArrayHelper::merge( [ + [ + 'class' => TimestampBehavior::className(), + 'value' => function(){ return date('Y-m-d H:i:s' ); }, + 'updatedAtAttribute' => false, + ] + ], parent::behaviors()); + } /** * @inheritdoc diff --git a/common/models/Log.php b/common/models/Log.php index e82bf08..de4f143 100644 --- a/common/models/Log.php +++ b/common/models/Log.php @@ -40,6 +40,7 @@ class Log extends BaseFitnessActiveRecord public static $TYPE_DEFAULT_ACCOUNT= 60; public static $TYPE_CREATE_CUSTOMER= 70; public static $TYPE_PROCUREMENT_UPDATE = 80; + public static $TYPE_TICKET_COUNT_MOVE_OUT = 90; /** * @inheritdoc */ diff --git a/common/models/Ticket.php b/common/models/Ticket.php index 7a25bb4..b2250f1 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -42,7 +42,7 @@ class Ticket extends \common\models\BaseFitnessActiveRecord where ticket.start <= CURDATE() and ticket.end >= curdate() and ticket.status = 10 - and ticket.usage_count < ticket.max_usage_count + and ticket.count_move_out < ticket.max_usage_count group by id_card order by id_card desc ) as t @@ -57,7 +57,7 @@ class Ticket extends \common\models\BaseFitnessActiveRecord where ticket.start <= CURDATE() and ticket.end >= curdate() and ticket.status = 10 - and ticket.usage_count < ticket.max_usage_count + and ticket.count_move_out < ticket.max_usage_count and ticket.id_card = :id group by id_card order by id_card desc diff --git a/console/controllers/DoorlogController.php b/console/controllers/DoorlogController.php new file mode 100644 index 0000000..7950085 --- /dev/null +++ b/console/controllers/DoorlogController.php @@ -0,0 +1,56 @@ +id_card = 9719; + $log->id_customer = 5559; + $log->id_ticket_current = 10430; + $log->direction = 7; + $log->id_key = null; + $log->type = 0; + $log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN; + $log->id_account = null; + $log->card_flag = 0; + $log->flag_out= 0; + + + + $log->save(false); + } + + public function actionOut() + { + $log = new DoorLog(); + + + $log->id_card = 9719; + $log->id_customer = 5559; + $log->id_ticket_current = 10430; + $log->direction = 5; + $log->id_key = null; + $log->type = 0; + $log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN; + $log->id_account = null; + $log->card_flag = 0; + $log->flag_out= 0; + + + $log->save(false); + } + +} \ No newline at end of file diff --git a/console/migrations/m160418_172543_add_move_aout.php b/console/migrations/m160418_172543_add_move_aout.php new file mode 100644 index 0000000..e2bcdea --- /dev/null +++ b/console/migrations/m160418_172543_add_move_aout.php @@ -0,0 +1,30 @@ +addColumn("ticket", "count_move_out", "int default 0"); + } + + public function down() + { + echo "m160418_172543_add_move_aout cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m160418_180010_update_ticket_field_count_move_out.php b/console/migrations/m160418_180010_update_ticket_field_count_move_out.php new file mode 100644 index 0000000..e667388 --- /dev/null +++ b/console/migrations/m160418_180010_update_ticket_field_count_move_out.php @@ -0,0 +1,30 @@ +execute(" update ticket set count_move_out = GREATEST(usage_count,0) "); + } + + public function down() + { + echo "m160418_180010_update_ticket_field_count_move_out cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m160418_200934_add__trigger_move_in__and__move_out.php b/console/migrations/m160418_200934_add__trigger_move_in__and__move_out.php new file mode 100644 index 0000000..44dd814 --- /dev/null +++ b/console/migrations/m160418_200934_add__trigger_move_in__and__move_out.php @@ -0,0 +1,143 @@ +execute(" + 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; + + delete from devlog; + /** van vendég*/ + IF NEW.id_customer is not null + /*van kártya*/ + and NEW.id_card is not null + /**van bérlet*/ + and NEW.id_ticket_current is not null + /**bemozgás volt*/ + + then + + if NEW.direction = 7 then + insert into devlog ( msg) values('direction = in'); + select count(*) into @p_count_all from door_log where created_at >= CURDATE() and id_ticket_current = New.id_ticket_current and direction = 7; + + insert into devlog ( msg) values( concat( 'count all' ,@p_count_all ) ); + + IF @p_count_all = 1 + THEN + 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; + 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 + */ + + 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; + + + /** + + */ + -- select count(*) into @p_count_all_2 from door_log where created_at >= p_from and id_ticket_current ; + 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; + + -- insert into devlog ( msg) values(CONCAT( 'ticket count since last period begin: ', @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( 'need to increase usage count multiple intervals ' ); + 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; + + 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; + + End IF; -- end type == 7 ( move in ) + + + + IF NEW.direction = 5 -- type move out + then + insert into devlog ( msg) values('bérlet kilépés...'); + update ticket set count_move_out = count_move_out +1 where id_ticket = NEW.id_ticket_current; + END IF; -- end type move out + + /** + Van e érvényes bérlet státusz frissítése a bérletkártyán + update card.flag and card.id_ticket_current + + */ + -- insert into devlog ( msg) values( 'updateing card state' ); + 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.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; + + + END IF; + +END; +$$ +DELIMITER ; + + "); + } + + public function down() + { + echo "m160418_200934_add__trigger_move_in__and__move_out cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +}