<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
require('../cfg/res.php');
require('../cfg/cfg.php');

$fini1= date('2000-01-01');
$sql_fini1='select * from empr0';
$res_fini1=mysql_query($sql_fini1,$link_vcrx);
$fini1=mysql_result($res_fini1,0,'empr_fcont');
//echo '-'.$fini1.'-';

$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ( '-120 month' , strtotime ( $fecha1 ) ) ;
$xfecha1 = date ( 'Y/m/j' , $nuevafecha );
$xfecha1= date('Y/m/'.'1');
$xfecha2= date('Y/m/d');


if($_SESSION['user_vcrx_empr']==''){die('SU SESION EXPIRO');}

// 	$fini1= date('2000-01-01');
	if (@mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
	{$fini1=date('2010-11-01');}


	if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']))
	{
		$fini=$_GET['fecha_ini'];
	}
	else
	{
		$fini=date('2000-01-01');
		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{$fini=date('2010-11-01');}
	}
	
	if(isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
	{
		$ffin=$_GET['fecha_fin'];
	}
	else
	{
		$ffin=date('Y-m-d');
	}


if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']) && isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
{
	$xfecha1=$_GET['fecha_ini'];
	$xfecha2=$_GET['fecha_fin'];
}


if(isset($_GET['fcero']) && ($_GET['fcero']=='false'))
{$xfcero=$_GET['fcero'];}
else
{$xfcero='true';}

if(isset($_GET['fcord']) && ($_GET['fcord']=='false'))
{$xfcord=$_GET['fcord'];}
else
{$xfcord='true';}

if(isset($_GET['fcom0']) && ($_GET['fcom0']=='true'))
{$xfcom0=$_GET['fcom0'];}
else
{$xfcom0='false';}

if(isset($_GET['fcom1']) && ($_GET['fcom1']=='true'))
{$xfcom1=$_GET['fcom1'];}
else
{$xfcom1='false';}

if(isset($_GET['fcom2']) && ($_GET['fcom2']=='true'))
{$xfcom2=$_GET['fcom2'];}
else
{$xfcom2='false';}

if(isset($_GET['fdtr1']) && ($_GET['fdtr1']=='false'))
{$xfdtr1=$_GET['fdtr1'];}
else
{$xfdtr1='true';}

if(isset($_GET['f2col']) && ($_GET['f2col']=='true'))
{$xf2col=$_GET['f2col'];}
else
{$xf2col='false';}



// $sql_where='cant!=0 and fecha >= "'.$fini1.'" AND fecha <= "'.$ffin.'"';
$sql_where='cant!=0 and fecha >= "'.$fini1.'" AND fecha <= "'.$ffin.'" and ctas!=""';

$xfrepor='S';
if(isset($_GET['frepor']) && !empty($_GET['frepor']))
{
	$xfrepor=$_GET['frepor'];
}


if($xfrepor=='B')
{
	$sql_where.=' and(ctan1<="3" or ctan1>="8") ';
	if($xfcord=='false')
		{$sql_where.=' and(ctan1<="3") ';}
	if($xfdtr1=='false')
		{
      $fecha2 = date('Y-12-31');
      $fecha2 = strtotime($ffin) ;
      $fecha2 = date('Y-12-31',$fecha2);
      $sql_ldtr='select * from cont1 where docu like "DTR%" and fecha="'.$fecha2.'" and status!="X" and status!="O" order by idnum1 desc';
      $res_ldtr=mysql_query($sql_ldtr,$link_vcrx);
      if(mysql_num_rows($res_ldtr)!=0)
      {
        $idldtr=@mysql_result($res_ldtr,0,'idnum1');
        $sql_where.=' and(idnum1!="'.$idldtr.'") ';
      }
    }
}

if($xfrepor=='R')
{
	$sql_where.=' and fecha >= "'.$xfecha1.'" ';
	$sql_where.=' and(ctan1>="4" and ctan1<="8") ';
	if($xfdtr1=='false')
		{$sql_where.=' and(docu not like "DTR%") ';}
	
}




$xfnivel='5';
if(isset($_GET['fnivel']) && !empty($_GET['fnivel']))
{
	$xfnivel=$_GET['fnivel'];
}

$ctas_descri="";
if(isset($_GET['ctas']) && !empty($_GET['ctas']))
{
// 	$sql_where.=' and  (ctas like "'.$_GET['ctas'].'%" or ctas_descri like "%'.$_GET['ctas'].'%" ) ';
	$sql_where.=' and  (ctas like "'.$_GET['ctas'].'%") ';
	$xfctas=$_GET['ctas'];
	$sql_ctas='select * from ctas0 where ctas_codigo="'.$xfctas.'"';
	$res_ctas=mysql_query($sql_ctas,$link_vcrx);
	$ctas_descri=mysql_result($res_ctas,0,'ctas_descri');

}


if(isset($_GET['fenti']) && !empty($_GET['fenti']))
{
	$sql_where.=' and  (enti like "'.$_GET['fenti'].'%" or enti_descri like "%'.$_GET['enti'].'%" ) ';
	$xfenti=$_GET['fenti'];
}

if(isset($_GET['fitem']) && !empty($_GET['fitem']))
{
	$sql_where.=' and  (item like "'.$_GET['fitem'].'%" or item_descri like "%'.$_GET['item'].'%" ) ';
	$xfenti=$_GET['fitem'];
}


if(isset($_GET['flsub']) && !empty($_GET['flsub']))
{
	$sql_where.=' and  (item_lsub like "'.$_GET['flsub'].'%" or lsub_descri like "%'.$_GET['flsub'].'%" ) ';
	$xflsub=$_GET['flsub'];
}


if(isset($_GET['fzsub']) && !empty($_GET['fzsub']))
{
	$sql_where.=' and  (item like "%'.$_GET['fzsub'].'%" ) ';
	$xfzsub=$_GET['fzsub'];
}




if(isset($_GET['fctlg']) && !empty($_GET['fctlg']))
{
//	$sql_icont.=' and  (item_nctlg ='.$_GET['fctlg'].' )';
//	$xfctlg=$_GET['fctlg'];
}




$sql_icont='';

$sql_icont.='           select cont12.*, 1 as nivel, ctan1 as ctas_codi, descri1 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan1 ';

if ($xfnivel>=2)
{$sql_icont.=' union '.'select cont12.*, 2 as nivel, ctan2 as ctas_codi, descri2 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan2 '; }

if ($xfnivel>=3)
{$sql_icont.=' union '.'select cont12.*, 3 as nivel, ctan3 as ctas_codi, descri3 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan3 ';}

if ($xfnivel>=4)
{$sql_icont.=' union '.'select cont12.*, 4 as nivel, ctan4 as ctas_codi, descri4 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan4 ';}

if ($xfnivel>=5)
{$sql_icont.=' union '.'select cont12.*, 5 as nivel, ctan5 as ctas_codi, descri5 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan5 ';}


if(mb_strstr('S',$xfrepor))
{
$sql_icont='select cont12.*,
			'.$xfnivel.' as nivel,
			ctan'.$xfnivel.' as ctas_codi,
			descri'.$xfnivel.' as ctas_desc,
			sum(case when fecha<"'.$xfecha1.'" then tcant end) as saldoi, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tdebito end) as tdeb, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tcredito end) as tcre, 
			sum(tcant) as saldo
			from cont12
			where '.$sql_where;

	
// $res_icontt=mysql_query($sql_icont,$link_vcrx);
$sql_icontt=$sql_icont;	
$sql_icont.=' group by ctan'.$xfnivel;
}

if(mb_strstr('T',$xfrepor))
{
$sql_icont='select cont12.*,
			'.$xfnivel.' as nivel,
			ctan'.$xfnivel.' as ctas_codi,
			descri'.$xfnivel.' as ctas_desc,
			sum(case when fecha<"'.$xfecha1.'" then tcant end) as saldoi, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tdebito end) as tdeb, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tcredito end) as tcre, 
			sum(tcant) as saldo
			from cont12
			where '.$sql_where;

// $res_icontt=mysql_query($sql_icont,$link_vcrx);
$sql_icontt=$sql_icont;	
$sql_icont.=' group by ctan'.$xfnivel;
	
$xfniv=$xfnivel-1;	
//echo $xfniv;
while ($xfniv>0)
{
//echo $sql_icont;
	$sql_icont.=' union '.
			'select cont12.*,
			'.$xfniv.' as nivel,
			ctan'.$xfniv.' as ctas_codi,
			descri'.$xfniv.' as ctas_desc,
			sum(case when fecha<"'.$xfecha1.'" then tcant end) as saldoi, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tdebito end) as tdeb, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tcredito end) as tcre, 
			sum(tcant) as saldo
			from cont12
			where '.$sql_where;
	$sql_icont.=' group by ctan'.$xfniv;
	$xfniv--;
}	
}





$xmeses=0;
$xwidth = 90;
if ($xfcom1=='true' || $xfcom2=='true')
{
		$fini=date('Y-m-01',strtotime($fini));
		$ffin=date('Y-m-t', strtotime($ffin));
		$xf2col='false';
// 		$sql_meses='select distinct fanio,fmes,fmesn from cont12 where fecha<="'.$ffin.'" order by fanio,fmes';
		$sql_meses='select distinct fanio,fmes,fmesn from cont12 where fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fanio,fmes';
		$res_meses=mysql_query($sql_meses,$link_vcrx);
		$xmeses = mysql_num_rows($res_meses);
		$xmes1 = $xmeses -11;
		if($xmes1<=0){$xmes1=1;}
		$xcmes=0;
		$sql_pivot='';
		while ($r_mes0=mysql_fetch_assoc($res_meses))	
		{
			$xcmes++;
			$xfanio=$r_mes0['fanio'];
			$xfmes=$r_mes0['fmes'];

			if ($xcmes==$xmes1){$finic=date($xfanio.'-'.$xfmes.'-01');}
			if ($xcmes>=$xmes1)
			{
// 			$colmes = $xfanio.$xfmes;	
			$colmes = $xcmes-$xmes1+1;	
			if ($colmes<10){$colmes='0'.$colmes;}
// 			$sql_pivot.='sum(case when fanio='.$xfanio.' and fmes='.$xfmes.' then tcant end) as s'.$colmes.',';	
			$sql_pivot.='sum(case when concat(fanio,fmes)<='.$xfanio.$xfmes.' then tcant end) as s'.$colmes.',';	
			$sql_pivot.='sum(case when fanio='.$xfanio.' and fmes='.$xfmes.' then tdebito end) as d'.$colmes.',';	
			$sql_pivot.='sum(case when fanio='.$xfanio.' and fmes='.$xfmes.' then tcredito end) as c'.$colmes.',';	
			}
		}

	$sql_icont=str_replace($fini,$finic,$sql_icont);
	$sql_icont=str_replace('cont12.*,','cont12.*,'.$sql_pivot,$sql_icont);

	$sql_icontt=str_replace($fini,$finic,$sql_icontt);
	$sql_icontt=str_replace('cont12.*,','cont12.*,'.$sql_pivot,$sql_icontt);
// 	echo $sql_icont;
	if($xmeses>6){$xwidth = 100;}
	
}


if(mb_strstr('BR',$xfrepor))
{
//ql_icontt=' select cont12.*,1 as nivel, ctan1 as ctas_codi, descri1 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan1 ';
$sql_icontt=' select cont12.*,1 as nivel, ctan1 as ctas_codi, descri1 as ctas_desc, sum(tcant) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan1 order by ctas';
$res_icontt=mysql_query($sql_icontt,$link_vcrx);
}

$sql_icont.=' order by ctas_codi,nivel';

// echo $sql_icont;
$res_icont=mysql_query($sql_icont,$link_vcrx);


?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Informes Contabilidad</title>
<style type="text/css">
body{
  font-family:"Courier New", Courier, monospace;
  }
/*    size: 7in 9.25in; */
/* 	 top left bott right */
<?php 
// 	if ($xmeses>6){echo 'size: 297mm 215mm;';}else{echo 'size: 215mm 297mm;';}
	?>
<?php	if ($xmeses<=6): ?>
@page {
    size: 21cm 29.7cm;
    margin: 15mm 0mm 0mm 0mm;
}	
<?php else: ?>
@page {
    size: 29.7cm 21cm;
    margin: 15mm 10mm 10mm 10mm; 
}	
<?php endif; ?>
   

</style>

</head>
<body>

<div align="center">
<?php if ($xmeses<=6): ?>
<div align="center" style="width:1150px; font-size:12px;">
<?php else: ?>
<div align="center" style="width:1600px; font-size:12px;">
<?php endif; ?>
<div align="center" style="font-size:18px;font-weight: bold; border:#000 solid 1px; background: #F7F7F7; margin:2px;">INFORMES CONTABLES</div>

<table width="90%" 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="30%" style="background: #F7F7F7;">PARAMETROS</th>
	<th></th>
</tr>

<tr>
    	<td style="font-weight:bolder;">FECHA</td>
	<?php if($xfrepor=='B'){; ?>
  	<td><?php echo '  '.date_format(date_create($ffin),'d/M/Y');?></td>
  <?php }else{ ?>
	  <td><?php echo date_format(date_create($fini),'d/M/Y').' - '.date_format(date_create($ffin),'d/M/Y');?></td>
  <?php } ?>
</tr>

<tr>
    	<td style="font-weight:bolder;">REPORTE</td>
	<td>
	<?php

	if($xfrepor=='S'){echo 'SUMAS Y SALDOS';};
	if($xfrepor=='T'){echo 'SUMAS Y SALDOS (X Nivel)';};
	if($xfrepor=='B'){echo 'BALANCE GENERAL';};
	if($xfrepor=='R'){echo 'RESULTADOS';};
	if($xfcom1=='true'){echo '(COMPARATIVO ACUMULADO)';};
	if($xfcom2=='true'){echo '(COMPARATIVO X PERIODO)';};
  
  if(mb_stristr($ffin,'12-31'))
  {if($xfdtr1=='true'){echo ' - CON DTR';}else{echo ' - SIN DTR';}}
	?>
	</td>
</tr>

<tr>
    	<td style="font-weight:bolder;">CUENTAS</td>
	<td><?php  if($xfctas==""){echo 'TODAS';}else{echo $xfctas.' - '.$ctas_descri;}?></td>
</tr>


	
<tr>
    	<td style="font-weight:bolder;">NIVEL</td>
	<td><?php echo $xfnivel;?><?php if($xfcero=='false' && $xfnivel>=5){echo ' ( NO CEROS )';};?></td>
</tr>




</table>




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

<?php
$sql_empr='select * from empr0';
$res_empr=mysql_query($sql_empr,$link_vcrx);
?>
<tr>
	<td align="right">EMPRESA:</td>
	<td><?php echo mysql_result($res_empr,0,'empr_descri');?></td>
</tr>
<tr>
	<td align="right">USUARIO:</td>
	<td><?php echo $_SESSION['user_vcrx_id'].' / '.$_SESSION['user_vcrx_descri'];?></td>
</tr>
<tr>
	<td align="right">FECHA / HORA :</td>
	<td><?php echo date('d/M/Y H:m:s');?></td>
</tr>


<!-- 	<?php
if(mb_strstr('BR',$xfrepor))
{
$xtotalg=0;
		while($r_icontt=mysql_fetch_assoc($res_icontt)) 
		{
				$xtotalg+=$r_icontt['saldo'];
				$xtsaldo = 1;
				if(mb_stristr('2.3.4',substr($r_icontt['ctas_codi'],0,1))){$xtsaldo=-1;}			
				?>
			<tr>
				<td align="right"><?php echo $r_icontt['descri1'];?></td>
				<td align="right"><?php echo number_format($r_icontt['saldo']* $xtsaldo,2);?></td>
			</tr>	
			<?php
		}
	?>
	<?php
if(mb_strstr('B',$xfrepor))
{
			?>
		<tr>
			<td align="right"><?php echo 'ACTIVO-(PASIVO+PATRIM)';?></td>
			<td align="right"><?php echo  number_format($xtotalg,2);?></td>
		</tr>	
			<?php
}	
	?>

	<?php
if(mb_strstr('R',$xfrepor))
{
			?>
		<tr>
			<td align="right"><?php echo 'RESULTADO';?></td>
			<td align="right"><?php echo  number_format($xtotalg*-1,2);?></td>
		</tr>	
			<?php
}	
	?>
	
	<?php
}	
	?>
 -->
	
	</table>
<!----++++++++++------>
</td>
</tr>
	
	
	
	</table>
<!----++++++++++------>
<?php if($xfcom1!='true' && $xfcom2!='true') :  ?>
<?php
if(mb_strstr('BR',$xfrepor))
{	
?>	
<hr/>

<table border="1" width="90%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr style="background: #F7F7F7;">
	<?php
		$res_icontt=mysql_query($sql_icontt,$link_vcrx);
		$xwidth1 = 100/(mysql_num_rows($res_icontt)+2);
		while($r_icontt=mysql_fetch_assoc($res_icontt)) 
		{	?>
				<td align="center" width="<?php echo $xwidth1?>%"><b><?php echo $r_icontt['descri1'];?></td>
		<?php	}	?>

	<?php
if(mb_strstr('B',$xfrepor))
{
			?>
			<td align="center"><b><?php echo 'ACTIVO-(PASIVO+PATRIM)';?></td>
			<?php
}	
	?>

	<?php
if(mb_strstr('R',$xfrepor))
{
			?>
			<td align="center"><b><?php echo 'RESULTADO';?></td>
			<?php
}	
	?>
	
	
</tr>
<tr>
	<?php 
		$res_icontt=mysql_query($sql_icontt,$link_vcrx);
		while($r_icontt=mysql_fetch_assoc($res_icontt)) 
		{
				$xtotalg+=$r_icontt['saldo'];
				$xtsaldo = 1;
				if(mb_stristr('2.3.4',substr($r_icontt['ctas_codi'],0,1))){$xtsaldo=-1;}			
				?>
				<td align="center"><?php echo number_format($r_icontt['saldo']* $xtsaldo,2);?></td>
			<?php
		}
	?>
	<?php
if(mb_strstr('B',$xfrepor))
{
			?>
			<td align="center"><?php echo  number_format($xtotalg/2,2);?></td>
			<?php
}	
	?>

	<?php
if(mb_strstr('R',$xfrepor))
{
			?>
			<td align="center"><?php echo  number_format($xtotalg/2*-1,2);?></td>
			<?php
}	
	?>

</tr>	
</table>

<?php
}
	?>	
	
<?php endif;  ?>
	
<hr/>
	
	
	
<table border="0" width="<?php echo $xwidth?>%" 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($xfcom1=='true' || $xfcom2=='true') {  ?>
				<th>CODIGO</th>
				<th>CUENTA</th>
				
		<?php if(mb_stristr('ST',$xfrepor)): ?>
				<th>INICIAL</th>
		<?php endif; ?>
	<?php	
// 		$sql_meses='select distinct fanio,fmes,fmesn from cont12 where fecha<="'.$xfecha2.'" order by fanio,fmes';
		$res_meses=mysql_query($sql_meses,$link_vcrx);
		$xmeses = mysql_num_rows($res_meses);
		$xmes1 = $xmeses -11;
		$xcmes=0;
		if($xmes1<=0){$xmes1=1;}
		while ($r_mes0=mysql_fetch_assoc($res_meses))	
		{
			$xcmes++;
			if ($xcmes>=$xmes1){echo '<th>'.$r_mes0['fmesn'].'/'.substr($r_mes0['fanio'],2,4).'</th>';};
		}
	?>
			<th></th>

	<?php }else{	?>

				<?php  if(mb_stristr('ST',$xfrepor)): ?>
				<th >CODIGO</th>
				<th >CUENTA</th>
				<th >INICIAL</th>
				<th >DEBITOS</th>
				<th >CREDITOS</th>
				<th >SALDO</th>
				<?php endif; ?>
  <?php } ?>

  <?php
if(mb_strstr('BR',$xfrepor))
  {
  ?>
  <?php }?>
</tr>


  <?php
  if(mb_strstr('BR',$xfrepor))
  {
  if ($xf2col=='true')
  {
  ?>
  <tr valign="top">
  <td>
  <table>
  <?php }?>
  <?php }?>



<?php
	if ($xmeses>=12 && $xfnivel >3){$xfnivel--;} 
?>

<?php 
$filax=0; 
$xsaldo = 0;

while($r_icont=mysql_fetch_assoc($res_icont)) 
{
?>



<?php
$xsaldo0=1/100;
if(
   ($xfcero=='true') 
   or ($r_icont['nivel']<$xfnivel) 
   or (mb_strstr('BR',$xfrepor) and $xfcero=='false' and $r_icont['saldo']!=0)
   or (mb_strstr('ST',$xfrepor) and $xfcero=='false' and +abs($r_icont['saldoi'])+abs($r_icont['tdeb'])+abs($r_icont['tcre'])+abs($r_icont['saldo'])!=0)
  )

{
?>

		
<?php
$xfont='';
$xtabs='';
if(mb_strstr('BRT',$xfrepor))
{
  if ($xfnivel==3)
  {
    if($r_icont['nivel']=='1'){$xfont='<font size="3"><b>';}
    if($r_icont['nivel']=='2'){$xfont='<font size="3">';$xtabs='.';}
    if($r_icont['nivel']=='3'){$xfont='<font size="2"><i>';$xtabs='..';}

    if($r_icont['nivel']=='4'){$xfont='<font size="2">';$xtabs='...';}
    if($r_icont['nivel']=='5'){$xfont='<font size="2">';$xtabs='....';}
  }

  if ($xfnivel==4)
  {
    if($r_icont['nivel']=='1'){$xfont='<font size="4"><b><i>';}
    if($r_icont['nivel']=='2'){$xfont='<font size="3"><b>';}
    if($r_icont['nivel']=='3'){$xfont='<font size="3">';$xtabs='.';}
    if($r_icont['nivel']=='4'){$xfont='<font size="2"><i>';$xtabs='..';}

    if($r_icont['nivel']=='5'){$xfont='<font size="2"><i>';$xtabs='...';}
  }

  if ($xfnivel==5)
  {
    if($r_icont['nivel']=='1'){$xfont='<font size="4"><b>';}
    if($r_icont['nivel']=='2'){$xfont='<font size="4"><b><i>';}
    if($r_icont['nivel']=='3'){$xfont='<font size="3"><b>';$xtabs='.';}
    if($r_icont['nivel']=='4'){$xfont='<font size="3">';$xtabs='..';}
    if($r_icont['nivel']=='5'){$xfont='<font size="2"><i>';$xtabs='...';}
  }
}
?>

<?php
if(mb_strstr('BR',$xfrepor))
{
if ($xf2col=='true')
{
if($r_icont['ctas_codi']=='2' or $r_icont['ctas_codi']=='5'){echo '</table></td><td style="border-left:solid 2px #000000"><table>';}
$xtabs=substr($xtabs,0,-1);
}
}


?>
<?php          
      $xctascodigo=trim($r_icont['ctas_codi']);
      $xctasdescri=trim($r_icont['ctas_desc']);
      if($xctasdescri=='')
      {
        $sql_ctax='select * from ctas0 where ctas_codigo like "'.$xctascodigo.'%"';
        $res_ctax=mysql_query($sql_ctax,$link_vcrx);
        $xctasdescri=trim(@mysql_result($res_ctax,0,'ctas_descri'));
      }
?>


<tr valign="top" style="<?php if($filax!=3){$filax++;}else{echo 'border-bottom:dotted 1px #000000;'; $filax=0;}?>">
  <td align="left"><?php echo $xfont.$r_icont['ctas_codi']; ?></td>
  <td align="left"><?php echo $xtabs.$xfont.$xctasdescri; ?></td>


	
		<?php if ($xfcom1=='true' || $xfcom2=='true'){ ?> 
		<?php if(mb_stristr('ST',$xfrepor)){ ?>
		<?php 	echo '<td align="right">'.$xfont.number_format($r_icont['saldoi'],2).'</td>'; ?>
		<?php } ?>

					<?php	
						if ($xmeses>12){$xmeses=12;}
						for ($cmes=1;$cmes<=$xmeses;$cmes++)
						{
							$xcmes=$cmes;
							if ($xcmes<10){$xcmes='0'.$cmes;}
							if ($xfcom1=='true'){echo '<td align="right">'.$xfont.number_format($r_icont['s'.$xcmes],2).'</td>';}
							if ($xfcom2=='true'){echo '<td align="right">'.$xfont.number_format($r_icont['d'.$xcmes]-$r_icont['c'.$xcmes],2).'</td>';}
						}
					?>
		
		<?php }else{ ?> 
	
	
				<?php if(mb_strstr('ST',$xfrepor)){?>
						<td align="right">
						<?php
						if((mb_stristr('1.5.6.7.9',$r_icont['ctan1']) and $r_icont['saldoi']<0) 
						or (mb_stristr('2.3.4.8',$r_icont['ctan1']) and $r_icont['saldoi']>0) 
						)
						{echo '<font color="red">';} 
						?>
						<?php echo'<span >'.$xfont.number_format($r_icont['saldoi'],2).'</span>';?>
						</td>

					<td align="right"><?php echo $xfont.number_format($r_icont['tdeb'],2); ?></td>
						<td align="right"><?php echo $xfont.number_format($r_icont['tcre'],2); ?></td>
						<td align="right">
						<?php
// 						if((mb_stristr('1.5.6.8.9',$r_icont['ctan1']) and $r_icont['saldo']<0) 
// 						or (mb_stristr('2.3.4.7',$r_icont['ctan1']) and $r_icont['saldo']>0) 
// 						)
// 						{echo '<font color="red">';} 
						?>
						<?php echo'<span >'.$xfont.number_format($r_icont['saldo'],2).'</span>';?>
						</td>
				<?php } ?>


				<?php
			if(mb_strstr('BR',$xfrepor))
				{
				?>

				<?php
				if($r_icont['nivel']<'5'){$filax=0;}		
				?>

				<?php
				if ($xf2col=='false')
				{
				if($r_icont['nivel']<'3'){echo'<td></td>';}
				}
				if($r_icont['nivel']<'5'){echo'<td></td>';}		
				?>


				<td align="right">
				<?php
// 				if((mb_stristr('1',$r_icont['ctan1']) and $r_icont['saldo']<0) 
// 				or (mb_stristr('2.3',$r_icont['ctan1']) and $r_icont['saldo']>0) 
// 				or (mb_stristr('4.7',$r_icont['ctan1']) and $r_icont['saldo']>0) 
// 				or (mb_stristr('5.6.8.9',$r_icont['ctan1']) and $r_icont['saldo']<0) 
// 				)
// 				{echo '<font color="red">';} 
				?>

				<?php if(mb_stristr('1',$r_icont['ctan1']))
				{echo'<span >'.$xfont.number_format(($r_icont['saldo']),2).'</span>';}?>

				<?php if(mb_stristr('2.3',$r_icont['ctan1']))
				{echo'<span >'.$xfont.number_format(($r_icont['saldo']*-1),2).'</span>';}?>

				<?php if(mb_stristr('4.7',substr($r_icont['ctan1'],0,1)))
				{echo'<span >'.$xfont.number_format(($r_icont['saldo']*-1),2).'</span>';}?>

				<?php if(mb_stristr('5.6.8.9',substr($r_icont['ctan1'],0,1)))
				{echo'<span >'.$xfont.number_format(($r_icont['saldo']),2).'</span>';}?>

				</td>


				<?php }?>
		<?php }?>
    
</tr>

<?php }?>
<?php }?>


  <?php
if(mb_strstr('BR',$xfrepor))
  {
  if($xf2col=='true')
  {
  ?>
  </table>
  </td>
  </tr>
  <?php }?>
  <?php }?>



  <tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
  <?php
if(mb_strstr('ST',$xfrepor))
  {
		$res_icontt=mysql_query($sql_icontt,$link_vcrx);
  ?>
	<?php if($xfcom1=='true') :  ?>
			<th></th>
			<th>TOTALES</th>
	<?php	
// 		$sql_meses='select distinct fanio,fmes,fmesn from cont12 where fecha<="'.$xfecha2.'" order by fanio,fmes';
// 		$res_meses=mysql_query($sql_meses,$link_vcrx);
// 		$xmeses = mysql_num_rows($res_meses);

						for ($cmes=1;$cmes<=$xmeses;$cmes++)
						{
							$xcmes=$cmes;
							if ($xcmes<10){$xcmes='0'.$cmes;}
							echo '<th align="right">'.$xfont.number_format(mysql_result($res_icontt,0,'s'.$xcmes),2).'</th>';
						}
	?>

			<th></th>
		
	<?php else: ?>		

	<th></th>
  <th>TOTALES</th>
  <th align="right"><?php echo number_format(mysql_result($res_icontt,0,'saldoi'),2);?></th>
  <th align="right"><?php echo number_format(mysql_result($res_icontt,0,'tdeb'),2);?></th>
  <th align="right"><?php echo number_format(mysql_result($res_icontt,0,'tcre'),2);?></th>
  <th align="right"><?php echo number_format(mysql_result($res_icontt,0,'saldo'),2);?></th>
	<?php endif; ?>		

  <?php }?>
	</tr>



</table>

<hr>
<br>
<br>
<br>
<br>
<br>
<table>
<tr>
				<td></td>
				<td align="center" valign="top" style="width:40mm; border-top:solid 1px;">FIRMA AUTORIZADA</td>
				<td></td>
  </tr>  
  
  </table>  

</div>
</div>

</body>
</html>
