Created
January 27, 2017 07:56
-
-
Save juananruiz/7fca444ad9f550c5eb7676f94a0b20d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//--------------------------------------------------------------------------------------------------- | |
// Proyecto: Servicio Informático Recursos Humanos Universidad de Sevilla | |
// Archivo: app_code/robot_hominis_excel.php | |
//--------------------------------------------------------------------------------------------------- | |
include("../../cascara_core/lib/phpExcel/PHPExcel/Reader/Excel5.php"); | |
global $smarty; | |
global $plantilla; | |
global $usuario; | |
$objReader = new PHPExcel_Reader_Excel5(); | |
$objReader->setReadDataOnly(true); | |
$objPHPExcel = $objReader->load("/mnt/forpas/Volcado_hominis/DAT_EXC.xls"); | |
$objWorksheet = $objPHPExcel->getActiveSheet(); | |
$participante = new participante; | |
$db = $participante->DB(); | |
$query = "DELETE FROM hominis;"; | |
$db->Execute($query); | |
$query = ""; | |
$primera_fila = true; | |
foreach ($objWorksheet->getRowIterator() as $row) | |
{ | |
if ($primera_fila == false) { | |
$query .= ";"; | |
} else { | |
$primera_fila = false; | |
} | |
$query .= "INSERT INTO hominis ([NIF],[APELLIDOS Y NOMBRE],[CCE],[DESC_CCE],[GRUPO], [NIVEL], [TRJ], [DESC_TRJ], [SIT_ADVA], [DESC_SITADVA], [PLAZA], [DES_PLAZA], [NRPT], [SUBUNIDAD], [UNIDAD], [CENTRO DE DESTINO], [NOMBRE], [APELLIDOS]) VALUES ("; | |
$cellIterator = $row->getCellIterator(); | |
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells, even if it is not set. | |
// By default, only cells that are set will be iterated. | |
foreach ($cellIterator as $cell) { | |
$celda = $cell->getValue(); | |
$columna = $cell->getColumn(); | |
// Hay un par de columnas que necesitan un tratamiento previo | |
if ($columna == 'A') { | |
$celda = str_replace('-', '', $celda); | |
} elseif ($columna == 'B') { | |
$apellidos_nombre = explode(', ', $celda); | |
$apellidos = utf8_decode($apellidos_nombre[0]); | |
$nombre = utf8_decode($apellidos_nombre[1]); | |
$query .= ","; | |
} else { | |
$query .= ","; | |
} | |
$celda = utf8_decode($celda); | |
$query .= "'$celda'"; | |
} | |
$query .= ",'$nombre','$apellidos')"; | |
} | |
$query .= ";"; | |
if ($db->Execute($query)) { | |
// Insertamos nuevas personas en la tabla participante desde la tabla hominis | |
$query1 = "INSERT INTO participante ( nif, apellidos, nombre, codigo_cce, descripcion_cce, grupo, nivel, t_r_juridico, situacion_admin, codigo_plaza, puesto_trabajo, codigo_rpt, subunidad, unidad, centro_destino ) SELECT NIF, APELLIDOS, NOMBRE, CCE, DESC_CCE, GRUPO, NIVEL, TRJ, DESC_SITADVA, PLAZA, DES_PLAZA, NRPT, SUBUNIDAD, UNIDAD, [CENTRO DE DESTINO] FROM hominis WHERE (NIF Not In (SELECT nif FROM participante));"; | |
if ($db->Execute($query1)) { | |
$mensaje .= "Personas nuevas agregadas\n"; | |
} | |
// Modificar datos de personas existentes (están en activo pero han cambiado de plaza) | |
$query2 = "UPDATE participante SET participante.apellidos = hominis.apellidos, participante.nombre = hominis.nombre, participante.descripcion_cce = hominis.DESC_CCE, participante.codigo_cce = hominis.CCE, participante.grupo = hominis.grupo, participante.nivel = hominis.nivel, participante.puesto_trabajo = hominis.des_plaza, participante.codigo_rpt = hominis.NRPT, participante.subunidad = hominis.subunidad, participante.unidad = hominis.unidad, participante.centro_destino = hominis.[centro de destino], participante.t_r_juridico = hominis.trj, participante.situacion_admin = hominis.desc_sitadva, participante.codigo_plaza = hominis.plaza FROM participante INNER JOIN hominis ON participante.nif = hominis.nif;"; | |
if ($db->Execute($query2) { | |
$mensaje .= "Personas existentes modificadas\n"; | |
} | |
// Dejamos en blanco los campos relativos a la plaza de personas que cesan actividad | |
$query3 = "UPDATE participante SET participante.descripcion_cce = '', participante.codigo_cce = '', participante.grupo = '', participante.nivel = '', participante.puesto_trabajo = '', participante.codigo_rpt = '', participante.subunidad = '', participante.unidad = '', participante.centro_destino = '', participante.t_r_juridico = '', participante.situacion_admin = '', participante.codigo_plaza = '', participante.telefono_trabajo = '', participante.turno = '', participante.validador2 = '', participante.validador1 = '' WHERE (((participante.nif) Not In (SELECT nif FROM hominis)));"; | |
if ($db->Execute($query3)) { | |
$mensaje .= "Personas cesadas con plazas en blanco\n"; | |
} | |
} else { | |
$mensaje = "No se han podido grabar los datos en la tabla hominis o falló al leer fichero Excel"; | |
} | |
$smarty->assign('mensaje', $mensaje); | |
$plantilla = "robot_hominis.tpl"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment