<?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,$prop;
			
			$tamano = $objcxc->FontSizeTotal;
			$TotExcento = 0;
			$TotBaismp = 0;	
				
			$rscargos = $objcxc->TotalesCargosFact($prop);
			if($rscargos===false){exit();}
			$TotCar="";
			foreach($rscargos as $datcar){
				$TotCar .= ' <tr bgcolor="#FFFFFF" >
								<td width="'.($ancho_detalle[5]+20).'" ><p align="right"><strong><font size="'.$tamano.'">Tot Iva ('.number_format($datcar['porcar'],2,',','.').'%)'.$objcxc->TxtMoneda.':</font></strong></p></td>
								<td width="'.$ancho_detalle[6].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($datcar['totimp']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							  </tr>	';
			}
			
			$rsbasimp = $objcxc->TotalesBaseImpFact($prop);
			if($rsbasimp===false){exit();}
			$StrTotBaismp="";
			
			foreach($rsbasimp as $datbasimp){
				$StrTotBaismp .= ' <tr bgcolor="#FFFFFF" >
									<td width="'.($ancho_detalle[5]+20).'" ><p align="right"><strong><font size="'.$tamano.'">Bas Imp ('.number_format($datbasimp['porcar'],2,',','.').'%)'.$objcxc->TxtMoneda.':</font></strong></p></td>
									<td width="'.$ancho_detalle[6].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($datbasimp['totbasimp']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
							    </tr>	';
				$TotBaismp += $datbasimp['totbasimp'];
			}
			
			$TotExcento = $objcxc->DatosFact['subtot']-$TotBaismp;
			
			$total ='<p><table width="'.$ancho_total.'" border="1" cellspacing="0" cellpadding="2" bordercolor="#CCCCCC">
						   <tr bgcolor="#FFFFFF" >
							<td width="'.($ancho_detalle[5]+20).'" ><p align="right"><strong><font size="'.$tamano.'">Sub Total'.$objcxc->TxtMoneda.':</font></strong></p></td>
							<td width="'.$ancho_detalle[6].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($objcxc->DatosFact['subtot']/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
						  </tr>
						  '.$TotCar.$StrTotBaismp.'
						   <tr bgcolor="#FFFFFF" >
							<td width="'.($ancho_detalle[5]+20).'" ><p align="right"><strong><font size="'.$tamano.'">Excento'.$objcxc->TxtMoneda.':</font></strong></p></td>
							<td width="'.$ancho_detalle[6].'"><p align="right"><strong><font size="'.$tamano.'">'.number_format($TotExcento/$objcxc->TasaCambio,2,',','.').'</font></strong></p></td>
						  </tr>	
						   <tr bgcolor="#FFFFFF" >
							<td width="'.($ancho_detalle[5]+20).'" ><p align="right"><strong><font size="'.$tamano.'">Neto'.$objcxc->TxtMoneda.':</font></strong></p></td>
							<td width="'.$ancho_detalle[6].'"><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');



$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);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_MEDIDATOTALES';
$objcxc->MED_TOTALES = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_MOSTRAR_LOGO';
$objcxc->MOSTRAR_LOGO = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_DETALLE';
$objcxc->FontSize = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_TOTALES';
$objcxc->FontSizeTotal = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_DATOS';
$objcxc->FontSizeDatosGen = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_DESCDET';
$objcxc->FontSizeDescDet = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FORMTATO_DESC';
$objcxc->FormatoDescDet = $objcxc->select_config($param);


$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_DESCRIPCION';
$objcxc->FontSizeDescrip = $objcxc->select_config($param);

$param['codsis'] = 'CXC';
$param['seccion'] = 'CONFIG';
$param['entry'] = 'REPORTE_FACTURA_FONTSIZE_COMENTARIO';
$objcxc->FontSizeComentario = $objcxc->select_config($param);


if($objcxc->MOSTRAR_LOGO){$pdf->Image($ruta.'shared/imagebank/'.$_SESSION["ls_logo"],$margenes['left'],$margenes['top'], 30, 15);} 

$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', '', $objcxc->FontSizeDatosGen);
$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="1" cellspacing="0" cellpadding="2" bordercolor="#CCCCCC">
			   <tr bgcolor="#CCCCCC" >
				<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] = 258;
$ancho_detalle[2] = 30;
$ancho_detalle[3] = 64;
$ancho_detalle[4] = 30;
$ancho_detalle[5] = 64;
$ancho_detalle[6] = 94;
$tot_ancho_detalle = array_sum($ancho_detalle);


$pdf->SetFont('helvetica', '', $objcxc->FontSize);
$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 />'.$datosdet['comentario']):$datosdet['dendetalle'].'<br />'.$pdf->Y;
	
	switch($objcxc->FormatoDescDet){
		case 'PD':
			
			break;
			
		case 'U':
			$desc = strtoupper($desc);
			break;
			
		case 'L':
			$desc = strtolower($desc);
			break;
			
		case 'FU':
			$desc = ucfirst(strtolower($desc));
			break;
	}
	
	
	$filas .= '<tr bgcolor="'.$color_fila.'">
					<td width="'.$ancho_detalle[1].'" align="justify"><font size="'.$objcxc->FontSizeDescDet.'">'.$desc.'</font></td>
					<td width="'.$ancho_detalle[2].'" align="right"><font size="'.$objcxc->FontSize.'">'.$datosdet['cantidad_detalle'].'</font></td>
					<td width="'.$ancho_detalle[3].'" align="right"><font size="'.$objcxc->FontSize.'">'.$datosdet['precio_detalle'].'</font></td>
					<td width="'.$ancho_detalle[4].'" align="right"><font size="'.$objcxc->FontSize.'">'.$datosdet['porciva'].'</font></td>
					<td width="'.$ancho_detalle[5].'" align="right"><font size="'.$objcxc->FontSize.'">'.$datosdet['iva_detalle'].'</font></td>
					<td width="'.$ancho_detalle[6].'" align="right"><font size="'.$objcxc->FontSize.'">'.$datosdet['neto_detalle'].'</font></td>
				</tr>';

}




$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="'.$objcxc->FontSize.'">Descripción</font></strong></p></td>
				<td width="'.$ancho_detalle[2].'"><p align="center"><strong><font size="'.$objcxc->FontSize.'">Cant.</font></strong></p></td>
				<td width="'.$ancho_detalle[3].'"><p align="center"><strong><font size="'.$objcxc->FontSize.'">P.U'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[4].'"><p align="center"><strong><font size="'.$objcxc->FontSize.'">(%)</font></strong></p></td>
				<td width="'.$ancho_detalle[5].'"><p align="center"><strong><font size="'.$objcxc->FontSize.'">Iva'.$objcxc->TxtMoneda.'</font></strong></p></td>
				<td width="'.$ancho_detalle[6].'"><p align="center"><strong><font size="'.$objcxc->FontSize.'">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="'.$objcxc->FontSizeDescrip.'"> <b>Observación:</b> '.$datos['descripfact'].'</font></p>';
	$pdf->writeHTML(utf8_encode($observacion), true, false, false, false, '');
	$pdf->Ln();
}
$pdf->SetMargins(142.2, 10, PDF_MARGIN_RIGHT);
$pdf->Ln();
$pdf->SetY($objcxc->MED_TOTALES);
$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();

$nota = '';
if($nota){
	$pdf->writeHTML(utf8_encode($nota), true, false, false, false, '');
	$pdf->Ln();
}

$pdf->Output('factura.pdf', 'I');


?>
