<?php
 
session_start();
include("DMMYSQLG.php");
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>
<body>
<form name="frm1" id="frm1" method="post" action="">
<?php
 
if($_POST['tnum']=="" and isset($_POST['tnum'])){
	echo '<input name="tver" type="hidden" value="1">';
}
if(isset($_REQUEST['tver'])){
	echo '<input name="tver" type="hidden" value="1">';
	$cambio = $_REQUEST['txt_p'] - $_REQUEST['ttot'];
	echo $_REQUEST['txt_p'] . '|' . $_REQUEST['ttot'];
	//@$sql=mysqli_query($con ,"update dgger_cajerosj set importe = '".$_REQUEST['tcan']."', imp = '".$_REQUEST['ttot']."', cambio = '$cambio' where n_rec = '".$_REQUEST['tger_cajerosj']."'");
}
if(isset($_POST['Submit4'])){// SI NUEVO REGISTRO
	//saca totales
	
	
	@$sql=mysqli_query($con ,"INSERT INTO dgger_cajerosj(vendedor, tip_caj, fec_rec, tip_mov, num_cli, concepto, importe,  pedido, acc_fec, acc_hor, usuario, tip_afe) values ('".$_POST['ver11']."','".$_POST['ver1']."', '".date("Y-m-d")."', '1', '".$_POST['ver33']."', '$ccon', '".$_POST['txt_to']."', '".$_POST['tnum']."', '".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."', '".$_REQUEST['select3']."')");
	$nrc = mysqli_insert_id($con);
	//echo 'id2:'.$nrc.'<br>';
	//@$buus=mysqli_query($con ,"update dgger_cajeroso set rec_caj = '$nrc' where n_reg = '$nn'");
	echo '<input name="tger_cajerosj" type="text" value="'.$nrc.'">';
	// SI NUEVO REGISTRO
	//SACA EL TOTAL A COBRAR
	$ccon = strtoupper($_REQUEST['tcon']);

	$sql = mysqli_query($con ,"select * from dgpemema where fol_ven = '".$_POST['tnum']."'");
	$rr = mysqli_fetch_array($sql);
	$total = $rr['tot_ven'] - $rr['imp_pag'];

	//echo 'Saldo:'.$saldo.'<br>';
	$sql = mysqli_query($con ,"insert into dgger_cajeroso (rec_caj, fec_doc ,num_doc ,codigo ,fec_ven ,import, saldo ,acc_fec ,acc_hor ,usuario) values ('$nrc', '".date("Y-m-d")."', '".$_POST['tnum']."', '".$rr['cod_cli']."', '".$rr['fec_ven']."', '".$rr['tot_ven']."', '$saldo', '".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."')");
	$nn = mysqli_insert_id($con);
	//echo 'id1:'.$nn.'<br>';
	//echo 'total:'.$total.'<br>';
}

if(isset($_REQUEST['Submit'])){//si agregar pago
echo '<input name="tger_cajerosj" type="text" value="'.$_REQUEST['tger_cajerosj'].'">';
	//revisar si hay datos para hacer la operacion
	if(!is_numeric($_POST['tcan'])){
		echo '<div align="center" class="thcon">&iexcl;&iexcl;&iexcl;Corriga la cantidad!!!</div>';
		$errx = 1;
	}else{//SI CASO CONTRARIO ES POR QUE SI ES CANTIDAD
		//$tpp = explode("|",$_POST['ver11']);
		$a = 1;
		$dif1 = 0;
		$tp = 0;

		//echo $a.'<br>';
		if($a == 1){
			//saco el importe a pagar		
			$sql=mysqli_query($con ,"SELECT * FROM dgger_cajerosj where n_rec = '".$_REQUEST['tger_cajerosj']."'");
			$bus = mysqli_fetch_array($sql);
			$imp = $bus['importe'];//importe por pagar
			

			//calcular la cantidad pagada
			$sqlf=mysqli_query($con ,"SELECT * FROM dgger_cajerosd where n_rec = '".$_REQUEST['tger_cajerosj']."'");
			$sc = 0;
			while($buse = mysqli_fetch_array($sqlf)){
				$sc += $buse['importe'];
			}
			
			//si el importe es mayor al ya pagado 
			if($imp == 0){
				$imp = 0;
			}
			echo $_REQUEST['tger_cajerosj'].'<br>';
			echo 'val1: '.number_format($imp, 10, '.', ',').'<br>';
			echo 'val2: '.number_format($sc, 10, '.', ',').'<br>';
			if($sc < $imp){
				$cdif = $imp - $sc;
				//echo $cdif.'<br>';
				$dif = $cdif - $_REQUEST['tcan'];
				//echo $dif.'<br>';
				 if ($dif < 0){
					$sa = $cdif;
					$cam = $_REQUEST['tcan']-$cdif;
				}else{
					$sa = $_REQUEST['tcan'];
					$cam = 0;
				}	
				//echo $sa.'<br>';
				//echo $cam.'<br>';
				//agrego datos a detalle
				@$sql=mysqli_query($con ,"INSERT INTO dgger_cajerosd (n_rec, tip_pag, importe, ref, aut, acc_fec, acc_hor, usuario, tdp) values ('".$_REQUEST['tger_cajerosj']."', '$tpp[0]','$sa','".$_REQUEST['tref']."', '".$_REQUEST['taut']."', '".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."', '$tp')");
				$ultimo_id = mysqli_insert_id($con);
				//calcular la cantidad pagada
				$sql=mysqli_query($con ,"SELECT * FROM dgger_cajerosd where n_rec = '".$_REQUEST['tger_cajerosj']."'");
				$sc = 0;
				while($buse = mysqli_fetch_array($sql)){
					$sc += $buse['importe'];
				}
				//echo $dif1.'<br>';
		    	@$frg=mysqli_query($con ,"update dgger_cajerosj set imp_pag='$sc', cambio='$cam' where n_rec = '".$_REQUEST['tger_cajerosj']."'");
			}else{
				//echo 'VALOR 1'.$sc.'<br>';
				//echo 'VALOR 2'.$imp.'<br>';
				echo '<div align="center" class="thcon">Recibo Pagado totalmente!!!</div>';
			}
		}
	}
}

// --> Si click en eliminar un registro
if(isset($_REQUEST['Submit2']) and isset($_REQUEST['bot'])){//si agregar pago
	//buscar el numero del recibo que sirvio para pagar este registro
	$sql=mysqli_query($con ,"SELECT * FROM dgger_cajerosd where num_pag = '".$_REQUEST['bot']."'");
	$bus = mysqli_fetch_array($sql);
	//echo $bus['tdp'].'<br>';
	if($bus['tdp'] <> 0){
		//saco el saldo aplicado
		$sqld=mysqli_query($con ,"SELECT * FROM dgger_cajerosj where n_rec = '".$bus['tdp']."'");
		$busd = mysqli_fetch_array($sqld);
		//le resto la cantidad 
		//echo $busd['sal_apl'].'<br>';
		//echo $bus['tdp'].'<br>';
		$ncat = $busd['sal_apl']-$bus['importe'];
		//echo $ncat.'<br>';
		//guardo la nueva cantidad
		$sqlw=mysqli_query($con ,"update dgger_cajerosj set sal_apl = $ncat where n_rec = '".$bus['tdp']."'");
	}
	@$sql=mysqli_query($con ,"delete from dgger_cajerosd where num_pag = '".$_REQUEST['bot']."'");		
	//saco el importe a pagar		
	$sql=mysqli_query($con ,"SELECT * FROM dgger_cajerosj where n_rec = '".$_REQUEST['tger_cajerosj']."'");
	$bus = mysqli_fetch_array($sql);
	$imp = $bus['importe'];//importe por pagar
	//calcular la cantidad pagada
	$sql=mysqli_query($con ,"SELECT * FROM dgger_cajerosd where n_rec = '".$_REQUEST['tger_cajerosj']."'");
	$sc = 0;
	while($buse = mysqli_fetch_array($sql)){
		$sc += $buse['importe'];
	}
	//-->fin del saldo aplicado
	//echo $dif1.'<br>';
	@$frg=mysqli_query($con ,"update dgger_cajerosj set imp_pag='$sc', cambio='$cam' where n_rec = '".$_REQUEST['tger_cajerosj']."'"); 
}	
//--> Si termina el pago
if(isset($_REQUEST['Submit3']) or isset($_REQUEST['Submit5'])){//si agregar pago
	if($_REQUEST['ttot']<=$_REQUEST['txt_p'] or isset($_REQUEST['Submit5'])){
		
		echo '<meta content="0; url=DG24000_AG3.php?n_r='.$_REQUEST['tger_cajerosj'].'&ipp='.$_REQUEST['ttot'].'&ipa='.$_REQUEST['txt_p'].'&tve='.$_REQUEST['tver'].'&tmo='.$_REQUEST['tmov'].'" HTTP-EQUIV="REFRESH">';
		exit();
	}else{
	   echo '<div align="center" class="thcon">&iexcl;&iexcl;&iexcl;No esta total mente saldado el pago no se puede terminar?!!!</div>';
	}
}	
?>
<table  align="center">
      <tr>
        <th colspan="7" class="titulo">RECIBO DE CAJA   </th>
      </tr>
      <tr>
        <td >Fecha</td>
        <td colspan="4"><span class="inputcodigo"><?php
 echo date("j/m/Y") ?></span></td>
        <td>Recibo de caja: </td>
        <td class="estilocelda"><span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['tped'])){
			echo '<input name="tped" type="hidden" value="'.$_REQUEST['tped'].'">';
			$va = $_REQUEST['tped'];
		}else{
			echo '<input name="tped" type="hidden" value="'.$_POST['tnum'].'">';
			$va = $_POST['tnum'];
		}
		echo $va 
		?>
        </span></td>
      </tr>
      <tr>
        <td>Vendedor</td>
        <td colspan="4"><span class="inputcodigo">
		<?php
 
		if(isset($_REQUEST['tven'])){
			echo '<input name="tven" type="text" value="'.$_REQUEST['tven'].'">';
			$va = $_REQUEST['tven'];
		}else{
			echo '<input name="tven" type="text" value="'.$_POST['ver11'].'">';
			$va = $_POST['ver11'];
		}
		@$resultado=mysqli_query($con ,"select * from dgcatven where cod_ven = '$va'");
		@$registro=mysqli_fetch_array($resultado);
		echo $registro['nom_ven'].' '.$registro['ap_pat_ven'].' '.$registro['ap_mat_ven'];
		?>
		</span></td>
        <td>Tipo de Movimiento: </td>
        <td><span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['tmov'])){
			echo '<input name="tmov" type="hidden" value="'.$_REQUEST['tmov'].'"/>';
			$va = $_REQUEST['tmov'];
		}else{
			echo '<input name="tmov" type="hidden" value="1"/>';
			$va = 1;
		}
		if($va==1){
			echo 'COBRO DE PEDIDOS';
		}elseif($va==2){
			echo 'COBRO DE FACTURAS';
		}elseif($va==3){
			echo 'ANTICIPOS';
		}elseif($va==4){
			echo 'DEVOLUCION DE VENTAS';
		}elseif($va==5){
			echo 'OTROS';
		}else{
			echo '&nbsp;';
		}
		?>
        </span></td>
      </tr>
      <tr>
        <td>Importe </td>
        <td><span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['timp'])){
			echo '<input name="timp" type="hidden" value="'.$_REQUEST['timp'].'">';
			$va = $_REQUEST['timp'];
		}else{
			echo '<input name="timp" type="hidden" value="'.$_REQUEST['txt_to'].'">';
			$va = $_REQUEST['txt_to'];
			
		}
		echo '$  ' .number_format($va,2,'.',',');
		?>
        </span></td>
        <td>Descuento:</td>
        <td colspan="2"><span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['tdes'])){
			echo '<input name="tdes" type="hidden" value="'.$_REQUEST['tdes'].'">';
			$va = $_REQUEST['tdes'];
		}else{
			echo '<input name="tdes" type="hidden" value="'.$_REQUEST['des'].'">';
			$va = $_REQUEST['des'];
		}
		echo '$  ' .number_format($va,2,'.',',');
		?>
        </span></td>
        <td>Total</td>
        <td><span class="inputcodigo">
<?php
 
	if(isset($_REQUEST['ttot'])){
		echo '<input name="ttot" type="hidden" value="'.$_REQUEST['ttot'].'">';
		$va = $_REQUEST['ttot'];
	}else{
		echo '<input name="ttot" type="hidden" value="'.$total.'">';
		$va = $total;
	}
	echo '$  ' .number_format($va,2,'.',',');
?>
        </span></td>
      </tr>
      
      <tr>
        <td>Cliente </td>
        <td colspan="4"><?php
 
		if(isset($_REQUEST['tcli'])){
			echo '<input name="tcli" type="hidden" value="'.$_REQUEST['tcli'].'">';
			$va = $_REQUEST['tcli'];
		}else{
			echo '<input name="tcli" type="hidden" value="'.$_POST['tnum'].'">';
			$va = $_POST['tnum'];
		}
		@$res=mysqli_query($con ,"select * from dgpemema where fol_ven = '$va'");
		$rr=mysqli_fetch_array($res);
		@$rrr=mysqli_query($con ,"select * from dgcatcli where cod_cli = '".$rr['cod_cli']."'");
		$r=mysqli_fetch_array($rrr);
		echo $r['nom_cli'] . ' ' . $r['ap_pat_cli'] . ' ' . $r['ap_mat_cli'];
	?></td>
        <td>Pedido:</td>
        <td><span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['tped'])){
			echo '<input name="tped" type="hidden" value="'.$_REQUEST['tped'].'">';
			$va = $_REQUEST['tped'];
		}else{
			echo '<input name="tped" type="hidden" value="'.$_POST['tnum'].'">';
			$va = $_POST['tnum'];
		}
		echo $va 
		?>
        </span></td>
      </tr>
    <tr>
      <td>Concepto:</td>
      <td colspan="6"><?php
 
		if(isset($_REQUEST['tcon'])){
			echo '<input name="tcon" type="hidden" value="'.$_REQUEST['tcon'].'">';
			echo $ccon;
		}else{
			echo '<input name="tcon" type="hidden" value="'.$ccon.'">';
			echo $ccon;
		}
	?>&nbsp;</td>
    </tr>
      
      
      <tr>
        <td colspan="7" class="tabagre">FORMA DE PAGO </td>
      </tr>
      <tr>
        <td colspan="2">Tipo de pago:<select name="select" tabindex="2" onChange="hab_camp(this.value)">
            <?php

@$resultado=mysqli_query($con ,"select * from dgtippag");
while($registro=mysqli_fetch_array($resultado)){
	if($_POST['select']==$registro['tip_pag']){
	echo '<option value="'.$registro['tip_pag'].'|'.$registro['tip_mv_pag'].'|'.$registro['tip_aplica'].'" selected>'.$registro['desc_pag']. '</option>';
	}else{
	echo '<option value="'.$registro['tip_pag'].'|'.$registro['tip_mv_pag'].'|'.$registro['tip_aplica'].'">'.$registro['desc_pag']. '</option>';
	}
	
}			
?></select></td>
        <td>Pago: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$</td>
        <td >
		<input name="tcan" type="text" id="tcan" size="10" maxlength="10"
		<?php
 
		if(isset($_REQUEST['tcan']) && $errx == 1){
			if(is_numeric($_REQUEST['tcan'])){
				echo 'value="'.$_REQUEST['tcan'].'"';
			}
		}

		?>
		></td>
        <td >Referencia:</td>
        <td><input name="tref" type="text" id="tref" size="15"
		<?php
 
		if(isset($_REQUEST['tref']) && $errx == 1){
			echo 'value="'.$_REQUEST['tref'].'"';
		}
		?>
		></td>
        <td>Autorizacion:
        <input name="taut" type="text" id="taut" size="10" maxlength="30"
		<?php
 
		if(isset($_REQUEST['taut']) && $errx == 1){
			echo 'value="'.$_REQUEST['taut'].'"';
		}
		?>
		>
        <input name="taut1" type="hidden" id="taut1"></td>
      </tr>
      <tr>
        <td colspan="7"><div align="center">
          <input type="submit" name="Submit" value="Agregar" onClick="openpage('DG24000_AG2.php','_self')">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" name="Submit3" value="Terminar" onClick="openpage('DG24000_AG2.php','_self')">
        </div></td>
      </tr>
  </table>
    <table border="0" align="center">
	<!--Encabezado-->
     
      <tr>
        <td colspan="6" class="inputcodigo"><div align="center" class="mentab">DETALLE DE PAGO</div></td>
      </tr> 
	  <tr>
        <td colspan="6" class="inputcodigo"><input type="submit" name="Submit2" value="Eliminar" onClick="openpage('DG24000_AG2.php','_self')"></td>
      </tr>
	   <tr>
	   	 <td class="inputcodigo"><div align="center">SEL</div></td>
	     <td class="inputcodigo"><div align="center">TIPO DE PAGO</div></td>
	     <td class="inputcodigo"><div align="center">IMPORTE</div></td>
	     <td class="inputcodigo"><div align="center">REFERENCIA DE PAGO</div></td>
	     <td class="inputcodigo"><div align="center">AUTORIZACION</div></td>
	     <td class="inputcodigo"><div align="center">RESTA</div></td>
      </tr> 
	  <!--Desarrollo de pago-->
<?php
 
@$sql = mysqli_query($con ,"select * from dgger_cajerosd where n_rec = '".$_REQUEST['tger_cajerosj']."'");
while(@$row = mysqli_fetch_array($sql)){
	echo "<tr>";
		 echo '<td><input name="bot" type="radio" value="'.$row['num_pag'].'"</td>';
		 @$sqlx = mysqli_query($con ,"select * from dgtippag where tip_pag = '".$row['tip_pag']."'");
		 @$rowx = mysqli_fetch_array($sqlx);
		 echo '<td><div align="center">' . $rowx['desc_pag'] . '&nbsp;</div></td>';
		 echo '<td><div align="center">' . $row['importe'] . '&nbsp;</div></td>';
		 $sum +=  $row['importe'];
		 echo '<td><div align="center">' . $row['ref'] . '&nbsp;</div></td>';
		 echo '<td><div align="center">' . $row['aut'] . '&nbsp;</div></td>';
		 $abono += $row['importe'] ; 
		 $res = $imp-$abono;
		 if($res>=0){
		 	 echo '<td> $' .number_format($res,2,'.',',') ."&nbsp;</td>";
		 }else{
		 	echo '<td> $' .number_format('0',2,'.',',') . "&nbsp;</td>";
		 }
	echo "</tr>";
}
?>
	  <!--Totales-->
	   <tr>
	     <td colspan="3">&nbsp;</td>
	     <td>&nbsp;</td>
	     <td class="mentab">PAGADO</td>
	     <td class="mentab"><?php
 
	echo '<input name="txt_p" type="hidden" value="'.$sum.'">';
	echo '$  ' . number_format($sum, 2, '.', ',');
	?>		 </td>
      </tr>
	   <tr>
	     <td colspan="3">&nbsp;</td>
	     <td>&nbsp;</td>
	     <td class="mentab">RESTA</td>
	     <td class="mentab"><?php
 
	echo '<input name="txt_r" type="hidden" value="'.$dif1.'">';
	echo '$  ' . number_format($dif1, 2, '.', ',');
	?>&nbsp;</td>
      </tr>
	   <tr>
        <td colspan="3">&nbsp;</td>
        <td>&nbsp;</td>
        <td class="mentab">CAMBIO</td>
        <td class="mentab">
		<?php
 
	echo '<input name="txt_s" type="hidden" value="'.$s.'">';
	echo '$ ' . number_format($cam, 2, '.', ',');
	
	?>
	
         &nbsp;</td>
      </tr>
  </table>
    <p>&nbsp;</p>
</form>
</blockquote>
</body>
</html>
