/**
  * Finds user by [[username]]
  *
  * @return User|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = Users::findByUsername($this->username);
     }
     return $this->_user;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Users::find();
     $dataProvider = new ActiveDataProvider(['query' => $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->joinWith('userRole');
     $query->andFilterWhere(['id' => $this->id, 'username' => $this->username]);
     $query->andFilterWhere(['like', 'first_name', $this->first_name])->andFilterWhere(['like', 'last_name', $this->last_name])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'user_roles.name', $this->user_role_id])->andFilterWhere(['like', 'password', $this->password]);
     return $dataProvider;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getFromwhoom()
 {
     return $this->hasOne(Users::className(), ['id' => 'fromwhoom_id']);
 }
 /**
  * Finds the Users model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Users the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Users::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUsers()
 {
     return $this->hasMany(Users::className(), ['user_role_id' => 'id']);
 }
echo $form->field($model, 'company_id')->dropDownList(ArrayHelper::map(Companies::find()->asArray()->all(), 'id', 'name'), ['prompt' => 'Select Company', 'onchange' => '
                $.post("http://orderprocessing/web/admin/affiliates/lists?id=' . '"+$(this).val(), function(data){
                    $("select#orders-affiliate_id").html(data);
                });']);
?>

    <?php 
echo $form->field($model, 'affiliate_id')->dropDownList(ArrayHelper::map(Affiliates::find()->asArray()->all(), 'id', 'name'), ['prompt' => 'Select Affiliate']);
?>



    <?php 
echo $form->field($model, 'order_status_id')->dropDownList(ArrayHelper::map(Orderstatuses::find()->all(), 'id', 'title'), ['prompt' => 'Select Order Status']);
?>
    <?php 
echo $form->field($model, 'assignedto_id')->dropDownList(ArrayHelper::map(Users::find()->where(['user_role_id' => 2])->all(), 'id', 'username'), ['prompt' => 'Select User']);
?>

    <div class="form-group">
        <?php 
echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
    </div>

    <?php 
ActiveForm::end();
?>

</div>
 /**
  * Deletes an existing Orders model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  * @throws ForbiddenHttpException
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function actionDelete($id)
 {
     $user = null;
     if (!empty(Yii::$app->user)) {
         $user = Users::findOne(Yii::$app->user->identity->getId());
     }
     if ($user['user_role_id'] !== 1) {
         throw new ForbiddenHttpException();
     }
     $this->findModel($id)->delete();
     return $this->redirect(['index']);
 }