<?php
 include ("otro_numeroapalabras.php");
 
 //------INI-Coneccion Local
 require("conecta.php");
 //-----INI-Coneccion Local
 
$numero1=sprintf("%010d",$_POST['hddoc']);
//$anno=(int)substr(date("Ymd"),1,3); 
//$mes=date('m');
$meses = array("ENERO","FEBRERO","MARZO","ABRIL","MAYO","JUNIO","JULIO","AGOSTO", "SEPTIEMBRE", "OCTUBRE","NOVIEMBRE","DICIEMBRE");

//consulta encabezado
$sqlc="SELECT * from m02005 where NUMERO like '%$numero1';";
 $resc=mysql_query($sqlc,$con) or die(mysql_error());
 if (mysql_num_rows($resc)>0){
  while($fila=mysql_fetch_row($resc)){//while
   $numerok=$fila[1];
   $fecha=$fila[2];
   $anno=substr($fecha,0,4);
   $mes=substr($fecha,4,2);
   $dia=substr($fecha,6,2);
   $nombre=$fila[5];
   $direccion=$fila[6];
   $largorut=strlen($fila[4]);
   $rutnum = substr($fila[4],0,$largorut-1);
   $dv=substr($fila[4],$largorut-1,1);
   $rut=$rutnum . "-" .$dv;
   $d1=$fila[10];
   $cod_comuna=$fila[7];
   $fono=$fila[22];
   $ciu=$fila[23];
   $giro=$fila[8];
   $sql2="SELECT * from m01003 where codcomu='$cod_comuna';";
   $res2=mysql_query($sql2,$con) or die(mysql_error());
   if (mysql_num_rows($res2)>0){
    while($fila2=mysql_fetch_row($res2)){//while
	 $glosacom=$fila2[1];
	 }
    }else{
   	 $glosacom="";
   }
   //*********************ciudad*****
   //********************************
   $sql2="SELECT * from m01003 where codcomu='$ciu';";
   $res2=mysql_query($sql2,$con) or die(mysql_error());
   if (mysql_num_rows($res2)>0){
    while($fila2=mysql_fetch_row($res2)){//while
	 $glosaciu=$fila2[1];
	 }
    }else{
   	 $glosaciu="";
   }
   //****fin ciudad*************
   
   $cpago=$fila[16];
   $sql3="SELECT * from m01002 where CODCONPA='$cpago';";
   $res3=mysql_query($sql3,$con) or die(mysql_error());   
   if (mysql_num_rows($res3)>0){
    while($fila3=mysql_fetch_row($res3)){//while
	 $glosaconpa=$fila3[1];
	 }
    }else{
   	 $glosaconpa="";
   }
   $neto=$fila[13];	
   $iva=$fila[14];
   $total=$fila[15];
  }//while
  
 }
 
 //consulta detalle
 $sqldet="SELECT * from m02007 where llave like '%$numero1%';";
 $resdet=mysql_query($sqldet,$con) or die(mysql_error());
 $cont=0;
 if (mysql_num_rows($resdet)>0){
  while($filadet=mysql_fetch_row($resdet)){//while
   $cantidad=$filadet[1];
   $glosadet=$filadet[3];
   $precio_v=$filadet[4] ;
   $stotal=$filadet[5];
   $cont++;
  }//while
  
 }
 //pagar docuemnto
 $sqlpag="update m02005 set estado='03' where numero = '$numerok';";
 $respag=mysql_query($sqlpag,$con) or die(mysql_error());
 //fin pagar docuemnto
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style type="text/css">
<!--
.Estilo1 {font-size: 9px}
.Estilo2 {color: #FFFFFF}
-->
</style>
<script language="javascript">
 function imprime(){
  window.print();
  window.close();
  opener.salir();
 }
</script>

</head>

<body  onload="imprime()">
<form id="form1" name="form1" method="post" action="m0205001.php">
  <table width="671" border="0" cellpadding="0" cellspacing="0">

    <tr>
      <td width="13">&nbsp;</td>
      <td width="16">&nbsp;</td>
      <td width="13">&nbsp;</td>
      <td width="13">&nbsp;</td>
      <td width="13">&nbsp;</td>
      <td width="13">&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td width="107">&nbsp;</td>
      <td colspan="2"><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp; </td>
      <td>&nbsp;</td>
      <td colspan="2"><span class="Estilo1"><?php echo $numero1 ?></span></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    
    <tr>
      <td height="35">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp; </td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp; </td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td height="31">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="3"><div align="left"><?php echo $dia ?></div></td>
      <td><div align="left"><?php echo strtoupper($meses[($mes-1)]) ?></div></td>
      <td><div align="left"><?php echo $anno ?></div></td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td height="32">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4"><div align="left"><?php echo strtoupper($nombre) ?></div></td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4"><div align="left"><?php echo strtoupper($direccion) ?></div></td>
      <td>&nbsp;</td>
      <td width="4">&nbsp;</td>
      <td width="86"><div align="right"><?php echo $rut ?></div></td>
    </tr>
    <tr>
      <td height="32">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="left"><?php echo strtoupper($glosacom) ?></div></td>
      <td width="39">&nbsp; </td>
      <td width="236"><div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo strtoupper($glosaciu) ?></div></td>
      <td colspan="3"><div align="left"><?php echo strtoupper(substr($giro,0,13)) ?></div></td>
    </tr>
    <tr>
      <td height="40">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="left"><?php echo $fono; ?></div></td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="center"><?php echo strtoupper($glosaconpa) ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td width="73">&nbsp;</td>
      <td colspan="3">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="3">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="3">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
	<?php
	 //consulta detalle
     $sqldet="SELECT * from m02007 where llave like '%$numero1%';";
     $resdet=mysql_query($sqldet,$con) or die(mysql_error());
     //$cont=0;
	 $filas=mysql_num_rows($resdet);
     if (mysql_num_rows($resdet)>0){
	  while($filadet=mysql_fetch_row($resdet)){//while
	   $cantidad=number_format($filadet[1],0,".",",");
	   $glosadet=strtolower($filadet[3]);
	   $glosadet= substr($glosadet,0,30);
	   $precio_v=number_format(round($filadet[4]/1.19),0,".",",");
	   $totalito= round($filadet[4]/1.19)* $filadet[1];
	   $sumaneto=$sumaneto + $totalito;
	   $stotal=number_format($totalito,0,".",",");
	   echo "<tr>";	
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>$cantidad</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   $glosadet=strtoupper($glosadet);
	   echo "<td colspan='3'><div align='left' size='10'>$glosadet</div></td>";
	   echo "<td><div align='right'>$precio_v</div></td>";
	   echo "<td colspan='2'><div align='right'>$stotal</div></td>";
	   echo "</tr>";
	  }//while
	   //**************************************************
	   //****informamos el descuento.
	   $descuento=$sumaneto - $neto;
	   $descuentoe=number_format($descuento,0,".",",");
	   echo "<tr>";	
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td>&nbsp;</td>";
	   echo "<td colspan='3'><div align='left'>Descuento=$descuentoe</div></td>";
	   echo "<td><div align='right'>&nbsp;</div></td>";
	   echo "<td colspan='2'><div align='right'>&nbsp;</div></td>";
	   echo "</tr>"; 
	   //**********************************************
	   //**************fin de informe de descuento
     }
	 while($filas <	17){//while
	  echo "<tr>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td colspan='3'>&nbsp;</td>";
      echo "<td>&nbsp;</td>";
      echo "<td colspan='2'>&nbsp;</td>";
	  echo "</tr>";
      $filas++;
	 }	
	?>		
      <td><div align="left" class="Estilo2">1</div></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4" rowspan="2" align="left" valign="top"><?php $numero=$total;
	  echo ucfirst(docenumeros($numero));
	  ?></td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td height="26">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    
    <tr>
      <td height="29">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="right"><?php echo number_format($neto,0,".",",") ?></div></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="right"><?php echo number_format($iva,0,".",",") ?></div></td>
    </tr>
    <tr>
      <td height="27">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="right"><?php echo number_format($total,0,".",",") ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="4">&nbsp;</td>
      <td>&nbsp;</td>
      <td colspan="2"><div align="right"></div></td>
    </tr>
  </table>
</form>
</body>
<?php
 mysql_close($con);
?>
</html>
