";
}
- $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 .= "
";
- $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 = '