$fecha = date("j")."/".date("n")."/".date("Y");
$hora = date("G").":".date("i")." hr.";
$usuario = $_SESSION['dpi'];
$nombreU = $_SESSION['nombre'].' '.$_SESSION['apellido'];
extract($_POST);
@$action = $_POST['action'];
if ($action == "upload") {
//cargamos el archivo al servidor con el mismo nombre
//solo le agregue el sufijo bak_
$archivo = $_FILES['excel']['name'];
$tipo = $_FILES['excel']['type'];
$destino = "bak_" . $archivo;
if (copy($_FILES['excel']['tmp_name'], $destino)){
echo "<p style='margin:10px; color: Green; display:block;'> - Archivo Cargado Con Éxito</p>";
}
else{
echo "<p style='margin:10px; color: Red; display:block;'> - Error Al Cargar el Archivo</p>";
}
if (file_exists("bak_".$archivo)) {
/** Clases necesarias */
require_once('Classes/PHPExcel.php');
require_once('Classes/PHPExcel/Reader/Excel2007.php');
// Cargando la hoja de cálculo
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load("bak_".$archivo);
$objFecha = new PHPExcel_Shared_Date();
// Asignar hoja de excel activa
$objPHPExcel->setActiveSheetIndex(0);
//Obtener el numero de filas del archivo
$numRows = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
// Llenamos el arreglo con los datos del archivo xlsx
for ($i = 2; $i <= $numRows; $i++) {
$_DATOS_EXCEL[$i]['orden'] = $objPHPExcel->getActiveSheet()->getCell('A' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['tipo_o'] = $objPHPExcel->getActiveSheet()->getCell('B' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['tipo_i'] = $objPHPExcel->getActiveSheet()->getCell('C' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['etapa'] = $objPHPExcel->getActiveSheet()->getCell('D' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['virtual'] = $objPHPExcel->getActiveSheet()->getCell('E' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['cliente'] = $objPHPExcel->getActiveSheet()->getCell('F' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['con1'] = $objPHPExcel->getActiveSheet()->getCell('G' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['con2'] = $objPHPExcel->getActiveSheet()->getCell('H' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['con3'] = $objPHPExcel->getActiveSheet()->getCell('I' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['direccion'] = $objPHPExcel->getActiveSheet()->getCell('J' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['ref'] = $objPHPExcel->getActiveSheet()->getCell('K' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['red_s'] = $objPHPExcel->getActiveSheet()->getCell('L' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['cd'] = $objPHPExcel->getActiveSheet()->getCell('M' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['cb'] = $objPHPExcel->getActiveSheet()->getCell('N' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['pp'] = $objPHPExcel->getActiveSheet()->getCell('O' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['ct'] = $objPHPExcel->getActiveSheet()->getCell('P' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['ps'] = $objPHPExcel->getActiveSheet()->getCell('Q' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['puentista'] = $objPHPExcel->getActiveSheet()->getCell('R' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['distancia'] = $objPHPExcel->getActiveSheet()->getCell('S' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['centro'] = $objPHPExcel->getActiveSheet()->getCell('T' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['central'] = $objPHPExcel->getActiveSheet()->getCell('U' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['tecnico'] = $objPHPExcel->getActiveSheet()->getCell('V' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['comentario'] = $objPHPExcel->getActiveSheet()->getCell('W' . $i)->getCalculatedValue();
$_DATOS_EXCEL[$i]['clase'] = $objPHPExcel->getActiveSheet()->getCell('X' . $i)->getCalculatedValue();
}
}
//si por algo no cargo el archivo bak_
else {
echo "<p style='margin:10px; color: Red; display:block;'> - Necesitas primero importar el archivo</p>";
}
$errores = 0;
//recorremos el arreglo multidimensional
//para ir recuperando los datos obtenidos
//del excel e ir insertandolos en la BD
$n = 0;
$campo = 0;
$No = 0;
echo "
<center> <strong>".$fecha." ".$hora."</strong> <br/> <p>Personal: ".$nombreU."</p></center>
<div class='contenedor-tabla-importar'>
<table id='tabla-vista-importar-orden'>
<tr>
<th>No</th>
<th>orden</th>
<th>tipo<br/>orden</th>
<th>tipo<br/>instalación</th>
<th>etapa</th>
<th>virtual</th>
<th>cliente</th>
<th>tel</th>
<th>tel</th>
<th>tel</th>
<th>dirección</th>
<th>referencia</th>
<th>red<br/>sugerida</th>
<th>cd</th>
<th>cb</th>
<th>pp</th>
<th>c/t</th>
<th>ps</th>
<th>puentista</th>
<th>distancia</th>
<th>centro</th>
<th>central</th>
<th>técnico</th>
<th>comentario</th>
<th>clase orden</th>
</tr>
";
for ($n=2; $n <= $numRows; $n++) {
$No += 1;
if (!$_DATOS_EXCEL[$n]['orden'] == 0 && !$_DATOS_EXCEL[$n]['orden'] == Null)
{
$campo +=1;
echo
<tr>
<td>".$No."</td>
<td>".$_DATOS_EXCEL[$n]['orden']."</td>
<td>".$_DATOS_EXCEL[$n]['tipo_o']."</td>
<td>".$_DATOS_EXCEL[$n]['tipo_i']."</td>
<td>".$_DATOS_EXCEL[$n]['etapa']."</td>
<td>".$_DATOS_EXCEL[$n]['virtual']."</td>
<td>".$_DATOS_EXCEL[$n]['cliente']."</td>
<td>".$_DATOS_EXCEL[$n]['con1']."</td>
<td>".$_DATOS_EXCEL[$n]['con2']."</td>
<td>".$_DATOS_EXCEL[$n]['con3']."</td>
<td>".$_DATOS_EXCEL[$n]['direccion']."</td>
<td>".$_DATOS_EXCEL[$n]['ref']."</td>
<td>".$_DATOS_EXCEL[$n]['red_s']."</td>
<td>".$_DATOS_EXCEL[$n]['cd']."</td>
<td>".$_DATOS_EXCEL[$n]['cb']."</td>
<td>".$_DATOS_EXCEL[$n]['pp']."</td>
<td>".$_DATOS_EXCEL[$n]['ct']."</td>
<td>".$_DATOS_EXCEL[$n]['ps']."</td>
<td>".$_DATOS_EXCEL[$n]['puentista']."</td>
<td>".$_DATOS_EXCEL[$n]['distancia']."</td>
<td>".$_DATOS_EXCEL[$n]['centro']."</td>
<td>".$_DATOS_EXCEL[$n]['central']."</td>
<td>".$_DATOS_EXCEL[$n]['tecnico']."</td>
<td>".$_DATOS_EXCEL[$n]['comentario']."</td>
<td>".$_DATOS_EXCEL[$n]['clase']."</td>
</tr>";
//Insertar las ordenes en la base de datos//
////////////////////////////////////////////
//Comprobar que la ORDEN ingresada no exista en la DB
$conOrden = 0;
$consulta = mysql_query("SELECT orden From logorden ;",$conexion) or die("No se puedo estableser la conexion con la DB ".mysql_error());
while ($fila1 = mysql_fetch_array($consulta)) {
if ($fila1['orden'] == $_DATOS_EXCEL[$n]['orden']) {
//La orden ya existe
$conOrden = 1;
}
else {
}
}
///////////////////////////////////////////////////
if ($conOrden == 0) {
$a = 0;
//$sql = "INSERT INTO datos (nombre,apellido) VALUES ('".$_DATOS_EXCEL[$n]['nombre']."', '".$_DATOS_EXCEL[$n]['direccion']."');";
//Insertar la nueva orden en la tabla LOGORDEN
$sql1 = "INSERT INTO logorden
(orden, tipo_o, tipo_i, etapa, virtual, cliente, con1, con2, con3, direccion, ref, red_s, cd, cb, pp, ct, ps, asigno, puentista, distancia, centro, central, tecnico, comentario, dia, mes, ano, hora, clase)
VALUES
('".$_DATOS_EXCEL[$n]['orden']."','".$_DATOS_EXCEL[$n]['tipo_o']."','".$_DATOS_EXCEL[$n]['tipo_i']."','".$_DATOS_EXCEL[$n]['etapa']."','".$_DATOS_EXCEL[$n]['virtual']."','".$_DATOS_EXCEL[$n]['cliente']."','".$_DATOS_EXCEL[$n]['con1']."','".$_DATOS_EXCEL[$n]['con2']."','".$_DATOS_EXCEL[$n]['con3']."','".$_DATOS_EXCEL[$n]['direccion']."','".$_DATOS_EXCEL[$n]['ref']."','".$_DATOS_EXCEL[$n]['red_s']."','".$_DATOS_EXCEL[$n]['cd']."','".$_DATOS_EXCEL[$n]['cb']."','".$_DATOS_EXCEL[$n]['pp']."','".$_DATOS_EXCEL[$n]['ct']."','".$_DATOS_EXCEL[$n]['ps']."','".$_SESSION['dpi']."','".$_DATOS_EXCEL[$n]['puentista']."','".$_DATOS_EXCEL[$n]['distancia']."','".$_DATOS_EXCEL[$n]['centro']."','".$_DATOS_EXCEL[$n]['central']."','".$_DATOS_EXCEL[$n]['tecnico']."','".$_DATOS_EXCEL[$n]['comentario']."','".date("j")."','".date("n")."','".date("Y")."','".date("G").":".date("i")."','".$_DATOS_EXCEL[$n]['clase']."');";
//Insertar la orden en la tabla ORDEN
$sql2 = "INSERT INTO orden (orden, virtual, tipo_o, dpi, clase, a)
VALUES
('".$_DATOS_EXCEL[$n]['orden']."','".$_DATOS_EXCEL[$n]['virtual']."','".$_DATOS_EXCEL[$n]['tipo_o']."','".$_DATOS_EXCEL[$n]['tecnico']."','".$_DATOS_EXCEL[$n]['clase']."','".$a."');";
$resultado1 = mysql_query($sql1,$conexion);
$resultado2 = mysql_query($sql2,$conexion);
if (!$resultado1 || !$resultado2) {
echo "Error al insertar registro " . $campo;
$errores+=1;
$campo -=1;
}
$con = mysql_query("SELECT * From usuarios WHERE dpi = '".$_DATOS_EXCEL[$n]['tecnico']."';",$conexion) or die("No se puedo estableser la conexion con la DB ".mysql_error());
while ($row = mysql_fetch_array($con)) {
$nomT = $row['nombre']." ".$row['apellido'];
$correo = $row['correo'];
}
//Enviar E-mail
//Preparar el mensaje
$para = $correo;
$asunto = "Nueva Orden: ".$_DATOS_EXCEL[$n]['orden'];
$cabecera = "De: App Delta";
$message = '<div style="width:100%">
<laber style=" color: #c9c9c9;"> '.$fecha.' - '.$hora.' </label>
<h1 align="center">Orden '.$_DATOS_EXCEL[$n]['orden'].'</h1>
<p style="padding: 10px;">'.$nomT.', se te ha asignado una nueva orden, por favor revisar tu cuenta para obtener mayor información de la orden asignada en <strong>josueym.com</strong></p>
</div>';
//Para que el servidor no lo tome como span
$headers = "MINE-Version:1.0;\r\n";
$headers .= "Content-type: text/html; \r\n charset=utf-8 \r\n";
$headers .= "From: App Delta \r\n";
$headers .= "To: ".$para."; \r\n Subject: ".$asunto." \r\n";
if(mail ($para,$asunto, $message, $headers)) {
} else {
header('location: nuevaOrden.php?error=2');
}
}
else {
#Ya existe la ORDEN
#no hacer nada
}
}
/////////
}
echo "</table></div>";
echo "<br/><strong><center>ARCHIVO IMPORTADO CON EXITO, EN TOTAL <strong style='color:Green;'>$campo REGISTROS</strong> Y <strong style='color:Red;'>$errores ERRORES</strong></center></strong>";
//una vez terminado el proceso borramos el archivo que esta en el servidor el bak_
unlink($destino);
}
echo "<center><a href='principal.php' style='color:#FF5722;'>Regresar a Inicio</a></center>";