From 93841811286914cf2f5f40fe745240ccc61b81e9 Mon Sep 17 00:00:00 2001 From: rmontanana Date: Mon, 24 Feb 2014 12:36:43 +0100 Subject: [PATCH] =?UTF-8?q?ref#=203=20Casi=20terminada=20la=20importaci?= =?UTF-8?q?=C3=B3n=20de=20datos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Csv.php | 171 +++++++++++++++++++++++++----------------- InformeInventario.php | 2 - Sql.php | 4 +- plant/bootstrap.html | 4 +- 4 files changed, 108 insertions(+), 73 deletions(-) diff --git a/Csv.php b/Csv.php index 8904db8..220f84a 100644 --- a/Csv.php +++ b/Csv.php @@ -58,6 +58,18 @@ class Csv { */ private $datosFichero; + /** + * Indices a los campos correspondientes + * + */ + private $idElemento; + private $idArticulo; + private $idUbicacion; + private $fechaCompra; + private $cantidad; + private $cantidadReal; + private $nSerie; + /** // El constructor necesita saber cuál es la opción actual /** @@ -148,47 +160,43 @@ class Csv { } $mensaje .= "Acción"; $mensaje .=""; + $this->cargaIndices($this->datosFichero[0]); //echo "$mensaje contar Datosfichero=[".count($datosFichero)."]"; - 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"; + 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($i); + if ($estado != 0) { + $color = "warning"; + if ($estado > 0) { + $estado = "+" . $estado; } - break; - default: throw new Exception("El archivo csv tiene un formato incorrecto.
Bajas=[$dato]"); - } + } else { + $estado = "igual"; + $color = "info"; + } + break; + default: throw new Exception("El archivo csv tiene un formato incorrecto.
Bajas=[$dato]"); } - $mensaje .= "" . $dato . ""; } - $mensaje .= ''; - $mensaje .= ""; + $mensaje .= "" . $dato . ""; } - } 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 .= ''; + $mensaje .= ""; } $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 .= '
@@ -203,81 +211,107 @@ class Csv { * @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) { + private function compruebaCantidades($i) { $ultimo = count($datos); - return $datos[$ultimo - 2] - $datos[$ultimo - 1]; + return $this->datosFichero[$i][$this->cantidadReal] - $this->datosFichero[$i][$this->cantidad]; } - private function panelMensaje($info) { - $mensaje = '
'; - $mensaje .= '

ATENCIÓN

'; + private function panelMensaje($info, $tipo = "danger", $cabecera = "¡Atención!") { + $mensaje = '
'; + $mensaje .= '

' . $cabecera . '

'; $mensaje .= '
'; $mensaje .= $info; $mensaje .= '
'; $mensaje .= '
'; return $mensaje; } - - private function escribeFic($comando) - { - $fp = fopen("tmp/comandos","a"); - fputs($fp,$comando); + + private function escribeFic($comando) { + $fp = fopen("tmp/comandos", "a"); + fputs($fp, $comando . "\n"); fclose($fp); } - - private function bajaElemento($i) - { - $id = $this->datosFichero['idElem'][$i]; - $comando = 'delete from Elementos where id="'.$id.'";'; + + private function bajaElemento($i) { + $id = $this->datosFichero[$i][$this->idElemento]; + $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.'";'; + + private function modificaElemento($i) { + $id = $this->datosFichero[$i][$this->idElemento]; + $comando = 'update Elementos set Cantidad=' . $this->datosFichero[$i][$this->cantidadReal] . ' where id="' . $id . '";'; $this->escribeFic($comando); } - - private function altaElemento($i) - { + + private function altaElemento($i) { if ($this->cabecera[0] == "Articulo") { - $idUbicacion = $this->datosFichero['idUbic'][$i]; + $idUbicacion = $this->datosFichero[$i][$this->idUbicacion]; $idArticulo = $this->cabecera[1]; + $comando = 'select id from Ubicaciones where Descripcion="'.$this->datosFichero[$i][$this->desUbicacion].'";'; } 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.'].'");'; + $idArticulo = $this->datosFichero[$i][$this->idArticulo]; + } + $idArt = $datosFichero[$i]; + $comando = 'insert into Elementos () values (null,' . $idArticulo . ',' . $idUbicacion . ',"' . $this->datosFichero[$i][$this->nSerie]; + $comando .= '",' . $this->datosFichero[$i][$this->cantidadReal] . ',"' . $this->datosFichero[$i][$this->fechaCompra] . '");'; $this->escribeFic($comando); } + private function cargaIndices($campos) { + for ($i = 0; $i < count($campos); $i++) { + switch ($campos[$i]) { + case "Cant. Real": $this->cantidadReal = $i; + break; + case "Fecha C.": $this->fechaCompra = $i; + break; + case "idUbic": $this->idUbicacion = $i; + break; + case "idArt": $this->idArticulo = $i; + break; + case "idElem": $this->idElemento = $i; + break; + case "Cantidad": $this->cantidad = $i; + break; + case "N Serie": $this->nSerie = $i; + break; + } + } + $this->EscribeFic("Cantreal=[$this->cantidadReal] fechacompra=[$this->fechaCompra] ubicacion=[$this->idUbicacion] articulo=[$this->idArticulo]"); + $this->EscribeFic("idElemento=[$this->idElemento] Cantidad=[$this->cantidad] nserie=[$this->nserie]"); + } + /** * Procesa contra la base de datos todas las acciones del archivo */ public function ejecutaFichero() { + $this->cargaIndices($this->datosFichero[0]); //Realiza una transacción para que no se ejecute parcialmente una actualización try { $this->bdd->comienzaTransaccion(); - $campos = $this->datosFichero[0]; + $acciones = 0; for ($i = 1; $i < count($this->datosFichero); $i++) { switch ($this->datosFichero[$i][0]) { case 'S': $this->bajaElemento($i); + $acciones++; break; case 'Alta': - $this->altaElemento($this->datosFichero[$i]); + $this->altaElemento($i); + $acciones++; break; case 'N': - if ($this->compruebaCantidades($this->datosFichero[$i]) != 0) { - $this->modificaElemento($this->datosFichero[$i]); + if ($this->compruebaCantidades($i) != 0) { + $this->modificaElemento($i); + $acciones++; } break; default: throw new Exception("Acción no reconocida en la importacion [" . $this->datosFichero[0] . "]"); } } + $mensaje = "Se han procesado correctamente $acciones acciones en la Base de Datos."; + return $this->panelMensaje($mensaje,"success", "Información"); } 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."; @@ -285,17 +319,18 @@ class Csv { } } - public function ejecutaFichero2() { + private function ejecutaFichero2() { echo ''; for ($i = 1; $i < 80; $i++) { //sleep(1); $progreso = $i; - echo ''; + echo ''; //echo str_repeat(' ',1024*64); flush(); //echo '$(".bar").css("width", "'.$progreso.'");'; } } + } ?> diff --git a/InformeInventario.php b/InformeInventario.php index 43e5d53..6138e3a 100644 --- a/InformeInventario.php +++ b/InformeInventario.php @@ -70,7 +70,6 @@ class InformeInventario { $hoja = new Csv($this->bdd); $hoja->crea($nombre); $hoja->ejecutaConsulta($salida); - $hoja->cierra(); echo ''; } } @@ -109,7 +108,6 @@ class InformeInventario { $hoja = new Csv($this->bdd); $hoja->crea($nombre); $hoja->ejecutaConsulta($salida); - $hoja->cierra(); echo ''; } //header('Location: index.php'); diff --git a/Sql.php b/Sql.php index 46dc784..cd06898 100644 --- a/Sql.php +++ b/Sql.php @@ -199,7 +199,9 @@ class Sql { } public function abortaTransaccion() { - return $this->bdd->rollback(); + $codigo = $this->bdd->rollback(); + $this->bdd->autocommit(true); + return $codigo; } public function finalizaTransaccion() { diff --git a/plant/bootstrap.html b/plant/bootstrap.html index 3f3364a..b7692cb 100644 --- a/plant/bootstrap.html +++ b/plant/bootstrap.html @@ -57,7 +57,7 @@
-
+ {contenido}