<?php
 session_start();
 require "funciones.php";
 include "emenux.php";
 require ("conecta.php");
 $cod_tipo_d=$_POST['hdtipod'];
 $numero=$_POST['numero'];
 //********************************
 $sw=$_POST['hdsw'];
 $sw2=$_POST['hdsw'];
 $cont=0;
 $error2= "0";
 $recomendacion2="0";
 if ($sw=="buscar"){
  $sqlb="select * from m02005 where numero = '$numero' ;";
  $resb=mysql_query($sqlb,$con);
  if (mysql_num_rows($resb)>0){ 
   $sw="encontrado";
   $sw2="encontrado";
   while ($filab = mysql_fetch_row($resb)){
    $cod_tipo_d=$filab[0];
	$numero=$filab[1];
	$fecha=$filab[2];
	$usuario=$filab[3];
	$nombre=$filab[4];
	$neto=$filab[13];
	$iva=$filab[14];
	$total=$filab[15];
	$codtipod=$filab[16];
	$banco=$filab[17];
	$cheque=$filab[18];
	$cheque=$filab[18];
	
	$COD_TIPO_D=$filab[2];
	
	
    $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>
<TITLE>Maestro Caja</TITLE>
<script language="javascript" src="fstandar.js"></script>
<script language="javascript">

function carga_datos(){
 if (event.keyCode==13){
  document.form1.hdsw.value="buscar";
  document.form1.action="M0205003.php"
  document.form1.method="post";
  document.form1.submit();
 }//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  
}  
</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 Caja :</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 Caja:<br />
                      <br />
                      <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 width="16%" scope="col"><div align="right" class="Emphasize">Tipo:</div></th>
                    <th width="33%" 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 width="15%" scope="col"><div align="right"><span class="Emphasize">Numero:</span></div></th>
                    <th width="36" colspan="2" scope="col"><div align="left">
                      <input name="numero" type="text" id="numero"  value="<?php echo $numerof ?>" size="11" maxlength="10" onkeypress="carga_datos()"/>
                    </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" disabled/>
                    </div></td>
                    <td><div align="right"><span class="Emphasize">Vendedor:</span></div></td>
                    <td colspan="2"><div align="left">
                      <input name="usuario" type="text" id="usuario" value="<?php echo $usuario ?>" size="11" maxlength="10" disabled/>
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize">Nombre:</div>
                        </label></th>
                    <td colspan="4"><div align="left">
                      <input name="nombre" type="text" id="nombre" value="<?php echo $nombre ?>" size="50" maxlength="100" disabled/>
                    </div></td>
                  </tr>
                  <tr>
                    <td scope="row"><label>
                        <div align="right" class="Emphasize"></div>
                        </label></td>
                    <td colspan="4"><div align="left"></div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right" class="Emphasize"></div>
                      </label></th>
                    <td colspan="4"><div align="left"></div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right" class="Emphasize"></div></th>
                    <td colspan="4">
                          <div align="left"></div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label></label></th>
                    <td colspan="4"><div align="left"></div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label></label></th>
                    <td colspan="4"><div align="left">                      <label> </label>
                      <div align="right" class="Emphasize">
                        <div align="left"></div>
                      </div>                    
                       </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><label>
                        <div align="right"><span class="Emphasize">Neto</span>:</div>
                        </label></th>
                    <td colspan="4"><div align="left">
                      <input name="neto" type="text" id="neto" value="<?php echo $neto ?>" size="11" maxlength="10" disabled/>
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right"><span class="Emphasize">IVA</span>:</div></th>
                    <td colspan="4"><div align="left">
                      <input name="iva" type="text" id="iva" value="<?php echo $iva ?>" size="11" maxlength="10" disabled/>
                    </div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right"><span class="Emphasize">Total:</span></div></th>
                    <td colspan="4"><div align="left">
                      <input name="total" type="text" id="total2" value="<?php echo $total ?>" size="11" maxlength="10" disabled/>
</div></td>
                  </tr>
                  <tr>
                    <th scope="row"><div align="right"><span class="Emphasize">Cond.Pago:</span></div></th>
                    <td colspan="2"><div align="left">
					<select name="cpago" id="cpago" onchange="ponecpago()" disabled>
					<option value='00'>Seleccione una</option>
				    <?php
					   $sqlcp="select * from m01002 order by 2;";
					   $rescp=mysql_query($sqlcp,$con);
					   while ($filacp=mysql_fetch_array($rescp)){ 					  
					    echo "<option value=$filacp[0]>$filacp[1]</option>";
						}
					  ?>
                      </select>
                    <input name="hdtipod" type="hidden" id="hdtipod" value="<?php echo $codtipod?>"/>
                    </div></td>
                    <td><div align="right"><span class="Emphasize">&nbsp;&nbsp;&nbsp;&nbsp;N.cheq. </span></div>                      <span class="Emphasize"><label></label>
                    </span></td>
                    <td><input name="cheque" type="text" disabled="disabled" id="cheque" size="15" maxlength="15" onkeydown="ponecheque()"/></td>
                  </tr>
                  <tr>
                    <th scope="row">&nbsp;&nbsp;
                      <div align="right"></div>
                      <div align="right"><span class="Emphasize">Banco:</span></div></th>
                    <td colspan="2"><div align="left">
                      <input name="banco" type="text" disabled="disabled" id="banco" size="25" maxlength="15"  onkeydown="ponebanco()"/>
                    </div></td>
                    <td><div align="right"><span class="Emphasize">&nbsp;Fecha. </span></div>                      <span class="Emphasize"><label></label>
                    </span></td>
                    <td><input name="fechac" type="text" disabled="disabled" id="fechac" size="15" maxlength="15" /></td>
                  </tr>
                  <tr>
                    <th colspan="5" scope="row">&nbsp;</th>
                  </tr>
                  <tr>
                    <th colspan="5" scope="row"><input name="cmdgra" type="button" id="cmdgra" value="Grabar" onclick="valida()" />
                      <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>
	   <script language="JavaScript" src="menu.js"></script>
<script language="JavaScript" src="menu_tpl.js"></script>
<script language="JavaScript">
	new menu (MENU_ITEMS, MENU_POS);
</script>
	  </TABLE>
	</form>  
	</BODY>
<?php
	mysql_close($con);
?>
</HTML>