<?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"];
$BodegaNom2=$_GET["BodegaNom2"];

$arrayError=array();

# QUERYS
$sDoctose = "SELECT *, 
    TRIM(de.de_obs) AS de_obs,
    TRIM(bod.bod_nom) AS bod_nom,
    TRIM(bods.bod_nom) AS bod_noms
    FROM doctose de LEFT JOIN bodegas bod ON de.de_emp=bod.bod_emp AND de.de_bodent=bod.bod_cod LEFT JOIN bodegas bods ON de.de_emp=bod.bod_emp AND de.de_bodsal=bod.bod_cod WHERE de_emp='$empCod' AND de_tipo='$selTdoci' AND de_num='$numdoc'";

$sDoce_det = "SELECT * FROM doce_det WHERE ded_emp='$empCod' AND ded_tipo='$selTdoci' AND ded_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["doctose"] = $db->query($sDoctose);
$select["doce_det"] = $db->query($sDoce_det);

$rec = $db->fetchAssoc($select["doctose"]);
$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["de_fec"])),0,1,"L");
$lineHeight = .42;
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("BODEGA SALIO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/3.92, $lineHeight, utf8_decode($BodegaNom),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("BODEGA ENTRO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/3.92, $lineHeight, utf8_decode($BodegaNom2),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["de_obs"]),0,1,"L");
$pdf->Ln();
//$pdf->Ln();

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

$header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"MEDIDA","F/VENCE","CANTIDAD","UNITARIO","TOTAL");
$widths = array(2.6,7,1.9,1.7,1.7,2,2.5);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C","C","C"));
$pdf->Row($header, $lineHeight);
$pdf->SetAligns(array("L","L","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);
} */
$pdf->SetFont("Arial","",7);
$sumcant=0;
while ($recDet = $db->fetchAssoc($select["doce_det"])) {
    $body = array(
        $recDet["ded_cod"],
        $recDet["ded_desc"],
        $recDet["ded_unimed"],
        strftime("%d/%m/%Y", strtotime($recDet["ded_fvence"])),
        ($recDet["ded_cant"] > 0) ? number_format($recDet["ded_cant"],2) : "",
        ($recDet["ded_cosu"] > 0) ? number_format($recDet["ded_cosu"],5) : "",
        ($recDet["ded_cost"] > 0) ? number_format($recDet["ded_cost"],5) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumcant+=$recDet["ded_cant"];
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.9, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

$body = array(
    "",
    "             TOTAL DEL DOCUMENTO: ",
    "",
    "",
    ($sumcant > 0) ? number_format($sumcant,2) : "",
    "",
    number_format($rec["de_total"],5),
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.9, $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, "Recibido 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("Trasladosc").".pdf","I");

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