<?php require_once('../Connections/cn_conexion.php'); ?>
<?php include_once("../includes/funciones.php");?>
<?php include_once("../includes/numerosToletras.php");?>
<?php  header("Content-Type: text/html; charset=iso-8859-1 ");?>
<?php  mysql_query("SET NAMES 'utf8'"); ?>
<?php
$total=0;
 $V=new EnLetrasS();
 $con_letra=strtoupper($V->ValorEnLetras($total,"con"));
?>
<?php
$basex=-20;
$basey=-30;
$despy_d=0;
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_documento = "SELECT * FROM documento, clientes WHERE documento.id_documento='".$_SESSION["iddocprint"]."' AND clientes.id_cliente=documento.id_cliente";
$rs_documento = mysql_query($query_rs_documento, $cn_conexion) or die(mysql_error());
$row_rs_documento = mysql_fetch_assoc($rs_documento);
$totalRows_rs_documento = mysql_num_rows($rs_documento);

$fech=explode("-",$row_rs_documento['fecha_emision']);
$mes=$fech[1];
$dia=$fech[2];
$year=$fech[0];
$nombre_mes=nombre_mes($mes);

if($row_rs_documento['tipo_documento']=='f'){
	$despy_d=0;
}else{
	$despy_d=7;
	}


?>
<?php
require('../fpdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage('P','Letter');
$pdf->SetFont('Arial','B',11);
$pdf->SetXY($basex+50,$basey+50+$despy_d);
$pdf->Cell(40,6,utf8_decode($row_rs_documento['nom_cliente']));
$pdf->SetXY($basex+50,$basey+63+$despy_d);
$pdf->multiCell(100,6,utf8_decode($row_rs_documento['dir_cliente']));

if($row_rs_documento['tipo_documento']=='f'){
	$pdf->SetXY($basex+50,$basey+76);
	$pdf->Cell(40,6,$row_rs_documento['ruc_cliente']);
	$pdf->SetXY($basex+180,$basey+76);
	$pdf->Cell(40,6,($dia." de ".$nombre_mes." de ".$year));
}else{
	$pdf->SetXY($basex+160,$basey+63+$despy_d);
	$pdf->Cell(40,6,($dia." de ".$nombre_mes." de ".$year));
	}

$pdf->SetXY($basex+30,$basey+90);
$pdf->multiCell(0, 6, utf8_decode($row_rs_documento['concepto']), 0, "L", 0); 
if($row_rs_documento['detracc']=='1'){
	$pdf->Ln(5);
	$pdf->SetX($basex+30);
$pdf->SetFont('Helvetica','B',12);
	$pdf->multiCell(0, 6, utf8_decode($row_rs_documento['detraccion']), 0, "L", 0); 
	}
	
if($row_rs_documento['moneda']=='S'){
	$V=new EnLetrasS();	
	$moneda="S/";
	}else{
		$V=new EnLetrasD();	
		$moneda="US $";
		}	

$pdf->SetFont('Helvetica','B',14);

if($row_rs_documento['tipo_documento']=='f'){
	$igv=round($row_rs_documento['igv']*$row_rs_documento['monto']/100,2);	
	$pdf->SetXY($basex+180,$basey+170);
	$pdf->multiCell(30, 6, $moneda."   ", 0, "L", 0); 
	$pdf->SetXY($basex+190,$basey+170);
	$pdf->multiCell(30, 6, number_format(decimals($row_rs_documento['monto']),2), 0, "R", 0); 
	$pdf->SetXY($basex+180,$basey+177);
	$pdf->multiCell(30, 6, $moneda."   ", 0, "L", 0); 
	$pdf->SetXY($basex+190,$basey+177);
	$pdf->multiCell(30, 6, number_format(decimals($igv),2), 0, "R", 0); 
	}else{
	$igv=0;
	$igv_b=0;
	$igv_b=round(($row_rs_documento['igv']*$row_rs_documento['monto'])/(100),2);
	$igv=$igv_b;
	}

$pdf->SetXY($basex+180,$basey+190-$despy_d);
$pdf->multiCell(30, 6, $moneda."   ", 0, "L", 0); 
$pdf->SetXY($basex+190,$basey+190-$despy_d);
$pdf->multiCell(30, 6, number_format(decimals($row_rs_documento['monto']+$igv),2), 0, "R", 0);

$pdf->SetFont('Arial','B',11);
 
$total=($row_rs_documento['monto']+$igv);

$con_letra=strtoupper($V->ValorEnLetras($total,"con"));

$pdf->SetXY($basex+30,$basey+195-$despy_d);
$pdf->multiCell(0, 6, "SON: ".$con_letra, 0, "L", 0); 




$pdf->Output();
?>
<?php
mysql_free_result($rs_documento);
?>
