160 lines
4.6 KiB
PHP
160 lines
4.6 KiB
PHP
|
|
<?php
|
||
|
|
require_once __DIR__.'/auth_guard.php';
|
||
|
|
ini_set('display_errors', 1);
|
||
|
|
ini_set('display_startup_errors', 1);
|
||
|
|
error_reporting(E_ALL);
|
||
|
|
require_once "../config.php";
|
||
|
|
require __DIR__ . '/../vendor/autoload.php';
|
||
|
|
$return = new stdClass();
|
||
|
|
//print_r($_FILES); die;
|
||
|
|
$files = $_FILES['customFile'];
|
||
|
|
$file_path = $files['tmp_name'][0]; // temporary upload path of the first file
|
||
|
|
$fileName = 'tmpusers.xlsx';
|
||
|
|
$dir_subida = '../data/tmpusers.xlsx';
|
||
|
|
move_uploaded_file($file_path, $dir_subida);
|
||
|
|
/** Identify the type of $inputFileName **/
|
||
|
|
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($dir_subida);
|
||
|
|
/** Create a new Reader of the type that has been identified **/
|
||
|
|
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
|
||
|
|
/** Load $inputFileName to a Spreadsheet Object **/
|
||
|
|
$spreadsheet = $reader->load($dir_subida);
|
||
|
|
/** Convert Spreadsheet Object to an Array for ease of use **/
|
||
|
|
$schdeules = $spreadsheet->getActiveSheet()->toArray();
|
||
|
|
unlink($dir_subida);
|
||
|
|
$return = new stdClass();
|
||
|
|
//paso 1 verifcar headers
|
||
|
|
$headers = $schdeules[0];
|
||
|
|
global $link;
|
||
|
|
dbConnect();
|
||
|
|
if(!in_array ( "numempl", $headers )){
|
||
|
|
$return->error = "1";
|
||
|
|
$return->msg = "No existe la columna número de empleado (numempl)";
|
||
|
|
}else{
|
||
|
|
|
||
|
|
$values = array_splice ($schdeules, 1, sizeof($schdeules)-1);
|
||
|
|
$return->procesados = sizeof($values);
|
||
|
|
$return->correctos = 0;
|
||
|
|
$return->results = [];
|
||
|
|
foreach ($values as $value) {
|
||
|
|
if( $value[0] == "" ){
|
||
|
|
array_push ($return->results, '<span class="text-danger font-weight-bold">Usuario sin numempl: '.implode(', ', $value) .'<span>');
|
||
|
|
}else if(userExist($value[0])){
|
||
|
|
|
||
|
|
for ($i=1; $i < sizeof($headers) ; $i++) {
|
||
|
|
//echo $headers[$i]." - ";
|
||
|
|
$categoria = buscarCategoria($headers[$i]);
|
||
|
|
$userid = buscarID($value[0]);
|
||
|
|
//echo $userid." - ";
|
||
|
|
//echo $categoria." - ";
|
||
|
|
|
||
|
|
if ($value[$i] == 0) {
|
||
|
|
//echo "ELIMINAR -";
|
||
|
|
|
||
|
|
if (buscarUser($categoria,$userid)) {
|
||
|
|
|
||
|
|
$sql = "DELETE FROM asignacion WHERE catid = '$categoria' AND uid = '$userid'";
|
||
|
|
|
||
|
|
if(mysqli_query($link, $sql)){
|
||
|
|
$return->correctos++;
|
||
|
|
array_push ($return->results, 'El usuario: <b>' . $value[0] . '</b> ya no esta asignado a la categoría <b>'.$headers[$i].'</b>.');
|
||
|
|
}else{
|
||
|
|
array_push ($return->results, 'El usuario: ' . $value[0] . ' error: '. mysqli_error($link));
|
||
|
|
}
|
||
|
|
|
||
|
|
}else{
|
||
|
|
//echo "No se encontró asignado el usuario ".$value[0]."<br>";
|
||
|
|
array_push($return->results,'No se encontró asignado el usuario <b>'.$value[0].'</b> a la categoría <b>'.$headers[$i].'</b>.');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}else{
|
||
|
|
//echo "ASIGNAR - ";
|
||
|
|
|
||
|
|
if (!buscarUser($categoria,$userid)) {
|
||
|
|
|
||
|
|
$sql = "INSERT INTO asignacion(uid, catid) VALUES ($userid,$categoria)";
|
||
|
|
|
||
|
|
if(mysqli_query($link, $sql)){
|
||
|
|
$return->correctos++;
|
||
|
|
array_push ($return->results, 'El usuario: <b>' . $value[0] . '</b> se asignó correctamentea la categoría <b>'.$headers[$i].'</b>.');
|
||
|
|
}else{
|
||
|
|
array_push ($return->results, 'El usuario: ' . $value[0] . ' error: '. mysqli_error($link));
|
||
|
|
}
|
||
|
|
|
||
|
|
}else{
|
||
|
|
//echo "Ya se encuentra asignado el usuario ".$value[0]." <br>";
|
||
|
|
array_push($return->results,'Ya se encuentra asignado el usuario <b>'.$value[0].'</b> a la categoría <b>'.$headers[$i].'</b>.');
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}else{
|
||
|
|
array_push ($return->results, 'El usuario: ' . $value[0] . ' no se encuentra registrado aún.');
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function userExist($numempl){
|
||
|
|
global $link;
|
||
|
|
dbConnect();
|
||
|
|
$sql ="select * from user where numempl = '$numempl'";
|
||
|
|
$result = mysqli_query($link, $sql);
|
||
|
|
if ( mysqli_num_rows($result) <= 0 ) {
|
||
|
|
return false;
|
||
|
|
}else{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function buscarID($numempl){
|
||
|
|
global $link;
|
||
|
|
dbConnect();
|
||
|
|
$sql ="select userid from user where numempl = '$numempl'";
|
||
|
|
$result = mysqli_query($link, $sql);
|
||
|
|
if ( mysqli_num_rows($result) <= 0 ) {
|
||
|
|
//return false;
|
||
|
|
}else{
|
||
|
|
while ($row = mysqli_fetch_assoc ($result)){
|
||
|
|
$userid = $row['userid'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $userid;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function BuscarCategoria($categorianombre){
|
||
|
|
global $link;
|
||
|
|
dbConnect();
|
||
|
|
|
||
|
|
$sql ="SELECT id,clave,nombre FROM categoria WHERE nombre LIKE '$categorianombre'";
|
||
|
|
$result = mysqli_query($link, $sql);
|
||
|
|
if ( mysqli_num_rows($result) <= 0 ) {
|
||
|
|
|
||
|
|
}else{
|
||
|
|
while ($row = mysqli_fetch_assoc ($result)){
|
||
|
|
$idcat = $row['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $idcat;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function buscarUser($categoria,$user){
|
||
|
|
global $link;
|
||
|
|
dbConnect();
|
||
|
|
$sql ="SELECT * FROM `asignacion` WHERE catid = '$categoria' AND uid = '$user'";
|
||
|
|
$result = mysqli_query($link, $sql);
|
||
|
|
if ( mysqli_num_rows($result) <= 0 ) {
|
||
|
|
return false;
|
||
|
|
}else{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
echo json_encode($return, JSON_UNESCAPED_UNICODE);
|
||
|
|
?>
|