/**
  * 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 = OrdenComercio::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->andFilterWhere(['id' => $this->id, 'orden' => $this->orden, 'id_ruta' => $this->id_ruta, 'id_comercio' => $this->id_comercio]);
     return $dataProvider;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrdenComercios()
 {
     return $this->hasMany(OrdenComercio::className(), ['id_ruta' => 'id']);
 }
 public function actionSalvarRutaManual()
 {
     $model = new OrdenRutaForm();
     $model->setScenario('create');
     //die(var_dump(Yii::$app->request->post()['orden-ruta-form']));
     $model->load(Yii::$app->request->post());
     if ($model->ordenComercios != null && $model->ordenComercios != '') {
         $comerciosOrdenados = explode(',', $model->ordenComercios);
         $orden = 1;
         foreach ($comerciosOrdenados as $idComercio) {
             $ordenComercio = new OrdenComercio();
             $ordenComercio->id_comercio = $idComercio;
             $ordenComercio->id_ruta = $model->idRuta;
             $ordenComercio->orden = $orden;
             $ordenComercio->save();
             $orden++;
         }
         return $this->redirect(['ruta/view', 'id' => $model->idRuta]);
     } else {
         Yii::$app->getSession()->setFlash('danger', Yii::t('core', 'You must select at least 1 Store'));
         return $this->redirect(['generar-ruta-manual', 'idRuta' => $model->idRuta, 'idRelevador' => $model->idUsuario, 'dia' => $model->dia]);
     }
 }