<?php
class vrcelec1
{
	function vrcelec1($docu,$idnum,$formato)
	{
		require('../cfg/res.php');
		$sql_tabla='select *from docu0 where docu_codigo="'.$docu.'"';
		$res_tabla=mysql_query($sql_tabla,$link_vcrx);
		
		$sql_empr='select *from empr1';
		$res_empr=mysql_query($sql_empr,$link_vcrx);
		
		
		
		$docu_tabla=mysql_result($res_tabla,0,'docu_tabla');
		$docu_tabla=strtolower($docu_tabla);

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

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


		
		$sql_tran1='select *from '.$docu_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);


		//$sql_tran2='select *from '.$docu_tabla.'12 where idnum1="'.$idnum.'" order by item_codigov, item ';
		$sql_tran2='select *from '.$docu_tabla.'12 where idnum1="'.$idnum.'" order by item_nctlg desc, item_pctlg, item ';
		$res_tran2=mysql_query($sql_tran2,$link_vcrx);
		
		$sql_tran3='select * from '.$docu_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']))
		{
		$xdrtr=mysql_result($res_tran2,0,'drtr');
		if (!empty($xdrtr))
		{
		$sql_drtr='select *from enti00 where enti_codigo="'.$xdrtr.'"';
		$res_drtr=mysql_query($sql_drtr,$link_vcrx);
		$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='-';
				
			}



		}
		
		$sql_enti='select *from enti00 where enti_codigo="'.mysql_result($res_tran1,0,'enti').'"';
		$res_enti=mysql_query($sql_enti,$link_vcrx);
		
		//echo $sql_enti;
		
		$sql_drtr='select *from drtr0 where left(enti_codigo,10) like "'.$_SESSION['user_vcrx_id'].'%"';
		$res_drtr=mysql_query($sql_drtr,$link_vcrx);
		$drtr=0;
	
		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);


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

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

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



<div align="center">
<div align="center" style="width:1150px; font-size:12px;">
<!-- 
<div align="right" style="font-size:18px; border:#000 solid 1px; background: #F7F7F7; margin:2px;"><strong>FACTURA : <?php echo mysql_result($res_tran1,0,'ptonumero');?></strong></div> 
-->
<br></br>
<br></br>
<br></br>
<br></br>
<table width="100%" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="50%">
<!----++++++++++------>
<table border="1" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr>
	<th width="50%" style="background: #F7F7F7;">E M I S O R</th>
	<th></th>
</tr>
<tr>
	<td>RUC</td>
	<td><?php echo mysql_result($res_empr,0,'enti_codigo');?></td>
</tr>
<tr>
	<td>RAZON SOCIAL</td>
	<td><?php echo mysql_result($res_empr,0,'enti_razsoc');?></td>
</tr>

<tr>
	<td>NOMBRE COMERCIAL</td>
	<td><?php echo mysql_result($res_empr,0,'enti_nomcom');?></td>
</tr>
</table>




<!----++++++++++------>
</td>
<td>
<!----++++++++++------>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr>
	<th width="50%"  style="background: #F7F7F7;">DATOS DEL COMPROBANTE</th>
	<th></th>    
</tr>

<tr >
	<td align="right">FACTURA:</td>
	<td><strong><?php echo mysql_result($res_tran1,0,'ptonumero');?></strong></td>
</tr>
<tr>
	<td align="right">FECHA :</td>
	<td><?php echo mysql_result($res_tran1,0,'fechadmy');?></td>
</tr>
<tr>
	<td align="right">VENCE:</td>
    <td><?php echo mysql_result($res_tran1,0,'vencedmy');?></td>
</tr>

	<?php 

		if(mb_stristr($xcelec,$docu_tabla))
		{
	?>

<?php
$clavea=mysql_result($res_tran1,0,'claveacc');
if (!empty($clavea))
//if (1==0)
{
?>
<tr>
	<td align="right">CLAVE DE ACCESO :</td>
	<td><?php echo mysql_result($res_tran1,0,'claveacc');?></td>
</tr>
<?php
}
?>
<?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="50%" style="background: #F7F7F7;">A D Q U I R I E N T E</th>
	<th></th>
</tr>
<tr>
	<td>RUC /CI</td>
	<td><?php echo mysql_result($res_enti,0,'enti_codigo');?></td>
</tr>

<?php
 if(mysql_result($res_enti,0,'enti_nomcom')<>"")
{
?>


<tr>
	<td>RAZON SOCIAL</td>
	<td><?php echo mysql_result($res_enti,0,'enti_razsoc');?></td>
</tr>


<?php }else {?>
<tr>
	<td>CLIENTE</td>
	<td><?php echo mysql_result($res_enti,0,'enti_descri');?></td>
</tr>


<?php }?>

<?php
 if(mysql_result($res_enti,0,'enti_nomcom')<>"")
{
?>

<tr>
	<td>NOMBRE COMERCIAL</td>
	<td><?php echo mysql_result($res_enti,0,'enti_nomcom');?></td>
</tr>

<?php }?>


<tr>
	<td>PROV /CIUDAD</td>
	<td><?php echo mysql_result($res_enti,0,'prvc_descri').' /'.mysql_result($res_enti,0,'cdad_descri');?></td>
</tr>
<tr>
	<td>DIRECCION</td>
	<td>
	<?php 
	echo  trim(mysql_result($res_enti,0,'enti_direccion')).' /'.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');?>
	</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>

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

<tr align="right">
	<td>UNIDADES :</td>
	<td><?php echo abs(mysql_result($res_tran1,0,'tcant'));?></td>
</tr>



<tr align="right">
	<td>SUBTOTAL :</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tsubt')),2);?></td>
</tr>

<?php
if(mysql_result($res_tran1,0,'tdcto')>0)
{
?>

<tr align="right">
	<td>DESCUENTO <?php echo number_format(mysql_result($res_tran1,0,'dcto'),2);?>%:</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tdcto')),2);?></td>
</tr>
<?php
}
?>



<?php
if(mysql_result($res_tran1,0,'tsubtice')>0)
{
?>
<tr align="right">
	<td>SUBTOTAL ICE <?php echo number_format(mysql_result($res_tran1,0,'ice'),0);?>%:</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tsubtice')),2);?></td>
</tr>
<tr align="right">
	<td>ICE :</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tice')),2);?></td>
</tr>

<?php
}
?>

<tr align="right">
	<td>SUBTOTAL IVA 0% :</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tsubtiva0')),2);?></td>
</tr>
<tr align="right">
	<td>SUBTOTAL IVA <?php echo number_format(mysql_result($res_tran1,0,'iva'),0);?>% :</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tsubtiva')),2);?></td>
</tr>
<tr align="right">
	<td>IVA :</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tiva')),2);?></td>
</tr>
<tr align="right">
	<td><strong>VALOR TOTAL :</strong></td>
	<td><strong><?php echo number_format(abs(mysql_result($res_tran1,0,'total')),2);?></strong></td>
</tr>



<?php
//if (mysql_num_rows($res_tran1)!=0)
if (mb_stristr('vtas.clie',$docu_tabla))
	{if (mysql_result($res_tran1,0,'tcomi')!=0 )
		{if(mb_stristr('gnya',$_SESSION['user_vcrx_empr']) && ($drtr==1 || mb_stristr('admi',substr($_SESSION['user_vcrx_tipo'],0,4))))
			{?>
			<tr><td></td></tr>
			<tr align="right">
			<td>COMISION : <?php echo number_format(mysql_result($res_tran1,0,'comi'),0);?>%:</td>
			<td><?php echo number_format(mysql_result($res_tran1,0,'tcomi'),2);?></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="50%" 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
}
?>



<?php
if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
{
?>

<?php
//if($xenvioa==1)
if(1==1)
{
?>
<tr>
	<td>DIRECTOR:</td>
    <td><?php echo $xdrtr;?></td>
</tr>

<?php	
}
?>

<?php	
if($xenvioa!=1 && 1==2)
{
?>

<tr>
	<td>ENVIAR A DIRECTOR:</td>
    <td><?php echo $envio_descri;?></td>
</tr>
<tr>
	<td>CIUDAD ENVIO:</td>
    <td><?php echo $envio_ciudad;?></td>
</tr>
<tr>
	<td>DIRECCION ENVIO:</td>
    <td><?php echo $envio_direcc;?></td>
</tr>
<tr>
	<td>TELEFONO ENVIO:</td>
    <td><?php echo $envio_tlfn;?></td>
</tr>

<?php	
}
?>

<?php	
}
?>





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

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

<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>

<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">
    <th><?php if(mb_stristr('admi',substr($_SESSION['user_vcrx_tipo'],0,4))){echo 'Pag';}?></th>	
    <th>ITEM</th>
	<th>DESCRIPCION</th>
	<th>CANT.</th>
	<th>UNIT.</th>
	<th>SUBT.</th>
<?php
if(mysql_result($res_tran1,0,'tdcto')>0)
{
?>
	<th>DESC.</th>
<?php
}
?>	
<?php
if(mysql_result($res_tran1,0,'tice')>0)
{
?>
	<th>ICE</th>
<?php
}
?>	
	<th>IVA</th>
	<th>TOTAL</th>
</tr>
<?php
$filax=1;
$res_tran2=mysql_query($sql_tran2,$link_vcrx);
while($r_tran2=mysql_fetch_assoc($res_tran2))
{
?>
<tr style=" <?php if($filax==3){echo 'border-bottom:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >

    	<td align="center"><?php if(mb_stristr('admi',substr($_SESSION['user_vcrx_tipo'],0,4))){ echo $r_tran2['item_pctlg']; }?></td>	
	<td><?php echo $r_tran2['item_codigov'];?></td>
	<td><?php echo $r_tran2['item_descri'];?></td>
		<?php
		if ($r_tran2['cant']==0 && substr($r_tran2['docu'],0,2)=='OP')
		{
		?>
			<td align="right"><?php echo '*N/D* '.number_format($r_tran2['cantio'],0);?></td>
		<?php
		}
		else
		{
		?>
			<td align="right"><?php echo number_format($r_tran2['cant'],0);?></td>
		<?php
		}
		?>
	
	
	<td align="right"><?php echo abs(number_format($r_tran2['puni'],2));?></td>
	<td align="right"><?php echo abs(number_format($r_tran2['tsubt'],2));?></td>
<?php
if(mysql_result($res_tran1,0,'tdcto')>0)
{
?>
	<td align="right"><?php echo abs(number_format($r_tran2['tdcto'],2));?></td>
<?php
}
?>	
<?php
if(mysql_result($res_tran1,0,'tice')>0)
{
?>
	<td align="right"><?php echo abs(number_format($r_tran2['tice'],2));?></td>
<?php
}
?>	

	<td align="right"><?php echo abs(number_format($r_tran2['tiva'],2));?></td>
	<td align="right"><?php echo abs(number_format($r_tran2['total'],2));?></td>
</tr>
<?php	
}
?>


<tr align="right" style="font-weight:bolder;border-top:solid 1px#000">
	<td></td>
	<td></td>
	<td>T O T A L E S</td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tcant')),2);?></td>
	<td></td>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tsubt')),2);?></td>
<?php
if(mysql_result($res_tran1,0,'tdcto')>0)
{
?>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tdcto')),2);?></td>
<?php
}
?>
<?php
if(mysql_result($res_tran1,0,'tice')>0)
{
?>
	<td><?php echo number_format(abs(mysql_result($res_tran1,0,'tice')),2);?></td>
<?php
}
?>

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


<?php
if (mb_stristr($xcelec,$docu_tabla))
{
?>
<table align="left" border="1" width="50%">
<tr align="center" font bgcolor=#DDDDDD>
<td><b><font size="2">NOTAS</b></td>
</tr>
<tr>
<td><font size="2">Visite : <a href="http://www.germanya.com.ec">http://www.germanya.com.ec </a></td>
</tr>
</table>
<?php
}
?>

</table>
</div>
</div>




<?php		
		
	}
}
?>

</html>