<?php
$con=mysqli_connect("localhost","root","castillejos");
mysqli_select_db("azteca_reyes");	
?>
<html>
<head>

</head>
<body style="font-family:'Courier New', Courier, monospace">
<form name="form1" method="post" action="">
  <label>
  <span class="style12">
  <select name="setipven" id="setipven">
    <?php
		  //	echo "<option value=\"TODOS\" selected>TODOS</option>";
		  	(!isset($_POST['setipven']))?($selTipVen=$_SESSION['tipven']):($selTipVen=$_POST['setipven']);
		  	$sq_tipven=mysqli_query($con ,"SELECT tip_ven,des_tipven FROM dgtipven order by des_tipven");
			while($dt_tipven=mysqli_fetch_array($sq_tipven)){
				if($selTipVen==$dt_tipven['tip_ven'])
					echo "<option value=\"$dt_tipven['tip_ven']\" selected>$dt_tipven['des_tipven']</option>";
				else
					echo "<option value=\"$dt_tipven['tip_ven']\">$dt_tipven['des_tipven']</option>";
			}
		  ?>
  </select>
  </span>
  <input name="fec_aju" type="text" id="fec_aju" size="10" value="<? if(!isset($_POST['fec_aju']))  echo date('d/m/Y'); else echo $_POST['fec_aju'];?>">
  <input name="num_fac" type="text" id="num_fac" size="10">
  <span class="style12">  </span>
  <input name="ent_est" type="submit" id="ent_est" value="Ajustar Pagos">
  </label>
</form>
<?php

if(isset($_POST['ent_est'])){
	$tipVen=$_POST['setipven'];
	$auxFecAju=explode("/",$_POST['fec_aju']);
	$numFacAju=$_POST['num_fac'];
	$fecAju=$auxFecAju[2].'-'.$auxFecAju[1].'-'.$auxFecAju[0];
	$dt_tipven=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgtipven WHERE tip_ven='$tipVen'"));
	$conODBC_MAIN=odbc_connect($dt_tipven['bas_nrlab'],"","");
	$sq_pemema=mysqli_query($con ,"SELECT * FROM dgpemema WHERE tip_ven='".$dt_tipven['tip_ven']."' AND num_fac='$numFacAju' order by dgpemema.con_ven");
	while($dt_pemema=mysqli_fetch_array($sq_pemema)){
		$dt_ger_cajerosfSum=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(imp_pag) as impPag FROM dgger_cajerosf WHERE fol_ven='".$dt_pemema['fol_ven']."' "));
		//echo $dt_ger_cajerosfSum['impPag']."<br>";
		if($dt_pemema['tot_ven']<=$dt_ger_cajerosfSum['impPag']){//Si Pagado
			$dt_ger_cajerosjLiq=mysqli_fetch_array(mysqli_query($con ,"SELECT dgger_cajerosj.fec_rec as fecPag FROM dgger_cajerosj INNER JOIN dgger_cajerosf ON dgger_cajerosf.n_rec=dgger_cajerosj.n_rec WHERE dgger_cajerosf.fol_ven='".$dt_pemema['fol_ven']."' order by dgger_cajerosj.fec_rec desc limit 1"));
			$esPagPem="S";
			$fecLiqPem=$dt_ger_cajerosjLiq['fecPag'];
			$auxFecLiq=explode("-",$dt_ger_cajerosjLiq['fecPag']);
			$ordLiq="L";
			$fecLiq=$auxFecLiq[2].'/'.$auxFecLiq[1].'/'.$auxFecLiq[0];
		}else{//No Pagado
			$esPagPem="N";		
			$fecLiqPem='0000-00-00';
			$ordLiq="P";
			$fecLiq='  /  /    ';
		}
		$rsPem=mysqli_query($con ,"UPDATE dgpemema SET imp_pag='".$dt_ger_cajerosfSum['impPag']."', pagado='$esPagPem',fec_liq='$fecLiqPem' WHERE fol_ven='".$dt_pemema['fol_ven']."'");
		$dt_demograf = odbcFetchArray(odbc_exec($conODBC_MAIN,$sqDem="
			SELECT * FROM demograf WHERE no_consec=$dt_pemema['con_ven']
		"));
		$rsPag = odbc_exec($conODBC_MAIN,$sqDem="
			delete from pagpac where no_consec=$dt_demograf['no_consec']
		");
		//echo $sqDem."<br>";
		echo "AJUSTANDO $dt_pemema['tip_ven']::$dt_pemema['con_ven']-->Fecha:$dt_pemema['fec_ven']-->$dt_demograf['clinica_an']<br>";
		$sq_ger_cajerosf=mysqli_query($con ,"SELECT dgger_cajerosj.*,dgger_cajerosf.fol_ven,dgger_cajerosf.imp_pag FROM dgger_cajerosj INNER JOIN dgger_cajerosf ON dgger_cajerosf.n_rec=dgger_cajerosj.n_rec WHERE dgger_cajerosf.fol_ven='".$dt_pemema['fol_ven']."' order by dgger_cajerosj.fec_rec asc");
		while($dt_ger_cajerosf=mysqli_fetch_array($sq_ger_cajerosf)){
			$impPag=$dt_ger_cajerosf['imp_pag'];
			$fecPag=$dt_ger_cajerosf['fec_rec'];
			$horPag=$dt_ger_cajerosf['acc_hor'];
			
			$obsPago="";
			$tipAfe=$dt_ger_cajerosf['tip_afe'];
			$activo=1;
			$status=1;
			$tipo='PACIE';
			
			$historico=0;
			$envio=0;
			$fecEnv="$fecPag $horPag";
			$enviado=0;
			$obsPago="POR PAGO DE FACTURA";
			$fecAlt=$dt_pemema['fec_ven'];
			
			/**********AGREGAR PAGO A NRLAB*********/
			$sq="INSERT INTO Pagpac (Clave,No_consec,fecha, Estampa, Persona, Mpago, Cantidad, Obspago, Activo, Status, Tipo, Fealta, Historico,Localidad,Envio,Fenvio,Enviado) VALUES('".$dt_demograf['clave']."', $dt_pemema['con_ven'], {d '$fecPag'}, {ts '$fecPag $horPag'}, '".$dt_demograf['usuario']."', 'E', $impPag, '$obsPago', $activo, '$status','$tipo',{d '$fecAlt'}, $historico, '".$dt_demograf['clinica_an']."', $envio,{ts '$fecEnv'},$enviado)";
			//echo $sq."<br>";
			$rs_detOrd = odbc_exec($conODBC_MAIN,$sq);
			/***************************************/
		}
		/**************ACTUALIZAR DEMOGRAF***************/
		$dt_pagpacSum=odbcFetchArray(odbc_exec($conODBC_MAIN,"SELECT SUM(cantidad) as totPag From pagpac WHERE no_consec=$dt_pemema['con_ven'] "));
		if($dt_pagpacSum['totpag']=="")$dt_pagpacSum['totpag']=0;
		$rs_odbc=odbc_exec($conODBC_MAIN,$sqa="UPDATE demograf SET anticipo=$dt_pagpacSum['totpag'] WHERE no_consec=$dt_pemema['con_ven']");
		$rs_odbc=odbc_exec($conODBC_MAIN,$squ="UPDATE rep_gral SET exp1='$ordLiq', fecha2=CTOD('$fecLiq') WHERE no_consec=$dt_pemema['con_ven']");					
		/************************************************/
		//echo $sqa."<br>";
		//echo $squ."<br>";
	}
}	
	
	
function odbcFetchArray(&$res){    
    $i = 0;
    $j = 0;
	
    $toReturn = "";
    if(odbc_fetch_row($res)){
        for ($j = 1; $j <= odbc_num_fields($res); $j++){        
             $field_name = odbc_field_name($res, $j);
             $ar[$field_name] = trim(odbc_result($res, $field_name));
        }
   		return $ar;
	}else{
		return false;    
	}
}
	
?>
</body>
</html>
