<?php

// Include the main TCPDF library (search for installation path).
include_once '../../librerias/tcpdf/tcpdf.php';
include_once '../../conexion.php';

$sqlcompras = "select *,(select numero_letras(com_total)) "
        . "as total_letra "
      . " from v_compras where com_cod = " . 
        $_REQUEST ['vcod'] . " order by 1";
$rscompras = consultas::get_datos($sqlcompras);


// create new PDF document
$pdf = new TCPDF('P', 'mm', 'A4');
$pdf->SetMargins(15, 15, 18);
$pdf->SetTitle('FACTURA COMPRA');
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

$pdf->AddPage();

$pdf->Ln(5);
$pdf->SetFont('Times', 'B', 18);
$pdf->Cell(85, 1, 'FARMMEDI', 0, 0, 'C',null,null,1);

$pdf->SetFont('Times', 'B', 12);
$pdf->Cell(85, 1, 'MEDICAMENTOS Y SERVICIOS', 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('Times', '', 12);
$pdf->Cell(85, 1, 'Dirección: Calle Prof. Gauto c/ Gral. Aquino 325', 0, 0, 'C');
$pdf->Ln(5);
$pdf->Cell(98, 1, 'Teléfono: (0971) 981011', 0, 0, 'C');
$pdf->Cell(50, 1, 'FACTURA '.$rscompras[0]['com_condicion'], 0, 0, 'L');

$pdf->SetFont('Times', '', 12);
$pdf->Ln(5);
$pdf->Cell(100, 1, "", 0, 0, 'C');
$pdf->Cell(50, 1, 'Factura N°: '.$rscompras[0]['com_nro_factura'], 0, 0, 'C');


$pdf->SetFont('Times', '', 12);
$pdf->Ln();
$pdf->Cell(98, 1, "", 0, 0, 'C');
$pdf->Cell(50, 1, 'Timbrado N°: '.$rscompras[0]['com_ntimbrado'], 0, 0, 'L');



$style6 = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0));
//$pdf->SetLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 255)));

//cuadros de arriba
$pdf->RoundedRect(15, 12, 90, 42, 6.0, '1111', '', $style6, array(200, 200, 200));
$pdf->RoundedRect(105, 12, 87, 42, 6.0, '1111', '', $style6, array(200, 200, 200));

//cuadro de cabecera
$pdf->RoundedRect(15, 55, 177, 20, 5.0, '1111', '', $style6, array(200, 200, 200));

//datos de cabecera
$pdf->Ln(15);
//Fecha
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(30, 1, '   FECHA: ', 0, 0, 'L');
$pdf->SetFont('Times', '', 10);
$pdf->Cell(/*1*/90, /*2*/1, /*3*/$rscompras[0]['fecdate'], /*4*/0, /*5*/1, /*6*/'L', /*7*/null, /*8*/null, /*9*/1, /*10*/null, /*11*/null, /*12*/null);
$pdf->Ln(3);
//nombre cliente
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(30, 1, '   PROVEEDOR: ', 0, 0, 'L');
$pdf->SetFont('Times', '', 10);
$pdf->Cell(/*1*/90, /*2*/1, /*3*/$rscompras[0]['prv'], /*4*/0, /*5*/0, /*6*/'L', /*7*/null, /*8*/null, /*9*/1, /*10*/null, /*11*/null, /*12*/null);

//ruc cliente
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(20, 1, 'RUC o CI: ', 0, 0, 'L');
$pdf->SetFont('Times', '', 10);
$pdf->Cell(90, 1, $rscompras[0]['prv_ruc'], 0, 1, 'L');
//datos de cabecera
//$pdf->Ln(3);

//cuadro de detalles
$pdf->RoundedRect(15, 75, 178, 155, 5.0, '1111', '', $style6, array(200, 200, 200));

$pdf->Ln(10);
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(15, 1, '#', 0, 0, 'C');
$pdf->Cell(30, 1, 'Descripcion', 0, 0, 'L');
$pdf->Cell(15, 1, 'Cant.', 0, 0, 'C');
$pdf->Cell(25, 1, 'Precio Unit', 0, 0, 'R');
$pdf->Cell(20, 1, 'Subtotal', 0, 0, 'R');
$pdf->Cell(20, 1, 'IVA 5', 0, 0, 'R');
$pdf->Cell(20, 1, 'IVA 10', 0, 0, 'R');
$pdf->Cell(20, 1, 'Exentas', 0, 0, 'R');
$pdf->Ln(5);

$consultas = "select * from v_compras_detalle where com_cod=".$_REQUEST ['vcod'];
$detcompras = consultas::get_datos($consultas);

foreach ($detcompras as $report) {
    $pdf->SetFont('Times', '', 10);
    $pdf->Cell(15, 1, $report['id_articulo'], 0, 0, 'C');
    $pdf->Cell(30, 1, /*3*/$report['art_descri'], 0, 0,'L',null,null,1,null,null,null);
    $pdf->Cell(15, 1,$report['cantidad'] , 0, 0, 'C');
    $pdf->Cell(22, 1, number_format(($report['precio_unit']),0,',','.'), 0, 0, 'R');
    $pdf->Cell(22, 1, number_format(($report['subtotal']),0,',','.'), 0, 0, 'R');
    $pdf->Cell(18, 1, number_format(($report['iva5']),0,',','.'), 0, 0, 'R');
    $pdf->Cell(22, 1, number_format(($report['iva10']),0,',','.'), 0, 0, 'R');
    $pdf->Cell(18, 1, number_format(($report['exentas']),0,',','.'), 0, 1, 'R');
}
$posicion = $pdf->GetY();
$pdf->Line(190,230,15,$posicion);


//cuadro de subtotales
$pdf->RoundedRect(15, 230, 177, 30, 4.0, '1111', '', $style6, array(200, 200, 200));



$pdf->SetFont('Times', '', 10);
$pdf->Text(18, 235, 'TOTAL IVA');
$pdf->SetFont('Times', '', 10);
$pdf->Text(165, 235, 'Gs. '.
        number_format(($rscompras[0]['com_totiva']),0,',','.'));

$pdf->SetFont('Times', 'B', 10);
$pdf->Text(18, 243, 'TOTAL GENERAL');
$pdf->SetFont('Times', '', 10);
$pdf->Text(165, 243, 'Gs. '.
        number_format(($rscompras[0]['com_total']),0,',','.'));

$pdf->SetFont('Times', 'B', 10);
$pdf->Text(18, 251, 'TOTAL EN LETRAS');
$pdf->SetFont('Times', '', 10);
$pdf->Text(55, 251, 'Son Gs. '.
        ucfirst(strtolower ($rscompras[0]['total_letra'])));
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('facturacompras.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

