<?php
// session_start();
// require('../../_lib/vrdocu0.php');
// require('../../_lib/vr_contab.php');
?>

<?php
class vrcelec1
{
	function vrcelec1($docu,$idnum,$formato)
	{
// 		formatos
// 			1 - general
// 			2 - diario contable (directo de la tabla cont1)
// 			3 - diario contable (desde el modulo a cont.idfrom)
		
		require('../cfg/res.php');
		require('../../_lib/vrnumlet2.php');
		require('../../_lib/vr_contab.php');
		//numtoletras($var);

		$xcelec='vtas.comp.guia.retc.retp.cont';
		$xcelec1='vtas.retp.guia';
		$xcelec2='comp.retc';

		
// 		$sql_tabla='update enti0 set enti_descri=concat(enti_ape1," ",enti_ape2," ",enti_nom1," ",enti_nom2) where enti_descri is null and enti_ape1!=""';
// 		$res_tabla=mysql_query($sql_tabla,$link_vcrx);

		if (mb_stristr('cont',$docu)){$formato=2;}	

		if (mb_stristr('vtas.comp.clie.prov.inve.nomi.caja.bcos.cjte.cont',$docu))
		{
			$tabla=$docu;
			$sql_tabla='select * from '.$docu.'1 where idnum1="'.$idnum.'"';
			$res_tabla=mysql_query($sql_tabla,$link_vcrx);
			$docu = mysql_result($res_tabla,0,'docu');
		}	
		
		$sql_tabla='select * from docu0 where docu_codigo="'.$docu.'"';
		$res_tabla=mysql_query($sql_tabla,$link_vcrx);
		$tabla=mysql_result($res_tabla,0,'docu_tabla');
		$doble=mysql_result($res_tabla,0,'docu_doble');
		$tabla=strtolower($tabla);

		mysql_query('update retp2 set cair="-" where puni>=30',$link_vcrx);

		$cdoble=1;
		if ($doble==1) {$cdoble=2;}
		$cdoble=2;
		
		if ($formato==2){$tabla='cont';}

		if ($formato==3):
			$sql_tabla='select * from cont1 where status!="X" and status!="O" and docu="'.$docu.'" and idfrom="'.$idnum.'"';
			$res_tabla=mysql_query($sql_tabla,$link_vcrx);

			if(mysql_num_rows($res_tabla)==0):
				$contab=new vrcontab($docu,$idnum);			
			endif;
// echo $contab;
			$sql_tabla='select * from cont1 where status!="X" and status!="O" and docu="'.$docu.'" and idfrom="'.$idnum.'"';
			$res_tabla=mysql_query($sql_tabla,$link_vcrx);
			$idnum=mysql_result($res_tabla,0,'idnum1');
			$tabla='cont';
		endif;

		$xtitulo='C O M P R O B A N T E  ';
		
		if (mb_stristr($xcelec,$tabla))
		{$xtitulo='C O M P R O B A N T E  -  E L E C T R O N I C O ';}
		
		if (mb_stristr('cont',$tabla))
		{$xtitulo='D I A R I O  -  C O N T A B L E';}
		

		if (mb_stristr('clie',substr($_SESSION['user_vcrx_tipo'],0,4)))
		{
			if (substr($docu,0,1)=='O')
			{$tabla='clie';}
	
			if (substr($docu,0,1)=='F')
			{$tabla='vtas';}

			if (substr($docu,0,1)=='N')
			{$tabla='vtas';}
		}

		if (mb_stristr('prov',substr($_SESSION['user_vcrx_tipo'],0,4)))
		{
			if (substr($docu,0,1)=='O')
			{$tabla='prov';}
	
			if (substr($docu,0,1)=='F')
			{$tabla='comp';}
			
			if (substr($docu,0,1)=='R')
			{$tabla='retp';}
		}

		
		$sql_tran1='select *from '.$tabla.'11 where idnum1="'.$idnum.'"';
		if (mb_stristr('clie.prov',substr($_SESSION['user_vcrx_empr'],0,4)))
		{
			$sql_tran1.='and enti like "'.$_SESSION['user_vcrx_empr'].'%"';
		}
		$res_tran1=mysql_query($sql_tran1,$link_vcrx);
		
		$docu_descri=mysql_result($res_tran1,0,'docu_descri');

		$sql_tran2='select *from '.$tabla.'12 where idnum1="'.$idnum.'" order by item_nctlg desc, item_pctlg, item ';

// 		if (mb_stristr('cont',$tabla))
// 		{$sql_tran2='select *from '.$tabla.'12 where idnum1="'.$idnum.'" order by iddet ';}
		
// 		if (mb_stristr('caja.bcos.cjte',$tabla))
// 		{$sql_tran2='select *from '.$tabla.'12 where idnum1="'.$idnum.'" order by iddet ';}

		if (mb_stristr('retc.retp',$tabla))
		{
			$xdocu=mysql_result($res_tran1,0,'docu');
			$xptoe=mysql_result($res_tran1,0,'ptoe');
			$xnume=mysql_result($res_tran1,0,'numero');
			$xenti=mysql_result($res_tran1,0,'enti');
			$sql_tran1='select *, ';
			$sql_tran1.='sum(rrta) as trrta, ';
			$sql_tran1.='sum(riva) as triva, ';
			$sql_tran1.='sum(abs(total)) as ttotal ';
			$sql_tran1.=' from '.$tabla.'11 where docu like "'.substr($xdocu,0,2).'%" and ptoe="'.$xptoe.'" and numero="'.$xnume.'" and enti="'.$xenti.'" order by idnum1 ';
			$res_tran1=mysql_query($sql_tran1,$link_vcrx);
			$sql_tran2='select * from '.$tabla.'12 where docu like "'.substr($xdocu,0,2).'%" and ptoe="'.$xptoe.'" and numero="'.$xnume.'" and enti="'.$xenti.'" order by docum,numer,cair,idnum1  ';
			$docu_descri='RETENCION';
// 			echo $sql_tran;
			$res_tran2=mysql_query($sql_tran2,$link_vcrx);
		}
		
		$sql_tran3='select * from '.$tabla.'12 where idnum1="'.$idnum.'" and item like "%TRAN%"';
		$res_tran3=mysql_query($sql_tran3,$link_vcrx);
		$xenvioa=1;


		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{
		if (mb_stristr('vtas.clie',$tabla))
		{
                        //echo '<input type="hidden" id="hdd_tran" value="'.$sql_tran2.'">';
			$res_tran2=mysql_query($sql_tran2,$link_vcrx);
			if(mysql_num_rows($res_tran2)!=0)
			{$xdrtr=mysql_result($res_tran2,0,'drtr');}
			else{$xdrtr="";}

			if (!empty($xdrtr))
			{
			$sql_drtr='select *from enti00 where enti_codigo="'.$xdrtr.'"';
                        //echo '<input type="hidden" id="drtr" value="'.$sql_drtr.'" >';
			$res_drtr=mysql_query($sql_drtr,$link_vcrx);
                         if(mysql_num_rows($res_drtr)!=0)
                         {$xdrtr=mysql_result($res_drtr,0,'enti_descri');}
			
			}
		}
		}		

		if(mysql_num_rows($res_tran3)!=0)
		{
			$xtran=mysql_result($res_tran3,0,'puni');
			if($xtran==1)
			{
				$xenvioa=2;
				$entienvio=mysql_result($res_tran1,0,'enti_referidox');
				
			}
			else
			{
				
				$xenvioa=3;
				$entienvio=mysql_result($res_tran1,0,'enti');

			}
			if (empty($xentienvio))
			{
				$entienvio=mysql_result($res_tran1,0,'drtr');
			}
			
			$sql_envioa='select *from enti00 where enti_codigo="'.$entienvio.'"';
			$res_envioa=mysql_query($sql_envioa,$link_vcrx);
			
                       
                        if(mysql_num_rows($res_envioa)!=0)
			{
				$envio_descri=mysql_result($res_envioa,0,'enti_descri');
				$envio_ciudad=mysql_result($res_envioa,0,'prvc_descri').' /'.mysql_result($res_envioa,0,'cdad_descri');
				$envio_direcc=trim(mysql_result($res_envioa,0,'enti_direccion')) .' /'.trim(mysql_result($res_envioa,0,'enti_calle1')) .' /'.trim(mysql_result($res_envioa,0,'enti_callen')) .' /'.trim(mysql_result($res_envioa,0,'enti_calle2')) .' /'.trim(mysql_result($res_envioa,0,'enti_calle3'));
				$envio_tlfn=mysql_result($res_envioa,0,'enti_telefono');
			}
			else
			{
				$envio_descri='-';
				$envio_ciudad='-';
				$envio_direcc='-';
				$envio_tlfn='-';
				
			}


		}

// 		$res_tran1=mysql_query($sql_tran1,$link_vcrx);

		$sql_emis='select * from empr1';
		$res_emis=mysql_query($sql_emis,$link_vcrx);
				
		$sql_enti='select *from enti00 where enti_codigo="'.mysql_result($res_tran1,0,'enti').'"';
		$res_enti=mysql_query($sql_enti,$link_vcrx);
		
		$lblruc1='E M I S O R';
		$lblruc2='A D Q U I R I E N T E';

		if (mb_stristr('comp.retc',$tabla))
		{
		$res_emis=mysql_query($sql_enti,$link_vcrx);
		$res_enti=mysql_query($sql_emis,$link_vcrx);
		}

		if (mb_stristr('retp',$tabla))
		{
		$lblruc2='P R O V E E D O R';
		}

		if (mb_stristr('prod',$tabla))
		{
		$lblruc2='T A L L E R';
		}

		
		//echo $sql_enti;
		
		$drtr=0;
		$sql_drtr='select *from drtr0 where left(enti_codigo,10) like "'.$_SESSION['user_vcrx_id'].'%"';
		$res_drtr=mysql_query($sql_drtr,$link_vcrx);
	
		if (mb_stristr('gnya',substr($_SESSION['user_vcrx_empr'],0,4)))
		{
			if (mysql_num_rows($res_drtr)!=0)
			{
			$drtr=1;
			}
		}


		$sql_drtr='select *from enti0 where enti_codigo="'.mysql_result($res_enti,0,'enti_referidox').'"';
		$res_drtr=mysql_query($sql_drtr,$link_vcrx);


		if (mb_stristr($xcelec1.$xcelec2,$tabla))
		{$html1 = "../celec1/".mysql_result($res_tran1,0,'claveacc').".html"  ;}
		else
		{$html1 = "../celec1/".$tabla.mysql_result($res_tran1,0,'idnum1').".html"  ;}

		ob_start();		

?>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comprobante Electronico</title>

<style type="text/css">
BODY{font-family: courier new;}

@page {
/*    size: 7in 9.25in; */
/* 	 top left bott right */
    size: 217mm 297mm; 
   margin: 0mm 25mm 0mm 10mm; /* 	top right bottom left */
}
	
</style>
</head>

<body>

<?php 
	for($xcont=1;$xcont<=$cdoble;$xcont++)
	{
?>
	
<!-- <div align="center"> -->
<div align="center" style="width:1040px; font-size:18px;">
<!-- <div align="center" style="font-size:18px; border:#000 solid 1px; background: #F7F7F7; margin:2px; ">
	<strong><?php echo $xtitulo;?></strong>
	</div> -->

<div style="height:150px;"></div>
	
<table width="100%" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="100%">
<!----++++++++++------>
	
<!----++++++++++------>
<?php
$xdireccion=trim(mysql_result($res_enti,0,'enti_direccion'));
if(mysql_result($res_enti,0,'enti_calle1')!="")
{$xdireccion=mysql_result($res_enti,0,'enti_calle1').' /'.mysql_result($res_enti,0,'enti_callen').' /'.mysql_result($res_enti,0,'enti_calle2').' /'.mysql_result($res_enti,0,'enti_calle3');}

$xemail=mysql_result($res_enti,0,'enti_email');	 
$xcontacto=mysql_result($res_enti,0,'enti_contacto');	 
?>

<?php
if (mb_stristr('vtas.clie.prod.retp.comp',$tabla))
{
?>
	
<table border="0" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr align="left">
	<td width="30%"></td>
	<td><?php echo 'Guayaquil,'.mysql_result($res_tran1,0,'fechadmy');?></td>
</tr>

<tr>
	<td></td>
		<td><?php echo mysql_result($res_enti,0,'enti_razsoc');?></td>
</tr>
<tr>
	<td></td>
		<td><?php echo mysql_result($res_enti,0,'enti_codigo');?></td>
</tr>
<tr>
	<td></td>
	<td>
	<?php  echo $xdireccion;	?>
	</td>
</tr>
<tr>
	<td></td>
	<td>
	<?php  echo mysql_result($res_tran2,0,'docum_descri');	?>
	</td>
</tr>
<tr>
	<td></td>
	<td>
	<?php  echo mysql_result($res_tran2,0,'ptonumer');	?>
	</td>
	<td align="right">
	<?php  echo mysql_result($res_tran2,0,'fanio');	?>
	</td>
</tr>



<!-- <tr>
	<td>TELEFONO</td>
	<td><?php echo mysql_result($res_enti,0,'enti_telefono');?></td>
</tr>
<tr>
	<td>E-MAIL</td>
	<td><?php echo mysql_result($res_enti,0,'enti_email');?></td>
</tr> -->
<!-- <tr>
	<td>CONTACTO</td>
	<td><?php echo mysql_result($res_enti,0,'enti_contacto');?></td>
</tr> -->
</table>

<!----++++++++++------>

<?php
}
?>


</td>




<td>
<!----++++++++++------>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse;display:none;">
<tr align="center">
	<th width="60%"   style="background: #F7F7F7;">R E S U M E N</th>
	<th></th>
</tr>



<?php
if (mb_stristr('retc.retp',$tabla))
{ ?>
<tr align="right">
	<td><font size="2">RTA.RETENIDO :</td>
	<td><font size="2"><?php echo number_format(abs(mysql_result($res_tran1,0,'trrta')),2);?></td>
</tr>
<tr align="right">
	<td><font size="2">IVA RETENIDO :</td>
	<td><font size="2"><?php echo number_format(abs(mysql_result($res_tran1,0,'triva')),2);?></td>
</tr>
<tr align="right">
	<td><strong><font size="4">TOTAL RETENIDO :</strong></td>
	<td><strong><font size="4"><?php echo number_format(abs(mysql_result($res_tran1,0,'ttotal')),2);?></strong></td>
</tr>
<?php }?>

	



</table>

<!----++++++++++------>
</td>
</tr>
<tr valign="top">
<td>
<!----++++++++++------>
<!-- <table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr align="center">
	<th width="40%" style="background: #F7F7F7;">INFORMACION ADICIONAL</th>
	<th></th>
</tr>
<?php
if(mysql_result($res_tran1,0,'concepto')!="")
{
?>
	<tr>
	<td>CONCEPTO</td>
	<td><?php echo mysql_result($res_tran1,0,'concepto');?></td>
	<tr/>
<?php
}
?>


</table> -->
<!----++++++++++------>
</td>

<td>
<!----++++++++++------>
<!----++++++++++------>
</td>
</tr>
</table>

<br>
<!-- <table border="1" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr align="center">
	<th width="200" style="background: #F7F7F7;">D E T A L L E</th>
	<th></th>
</tr>
</table>
 -->
<div style="height:60px;">
	
</div>
	
<div style="min-height:170px;">
	
<table border="0" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">

	
	
<?php
if (mb_stristr('retc.retp',$tabla))
{
?>
<?php }?>
</tr>

<?php
$filax=1;
$res_tran2=mysql_query($sql_tran2,$link_vcrx);
$duni=2;
while($r_tran2=mysql_fetch_assoc($res_tran2))
{
$duni=$r_tran2['item_duni'];
?>
<tr style=" <?php if($filax==3){echo 'border-bottom:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >


<?php
// 	 AQUI RETENCIONES
if (mb_stristr('retc.retp',$tabla))
{
	$cair=$r_tran2['cair'];
	$cair_descri=$r_tran2['cair_descri'];
	if ($cair=="-"){$cair_descri="I.V.A.";}else{$cair_descri="FTE.RTA. /".$cair_descri;}
?>

	<td align="center" width="80px"><?php echo $cair;?></td>
	<td align="left" width="480px"><?php echo $cair_descri;?></td>
	<td align="right" width="160px"><?php echo number_format($r_tran2['cant'],2);?></td>
	<td align="right" width="160px"><?php echo number_format($r_tran2['puni'],2);?></td>
	<td align="right" width="160px"><?php echo number_format(abs($r_tran2['total']),2);?></td>
	
<?php }?>


</tr>

	
<?php }?>

</table>

	</div>	
	
	
<table border="0" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr align="right" style="font-weight:bolder;">


<?php
if (mb_stristr('cont',$tabla))
{
?>
	<td></td>
	<td>T O T A L E S</td>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tdebito'),2);?></td>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tcredito'),2);?></td>

<?php } ?>

<?php
if (mb_stristr('vtas.clie.comp.prov',$tabla))
{
?>

	<td></td>
	<td></td>
	<td>T O T A L E S</td>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tcant'),$duni);?></td>
	<td></td>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tsubt'),2);?></td>

<?php
if(mysql_result($res_tran1,0,'tdcto')>0)
{
?>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tdcto'),2);?></td>
<?php
}
?>
<?php
if(mysql_result($res_tran1,0,'tice')>0)
{
?>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tice'),2);?></td>
<?php } ?>

	<td><?php echo number_format(mysql_result($res_tran1,0,'tiva'),2);?></td>

<?php
if(mysql_result($res_tran1,0,'tirbp')>0)
{
?>
	<td><?php echo number_format(mysql_result($res_tran1,0,'tirbp'),2);?></td>
<?php } ?>
	<td><?php echo number_format(mysql_result($res_tran1,0,'total'),2);?></td>
<?php } ?>
	<td></td>

	
<?php
if (mb_stristr('retc.retp',$tabla))
{
?>
	<td align="right"></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td align="right"><?php echo number_format(mysql_result($res_tran1,0,'ttotal'),2);?></td>

<?php } ?>

	
	
</tr>

<tr>
</tr>


<tr>
<td><br></td>

</tr>

<table align="right" border="0" width="50%">
<tr>
<td align="right"><b>
<?php 
if (mb_stristr('vtas.comp.clie.prov.inve.nomi.prod.caja.bcos',$tabla))
{echo 'SON: '.numtoletras(abs(mysql_result($res_tran1,0,'total')));}

if (mb_stristr('cont',$tabla))
{echo 'SON: '.numtoletras(abs(mysql_result($res_tran1,0,'total'))/2);}
// 		$res_tran1=mysql_query($sql_tran1,$link_vcrx);
//  echo $sql_tran1;
// echo mysql_result($res_tran1,0,'ttotal');
// if (mb_stristr('retc.retp',$tabla))
// {echo 'SON: '.numtoletras(number_format(abs(mysql_result($res_tran1,0,'ttotal')),2));}
?>
</td>
</tr>
</table>


<!-- <table align="left" border="0" width="50%" style="border-collapse:collapse">
<tr align="center" font bgcolor=#DDDDDD style="border-top: thin solid;border-bottom:thin solid">
<td><b><font size="2">N O T A S</b></td>
</tr>
	<?php
	if (mb_stristr($xcelec1,$tabla))
	{
	?>
	<tr>
	<td><font size="2">ESTA IMPRESION SOLO TIENE FINES INFORMATIVOS Y DE CONSULTA</td>
	</tr>
	<tr>
	<td><font size="2">ESTA IMPRESION NO TIENE VALIDEZ TRIBUTARIA</td>
	</tr>
	<tr>
	<td><font size="2">VALIDADOR : <a href="https://declaraciones.sri.gob.ec/comprobantes-electronicos-internet/publico/validezComprobantes.jsf">https://declaraciones.sri.gob.ec/comprobantes-electronicos-internet/publico/validezComprobantes.jsf</a></td>
	</tr>
	<tr>
	<td><font size="2">Mas informacion en : <a href="http://www.sri.gob.ec/web/10138/145">http://www.sri.gob.ec/web/10138/145 </a></td>
	</tr>
	<?php
	}
	?>
</table>
 -->
</table>
</div>
<!-- </div> -->
<?php if ($xcont==1) {?>
<div style="height:200px;"></div>
<?php } ?>
	
<?php } ?>

	</body>	
	
<?php		
//file_put_contents($html1, ob_get_contents());
	}
}
?>
	
</html>