<?php
require('../cfg/res.php');
// require('../../_lib/vrdocu0.php');
include('php_gene1.php');
//$sql_deta='select * from '.$tabla->tabla.'12 where idnum2='.$xidnum1.' order by iddet desc';
// echo '***'.$xidnum1;

$xidnum1=$_POST['idnumx'];
$xidnum1=intval($xidnum1);

// $tabla=new vrdocu0('../cfg/res.php',$_POST['docu']);
$tabla=r_fiel('docu',$_POST['docu'],'tabla');
$tabla=strtolower($tabla);
// $sql_tot1='select * from '.$tabla->tabla.'2 where idnum2='.$xidnum1;.

$sql_tot1='select * from '.$tabla.'2 where idnum2='.$xidnum1;
$res_tot1=mysql_query($sql_tot1,$link_vcrx);

// $sql_tota='select * from '.$tabla.'11 where idnum1='.$xidnum1;

$sql_tota ='select  ';
$sql_tota.=' docu,numero,fecha,';

$sql_tota.=' sum(tcant) as tcant,';
$sql_tota.=' sum(total) as total,';

if (mb_stristr('vtas.comp.clie.prov',$tabla))
{
  $sql_tota.=' max(dcto) as dcto,';
  $sql_tota.=' sum(tdcto) as tdcto,';
  $sql_tota.=' max(ice) as ice,';
  $sql_tota.=' sum(tsubtice) as tsubtice,';
  $sql_tota.=' sum(tice) as tice,';
  $sql_tota.=' sum(tsubtiva0) as tsubtiva0,';
  $sql_tota.=' sum(tsubtiva) as tsubtiva,';
  $sql_tota.=' max(iva) as iva,';
  $sql_tota.=' sum(tiva) as tiva,';
//   $sql_tota.=' sum(tsubtirbp) as tsubtirbp,';
  $sql_tota.=' max(irbp) as irbp,';
  $sql_tota.=' sum(tirbp) as tirbp,';
//   $sql_tota.=' max(irbp) as irbp,';
  $sql_tota.=' sum(tsubt) as tsubt,';
}

if (mb_stristr('retc.retp',$tabla))
{
  $sql_tota.=' sum(cant) as valbase,';
  $sql_tota.=' sum(case when cair like "3%" or cair like "4%" then abs(cant) else 0 end) as basefte,';
  $sql_tota.=' sum(case when cair not like "3%" and cair not like "4%" then abs(cant) else 0 end) as baseiva,';
  $sql_tota.=' sum(case when cair like "3%" or cair like "4%" then abs(total) else 0 end) as retefte,';
  $sql_tota.=' sum(case when cair not like "3%" and cair not like "4%" then abs(total) else 0 end) as reteiva,';
}
	$view_sql.='case ';
		 $view_sql.='when ctas_status="X" or ctas_status="I" then 0 ';
		 $view_sql.='when length(ctas_codigo)=1 or length(ctas_codigo)=2 then 1 ';
		 $view_sql.='when length(ctas_codigo)=3 or length(ctas_codigo)=4 then 2 ';
		 $view_sql.='when length(ctas_codigo)=5 or length(ctas_codigo)=6 then 3 ';
		 $view_sql.='when length(ctas_codigo)=7 or length(ctas_codigo)=8 then 4 ';
		 $view_sql.='when length(ctas_codigo)=9 or length(ctas_codigo)=10 then 5 ';
		 $view_sql.='when length(ctas_codigo)>=11 then 5 ';
		 $view_sql.='else 0 ';
		 $view_sql.='end as ctas_nivel';

if (mb_stristr('caja.bcos.cjte.dxco',$tabla))
{
  $sql_tota.=' sum(cant) as valbase,';
//   $tlineas=mysql_num_rows($res_tota);
}

if (mb_stristr('rolp.nvdd',$tabla))
{
//   $tlineas=mysql_num_rows($res_tota);
//   $sql_tota.=' sum(cant) as valbase,';
//   $tingreso=abs(mysql_result($res_tota,0,'tingreso'));
//   $tegreso=abs(mysql_result($res_tota,0,'tegreso'));
//   $tprovi=abs(mysql_result($res_tota,0,'tprovi1'))+abs(mysql_result($res_tota,0,'tprovi2'));
}

if (mb_stristr('nomi',$tabla))
{
//   $tsueldo=mysql_result($res_tota,0,'sueldo');
//   $tporanti=mysql_result($res_tota,0,'poranti');
//   $tvalanti=mysql_result($res_tota,0,'valanti');
}
// $total=mysql_result($res_tota,0,'total');


if (mb_stristr('nvdd.rolp',$tabla))
{
  $sql_tota.=' sum(tingreso) as tingreso,';
  $sql_tota.=' sum(tegreso) as tegreso,';
  $sql_tota.=' sum(tprovi1) as tprovi1,';
  $sql_tota.=' sum(tprovi2) as tprovi2,';
}




if (mb_stristr('cont',$tabla))
{
  $sql_tota.=' sum(tdebito) as tdebito,';
  $sql_tota.=' sum(tcredito) as tcredito,';
  $sql_tota.=' sum(diferencia) as diferencia,';
// 			$tlineas=mysql_num_rows($res_tota);
//   $tdebito=abs(mysql_result($res_tota,0,'tdebito'));
//   $tcredito=abs(mysql_result($res_tota,0,'tcredito'));
//   $tdifer=abs(mysql_result($res_tota,0,'diferencia'));
//   $total=mysql_result($res_tota,0,'total')/2;
// 			$tprovi=abs(mysql_result($res_tota,0,'tprovi1'))+abs(mysql_result($res_tota,0,'tprovi2'));
}



$sql_tota.=' status,stat_descri ';
$sql_tota.=' from '.$tabla.'12 ';
$sql_tota.=' where idnum1='.$xidnum1;


$res_tota=mysql_query($sql_tota,$link_vcrx);


if($tabla==''){echo 'NUEVO REGISTRO';die();}

if (mb_stristr('nomi',$tabla))
{
$sql_tota='select * from '.$tabla.'12 where idnum1='.$xidnum1.' and fecha2<now() order by fecha2 desc';
$res_tota=mysql_query($sql_tota,$link_vcrx);
}


if ($xidnum1==0)
{
	echo '<div><strong><font size="4">REGISTRO NUEVO.</div>';
}
else
{
// 	echo $sql_tota;	
  $totregi=@mysql_num_rows($res_tot1);
	if($totregi==0)
	{echo '<div><strong><font size="4">REGISTRO SIN DETALLE</div>';}
	else
	{
		if(mysql_num_rows($res_tota)!=0)
		{
	// 		$tcant=mysql_result($res_tota,0,'tcant');
	// 		$tsubt=mysql_result($res_tota,0,'tsubt');
			if (mb_stristr('vtas.comp.clie.prov',$tabla))
			{
				$tcant=abs(mysql_result($res_tota,0,'tcant'));
				$tsubt=abs(mysql_result($res_tota,0,'tsubt'));

				$iva=abs(mysql_result($res_tota,0,'iva'));
				$tdcto=abs(mysql_result($res_tota,0,'tdcto'));
				$tsubtice=abs(mysql_result($res_tota,0,'tsubtice'));
				$tice=abs(mysql_result($res_tota,0,'tice'));
				$tsubtiva=abs(mysql_result($res_tota,0,'tsubtiva'));
				$tsubtiva0=abs(mysql_result($res_tota,0,'tsubtiva0'));
				$tiva=abs(mysql_result($res_tota,0,'tiva'));
				$tirbp=abs(mysql_result($res_tota,0,'tirbp'));
			}

			if (mb_stristr('inve.prod',$tabla))
			{
      }

			if (mb_stristr('retc.retp',$tabla))
			{
				$tlineas=mysql_num_rows($res_tota);
				$tvalbase=abs(mysql_result($res_tota,0,'valbase'));
				$tbaseiva=abs(mysql_result($res_tota,0,'baseiva'));
				$treteiva=abs(mysql_result($res_tota,0,'reteiva'));
				$tbasefte=abs(mysql_result($res_tota,0,'basefte'));
				$tretefte=abs(mysql_result($res_tota,0,'retefte'));
			}

			if (mb_stristr('caja.bcos.cjte.dxco',$tabla))
			{
				$tlineas=mysql_num_rows($res_tota);
			}

			if (mb_stristr('rolp.nvdd',$tabla))
			{
				$tlineas=mysql_num_rows($res_tota);
				$tingreso=abs(mysql_result($res_tota,0,'tingreso'));
				$tegreso=abs(mysql_result($res_tota,0,'tegreso'));
				$tprovi=abs(mysql_result($res_tota,0,'tprovi1'))+abs(mysql_result($res_tota,0,'tprovi2'));
			}

			if (mb_stristr('nomi',$tabla))
			{
				$tsueldo=mysql_result($res_tota,0,'sueldo');
				$tporanti=mysql_result($res_tota,0,'poranti');
				$tvalanti=mysql_result($res_tota,0,'valanti');
			}
			$total=mysql_result($res_tota,0,'total');

			if (mb_stristr('cont',$tabla))
			{
	// 			$tlineas=mysql_num_rows($res_tota);
				$tdebito=abs(mysql_result($res_tota,0,'tdebito'));
				$tcredito=abs(mysql_result($res_tota,0,'tcredito'));
				$tdifer=abs(mysql_result($res_tota,0,'diferencia'));
				$total=mysql_result($res_tota,0,'total')/2;
	// 			$tprovi=abs(mysql_result($res_tota,0,'tprovi1'))+abs(mysql_result($res_tota,0,'tprovi2'));
			}


			if(mb_stristr('XO',mysql_result($res_tota,0,'status'))){
				$stat_descri=mysql_result($res_tota,0,'stat_descri');
			}
		}
		else
		{
			$tcant=0;
			$tsubt=0;
		}
		//$tstat=mysql_result($res_tota,0,'status');
		//$tstat_descri=mysql_result($res_tota,0,'stat_descri');

	?>

	<table id="cab3_reg" align="left" border="0" width="75%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
				<?php if($tcant!=0){?>
					<tr>
						<td align="right">UNIDADES</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo $tcant;?></div></td>
					</tr>
					<?php }?>

					<?php if($tsubt!=0){?>
					<tr>
						<td align="right">SUBTOTAL</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tsubt,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tdcto!=0){?>
					<tr>
						<td align="right">DESCUENTO</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tdcto,2);?></div></td>
					</tr>
					<?php }?>



		<?php if($tice!=0){?>
					<tr>
						<td align="right">SUBTOTAL ICE</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tsubtice,2);?></div></td>
					</tr>
					<tr>
						<td align="right">I.C.E.</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tice,2);?></div></td>
					</tr>
					<?php }?>





					<?php if($tsubtiva0!=0){?>
					<tr>
							<td align="right">SUBTOTAL IVA 0</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tsubtiva0,2);?></div></td>
					</tr>
					<?php }?>


					<?php if($tiva!=0){?>
					<tr>
							<td align="right">SUBTOTAL IVA <?php echo number_format($iva,0);?>%</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tsubtiva,2);?></div></td>
					</tr>
					<tr>
							<td align="right">I.V.A.</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tiva,2);?></div></td>
					</tr>
					<?php }?>



					<?php if($tirbp!=0){?>
					<tr>
							<td align="right">IRBP</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tirbp,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tvalbase!=0){?>
					<tr>
							<td align="right">BASE IVA</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tbaseiva,2);?></div></td>
          </tr>
					<tr>
							<td align="right">RETE IVA</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($treteiva,2);?></div></td>
          </tr>
					<tr>
							<td align="right">BASE FTE</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tbasefte,2);?></div></td>
          </tr>
					<tr>
							<td align="right">RETE FTE</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tretefte,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tingreso!=0){?>
					<tr>
							<td align="right">INGRESOS</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tingreso,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tegreso!=0){?>
					<tr>
							<td align="right">EGRESOS</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tegreso,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tprovi1!=0){?>
					<tr>
							<td align="right">PROVISIONES</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tprovi1);?></div></td>
					</tr>
					<?php }?>

					<?php if($tdebito!=0){?>
					<tr>
							<td align="right">DEBITOS</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tdebito,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tcredito!=0){?>
					<tr>
							<td align="right">CREDITOS</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tcredito,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tdifer!=0){?>
					<tr>
							<td align="right">DIFERENCIA</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tdifer,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tsueldo!=0){?>
					<tr>
						<td align="right"><strong><font size="4">SUELDO ACTUAL</font></strong></td>
							<td align="right"><div style="border: solid 1px #000;" ><strong><font size="5"><?php echo number_format(abs($tsueldo),2);?></font></strong></div></td>
					</tr>
					<?php }?>

					<?php if($tporanti!=0){?>
					<tr>
							<td align="right">%ANTICIPO</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tporanti,2);?></div></td>
					</tr>
					<?php }?>

					<?php if($tvalanti!=0){?>
					<tr>
							<td align="right">ANTICIPO</td>
							<td align="right"><div style="border: solid 1px #000;" ><?php echo number_format($tvalanti,2);?></div></td>
					</tr>
					<?php }?>


					<?php if (!mb_stristr('nomi',$tabla)) {?>
					<tr>
						<td align="right"><strong><font size="4">TOTAL</font></strong></td>
							<td align="right"><div style="border: solid 1px #000;" ><strong><font size="5"><?php echo number_format(abs($total),2);?></font></strong></div></td>
					</tr>
					<?php }?>

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

	<tr>
		<td align="right">STATUS</td>
		<td align="center"><div style="border: solid 1px #000;"><font color="red" size="6" ><strong><?php echo $stat_descri;?></strong></font></div></td>
	</tr>



	</table>
	<?php } 

?>
<?php } ?>