<?php 
session_start();

require('../cfg/res.php');
require('../cfg/cfg.php');
include('php_gene1.php');

$xacceso=0;
$sql_acce='select * from usrdoc1 where docu like "DD%" and info="1" and user="'.$_SESSION['user_vcrx_id'].'" ';
$res_acce=mysql_query($sql_acce,$link_vcrx);
if (mysql_num_rows($res_acce)!=0){$xacceso=1;}
if (mb_stristr('sist.supe',$_SESSION['user_vcrx_tipo'])){$xacceso=1;}
if ($xacceso==0){echo '<font size="8" color="red"><b>';die('No tiene acceso a Informe de Contabilidad ' );}	
	


$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 (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
//	{$fini1=date('2010-11-01');}


	$enti=$_POST['enti'];
	$docu=$_POST['docu'];
	if(isset($_POST['fecha_ini']) && !empty($_POST['fecha_ini']))
	{$fini=$_POST['fecha_ini'];}
	else
	{$fini=$fini1;}
	
	if(isset($_POST['fecha_fin']) && !empty($_POST['fecha_fin']))
	{$ffin=$_POST['fecha_fin'];}
	else
	{$ffin=date('Y-m-d');}

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



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

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

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

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

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




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

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


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



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

if($xfrepor=='D')
{$sql_where='cant!=0 and fecha >= "'.$xfecha1.'" AND fecha <= "'.$xfecha2.'" ';}


if($xfrepor=='B')
{
	//$sql_where.=' and(ctan1="1" or ctan1="2" or ctan1="3") ';
	$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" or ctan1="5" or ctan1="6") ';
	$sql_where.=' and(ctan1>="4" and ctan1<="8") ';
	if($xfdtr1=='false')
		{$sql_where.=' and(docu not like "DTR%") ';}
// 	echo $sql_where.'****';
	
}

if($xfrepor=='S' or $xfrepor=='T')
{
	$sql_where.=' ';
}



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



if(isset($_POST['ctas']) && !empty($_POST['ctas']))
{
// 	$sql_where.=' and  (ctas like "'.$_POST['ctas'].'%" or ctas_descri like "%'.$_POST['ctas'].'%" ) ';
	$sql_where.=' and  (ctas like "'.$_POST['ctas'].'%") ';
	$xfctas=$_POST['ctas'];
}


if(isset($_POST['docu']) && !empty($_POST['docu']))
{
	$sql_where.=' and  (docu like "'.$_POST['docu'].'%" or docu_descri like "%'.$_POST['docu'].'%" ) ';
	$xdocu=$_POST['docu'];
}

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

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

if($enti!='')
{$sql_where.=' and  (enti like "'.$enti.'%" or enti_descri like "%'.$enti.'%" ) ';}

if($item!='')
{$sql_where.=' and  (item like "'.$item.'%" or item_descri like "%'.$item.'%" ) ';}



if(isset($_POST['fcsub']) && !empty($_POST['fcsub']))
{
	$sql_where.=' and  (csub like "'.$_POST['fcsub'].'%" or csub_descri like "%'.$_POST['fcsub'].'%" ) ';
	$xfcsub=$_POST['fcsub'];
}

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


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




if(isset($_POST['fctlg']) && !empty($_POST['fctlg']))
{
//	$sql_icont.=' and  (item_nctlg ='.$_POST['fctlg'].' )';
//	$xfctlg=$_POST['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 ';}

// echo $sql_where;


if(mb_strstr('D',$xfrepor))
{
	$sql_icont='';
	$sql_icont.='select *, sum(tdebito) as tdebitos, sum(tcredito) as tcreditos, sum(diferencia) as tdiferencias from cont12 where '.$sql_where.' ';
	$sql_icont.=' group by idnum1';
}


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


if(mb_strstr('BR',$xfrepor))
{
$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';
$res_icontt=mysql_query($sql_icontt,$link_vcrx);
}


$xmeses=0;
$xwidth = 100;
if ($xfcom1=='true' || $xfcom2=='true')
{
		$xfecha1=date('Y-m-01',strtotime($xfecha1));
		$xfecha2=date('Y-m-t', strtotime($xfecha2));
		$xf2col='false';
		$sql_meses='select distinct fanio,fmes,fmesn from cont12 where fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" 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){$xfecha1c=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($xfecha1,$xfecha1c,$sql_icont);
	$sql_icont=str_replace('cont12.*,','cont12.*,'.$sql_pivot,$sql_icont);

	$sql_icontt=str_replace($xfecha1,$xfecha1c,$sql_icontt);
	$sql_icontt=str_replace('cont12.*,','cont12.*,'.$sql_pivot,$sql_icontt);
	// 	echo $sql_icont;
	if($xmeses>6){$xwidth = 100 + ($xmeses-6)*10;}
	
}


// $sql_icont.=' order by ctas_codi,nivel,fanio,fmes ';

if(mb_strstr('D',$xfrepor))
{
	$sql_icont.=' order by fecha,docu,numero,idnum1,ctas ';
	$sql_icontt=$sql_icont;
}
else
{$sql_icont.=' order by ctas_codi,nivel ';}


$res_icont=mysql_query($sql_icont,$link_vcrx);
$res_icontt=mysql_query($sql_icontt,$link_vcrx);
// echo $sql_icont;
// echo str_repeat('-',date('s')*2).'';
// echo date('H:m:s');
?>


<table id="tabla_infcont" width="<?php echo $xwidth?>%" border="0" cellspacing="0" cellpadding="2" style="border-collapse:collapse;font-size:12px; border:#000000 solid 1px;">

<?php if(mb_strstr('D',$xfrepor)) { ?>
	
	<?php //explain_sql($sql_icont);  die();?>
	<tr style='border-bottom:solid 1px;'>
	<th></th>
	<th>DOCU</th>
	<th>NUMERO</th>
	<th>FECHA</th>
	<th>DEBITOS</th>
	<th>CREDITOS</th>
	<th></th>
	<th>CONCEPTO</th>
	<th>ST</th>
	</tr>	


	
<?php
$uidnum=0;
$filax=0;
// 	echo $sql_icont;
	while($r_icont=mysql_fetch_assoc($res_icont))
	{
?>
	<?php $filax++; ?>
	<?php if($r_icont['idnum1']!=$uidnum) {?>
	<tr class="filax" style="<?php if($filax % 5==0){echo 'border-bottom:dotted 1px;';} ?>">	
<!-- 		<td colspan=6>
		<table>
		<tr> -->
		<td>
		<?php if($r_icont['mdfrom']!=""){?>
		<a href="../print1.php?prnt=1&docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idfrom']; ?>&formato=3" target="_blank"><img src="../img/print2.png" width="20"  title="Documento:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a> 
<!-- 	<a href="../print1.php?prnt=1&docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idfrom']; ?>&formato=2" target="_blank"><img src="../img/print2.png" width="20"  title="Diario:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a>  -->
<!--  <a href="../lib/celectop1.php?docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idfrom']; ?>&formato=3" target="_blank"><img src="../img/print2.png" width="20"  title="Diario:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a> -->
<!-- 	<a href="../lib/celectop1.php?docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idfrom']; ?>&formato=1" target="_blank"><img src="../img/print2.png" width="20"  title="Documento:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a> -->
		<?php }else{?>
		<a href="../print1.php?prnt=1&docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idnum1']; ?>&formato=1" target="_blank"><img src="../img/print2.png" width="20"  title="Diario:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a> 
<!-- 		<a href="../lib/celectop1.php?docu=<?php echo $r_icont['docu']; ?>&idnum=<?php echo $r_icont['idnum1']; ?>&formato=1" target="_blank"><img src="../img/print2.png" width="20"  title="Diario:<?php echo $r_icont['docu'];?>-<?php echo $r_icont['numero'];?>"  /></a> -->
		<?php }?>
		</td>
		<td><?php echo $r_icont['docu'].' - '.$r_icont['docu_descri']; ?></td>

		<td align="center"><a href="#" onClick="icont1d('D','<?php echo $r_icont['idnum1']; ?>',this);">
				<?php echo'<span title="'.$r_icont['ptonumero'].'">'.$xfont.$r_icont['ptonumero'].'</span>';?></a> </td>
			
		<td align="center"><?php echo $r_icont['fechadmy']; ?> </td>
		<td align="right"><?php echo number_format($r_icont['tdebitos'],2); ?> </td>
		<td align="right"><?php echo number_format($r_icont['tcreditos'],2); ?> </td>
		<td align="right" style="color:red;">
		<?php if($r_icont['tdiferencias']!=0 ){ ?>
			<?php echo number_format($r_icont['tdiferencias'],2); ?>
		<?php}?>
		</td>
		<td><?php echo $r_icont['concepto']; ?> </td>
<!-- 		</tr>	
		</table>
		</td> -->
	</tr>
<!-- 
	<tr style='border-bottom:solid 1px;'>
	<th>CUENTA</th>
	<th>DESCRIPCION</th>
	<th>DEBITO</th>
	<th>CREDITO</th>
	<th>CSUB</th>
	<th>DETALLE</th>
	</tr>	
	 -->
	<?php //$filax=0; ?>
	
	<?php 
		$sql_icont2='select * from cont12 where idnum1 ="'.$r_icont['idnum1'].'" and ctas!="" and cant!=0 order by ctas';
// 																			echo '<br>'.$sql_icont2;
		$res_icont2=mysql_query($sql_icont2,$link_vcrx);
		while($r_icont2=mysql_fetch_assoc($res_icont2) and 1==2)
		{			
	?>
	
	<?php $filax++; ?>
  <?php
      $xctascodigo=trim($r_icont2['ctas']);
      $xctasdescri=trim($r_icont2['ctas_descri']);
      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 style="<?php if($filax % 5==0){echo 'border-bottom:dotted 1px;';} ?>">	
<!-- 	<td>AQUIII</td> -->
	<td><?php echo $xctascodigo; ?></td>
	<td><?php echo $xctasdescri; ?></td>
	<td align="right"><?php echo number_format($r_icont2['tdebito'],2); ?></td>
	<td align="right"><?php echo number_format($r_icont2['tcredito'],2); ?></td>
	<td><?php echo $r_icont2['csub_descri']; ?></td>
	<td><?php echo $r_icont2['detalle']; ?></td>
	</tr>	
	
	<?php } ?>
	<?php } ?>
	
	
	<?php $uidnum=$r_icont['idnum1']; ?>
	<?php } ?>
	
<?php }else{ ?>	
	
<tr style="border-bottom:#000000 solid 2px; ">

	<?php if($xfcom1=='true' || $xfcom2=='true'){  ?>
				<th></th>
				<th>CODIGO</th>
				<th>CUENTA</th>
		<?php if(mb_stristr('ST',$xfrepor)){ ?>
				<th>INICIAL</th>
		<?php } ?>
	<?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_strstr('ST',$xfrepor)){  ?>
				<th></th>
				<th>CODIGO</th>
				<th>CUENTA</th>
				<th>INICIAL</th>
				<th>DEBITOS</th>
				<th>CREDITOS</th>
				<th>SALDO</th>
				<th></th>
  <?php }	?>

	<?php }	?>

</tr>

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

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

while($r_icont=mysql_fetch_assoc($res_icont))
{
	$duni=2;

	if($xfnivel<=3)	{$filax=1;}

	$xpnivel=$univel;
	$univel=$r_icont['nivel'];

		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)
			)
	{
	$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 ($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 ($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='...';}
		}
	}

		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" class="filax" id="<?php echo $r_icont['idnum1']; ?>" align="center" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >
		<td>
				<a href="../lib/impr_icont0d.php?grupo=C&idnum=<?php echo $r_icont['ctas_codi']; ?>&fecha_ini=<?php echo $fini; ?>&fecha_fin=<?php echo $ffin; ?>" target="_blank"><img src="http://vcrxol.com/_img/print2.png" width="20"  title="Imprimir  <?php echo $r_icont['ctas'];?>"  /></a>
		</td>

		<td align="left"><a href="#" onClick="icont1d('C','<?php echo $r_icont['ctas_codi']; ?>',this);">
				<?php echo'<span title="'.$xctasdescri.'">'.$xfont.$r_icont['ctas_codi'].'</span>';?></a> </td>
		<td align="left"><?php echo'<span title="'.$xctasdescri.'">'.$xtabs.$xfont.$xctasdescri.'</span>';?></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.8',$r_icont['ctan1']) and $r_icont['saldoi']<0) 
					or (mb_stristr('2.3.4.9',$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'<span >'.$xfont.number_format($r_icont['tdeb'],2).'</span>';?></td>
				<td align="right"><?php echo'<span >'.$xfont.number_format($r_icont['tcre'],2).'</span>';?></td>
				<td align="right">
					<?php
					if((mb_stristr('1.5.6.7.8',$r_icont['ctan1']) and $r_icont['saldo']<0) 
					or (mb_stristr('2.3.4.9',$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.8',$r_icont['ctan1']) and $r_icont['saldo']<0) 
					or (mb_stristr('2.3.9',$r_icont['ctan1']) and $r_icont['saldo']>0) 
					or (mb_stristr('4',$r_icont['ctan1']) and $r_icont['saldo']>0) 
					or (mb_stristr('5.6.7',$r_icont['ctan1']) and $r_icont['saldo']<0) 
					)
					{echo '<font color="red">';} 
						if(mb_stristr('1.8',$r_icont['ctan1'])){echo'<span >'.$xfont.number_format(($r_icont['saldo']),2).'</span>';}
						if(mb_stristr('2.3.9',$r_icont['ctan1'])){echo'<span >'.$xfont.number_format(($r_icont['saldo']*-1),2).'</span>';}
						if(mb_stristr('4',substr($r_icont['ctan1'],0,1))){echo'<span >'.$xfont.number_format(($r_icont['saldo']*-1),2).'</span>';}
						if(mb_stristr('5.6.7',substr($r_icont['ctan1'],0,1))){echo'<span >'.$xfont.number_format(($r_icont['saldo']),2).'</span>';}
					?>
				</td>

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

		<?php }?>

 		<?php } ?> 

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

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

  <?php
  if(mb_strstr('ST',$xfrepor))
  {
  ?>
  <tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">

	<?php if($xfcom1=='true' || $xfcom2=='true'){  ?>
			<th></th>
			<th></th>
			<th>TOTALES</th>
	<?php	
		$res_icontt=mysql_query($sql_icontt,$link_vcrx);

						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_icontt['s'.$xcmes],2).'</td>';}
							if ($xfcom2=='true'){echo '<td align="right">'.$xfont.number_format($r_icontt['d'.$xcmes]-$r_icontt['c'.$xcmes],2).'</td>';}
// 							echo '<th align="right">'.$xfont.number_format(mysql_result($res_icontt,0,'s'.$xcmes),2).'</th>';
						}
	?>

			<th></th>
		
	<?php }else{ ?>		
		
			<th></th>
			<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 } ?>		

	</tr>

	<?php }?>

<?php } ?>	

</table>
</div>
<br>




