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

$numdoc=$_GET["numdoc"];
$selTdoci=$_GET["selTdoci"];
$TdociNom=$_GET["TdociNom"];
$BodegaNom=$_GET["BodegaNom"];

$arrayError=array();

# QUERYS
$sInvefis = "SELECT *, 
    TRIM(inf.if_obs) AS if_obs,
    TRIM(bod.bod_nom) AS bod_nom
    FROM invefis inf LEFT JOIN bodegas bod ON inf.if_emp=bod.bod_emp AND inf.if_bod=bod.bod_cod WHERE if_emp='$empCod' AND if_tipo='$selTdoci' AND if_num='$numdoc'";

$sInvef_det = "SELECT * FROM invefdet WHERE infd_emp='$empCod' AND infd_tipo='$selTdoci' AND infd_num='$numdoc'";

$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["invefis"] = $db->query($sInvefis);
$select["invefdet"] = $db->query($sInvef_det);

$rec = $db->fetchAssoc($select["invefis"]);
$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",14);
$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/3, $lineHeight7, $TdociNom,0,0,"L");
$pdf->SetFont("Arial","B",14);
$pdf->MultiAlignCell($lineWidth/2, $lineHeight, ("No.  ".$numdoc),0,1,"R");

$pdf->SetFont("Arial","B",8);
// Derecha
$y = 1.3;
$x = 9;

$pdf->SetY($pdf->GetY()+.25);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.25);

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("FECHA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, date('d-m-Y', strtotime($rec["if_fec"])),0,1,"L");
$lineHeight = .42;
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("BODEGA AFECTA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/3.92, $lineHeight, utf8_decode($BodegaNom),0,1,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("OBSERVACIONES:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.92, $lineHeight, utf8_decode($rec["if_obs"]),0,1,"L");
$pdf->Ln();
//$pdf->Ln();

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

$header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","CANTIDAD");
$widths = array(3.5,10,2,3.5);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C"));
$pdf->Row($header, $lineHeight);

$pdf->SetAligns(array("L","L","C","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);
} */
$sumtotal=0;
$sumcant=0;
$pdf->SetFont("Arial","",9);
while ($recDet = $db->fetchAssoc($select["invefdet"])) {
    $body = array(
        $recDet["infd_cod"],
        $recDet["infd_desc"],
        $recDet["infd_unimed"],
        ($recDet["infd_cant"] > 0) ? number_format($recDet["infd_cant"],2) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumcant+=$recDet["infd_cant"];
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.5, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$body = array(
    "",
    "             TOTAL DEL DOCUMENTO: ",
    "",
    ($sumcant > 0) ? number_format($sumcant,2) : "",
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.5, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$pdf->Ln($lineHeight*4);
$pdf->Cell($lineWidth/6);
$pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Hecho por", "T", 0, "C");
$pdf->Cell($lineWidth/6);
$pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Revisado por", "T", 0, "C");

//$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("Toma Fisica").".pdf","I");

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