<?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
$sDoctoss = "SELECT *, 
    TRIM(ds.ds_obs) AS ds_obs,
    TRIM(bod.bod_nom) AS bod_nom
    FROM doctoss ds LEFT JOIN bodegas bod ON ds.ds_emp=bod.bod_emp AND ds.ds_bodsal=bod.bod_cod WHERE ds_emp='$empCod' AND ds_tipo='$selTdoci' AND ds_num='$numdoc'";

$sDocs_det = "SELECT * FROM docs_det WHERE dsd_emp='$empCod' AND dsd_tipo='$selTdoci' AND dsd_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["doctoss"] = $db->query($sDoctoss);
$select["docs_det"] = $db->query($sDocs_det);

$rec = $db->fetchAssoc($select["doctoss"]);
$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["ds_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,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["ds_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","UNITARIO","TOTAL");
$widths = array(2.9,8,2,1.7,2,2,1.6);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C","C"));
$pdf->Row($header, $lineHeight);

$pdf->SetAligns(array("L","L","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);
} */
$sumtotal=0;
$sumcant=0;
$pdf->SetFont("Arial","",7);
while ($recDet = $db->fetchAssoc($select["docs_det"])) {
    $body = array(
        $recDet["dsd_cod"],
        $recDet["dsd_desc"],
        $recDet["dsd_unimed"],
        ($recDet["dsd_cant"] > 0) ? number_format($recDet["dsd_cant"],2) : "",
        ($recDet["dsd_unit"] > 0) ? number_format($recDet["dsd_unit"],5) : "",
        ($recDet["dsd_monto"] > 0) ? number_format($recDet["dsd_monto"],5) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sumtotal+=$recDet["dsd_monto"];
    $sumcant+=$recDet["dsd_cant"];
}
$pdf->SetY($pdf->GetY()+.1);
$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+.2, $pdf->GetY());
$pdf->SetY($pdf->GetY()+.1);

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

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