diff --git a/common/models/Ticket.php b/common/models/Ticket.php index ccbf7e1..82c7293 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -70,4 +70,58 @@ class Ticket extends \common\models\BaseFitnessActiveRecord 'updated_at' => Yii::t('common/ticket', 'Updated At'), ]; } + + + public function getCard(){ + return $this->hasOne( Card::className(), ["id_card" =>"id_card" ] ); + } + + public function getUser(){ + return $this->hasOne( User::className(), ["id" =>"id_user" ] ); + } + + public function getTicketType(){ + return $this->hasOne( TicketType::className(), ["id_ticket_type" =>"id_ticket_type" ] ); + } + public function getDiscount(){ + return $this->hasOne( Discount::className(), ["id_discount" =>"id_discount" ] ); + } + + public function getAccount() { + return $this->hasOne ( Account::className (), [ + 'id_account' => 'id_account' + ] ); + } + public function getAccountName() { + $result = ""; + $account = $this->account; + if ( isset($account) ){ + $result = $this->account->name; + } + return $result; + } + public function getDiscountName() { + $result = ""; + $discount = $this->discount; + if ( isset($discount) ){ + $result = $this->discount->name; + } + return $result; + } + public function getTicketTypeName() { + $result = ""; + $ticketType = $this->ticketType; + if ( isset($ticketType) ){ + $result = $ticketType->name; + } + return $result; + } + public function getUserName() { + $result = ""; + $user = $this->user; + if ( isset($user) ){ + $result = $user->username; + } + return $result; + } } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 84b141e..74a37a4 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -148,6 +148,42 @@ class Transfer extends \yii\db\ActiveRecord return $transfer; } + /** + * @param $account common\models\Account + * @param $discount common\models\Discount + * @param $currency common\models\Currency + * @param $ticket common\models\Ticket + * */ + public static function createTicketTransfer($account, $discount, $currency, $count,$ticket ){ + $transfer = new Transfer(); + + $transfer->type = Transfer::TYPE_TICKET; + + $transfer->id_object = $ticket->id_ticket; + + $transfer->item_price = $ticket->price_brutto; + $totalPrice = $transfer->item_price; + + $transfer->count = $count; + $totalPrice = $totalPrice * $count; + + if ( isset( $discount ) ){ + $transfer->id_discount = $discount->id_discount; + $totalPrice = Discount::applyDiscount( $totalPrice, $discount); + } + + $transfer->money = $totalPrice; + + if ( isset( $currency ) ){ + $transfer->rate = $currency->rate; + $transfer->money_currency = Currency::applyCurrency($totalPrice, $currency); + } + + $transfer->id_account = $account->id_account; + + return $transfer; + } + public static function modelsToArray($transfers,$default = []){ if ( $transfers == null ){ @@ -168,7 +204,13 @@ class Transfer extends \yii\db\ActiveRecord return $transfer->account->name; }, 'product_name' => function ($transfer) { - return $transfer->product->name; + $result = ""; + if ( $transfer->type == Transfer::TYPE_TICKET ){ + $result = $transfer->ticket->type->name; + }else{ + $result = $transfer->product->name; + } + return $result; }, 'category' => function ($transfer) { return $transfer->product->productCategoryName; @@ -183,8 +225,8 @@ class Transfer extends \yii\db\ActiveRecord $query = Transfer::find(); - $query->innerJoinWith('userSoldItem'); - $query->andWhere(['user_sold_item.id_user' => $user->id ]); +// $query->innerJoinWith('userSoldItem'); + $query->andWhere(['transfer.id_user' => $user->id ]); $transfers = $query->all(); return $transfers; diff --git a/console/migrations/m151008_065256_alter__table__ticket__add_columns.php b/console/migrations/m151008_065256_alter__table__ticket__add_columns.php index 9e9c763..bca41ac 100644 --- a/console/migrations/m151008_065256_alter__table__ticket__add_columns.php +++ b/console/migrations/m151008_065256_alter__table__ticket__add_columns.php @@ -7,7 +7,11 @@ class m151008_065256_alter__table__ticket__add_columns extends Migration { public function up() { - + $this->addColumn('ticket', 'id_card', 'int(11)'); + $this->alterColumn('ticket', 'start', 'dateTime' ); + $this->alterColumn('ticket', 'end', 'dateTime' ); + $this->alterColumn('ticket', 'created_at', 'dateTime' ); + $this->alterColumn('ticket', 'updated_at', 'dateTime' ); } public function down() diff --git a/console/migrations/m151008_101155_alter_table_ticket__alter__column_comment.php b/console/migrations/m151008_101155_alter_table_ticket__alter__column_comment.php new file mode 100644 index 0000000..9ebba30 --- /dev/null +++ b/console/migrations/m151008_101155_alter_table_ticket__alter__column_comment.php @@ -0,0 +1,30 @@ +alterColumn('ticket', 'comment', 'varchar(255) null' ); + } + + public function down() + { + echo "m151008_101155_alter_table_ticket__alter__column_comment 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/m151008_101334_alter_table_ticket__alter__columns__not_null.php b/console/migrations/m151008_101334_alter_table_ticket__alter__columns__not_null.php new file mode 100644 index 0000000..a4728ee --- /dev/null +++ b/console/migrations/m151008_101334_alter_table_ticket__alter__columns__not_null.php @@ -0,0 +1,33 @@ +alterColumn('ticket', 'id_user', 'int(11) not null' ); + $this->alterColumn('ticket', 'id_ticket_type', 'int(11) not null' ); + $this->alterColumn('ticket', 'id_account', 'int(11) not null' ); + $this->alterColumn('ticket', 'id_user', 'int(11) not null' ); + } + + public function down() + { + echo "m151008_101334_alter_table_ticket__alter__columns__not_null 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/frontend/components/HtmlHelper.php b/frontend/components/HtmlHelper.php index 095e528..327d409 100644 --- a/frontend/components/HtmlHelper.php +++ b/frontend/components/HtmlHelper.php @@ -9,9 +9,9 @@ use yii\helpers\ArrayHelper; class HtmlHelper{ - private static function mkOptions( $models, $key, $value = 'name', $options = [] ){ + public static function mkOptions( $models, $key, $value = 'name' ){ $result = []; - $result = ArrayHelper::map($models, $key, $value); + $result = ArrayHelper::map( $models, $key, $value ); return $result; } @@ -20,23 +20,26 @@ class HtmlHelper{ * @var $models common\models\Account[] * @return * */ - public static function mkAccountOptions( $models, $options = [] ){ - return $result = self::mkOptions($models, 'id_account'); + public static function mkAccountOptions( $models ){ + $result = HtmlHelper::mkOptions( $models, 'id_account' ); + return $result; } /** * @var $models common\models\Discount[] * @return * */ - public static function mkDiscountOptions( $models, $options = [] ){ - return $result = self::mkOptions($models, 'id_discount'); + public static function mkDiscountOptions( $models ){ + $result = HtmlHelper::mkOptions( $models, 'id_discount' ); + return $result; } /** * @var $models common\models\TicketType[] * @return * */ - public static function mkTicketTypeOptions( $models, $options = [] ){ - return $result = self::mkOptions($models, 'id_ticket_type'); + public static function mkTicketTypeOptions( $models ){ + $result = HtmlHelper::mkOptions( $models, 'id_ticket_type'); + return $result; } diff --git a/frontend/controllers/TicketController.php b/frontend/controllers/TicketController.php index 8706659..8f95402 100644 --- a/frontend/controllers/TicketController.php +++ b/frontend/controllers/TicketController.php @@ -38,14 +38,24 @@ class TicketController extends Controller * Lists all Ticket models. * @return mixed */ - public function actionIndex() + public function actionIndex($number = null) { + $receptionForm = new ReceptionForm(); + $receptionForm->number = $number; + $receptionForm->readCard(); + + + if ( !isset($receptionForm->card ) ){ + throw new NotFoundHttpException( Yii::t('frontend/ticket', 'The requested card does not exist.')); + } + $searchModel = new TicketSearch(); - $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + $dataProvider = $searchModel->search( $receptionForm->card, Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, + 'receptionForm' => $receptionForm, ]); } @@ -75,6 +85,10 @@ class TicketController extends Controller $receptionForm->readCard(); + if ( !isset($receptionForm->card ) ){ + throw new NotFoundHttpException( Yii::t('frontend/ticket', 'The requested card does not exist.')); + } + $model = new TicketCreate(); $discounts = Discount::read(); @@ -89,9 +103,11 @@ class TicketController extends Controller $model->id_user = \Yii::$app->user->id; $model->status = Ticket::STATUS_ACTIVE; $model->usage_count = 0; + $model->id_card = $receptionForm->card->id_card; if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id_ticket]); + Yii::$app->session->setFlash('success', Yii::t('frontend/ticket', 'Ticket added to customer') ); + return $this->redirect(['index', 'number' => $receptionForm->card->number]); } else { $userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) ); diff --git a/frontend/models/TicketCreate.php b/frontend/models/TicketCreate.php index a10653d..fd7cbbf 100644 --- a/frontend/models/TicketCreate.php +++ b/frontend/models/TicketCreate.php @@ -5,10 +5,14 @@ use common\models\Ticket; use common\models\TicketType; use common\models\Account; use common\models\Discount; - +use common\models\Transfer; class TicketCreate extends Ticket{ - + + public $_currency; + public $_account; + public $_discount; + public function rules() { return [ @@ -47,7 +51,6 @@ class TicketCreate extends Ticket{ ///////////////////// //comment ///////////////////// - [['comment'], 'required'], [['comment'], 'string', 'max' => 255] ]; } @@ -59,18 +62,26 @@ class TicketCreate extends Ticket{ $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid ticket type' )); } } + public function validateAccount($attribute,$params){ - $acc = Account::findOne($this->id_account); - if ( !isset($acc)) { + $this->_account = Account::findOne($this->id_account); + if ( !isset($this->_account )) { $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid transfer' )); } } + public function validateDiscount($attribute,$params){ - $discount = Discount::findOne($this->id_discount); - if ( !isset($discount)) { + $this->_discount = Discount::findOne($this->id_discount); + if ( !isset($this->_discount)) { $this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid discount' )); } } - + public function afterSave($insert, $changedAttributes){ + + $transfer = Transfer::createTicketTransfer($this->_account, $this->_discount, null, 1, $this); + + } + + } \ No newline at end of file diff --git a/frontend/models/TicketSearch.php b/frontend/models/TicketSearch.php index 92af23b..46c075f 100644 --- a/frontend/models/TicketSearch.php +++ b/frontend/models/TicketSearch.php @@ -39,14 +39,19 @@ class TicketSearch extends Ticket * * @return ActiveDataProvider */ - public function search($params) + public function search($card, $params) { $query = Ticket::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, + 'sort'=> ['defaultOrder' => ['end'=>SORT_DESC]] ]); - + + + $query->innerJoinWith( 'card' ); + $query->andWhere( ['card.id_card' => $card->id_card ] ); + $this->load($params); if (!$this->validate()) { @@ -56,7 +61,6 @@ class TicketSearch extends Ticket } $query->andFilterWhere([ - 'id_ticket' => $this->id_ticket, 'id_user' => $this->id_user, 'id_ticket_type' => $this->id_ticket_type, 'id_account' => $this->id_account, diff --git a/frontend/views/common/_menu_reception.php b/frontend/views/common/_menu_reception.php index a52796e..22a7ab5 100644 --- a/frontend/views/common/_menu_reception.php +++ b/frontend/views/common/_menu_reception.php @@ -66,12 +66,11 @@ function mkBtn($card, $label,$route = null ){
- = Html::a(Yii::t('common/ticket', 'Create Ticket'), ['create'], ['class' => 'btn btn-success']) ?> + = Html::a(Yii::t('common/ticket', 'Create Ticket'), ['create' ,'number' => $card->number ], ['class' => 'btn btn-success']) ?>
= GridView::widget([ 'dataProvider' => $dataProvider, - 'filterModel' => $searchModel, 'columns' => [ - ['class' => 'yii\grid\SerialColumn'], - 'id_ticket', - 'id_user', - 'id_ticket_type', - 'id_account', - 'id_discount', - // 'start', - // 'end', - // 'max_usage_count', - // 'usage_count', - // 'status', - // 'price_brutto', - // 'comment', - // 'created_at', - // 'updated_at', + [ + 'attribute' => 'id_account', + 'value' => 'accountName' + ], + [ + 'attribute' => 'id_ticket_type', + 'value' => 'ticketTypeName' + ], + 'start:date', + 'end:date', + 'max_usage_count', + 'usage_count', + [ + 'attribute' => 'id_user', + 'value' => 'userName' + ], + [ + 'attribute' => 'id_discount', + 'value' => 'discountName' + ], + 'price_brutto', + 'created_at:datetime', - ['class' => 'yii\grid\ActionColumn'], + ['class' => 'yii\grid\ActionColumn', + 'template' =>'{view} {update}', + ], ], ]); ?> diff --git a/frontend/views/ticket/view.php b/frontend/views/ticket/view.php index 7bc9143..a9ea7d1 100644 --- a/frontend/views/ticket/view.php +++ b/frontend/views/ticket/view.php @@ -6,7 +6,7 @@ use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model common\models\Ticket */ -$this->title = $model->id_ticket; +$this->title = Yii::t('frontend/ticket', "Update ticket"); $this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket', 'Tickets'), 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> @@ -16,32 +16,34 @@ $this->params['breadcrumbs'][] = $this->title;= Html::a(Yii::t('common/ticket', 'Update'), ['update', 'id' => $model->id_ticket], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('common/ticket', 'Delete'), ['delete', 'id' => $model->id_ticket], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => Yii::t('common/ticket', 'Are you sure you want to delete this item?'), - 'method' => 'post', - ], - ]) ?>
= DetailView::widget([ 'model' => $model, 'attributes' => [ - 'id_ticket', - 'id_user', - 'id_ticket_type', - 'id_account', - 'id_discount', - 'start', - 'end', + [ + 'attribute' => 'id_ticket_type', + 'value' => $model->ticketTypeName, + ], + [ + 'attribute' => 'id_account', + 'value' => $model->accountName, + ], + [ + 'attribute' => 'id_discount', + 'value' => $model->discountName, + ], + 'start:date', + 'end:date', 'max_usage_count', 'usage_count', 'status', 'price_brutto', - 'comment', - 'created_at', - 'updated_at', + [ + 'attribute' => 'comment', + 'type' => 'raw', + ], + 'created_at:datetime', ], ]) ?>