<?php
 
session_start();
include("DMMYSQLG.php");
?>
<html>
<link rel="stylesheet" href="css/estilo1.csss" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>
<script language="javascript">
function multiTex(){
	var objTex1=document.getElementById('tcan'),
		objTex2=document.getElementById('tpre'),
		objTex9=document.getElementById('total');
	objTex9.value=(parseFloat(objTex1.value)*parseFloat(objTex2.value)).toFixed(2);
}
</script>
<script language="javascript">
function descTex(){
	var objTex1=document.getElementById('total'),
		objTex2=document.getElementById('des'),
		objTex9=document.getElementById('por_des');
	objTex9.value=((parseFloat(objTex1.value)/100)*parseFloat(objTex2.value)).toFixed(2);
}
function ivaTex(){
	var objTex1=document.getElementById('total'),
		objTex2=document.getElementById('iva'),
		objTex9=document.getElementById('totaliva');
	objTex9.value=((parseFloat(objTex1.value)/100)*parseFloat(objTex2.value)).toFixed(2);
}
function superTex(){
	var objTex1=document.getElementById('total'),
		objTex2=document.getElementById('por_des'),
		objTex3=document.getElementById('totaliva'),
		objTex9=document.getElementById('supertotal');
	objTex9.value=(parseFloat(objTex1.value)-parseFloat(objTex2.value)+parseFloat(objTex3.value)).toFixed(2);
}
</script>

<body>
<?php

$ayuda = 'DI3200A_AG1.php';
include("DYAYUDA.php");
?>
<?php

$fec=$_POST['tfec'];
include("DF20000.php");
//buscar almacen
if (isset($_POST['Submit22']))
{
	$sql="INSERT INTO dgreqdev (tip_ven,folio_control,fec_ven, acc_fec, acc_hor, usuario, trefe) VALUES ('".$_POST['tip_ven']."','".$_POST['folio_control']."', '".$f_m."' ,'".date('Y-m-d')."', '".date('H:i:s')."','".$_SESSION['num_usu']."', '".$_POST['trefe']."')";
	if (!mysqli_query($con ,$sql)){
		die('Error: ' . mysqli_error($con));
		exit();
	}
	$ultimo_id = mysqli_insert_id($con);
}
?>
<form name="frm1" id="frm1" method="post" action="">
<input name="ver" type="hidden" value="1">
<input name="s1" type="hidden" <?php
 echo 'value="'.$_REQUEST['s1'].'"' ?>>
<input name="s2" type="hidden" <?php
 echo 'value="'.$_REQUEST['s2'].'"' ?>>
<input name="s3" type="hidden" <?php
 echo 'value="'.$_REQUEST['s3'].'"' ?>>

<input name="controlSerie" type="hidden" <?php
 echo 'value="S"'; ?>>
<input name="afe_cos" type="hidden"
<?php
 
if (isset($_REQUEST['afe_cos'])){
	echo 'value="'.$_REQUEST['afe_cos'].'"';
}else{
	@$resultado=mysqli_query($con ,"select * from fproveedores where nparte1 = '".$_REQUEST['tip_ven']."'");
	$rr=mysqli_fetch_array($resultado);
	echo 'value="'.$rr['afe_cos'].'"';
}

?>
>
  <table   align="center">
<tr>
<td colspan="4" class="men">
<?php
  	
	if(isset($_POST['Submit22'])){
		@$ultimo_id = mysqli_insert_id($con);
		echo '<input name="tnum" type="hidden" value="'.$ultimo_id.'">';
		echo 'FOLIO DE ORDEN:'.$ultimo_id; 
		@$result = mysqli_query($con ,"SELECT * FROM dgreqdev WHERE fol_ven = '$ultimo_id'");
	}else{
		echo '<input name="tnum" type="hidden" value="'.$_POST['tnum'].'">';
		echo 'FOLIO DE ORDEN:'.$_POST['tnum']; 
		@$result = mysqli_query($con ,"SELECT * FROM dgreqdev WHERE fol_ven = '".$_POST['tnum']."'");
	}	
	@$row = mysqli_fetch_array($result);
	$op1=$row["tip_ven"];
	$op2=$row["fec_ven"];
	$op3=$row["cod_cli"];
	$op4=$row["con_cre"];
	$op5=$row["alm_gen"];
	$op6=$row["sub_ven"];
	$op7=$row["iva_ven"];
	$op8=$row["tot_ven"];
	$op9=$row["iva_des"];
	$op10=$row["por_des"];
	$op11=$row["cod_ven"];
	$op12=$row["des_ven"];
	$op13=$row["nom_cli_alt"];
	$op14=$row["folio_control"];
	$op15=$row["trefe"];
?>  </td>
  </tr>
  <tr>
    <th colspan="4" class="titulo">DATOS DE ENTRADA. </th>
  </tr>
  <tr>
    <td>ORDEN DE COMPRA. </td>
    <td width="27"><?php

		
		if(isset($_POST['ver5']))
		{
			@$resul=mysqli_query($con ,"select * from dgreqdev where folio_control = '".$_POST['ver5']."'");
			@$regis=mysqli_fetch_array($resul);
			echo '<input name="ver5" type="hidden" value="'.$regis['folio_control'].'">';
			echo $regis['folio_control'];
		}else{
			@$resul=mysqli_query($con ,"select * from dgreqdev where folio_control = '$op14'");
			@$regis=mysqli_fetch_array($resul);
			echo '<input name="ver5" type="hidden" value="'.$regis['folio_control'].'">';
			echo $regis['folio_control'];
		}	
	?></td>
    <td width="39">FECHA.      </td>
    <td width="17"><?php

		if(isset($_POST['ver2'])){
			echo '<input name="ver2" type="hidden" value="'.$_POST['ver2'].'">';
			echo $_POST['ver2'];
		}else{
			$fec1=$op2;
			include("DF20000.php");
			echo '<input name="ver2" type="hidden" value="'.$f_f.'">';
			echo $f_f;
		}
	?></td>
  </tr>
  
  <tr>
    <td width="107">PROVEEDOR.</td>
    <td><?php
 
		//echo $op1.'<br>';
		
		if(isset($_POST['ver1']))
		{
			@$result=mysqli_query($con ,"select * from fproveedores where nparte1 = '".$_POST['ver1']."'");
			@$catch=mysqli_fetch_array($result);
			echo '<input name="ver1" type="hidden" value="'.$catch['nparte1'].'">';
			echo $catch['np_desc1'];
		}else{
			@$result=mysqli_query($con ,"select * from fproveedores where nparte1 = '$op1'");
			@$catch=mysqli_fetch_array($result);
			echo '<input name="ver1" type="hidden" value="'.$catch['nparte1'].'">';
			echo $catch['np_desc1'];
		}	
	?></td>
    <td>FACTURA.</td>
    <td><?php
 
		
		if(isset($_POST['ver6']))
		{
			@$res=mysqli_query($con ,"select * from dgreqdev where trefe = '".$_POST['ver6']."'");
			@$reg=mysqli_fetch_array($res);
			echo '<input name="ver6" type="hidden" value="'.$reg['trefe'].'">';
			echo $reg['trefe'];
		}else{
			@$res=mysqli_query($con ,"select * from dgreqdev where trefe = '$op15'");
			@$reg=mysqli_fetch_array($res);
			echo '<input name="ver6" type="hidden" value="'.$reg['trefe'].'">';
			echo $reg['trefe'];
		}	
	?></td>
  </tr>
</table>
 <?php

/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////si eprecionan el boton de eliminar/
/////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['Submit2'])){
	include('AL21000_AC7.php');	
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////Si se agrega un producto para la venta
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['boton_car'])){
	include('AL21000_AC6.php');
}

?>

    <table  align="center">
  <tr>
    <th colspan="6" class=\"titulo\">PRODUCTOS DE ENTRADA. </th>
    </tr>
  <tr>
    <td colspan="2" nowrap> 
        <input name="tbus" type="text" id="tbus"
		<?php
 
		echo 'value ="'.$_POST['tbus'].'"';
		?>
		>
      <input type="submit" name="Submit" value="Buscar" onClick="cargapag('AL21000_AC.php','_self')"></td>
    <td colspan="4" nowrap><input name="r1" type="radio" value="clave" checked 
	<?php
 
	if($_POST['r1'] == "clave" ){
		echo ' checked';
	}
	?>
	>
CLAVE.
  <input name="r1" type="radio" value="des"
  <?php
 
	if($_POST['r1'] == "des"){
		echo ' checked';
	}
	?>
  >
DESCRIPCION.</td>
    <?php
 
@$resultado2=mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '".$_POST['producto']."'");
$regist2=mysqli_fetch_array($resultado2);
/* 	$a1 = $registro2['nparte1']; 
	$a2 = $registro2['np_desc1']; 
	$a3 = $registro2['exi_min']; 
	$a4 = $registro2['exi_max']; 
	$a5 = $registro2['linea']; 
	$a6 = $registro2['gpo_basico']; 
	$a7 = $registro2['cve_marca']; */
	$pp8 = $regist2['t3']; 
	$pp9 = $regist2['lim_vta2']; 
	$pp10 = $regist2['lim_vta3'];  
	$pp1 = $regist2['lim_1']; 
	$pp2 = $regist2['lim_2']; 
	$pp3 = $regist2['lim_3']; 
	/* $a14 = $registro2['tip_mon']; 
	 
	$a16 = $registro2['uc_num_doc']; 
	$a17 = $registro2['uc_pre_com']; 
	$a18 = $registro2['cos_prom']; 
	$a19 = $registro2['cos_prom_ant']; 
	$a20 = $registro2['exi_fis']; 
	$a21 = $registro2['t5']; 
	$a22 = $registro2['fec_bck_ord']; 
	$a23 = $registro2['bck_ord']; 
	$a24 = $registro2['cve_uni_com']; */
	$a25 = $regist2['ven_esp1']; 
/* 	$a26 = $registro2['cve_uni_vta']; 
	$a27 = $registro2['cod_pro'];  */ 
?>
    </tr>
  
  <tr>
    <td colspan="3" nowrap>PRODUCTO.
      <select name="producto" onChange="javascript: document.getElementById('frm1').submit()">
	  <option></option>
<?php
 
	if ($_POST['r1']=="clave"){//si 
		@$resultado=mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   nparte1 like '%".$_POST['tbus']."%'");
		$nr = mysqli_num_rows($resultado);
		//$x = 1;
		if($nr == 0){
			@$resultado=mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   nparte1 like '%".$_POST['tbus']."%'");
			$nr1 = mysqli_num_rows($resultado);
			//$x = 2;
		}
	}elseif($_POST['r1']=="des"){
		@$resultado=mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   np_desc1 like '%".$_POST['tbus']."%'");
		//$x = 3;
	}
	 while(@$registro=mysqli_fetch_array($resultado))
	{
		if(($_POST['producto'] == $registro['nparte1']) or (isset($_POST['+[a-z_]+']) and $nr == 1)){
			echo '<option value="' . $registro['nparte1'] . '" selected>' . $registro['nparte1'] . ' | ' .$registro['np_desc1'] . ' | Dis: ' .$registro['t5'] .'</option>';
			$va =  $registro['nparte1'];
		}else{
			echo '<option value="' . $registro['nparte1'] . '">' . $registro['nparte1'] . ' | ' .$registro['np_desc1'] . ' | Dis: ' .$registro['t5'] .'</option>';
		}
	}			
?>
      </select></td>
    <td colspan="3" nowrap>TIPO DE COMPRA.
      <select name="select2" id="select2">
        <?php

		@$resultado=mysqli_query($con ,"select * from dgalmacen");
		while($registro=mysqli_fetch_array($resultado))
		{
			if(($registro['almacen']==$_POST['ver3'] or $registro['almacen']==$_POST['select2']) or (isset($_REQUEST['Submit3']) and $registro['almacen'] == $regyy['almacen'])){
				echo '<option value="' . $registro['almacen'] . '" selected>' . $registro['desc_almacen'].'</option>';
			}else{
				echo '<option value="' . $registro['almacen'] . '">' . $registro['desc_almacen'].'</option>';
			}
		}		
		
	?>
      </select></td>
  </tr>
  <tr>
    <td width="80" nowrap>CANTIDAD.</td>
    <td width="117" nowrap>PRECIO.</td>
    <td width="85" nowrap>IMPORTE</td>
    <td width="108" nowrap>DESCUENTO.</td>
    <td width="116" nowrap>I.V.A.</td>
    <td width="77" nowrap>TOTAL.</td>
  </tr>
  <tr>
    <?php
 
  //sacar el precio del dolar
  	@$ress=mysqli_query($con ,"select * from dgparame");
	@$num_ro = mysqli_fetch_array($ress);
  ?>
    <td nowrap><input name="tcan" type="text" size="8" maxlength="15" onKeyUp="seg_can(this.value)" onKeyPress="return soloNumeros(event)"
	 <?php
 
	 /* if(isset($_POST['producto']) or ($_POST['tbus']<>"" and isset($_POST['tbus']))){
		@$resultado2=mysqli_query($con ,"select * from dginvalm where nparte1 = '$va'");
		@$num_rows = mysqli_num_rows($resultado2);
		if ($num_rows==0){
			echo '0';
		}else{
			@$registro2=mysqli_fetch_array($resultado2);
			
			echo $registro2['t3'];
		}
	}else{
		echo '0';
	} */
	 ?> 
	  ></td>
    <td nowrap><strong><span class="Estilo16">$</span>
        <input name="tpre" type="text" class="tdato" size="12" onKeyUp="multiTex()"  
	<?php
 
	
	//if($_POST['tbus']<>"" and isset($_POST['tbus']) or (isset($_POST['producto']))){
		//if(isset($_POST['+[a-z_]+'])){
			@$result = mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   nparte1 ='".$_POST['producto']."'");
			@$row = mysqli_fetch_array($result);
	echo 'value="'.number_format($row['cos_prom'], 2, '.', ',').'"';	
	//echo 'value="'.$num_ro['tip_camb_ven'].'"';	
	?>
	
	>
    </strong></td>
    <td nowrap><input name="total" type="text" id="total" onClick="multiTex()" size="12"></td>
    <td nowrap><label>
      <input name="des" type="text" id="des" onKeyPress="return soloNumeros(event)" onKeyUp="descTex()" size="6" maxlength="15">
      
	  <input name="por_des" type="text" id="por_des" onKeyPress="return soloNumeros(event)" onKeyUp="seg_can(this.value)" size="8" maxlength="15" onClick="descTex()">
    </label></td>
    <td nowrap><input name="iva" type="text" id="iva" onKeyUp="ivaTex()" size="6">
      <input name="totaliva" type="text" id="totaliva" onClick="ivaTex()" size="8"></td>
    <td nowrap><input name="supertotal" type="text" id="supertotal" onClick="superTex()" size="10"></td>
  </tr>
  <tr>
    <td nowrap>&nbsp;</td>
    <td nowrap>&nbsp;</td>
    <td nowrap>&nbsp;</td>
    <td colspan="3" nowrap>&nbsp;</td>
  </tr>
</table>
    <div align="center"><br>
    <input type="submit" name="boton_car" value="Agregar Producto" onClick="sendFormNAS('frm1','AL21000_AC.php','_self')">
    <br>
</div>
<table border='1' align="center">
    <tr>
      <th colspan="10">&nbsp;</th>
    </tr>
    <tr>
	  <th><div align="center">SEL.</div></th>
      <th><div align="center">CLAVE. </div></th>
      <th><div align="center">DESCRIPCION.</div></th>
	  <th><div align="center">PRECIO.</div></th>
	  <th><div align="center">% DESC.</div></th>
	  <th><div align="center">DESCUENTO</div></th>
      <th><div align="center">CANTIDAD</div></th>
	  <th><div align="center">IMPORTE</div></th>
	  <th><div align="center">I.V.A.</div></th>
	  <th><div align="center">TOTAL.</div></th>
    </tr>

<?php

if (isset($_POST['boton_car']) || isset($_POST['producto']) || isset($_POST['Submit2']) || isset($_POST['Submit'])){
	//saca el numero de producto
	@$res=mysqli_query($con ,"select * from dgreqded where cons_ven = '".$_POST['tnum']."'");
	while(@$row = mysqli_fetch_array($res))
	{
		$npr[]=$row["nparte1"];	
		$nrx[]=$row["fol_ven1"];	
	}
	//contar cuantos registros
	$num_elementos=count($npr);
	//saca la descripcion del producto
	for($j=0;$j<=$num_elementos-1;$j++)
	{
		@$res=mysqli_query($con ,"SELECT * FROM farticulos WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '$npr[$j]'");
		while($row = mysqli_fetch_array($res))
		{
			$dpr[]=$row["np_desc1"];	
		}
	}
	$num_elementos1=count($npr);
	for($y=0;$y<=$num_elementos1-1;$y++)
	{	
		//mostrar datos encontrados en dgrefodd
		@$res=mysqli_query($con ,"select * from dgreqded where cons_ven = '".$_POST['tnum']."' and nparte1 = '$npr[$y]'");
		while($row = mysqli_fetch_array($res))
		{
			echo '<tr><td>';
			echo '<div align="center"><input name="r5" type="radio" value="'.$row['fol_ven1'].'"/></div></td>';
			echo '<input name="nu_re1'.$y.'" type="hidden" value="'.$nrx[$y].'">';
			echo '<input name="nu_re2'.$y.'" type="hidden" value="'. $npr[$y].'">';
			echo '<td><div align="center">' . $npr[$y] . '</div></td>';
			echo '<td><div align="center">' . $dpr[$y] . '</div></td>';
			$pbas=$row['pre_uni'];
			echo '<td><div align="center">$ ' . number_format($row['pre_uni'], 2, '.', ',') . '</div></td>';
			$pbase =  $pbase + $pbas;
			$c = $c + $row['can_ref'];
			echo '<td><div align="center">' . number_format($row['por_des'], 2, '.', ',') . '  %</div></td>';
			echo '<td><div align="center">$ ' . number_format($row['des'], 2, '.', ',') . '</div></td>';
			$d = $d + $row['des'];
			$pre_u = $pbas - $row['des'];
			$vgf = $vgf + $row['des'];
			//echo '<td><div align="center">$ ' . number_format($pre_u, 4, '.', ',') . '</div></td>';
			$p = $p + $pre_u;
			//sacra el iva
			$sbt = $pre_u * $row['can_ref'];
			echo '<td><div align="center">' . $row['can_ref'] . '</div></td>';
			echo '<td><div align="center">$ ' . number_format($row['subtotal'], 2, '.', ',') . '</div></td>';
			$s = $s + $sbt;
			$jsh = $jsh + $row['subtotal'];
			//$ti = $sbt
			$ivpp = $row['iva_pre'] ;
			echo '<td><div align="center">$ ' . number_format($ivpp, 2, '.', ',') . '</div></td>';
			$iv = $iv + $ivpp;
			$juy= $juy + $row['total_cd'];
			$t = $ivpp + $sbt;
			echo '<td><div align="center">$ ' . number_format($row['total_cd'], 2, '.', ',') . '</div></td>';
			$tot = $tot + $t;
			echo '</tr>'; 
		}
	}
 } 
 mysqli_close($con);
?>
<th colspan="2">Totales</th>
      <th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
      <th><?php
 
	echo '<input name="txt_s" type="hidden" value="'.$vgf.'">';
	echo '$' . number_format($vgf, 2, '.', ',');
	?></th>
	<th>&nbsp;</th>
	    <th>
	<?php
 
	echo '<input name="txt_s" type="hidden" value="'.$jsh.'">';
	echo '$' . number_format($jsh, 2, '.', ',');
	?>	</th>
	    <th>
	<?php
 
	echo '<input name="txt_iv" type="hidden" value="'.$iv.'">';
	echo '$' . number_format($iv, 2, '.', ',');
	?>	</th>
	    <th>
	<?php
 
	echo '<input name="txt_to" type="hidden" value="'.$juy.'">';
	echo '$' . number_format($juy, 2, '.', ',');
	?>	</th>
  </table>
  <div align="center">
    <input name="Submit5" type="Submit" value="Terminar Captura" onClick="cargapag('AL21000_AG2.php','_self')">
  </div>
</form>
</body>
</html>
