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

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=TRIM(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"];
$seriedoc=$_GET["selTipoCod"];
$TdocNom=$_GET["TdocNom"];
$totLetras=$_GET["totLetras"];
$idinterno=$_GET["idinterno"];

$arrayError=array();

# QUERYS
$sFactura = "SELECT *, 
    TRIM(fac.fact_nom) AS fact_nom,
    TRIM(fac.fact_dir) AS fact_dir,
    TRIM(fac.fact_tel) AS fact_tel,
    TRIM(fac.fact_nit) AS fact_nit
    FROM facturas fac
    WHERE fact_emp='$empCod' AND fact_transac='$idinterno'";

$sFact_det = "SELECT * FROM fact_det WHERE factd_emp='$empCod' AND factd_transac='$idinterno'";

# EJECUCION DE QUERYS
$db = new dbExec();

$func = new functions();

$select["facturas"] = $db->query($sFactura);
$select["fact_det"] = $db->query($sFact_det);

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

$arrayError=array();

$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");

$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",15);
$pdf->SetTextColor(0,0,0);
$pdf->SetMargins(1, 1, 1);

$lineHeight7 = .6;
/*$img = '../img/logo2.jpg';
$pdf->Image($img,1,1,2.2);
$pdf->SetY(1.75);
$pdf->Cell(0, 0, "", 0, 1);*/
//$pdf->Cell($lineWidth/4, $lineHeight7, "");
$pdf->Cell($lineWidth/1, $lineHeight7, $empNom,0,1,"C");
$pdf->Cell($lineWidth/1, $lineHeight7, "ENVIO DE MERCADERIA",0,1,"C");

$pdf->SetFont("Arial","B",10);
// 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/8, $lineHeight, utf8_decode("FECHA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, date('d-m-Y', strtotime($rec["fact_fec"])),0,1,"L");
$lineHeight = .42;
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode("CLIENTE:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.5, $lineHeight, TRIM($rec["fact_nom"]),0,1,"L");
$pdf->SetFont("","B");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode("DIRECCION:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.5, $lineHeight, TRIM($rec["fact_dir"]),0,1,"L");
$pdf->SetFont("","B");
$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,11,2.3,2.2);
$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);
} */
$pdf->SetFont("Arial","",9);
$sumcant=0;
$items=0;
/*foreach ($jsonDet as $obj) {
    $obj->descripd = utf8_decode($obj->descd);  */
while ($recDet = $db->fetchAssoc($select["fact_det"])) {
    $body = array(
        $recDet["factd_cod"],
        $recDet["factd_desc"],
        $recDet["factd_unimed"],
        ($recDet["factd_cant"] > 0) ? $recDet["factd_cant"] : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumcant+=$recDet["factd_cant"];
    $items+=1;
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.5, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$body = array(
    "",
    "      TOTAL ITEMS:  ".$items,
    "",
    "",
);
$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, "Recibido", "T", 0, "C");
$pdf->Cell($lineWidth/6);
$pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Entregado", "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("Envio").".pdf","I");

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