<?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/timezone.php');
include_once('../../../php/maxExecTime.php');
//include_once('../ws/COFIDI/wsExec.php');

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);
$empNomc=utf8_decode($_SESSION["empNomc"]);
//$empDir=utf8_decode($_SESSION["empDir"]);
$empNit=$_SESSION["empNit"];
$usuNom = utf8_decode($_SESSION['usuNom']);

$txtCod=$_GET["txtestilo"];

$arrayError=array();

# QUERYS
$sFestilo = "SELECT fes.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape,
    TRIM(fes_prenda) AS fes_prenda, TRIM(bp_nom) AS bp_nom 
    FROM festilos fes LEFT JOIN maestro mae ON fes.fes_emp=mae.mae_emp AND fes.fes_superv=mae.mae_cod
    LEFT JOIN bodegapro bp ON fes.fes_bod=bp.bp_cod
    WHERE fes_emp='$empCod' AND fes_cod='$txtCod'";

$sFestilod = "SELECT fesd.*, TRIM(mae.mae_nom) AS mae_nom, TRIM(mae.mae_ape) AS mae_ape, TRIM(tm.tm_nom) AS tm_nom 
            FROM festilos_d fesd 
            LEFT JOIN maestro mae ON fesd.fesd_emp=mae.mae_emp AND fesd.fesd_codemple=mae.mae_cod
            LEFT JOIN tipomaquina tm ON fesd.fesd_maquinaria=tm.tm_cod
            WHERE fesd_emp='$empCod' AND fesd_cod='$txtCod' ORDER BY fesd_linea";

$lineWidth=19.5;
$lineHeight=0.4;

class generatePDF extends PDF_MC_Table {
    function Header() {
    }
    
    function Footer() { 
        // Posición: a 1,5 cm del final
        $func = new functions();
        $this->SetY(-2);
        $this->SetFont("Arial","I",7);
        $this->Cell(0,1,strftime("%d/%m/%Y %T"),0,0,"L");
    }
}

$db = new dbExec();

$func = new functions();

# GENERAR PDF
$pdf = new generatePDF("P", "cm", "Letter");

# EJECUCION DE QUERYS
$select["festilos"] = $db->query($sFestilo);
$select["festilos_d"] = $db->query($sFestilod);

$rec = $db->fetchAssoc($select["festilos"]);

$pdf->AddPage();

// Paara anular factura
/*$pdf->SetFont('Arial','B',100);
$pdf->SetTextColor(255, 144, 144);
$pdf->SetY(12);
$pdf->Cell($lineWidth, 1, "ANULADO",0,0,"C");*/

$pdf->SetFont("Arial","B",13);
$pdf->SetTextColor(0,0,0);

$lineHeight7 = .6;
/*$img = '../img/logo2.jpg';
$pdf->Image($img,1,1,2.2);
$pdf->SetMargins(1, 1, 1);
$pdf->SetY(1.75);
$pdf->Cell(0, 0, "", 0, 1);*/
//$pdf->Cell($lineWidth/4, $lineHeight7, "");
$pdf->Cell($lineWidth/1, $lineHeight7, $empNom,0,1,"L");
$pdf->Cell($lineWidth/1, $lineHeight7, "LISTA DE PRECIOS ".$rec["bp_nom"],0,1,"L");
$pdf->SetFont("Arial","B",10);
// Derecha
$y = 1.6;
$x = 9;
$lineHeight = .55;
$pdf->SetY($pdf->GetY()+.25);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1.1, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.25);

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/5.3, $lineHeight, utf8_decode("TIPO DE PRENDA: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/3.9, $lineHeight, $rec["fes_prenda"],0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/9.8, $lineHeight, utf8_decode("ESTILO: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/6.9, $lineHeight, $rec["fes_cod"],0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.5, $lineHeight, utf8_decode("LINEA PRODUCCION: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/9, $lineHeight, number_format($rec["fes_lineapro"],0),0,1,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/5.3, $lineHeight, utf8_decode("SUPERVISOR: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.6, $lineHeight, $rec["mae_nom"]." ".$rec["mae_ape"],0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/11, $lineHeight, utf8_decode("FECHA: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, strftime("%d/%m/%Y", strtotime($rec["fes_fec"])),0,1,"L");
//$pdf->Ln();

$lineHeight=0.45;
$pdf->SetY($pdf->GetY()+.25);

$header = array("No.",utf8_decode("OPERACIÓN"),"TIPO DE MAQUINARIA","CAT","UNIDAD","HORA","META","VALOR","DIARIO","QUINCENA");
$widths = array(1,4.4,3,1,1.8,1.5,1.8,1.5,1.5,2.1);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C","C","C","C","C","C"));
$pdf->Row($header, $lineHeight);
$pdf->SetAligns(array("L","C","C","C","C","C","C","R","R","R"));

// Lineas separadoras del detalle
$detX = 0;
/*$detY = $pdf->GetY()+14.7;
$pdf->line($pdf->GetX(), $pdf->GetY(), $pdf->GetX(), $detY);
*foreach ($widths as $x) {
    $detX+=$x;
    $pdf->line($pdf->GetX()+$detX, $pdf->GetY(), $pdf->GetX()+$detX, $detY);
} */
$sumvalor=0;
$sumdiario=0;
$sumquincena=0;
$pdf->SetFont("Arial","",8);
while ($recDet = $db->fetchAssoc($select["festilos_d"])) {
    $body = array(
        $recDet["fesd_linea"],
        $recDet["fesd_desc"],
        $recDet["tm_nom"],
//        $recDet["mae_nom"]." ".$recDet["mae_ape"],
        $recDet["fesd_cat"],
        ($recDet["fesd_uni"] > 0) ? $recDet["fesd_uni"] : "",
        ($recDet["fesd_hora"] > 0) ? number_format($recDet["fesd_hora"],0) : "",
        ($recDet["fesd_meta"] > 0) ? number_format($recDet["fesd_meta"],0) : "",
        ($recDet["fesd_valor"] > 0) ? number_format($recDet["fesd_valor"],3) : "",
        ($recDet["fesd_diario"] > 0) ? number_format($recDet["fesd_diario"],2) : "",
        ($recDet["fesd_quincena"] > 0) ? number_format($recDet["fesd_quincena"],2) : "",
    );
    $pdf->Row($body, $lineHeight);
    $sumvalor+=$recDet["fesd_valor"];
    $sumdiario+=$recDet["fesd_diario"];
    $sumquincena+=$recDet["fesd_quincena"];

}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), 20.6, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);
$body = array(
    "",
    "",
//    "",
    "             TOTAL: ",
    "",
    "",
    "",
    "",            
    number_format($sumvalor,3),
    number_format($sumdiario,2),
    number_format($sumquincena,2),
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), 20.6, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

//$pdf->Cell($lineWidth, .1, "",1,1);
/*$pdf->Cell($lineWidth, $lineHeight, "DATOS FISCALES",1,1,"C");
$pdf->SetFont("Arial","B");
$pdf->Cell($lineWidth, $lineHeight, utf8_decode("Fecha de certificación: ").$sxe->xpath('//dte:FechaHoraCertificacion')[0],1,1);
$pdf->Cell($lineWidth, $lineHeight, utf8_decode("CERTIFICADOR: ").$sxe->xpath('//dte:NombreCertificador')[0],1,1); */

$pdf->Output(utf8_decode("Estilos").".pdf","I");

//$emailAttachment = $pdf->Output('','I');
$db->close();
?>