<?php
include_once("../../../php/sessionStart.php"); 
include_once("../../../php/db/dbExec.php");
include_once('../../../php/functions.php');
include_once("../../../php/fpdf/functions.php");
include_once('../../../php/maxExecTime.php');

global $empCod, $usuNom, $bitMod, $bitForm, $selBan, $dtFec, $numNum, $Banctab, $Banctac, $txtCodp, $txtNomp;

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

$numNum=$_GET["numNum"];
$selBodegapro=$_GET['selBodegapro'];
$selDep=$_GET['selDep'];
$selPuesto=$_GET['selPuesto'];
$selBan=$_GET["selBan"];
$Banctab=$_GET["Banctab"];
$Banctac=$_GET["Banctac"];
$dtFec=$_GET["dtFec"];

# QUERYS
if ($selDep=="000") {
    if ($selPuesto=="000") {
        $sNomper = "SELECT np.*, gru.*, dep.*, mae.mae_gru, mae.mae_dep, mae.mae_tpue,  TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, mae_igsssn, TRIM(mae.mae_nomch) AS mae_nomch, mae_fpago, mae_ctab,
                    TRIM(gru_nom) AS gru_nom, TRIM(dep_nom) AS dep_nom, mae.mae_fpago, TRIM(tpu.tpu_nom) AS tpu_nom 
                    FROM nomper np
                    LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
                    LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
                    LEFT JOIN grupo gru ON np.np_emp=gru.gru_emp AND np.np_gru = gru.gru_cod 
                    LEFT JOIN dep dep ON np.np_emp=dep.dep_emp AND np.np_dep = dep.dep_cod 
                    WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' AND mae.mae_fpago<3 ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
    } else {
        $sNomper = "SELECT np.*, gru.*, dep.*, mae.mae_gru, mae.mae_dep, mae.mae_tpue,  TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, mae_igsssn, TRIM(mae.mae_nomch) AS mae_nomch, mae_fpago, mae_ctab, 
                    TRIM(gru_nom) AS gru_nom, TRIM(dep_nom) AS dep_nom, mae.mae_fpago, TRIM(tpu.tpu_nom) AS tpu_nom 
                    FROM nomper np
                    LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
                    LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
                    LEFT JOIN grupo gru ON np.np_emp=gru.gru_emp AND np.np_gru = gru.gru_cod 
                    LEFT JOIN dep dep ON np.np_emp=dep.dep_emp AND np.np_dep = dep.dep_cod 
                    WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and mae.mae_tpue='$selPuesto' AND mae.mae_fpago<3 ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
    }       
} else {
    if ($selPuesto=="000") {
        $sNomper = "SELECT np.*, gru.*, dep.*, mae.mae_gru, mae.mae_dep, mae.mae_tpue,  TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, mae_igsssn, TRIM(mae.mae_nomch) AS mae_nomch, mae_fpago, mae_ctab, 
                    TRIM(gru_nom) AS gru_nom, TRIM(dep_nom) AS dep_nom, mae.mae_fpago, TRIM(tpu.tpu_nom) AS tpu_nom 
                    FROM nomper np
                    LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
                    LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
                    LEFT JOIN grupo gru ON np.np_emp=gru.gru_emp AND np.np_gru = gru.gru_cod 
                    LEFT JOIN dep dep ON np.np_emp=dep.dep_emp AND np.np_dep = dep.dep_cod 
                    WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and np_dep='$selDep' AND mae.mae_fpago<3 ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
    } else {
        $sNomper = "SELECT np.*, gru.*, dep.*, mae.mae_gru, mae.mae_dep, mae.mae_tpue,  TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, mae_igsssn, TRIM(mae.mae_nomch) AS mae_nomch, mae_fpago, mae_ctab, 
                    TRIM(gru_nom) AS gru_nom, TRIM(dep_nom) AS dep_nom, mae.mae_fpago, TRIM(tpu.tpu_nom) AS tpu_nom 
                    FROM nomper np
                    LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
                    LEFT JOIN tpuesto tpu ON mae.mae_tpue = tpu.tpu_cod 
                    LEFT JOIN grupo gru ON np.np_emp=gru.gru_emp AND np.np_gru = gru.gru_cod 
                    LEFT JOIN dep dep ON np.np_emp=dep.dep_emp AND np.np_dep = dep.dep_cod 
                    WHERE np_emp='$empCod' AND np_numpla='$numNum' and mae.mae_bodega='$selBodegapro' and np_dep='$selDep' and mae.mae_tpue='$selPuesto' AND mae.mae_fpago<3 ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
    }       
}       

# EJECUCION DE QUERYS
$db = new dbExec();
$carpeta = '/Acreditaciones';
if (!file_exists($carpeta)) {
    mkdir($carpeta, 0777, true);
}


$tc=",";

$filenom = fopen("/Acreditaciones/Acredita.txt", "w+");

# CREA EL CICLO PARA EL LISTADO
$item=1;

$select["nomper"] = $db->query($sNomper);
$totpag=0;

while ($rec = $db->fetchAssoc($select["nomper"])) {
    $otrosing=($rec["np_bonii"]+$rec["np_bonoe"]+$rec["np_depre"]+$rec["np_viati"]+$rec["np_comi"]+$rec["np_otrp"]);
    $devengado=($rec["np_sord"]+$rec["np_boni"]+$rec["np_sferi"]+$rec["np_sexts"]+$rec["np_sextd"]+$rec["np_ssept"]+$rec["np_bonii"]+$rec["np_bonoe"]+$rec["np_depre"]+$rec["np_viati"]+$rec["np_comi"]+$rec["np_otrp"]);
    $otrosdesc=($rec["np_isr"]+$rec["np_anti"]+$rec["np_unif"]+$rec["np_lent"]+$rec["np_preb"]+$rec["np_pree"]+$rec["np_desj"]+$rec["np_otrd"]);
    $totdesc=($rec["np_igss"]+$rec["np_isr"]+$rec["np_anti"]+$rec["np_unif"]+$rec["np_lent"]+$rec["np_preb"]+$rec["np_pree"]+$rec["np_desj"]+$rec["np_otrd"]);
    $liquido=($devengado-$totdesc);

    $cod=$rec["np_cod"];
    $nomch=$rec["mae_nomch"];
    if ($nomch=="") {
        $nomch=$rec["mae_nom"]." ".$rec["mae_ape"];
    }
    if ($liquido == 0){
        continue;
    }

    $t1= $rec["mae_ctab"];
    $t2= trim($nomch);
    $t3= $liquido;

    $t_fila = (string)($t1).$tc.(string)$t2.$tc.$t3;

    fwrite($filenom, $t_fila . PHP_EOL);

    $item += 1;
}
$db->close();

fclose($filenom);

$texto = file_get_contents("/Acreditaciones/Acredita.txt"); 
$texto = nl2br(utf8_encode($texto)); 
echo $texto;
exit;
?>