<?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');
	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['fcom1']) && ($_GET['fcom1']=='true'))
{$xfcom1=$_GET['fcom1'];}
else
{$xfcom1='false';}

echo $xfcom1;

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

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



$sql_where='fecha >= "'.$fini1.'" AND fecha <= "'.$ffin.'"';

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

if($xfrepor=='S')
{
$sql_where='fecha >= "'.$fini.'" and fecha <= "'.$ffin.'"';
}

if($xfrepor=='B')
{
	$sql_where.=' and(ctan1="1" or ctan1="2" or ctan1="3") ';
}

if($xfrepor=='R')
{
	$sql_where.=' and(ctan1="4" or ctan1="5" or ctan1="6") ';
}




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


if(isset($_GET['fctas']) && !empty($_GET['fctas']))
{
	$sql_where.=' and  (ctas like "'.$_GET['fctas'].'%" ';
	$xfctas=$_GET['fctas'];
}


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['fcsub']) && !empty($_GET['fcsub']))
{
	$sql_where.=' and  (item_csub like "'.$_GET['fcsub'].'%" or csub_descri like "%'.$_GET['fcsub'].'%" ) ';
	$xfcsub=$_GET['fcsub'];
}

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(total) 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(total) 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(total) 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(total) 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(total) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where.' group by ctan5 ';}


if($xfrepor=='S')
{
$sql_icont='           select cont12.*,'.$xfnivel.' as nivel, ctan'.$xfnivel.' as ctas_codi, descri'.$xfnivel.' as ctas_desc, sum(total) as saldo, sum(tdebito) as tdeb, sum(tcredito) as tcre from cont12 where '.$sql_where;
$res_icontt=mysql_query($sql_icont,$link_vcrx);
$sql_icont.=' group by ctan'.$xfnivel;
}

if($xfrepor=='B' or $xfrepor=='R')
{
$sql_icontt='           select cont12.*,1 as nivel, ctan1 as ctas_codi, descri1 as ctas_desc, sum(total) 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')
{
		$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 cont11 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);
// 	echo $sql_icont;
	if($xmeses>6){$xwidth = 100 + ($xmeses-6)*10;}
	
}








$sql_icont.=' order by ctas_codi';

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

@page {
/*    size: 7in 9.25in; */
/* 	 top left bott right */
<?php if ($xmeses>6){echo 'size: 297mm 215mm;'; }	else{echo 'size: 215mm 297mm;'}?>
/*     size: 215mm 297mm;  */
   margin: 15mm 0mm 0mm 0mm;
}
	
	
	
	
</style>

</head>
<body>

<div align="center">
<div align="center" style="width:1150px; font-size:12px;">
<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>
	<td><?php echo date_format(date_create($fini),'d/M/Y').' - '.date_format(date_create($ffin),'d/M/Y');?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">REPORTE</td>
	<td>
	<?php
	if($xfrepor=='S'){echo 'SUMAS Y SALDOS';};
	if($xfrepor=='B'){echo 'BALANCE GENERAL';};
	if($xfrepor=='R'){echo 'RESULTADOS';};
	if($xfcom1=='true'){echo '(COMPARATIVO)';};
	?>
	</td>
</tr>
<tr>
    	<td style="font-weight:bolder;">NIVEL</td>
	<td><?php echo $xfnivel;?></td>
</tr>


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



</table>




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




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


<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') :  ?>
				<th>CODIGO</th>
				<th>CUENTA</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);
		$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 >DEBITOS</th>
				<th >CREDITOS</th>
				<th >SALDO</th>
				<?php endif; ?>
  <?php endif; ?>

  <?php
  if($xfrepor=='B' or $xfrepor=='R' )
  {
  ?>
  <?php }?>


</tr>


  <?php
  if($xfrepor=='B' or $xfrepor=='R' )
  {
  if ($xf2col=='true')
  {
  ?>
  <tr valign="top">
  <td>
  <table>
  <?php }?>
  <?php }?>




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

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

<?php
$xsaldo0=1/100;
//if(($xfcero=='true') or ($xfcero='false' and $r_icont['saldo']!=0))
if(($xfcero=='true') or ($r_icont['nivel']<$xfnivel) or ($xfcero=='false' and $r_icont['saldo']!=0))

{
?>


<?php
$xfont='';
$xtabs='';
if($xfrepor=='B' or $xfrepor=='R')
{

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='...';}
}
}
?>

<?php
if($xfrepor=='B' or $xfrepor=='R' )
{
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);
}
}


?>


<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.$r_icont['ctas_desc']; ?></td>

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


  <?php
  if($xfrepor=='B' or $xfrepor=='R')
  {
  ?>

  <?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',$r_icont['ctan1']) and $r_icont['saldo']>0) 
	or (mb_stristr('5.6',$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',$r_icont['ctan1']))
  {echo'<span >'.$xfont.number_format(($r_icont['saldo']*-1),2).'</span>';}?>

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

  </td>


  <?php }?>
    
</tr>

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


  <?php
  if($xfrepor=='B' or $xfrepor=='R' )
  {
  if($xf2col=='true')
  {
  ?>
  </table>
  </td>
  </tr>
  <?php }?>
  <?php }?>



  <?php
  if($xfrepor=='S')
  {
  ?>
  <tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
  <th></th>
  <th>TOTALES</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>
  </tr>
  <?php }?>



</table>




</div>
</div>

</body>
</html>
