<?php
 session_start();
 require "funciones.php";
// include ("emenux.php"); 
 require ("conecta.php");
 $tipod=$_POST['hdrdbut'];
 $cod_tipo_d=$_POST['hdrdbut'];
 $numero="";
 //********************************
 //asigna folio
 $maxf=0;//máximo folio
 $sqlf="select max(numero) from m02005 where cod_tipo_d='$tipod';";
 $resf=mysql_query($sqlf,$con);
 if (mysql_num_rows($resf)>0){
  while ($filaf=mysql_fetch_row($resf)){
   $maxf=($filaf[0]*1);
  }//wend  
 }//fin si 
 //*****************fin asigna folio
 $numerof=$maxf+1;
 $fecha=date("Ymd");
 $usuario=$_SESSION['nick'];
 $rut="";
 $nombre="";
 $giro="";
 $direccion="";
 $cod_comuna="";
 $stotal="";
 $d1="";
 $d2="";
 $d3="";
 $neto="";
 $iva="";
 $total="";
 $sw=$_POST['hdsw'];
 $sw2=$_POST['hdsw'];
 $cont=0;
 $error2= "0";
 $recomendacion2="0";
 if ($sw=="buscar"){
  $sqlb="select * from m02001 where rut = '$rut' ;";
  $resb=mysql_query($sqlb,$con);
  if (mysql_num_rows($resb)>0){ 
   $sw="encontrado";
   $sw2="encontrado";
   while ($filab = mysql_fetch_row($resb)){
    $nombre=$filab[1];
	$giro=$filab[2];
    $direccion=$filab[3];
    $codcomuna=$filab[4];
    }//while
   }else{
    $error2= "rut no encontrado.";
    $recomendacion2="->Agregar.";
   }//fin si   
  }//fin de buscar
  if ($sw=="grabar"){
   $cod_tipo_d=$_POST['hdtipod'];
   $numero=$_POST['numero'];
   $fecha=$_POST['fecha'];
   $usuario=$_SESSION['nick'];
   $rut=strtoupper($_POST['rut']);
   $nombre=$_POST['nombre'];
   $giro=$_POST['giro'];
   $direccion=$_POST['direccion'];
   $cod_comuna=$_POST['hdcom'];
   $stotal=$_POST['stotal'];
   $d1=$_POST['d1'];
   $d2=$_POST['d2'];
   $d3=$_POST['d3'];
   $neto=$_POST['neto'];
   $iva=$_POST['iva'];
   $total=$_POST['total'];
//   $sqlg="replace into m02005 
//   (rut,nombre,direccio,codcomun,telefon1,telefon2,fax,contacto,email,codconpa)
//   values ('$rut','$nombre','$direccion','$codcomuna','$telefono1','$telefono2','$fax','$contacto','$email','$codcpago');";
   $resg=mysql_query($sqlg,$con); 
   $cod_tipo_d="";
   $numero="";
   $fecha="";
   $usuario="";
   $rut="";
   $nombre="";
   $giro="";
   $direccion="";
   $cod_comuna="";
   $stotal="";
   $d1="";
   $d2="";
   $d3="";
   $neto="";
   $iva="";
   $total="";
   $sw=""; 
 }//fin grabar
 if (strlen($codcomuna)==0){$codcomuna="5406";}
 if (strlen($cod_tipo_d)==0){$cod_tipo_d="01";} 
 //**************************************************
 //**************************************************
 //GRABACION DE ITEMES SELECCIONADOS
 $totalf=$_POST['hdcont'];
 $nick=$_SESSION['nick'];
 $stot=0;
  for ($i=1;$i<=$totalf;$i++){
   $icheck="check" . $i;
   $check[$i]=$_POST[$icheck];
   $icodigo="hd" . $i;
   $codigo[$i]=$_POST[$icodigo];
   $codc=$codigo[$i];
   $icant="hdcant" . $i;
   $cant[$i]=$_POST[$icant];
   $ipre="hdpre" . $i;
   $pre[$i]=$_POST[$ipre];   
   if($check[$i]=="checkbox"){
    $stot=$stot + ($pre[$i]*$cant[$i]);
    $code=$codigo[$i];
    $sqle="update m02004 set estado='1' where llave='$nick' and cod_interno='$codc';";
    $rese=mysql_query($sqle,$con) or die(mysql_error());
   }
  }//for
  $neto=round($stot/1.19);
  $iva=$stot-$neto;
  $total=$neto + $iva;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<?php
 $sqlp="select rut,nombre from m02001 order by nombre;";
 $resp=mysql_query($sqlp,$con);
 echo "<script language='JavaScript'>" ;
 echo  "var seleccionado = '';"; 
 echo  " var aEdit = new Array("; 
 $regs=mysql_num_rows($resp);
 $contp=0;
 while ($filap=mysql_fetch_row($resp)){
   $contp++;
   $rutp=$filap[0];
   $nombrep=$filap[1];
          echo      "\"" . $rutp . "," . $nombrep . "\""; 
          if ($regs != $contp){echo ",";} 
          
				}//fin while
            echo   ")"; 
 echo "</script>"; 
?>
<TITLE>Maestro Venta</TITLE>
<script language="javascript" src="fstandar.js"></script>
<script language="javascript">
function buscar(){
   open("popupB.php?nombre=" + document.form1.rut.value ); 
 }
function ponecomuna(){
 document.form1.hdcom.value=document.form1.cmbcom.value;
 }
function buscarut(){
 if (event.keyCode==13){
  for (var i = 0; i < aEdit.length; i++){
  var a =aEdit[i];
  if (document.form1.rut.value==a.substr(0,9)){
	document.form1.nombre.value=a.substr(10);	
	document.form1.giro.focus();
   }//si
  }//for
 }//fin eevnt.keycode 
}//fin funcion

function ponetipo(){
 document.form1.hdtipod.value=document.form1.cmbtipod.value;
}
function valida(){ 
 if (document.form1.rut.value.length > 0){
  err=0;
  document.form1.hdsw.value="grabar";
  if (modulo11(document.form1.rut.value)==-1  ){
   window.alert("El rut ingresado es incorrecto" );
   err=1;
   document.form1.rut.style.backgroundColor="#FFFFCC";
   document.form1.rut.focus();  
   }  
  if (document.form1.nombre.value.length==0){
   err=1;
   window.alert("debe ingresar nombre");
   document.form1.nombre.style.backgroundColor="#FFFFCC";
   document.form1.nombre.focus();
   } 
  if (document.form1.direccion.value.length==0){
   err=1;
   window.alert("debe ingresar direccion");
   document.form1.direccion.style.backgroundColor="#FFFFCC";
   document.form1.direccion.focus();
   }  
  if (err==0){
   document.form1.numero.disabled=false;
   document.form1.action="grabaventa.php"
   document.form1.method="post";
   document.form1.submit();
   }  
 }//fin si  
}  
function ponedesc(){
 if (event.keyCode==13){
 document.form1.neto.value=document.form1.stotal.value-Math.round((document.form1.stotal.value*document.form1.d1.value/100)); 
 document.form1.iva.value=Math.round(document.form1.neto.value*0.19);
 document.form1.total.value=(document.form1.neto.value *1) + (document.form1.iva.value*1);
 }
}
</script>
<link rel="stylesheet" href="styles.css">
<style type="text/css">
<!--
.Estilo1 {color: #0000FF}
.Estilo2 {color: #FF0000}
-->
</style>
</HEAD>
<BODY <?php
 if ($sw=="buscar"){
  echo "onload='javascript:(document.form1.nombre.focus())'";
  }
 if ($sw=="grabar" || $sw=="eliminar"){
  echo "onload='javascript:(document.form1.rut.focus())'";
  } 
 if ($sw=="encontrado" ){
  echo "onload='javascript:(document.form1.cmdgra.focus())'";
  }  
  ?>>
  <form id="form1" name="form1" method="post" action="">
<TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%" align=center 
border=0>
  <TBODY>
  <TR>
    <TD vAlign=center align=middle height="100%">
      <TABLE height=400 cellSpacing=0 cellPadding=0 width=718 border=0>
        <TBODY>
        <TR>
          <TD width="718" height=22>
            <TABLE height=22 cellSpacing=0 cellPadding=0 width="100%" 
              border=0><TBODY>
              <TR>
                <TD vAlign=top align=left width=24><A 
                  href="index.php"><IMG src="images/xc_left.gif" alt="Inicio" width=24 
                  height=22 
                  border=0></A></TD>
                <TD vAlign=top align=left width="100%"><IMG height=22 
                  src="images/xc_center.gif" width="100%" 
                border=0></TD>
              <TD vAlign=top align=right width=24><IMG height=22 
                  src="images/xc_right.gif" width=24 
              border=0></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD class=BottomTable vAlign=top 
          background=images/xc_stripes.gif height="100%">
            <TABLE class=IconsTable height=33 cellSpacing=0 cellPadding=0 
            width="100%" border=0>
              <TBODY>
              <TR>
                <TD height="32"><div align="right" class="larger">
                  <div align="right">Vemol</div>
                </div></TD>
                </TR></TBODY></TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="715" 
              border=0>
              <TBODY>
              <TR>
                <TD width=23 height=18>&nbsp;</TD>
                <TD vAlign=top align=left width=180 height=18>&nbsp;</TD>
                <TD width=12 height=18>&nbsp;</TD>
                <TD width="461" height=18 class=MainMenuHead><div align="left">Proceso de Venta :</div></TD>
                <TD width=20 height=18>&nbsp;</TD></TR>
              <TR>
                <TD width=23 height="100%">&nbsp;</TD>
                <TD vAlign=top align=left width=180 height="100%">
                  <TABLE cellSpacing=0 cellPadding=0 width="150" border=0>
                    <TBODY>
                    <TR>
                      <TD width=10 valign="top"><IMG height=12 
                        src="images/xc_dotgrey.gif" width=10></TD>
                      <TD width=4>&nbsp;</TD>
                      <TD class=LeftMenu noWrap><p>Acciones de Venta:<br />
                        -&gt;<a href="carro.php">Ver Carro de Compra.</a><br />
                          -&gt;<a href="M0205001.php">Volver a buscar Articulos</a> <br />
                          </p>
                        </TD>
                    </TR>
                    <tr>
                      <td valign="top"><img 
                        src="images/xc_dotgrey.gif" width="10" height="12" /></td>
                      <td>&nbsp;</td>
                      <td class="LeftMenu" nowrap="nowrap">Alertas:<span class="Estilo2"><br />
                            <?php echo $error2 ?></span></td>
                    </tr>
                    <tr>
                      <td valign="top"><img 
                        src="images/xc_dotgrey.gif" width="10" height="12" /></td>
                      <td>&nbsp;</td>
                      <TD class=LeftMenu noWrap>Se recomienda:<span class="Estilo1"><br />
                          <?php echo $recomendacion2 ?></span></TD>
                    </TR></TBODY></TABLE></TD>
                <TD width=12 height="100%">&nbsp;</TD>
                <TD class=contentcell vAlign=top align=middle width=461 
                height="100%"><table width="90%" border="0" cellspacing="0" cellpadding="0">
				 <tr>
                    <th scope="col"><div align="right" class="Emphasize">Tipo:</div></th>
                    <th scope="col"><div align="left">
                        <?php
									$sqltip="select * from m02006 order by 2;";
									$restip=mysql_query($sqltip,$con) ;				
								?>
                        <select name="cmbtipod" id="cmbtipod" onchange="ponetipo()">
                          <?php 
									while ($filatip=mysql_fetch_row($restip)){
									if ($filatip[0]==$cod_tipo_d){
				                  	echo "<option value=$filatip[0] selected='selected'>$filatip[1]</option>";
									}else{
									 //echo "<option value=$filatip[0]>$filatip[1]</option>";
									 }//si
									}
								?>
                        </select>
                        <input name="hdtipod" type="hidden" id="hdtipod" value="<?php echo $cod_tipo_d ?>"/>
                        <input name="hdsw" type="hidden" id="hdsw" value="nada" />
                    </div></th>
                    <th scope="col"><div align="right"><span class="Emphasize">Numero:</span></div></th>
                    <th scope="col"><div align="left">
                      <input name="numero" type="text" id="numero"  value="<?php echo $numerof ?>" size="11" maxlength="10" disabled/>
                    </div></th>
                  </tr>

                  <tr>
                    <th scope="row"><div align="right" class="Emphasize">Fecha:</div></th>
                    <td><div align="left">
                      <input name="fecha" type="text" id="fecha" value="<?php if (strlen($fecha)==8) echo editafecha($fecha); ?>" size="9" maxlength="8"/>
                    </div></td>
                    <td><div align="right"><span class="Emphasize">Vendedor:</span></div></td>
                    <td><div align="left">
                      <input name="usuario" type="text" id="usuario" value="<?php echo $usuario ?>" size="11" maxlength="10"/>
                    </div></td>
                  </tr>

                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">Rut:</div>
                        </label></th>
                    <td colspan="3"><div align="left">
                      <input name="rut" type="text" id="rut" value="<?php echo $rut ?>" size="10" maxlength="9" onkeydown="buscarut();" />
                    <img src="images/i.p.search.gif" alt="Buscar Rut" width="22" height="18" longdesc="Buscar Rut" onclick="buscar()" /></div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">Nombre:</div>
                        </label></th>
                    <td colspan="3"><div align="left">
                      <input name="nombre" type="text" id="nombre" value="<?php echo $nombre ?>" size="50" maxlength="100" />
                    </div></td>
                  </tr>
                  <tr>
                    <td scope="row"><label>
                        <div align="right" class="Emphasize">Giro:</div>
                        </label></td>
                    <td colspan="3"><div align="left">
                        <input name="giro" type="text" id="giro" value="<?php echo $giro ?>" size="50" maxlength="100" />
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">Direcci&oacute;n:</div>
                      </label></th>
                    <td colspan="3"><div align="left">
                      <input name="direccion" type="text" id="direccion" value="<?php echo $direccion ?>" size="50" maxlength="100" />
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right" class="Emphasize">Comuna:</div></th>
                    <td colspan="3">
                          <div align="left">
                            <?php
									$sqlcom="select * from m01003 order by 2;";
									$rescom=mysql_query($sqlcom,$con) ;				
								?>
                                <select name="cmbcom" id="cmbcom" onchange="ponecomuna()">
                                  <?php 
									while ($fila=mysql_fetch_row($rescom)){
									if ($fila[0]==$codcomuna){
				                  	echo "<option value=$fila[0] selected='selected'>$fila[1]</option>";
									}else{
									 echo "<option value=$fila[0]>$fila[1]</option>";
									 }//si
									}
								?>
                                    </select>                      
                            <input name="hdcom" type="hidden" id="hdcom" value="<?php echo $codcomuna ?>"/>
                                </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">Sub total:</div>
                      </label></th>
                    <td colspan="3"><div align="left">
                      <input name="stotal" type="text" id="stotal" align="right"  value="<?php echo $neto ?>" size="11" maxlength="10" />
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">%DESC.1:</div>
                        </label></th>
                    <td colspan="3"><div align="left">                      <label> </label>
                      <div align="right" class="Emphasize">
                        <div align="left">
                            <input name="d1" type="text" id="d1" value="<?php echo $d1 ?>" size="5" maxlength="2" onkeydown="ponedesc()"/>
                        </div>
                      </div>                    
                       </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right"><span class="Emphasize">Neto</span>:</div>
                        </label></th>
                    <td colspan="3"><div align="left">
                      <input name="neto" type="text" id="neto" value="<?php echo $neto ?>" size="11" maxlength="10"/>
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right"><span class="Emphasize">IVA</span>:</div></th>
                    <td colspan="3"><div align="left">
                      <input name="iva" type="text" id="iva" value="<?php echo $iva ?>" size="11" maxlength="10" />
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right"><span class="Emphasize">Total:</span></div></th>
                    <td colspan="3"><div align="left">
                      <input name="total" type="text" id="total2" value="<?php echo $total ?>" size="11" maxlength="10" />
</div></td>
                  </tr>
                  <tr>
                    <th colspan="4" scope="row"><input name="cmdgra" type="button" id="cmdgra" value="Grabar" onclick="valida()" />
                        <label></label>
                      &nbsp;&nbsp;
                      <label></label>
                      <label></label>
                      <label></label>
                      &nbsp;&nbsp;
                      <input name="cmcan" type="button" id="cmcan" value="Cancelar" onclick="eliminar()"/>                    </th>
                  </tr>
                </table></TD>
                <TD width=20 height="100%">&nbsp;</TD></TR>
              <TR>
                <TD width=23 height=8>&nbsp;</TD>
                <TD width=180 height=8>&nbsp;</TD>
                <TD width=12 height=8>&nbsp;</TD>
                <TD width="461" height=8>&nbsp;</TD>
        <TD width=20 
      height=8>&nbsp;</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
      <p>&nbsp;</p></TD></TR></TBODY>
	  </TABLE>
	</form>  
	</BODY>
<?php
	mysql_close($con);
?>
</HTML>