<?php require_once('../Connections/cn_conexion.php'); ?>
<?php include_once("../includes/funciones.php");?>
<?php include_once("../includes/numerosToletras.php");?>
<?php
$id=$_GET['id_documento'];
$tipodoc=$_GET['tipodoc'];
switch($tipodoc){
	case "f":
		$nom_doc="Factura";
		$type="text";
		$nomigv="%IGV";
		break;
	case "b":
		$nom_doc="Boleta";
		$type="hidden";
		$nomigv="";
		break;
	}

if (!isset($_SESSION)) {
  session_start();
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_documento = "SELECT * FROM documento, clientes WHERE documento.id_documento='".$id."' AND clientes.id_cliente=documento.id_cliente ";
$rs_documento = mysql_query($query_rs_documento, $cn_conexion) or die(mysql_error());
$row_rs_documento = mysql_fetch_assoc($rs_documento);
$totalRows_rs_documento = mysql_num_rows($rs_documento);

mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_cliente = "SELECT * FROM clientes WHERE clientes.hab_cliente='1' ORDER BY clientes.nom_cliente ASC";
$rs_cliente = mysql_query($query_rs_cliente, $cn_conexion) or die(mysql_error());
$row_rs_cliente = mysql_fetch_assoc($rs_cliente);
$totalRows_rs_cliente = mysql_num_rows($rs_cliente);

$compra=$_SESSION["compra"];
$venta=$_SESSION["venta"];
$igv=$_SESSION["igv"];

?>
<?php
$_SESSION["iddocprint"]=$row_rs_documento['id_documento'];
?>
<form id="form2" name="form2" method="POST" >
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td height="30" colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><p class="rot_man">&nbsp;&nbsp;&nbsp;&nbsp;Modificar <?php echo($nom_doc);?>
                      <input name="id_documento" type="hidden" id="id_documento" value="<?php echo $row_rs_documento['id_documento']; ?>" />
                    </p></td>
                    <td><p class="rot_man"><a href="nuevo_documento.php?tipo_doc=<?php echo($tipodoc);?>"  class="enlace_blanco">Nuevo</a></p></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td width="54%" height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td>Número</td>
                  </tr>
                  <tr>
                    <td><input name="num_documento" type="text" class="fn_text" id="num_documento" value="<?php echo $row_rs_documento['num_documento']; ?>" size="20" />
                    <input name="tipo_documento" type="hidden" id="tipo_documento" value="<?php echo($tipodoc);?>" />
                    <input name="numdocumento" type="hidden" id="numdocumento" value="<?php echo $row_rs_documento['num_documento']; ?>" /></td>
                  </tr>
                </table></td>
                <td width="45%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td>Emisión</td>
                  </tr>
                  <tr>
                    <td><input name="fecha_emision" type="text" class="fn_text" id="fecha_emision" value="<?php echo(fecha_peru($row_rs_documento['fecha_emision']))?>" size="20" onfocus="limpiar_fecha(this.value,'fecha_emision')"   onblur="analizar_fecha(this.value,'fecha_emision')" onkeyup="valid_fecha(this.value,'fecha_emision')"/></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td>Cliente</td>
                  </tr>
                  <tr>
                    <td><div id="select_cli">
                      <input name="cliente" type="text" class="fn_text" id="cliente" value="<?php echo $row_rs_documento['nom_cliente']; ?>" size="65" onkeypress="buscar_clientes(this.value)" />
                      <input type="button" name="button" id="button3" value="..." onclick="buscar_clientes('')"/>
                      <input name="idcliente" type="hidden" id="idcliente" value="<?php echo $row_rs_documento['id_cliente']; ?>" />
                    </div></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td width="1%">&nbsp;</td>
                <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                      <?php 
					  if($row_rs_documento['detracc']=='1'){
							$detraccion=$row_rs_documento['detraccion'];
							$estilo="block";
							$check='checked="checked"';
						  }else{
							$detraccion="\"SUJETA A DETRACCIÓN 10% RESOLUCIÓN SUNAT Nº 183-2004 \nCUENTA BANCO DE LA NACIÓN Nº 000-364495\"";	
							$estilo="none";
							$check='';
						  }
					  ?>
                        <td>Concepto</td>
                        <td><?php if($tipodoc=='f'){?>
                          <p>
                            <input name="checkbox" type="checkbox" id="checkbox" onclick="detraccion_d()"  <?php echo($check);?>/>
                            Detracción</p>
                        <?php }?></td>
                      </tr>
                    </table></td>
                  </tr>
                  <tr>
                    <td><textarea name="concepto" cols="80" rows="3" class="fn_text" id="concepto"><?php echo $row_rs_documento['concepto']; ?></textarea>
                    <input name="detracc" type="hidden" id="detracc" value="<?php echo($row_rs_documento['detracc']);?>" />
                      <textarea name="detraccion" cols="80" class="fn_text" id="detraccion" style="display:<?php echo($estilo);?>"><?php echo($detraccion);?></textarea>
                    </td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>Monto</td>
                      </tr>
                      <tr>
                        <td><input name="monto" type="text" class="fn_text" id="monto" value="<?php echo $row_rs_documento['monto']; ?>" size="10" /></td>
                      </tr>
                    </table></td>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><?php echo($nomigv);?></td>
                      </tr>
                      <tr>
                        <td><input name="igv" type="<?php echo($type);?>" class="fn_text" id="igv" value="<?php echo $row_rs_documento['igv']; ?>" size="10" /></td>
                      </tr>
                    </table></td>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>Moneda</td>
                      </tr>
                      <tr>
                        <td><select name="moneda" class="fn_text" id="moneda">
                <option value="S" <?php if (!(strcmp("S", $row_rs_documento['moneda']))) {echo "selected=\"selected\"";} ?>>Soles</option>
                <option value="D" <?php if (!(strcmp("D", $row_rs_documento['moneda']))) {echo "selected=\"selected\"";} ?>>Dolares</option>
              </select></td>
                      </tr>
                    </table></td>
                  </tr>
                  <tr>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>Cancelado</td>
                      </tr>
                      <tr>
                        <td><input name="fecha_cancelado" type="text" class="fn_text" id="fecha_cancelado" value="<?php echo fecha_peru($row_rs_documento['fecha_cancelado']); ?>" size="20" onfocus="limpiar_fecha(this.value,'fecha_cancelado')"   onblur="analizar_fecha(this.value,'fecha_cancelado')" onkeyup="valid_fecha(this.value,'fecha_cancelado')"/>
                        <input <?php if (!(strcmp($row_rs_documento['hab_documento'],0))) {echo "checked=\"checked\"";} ?> name="hab_documento" type="checkbox" id="hab_documento" value="0" /></td>
                      </tr>
                    </table></td>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>Tipo de Cambio</td>
                      </tr>
                      <tr>
                        <td><input name="tipo_cambio" type="text" class="fn_text" id="tipo_cambio" value="<?php echo $row_rs_documento['tipo_cambio']; ?>" size="10" /></td>
                      </tr>
                    </table></td>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>Anulado</td>
                      </tr>
                      <tr>
                        <td><input <?php if (!(strcmp($row_rs_documento['anulado'],"1"))) {echo "checked=\"checked\"";} ?> name="anulado" type="radio" id="radio" value="1" />
Si&nbsp;&nbsp;
<input name="anulado" type="radio" id="radio2" value="0" <?php if (!(strcmp($row_rs_documento['anulado'],"0"))) {echo "checked=\"checked\"";} ?> />
No </td>
                      </tr>
                    </table></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td height="30" colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td height="30">&nbsp;</td>
                <td align="right"><input type="button" name="button4" id="button4" onclick = "imprSelec('<?php echo $row_rs_documento['id_documento']; ?>')" value="Imprimir" />
                  <input type="submit" name="button2" id="button2" onclick = "this.form.action = 'documento.php'" value="Actualizar" />
&nbsp;&nbsp;                &nbsp;&nbsp;&nbsp;</td>
              </tr>
            </table>
          <input type="hidden" name="MM_update" value="form2" />
</form>

<?php
mysql_free_result($rs_documento);

mysql_free_result($rs_cliente);

?>
