<?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="">
<input name="ver" type="hidden" value="1">

  <table   align="center">
<tr>
<td colspan="4" class="men">
    <?php
  
	if(isset($_POST['bot2'])){
		@$ultimo_id = mysqli_insert_id($con);
		echo '<input name="tnum" type="hidden" value="'.$_POST['bot'].'">';
		echo 'NUMERO DE REQUISICION:'.$_POST['bot']; 
		@$result = mysqli_query($con ,"SELECT * FROM dgreqdev WHERE fol_ven = '".$_POST['bot']."'");
	}else{
		echo '<input name="tnum" type="hidden" value="'.$_POST['tnum'].'">';
		echo 'NUMERO DE REQUISICION:'.$_POST['tnum']; 
		@$result = mysqli_query($con ,"SELECT * FROM dgreqdev WHERE fol_ven = '".$_POST['tnum']."'");
	}	
	@$row = mysqli_fetch_array($result);
	$fol_ven=$row["fol_ven"];
	$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"];
	
	//-->se verifica si afecta a produccion 
	$sq_material_con=mysqli_query($con ,"select * from dpmaterial_con where tip_requ='E' and noreg_requ='$fol_ven'");
	if(mysqli_num_rows($sq_material_con)>0){
		$dt_material_con=mysqli_fetch_array($sq_material_con);
		echo '<input name="s1" type="hidden" value="PT">';
		echo '<input name="s2" type="hidden" value="'.$dt_material_con["cve_ord"].'">';
		echo '<input name="s3" type="hidden" value="'.$dt_material_con["noreg_maquilero"].'">';
	}


?>  </td>
  </tr>
  <tr>
    <th colspan="4" class=\"titulo\">Datos de la Entrada </th>
  </tr>
  <tr>
    <td width="117">Tipo:</td>
    <td width="267">
	<?php
 
		//echo $op1.'<br>';
		
		if(isset($_POST['ver1']))
		{
			@$resultado=mysqli_query($con ,"select * from dgtipcar where tip_car = '".$_POST['ver1']."'");
			@$registro=mysqli_fetch_array($resultado);
			echo '<input name="ver1" type="hidden" value="'.$registro['tip_car'].'">';
			echo $registro['des_tipcar'];
		}else{
			@$resultado=mysqli_query($con ,"select * from dgtipcar where tip_car = '$op1'");
			@$registro=mysqli_fetch_array($resultado);
			echo '<input name="ver1" type="hidden" value="'.$registro['tip_car'].'">';
			echo $registro['des_tipcar'];
		}	
	?>
	&nbsp;</td>
    <td colspan="2">Fecha : 
	  <?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>
</table>
 <?php

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

?>

    <table  align="center">
  <tr>
    <th colspan="3" class=\"titulo\">Productos de la Entrada </th>
    </tr>
  <tr>
    <td> 
        <input name="tbus" type="text" id="tbus"
		<?php
 
		echo 'value ="'.$_POST['tbus'].'"';
		?>
		>
          <input type="submit" name="Submit" value="Buscar" onClick="openpage('DI32000_AC.php','_self')"></td>
    <td colspan="2"><input name="r1" type="radio" value="clave" checked 
	<?php
 
	if($_POST['r1'] == "clave" ){
		echo ' checked';
	}
	?>
	>
Clave del producto
  <input name="r1" type="radio" value="des"
  <?php
 
	if($_POST['r1'] == "des"){
		echo ' checked';
	}
	?>
  >
Descripcion</td>
    <?php
 
@$resultado2=mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.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['lim_vta1']; 
	$pp9 = $regist2['lim_vta2']; 
	$pp10 = $regist2['lim_vta3'];  
	$pp1 = $regist2['lim_1']; 
	$pp2 = $regist2['lim_2']; 
	$pp3 = $regist2['lim_3']; 
	/* $a14 = $registro2['tip_mon']; 
	$a15 = $registro2['uc_fec_doc']; 
	$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['exi_dis']; 
	$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="2">Producto:
      <select name="producto" onChange="javascript: document.getElementById('frm1').submit()">
	  <option></option>
<?php
 
	if ($_POST['r1']=="clave"){//si 
		@$resultado=mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '".$_POST['tbus']."'");
		$nr = mysqli_num_rows($resultado);
		//$x = 1;
		if($nr == 0){
			@$resultado=mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '%".$_POST['tbus']."%'");
			$nr1 = mysqli_num_rows($resultado);
			//$x = 2;
		}
	}elseif($_POST['r1']=="des"){
		@$resultado=mysqli_query($con ,"select * from dginvent where np_desc1 = '%".$_POST['tbus']."%'");
		//$x = 3;
	}
	 while(@$registro=mysqli_fetch_array($resultado))
	{
		if(($_POST['producto'] == $registro['nparte1']) or (isset($_POST['Submit']) and $nr == 1)){
			echo '<option value="' . $registro['nparte1'] . '" selected>' . $registro['nparte1'] . ' | ' .$registro['np_desc1'] . ' | Dis: ' .$registro['exi_dis'] .'</option>';
			$va =  $registro['nparte1'];
		}else{
			echo '<option value="' . $registro['nparte1'] . '">' . $registro['nparte1'] . ' | ' .$registro['np_desc1'] . ' | Dis: ' .$registro['exi_dis'] .'</option>';
		}
	}			
?>
      </select></td>
    <td>Almacen:
      <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>
  <?php
 
  //sacar el precio del dolar
  	@$ress=mysqli_query($con ,"select * from dgparame");
	@$num_ro = mysqli_fetch_array($ress);
  ?>
    <td>Precio: $
      <input name="tpre" type="text" class="tdato" size="10" 
	<?php
 
	
	//if($_POST['tbus']<>"" and isset($_POST['tbus']) or (isset($_POST['producto']))){
		//if(isset($_POST['Submit'])){
			@$result = mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 ='".$_POST['tbus']."'");
			@$row = mysqli_fetch_array($result);
		/* }elseif(isset($_POST['producto'])){
			@$result = mysqli_query($con ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 ='".$_POST['producto']."'");
		}
		//consultar el registro  y sacar datos
		@$num_rows = mysqli_num_rows($result);
		if ($num_rows==0){
			echo 'value="0"';
		}else{
			while($row = mysqli_fetch_array($result))
			{
				$lim1=$row['lim_1'];
				$lim2=$row['lim_2'];
				$lim3=$row['lim_3'];
				$p1=$row['lim_vta1'];
				$p2=$row['lim_vta2'];	
				$p3=$row['lim_vta3'];
				$p4=$row['ven_esp1'];
				$ti_mo = $row['tip_mon'];
			}
			if($_POST['verd']==1){
				$pr = $p4;
			}elseif($_POST['tcan']<=$lim1){
				$pr = $p1;
			}elseif($_POST['tcan']<=$lim2){
				$pr = $p2;
			}elseif($_POST['tcan']>=$lim3){
				$pr = $p3;
			}
			if($_POST['tdis']==1){
				$pp = $pr4;
			}else{
				$pp = $pr;
			}
		}
	}elseif(isset($_POST['tpre']) and $_POST['tpre']<>""){
		$pp = $_POST['tpre'];
	}else{
		$pp = 0;
	} 
	if($ti_mo == "E"){
		$pp=$num_ro['tip_camb_ven'] * $pp;
	} */
	echo 'value="'.number_format($row['cos_prom'], 2, '.', ',').'"';	
	//echo 'value="'.$num_ro['tip_camb_ven'].'"';	
	?>
	
	></td>
    <td colspan="2">Cantidad de producto:
      <input name="tcan" type="text" size="10" 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['lim_vta1'];
		}
	}else{
		echo '0';
	} */
	 ?> 
	  ></td>
    </tr>
</table>
    <div align="center"><br>
    <input type="submit" name="boton_car" value="Agregar Porducto" onClick="openpage('DI32000_AC.php','_self')">
    <br>
</div>
<table border='1' align="center">
    <tr>
      <th> 
        <input type="submit" name="Submit2" value="Eliminar" onClick="openpage('DI32000_AC.php','_self')">
      </th>
      <th colspan="10"></th>
    </tr>
    <tr>
	  <th><div align="center">Sel</div></th>
      <th><div align="center">Clave Producto</div></th>
      <th><div align="center">Descripcion</div></th>
	  <th><div align="center">Precio Base</div></th>
	  <th><div align="center">% Descuento</div></th>
	  <th><div align="center">Descuento</div></th>
      <th><div align="center">Precio Unitario</div></th>
	  <th><div align="center">Cantidad</div></th>
	  <th><div align="center">Sub - total</div></th>
	  <th><div align="center">Iva</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'])  || isset($_POST['bot2'])){
	//saca el numero de producto
	@$res=mysqli_query($con ,"select * from dgreqded where cons_ven = '".$_POST['tnum']."' or cons_ven = '".$_POST['bot']."'");
	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 dginvent WHERE dginvent.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']."' or cons_ven = '".$_POST['bot']."')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'], 4, '.', ',') . '</div></td>';
			$pbase =  $pbase + $pbas;
			$c = $c + $row['can_ref'];
			echo '<td><div align="center">' . number_format($row['por_des'], 4, '.', ',') . '  %</div></td>';
			echo '<td><div align="center">$ ' . number_format($row['des'], 4, '.', ',') . '</div></td>';
			$d = $d + $row['des'];
			$pre_u = $pbas - $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($sbt, 2, '.', ',') . '</div></td>';
			$s = $s + $sbt;
			//$ti = $sbt
			$ivpp = $row['iva_pre'] ;
			echo '<td><div align="center">$ ' . number_format($ivpp, 2, '.', ',') . '</div></td>';
			$iv = $iv + $ivpp;
			$t = $ivpp + $sbt;
			echo '<td><div align="center">$ ' . number_format($t, 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>&nbsp;</th>
	<th>&nbsp;</th>
	    <th>&nbsp;</th>
	    <th>
	<?php
 
	echo '<input name="txt_s" type="hidden" value="'.$s.'">';
	echo '$' . number_format($s, 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="'.$tot.'">';
	echo '$' . number_format($tot, 2, '.', ',');
	?>	</th>
  </table>
  <div align="center">
    <input name="Submit5" type="Submit" value="Terminar Captura" onClick="openpage('DI32000_AG2.php','_self')">
  </div>
</form>
</body>
</html>
