<?php
 session_start(); 
 $m1="DOCUMENTO ANULADO CORRECTAMENTE";
 $m2="STOCKS CORREGIDOS";
 require ("conecta.php");
 $tipo=$_GET['tipo']; 
 //$numero=sprintf("%010d",$_GET['numero']); 
 $numero=$_GET['numero'];
 $sqlb="select estado from m02005 where estado='02' and cod_tipo_d='$tipo' and numero='$numero';";
 $resb=mysql_query($sqlb,$con);
 if (mysql_num_rows($resb)>0){
  $sw="no";
  $m1="EL DOCUMENTO YA ESTA ANULADO";
  $m2="NO SE HA PODIDO REALIZAR LA OPERACION";
 }//fin si 
 //echo $tipo . $numero . "<br>";
 if ($sw !="no"){//si principal
 //*******************************************************************************
 //******************marcando el encabezado de la factura*************************
 $sqle="update m02005 set estado='02',nombre='ANULADA' where cod_tipo_d='$tipo' and numero='$numero' ;";
 $rese=mysql_query($sqle,$con)or die(mysql_error());
 //********************************************************************************
 //echo $sqle;
 
 
 //*********************************************************************************
 //********************RECUPERANDO EL STOCK*****************************************
 $cod="";
 $cant=0;
 $llave=$tipo . $numero;
 $sqld="select * from m02007 where llave like '$llave%';";
 $resd=mysql_query($sqld,$con); //or die(mysql_error());
 while ($filad=mysql_fetch_array($resd)){
  $cod=$filad[2];
  $cant=$filad[1];
  //echo $cant . "<br>";
  $resstock=mysql_query("select stock from m07005 where cod_interno='$cod';",$con);
  while ($filastock=mysql_fetch_array($resstock)){
   $stock=$filastock[0];
  }//wend
  $stockf=$stock + $cant;
  $sqls="update m07005 set stock = '$stockf' where COD_INTERNO='$cod'";
  $ress=mysql_query($sqls,$con) or die(mysql_error());   
 }//wend 
 //*********************************************************************************
 //echo "sqlf" . $sqls;
 }//fin si principal
 mysql_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="">
  <table width="724" border="0">
    <tr>
      <td><div align="center"><strong>-<?php echo $m1 ?> </strong></div></td>
    </tr>
    <tr>
      <td><div align="center"><strong>-<?php echo $m2 ?> </strong></div></td>
    </tr>
    <tr>
      <td><div align="center"><strong><a href="javascript:window.close();">CERRAR</a></strong></div></td>
    </tr>
  </table>
</form>
</body>
</html>
