<?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, 10);
$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;
			$tamano = 10;
                        $comentario='<p  style="text-align:left;"><font size="10"> <b>Observación:</b> /font></p>';
			$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']/$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']/$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']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
						  </tr>							 	 
						</table></p>';
			$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_fact'] = $_GET['gid_fact'];
$datosFact = $objcxc->extraerdatos_factura($prop);
$datos = $datosFact['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();}

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_MEDIDAENCABEZADO';
$objcxc->MED_ENCABEZADO = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_MEDIDADETALLE';
$objcxc->MED_DETALLE = $objcxc->select_config($param);


$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($objcxc->MED_ENCABEZADO);
$datos_factura = '<b>Nº Factura:</b> '.$datos['numfact'].'<br/>'.
                 '<b>Fecha Emisión:</b> '.$datos['fecfact'].'<br/>'.
				 '<b>Fecha Vencimiento:</b> '.$datos['fecvenc'].'<br/>'.
				 '<b>Condición Pago:</b> '.$datos['desccondpago'].'<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> '.$datos['dircliente'].'';


$encabezado ='<table width="'.$ancho_total.'" border="0" cellspacing="0" cellpadding="2">
			   <tr>
				<td width="'.$ancho[1].'" ><p align="left"><strong>Datos de Factura</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_fact($datos);
$filas = '';
foreach($detalle['rs'] as $datosdet){
		
	$datosdet = $objcxc->formatear_detalle_grilla($datosdet);
	
	$desc = ($datosdet['comentario'])?($datosdet['dendetalle'].'<br />'.strtoupper($datosdet['comentario'])):$datosdet['dendetalle'].'<br />'.$pdf->Y;
	
	$filas .= '<tr bgcolor="'.$color_fila.'">
					<td width="'.$ancho_detalle[1].'" align="justify"><font size="8">'.$desc.'</font></td>
					<td width="'.$ancho_detalle[2].'" align="right"><font size="8">'.$datosdet['cantidad_detalle'].'</font></td>
					<td width="'.$ancho_detalle[3].'" align="right"><font size="8">'.$datosdet['precio_detalle'].'</font></td>
					<td width="'.$ancho_detalle[4].'" align="right"><font size="8">'.$datosdet['iva_detalle'].'</font></td>
					<td width="'.$ancho_detalle[5].'" align="right"><font size="8">'.$datosdet['neto_detalle'].'</font></td>
				</tr>';

}




$detalle ='<table width="'.$tot_ancho_detalle.'" border="0" cellspacing="0" cellpadding="1">
			   <tr>
				<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>';
$pdf->SetY($objcxc->MED_DETALLE);

//echo $pdf->GetY();
$pdf->writeHTML(utf8_encode($detalle), true, false, false, false, '');
$pdf->Ln();
$pdf->Ln();
//$datos['descripfact']='Y:'.$pdf->GetY();
/*if($datos['descripfact']){
	$observacion = '<p  style="text-align:left;"><font size="10"> <b>Observación:</b> '.$datos['descripfact'].'</font></p>';
	$pdf->writeHTML(utf8_encode($observacion), true, false, false, false, '');
	$pdf->Ln();
}*/
//$datos['comentadifact']='Y:'.$pdf->GetY();

if($datos['comentadifact']){
	$comentario = '<p  style="text-align:left;"><font size="10"> <b>Comentario:</b> '.strtoupper($datos['comentadifact']).'</font></p>';
$pdf->SetY(224);
	$pdf->writeHTML(utf8_encode($comentario), true, false, false, false, '');
        
	$pdf->Ln();
}
$pdf->SetMargins(146.2, 10, PDF_MARGIN_RIGHT);
$pdf->Ln();
$pdf->SetY(220);
$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, 50, PDF_MARGIN_RIGHT);
$pdf->Ln();

$nota = '';
if($nota){
	$pdf->writeHTML(utf8_encode($nota), true, false, false, false, '');
	$pdf->Ln();
}

$pdf->Output('factura.pdf', 'I');


?>
