<?php session_start();?>
<?php include('../_cfg/config.php');?>
<?php include('php_gene1.php'); ?>
<?php include('php_session.php');?>

<?php foreach($_POST as $campo => $valor){${$campo}=$valor;} ?>

<?php if($pant==''){$pant='PANT0';}?>

<?php if(!mb_stristr('PANT.GRAB',substr($pant,0,4))) {return;} ?>


<?php

$xenti='';
$xdocu='FP10';
$xptoe='001001';
// $numero='';
$xcair='312';
if($idnum=='undefined'){$idnum='';}  
if($docu=='undefined' or $docu==''){$docu=r_fiel('comp1',$idnum,'docu');}  

// echo $docu.$idnumf

?>

<?php	
	
$user_anul=0;
$user_elim=0;

$modi_docu=1;

if($user_tipo=='sist'){$user_anul=1;$user_elim=1;}
if($user_tipo=='supe'){$user_anul=1;$user_elim=1;}
// if($user_tipo=='clie' and substr($docu,0,2)=='OP'){$user_anul=1;}
if($user_tipo=='clie' and substr($docu,0,2)=='PC'){$user_anul=1;}


// echo '*/*/*'.$docu;
if($docu=='undefined'){$docu='';}
if($docu==''){$docu='FP10';}

// else
// {
  $docu.='10';
  $docu=substr($docu,0,4);
// }


$tabla=r_fiel('docu',$docu,'tabla');
$entilabel='ENTIDAD';

$tabla='retp';
  

if($tabla=='acce'){$user_anul=0;$user_elim=0;$modi_docu=0;}


$xfontsize=12;
if($orient=='V'){$xfontsize=40;}
if($orient=='H'){$xfontsize=12;}

$hpant1 =ceil($screenh*0.30);
$hpant11=ceil($screenh*0.10);
$hpant2 =ceil($screenh*0.30);

$wpant1 =$screenw*.9;
	
if ($orient=='V')
{
	$hpant1 =ceil($screenh*0.90);
	$hpant11=ceil($screenh*0.40);
	$hpant2 =ceil($screenh*0.90);
  $wpant1 =$screenw-20;
}

?>

<?php
$docuacce='RPRO';  
include('php_useracce.php');
?>


<?php ob_start(); ?>
<resultado>
  
<?php val_usercodigo(); ?>
  
<?php $xobj=3;?>
<?php if($mobile=='true'){$xobj=3; $xfontsize=$xfontsize*1.0; }?>
  
<?php
// PRUEBAS	
$date=date('H:i:s',strtotime('now')) ;
//   echo '*/*/*/'.$user_nuev.$user_cons;
  
?>	
	
  
<?php if(mb_stristr('PANT0',$pant) and ($user_nuev==1 or $user_cons==1)) { ?>
<?php //echo get_ced1('0915379374');?>	
<?php $fecha1=date('Y-m-01',strtotime ('today')); ?>
<?php $fecha2=date('Y-m-01',strtotime ('+1 month')); ?>
<?php $fecha2=date('Y-m-d',strtotime ($fecha2.' -1 day')); ?>
<?php $fecha2=$fechaa; ?>

<?php $pidtxt='dl_prta'; $pdocu='RPRO'; $pcolu='puni'; $pwhere='(cair like "3%" or cair like "4%")'; include('vrsetlist.php');?>
<?php $pidtxt='dl_piva'; $pdocu='RPRO'; $pcolu='puni'; $pwhere='(cair not like "3%" and cair not like "4%")';include('vrsetlist.php');?>
  
  
<?php
if($idnum=='' or $idnum=='undefined')
{
  $sql_docu =' select idnum1,docu,numero from comp1 ';
  $sql_docu.=' where concat(docu,numero) not in(select concat(docum,numer) as clave from retp12 where docum is not null and numer is not null) ';
  $sql_docu.=' and enti not like "99%"';
  $sql_docu.=' and status!="O"';
  $sql_docu.=' and status!="X"';
  $sql_docu.=' order by idnum1 desc';
  $sql_docu.=' limit 10';
//   $res_docu=msql_query($sql_docu,$link_vcrx);
//   $idnum=@msql_result($res_docu,0,'idnum1');
}

if($idnum!='' and $idnum!='undefined')
{
  $sql_comp ='select *, max(cair*1) as max_cair,  ';
  $sql_comp.='sum(tsubtiva0+tsubtiva) as base_rta, ';
  $sql_comp.='sum(tiva) as base_iva ';
  $sql_comp.=' from comp12 ';
  $sql_comp.=' where idnum1='.$idnum.' ';
//   $sql_comp.=' group by idnum1';
  $sql_comp.=' group by idnum1, cair';
  $sql_comp.=' order by cair ';
  $res_comp=msql_query($sql_comp,$link_vcrx);
//   explain_sql($sql_docu);
  
  $xenti=msql_result($res_comp,0,'enti');
  $xfecha=msql_result($res_comp,0,'fecha');
  $xdocu=msql_result($res_comp,0,'docu');
  $xptoe=msql_result($res_comp,0,'ptoe');
  $xnumero=msql_result($res_comp,0,'numero');
  
  $xcair1=msql_result($res_comp,0,'max_cair');
  $base_rta=msql_result($res_comp,0,'base_rta');
  $base_iva=msql_result($res_comp,0,'base_iva');
  
  $sql_docu ='select *, ';
  $sql_docu.=' max(cair*1) as max_cair,';
  $sql_docu.=' max(prta) as por_rta, ';
  $sql_docu.=' max(piva) as por_iva';
  $sql_docu.=' from retp12';
  $sql_docu.=' where enti like "'.substr($xenti,0,10).'%" ';
  $sql_docu.=' and status!="O" ';
  $sql_docu.=' group by idnum1';
  $sql_docu.=' order by fecha desc';
  $res_docu=msql_query($sql_docu,$link_vcrx);
//   explain_sql($sql_docu);
  $xcair=@msql_result($res_docu,0,'max_cair').'';
  $porc_rta=@msql_result($res_docu,0,'por_rta');
  $porc_iva=@msql_result($res_docu,0,'por_iva');
  
//   echo $xcair;
  
  if($xcair==''){$xcair=$xcair1;}
  if($porc_rta==0){$porc_rta=r_fiel('cair',$xcair,'porc');}
  if($porc_iva==0){$porc_iva=r_fiel('comp1',$idnum,'priva');}
  
  
}
?>

  <?php	val_usercodigo(); ?>
  <?php $pidtxt='detadocu'; $pcaption='Detalle'; $ponclick='regi_rpro("DETA")'; $psize=$xfontsize; $pdefa=0; $phide=1; include('vrchkbox.php'); ?>

		<div id="pant0" style="border:solid 2px; overflow:auto;">
		<?php { ?>
			<table id="tpant0" cellpadding="2" width="99%" border="0"
					 style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:white;">
				<tr>
          <td valign="top">
          <table width="100%" cellpadding="2" border="0"
                style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:white;">
              <tr>
                <?php if($mobile!='true'){?>        
                <td width="25%">
                </td>
                <?php } ?>
                <td colspan="2" align="center" <?php if($mobile=='false'){echo 'style="display:none;"';}else{echo 'style="text-transform:uppercase;font-weight:bold;font-family:arial;font-size:'.($xfontsize*1.2).';"';} ?> >
                  <titulo>Registro de Retenciones</titulo>
                </td>
              </tr>
            
              <tr>
                <?php $pidtxt='enti'; $pcaption=$entilabel; $ptabla='enti';  $psize=$xfontsize; $pdefa=$xenti;  $pobj=$xobj; if($idnum!=''){$preado=1;}  include('vrgetcod.php'); ?>
              </tr>
              <tr>
                <?php $pidtxt='docu'; $pcaption='DOCUM.'; $ptabla='docu';  $psize=$xfontsize; $pdefa=$docu;  $pobj=$xobj; if($idnum!=''){$preado=1;} include('vrgetcod.php'); ?>
              </tr>
            
              <tr>
                <?php $pidtxt='ptoe'; $pcaption='Pto.Emi.';  $psize=$xfontsize; $pdefa=$xptoe; $plong=6;   if($idnum!=''){$preado=1;};$pobj=$xobj; include('vrgetnum.php'); ?>
              </tr>
              <tr>
                <?php $pidtxt='numero'; $pcaption='Numero';  $psize=$xfontsize; $pdefa=$xnumero; $pobj=$xobj; if($idnum!=''){$preado=1;} include('vrgetnum.php'); ?>
              </tr>
              <tr>
                <?php $pidtxt='fecha'; $pcaption='Fecha';  $psize=$xfontsize; $pdefa=$xfecha; $pobj=$xobj; if($mobile=='true'){$pobj=6;}; if($idnum!=''){$preado=1;} include('vrgetfec.php'); ?>
              </tr>
         </tr>

            
            

            
          <?php if($mobile!='true'){?>        
         </table>

          </td>
          <td valign="top">
            
          <table width="100%" cellpadding="2" border="0"
                 style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:white;">
            <tr>
              <td width="25%"></td>
            </tr>
          <?php } ?>
            
            <tr>
              <td width="1%" colspan="2">
                <table width="50%" cellpadding="2" border="0" caption=""
                       style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:gainsboro;">
                <tr>
                  <th colspan="3">RETENCION RENTA</th>
                </tr>
                <tr>
                  <th align="left">CAIR</th>
                  <th align="center">BASE</th>
                  <th align="center">%</th>
                </tr>
                      <?php $crow=0; ?>
                      <?php //$res_comp=msql_query($sql_comp,$link_vcrx); ?>
                     <?php msql_data_seek($res_comp,0); ?>
                      <?php while($aso_comp=@msql_fetch_assoc($res_comp)){ ?>
                      <?php $crow++; ?>
                      <?php $duni=$aso_comp['item_duni']; ?>
                      <?php $cair=$aso_comp['cair']; ?>
                      <?php $base_rta=$aso_comp['base_rta']; ?>
                      <?php $porc_rta=$aso_comp['prrta']; ?>
                      <?php $porc_rta=$aso_comp['cair_porc']; ?>
                      <?php $base_iva=$aso_comp['priva']; ?>
                      <tr>
                        <?php $pidtxt='cair'.$crow; $pcaption='CAIR'; $ptabla='cair'; $pdefa=$cair; $psize=$xfontsize;   if($mobile=='true'){$pobj=6;}; $pobj=1; include('vrgetcod.php'); ?>
                        <?php $pidtxt='base_rta'.$crow; $pcaption='base rta'; $pdefa=number_format($base_rta,2,'.','');  $pobj=1; $psize=$xfontsize; include('vrgetval.php'); ?>
                        <?php $pidtxt='porc_rta'.$crow; $pcaption='% rta'; $pdefa=@number_format($porc_rta,2); $psize=$xfontsize;  $pobj=1; $plist='dl_prta'; include('vrgetval.php'); ?>
                        
                      </tr>
                      <?php } ?>
                
              </table>
              </td>
            </tr>
            
          <?php if($mobile!='true'){?>        
         </table>

          </td>
          <td valign="top">
            
          <table width="100%" cellpadding="2" border="0"
                 style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:white;">
            <tr>
              <td width="25%"></td>
            </tr>
          <?php } ?>

             <tr>
              <td width="1%" colspan="2">
                <table width="50%" cellpadding="2" border="0" caption=""
                       style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:gainsboro;">
                <tr>
                  <th colspan="2">RETENCION IVA</th>
                </tr>
                <tr>
                  <th align="center">BASE</th>
                  <th align="center">%</th>
                </tr>
                      <?php $crow=0; ?>
                      <?php //$res_comp=msql_query($sql_comp,$link_vcrx); ?>
                     <?php msql_data_seek($res_comp,0); ?>
                      <?php while($aso_comp=@msql_fetch_assoc($res_comp)){ ?>
                      <?php $crow++; ?>
                      <?php $duni=$aso_comp['item_duni']; ?>
                      <?php $cair=$aso_comp['cair']; ?>
                      <?php $base_iva=$aso_comp['base_iva']; ?>
                      <?php $porc_iva=$aso_comp['priva']; ?>
                     <?php if($base_iva!=0 or $porc_iva != 0){ ?>
                      <tr>
                        <?php $pidtxt='base_iva'.$crow; $pcaption='base iva'; $pdefa=@number_format($base_iva,2,'.',''); $psize=$xfontsize;  $pobj=1; $plist='dl_piva'; include('vrgetval.php'); ?>
                        <?php $pidtxt='porc_iva'.$crow; $pcaption='% iva'; $pdefa=number_format($porc_iva,2); $psize=$xfontsize; $pobj=1; include('vrgetval.php'); ?>
                        
                      </tr>
                      <?php } ?>
                      <?php } ?>
                
              </table>
              </td>
            </tr>
            
            <tr>
              <td></td>
              <td></td>
              <td>
              </td>
            </tr>
          </table>

         <?php if($mobile!='true'){?>        
         </td>
         <td valign="top">
         <?php } ?>

          <table width="100%" cellpadding="2" border="0"
                  style="font-size:<?php echo $xfontsize; ?>px;border-collapse:collapse; background-color:white;">
           <tr>
           <td style="display:;">
            <?php $pidtxt='consul'; $pcaption='Consultar '; $ponclick="info_docu('PANT1','RPXX');"; $psize=$xfontsize; include('vrbutton.php'); ?>
            </td>  
            <?php if($mobile!='true'){?>        
            </tr>
            <tr><td><br></td></tr>
            <tr>
            <?php } ?>              
           <td style="display:;">
            <?php $pidtxt='modif'; $pcaption='Modificar '; $ponclick="info_docu('MODI2','".$docu."','".$idnum."');"; $psize=$xfontsize; include('vrbutton.php'); ?>
            </td>  
            <?php if($mobile!='true'){?>        
            </tr>
            <tr><td><br></td></tr>
            <tr>
            <?php } ?>              
            <td>
            <?php $pidtxt='graba'; $pcaption='Grabar'; $ponclick='if(confirm("GRABAR RETENCIONES ?")){regi_rpro("GRABA");}info_docu("PANT1","RPXX");'; $psize=$xfontsize; include('vrbutton.php'); ?>
            </td>
            <?php if($mobile=='true'){?>        
              </tr>
              <tr><td><br></td></tr>
              <tr>
            <?php } ?>              
            </tr>
          </table>
            
           
           
         </td>
              	
				</tr>	
			</table>
		<?php } ?>
		</div>	
		<div id="pant1" style="border:solid 1px; overflow:auto; font-family:arial;">
		<div id="tpant1b"></div>
    </div>
		<hr>
		<div id="pant11" style="border:solid 1px; overflow:auto; font-family:arial; display:none;"></div>
		<div id="pant2"  style="border:solid 1px; overflow:auto; font-family:arial; display:none;
					<?php if($orient=='V'){echo'min-height:'.($hpant2-75).'px;';}else{echo'min-height:'.($hpant2-90).'px;';}  ?> 
					<?php if($orient=='V'){echo'height:'.($hpant2-75).'px;';}else{echo'height:'.($hpant2-30).'px;';}  ?> 
           "></div>
		<div id="pant9"></div>
<?php } ?>			

<?php if(mb_stristr('GRABA',$pant)){ ?>
	
	<?php $crow=0; ?>
	<?php $ccol=0; ?>
	<?php $tnumero=0; ?>
	<?php //echo '***'.date('H:i:s') .'<br>';?>
	<pant1>
	<?php	val_usercodigo(); ?>
	<?php 	
   if(mb_stristr('GRABA',$pant))  
   {
     $grabaok=1;
     if($docu=='' or $ptoe=='' or $numero=='' or $enti=='' )
     {$grabaok=0;die('<r><msgalert>DATOS NO VALIDOS</msgalert></r>');}     
        
     $base_rta=0;
     $base_iva=0;
     for($xidx=1;$xidx<=9;$xidx++)
     {
          $base_rta =$base_rta +${'base_rta'.$xidx};
          $base_iva =$base_iva +${'base_iva'.$xidx};
     }

      if($base_rta==0 and $base_iva==0)
      {$grabaok=0;die('<r><msgalert>BASES NO VALIDAS</msgalert></r>');}

     $sql_fact ='select * ';
     $sql_fact.=' from comp1 ';
     $sql_fact.=' where status!="O" ';
     $sql_fact.=' and status !="X" ';
     $sql_fact.=' and enti like "'.substr($enti,0,10).'%" ';
     $sql_fact.=' and docu = "'.$docu.'" ';
     $sql_fact.=' and ptoe = "'.$ptoe.'" ';
     $sql_fact.=' and numero = "'.$numero.'" ';
     $sql_fact.=' order by idnum1 desc';
     $res_fact=msql_query($sql_fact,$link_vcrx);
     $fechafac=@msql_result($res_fact,0,'fecha');

     $sql_rete ='select *, sum(brta) as sbrta, sum(biva) as sbiva ';
     $sql_rete.=' from retp12 ';
     $sql_rete.=' where status!="O" ';
     $sql_rete.=' and status !="X" ';
     $sql_rete.=' and enti like "'.substr($enti,0,10).'%" ';
     $sql_rete.=' and docum = "'.$docu.'" ';
     $sql_rete.=' and ptoem = "'.$ptoe.'" ';
     $sql_rete.=' and numer = "'.$numero.'" ';
  //    $sql_rete.=' and (cair like "3%" or cair like "4%")';
     $sql_rete.=' order by idnum1 desc';
  //    $sql_rete.=' group by idnum1';
     $res_rete=msql_query($sql_rete,$link_vcrx);

     $idnum=msql_result($res_rete,0,'idnum1');
     $sbrta=msql_result($res_rete,0,'sbrta');
     $sbiva=msql_result($res_rete,0,'sbiva');
     $stfin=msql_result($res_rete,0,'stfin');

     $fecharet5=date('Y-m-d',strtotime($fecha.' 5 days'));
     $fecharet30=date('Y-m-t',strtotime($fecha));
     if($fecharet5>$fecharet30){$fecharet5=$fecharet30;}

     if(days_diff($fecharet30,$fechaa)>30)
      {$grabaok=0;die('<r><msgalert>FECHA NO VALIDA</msgalert></r>');}

     if($fechaa>$fecharet30){$fecharet=$fecharet30;}
     else
     {
       if(days_diff($fechafac,$fechaa)<=5){$fecharet=$fechaa;}
       else{$fecharet=$fecharet5;}
     }

     if($grabaok==1)
     {
       if(!empty($idnum)) // ya tiene retencion
       {echo '<r><msgalert>'.'YA TIENE RETENCION :'.$idnum.'</msgalert></r>';}  
       
       if(empty($idnum))//no tiene retencion
       {
         $xdocu='RPRO';
//          $xptoe='001001';
         
         $sql_ptoe='select * from retp1 where status!="O" and status!="X" order by fecha desc, numero desc';
         $res_ptoe=msql_query($sql_ptoe,$link_vcrx);
         $xptoe=@msql_result($res_ptoe,0,'ptoe');

         if($xptoe==''){$xptoe='001101';}
         $xnumero=nume_docu($xdocu,$xptoe);
         
         $sql_iret ='insert into retp1(docu,ptoe,numero,enti,status,fecha,idate)';
         $sql_iret.='value("'.$xdocu.'","'.$xptoe.'","'.$xnumero.'","'.$enti.'","-","'.$fecharet.'",now())';
         $res_iret=msql_query($sql_iret,$link_vcrx);

         $sql_rete ='select * from retp1 where status!="X" and status!="O" and docu="'.$xdocu.'" and ptoe="'.$xptoe.'" and numero="'.$xnumero.'" order by idnum1 desc';
         $res_rete=msql_query($sql_rete,$link_vcrx);
         $idnum=msql_result($res_rete,0,'idnum1');
       }

       $iddet=0;
          
       // permite hacer retenciones libres (fp aun no registrada)
       if(empty($fechafac) and empty($fbaserta) and empty($fbaseiva)){$grabaokrta=1; $grabaokiva=1;}
          
       if(($fbaserta-$sbrta-$base_rta)>=(-0.01) and $base_rta>=(0.01)  and $cair!='-' and $stfin!=1)
       {$grabaokrta=1;}
          
       if(($fbaseiva-$sbiva-$base_iva)>=(-0.01) and $base_iva>=(0.01) and $stfin!=1)
       {$grabaokiva=1;}

// //        if($sbrta==0 and $base_rta!=0 and $cair!='-' and $stfin!=1)
//        if($grabaokrta==1)
//        {
//          $iddet=get_iddet($xdocu,$idnum);
//          $sql_iret ='insert into retp2(idnum2,iddet,cant,puni,cair,docum,ptoem,numer)';
//          $sql_iret.='value("'.$idnum.'","'.$iddet.'","'.$base_rta.'","'.$porc_rta.'","'.$cair.'","'.$docu.'","'.$ptoe.'","'.$numero.'")';
//          $res_iret=msql_query($sql_iret,$link_vcrx);
//        }

// //        if($sbiva==0 and $base_iva!=0 and $stfin!=1)
//        if($grabaokiva==1)
//        {
//          $iddet=get_iddet($xdocu,$idnum);
//          $sql_iret ='insert into retp2(idnum2,iddet,cant,puni,cair,docum,ptoem,numer)';
//          $sql_iret.='value("'.$idnum.'","'.$iddet.'","'.$base_iva.'","'.$porc_iva.'","-","'.$docu.'","'.$ptoe.'","'.$numero.'")';
//          $res_iret=msql_query($sql_iret,$link_vcrx);
//        }
          
     for($xidx=1;$xidx<=9;$xidx++)
     {
          $cair =${'cair'.$xidx};
          $base_rta =${'base_rta'.$xidx};
          $porc_rta =${'porc_rta'.$xidx};
          $base_iva =${'base_iva'.$xidx};
          $porc_iva =${'porc_iva'.$xidx};
          
          if($base_rta>0)
          {
              $iddet=get_iddet($xdocu,$idnum);
              $sql_iret ='insert into retp2(idnum2,iddet,cant,puni,cair,docum,ptoem,numer)';
              $sql_iret.='value("'.$idnum.'","'.$iddet.'","'.$base_rta.'","'.$porc_rta.'","'.$cair.'","'.$docu.'","'.$ptoe.'","'.$numero.'")';
              $res_iret=msql_query($sql_iret,$link_vcrx);
          }
          
          if($base_iva>0)
          {
              $iddet=get_iddet($xdocu,$idnum);
              $sql_iret ='insert into retp2(idnum2,iddet,cant,puni,cair,docum,ptoem,numer)';
              $sql_iret.='value("'.$idnum.'","'.$iddet.'","'.$base_iva.'","'.$porc_iva.'","-","'.$docu.'","'.$ptoe.'","'.$numero.'")';
              $res_iret=msql_query($sql_iret,$link_vcrx);
          }
     }
          
          
          
          
          
     }
   }   
  ?>

  </pant1>
	<?php //echo '***'.date('H:i:s') .'<br>';?>
				
<?php } ?>			
		
    
    
    
</resultado>

<?php $obstart = ob_get_clean(); ?>
<?php $obstart = alltrim($obstart) ?>
<?php echo $obstart;	?>

		