<?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');

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

$numNum=$_GET['numNum'];
$selBodegapro=$_GET['selBodegapro'];
$selDep=$_GET['selDep'];
$selPuesto=$_GET['selPuesto'];
$numCod=(empty($_GET['numCod'])) ? 0 : $_GET['numCod'];
$dtDel=$_GET["dtDel"];
$dtAl=$_GET["dtAl"];
$dtFec=$_GET["dtFec"];
$crTrepo=$_GET['crTrepo'];

$lineWidth=19;
$lineHeight=0.47;

class generatePDF extends PDF_MC_Table {
    function Header() {
    }
    
    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 ($numCod!=0) {
    $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(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 np.np_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 np_cod='$numCod' ORDER BY np_dep, np.np_tpue,mae.mae_nom,mae.mae_ape";
} else {
    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(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 np.np_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 np.np_bodega='$selBodegapro' ORDER BY np_dep, np.np_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(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 np.np_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 np.np_bodega='$selBodegapro' and np.np_tpue='$selPuesto' ORDER BY np_dep, np.np_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(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 np.np_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 np.np_bodega='$selBodegapro' and np_dep='$selDep' ORDER BY np_dep, np.np_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(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 np.np_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 np.np_bodega='$selBodegapro' and np_dep='$selDep' and mnp.np_tpue='$selPuesto' ORDER BY np_dep, np.np_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);
//$db->close();

# GENERAR PDF
//$pdf = new generatePDF("L","cm", "Letter");
$pdf = new generatePDF("P","cm", "Letter");
$pdf->AddPage();
$func = new functions();
$ti_clasi="XXX";
while ($rec = $db->fetchAssoc($select["nomper"])) {
    if ($crTrepo=='S' && $rec["mae_igsssn"] =='N') {
        continue;
    }
    if ($crTrepo=='N' && $rec["mae_igsssn"] =='S') {
        continue;
    }
    $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"]);

    $pdf->SetFont("ARIAL","B",16);
    $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("RECIBO DE PAGO"),0,1,"C");
    //.strftime("%d/%m/%Y")),0,1,"L");
    $pdf->Ln();
    $pdf->SetFont("","",10);
    $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("RECIBI DE: ").$empNom,0,1,"L");
    $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LA CANTIDAD DE: ------ Q. ").number_format($liquido, 2, '.', ',')." ----",0,1,"L");
    $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("POR: Pago de Salario Correpondiente al periodo del  ").strftime("%d/%m/%Y", strtotime($rec["np_fini"]))."  al  ".strftime("%d/%m/%Y", strtotime($rec["np_ffin"])),0,1,"L");
    //$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
    $pdf->SetFont("","B",10);
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, utf8_decode("INGRESOS:"),1,0,"L");
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, utf8_decode("DESCUENTOS:"),1,0,"L");
    $pdf->SetFont("","",10);
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Días Laborados:  ").number_format($rec["np_dtra"], 1, '.', ','),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Septimos:  ").number_format($rec["np_dsept"], 1, '.', ','),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Total Dias:  ").number_format($rec["np_dtra"]+$rec["np_dsept"], 1, '.', ','),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Total Producido:  ").number_format($uniprod, 2, '.', ','),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Sueldo Base:   ").number_format(round($rec["np_snom"],2), 2, '.', ','),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Sueldo Base:   ").number_format(round($rec["np_snom"]/15,2), 2, '.', ','),0,1,"L");
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Devengado: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($valprod, 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Descuentos: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($descu, 2, '.', ','),0,1,"R");

    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Feriado/Asueto: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($rec["np_sferi"], 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");

    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Bonifi. Decreto 78-89: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($rec["np_boni"], 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");

    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Otros Ingresos: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($otrosing, 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");


    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Ajuste: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($ajuste, 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");


    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Sub Total: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($subtotal, 2, '.', ','),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");

    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");


    $pdf->line($pdf->GetX()+10, $pdf->GetY(), $pdf->GetX()+19, $pdf->GetY());    
    $pdf->SetFont("","",10);
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), $pdf->GetX()+19, $pdf->GetY());    
    $pdf->SetFont("","B",10);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, "",0,1,"R");
    $pdf->SetFont("","B",11);
    //$pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1.9, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("LIQUIDO A RECIBIR: "),0,0,"L");
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, number_format($liquido, 2, '.', ','),1,1,"R");
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), $pdf->GetX()+19, $pdf->GetY());  
    $pdf->line($pdf->GetX()+0, $pdf->GetY()-7, $pdf->GetX()+0, $pdf->GetY());  
    $pdf->line($pdf->GetX()+19, $pdf->GetY()-7, $pdf->GetX()+19, $pdf->GetY());  
    $pdf->SetFont("","",8);
    $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("Fecha de Emisión:  ").strftime("%d/%m/%Y", strtotime($dtFec)),0,1,"L");
    $pdf->Ln(0.9);
    $pdf->SetFont("","",10);
    $pdf->line($pdf->GetX()+6, $pdf->GetY(), 17, $pdf->GetY());    
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, $rec["np_cod"]." - ".$rec["mae_nom"]." ".$rec["mae_ape"],0,1,"C");
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight, " ",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, $rec["tpu_nom"],0,1,"C");
    $pdf->Ln(1.5);

    //La linea de abajo se desbloquea, si se puede configurar media carta en la impresora que usen
//    $pdf->AddPage();
}
$pdf->Output(utf8_decode("Recibos Prod").".pdf","I");
$db->close();
?>