/**
  * Creates a new ReclamoSugerencia model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ReclamoSugerencia();
     if (Yii::$app->request->isAjax && $model->load($_POST)) {
         Yii::$app->response->format = 'json';
         return \yii\widgets\ActiveForm::validate($model);
     }
     if ($model->load(Yii::$app->request->post())) {
         $model->REC_FECHA = date('Y-m-d');
         $model->REC_HORA = date('H:i:s');
         $query = new Query();
         $query->select('REC_NUMERO')->from('RECLAMO_SUGERENCIA')->where('YEAR(REC_FECHA) = DATEPART(yyyy,getDate())')->orderBy('REC_NUMERO DESC')->limit('1');
         $rows = $query->one();
         $current_year = date('Y');
         if ($rows) {
             $past = implode($rows);
             $id = explode("-", $past);
             $tmp_last_id = (int) $id[0] + 1;
             if ($current_year != $id[1]) {
                 $tmp_last_id = 0;
             }
             $digits_count = preg_match_all("/[0-9]/", $tmp_last_id);
             $last_id = $tmp_last_id . "-" . $current_year;
             if ($digits_count < 2) {
                 $last_id = 0 . $last_id;
             }
         } else {
             $new_id = 0 . "-" . $current_year;
             $last_id = 0 . $new_id;
         }
         $model->REC_NUMERO = $last_id;
         $model->ERS_ID = 1;
         //Instancia para el adjunto
         $name = 'solicitud ' . $model->REC_NUMERO . ' ' . $model->REC_FECHA . ' ' . date('H i');
         $model->file = UploadedFile::getInstance($model, 'file');
         $model->save();
         //si el archivo no es null, entonces lo guarda y guarda el adjunto en la bd.
         if ($model->file != null) {
             $model->file->saveAs('uploads/reclamo-sugerencia/Adjunto ' . $name . '.' . $model->file->extension);
             //guardar la ruta en la bd
             $adjunto = new Adjuntos();
             $adjunto->REC_NUMERO = $model->REC_NUMERO;
             $adjunto->ADJ_TIPO = 'Reclamo-Sugerencia';
             $adjunto->ADJ_URL = 'uploads/reclamo-sugerencia/Adjunto ' . $name . '.' . $model->file->extension;
             $adjunto->save();
         }
         //guardar Creacion en el Historial
         $historial = new HistorialEstados();
         $historial->REC_NUMERO = $model->REC_NUMERO;
         $historial->ERS_ID = $model->ERS_ID;
         $historial->USU_RUT = $model->USU_RUT;
         $historial->HES_FECHA_HORA = date('Y-m-d H:i:s');
         if ($model->TRS_ID == 1) {
             $historial->HES_COMENTARIO = "El usuario " . $historial->USU_RUT . " ha ingresado el Reclamo Nº " . $historial->REC_NUMERO . " el día " . $historial->HES_FECHA_HORA;
         } else {
             $historial->HES_COMENTARIO = "El usuario " . $historial->USU_RUT . " ha ingresado la Sugerencia Nº " . $historial->REC_NUMERO . " el día " . $historial->HES_FECHA_HORA;
         }
         $historial->save();
         return $this->redirect(['view', 'id' => $model->REC_NUMERO]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }