From ab885b13e9ee681b9fdbf17b71f406e2bd3ef5d6 Mon Sep 17 00:00:00 2001 From: rocho Date: Tue, 22 Sep 2015 09:19:46 +0200 Subject: [PATCH 1/2] add changes to ticket_type --- backend/components/AdminMenuStructure.php | 1 + backend/controllers/TicketTypeController.php | 119 ++++++++++++++++ backend/models/TicketTypeSearch.php | 76 ++++++++++ backend/views/ticket-type/_form.php | 68 +++++++++ backend/views/ticket-type/_search.php | 49 +++++++ backend/views/ticket-type/create.php | 22 +++ backend/views/ticket-type/index.php | 44 ++++++ backend/views/ticket-type/update.php | 23 +++ backend/views/ticket-type/view.php | 64 +++++++++ common/models/Account.php | 2 +- common/models/TicketType.php | 132 ++++++++++++++++++ ...m150921_162327_add__table__ticket_type.php | 49 +++++++ 12 files changed, 648 insertions(+), 1 deletion(-) create mode 100644 backend/controllers/TicketTypeController.php create mode 100644 backend/models/TicketTypeSearch.php create mode 100644 backend/views/ticket-type/_form.php create mode 100644 backend/views/ticket-type/_search.php create mode 100644 backend/views/ticket-type/create.php create mode 100644 backend/views/ticket-type/index.php create mode 100644 backend/views/ticket-type/update.php create mode 100644 backend/views/ticket-type/view.php create mode 100644 common/models/TicketType.php create mode 100644 console/migrations/m150921_162327_add__table__ticket_type.php diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index baac8d3..ae957ec 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -39,6 +39,7 @@ class AdminMenuStructure{ $items[] = ['label' => 'Kasszák', 'url' =>['/account/index']]; $items[] = ['label' => 'Kedvezmények', 'url' => ['/discount/index'] ]; $items[] = ['label' => 'Termék kategóriák', 'url' => ['/product-category/index'] ]; + $items[] = ['label' => 'Bérlet típusok', 'url' => ['/ticket-type/index'] ]; if ( count($items) > 0 ){ $userMainMenu = ['label' => 'Beállítások', 'url' => null, diff --git a/backend/controllers/TicketTypeController.php b/backend/controllers/TicketTypeController.php new file mode 100644 index 0000000..4f98888 --- /dev/null +++ b/backend/controllers/TicketTypeController.php @@ -0,0 +1,119 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + } + + /** + * Lists all TicketType models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new TicketTypeSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single TicketType model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new TicketType model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new TicketType(); + + $model->type = TicketType::TYPE_DEFAULT; + $model->status = TicketType::STATUS_ACTIVE; + $model->time_unit_type = TicketType::TIME_UNIT_MONTH; + $model->time_unit_count = 1; + $model->max_usage_count = 0; + $accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_ticket_type]); + } else { + return $this->render('create', [ + 'model' => $model, + 'accounts' => $accounts + ]); + } + } + + /** + * Updates an existing TicketType model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + $accounts = Account::find()->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $model->id_account] ])->all(); + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_ticket_type]); + } else { + return $this->render('update', [ + 'model' => $model, + 'accounts' => $accounts + ]); + } + } + + + /** + * Finds the TicketType model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return TicketType the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = TicketType::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/backend/models/TicketTypeSearch.php b/backend/models/TicketTypeSearch.php new file mode 100644 index 0000000..5749c34 --- /dev/null +++ b/backend/models/TicketTypeSearch.php @@ -0,0 +1,76 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + $query->andFilterWhere([ + 'id_ticket_type' => $this->id_ticket_type, + 'type' => $this->type, + 'max_usage_count' => $this->max_usage_count, + 'time_unit_type' => $this->time_unit_type, + 'time_unit_count' => $this->time_unit_count, + 'price_brutto' => $this->price_brutto, + 'id_account' => $this->id_account, + 'flag_student' => $this->flag_student, + 'status' => $this->status, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } +} diff --git a/backend/views/ticket-type/_form.php b/backend/views/ticket-type/_form.php new file mode 100644 index 0000000..330979d --- /dev/null +++ b/backend/views/ticket-type/_form.php @@ -0,0 +1,68 @@ + +"; + $s .= Html::beginTag($tag); + $s .= $name; + $s .= Html::endTag($tag); + $s .= "
"; + return $s; + } + $account_options = ArrayHelper::map($accounts, 'id_account', 'name'); +?> +
+ + + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + + field($model, 'type')->dropDownList(TicketType::ticketTypes() ) ?> + + + field($model, 'max_usage_count')->textInput() ?> + + +
+
+ field($model, 'time_unit_count')->textInput() ?> +
+
+ field($model, 'time_unit_type')->dropDownList(TicketType::timeUnitTypes()) ?> +
+
+ + + + field($model, 'price_brutto')->textInput() ?> + + + field($model, 'id_account')->dropDownList($account_options) ?> + + + + field($model, 'status')->checkbox( ['value' => 10, 'label' => Yii::t('common/ticket_type', "Active") ]) ?> + + field($model, 'flag_student')->checkbox( ['value' => 1, 'label' => Yii::t('common/ticket_type', "Student") ]) ?> + + + +
+ isNewRecord ? Yii::t('common/ticket_type', 'Create') : Yii::t('common/ticket_type', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/backend/views/ticket-type/_search.php b/backend/views/ticket-type/_search.php new file mode 100644 index 0000000..e1b5e0b --- /dev/null +++ b/backend/views/ticket-type/_search.php @@ -0,0 +1,49 @@ + + + diff --git a/backend/views/ticket-type/create.php b/backend/views/ticket-type/create.php new file mode 100644 index 0000000..3b1eaa2 --- /dev/null +++ b/backend/views/ticket-type/create.php @@ -0,0 +1,22 @@ +title = Yii::t('common/ticket_type', 'Create Ticket Type'); +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Ticket Types'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'accounts' => $accounts, + ]) ?> + +
diff --git a/backend/views/ticket-type/index.php b/backend/views/ticket-type/index.php new file mode 100644 index 0000000..2b16b80 --- /dev/null +++ b/backend/views/ticket-type/index.php @@ -0,0 +1,44 @@ +title = Yii::t('common/ticket_type', 'Ticket Types'); +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ render('_search', ['model' => $searchModel]); ?> + +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'columns' => [ + 'name', + 'max_usage_count', + 'price_brutto', + 'time_unit_type', + 'time_unit_count', + 'id_account', + 'flag_student', + 'status', + 'created_at', + 'updated_at', + + [ + 'class' => 'yii\grid\ActionColumn', + 'template' =>'{view} {edit}' + + ], + ], + ]); ?> + +
diff --git a/backend/views/ticket-type/update.php b/backend/views/ticket-type/update.php new file mode 100644 index 0000000..77355d4 --- /dev/null +++ b/backend/views/ticket-type/update.php @@ -0,0 +1,23 @@ +title = Yii::t('common/ticket_type', 'Update {modelClass}: ', [ + 'modelClass' => 'Ticket Type', +]) . ' ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Ticket Types'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id_ticket_type]]; +$this->params['breadcrumbs'][] = Yii::t('common/ticket_type', 'Update'); +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/ticket-type/view.php b/backend/views/ticket-type/view.php new file mode 100644 index 0000000..2443313 --- /dev/null +++ b/backend/views/ticket-type/view.php @@ -0,0 +1,64 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Ticket Types'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ $model->id_ticket_type], ['class' => 'btn btn-primary']) ?> + $model->id_ticket_type], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('common/ticket_type', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]) + */ + ?> +

+ + $model, + 'attributes' => [ + 'name', + [ + 'attribute' => 'type', + 'value' => $model->typeHuman + ], + 'max_usage_count', + [ + 'attribute' => 'time_unit_count', + ], + [ + 'attribute' => 'time_unit_type', + 'value' => $model->timeUnitHuman + ], + 'price_brutto', + [ + 'attribute' => 'id_account', + 'value' => $model->account->name, + 'label' => Yii::t('common/ticket_type','Account') + ], + 'flag_student', + [ + 'attribute' => 'status', + 'value' => $model->statusHuman + ], + 'created_at:datetime', + 'updated_at:datetime', + ], + ]) ?> + +
diff --git a/common/models/Account.php b/common/models/Account.php index 291465e..9cc1092 100644 --- a/common/models/Account.php +++ b/common/models/Account.php @@ -72,7 +72,7 @@ class Account extends \yii\db\ActiveRecord ]; } - static function statuses() { + static function statuses() { return [ self::STATUS_ACTIVE => Yii::t('common/account', 'Active'), self::STATUS_DELETED => Yii::t('common/account', 'Inactive'), diff --git a/common/models/TicketType.php b/common/models/TicketType.php new file mode 100644 index 0000000..4d8a45c --- /dev/null +++ b/common/models/TicketType.php @@ -0,0 +1,132 @@ + 64] + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id_ticket_type' => Yii::t('common/ticket_type', 'Id Ticket Type'), + 'name' => Yii::t('common/ticket_type', 'Name'), + 'type' => Yii::t('common/ticket_type', 'Type'), + 'max_usage_count' => Yii::t('common/ticket_type', 'Max Usage Count'), + 'time_unit_type' => Yii::t('common/ticket_type', 'Time Unit Type'), + 'time_unit_count' => Yii::t('common/ticket_type', 'Time Unit Count'), + 'price_brutto' => Yii::t('common/ticket_type', 'Price Brutto'), + 'id_account' => Yii::t('common/ticket_type', 'Id Account'), + 'flag_student' => Yii::t('common/ticket_type', 'Flag Student'), + 'status' => Yii::t('common/ticket_type', 'Status'), + 'created_at' => Yii::t('common/ticket_type', 'Created At'), + 'updated_at' => Yii::t('common/ticket_type', 'Updated At'), + ]; + } + + + static function statuses() { + return [ + self::STATUS_ACTIVE => Yii::t('common/ticket_type', 'Active'), + self::STATUS_DELETED => Yii::t('common/ticket_type', 'Inactive'), + ]; + } + + public function getStatusHuman(){ + $result = null; + $s = self::statuses($this->status); + if ( array_key_exists($this->status, $s)){ + $result = $s[$this->status]; + } + return $result; + } + + + static function timeUnitTypes() { + return [ + self::TIME_UNIT_DAY => Yii::t('common/ticket_type', 'Nap'), + self::TIME_UNIT_MONTH => Yii::t('common/ticket_type', 'Hónap'), + self::TIME_UNIT_MONTH_REFERENCE => Yii::t('common/ticket_type', 'Tárgyhónap'), + ]; + } + + public function getTimeUnitHuman(){ + $result = null; + $s = self::timeUnitTypes($this->time_unit_type); + if ( array_key_exists($this->time_unit_type, $s)){ + $result = $s[$this->time_unit_type]; + } + return $result; + } + static function ticketTypes() { + return [ + self::TYPE_NORMAL => Yii::t('common/ticket_type', 'Normal'), + ]; + } + + public function getTypeHuman(){ + $result = null; + $s = self::ticketTypes( ); + if ( array_key_exists($this->type, $s)){ + $result = $s[$this->type]; + } + return $result; + } + + + public function getAccount(){ + return $this->hasOne(Account::className(), [ 'id_account' => 'id_account' ]); + } + +} diff --git a/console/migrations/m150921_162327_add__table__ticket_type.php b/console/migrations/m150921_162327_add__table__ticket_type.php new file mode 100644 index 0000000..eea0236 --- /dev/null +++ b/console/migrations/m150921_162327_add__table__ticket_type.php @@ -0,0 +1,49 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%ticket_type}}', [ + 'id_ticket_type' => $this->primaryKey(), + 'name' => $this->string(64)->notNull(), + 'type' => $this->integer(), + 'max_usage_count' => $this->integer(), + 'time_unit_type' => $this->integer(), + 'time_unit_count' => $this->integer(), + 'price_brutto' => $this->integer(), + 'id_account' => $this->integer()->notNull(), + 'flag_student' => $this->smallInteger()->notNull()->defaultValue(0), + 'status' => $this->smallInteger()->notNull()->defaultValue(10), + 'created_at' => $this->timestamp()->notNull(), + 'updated_at' => $this->timestamp()->notNull(), + ], $tableOptions ); + } + + public function down() + { + echo "m150921_162327_add__table__ticket_type cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} From db8fca563083eade16b9bc3b52e7ef78ca199a56 Mon Sep 17 00:00:00 2001 From: rocho Date: Tue, 22 Sep 2015 11:59:58 +0200 Subject: [PATCH 2/2] =?UTF-8?q?add=20ticket=20type=20model=20+=20crud=20(?= =?UTF-8?q?=20b=C3=A9rlet=20t=C3=ADpus)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/views/ticket-type/index.php | 32 ++- backend/views/ticket-type/update.php | 7 +- backend/views/ticket-type/view.php | 8 +- common/messages/hu/app.php | 28 +-- common/messages/hu/common.php | 3 +- .../messages/hu/common/product_category.php | 2 - common/messages/hu/common/ticket_type.php | 48 ++++ common/models/Account.php | 5 + common/models/TicketType.php | 215 ++++++++++++------ 9 files changed, 242 insertions(+), 106 deletions(-) create mode 100644 common/messages/hu/common/ticket_type.php diff --git a/backend/views/ticket-type/index.php b/backend/views/ticket-type/index.php index 2b16b80..4db42f4 100644 --- a/backend/views/ticket-type/index.php +++ b/backend/views/ticket-type/index.php @@ -23,19 +23,35 @@ $this->params['breadcrumbs'][] = $this->title; 'dataProvider' => $dataProvider, 'columns' => [ 'name', - 'max_usage_count', 'price_brutto', - 'time_unit_type', + 'max_usage_count', 'time_unit_count', - 'id_account', - 'flag_student', - 'status', - 'created_at', - 'updated_at', + [ + + 'attribute' =>'time_unit_type', + 'value' =>'timeUnitHuman', + ], + [ + 'attribute' => 'id_account', + 'value' => 'accountName', + ], + [ + 'attribute' =>'flag_student', + 'value' =>function($model, $key, $index, $column){ + return $model->isStudent() ? Yii::t('common', "Yes") : Yii::t('common', "No") ; + }, + + ], + [ + 'attribute' =>'status', + 'value' =>'statusHuman', + ], + 'created_at:datetime', + 'updated_at:datetime', [ 'class' => 'yii\grid\ActionColumn', - 'template' =>'{view} {edit}' + 'template' =>'{view} {update}' ], ], diff --git a/backend/views/ticket-type/update.php b/backend/views/ticket-type/update.php index 77355d4..efd086d 100644 --- a/backend/views/ticket-type/update.php +++ b/backend/views/ticket-type/update.php @@ -4,10 +4,9 @@ use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $model common\models\TicketType */ +/* @var $accounts common\models\Account[] */ -$this->title = Yii::t('common/ticket_type', 'Update {modelClass}: ', [ - 'modelClass' => 'Ticket Type', -]) . ' ' . $model->name; +$this->title = Yii::t('common/ticket_type', 'Update ticket type:' ) . ' ' . $model->name; $this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Ticket Types'), 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id_ticket_type]]; $this->params['breadcrumbs'][] = Yii::t('common/ticket_type', 'Update'); @@ -18,6 +17,8 @@ $this->params['breadcrumbs'][] = Yii::t('common/ticket_type', 'Update'); render('_form', [ 'model' => $model, + 'accounts' => $accounts, + ]) ?> diff --git a/backend/views/ticket-type/view.php b/backend/views/ticket-type/view.php index 2443313..3483836 100644 --- a/backend/views/ticket-type/view.php +++ b/backend/views/ticket-type/view.php @@ -48,10 +48,12 @@ $this->params['breadcrumbs'][] = $this->title; 'price_brutto', [ 'attribute' => 'id_account', - 'value' => $model->account->name, - 'label' => Yii::t('common/ticket_type','Account') + 'value' => $model->accountName, ], - 'flag_student', + [ + 'attribute' => 'flag_student', + 'value' => ( $model->isStudent() ? Yii::t('common', 'Yes' ) : Yii::t('common', 'No' ) ), + ], [ 'attribute' => 'status', 'value' => $model->statusHuman diff --git a/common/messages/hu/app.php b/common/messages/hu/app.php index 7fde174..fc44c90 100644 --- a/common/messages/hu/app.php +++ b/common/messages/hu/app.php @@ -17,18 +17,18 @@ * NOTE: this file must be saved in UTF-8 encoding. */ return [ - 'Aktív' => '', - 'Are you sure you want to delete this item?' => '', - 'Delete' => '', - 'Felhasználók' => '', - 'Inaktív' => '', - 'Jelszó' => '', - 'Jelszó és jelszó újra nem egyezik!' => '', - 'Jelszó újra' => '', - 'Keresés' => '', - 'Mentés' => '', - 'Update' => '', - 'Update {modelClass}: ' => '', - 'Users' => '', - 'Új felhasználó' => '', + 'Aktív' => 'Aktív', + 'Are you sure you want to delete this item?' => 'Biztosan törölni szeretné ezt az element?', + 'Delete' => 'Törlés', + 'Felhasználók' => 'Felhasználók', + 'Inaktív' => 'Inaktív', + 'Jelszó' => 'Jelszó', + 'Jelszó és jelszó újra nem egyezik!' => 'Jelszó és jelszó újra nem egyezik!', + 'Jelszó újra' => 'Jelszó újra', + 'Keresés' => 'Keresés', + 'Mentés' => 'Mentés', + 'Update' => 'Update', + 'Update {modelClass}: ' => '{modelClass} módosítása: ', + 'Users' => 'Felhasználók', + 'Új felhasználó' => 'Új felhasználó', ]; diff --git a/common/messages/hu/common.php b/common/messages/hu/common.php index 565f9e5..0968165 100644 --- a/common/messages/hu/common.php +++ b/common/messages/hu/common.php @@ -17,5 +17,6 @@ * NOTE: this file must be saved in UTF-8 encoding. */ return [ - 'Create' => 'Mentés', + 'No' => 'Nem', + 'Yes' => 'Igen', ]; diff --git a/common/messages/hu/common/product_category.php b/common/messages/hu/common/product_category.php index e804f72..3dab63e 100644 --- a/common/messages/hu/common/product_category.php +++ b/common/messages/hu/common/product_category.php @@ -18,11 +18,9 @@ */ return [ 'Active' => 'Aktív', - 'Are you sure you want to delete this item?' => 'Biztosan törölni szeretné a termék kategóriát?', 'Create' => 'Mentés', 'Create Product Category' => 'Új termék kategória', 'Created At' => 'Létrehozás ideje', - 'Delete' => 'Törlés', 'Id Product Category' => 'Azonosító', 'Inactive' => 'Inaktív', 'Name' => 'Név', diff --git a/common/messages/hu/common/ticket_type.php b/common/messages/hu/common/ticket_type.php new file mode 100644 index 0000000..c15f450 --- /dev/null +++ b/common/messages/hu/common/ticket_type.php @@ -0,0 +1,48 @@ + 'Bérlet módosítása: ', + 'Active' => 'Aktív', + 'Create' => 'Mentés', + 'Create Ticket Type' => 'Új bérlet típus', + 'Created At' => 'Létrehozás ideje', + 'Day' => 'Nap', + 'Flag Student' => 'Diák', + 'Id Account' => 'Kassza', + 'Id Ticket Type' => 'Bérlet típus', + 'Inactive' => 'Inaktív', + 'Invalid account (inactive)!' => 'Érvénytelen kassza (inaktív)!', + 'Invalid account!' => 'Érvénytelen kassza!', + 'Max Usage Count' => 'Akalmak', + 'Month' => 'Hónap', + 'Name' => 'Név', + 'Normal' => 'Normál', + 'Price Brutto' => 'Bruttó ár', + 'Reference month' => 'Tárgyhónap', + 'Reset' => 'Reset', + 'Search' => 'Keresés', + 'Status' => 'Státusz', + 'Student' => 'Diák', + 'Ticket Types' => 'Bérlet típusok', + 'Time Unit Count' => 'Idő egység mennyiség', + 'Time Unit Type' => 'Idő egység típus', + 'Type' => 'Típus', + 'Update' => 'Módosítás', + 'Updated At' => 'Módosítás ideje', +]; diff --git a/common/models/Account.php b/common/models/Account.php index 9cc1092..125a73c 100644 --- a/common/models/Account.php +++ b/common/models/Account.php @@ -103,4 +103,9 @@ class Account extends \yii\db\ActiveRecord } return $result; } + + public function isInactive(){ + return $this->status == self::STATUS_DELETED; + } + } diff --git a/common/models/TicketType.php b/common/models/TicketType.php index 4d8a45c..11a86d4 100644 --- a/common/models/TicketType.php +++ b/common/models/TicketType.php @@ -20,37 +20,74 @@ use Yii; * @property string $created_at * @property string $updated_at */ -class TicketType extends \common\models\BaseFitnessActiveRecord -{ - +class TicketType extends \common\models\BaseFitnessActiveRecord { const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; - - CONST TIME_UNIT_DAY = 10; - CONST TIME_UNIT_MONTH = 20; - CONST TIME_UNIT_MONTH_REFERENCE = 30; - + CONST TIME_UNIT_DAY = 10;//nap + CONST TIME_UNIT_MONTH = 20;//hónap + CONST TIME_UNIT_MONTH_REFERENCE = 30; //tárgy hónap const TYPE_NORMAL = 10; const TYPE_DEFAULT = self::TYPE_NORMAL; - /** - * @inheritdoc - */ - public static function tableName() - { - return 'ticket_type'; - } - - /** + const FLAG_STUDENT_OFF = 0; + const FLAG_STUDENT_ON = 1; + + /** + * @inheritdoc + */ + public static function tableName() { + return 'ticket_type'; + } + + /** + * @formatter:off * @inheritdoc */ public function rules() { return [ - [['name', 'id_account'], 'required'], - [['type', 'max_usage_count', 'time_unit_type', 'time_unit_count', 'price_brutto', 'id_account', 'flag_student', 'status'], 'integer'], - [['created_at', 'updated_at'], 'safe'], - [['name'], 'string', 'max' => 64] + [['name', 'id_account','time_unit_count','type' ,'time_unit_type' ,'max_usage_count','price_brutto'], 'required'], + //////////////// + //price brutto + //////////////// + [[ 'price_brutto', ], 'integer'], + //////////////// + //time_unit_type + //////////////// + [['time_unit_type',], 'integer'], + [['time_unit_type',], 'in', 'range' => [ self::TIME_UNIT_DAY,self::TIME_UNIT_MONTH,self::TIME_UNIT_MONTH_REFERENCE] ], + //////////////// + //time_unit_count + //////////////// + [['time_unit_count',], 'integer','min' => 1 , 'max' => 100], + //////////////// + //max_usage_count + //////////////// + [['max_usage_count',], 'integer','min' => 0 , 'max' => 10000], + //////////////// + //flag_student + //////////////// + [['flag_student',], 'integer'], + [['flag_student',], 'in', 'range' => [ self::FLAG_STUDENT_OFF, self::FLAG_STUDENT_ON ]], + //////////////// + //status + //////////////// + [['status',], 'integer'], + [['status',], 'in', 'range' => [ self::STATUS_ACTIVE, self::STATUS_DELETED ]], + //////////////// + //type + //////////////// + [['type',], 'integer'], + [['type',], 'in', 'range' => [ self::TYPE_NORMAL ]], + //////////////// + //name + //////////////// + [['name'], 'string', 'max' => 64], + //////////////// + //id_account + //////////////// + [['id_account',], 'integer'], + [['id_account',], 'validateIdAccount'], ]; } @@ -75,58 +112,86 @@ class TicketType extends \common\models\BaseFitnessActiveRecord ]; } - - static function statuses() { - return [ - self::STATUS_ACTIVE => Yii::t('common/ticket_type', 'Active'), - self::STATUS_DELETED => Yii::t('common/ticket_type', 'Inactive'), - ]; - } - - public function getStatusHuman(){ - $result = null; - $s = self::statuses($this->status); - if ( array_key_exists($this->status, $s)){ - $result = $s[$this->status]; - } - return $result; - } - - - static function timeUnitTypes() { - return [ - self::TIME_UNIT_DAY => Yii::t('common/ticket_type', 'Nap'), - self::TIME_UNIT_MONTH => Yii::t('common/ticket_type', 'Hónap'), - self::TIME_UNIT_MONTH_REFERENCE => Yii::t('common/ticket_type', 'Tárgyhónap'), - ]; - } - - public function getTimeUnitHuman(){ - $result = null; - $s = self::timeUnitTypes($this->time_unit_type); - if ( array_key_exists($this->time_unit_type, $s)){ - $result = $s[$this->time_unit_type]; - } - return $result; - } - static function ticketTypes() { - return [ - self::TYPE_NORMAL => Yii::t('common/ticket_type', 'Normal'), - ]; - } - - public function getTypeHuman(){ - $result = null; - $s = self::ticketTypes( ); - if ( array_key_exists($this->type, $s)){ - $result = $s[$this->type]; - } - return $result; - } - - - public function getAccount(){ - return $this->hasOne(Account::className(), [ 'id_account' => 'id_account' ]); - } - + /** + * @formatter:on + */ + static function statuses() { + return [ + self::STATUS_ACTIVE => Yii::t ( 'common/ticket_type', 'Active' ), + self::STATUS_DELETED => Yii::t ( 'common/ticket_type', 'Inactive' ) + ]; + } + public function getStatusHuman() { + $result = null; + $s = self::statuses ( $this->status ); + if (array_key_exists ( $this->status, $s )) { + $result = $s [$this->status]; + } + return $result; + } + static function timeUnitTypes() { + return [ + self::TIME_UNIT_DAY => Yii::t ( 'common/ticket_type', 'Day' ), + self::TIME_UNIT_MONTH => Yii::t ( 'common/ticket_type', 'Month' ), + self::TIME_UNIT_MONTH_REFERENCE => Yii::t ( 'common/ticket_type', 'Reference month' ) + ]; + } + public function getTimeUnitHuman() { + $result = null; + $s = self::timeUnitTypes ( $this->time_unit_type ); + if (array_key_exists ( $this->time_unit_type, $s )) { + $result = $s [$this->time_unit_type]; + } + return $result; + } + static function ticketTypes() { + return [ + self::TYPE_NORMAL => Yii::t ( 'common/ticket_type', 'Normal' ) + ]; + } + public function getTypeHuman() { + $result = null; + $s = self::ticketTypes (); + if (array_key_exists ( $this->type, $s )) { + $result = $s [$this->type]; + } + return $result; + } + public function getAccount() { + return $this->hasOne ( Account::className (), [ + 'id_account' => 'id_account' + ] ); + } + public function getAccountName() { + return $this->account->name; + } + + public function isStudent(){ + return $this->flag_student == ( self::FLAG_STUDENT_ON); + } + + + public function validateIdAccount($attribute,$params){ + $account = null; + if ( !$this->hasErrors("id_account")){ + $account = Account::findOne($this->$attribute); + + if ( !isset($account)){ + $this->addError($attribute,Yii::t('common/ticket_type','Invalid account!')); + }else{ + + //on update + if ( !$this->isNewRecord ){ + //if selected account is inactive ... + if ( $account->isInactive() ){ + //... and changed + if ( $this->isAttributeChanged('id_account')){ + $this->addError($attribute,Yii::t('common/ticket_type','Invalid account (inactive)!')); + } + } + } + } + } + } + }