Esempio n. 1
0
 public function activePrinciplesUpload()
 {
     $active_principle = new ActivePrinciple();
     $recommendation = new Recommendation();
     $input = Input::All();
     $header = array_shift($input);
     if (!$this->checkHeader($header, $active_principle->columns())) {
         return Response::json(array("success" => false, "info" => "Las cabeceras del excel no coinciden con la base de datos.\n                    No se procederá a modificar la base de datos."));
     }
     DB::table($recommendation->getTableName())->update(array("active_principle_id" => NULL));
     DB::table($active_principle->getTableName())->delete();
     foreach ($input as $row) {
         try {
             $row = $this->readRow($row, $header);
             $a_p = new ActivePrinciple($row);
             $a_p->save();
         } catch (Exception $ex) {
             return Response::json(array("success" => false, "info" => "Ha habido algún problema cargando el Excel en la base de datos." . "\n" . $ex->getMessage()));
         }
     }
     $recommendations = Recommendation::All();
     if (count($recommendations) > 0) {
         foreach ($recommendations as $r) {
             $a_p = DB::table($active_principle->getTableName())->select("id", "principio_activo")->where("principio_activo", "=", $r->principio_activo)->first();
             if ($a_p == NULL) {
                 $r->delete();
             } else {
                 $r->active_principle_id = $a_p->id;
                 $r->principio_activo = $a_p->principio_activo;
                 $r->save();
             }
         }
     }
     return Response::json(array("success" => true, "info" => "Se ha cargado el excel en la base de datos correctamente."));
 }