<?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, $Bannom, $dtDel, $dtAl;

$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"];
$Bannom=$_GET["Bannom"];
$Banctab=$_GET["Banctab"];
$Banctac=$_GET["Banctac"];
$dtFec=$_GET["dtFec"];
$dtDel=$_GET["dtDel"];
$dtAl=$_GET["dtAl"];

$lineWidth=19;
$lineHeight=0.42;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth, $dtFec, $numNum, $txtCodp, $txtNomp, $Banctab, $Bannom;
        $this->SetMargins(1.5, 1, 2);
        $this->Cell(0, 0, "", 0, 1);

        $this->SetFont("Arial","B",12);
        $this->Cell($lineWidth,.7,$empNom,0,0,"L");

        $this->SetFont("Arial","I",8);
        $this->SetY(.7);
        $this->Cell(0,1,utf8_decode("PÁGINA ").$this->PageNo(),0,1,"R");

        $this->SetFont("","",9);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LISTADO PARA PAGO EN EFECTIVO FECHA DEL REPORTE: ".strftime("%d/%m/%Y", strtotime($dtFec))),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, "PLANILLA No. : ".$numNum,0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, "CUENTA BANCARIA: ".$Banctab,0,1,"L");

        $this->Ln();
        $this->SetFont("","",9);
        $header = array("CUENTA","NOMBRE","MONTO");
        $this->SetFont("","B");
        $this->SetWidths(array(5,10,3));
        $this->SetAligns(array("C","C","C"));
        $this->Row($header, $lineHeight);

        $this->SetFont("");
        $this->SetAligns(array("L","L","R"));
    }
    
    function Footer() {
        // Posición: a 1,5 cm del final
        //$this->SetY(-2.5);
        //$this->SetFont("Arial","I",8);
        //$this->Cell(0,1,"PAGINA ".$this->PageNo(),0,0,"C");
    }
}

# 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=5 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=5 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=5 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=5 ORDER BY np_dep, mae.mae_tpue,mae.mae_nom,mae.mae_ape";
    }       
}       

function sNomper_prod($cod){
    global $empCod, $dtDel, $dtAl;
    return "SELECT *, SUM(npp_cant) AS npp_uniprod, SUM(npp_tot) AS npp_valprod FROM nomper_prod WHERE npp_emp='$empCod' AND npp_codemple='$cod' AND npp_fec>='$dtDel' AND npp_fec<='$dtAl' GROUP BY npp_emp,npp_codemple";
}

# EJECUCION DE QUERYS
$db = new dbExec();
$select["nomper"] = $db->query($sNomper);
$totpag=0;
# GENERAR PDF
//$pdf = new generatePDF("L","cm", "Letter");
$pdf = new generatePDF("P","cm", "Letter");
$pdf->AddPage();
$func = new functions();

while ($rec = $db->fetchAssoc($select["nomper"])) {
    $cod=$rec["np_cod"];
    $uniprod=0;
    $valprod=0;
    $select["nomper_prod"] = $db->query(sNomper_prod($cod));
    if ($db->numRows($select["nomper_prod"]) > 0) {
       $fetchAssoc["nomper_prod"] = $db->fetchAssoc($select["nomper_prod"]);
       $uniprod=$fetchAssoc["nomper_prod"]["npp_uniprod"];
       $valprod=$fetchAssoc["nomper_prod"]["npp_valprod"]+$rec["np_bonii"];
    }
    $ingrepla=($rec["np_sord"]+$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"]+$rec["np_boni"]+$rec["np_sferi"]+$rec["np_bono14"]+$rec["np_agui"]+$rec["np_vaca"]);
    if ($ingrepla>=$valprod) {
        $subtotal=$ingrepla;
        $ajuste=$ingrepla-$valprod;
    } else {
        $subtotal=$valprod;
        $ajuste=0;
    }   
    $descu=($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=($subtotal-$descu);
    $otrosing=($rec["np_sexts"]+$rec["np_sextd"]+$rec["np_bonii"]+$rec["np_bonoe"]+$rec["np_depre"]+$rec["np_viati"]+$rec["np_comi"]+$rec["np_otrp"]+$rec["np_bono14"]+$rec["np_agui"]+$rec["np_vaca"]);

    $nomch=$rec["mae_nomch"];
    if ($nomch=="") {
        $nomch=$rec["mae_nom"]." ".$rec["mae_ape"];
    }
    if ($liquido == 0){
        continue;
    }
    $body = array(
        $rec["mae_ctab"],
        trim($nomch),
        number_format($liquido, 2, '.', ','),
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $totpag+=$liquido;
}
$pdf->line($pdf->GetX()+9, $pdf->GetY(), 19.5, $pdf->GetY());
$body = array(
    "",
    "                                                TOTAL",        
    number_format($totpag, 2, '.', ','),        
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->Output(utf8_decode("ListaAcreditacionProd").".pdf","I");
$db->close();
?>