/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SeasonTicket::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'amount' => $this->amount, 'cost' => $this->cost, 'limit_format' => $this->limit_format, 'limit_value' => $this->limit_value]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
 /**
  * Creates a new Pay model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @param integer $user_id
  * @return mixed
  */
 public function actionCreate($user_id = false)
 {
     if ($user_id) {
         $profile = Profile::findOne(['user_id' => $user_id]);
         if (!$profile) {
             throw new HttpException(404);
         }
         $model = new Pay();
         $model->setScenario('pay-create');
         $model->maxBonusBalance = $profile->bonus_balance;
         $tickets = SeasonTicket::getTicketArray();
         //ajax validation
         if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         }
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             $groups = Group::getGroupArray();
             return $this->render('create', ['model' => $model, 'profile' => $profile, 'tickets' => $tickets, 'groups' => $groups]);
         }
     } else {
         $searchModel = new UserSearch();
         $dataProvider = $searchModel->search(Yii::$app->request->get());
         return $this->render('listusers', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel]);
     }
 }
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['ticket_id', 'id', 'user_id'], 'integer'], ['ticket_id', 'exist', 'targetAttribute' => 'id', 'targetClass' => SeasonTicket::className()], [['comment', 'create_at', 'userFullName'], 'safe'], [['current_cost', 'cash', 'bonus_cash'], 'number']];
 }
 private function addPaidEmployment()
 {
     if (!$this->ticket_id) {
         return true;
     }
     $this->_ticketModel = SeasonTicket::findOne(['id' => $this->ticket_id]);
     if (!$this->_ticketModel) {
         return true;
     }
     $this->_currentDate = Carbon::createFromFormat('d.m.Y', $this->startdate);
     $this->_endDate = self::getEmploymentEndDate($this->_currentDate, $this->_ticketModel->limit_format, $this->_ticketModel->limit_value);
     $timeTableGroups = Timetable::find()->asArray()->where(['group_id' => $this->groups])->all();
     $cond = 'date >= :startdate AND pay.user_id = :user_id';
     $params = [':user_id' => $this->user_id, ':startdate' => $this->_currentDate->toDateString()];
     $existEmployments = PaidEmployment::find()->select(['date', 'timetable_id', 'group_id' => 'timetable.group_id'])->asArray()->joinWith(['pay', 'timetable'])->where($cond, $params)->andWhere(['timetable.group_id' => $this->groups])->all();
     $existLastEmployment = PaidEmployment::find()->select(['date', 'timetable_id', 'group_id' => 'timetable.group_id'])->asArray()->joinWith(['pay', 'timetable'])->where($cond, $params)->andWhere(['timetable.group_id' => $this->groups])->orderBy('date DESC')->one();
     $this->addInsertArrayTimeLimit($timeTableGroups, $existEmployments, $this->_currentDate, $this->_endDate);
     if ($existLastEmployment) {
         $lastEmploymentDate = Carbon::createFromFormat('Y-m-d', $existLastEmployment['date'])->addDay();
         $this->_endDate = self::getEmploymentEndDate($lastEmploymentDate, $this->_ticketModel->limit_format, $this->_ticketModel->limit_value);
     }
     if ($this->_ticketModel->amount > 0) {
         $this->addInsertArrayAmountLimit($timeTableGroups, $existEmployments, $this->_currentDate, $this->_endDate);
     }
     if (!empty($this->_insertArray)) {
         $command = Yii::$app->db->createCommand()->batchInsert('{{%paid_employment}}', ['date', 'pay_id', 'timetable_id'], $this->_insertArray);
         return is_int($command->execute());
     } else {
         return true;
     }
 }
 /**
  * Finds the SeasonTicket model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SeasonTicket the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModelAll($id)
 {
     if (($model = SeasonTicket::find()->where(['id' => $id])->all()) !== null) {
         return $model;
     } else {
         throw new HttpException(404);
     }
 }
?>
<div class="<?php 
echo $gridId;
?>
">
    <div class="box box-default">
        <div class="box-header">
            <div class="pull-right">
                <?php 
echo Html::a('<i class="fa fa-plus"></i>', ['create'], ['class' => 'btn btn-primary btn-sm', 'title' => Ticket::t('ticket', 'Create')]);
?>
                <?php 
echo Html::a('<i class="fa fa-trash"></i>', ['batch-delete'], ['class' => 'btn btn-danger btn-sm', 'id' => 'batch-delete', 'title' => Ticket::t('ticket', 'Delete selected')]);
?>
            </div>
        </div>
        <div class="box-body">
            <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

            <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'id' => $gridId, 'filterModel' => $searchModel, 'columns' => [['class' => CheckboxColumn::classname()], 'title', 'description', 'cost', 'amount', 'limit_value', ['attribute' => 'limit_format', 'value' => function ($model) {
    return SeasonTicket::getLimitFormatArray()[$model->limit_format];
}, 'filter' => Html::activeDropDownList($searchModel, 'limit_format', SeasonTicket::getLimitFormatArray(), ['class' => 'form-control', 'prompt' => ''])], ['class' => 'yii\\grid\\ActionColumn', 'buttonOptions' => ['class' => 'btn btn-default btn-xs'], 'headerOptions' => ['style' => 'width:100px;'], 'header' => Ticket::t('ticket', 'Actions')]]]);
?>

        </div>
    </div>
</div>