mirror of
https://github.com/rmontanana/inventario2.git
synced 2025-08-16 07:56:00 +00:00
ref# 3 Casi terminada la importación de datos
This commit is contained in:
171
Csv.php
171
Csv.php
@@ -58,6 +58,18 @@ class Csv {
|
|||||||
*/
|
*/
|
||||||
private $datosFichero;
|
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
|
// El constructor necesita saber cuál es la opción actual
|
||||||
/**
|
/**
|
||||||
@@ -148,47 +160,43 @@ class Csv {
|
|||||||
}
|
}
|
||||||
$mensaje .= "<th><b>Acción</b></th>";
|
$mensaje .= "<th><b>Acción</b></th>";
|
||||||
$mensaje .="</theader><tbody>";
|
$mensaje .="</theader><tbody>";
|
||||||
|
$this->cargaIndices($this->datosFichero[0]);
|
||||||
//echo "$mensaje contar Datosfichero=[".count($datosFichero)."]";
|
//echo "$mensaje contar Datosfichero=[".count($datosFichero)."]";
|
||||||
try {
|
for ($i = 1; $i < count($this->datosFichero); $i++) {
|
||||||
for ($i = 1; $i < count($this->datosFichero); $i++) {
|
$mensaje .= "<tr>";
|
||||||
$mensaje .= "<tr>";
|
$primero = true;
|
||||||
$primero = true;
|
foreach ($this->datosFichero[$i] as $dato) {
|
||||||
foreach ($this->datosFichero[$i] as $dato) {
|
if ($primero) {
|
||||||
if ($primero) {
|
$primero = false;
|
||||||
$primero = false;
|
switch ($dato) {
|
||||||
switch ($dato) {
|
case 'S': $estado = "-Baja-";
|
||||||
case 'S': $estado = "-Baja-";
|
$color = "danger";
|
||||||
$color = "danger";
|
break;
|
||||||
break;
|
case 'Alta': $estado = "-Alta-";
|
||||||
case 'Alta': $estado = "-Alta-";
|
$color = "primary";
|
||||||
$color = "primary";
|
break;
|
||||||
break;
|
case "N" : $estado = $this->compruebaCantidades($i);
|
||||||
case "N" : $estado = $this->compruebaCantidades($this->datosFichero[$i]);
|
if ($estado != 0) {
|
||||||
if ($estado != 0) {
|
$color = "warning";
|
||||||
$color = "warning";
|
if ($estado > 0) {
|
||||||
if ($estado > 0) {
|
$estado = "+" . $estado;
|
||||||
$estado = "+".$estado;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$estado = "igual";
|
|
||||||
$color = "info";
|
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
default: throw new Exception("El archivo csv tiene un formato incorrecto.<br>Bajas=[$dato]");
|
$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 .= "<td>" . $dato . "</td>";
|
||||||
$mensaje .= "</tr>";
|
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
$mensaje .= '<td align="center"><label class="label label-' . $color . '">' . $estado . '</label></td>';
|
||||||
$mensaje = "Se ha producido el error [" . $e->getMessage() . "]<br>NO se ha realizado ningún cambio en la Base de Datos.";
|
$mensaje .= "</tr>";
|
||||||
return $mensaje;
|
|
||||||
}
|
}
|
||||||
$mensaje .= "</tbody></table></p><br>";
|
$mensaje .= "</tbody></table></p><br>";
|
||||||
$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 .= $this->panelMensaje('Si se produce cualquier error en el procesamiento del fichero, no se aplicará ningún cambio en la base de datos.');
|
||||||
|
|
||||||
$mensaje .= '<form method="post" name="Aceptar" action="index.php?importacion&opc=ejecutar">
|
$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="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="submit" name="Aceptar" value="Aceptar" class="btn btn-primary">
|
||||||
@@ -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
|
* @param $array línea de datos del fichero csv para comprobar las cantidades si se han modificado o no
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function compruebaCantidades($datos) {
|
private function compruebaCantidades($i) {
|
||||||
$ultimo = count($datos);
|
$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) {
|
private function panelMensaje($info, $tipo = "danger", $cabecera = "¡Atención!") {
|
||||||
$mensaje = '<div class="panel panel-danger"><div class="panel-heading">';
|
$mensaje = '<div class="panel panel-' . $tipo . '"><div class="panel-heading">';
|
||||||
$mensaje .= '<h3 class="panel-title">ATENCIÓN</h3></div>';
|
$mensaje .= '<h3 class="panel-title">' . $cabecera . '</h3></div>';
|
||||||
$mensaje .= '<div class="panel-body">';
|
$mensaje .= '<div class="panel-body">';
|
||||||
$mensaje .= $info;
|
$mensaje .= $info;
|
||||||
$mensaje .= '</div>';
|
$mensaje .= '</div>';
|
||||||
$mensaje .= '</div>';
|
$mensaje .= '</div>';
|
||||||
return $mensaje;
|
return $mensaje;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function escribeFic($comando)
|
private function escribeFic($comando) {
|
||||||
{
|
$fp = fopen("tmp/comandos", "a");
|
||||||
$fp = fopen("tmp/comandos","a");
|
fputs($fp, $comando . "\n");
|
||||||
fputs($fp,$comando);
|
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function bajaElemento($i)
|
private function bajaElemento($i) {
|
||||||
{
|
$id = $this->datosFichero[$i][$this->idElemento];
|
||||||
$id = $this->datosFichero['idElem'][$i];
|
$comando = 'delete from Elementos where id="' . $id . '";';
|
||||||
$comando = 'delete from Elementos where id="'.$id.'";';
|
|
||||||
$this->escribeFic($comando);
|
$this->escribeFic($comando);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function modificaElemento($i)
|
private function modificaElemento($i) {
|
||||||
{
|
$id = $this->datosFichero[$i][$this->idElemento];
|
||||||
$id = $this->datosFichero['idElem'][$i];
|
$comando = 'update Elementos set Cantidad=' . $this->datosFichero[$i][$this->cantidadReal] . ' where id="' . $id . '";';
|
||||||
$comando = 'update Elementos set Cantidad="'.$datosFichero['cantidadReal'][$i].'" where id="'.$id.'";';
|
|
||||||
$this->escribeFic($comando);
|
$this->escribeFic($comando);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function altaElemento($i)
|
private function altaElemento($i) {
|
||||||
{
|
|
||||||
if ($this->cabecera[0] == "Articulo") {
|
if ($this->cabecera[0] == "Articulo") {
|
||||||
$idUbicacion = $this->datosFichero['idUbic'][$i];
|
$idUbicacion = $this->datosFichero[$i][$this->idUbicacion];
|
||||||
$idArticulo = $this->cabecera[1];
|
$idArticulo = $this->cabecera[1];
|
||||||
|
$comando = 'select id from Ubicaciones where Descripcion="'.$this->datosFichero[$i][$this->desUbicacion].'";';
|
||||||
} else {
|
} else {
|
||||||
$idUbicacion = $this->cabecera[1];
|
$idUbicacion = $this->cabecera[1];
|
||||||
$idArticulo = $this->datosFichero['idArt'][$i];
|
$idArticulo = $this->datosFichero[$i][$this->idArticulo];
|
||||||
}
|
}
|
||||||
$idArt = $datosFichero['idArt'][$i];
|
$idArt = $datosFichero[$i];
|
||||||
$comando = 'insert into Elementos () values (null,"'.$idArticulo.'","'.$idUbicacion.'","'.$this->datosFichero['N Serie'][$i]
|
$comando = 'insert into Elementos () values (null,' . $idArticulo . ',' . $idUbicacion . ',"' . $this->datosFichero[$i][$this->nSerie];
|
||||||
.'",'.$this->datosFichero['Cant. Real'][$i].',"'.$this->datosFichero['Fecha C.'].'");';
|
$comando .= '",' . $this->datosFichero[$i][$this->cantidadReal] . ',"' . $this->datosFichero[$i][$this->fechaCompra] . '");';
|
||||||
$this->escribeFic($comando);
|
$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
|
* Procesa contra la base de datos todas las acciones del archivo
|
||||||
*/
|
*/
|
||||||
public function ejecutaFichero() {
|
public function ejecutaFichero() {
|
||||||
|
$this->cargaIndices($this->datosFichero[0]);
|
||||||
//Realiza una transacción para que no se ejecute parcialmente una actualización
|
//Realiza una transacción para que no se ejecute parcialmente una actualización
|
||||||
try {
|
try {
|
||||||
$this->bdd->comienzaTransaccion();
|
$this->bdd->comienzaTransaccion();
|
||||||
$campos = $this->datosFichero[0];
|
$acciones = 0;
|
||||||
for ($i = 1; $i < count($this->datosFichero); $i++) {
|
for ($i = 1; $i < count($this->datosFichero); $i++) {
|
||||||
switch ($this->datosFichero[$i][0]) {
|
switch ($this->datosFichero[$i][0]) {
|
||||||
case 'S':
|
case 'S':
|
||||||
$this->bajaElemento($i);
|
$this->bajaElemento($i);
|
||||||
|
$acciones++;
|
||||||
break;
|
break;
|
||||||
case 'Alta':
|
case 'Alta':
|
||||||
$this->altaElemento($this->datosFichero[$i]);
|
$this->altaElemento($i);
|
||||||
|
$acciones++;
|
||||||
break;
|
break;
|
||||||
case 'N':
|
case 'N':
|
||||||
if ($this->compruebaCantidades($this->datosFichero[$i]) != 0) {
|
if ($this->compruebaCantidades($i) != 0) {
|
||||||
$this->modificaElemento($this->datosFichero[$i]);
|
$this->modificaElemento($i);
|
||||||
|
$acciones++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default: throw new Exception("Acción no reconocida en la importacion [" . $this->datosFichero[0] . "]");
|
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) {
|
} catch (Exception $e) {
|
||||||
$this->bdd->abortaTransaccion();
|
$this->bdd->abortaTransaccion();
|
||||||
$mensaje = "Se ha producido el error [" . $e->getMessage() . "]<br>NO se ha realizado ningún cambio en la Base de Datos.";
|
$mensaje = "Se ha producido el error [" . $e->getMessage() . "]<br>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 '<script>visualizaProgreso();</script>';
|
echo '<script>visualizaProgreso();</script>';
|
||||||
for ($i = 1; $i < 80; $i++) {
|
for ($i = 1; $i < 80; $i++) {
|
||||||
//sleep(1);
|
//sleep(1);
|
||||||
$progreso = $i;
|
$progreso = $i;
|
||||||
echo '<script>actProgreso('.$progreso.');</script>';
|
echo '<script>actProgreso(' . $progreso . ');</script>';
|
||||||
//echo str_repeat(' ',1024*64);
|
//echo str_repeat(' ',1024*64);
|
||||||
flush();
|
flush();
|
||||||
//echo '$(".bar").css("width", "'.$progreso.'");';
|
//echo '$(".bar").css("width", "'.$progreso.'");';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -70,7 +70,6 @@ class InformeInventario {
|
|||||||
$hoja = new Csv($this->bdd);
|
$hoja = new Csv($this->bdd);
|
||||||
$hoja->crea($nombre);
|
$hoja->crea($nombre);
|
||||||
$hoja->ejecutaConsulta($salida);
|
$hoja->ejecutaConsulta($salida);
|
||||||
$hoja->cierra();
|
|
||||||
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
|
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,7 +108,6 @@ class InformeInventario {
|
|||||||
$hoja = new Csv($this->bdd);
|
$hoja = new Csv($this->bdd);
|
||||||
$hoja->crea($nombre);
|
$hoja->crea($nombre);
|
||||||
$hoja->ejecutaConsulta($salida);
|
$hoja->ejecutaConsulta($salida);
|
||||||
$hoja->cierra();
|
|
||||||
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
|
echo '<script type="text/javascript"> window.open( "' . $nombre . '" ) </script>';
|
||||||
}
|
}
|
||||||
//header('Location: index.php');
|
//header('Location: index.php');
|
||||||
|
4
Sql.php
4
Sql.php
@@ -199,7 +199,9 @@ class Sql {
|
|||||||
}
|
}
|
||||||
public function abortaTransaccion()
|
public function abortaTransaccion()
|
||||||
{
|
{
|
||||||
return $this->bdd->rollback();
|
$codigo = $this->bdd->rollback();
|
||||||
|
$this->bdd->autocommit(true);
|
||||||
|
return $codigo;
|
||||||
}
|
}
|
||||||
public function finalizaTransaccion()
|
public function finalizaTransaccion()
|
||||||
{
|
{
|
||||||
|
@@ -57,7 +57,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col-sm-10 col-sm-offset-1 col-md-11 col-md-offset-1 main">
|
<div class="col-sm-10 col-sm-offset-1 col-md-11 col-md-offset-1 main">
|
||||||
<!--<h1 class="page-header">{opcion}</h1>-->
|
<!--<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%">
|
<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>
|
<span id="barra-valor">0% Completado</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -73,7 +73,7 @@
|
|||||||
function escondeProgreso() {
|
function escondeProgreso() {
|
||||||
$('#divBara').style.visibility = "hidden";
|
$('#divBara').style.visibility = "hidden";
|
||||||
}
|
}
|
||||||
</script>
|
</script>-->
|
||||||
{contenido}
|
{contenido}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user