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

$numNum=$_GET["numNum"];
$selSerieo=$_GET["selSerieo"];
$totlet=$_GET["totlet"];
$arrayError=array();

# QUERYS
$sOrdenc = "SELECT *, 
    TRIM(od.od_codp) AS od_codp,
    TRIM(od.od_prov) AS od_prov,
    TRIM(od.od_dirp) AS od_dirp,
    TRIM(od.od_telp) AS od_telp,
    TRIM(od.od_facnom) AS od_facnom,
    TRIM(od.od_facdir) AS od_facdir,
    TRIM(od.od_dirent) AS od_dirent,
    TRIM(od.od_soli) AS od_soli,
    TRIM(od.od_autorizo) AS od_autorizo,
    TRIM(od.od_obs) AS od_obs
    FROM ordenc od WHERE od_emp='$empCod' AND od_serie='$selSerieo' AND od_num='$numNum'";

$sOrdendc = "SELECT * FROM ordendc WHERE odd_emp='$empCod' AND odd_serie='$selSerieo' AND odd_num='$numNum'";

$lineWidth=19.5;
$lineHeight=0.4;

class generatePDF extends PDF_MC_Table {
    function Header() {
    }
    
    function Footer() { 
    }
}

$db = new dbExec();

$func = new functions();

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

# EJECUCION DE QUERYS
$select["ordenc"] = $db->query($sOrdenc);
$select["ordendc"] = $db->query($sOrdendc);

$rec = $db->fetchAssoc($select["ordenc"]);
$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",22);
$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,"C");
$pdf->Cell($lineWidth/1, $lineHeight7, "ORDEN DE COMPRA",0,1,"C");
$pdf->SetFont("Arial","B",20);
$pdf->MultiAlignCell($lineWidth/1.1, $lineHeight, ("No.  ".$numNum),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["od_fec"])),0,1,"L");
$lineHeight = .42;
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("PROVEDOR:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.6, $lineHeight, utf8_decode($rec["od_prov"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("NIT:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode($rec["od_nit"]),0,1,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("DIRECCION PROVEDOR:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.92, $lineHeight, utf8_decode($rec["od_dirp"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("TELEFONO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode($rec["od_telp"]),0,1,"L");

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("FACTURAR A NOMBRE DE:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.92, $lineHeight, utf8_decode($rec["od_facnom"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("NIT FACT:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode($rec["od_facnit"]),0,1,"L");

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("DIRECCION FACTURAR: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode($rec["od_facdir"]),0,1,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("DIRECCION ENTREGA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode($rec["od_dirent"]),0,1,"L");

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("SOLICITADO POR: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1.93, $lineHeight, utf8_decode($rec["od_soli"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/6.9, $lineHeight, utf8_decode("DIAS CREDITO: :"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode($rec["od_cred"]),0,1,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("TASA DE CAMBIO:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/5, $lineHeight, utf8_decode($rec["od_tcam"]),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode("VALIDA HASTA:"),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/5.2, $lineHeight, strftime("%d/%m/%Y", strtotime($rec["od_valida"])),0,0,"L");
$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("MONEDA:"),0,0,"L");
if ($rec["od_tmone"] == "L") {
   $moneda="LOCAL";
} else {
   $moneda="DOLARES";
}   
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, $moneda,0,1,"L");

$pdf->SetFont("","B");
$pdf->MultiAlignCell($lineWidth/4.9, $lineHeight, utf8_decode("OBSERVACIONES: "),0,0,"L");
$pdf->SetFont("","");
$pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode($rec["od_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","SUBTOTAL","%DEC","VAL/DESC","TOTAL");
$widths = array(1.8,5.5,1.3,1.7,2,2,1.3,1.6,2.3);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C","C","C","C","C"));
$pdf->Row($header, $lineHeight);

$pdf->SetAligns(array("L","L","C","R","R","R","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);
while ($recDet = $db->fetchAssoc($select["ordendc"])) {
    $body = array(
        $recDet["odd_cod"],
        $recDet["odd_desc"],
        $recDet["odd_unimed"],
        ($recDet["odd_cant"] > 0) ? number_format($recDet["odd_cant"],2) : "",
        ($recDet["odd_cosu"] > 0) ? number_format($recDet["odd_cosu"],5) : "",
        ($recDet["odd_sub"] > 0) ? number_format($recDet["odd_sub"],5) : "",
        ($recDet["odd_pdesc"] > 0) ? number_format($recDet["odd_pdesc"],2) : "",
        ($recDet["odd_descu"] > 0) ? number_format($recDet["odd_descu"],2) : "",
        ($recDet["odd_tot"] > 0) ? number_format($recDet["odd_tot"],5) : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
}

$pdf->SetY(22.8);

//$pdf->Cell($lineWidth, $lineHeight, "Sujeto a pagos trimestrales ISR",1,1,"C");
        $pdf->SetFont("","",6);
        $pdf->MultiAlignCell(15.9, $lineHeight, "TOTAL EN LETRAS: ".$totlet,'TLB',0,"L");
        $pdf->SetFont("","",8);
        $pdf->Cell(1.3, $lineHeight, "TOTAL:",'TBR',0,"R");
        $pdf->Cell(2.3, $lineHeight, number_format($rec["od_tot"], 2),1,1,"R");



//$pdf->Cell(17.2, $lineHeight, "TOTAL",1,0,"R");
//$pdf->Cell(2.3, $lineHeight, number_format($rec["od_tot"], 2),1,1,"R");

$pdf->Ln();

$pdf->MultiAlignCell($lineWidth/2, $lineHeight, "ELABORADO POR\n\n________________________\n".$usuNom,1,0,"C");
$pdf->MultiAlignCell($lineWidth/2, $lineHeight, "AUTORIZADO POR\n\n________________________\n".utf8_decode($rec["od_autorizo"]),1,0,"C");

$pdf->Ln(2);

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

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