ref# 3 Casi terminada la importación de datos

This commit is contained in:
rmontanana
2014-02-24 12:36:43 +01:00
parent 8392f5bfad
commit 9384181128
4 changed files with 108 additions and 73 deletions

109
Csv.php
View File

@@ -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,8 +160,8 @@ class Csv {
}
$mensaje .= "<th><b>Acci&oacute;n</b></th>";
$mensaje .="</theader><tbody>";
$this->cargaIndices($this->datosFichero[0]);
//echo "$mensaje contar Datosfichero=[".count($datosFichero)."]";
try {
for ($i = 1; $i < count($this->datosFichero); $i++) {
$mensaje .= "<tr>";
$primero = true;
@@ -163,11 +175,11 @@ class Csv {
case 'Alta': $estado = "-Alta-";
$color = "primary";
break;
case "N" : $estado = $this->compruebaCantidades($this->datosFichero[$i]);
case "N" : $estado = $this->compruebaCantidades($i);
if ($estado != 0) {
$color = "warning";
if ($estado > 0) {
$estado = "+".$estado;
$estado = "+" . $estado;
}
} else {
$estado = "igual";
@@ -182,10 +194,6 @@ class Csv {
$mensaje .= '<td align="center"><label class="label label-' . $color . '">' . $estado . '</label></td>';
$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 .= $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.');
@@ -203,14 +211,14 @@ 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 = '<div class="panel panel-danger"><div class="panel-heading">';
$mensaje .= '<h3 class="panel-title">ATENCI&Oacute;N</h3></div>';
private function panelMensaje($info, $tipo = "danger", $cabecera = "&iexcl;Atenci&oacute;n!") {
$mensaje = '<div class="panel panel-' . $tipo . '"><div class="panel-heading">';
$mensaje .= '<h3 class="panel-title">' . $cabecera . '</h3></div>';
$mensaje .= '<div class="panel-body">';
$mensaje .= $info;
$mensaje .= '</div>';
@@ -218,66 +226,92 @@ class Csv {
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];
$idArticulo = $this->datosFichero[$i][$this->idArticulo];
}
$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.'].'");';
$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&oacute;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&oacute;n");
} 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.";
@@ -285,17 +319,18 @@ class Csv {
}
}
public function ejecutaFichero2() {
private function ejecutaFichero2() {
echo '<script>visualizaProgreso();</script>';
for ($i = 1; $i < 80; $i++) {
//sleep(1);
$progreso = $i;
echo '<script>actProgreso('.$progreso.');</script>';
echo '<script>actProgreso(' . $progreso . ');</script>';
//echo str_repeat(' ',1024*64);
flush();
//echo '$(".bar").css("width", "'.$progreso.'");';
}
}
}
?>

View File

@@ -70,7 +70,6 @@ class InformeInventario {
$hoja = new Csv($this->bdd);
$hoja->crea($nombre);
$hoja->ejecutaConsulta($salida);
$hoja->cierra();
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
}
}
@@ -109,7 +108,6 @@ class InformeInventario {
$hoja = new Csv($this->bdd);
$hoja->crea($nombre);
$hoja->ejecutaConsulta($salida);
$hoja->cierra();
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
}
//header('Location: index.php');

View File

@@ -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()
{

View File

@@ -57,7 +57,7 @@
</div>
<div class="col-sm-10 col-sm-offset-1 col-md-11 col-md-offset-1 main">
<!--<h1 class="page-header">{opcion}</h1>-->
<div id="divBarra" class="progress progress-striped active">
<!--<div id="divBarra" class="progress progress-striped active">
<div class="progress-bar" id="barra" role="progressbar" aria-valuenow="0" aria-valuemin="40" aria-valuemax="100" style="width: 0%">
<span id="barra-valor">0% Completado</span>
</div>
@@ -73,7 +73,7 @@
function escondeProgreso() {
$('#divBara').style.visibility = "hidden";
}
</script>
</script>-->
{contenido}
</div>
</div>