/**
  * Creates a new RutaDiaria model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($idRuta)
 {
     $model = new RutaDiaria();
     $ruta = Ruta::find()->where(['id' => $idRuta])->one();
     $ordenComercios = OrdenComercio::find()->where(['id_ruta' => $idRuta])->all();
     $model->id_usuario = $ruta->id_usuario;
     $model->fecha = date("Y-m-d");
     if (!$this->existeRutaDiariaPorUsuarioYFecha($model->id_usuario, $model->fecha)) {
         $idRutaDiaria = $model->save();
         if ($idRutaDiaria !== false) {
             $orden = 1;
             foreach ($ordenComercios as $ordenComercio) {
                 $rutaDiariaComercio = new RutaDiariaComercio();
                 $rutaDiariaComercio->id_comercio = $ordenComercio->id_comercio;
                 $rutaDiariaComercio->link("idRutaDiaria", $model);
                 $rutaDiariaComercio->orden = $orden;
                 $rutaDiariaComercio->save();
                 $orden++;
             }
         }
         Yii::$app->getSession()->setFlash('success', Yii::t('core', 'Daily Route has been created'));
     } else {
         Yii::$app->getSession()->setFlash('danger', Yii::t('core', 'There is already an instance of this route for today'));
     }
     return $this->redirect(['ruta/view', 'id' => $idRuta]);
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Ruta::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;
     }
     $user = User::findOne(['username' => $this->id_usuario]);
     $query->andFilterWhere(['id' => $this->id]);
     if ($this->id_usuario != null) {
         $query->andFilterWhere(['id_usuario' => $user != null ? $user->id : 0]);
     }
     if (strtolower($this->esActivo) == strtolower(Yii::t('core', 'Yes'))) {
         $query->andFilterWhere(['esActivo' => $this->esActivo == 0]);
     } else {
         if (strtolower($this->esActivo) == strtolower(Yii::t('core', 'No'))) {
             $query->andFilterWhere(['esActivo' => $this->esActivo == 1]);
         }
     }
     if (strtolower($this->dia) == strtolower(Yii::t('core', 'Monday'))) {
         $query->andFilterWhere(['dia' => 1]);
     } else {
         if (strtolower($this->dia) == strtolower(Yii::t('core', 'Tuesday'))) {
             $query->andFilterWhere(['dia' => 2]);
         } else {
             if (strtolower($this->dia) == strtolower(Yii::t('core', 'Wednesday'))) {
                 $query->andFilterWhere(['dia' => 3]);
             } else {
                 if (strtolower($this->dia) == strtolower(Yii::t('core', 'Thursday'))) {
                     $query->andFilterWhere(['dia' => 4]);
                 } else {
                     if (strtolower($this->dia) == strtolower(Yii::t('core', 'Friday'))) {
                         $query->andFilterWhere(['dia' => 5]);
                     } else {
                         if (strtolower($this->dia) == strtolower(Yii::t('core', 'Saturday'))) {
                             $query->andFilterWhere(['dia' => 6]);
                         } else {
                             if (strtolower($this->dia) == strtolower(Yii::t('core', 'Sunday'))) {
                                 $query->andFilterWhere(['dia' => 7]);
                             }
                         }
                     }
                 }
             }
         }
     }
     return $dataProvider;
 }
 public function comerciosPorDiaSinRutasActivas($dia)
 {
     //ruta: las rutas creadas
     //orden_comercio
     //comercio
     //Devuelve todas las rutas activas
     //$query = new Query();
     $query = Ruta::find()->select('id')->where(['esActivo' => "1"]);
     //Devuelve los comercios en rutas activas
     //$query2 = new Query();
     $query2 = OrdenComercio::find()->select('id_comercio')->where(['in', 'id_ruta', $query]);
     //Devuelve los comercios sin rutas activas
     //$comercios = [];
     $comercios = Comercio::find()->where(['not in', 'id', $query2])->andWhere(['dia' => $dia])->andWhere(['esActivo' => '1'])->all();
     return $comercios;
 }