<?php
 
session_start();
include("DMMYSQLG.php");
include ("varfun.php");

?>
<?php
 
$fec=$_POST['tfec'];
include("DF20000.php");
//buscar almacen
if (isset($_POST['Submit22']))
	{
	    $usu=mysqli_real_escape_string($con,$_POST['trefe']); //FOL FACTURA
		$usa=mysqli_real_escape_string($con,$_POST['tip_ven']); //ID DEL PROVEEDOR
		$usor=mysqli_real_escape_string($con,$_POST['folio_control']); //ID DEL PROVEEDOR 
		$dt_user=mysqli_fetch_array(mysqli_query($con ,"SELECT   count(*) as numRows, tip_ven FROM dgreqdev WHERE tip_ven = '$usa' AND trefe='$usu' "));
			echo mysqli_error($con);
			 	if($dt_user['numRows']>=1){
					echo '<script language="javascript"> alert("����LA FACTURA YA EXISTE!!!!") </script>';
				    echo '<meta content="0; url=AL10700_PR2.php" HTTP-EQUIV="REFRESH">';
				}else
				{
					$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);
					$pase=1;
				}
		}	
?>
<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 subt(price,cant,porde,poriv)
{
//Multiplico precio por cantidad
var subtot = parseFloat (document.getElementById(price).value) * parseFloat (document.getElementById(cant).value);
var descu = (parseFloat ((document.getElementById(porde).value)* subtot)/100);
var iva = (parseFloat((document.getElementById(poriv).value)* subtot)/100);
var subtt= parseFloat((subtot-descu)+iva);
//Creo variable con el nombre del subtotal y name[1] que contiene el $row['+[a-z_]+']
var subname = 'total';
var subname1 = 'por_des';
var subname2 = 'totaliva';
var subname3 = 'supertotal';
//Asigno el valor subtot al cuadro de texto Subtotal del producto correspondiente
document.getElementById(subname).value=subtot.toFixed(2);
document.getElementById(subname1).value=descu.toFixed(2);
document.getElementById(subname2).value=iva.toFixed(2);
document.getElementById(subname3).value=subtt.toFixed(2);
//Total = parseInt(document.getElementById('Total').value) + subtot;
//document.getElementById('Total').value = Total;
}
</script>
<script language="javascript">
function seleccionar(i){ 
   //for (i=0;i<document.f1.elements.length;i++) 
      if(document.frm1.txtcod['i'].type == "checkbox")	
         document.frm1.txtcod['i'].checked=1 
} 

function desactiva(){
document.frm1.subtotal.disabled=true;
//document.frm1.subtotal.readonly=true;
}
</script>



<body>
<?php
 
$ayuda = 'DI3200A_AG1.php';
include("DYAYUDA.php");
?>
<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" style="display:NONE">
<?php
  	
if($_GET['tnum']!=0)
		{
		echo '<input name="tnum" type="text" value="'.$_GET['tnum'].'">';
		echo 'FOLIO DE ORDEN:'.$_GET['tnum']; 
		@$result = mysqli_query($con ,"SELECT * FROM dgreqdev WHERE fol_ven = '".$_GET['tnum']."'");
		}
	elseif(isset($_POST['Submit22'])){
		@$ultimo_id = mysqli_insert_id($con);
		echo '<input name="tnum" type="text" 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="text" 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><span class="Estilo17">ORDEN DE COMPRA.</span> </td>
    <td width="27"><?php
 
			if($_GET['pas']!=0)
		{
			@$resul=mysqli_query($con ,"select * from dgreqdev where folio_control = '".$_GET['pas']."'");
			@$regis=mysqli_fetch_array($resul);
			echo '<input name="ver5" type="hidden" value="'.$regis['folio_control'].'">';
			echo str_pad($regis['folio_control'],5,'0',STR_PAD_LEFT);
		}
		elseif(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 str_pad($regis['folio_control'],5,'0',STR_PAD_LEFT);
		}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 str_pad($regis['folio_control'],5,'0',STR_PAD_LEFT);
		}	
	
	?></td>
    <td width="39"><span class="Estilo17">FECHA. </span></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"><span class="Estilo17">PROVEEDOR.</span></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><span class="Estilo17">FACTURA.</span></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 str_pad($reg['trefe'],5,'0',STR_PAD_LEFT);
		}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 str_pad($reg['trefe'],5,'0',STR_PAD_LEFT);
		}	
	?></td>
  </tr>
</table>
 <?php

 //VALIDA QUE SEA ORDEN O FACTURA DIRECTA 
/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////si eprecionan el boton de eliminar/
/////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['Submit2'])){    //ELIMINA LOS ARTICULOS 
	include('AL10700_AC7.php');	
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////Si se agrega un producto para la venta
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['boton_car'])){  //AGREGA LOS ARTICULOS 
	if  (isset($_POST['txt_to']))
	{ //virifica que la cantidad sea aceptable
		$suma=$_POST['txt_s']+$_POST['total'];
		if ($suma>$_POST['subtotal']){
		echo '<script language="javascript">
				if (alert("EL IMPORTE SOBREPASA EL TOTAL DE LA FACTURA!!!!")){ 
					document.frm1.tcan.focus();
					}
				 </script>';
		}else {
			include('AL10700_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('AL10701_AC.php','_self')"></td>
    <td colspan="2" nowrap><input name="r1" type="radio" value="clave" checked 
	<?php
 
	if($_POST['r1'] == "clave" ){
		echo ' checked';
	}
	?>
	>
      <span class="Estilo17">CLAVE.</span>
  <input name="r1" type="radio" value="des"
  <?php
 
	if($_POST['r1'] == "des"){
		echo ' checked';
	}
	?>
  >
  <span class="Estilo17">DESCRIPCION.</span></td>
    <td colspan="2" nowrap><span class="Estilo17">TIPO DE COMPRA.</span>      <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>
    <?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']; 
	$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['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="4" nowrap><span class="Estilo17">PRODUCTO.</span>      <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'] . ' | Existencia: ' .$registro['t5'] .'|&nbsp;&nbsp;&nbsp;&nbsp; I.V.A: ' .$registro['web'] .'</option>';
			$va =  $registro['nparte1'];
		}else{
			echo '<option value="' . $registro['nparte1'] . '">' . $registro['nparte1'] . ' | ' .$registro['np_desc1'] . ' | Existencia: ' .$registro['t5'] .'|&nbsp;&nbsp;&nbsp;&nbsp; I.V.A: ' .$registro['web'] .'</option>';
		}
	}			
?>
      </select></td>
    <td colspan="2" nowrap><span class="Estilo17">SUBTOTAL.</span>
      <label><strong><span class="Estilo16">$</span></strong>
      <input name="subtotal" type="text" id="subtotal" size="10" onBlur="subt('tpre','tcan','por_des','iva');"
			<?  if (isset($_GET['sub'])){
					echo 'value="'. $_GET['sub'] .'"';			
				}else if(isset($_POST['subtotal'])){ //este es el truco
					echo 'value="'. $_POST['subtotal'] .'"';
				}else {
					echo 'value="0"';
				}
			?>>  
      <input name="descu" type="hidden" id="descu">
      </label></td>
    </tr>
  <tr>
    <td width="80" nowrap><span class="Estilo17">CANTIDAD.</span></td>
    <td width="117" nowrap><span class="Estilo17">PRECIO.</span></td>
    <td width="85" nowrap><span class="Estilo17">IMPORTE.</span></td>
    <td width="108" nowrap><span class="Estilo17">DESCUENTO.</span></td>
    <td width="116" nowrap><span class="Estilo17">I.V.A.</span></td>
    <td width="77" nowrap><span class="Estilo17">TOTAL.</span></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" onBlur="subt('tpre','tcan','des','iva')" onKeyPress="return soloNumeros(event)" value="0" size="8" maxlength="15">	</td>
    <td nowrap><strong><span class="Estilo16">$</span>
    <input name="tpre" type="text" class="tdato"  onblur="subt('tpre','tcan','des','iva')" size="12"
	<?php
 
			@$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, '.', ',').'"';	
	?>
	>
    </strong></td>
    <td nowrap><input name="total" type="text" id="total" readonly="" size="12"></td>
    <td nowrap><label>
      <input name="des" type="text" id="des" onKeyPress="return soloNumeros(event)" onBlur="subt('tpre','tcan','des','iva')" size="6" maxlength="15" value="0">
      
	  <input name="por_des" type="text" id="por_des" onKeyPress="return soloNumeros(event)" size="8" maxlength="15" readonly="">
    </label></td>
    <td nowrap><input name="iva" type="text" id="iva"  size="6" value="0" onBlur="subt('tpre','tcan','des','iva')"/>
      <input name="totaliva" type="text" id="totaliva"  readonly=""size="8"></td>
    <td nowrap><input name="supertotal" type="text" id="supertotal"  readonly="" size="10"></td>
  </tr>
</table>
    <div align="center"><br>
    <input type="submit" name="boton_car" value="Agregar Producto" onClick="sendFormNAS('frm1','AL10701_AC.php','_self');">
    <br>
</div>
<table border='1' align="center">
    <tr>
      <th colspan="10"><div align="left">
        <input type="submit" name="Submit2" value="Eliminar" onClick="sendFormNAS('frm1','AL10701_AC.php','_self')">
      </div></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_d" 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('AL10700_AG2.php','_self')">
  </div>
    <div align="center">
    </div>
  
</form>
</body>
</html>

