<?php
session_start();
$ruta = '../../';
include($ruta."cxc/clases/covensol_cxc_c_cuentasxcobrar.php");
$propiedades['habilitar_json'] = 'no';
$objcxc = new covensol_cxc_cuentasxcobrar($propiedades);
$valido = $objcxc->cargar_seguridad("CXC","covensol_cxc_rpp_factura.php");
if($valido===false){exit();}

require_once($ruta.'shared/tcpdf/config/lang/ita.php');
require_once($ruta.'shared/tcpdf/tcpdf.php');  
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
//ini_set('memory_limit','256M');
//ini_set('max_execution_time','0');	

/*
if(!$_GET['gid_fact']){
	echo '<script language="JavaScript"> alert('No hay información para generar la factura'); close(); </script>';
}
$_GET['gid_fact'] = 10;
*/

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false); 
//$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
//$pdf->SetFooterMargin(15);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setPrintHeader(false);
$direccion="Calle Caroní, entre calles París y Londres de la Urbanización Las Mercedes, Edif. Luisiana, Piso 3 CARACAS Estado Miranda
			Rif: G-20010164-4 Zona Postal: 1060  Teléfonos: +582129940202 Fax: +584122593413 ";
$direccion="";
//$pdf->setTextoFooter(utf8_encode($direccion));
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();

function fecha_factura($opciones=array()){		
														
			global $pdf;
			
			$pdf->SetFont('helvetica', '', 12);
			$pdf->SetTextColor(0);
			$pdf->SetFillColor(255,255,255);
			$margen_tabla = 115;
			$fechasep = explode('/',$opciones['fecfact']);
			$fechasep['dia'] = $fechasep[0];
			$fechasep['mes'] = $fechasep[1];
			$fechasep['ano'] = $fechasep[2];
			$fecha = $fechasep['dia'].'   '.$fechasep['mes'].'   '.$fechasep['ano'];
			//CELDA DE MARGEN
			$pdf->Cell($margen_tabla, 3,"", 0,0,'L',1);			
			//Cell( $w, $h, $txt, $border,$ln,$align, $fill,$link,$stretch,$ignore_min_height)
			$pdf->Cell(47, 3,"Caracas", 0,0,'L',1);
			$pdf->Cell(30, 3,$fecha, 0,0,'L',1);
			$pdf->Ln();		
}

function totales($opciones=array()){		
														
			global $pdf,$ancho_detalle,$objcxc,$extrajera,$totalNacional,$ancho_total;
			$tamano = 9;
			if(!$extrajera){
				$total ='<p><table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#CCCCCC">
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Sub Total'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['subtot_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Total Iva'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['iva_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>	
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Neto'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['total_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>							 	 
							</table></p>';				
			}else{
				$total ='<p><table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#CCCCCC">
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Sub Total'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['subtot_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Tax'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['iva_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>	
							   <tr bgcolor="#FFFFFF" >
								<td width="'.$ancho_detalle[4].'" ><p align="right"><strong><font size="'.$tamano.'">Total'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[5].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['total_doc']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>							 	 
							</table></p>';
							
							
					$totalNacional = '<table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#FAFAFA">
										   <tr bgcolor="#FAFAFA" >
												<td width="'.($ancho_total/3).'"><p align="center"><b>Sub Total ('.$objcxc->AbrMonDef.')</b></p></td>
												<td width="'.($ancho_total/3).'"><p align="center"><b>Iva ('.$objcxc->AbrMonDef.')</b></p></td>
												<td width="'.($ancho_total/3).'"><p align="center"><b>Total ('.$objcxc->AbrMonDef.')</b></p></td>
										   </tr>
										   <tr bgcolor="#FFFFFF" >
												<td width="'.($ancho_total/3).'"><p align="right">'.number_format($objcxc->DatosFact['subtot_doc'],2,',','.').'</p></td>
												<td width="'.($ancho_total/3).'"><p align="right">'.number_format($objcxc->DatosFact['iva_doc'],2,',','.').'</p></td>
												<td width="'.($ancho_total/3).'"><p align="right">'.number_format($objcxc->DatosFact['total_doc'],2,',','.').'</p></td>
										   </tr>				  
										</table>';
			}
			
			$pdf->writeHTML(utf8_encode($total), true, false, false, false, '');
			
}


$margenes = $pdf->getMargins();

$fecha = date('m/d/Y');
$ano = date('Y');
$mes = date('m');
$dia = date('d');

//$pdf->Image($ruta.'shared/imagebank/'.$_SESSION["ls_logo"],$margenes['left'],$margenes['top'], 30, 15); 

$prop['id_doc'] = $_GET['id_doc'];
$datosDoc = $objcxc->extraerdatos_documento($prop);
$datos = $datosDoc['fila'];

$objcxc->CargarMonedaLocal=$_GET['forzarlocal'];
$resp=$objcxc->ConfPresentMoneda($objcxc->DatosFact);

//SEGURIDAD DE LA SUCURSAL
$objcxc->codintper = $datos['codsuc'];		
if(!$objcxc->obtSegCxc()){$objcxc->MensajeReporte("No tiene permisos para la Sucursal"); $objcxc->CerrarVentana();}

//SEGURIDAD DE LA CAJA
$objcxc->codintper = $datos['codsuc'].' - '.$datos['codcaj'];		
if(!$objcxc->obtSegCxc()){$objcxc->MensajeReporte("No tiene permisos para la Caja"); $objcxc->CerrarVentana();}


$pdf->SetLineStyle(array('width' => 0.2, 'color' => $pdf->decodifica_color('#DDDDDD')));
$ancho[1] = 210;
$ancho[2] = 330;
$ancho_total = array_sum($ancho);

//$pdf->SetY(30);
//fecha_factura($datos);
$pdf->SetFont('helvetica', '', 10);
$pdf->SetY(45);

$extrajera = ($objcxc->MonedaExtranjera)?1:0;

$datos_factura = '<b>Nº Doc:</b> '.$datos['numdoc'].'<br/>'.
                 '<b>Nº Factura:</b> '.$datos['numfact'].'<br/>'.
				 '<b>Fecha Doc.:</b> '.$datos['fecdoc'].'<br/>'.				
				  $objcxc->TxtTasaCambio;
$espacios = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;			
             &nbsp;&nbsp;';

$datos_cliente = '<b>Cliente:</b> '.$datos['codcliente'].$espacios.'<b>RIF:</b> '.$datos['rif_completo'].'<br/>'.
				 '<b>Razón Social:</b> '.$datos['nombre_cliente'].'<br/>'.
				 '<b>Domicilio Fiscal:</b> <font size="6">'.$datos['dircliente'].'</font><br/>'.
				 '<b>Estado:</b> '.$datos['entidad_federal'];


$encabezado ='<table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#CCCCCC">
               <tr bgcolor="#555555" color="#FFFFFF" >
				<td colspan="2" width="'.$ancho_total.'" ><p align="center"><strong>NOTA DE CRÉDITO</strong></p></td>				
			   </tr>
			   <tr bgcolor="#CCCCCC">
				<td width="'.$ancho[1].'" ><p align="left"><strong>Datos Nota de Crédito</strong></p></td>
				<td width="'.$ancho[2].'"><p align="left"><strong>Datos del Cliente</strong></p></td>
			   </tr>
			   <tr>
				<td width="'.$ancho[1].'"> '.$datos_factura.' </td>
				<td width="'.$ancho[2].'"> '.$datos_cliente.' </td>
			   </tr>			 
			  </table>';


$pdf->writeHTML(utf8_encode($encabezado), true, false, false, false, '');



$ancho_detalle[1] = 248;
$ancho_detalle[2] = 50;
$ancho_detalle[3] = 74;
$ancho_detalle[4] = 74;
$ancho_detalle[5] = 94;
$tot_ancho_detalle = array_sum($ancho_detalle);


$pdf->SetFont('helvetica', '', 8);
$datos['criterio']='por_detalle';
$detalle = $objcxc->consulta_detalles_doc($datos);
$filas = '';

foreach($detalle['rs'] as $datosdet){
		
	$datosdet = $objcxc->formatear_detalle_grilla_doc($datosdet);
	
	$desc = ($datosdet['comentdoc'])?($datosdet['dendetalle'].'<br />'.$datosdet['comentdoc']):$datosdet['dendetalle'];
	
	$filas .= '<tr bgcolor="'.$color_fila.'">
					<td width="'.$ancho_detalle[1].'" align="left"><font size="6">'.$desc.'</font></td>
					<td width="'.$ancho_detalle[2].'" align="right"><font size="7">'.$datosdet['cantidad_detdoc'].'</font></td>
					<td width="'.$ancho_detalle[3].'" align="right"><font size="7">'.$datosdet['precio_detdoc'].'</font></td>
					<td width="'.$ancho_detalle[4].'" align="right"><font size="7">'.$datosdet['iva_detdoc'].'</font></td>
					<td width="'.$ancho_detalle[5].'" align="right"><font size="7">'.$datosdet['neto_detdoc'].'</font></td>
				</tr>';

}



if(!$extrajera){
$detalle ='<table width="'.$tot_ancho_detalle.'" border="1" cellspacing="0" cellpadding="1" bordercolor="#CCCCCC">
			   <tr bgcolor="#CCCCCC" >
				<td width="'.$ancho_detalle[1].'" ><p align="center"><strong><font size="9">Descripción</font></strong></p></td>
				<td width="'.$ancho_detalle[2].'"><p align="center"><strong><font size="9">Cant.</font></strong></p></td>
				<td width="'.$ancho_detalle[3].'"><p align="center"><strong><font size="9">P.U'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[4].'"><p align="center"><strong><font size="9">Iva'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[5].'"><p align="center"><strong><font size="9">Total'.$objcxc->TxtMoneda.'</font></strong></p></td>				
			  </tr>
			'.$filas.'			  		 
			</table>';
/*
$nota = '<p  style="text-align:left;"><font size="10"> <b>Nota:</b> Favor emitir pago a nombre de <b>CORPORACIÓN VENEZOLANA DE NAVEGACIÓN, S.A.</b></font><br />
         En cheque o transferencia a nuestra cuenta corriente del Banco del Tesoro N° 0163-0903-65-9033001985</p>';
*/
}
else{
$detalle ='<table width="'.$tot_ancho_detalle.'" border="1" cellspacing="0" cellpadding="1" bordercolor="#CCCCCC">
			   <tr bgcolor="#CCCCCC" >
				<td width="'.$ancho_detalle[1].'" ><p align="center"><strong><font size="9">Description</font></strong></p></td>
				<td width="'.$ancho_detalle[2].'"><p align="center"><strong><font size="9">Quant.</font></strong></p></td>
				<td width="'.$ancho_detalle[3].'"><p align="center"><strong><font size="9">U.P'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[4].'"><p align="center"><strong><font size="9">Tax'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[5].'"><p align="center"><strong><font size="9">Total'.$objcxc->TxtMoneda.'</font></strong></p></td>				
			  </tr>
			'.$filas.'			  		 
			</table>';
/*			
$columnatxt1 = 'FIELD 57:BENEFICIARIO<br />
				BANCO BANDES URUGUAY, S.A.<br />
				CODIGO SWIFT: CFACUYMN<br />
				';
$columnatxt2 = 'FIELD 59:BENEFICIARIO<br />
				NUMERO DE CUENTA:40013810<br />
				CODIGO SWIFT: CORPORACION VENEZOLANA DE NAVEGACION, S.A.<br />
				';
$columnatxt3 = 'FIELD 56A: BANCO INTERMEDIARIO<br />
				COMMERZBANK AG<br />
				FRANKFURT-GERMANY<br />
				CODIGO SWIFT:COBADEFFXXX
				';
*/
/*
$nota = '<table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#FAFAFA">
				   <tr bgcolor="#CCCCCC" colspan="3" >
						<td><p align="center"><b>FOUNDS TRANSFER INSTRUCTION</b></p></td>						
				   </tr>
				   <tr bgcolor="#FAFAFA" >
						<td width="'.($ancho_total/3).'"><p align="left">'.$columnatxt1.'</p></td>
						<td width="'.($ancho_total/3).'"><p align="left">'.$columnatxt2.'</p></td>
						<td width="'.($ancho_total/3).'"><p align="left">'.$columnatxt3.'</p></td>
				   </tr>				  
				</table>';

*/

}			
$pdf->writeHTML(utf8_encode($detalle), true, false, false, false, '');
$pdf->Ln();
$pdf->Ln();
if($datos['descripfact']){
	$observacionEsp = '<p  style="text-align:left;"><font size="10"> <b>Observación:</b> '.$datos['descripdoc'].'</font></p>';
	$observacionIng = '<p  style="text-align:left;"><font size="10"> <b>Observation:</b> '.$datos['descripdoc'].'</font></p>';
	$observacion = $extrajera?$observacionIng:$observacionEsp;
	$pdf->writeHTML(utf8_encode($observacion), true, false, false, false, '');
	$pdf->Ln();
}
$pdf->SetMargins(146.2, 10, PDF_MARGIN_RIGHT);
$pdf->Ln();
$pdf->SetY(180);
$pdf->SetLineStyle(array('width' => 0.3, 'color' => $pdf->decodifica_color('#AAAAAA')));
$pdf->Line(15, $pdf->GetY(), 205, $pdf->GetY());
$pdf->SetLineStyle(array('width' => 0.2, 'color' => $pdf->decodifica_color('#DDDDDD')));
$pdf->Ln();
totales($datos);
$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
$pdf->Ln();	
if($totalNacional){	
	$pdf->writeHTML(utf8_encode($totalNacional), true, false, false, false, '');
	$pdf->Ln();
}	
if($nota){
	$pdf->writeHTML(utf8_encode($nota), true, false, false, false, '');
	$pdf->Ln();
}

$pdf->Output('factura.pdf', 'I');


?>
