ref #3 Casi terminado el proceso de importación de datos, incluida la gestión de transacciones. Falta mostrar progreso de la importación o no.

This commit is contained in:
2014-02-24 00:11:41 +01:00
parent 37a2afa9f4
commit 8392f5bfad
8 changed files with 224 additions and 108 deletions

224
Csv.php
View File

@@ -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 = "<center><h1>Archivo [inventario".utf8_decode($archivo)."]</h1>";
$mensaje .= "<h2>id=[$idCabecera] Descripci&oacute;n=[".utf8_decode($cabecera)."]</h2><br>";
$mensaje = "<center><h1>Archivo [inventario" . utf8_decode($this->cabecera[0]) . "]</h1>";
$mensaje .= "<h2>id=[" . $this->cabecera[1] . "] Descripci&oacute;n=[" . utf8_decode($this->cabecera[2]) . "]</h2><br>";
$mensaje .= '<table border=1 class="table table-striped table-bordered table-condensed table-hover"><theader>';
foreach ($datosFichero[0] as $campo) {
foreach ($this->datosFichero[0] as $campo) {
$dato = $campo;
$mensaje .= "<th><b>$dato</b></th>";
}
$mensaje .= "<th><b>Acci&oacute;n</b></th>";
$mensaje .="</theader><tbody>";
//echo "$mensaje contar Datosfichero=[".count($datosFichero)."]";
for ($i=1; $i < count($datosFichero); $i++) {
$mensaje .= "<tr>";
foreach($datosFichero[$i] as $dato) {
$mensaje .= "<td>".$dato."</td>";
try {
for ($i = 1; $i < count($this->datosFichero); $i++) {
$mensaje .= "<tr>";
$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.<br>Bajas=[$dato]");
}
}
$mensaje .= "<td>" . $dato . "</td>";
}
$mensaje .= '<td align="center"><label class="label label-' . $color . '">' . $estado . '</label></td>';
$mensaje .= "</tr>";
}
$mensaje .= "</tr>";
} catch (Exception $e) {
$mensaje = "Se ha producido el error [" . $e->getMessage() . "]<br>NO se ha realizado ning&uacute;n cambio en la Base de Datos.";
return $mensaje;
}
$mensaje .= "</tbody></table></p><br>";
$mensaje .= '<form method="post" name="Aceptar" action="index.php?Importacion&opc=Ejecutar">
<input type="button" name="Cancelar" value="Cancelar" onClick="location.href=' . "'index.php'" .'" class="btn btn-danger">
<input type="submit" name="Aceptar" value="Aceptar" class="btn btn-primary"></form></center>';
$mensaje .= $this->panelMensaje('Si se produce cualquier error en el procesamiento del fichero, no se aplicar&aacute; ning&uacute;n cambio en la base de datos.');
$mensaje .= '<form method="post" name="Aceptar" action="index.php?importacion&opc=ejecutar">
<input type="button" name="Cancelar" value="Cancelar" onClick="location.href=' . "'index.php'" . '" class="btn btn-danger">
<input type="submit" name="Aceptar" value="Aceptar" class="btn btn-primary">
<input type="hidden" name="ficheroCSV" value="' . $this->nombre . '">
</form></center>';
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 = '<div class="panel panel-danger"><div class="panel-heading">';
$mensaje .= '<h3 class="panel-title">ATENCI&Oacute;N</h3></div>';
$mensaje .= '<div class="panel-body">';
$mensaje .= $info;
$mensaje .= '</div>';
$mensaje .= '</div>';
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&oacute;n no reconocida en la importacion [" . $this->datosFichero[0] . "]");
}
}
} catch (Exception $e) {
$this->bdd->abortaTransaccion();
$mensaje = "Se ha producido el error [" . $e->getMessage() . "]<br>NO se ha realizado ning&uacute;n cambio en la Base de Datos.";
return $this->panelMensaje($mensaje);
}
}
public function ejecutaFichero2() {
echo '<script>visualizaProgreso();</script>';
for ($i = 1; $i < 80; $i++) {
//sleep(1);
$progreso = $i;
echo '<script>actProgreso('.$progreso.');</script>';
//echo str_repeat(' ',1024*64);
flush();
//echo '$(".bar").css("width", "'.$progreso.'");';
}
}
}
?>