From 8392f5bfad491f15c5c93d30a8d05ecc662f6180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Mon, 24 Feb 2014 00:11:41 +0100 Subject: [PATCH] =?UTF-8?q?ref=20#3=20Casi=20terminado=20el=20proceso=20de?= =?UTF-8?q?=20importaci=C3=B3n=20de=20datos,=20incluida=20la=20gesti=C3=B3?= =?UTF-8?q?n=20de=20transacciones.=20Falta=20mostrar=20progreso=20de=20la?= =?UTF-8?q?=20importaci=C3=B3n=20o=20no.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Csv.php | 224 ++++++++++++++++++++++++++--------- Importacion.php | 31 +++-- InformeInventario.php | 2 +- Sql.php | 14 +++ plant/bootstrap.html | 19 ++- xml/informeUbicaciones_1.xml | 14 --- xml/informeUbicaciones_2.xml | 14 --- xml/informeUbicaciones_3.xml | 14 --- 8 files changed, 224 insertions(+), 108 deletions(-) delete mode 100644 xml/informeUbicaciones_1.xml delete mode 100644 xml/informeUbicaciones_2.xml delete mode 100644 xml/informeUbicaciones_3.xml diff --git a/Csv.php b/Csv.php index ec7eb2d..8904db8 100644 --- a/Csv.php +++ b/Csv.php @@ -48,6 +48,16 @@ class Csv { */ private $numRegistros; + /** + * @var array Cabecera del archivo csv + */ + private $cabecera; + + /** + * @var array los datos del fichero csv en memoria + */ + private $datosFichero; + /** // El constructor necesita saber cuál es la opción actual /** @@ -75,6 +85,10 @@ class Csv { fputcsv($this->fichero, $datos, ',', '"') or die("No puedo escribir en el fichero csv"); } + public function __destruct() { + $this->cierra(); + } + public function cierra() { fclose($this->fichero) or die("No puedo cerrar el archivo csv"); } @@ -86,7 +100,7 @@ class Csv { public function ejecutaConsulta($fichero) { $consulta = simplexml_load_file($fichero) or die("No puedo cargar el fichero xml " . $fichero . " al csv"); // Escribe la cabecera del fichero - $this->escribeLinea(array($consulta->Pagina->Cabecera,$consulta->Titulo['id'],$consulta->Titulo['Texto'])); + $this->escribeLinea(array($consulta->Pagina->Cabecera, $consulta->Titulo['id'], $consulta->Titulo['Texto'])); foreach ($consulta->Pagina->Cuerpo->Col as $campo) { $campos[] = utf8_decode($campo['Titulo']); } @@ -101,48 +115,7 @@ class Csv { $this->escribeLinea($campos); } } - private function quitaComillas($dato) { - //return substr($dato, 1, -1); - return str_replace("\"", "", $dato); - } - /** - * - * @param String $ficheroCSV Nombre del archivo csv - */ - public function cargaCSV2($ficheroCSV) { - $this->nombre = $ficheroCSV; - $this->fichero = fopen($this->nombre, "r") or die('No puedo abrir el archivo ' . $this->nombre . " para lectura."); - $linea = fgets($this->fichero); - $datos = explode(",", $linea); - $cabecera = $this->quitaComillas($datos[0]); - $idCabecera = $this->quitaComillas($datos[1]); - \fgets($this->fichero); - $linea = fgets($this->fichero); - $linea = $this->quitaComillas($linea); - $linea = explode(",", $linea); - $linea = explode(" ", $linea[0]); - $archivo = trim($linea[2]); - $ficheroXML = "xml/inventario" . ucfirst($archivo) . "CSV.xml"; - $consulta = simplexml_load_file($ficheroXML) or die("No puedo cargar el fichero xml " . $ficheroXML . " al cargar csv"); - \fgets($this->fichero); - $lineas = 0; $datosFichero = array(); - //var_dump($consulta); - while ($linea = fgets($this->fichero)) { - $datos = explode(",", $linea); - $i = 1; - $lineas++; - $datosFichero["Baja"][] = $this->quitaComillas($linea[0]); - foreach ($consulta->Pagina->Cuerpo->Col as $campo) { - $datosFichero[utf8_decode($campo['Titulo'])][] = $this->quitaComillas($datos[$i++]); - //echo "datosfichero"; var_dump($datosFichero); - //echo "campo=[$campo]"; - } - $datosFichero["Cant Real"] = $linea[$i]; - } - $this->numRegistros = $lineas; - return $this->Resumen($cabecera, $idCabecera, $archivo, $datosFichero, $consulta); - } /** * * @param String $ficheroCSV Nombre del archivo csv @@ -154,34 +127,175 @@ class Csv { while ($linea = fgetcsv($this->fichero)) { $datosFichero[] = $linea; } - return $this->Resumen($cabecera, $idCabecera, $archivo, $datosFichero); + $this->cabecera[] = $archivo; + $this->cabecera[] = $idCabecera; + $this->cabecera[] = $cabecera; + $this->datosFichero = $datosFichero; } - public function Resumen($cabecera, $idCabecera, $archivo, $datosFichero) { + /** + * Muestra un resumen de los datos del fichero csv cargado por pantalla + * + */ + public function resumen() { //$mensaje .= - $mensaje = "

Archivo [inventario".utf8_decode($archivo)."]

"; - $mensaje .= "

id=[$idCabecera] Descripción=[".utf8_decode($cabecera)."]


"; + $mensaje = "

Archivo [inventario" . utf8_decode($this->cabecera[0]) . "]

"; + $mensaje .= "

id=[" . $this->cabecera[1] . "] Descripción=[" . utf8_decode($this->cabecera[2]) . "]


"; $mensaje .= ''; - foreach ($datosFichero[0] as $campo) { + foreach ($this->datosFichero[0] as $campo) { $dato = $campo; $mensaje .= ""; } + $mensaje .= ""; $mensaje .=""; //echo "$mensaje contar Datosfichero=[".count($datosFichero)."]"; - for ($i=1; $i < count($datosFichero); $i++) { - $mensaje .= ""; - foreach($datosFichero[$i] as $dato) { - $mensaje .= ""; + try { + for ($i = 1; $i < count($this->datosFichero); $i++) { + $mensaje .= ""; + $primero = true; + foreach ($this->datosFichero[$i] as $dato) { + if ($primero) { + $primero = false; + switch ($dato) { + case 'S': $estado = "-Baja-"; + $color = "danger"; + break; + case 'Alta': $estado = "-Alta-"; + $color = "primary"; + break; + case "N" : $estado = $this->compruebaCantidades($this->datosFichero[$i]); + if ($estado != 0) { + $color = "warning"; + if ($estado > 0) { + $estado = "+".$estado; + } + } else { + $estado = "igual"; + $color = "info"; + } + break; + default: throw new Exception("El archivo csv tiene un formato incorrecto.
Bajas=[$dato]"); + } + } + $mensaje .= ""; + } + $mensaje .= ''; + $mensaje .= ""; } - $mensaje .= ""; + } catch (Exception $e) { + $mensaje = "Se ha producido el error [" . $e->getMessage() . "]
NO se ha realizado ningún cambio en la Base de Datos."; + return $mensaje; } $mensaje .= "
$datoAcción
".$dato."
" . $dato . "


"; - $mensaje .= '
- -
'; + $mensaje .= $this->panelMensaje('Si se produce cualquier error en el procesamiento del fichero, no se aplicará ningún cambio en la base de datos.'); + + $mensaje .= '
+ + + +
'; + return $mensaje; } + /** + * + * @param $array línea de datos del fichero csv para comprobar las cantidades si se han modificado o no + * @return string + */ + private function compruebaCantidades($datos) { + $ultimo = count($datos); + return $datos[$ultimo - 2] - $datos[$ultimo - 1]; + } + + private function panelMensaje($info) { + $mensaje = '
'; + $mensaje .= '

ATENCIÓN

'; + $mensaje .= '
'; + $mensaje .= $info; + $mensaje .= '
'; + $mensaje .= '
'; + return $mensaje; + } + + private function escribeFic($comando) + { + $fp = fopen("tmp/comandos","a"); + fputs($fp,$comando); + fclose($fp); + } + + private function bajaElemento($i) + { + $id = $this->datosFichero['idElem'][$i]; + $comando = 'delete from Elementos where id="'.$id.'";'; + $this->escribeFic($comando); + } + + private function modificaElemento($i) + { + $id = $this->datosFichero['idElem'][$i]; + $comando = 'update Elementos set Cantidad="'.$datosFichero['cantidadReal'][$i].'" where id="'.$id.'";'; + $this->escribeFic($comando); + } + + private function altaElemento($i) + { + if ($this->cabecera[0] == "Articulo") { + $idUbicacion = $this->datosFichero['idUbic'][$i]; + $idArticulo = $this->cabecera[1]; + } else { + $idUbicacion = $this->cabecera[1]; + $idArticulo = $this->datosFichero['idArt'][$i]; + } + $idArt = $datosFichero['idArt'][$i]; + $comando = 'insert into Elementos () values (null,"'.$idArticulo.'","'.$idUbicacion.'","'.$this->datosFichero['N Serie'][$i] + .'",'.$this->datosFichero['Cant. Real'][$i].',"'.$this->datosFichero['Fecha C.'].'");'; + $this->escribeFic($comando); + } + + /** + * Procesa contra la base de datos todas las acciones del archivo + */ + public function ejecutaFichero() { + //Realiza una transacción para que no se ejecute parcialmente una actualización + try { + $this->bdd->comienzaTransaccion(); + $campos = $this->datosFichero[0]; + for ($i = 1; $i < count($this->datosFichero); $i++) { + switch ($this->datosFichero[$i][0]) { + case 'S': + $this->bajaElemento($i); + break; + case 'Alta': + $this->altaElemento($this->datosFichero[$i]); + break; + case 'N': + if ($this->compruebaCantidades($this->datosFichero[$i]) != 0) { + $this->modificaElemento($this->datosFichero[$i]); + } + break; + default: throw new Exception("Acción no reconocida en la importacion [" . $this->datosFichero[0] . "]"); + } + } + } catch (Exception $e) { + $this->bdd->abortaTransaccion(); + $mensaje = "Se ha producido el error [" . $e->getMessage() . "]
NO se ha realizado ningún cambio en la Base de Datos."; + return $this->panelMensaje($mensaje); + } + } + + public function ejecutaFichero2() { + echo ''; + for ($i = 1; $i < 80; $i++) { + //sleep(1); + $progreso = $i; + echo ''; + //echo str_repeat(' ',1024*64); + flush(); + //echo '$(".bar").css("width", "'.$progreso.'");'; + } + } } ?> diff --git a/Importacion.php b/Importacion.php index 0bcd8e4..db3935c 100644 --- a/Importacion.php +++ b/Importacion.php @@ -31,11 +31,15 @@ class Importacion { } public function ejecuta() { - $opc = $_GET['opc']; + $opc = ''; + if (isset($_GET['opc'])) { + $opc = $_GET['opc']; + } switch ($opc) { case 'form':return $this->formulario(); case 'importar':return $this->importarFichero(); - case 'ejecutar':return "Ok"; + case 'ejecutar':return $this->ejecutaFichero(); + default: return "Importacion: No entiendo qué me has pedido."; } } @@ -45,26 +49,35 @@ class Importacion { die('No se pudo subir el fichero ' . $_FILES['userfile']['tmp_name']); } $csv = new Csv($this->bdd); - return $csv->cargaCSV($uploadfile); + $csv->cargaCSV($uploadfile); + return $csv->resumen(); } private function formulario() { $accion = "index.php?importacion&opc=importar"; $salida = '
' . "\n"; - $salida .="

Elige Archivo\n"; - $salida .= ''; - $salida .='


' . "\n"; - $salida .=""; return $salida; } + private function ejecutaFichero() { + $archivo = $_POST['ficheroCSV']; + $csv = new Csv($this->bdd); + $csv->cargaCSV($archivo); + return $csv->ejecutaFichero(); + } } ?> diff --git a/InformeInventario.php b/InformeInventario.php index c7331ae..43e5d53 100644 --- a/InformeInventario.php +++ b/InformeInventario.php @@ -150,7 +150,7 @@ class InformeInventario { $salida.=$lista; $salida.="

"; $salida.='
'; - $salida.='
'; + $salida.='
'; $salida.="

"; $salida.='


' . "\n"; return $salida; diff --git a/Sql.php b/Sql.php index a107c58..46dc784 100644 --- a/Sql.php +++ b/Sql.php @@ -193,6 +193,20 @@ class Sql { { return $this->bdd; } + public function comienzaTransaccion() + { + return $this->bdd->autocommit(false); + } + public function abortaTransaccion() + { + return $this->bdd->rollback(); + } + public function finalizaTransaccion() + { + $codigo = $this->bdd->commit(); + $this->bdd->autocommit(true); + return $codigo; + } } ?> diff --git a/plant/bootstrap.html b/plant/bootstrap.html index 0271135..3f3364a 100644 --- a/plant/bootstrap.html +++ b/plant/bootstrap.html @@ -33,7 +33,7 @@ {aplicacion}
+
+
+ 0% Completado +
+
+ {contenido}
diff --git a/xml/informeUbicaciones_1.xml b/xml/informeUbicaciones_1.xml deleted file mode 100644 index 593d7e1..0000000 --- a/xml/informeUbicaciones_1.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - select * from Ubicaciones order by Descripcion; - - - Relación alfabética - - - - - - diff --git a/xml/informeUbicaciones_2.xml b/xml/informeUbicaciones_2.xml deleted file mode 100644 index 593d7e1..0000000 --- a/xml/informeUbicaciones_2.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - select * from Ubicaciones order by Descripcion; - - - Relación alfabética - - - - - - diff --git a/xml/informeUbicaciones_3.xml b/xml/informeUbicaciones_3.xml deleted file mode 100644 index 593d7e1..0000000 --- a/xml/informeUbicaciones_3.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - select * from Ubicaciones order by Descripcion; - - - Relación alfabética - - - - - -