<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Ajustes_de_inventario.xls");

 include ("../conexion.php");	

       $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $Razon          =  $row_para["RAZSOC"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
  


	 $fecini    = $_GET["fechainicio"];
	 $fecfin    = $_GET["fechafin"];
	 $cdgtipaju = $_GET["cdgtipaju"];
	 $accaju    = $_GET["accaju"];
	 $numaju    = $_GET["numaju"];
	
	 

	 
	 
	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
    $where_notas   = "";
    $where         = "";
	$exento_fac    = 0;
	$gravado_fac   = 0;
	$subtotal_fac  = 0;
	$descuento_fac = 0;
	$impuesto_fac  = 0;
	$total_fac     = 0;	 
	$exento        = 0  ;
	$gravado       = 0 ;
	$subtotal      = 0 ;
	$descuento     = 0  ;
	$impuesto      = 0 ;
	$total         = 0  ;
	
	
		 	 /////////////////        where para facturas  y notas ********************


 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 



$where="1=1";
if ($numaju <> "") { $where.=" AND ajustes_inventario.numaju='$numaju'"; }
if ($cdgtipaju <> "") { $where.=" AND ajustes_inventario.cdgtipaju='$cdgtipaju'"; }
if ($accaju <> "") { $where.=" AND tipo_ajuste.accaju='$accaju'"; }


if (($fechainicio<>"") and ($fechafin<>"")) {
	 $where.=" AND ajustes_inventario.fecaju between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND ajustes_inventario.fecaju >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND ajustes_inventario.fecaju <='".$fechafin."'";
		}
	}
}



 if ($cdgtipaju=="") {
	 
	 $nomajuste="TODOS LOS AJUSTES";
	 
	     } else {


	   $query_codcaj="SELECT * FROM tipo_ajuste  WHERE cdgtipaju='$cdgtipaju' ";
	   
	
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomajuste    = $row["dscaju"];
     }
		
		
$totalfacturas=0;
  
if ($result = $mysqli->query("SELECT * FROM ajustes_inventario WHERE  ".$where)) {
   $filas = $result->num_rows;
   $result->close();
}
					
?>
<!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=utf-8" />

<title>Reporte Ajustes de Inventario</title>
<style type="text/css">
<!--
.Estilo1 {color: #CCCCCC}
.Estilo4 {font-size: medium}
.Estilo6 {
	color: #000000;
	font-weight: bold;
}
.Estilo17 {color: #000000; font-weight: bold; font-size: 14px; }
-->
</style>
</head>
<body>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><div align="center"></div></td>
    <td colspan="4"  ><div align="center"><font size=8 ><?php echo $NombreComercial?></font></div>
        <div align="center"></div>
      <div align="center"></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="4" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
  </tr>
  <tr>
    <td><div align="center"></div></td>
    <td colspan="4" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
  </tr>
  <tr>
    <td><div align="center"></div></td>
    <td colspan="4" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
  </tr>
  <tr>
    <td><div align="center"></div></td>
    <td colspan="4" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
  </tr>
  <tr>
    <td><span class="Estilo1"></span></td>
    <td colspan="4" ><span class="Estilo1"></span></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="4"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Ajuste de Inventario</font> </strong></div></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="4"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> - <?php echo $nomajuste; ?></font></div></td>
  </tr>
  <tr>
    <td width="5%"></td>
    <td width="24%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>N. AJUSTE</strong></span></td>
    <td width="12%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA</strong></span></td>
    <td width="50%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>DETALLE</strong></span></td>
    <td width="9%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>MONTO</strong></span></td>
  </tr>
  <?PHP
	
				$cantidadfam=0; 
				$importefam=0;
				$mondesfam=0;
				$impuestofam=0;
				$totalfam=0;
				$subexefam=0;
				$subgrafam=0;
				
				$subtotalajuste=0;
				


			
 $consulta2="SELECT SUM(totalajuste) AS totalajuste, SUM(subtot) AS subtot, SUM(impven) AS impven, ajustes_inventario.cdgtipaju, ajustes_inventario.borrado,ajustes_inventario.numaju,ajustes_inventario.detaju ,ajustes_inventario.fecaju , tipo_ajuste.dscaju FROM ajustes_inventario, tipo_ajuste WHERE   ajustes_inventario.cdgtipaju=tipo_ajuste.cdgtipaju AND ajustes_inventario.borrado=0   AND ".$where. " GROUP BY  ajustes_inventario.codaju   ORDER BY  ajustes_inventario.cdgtipaju ASC " ;
 
				  		
		
		if ($resultado3 = $mysqli->query($consulta2)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	$cdgtipaju_actual = $dato['cdgtipaju'];

	   } 
		

      
	    $contador=0;	
		if ($resultado2 = $mysqli->query($consulta2)) 
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos
				$fecha      = $dato1['fecaju'];
 				$fecfac= implode("-", array_reverse(explode("-", $fecha))); 	
				$fecha2      = $dato1['fecaju'];
				$fecfac= implode("-", array_reverse(explode("-", $fecha2))); 
				$codigo     = $dato1['numaju'];
	            $cdgtipaju     = $dato1['cdgtipaju'];
				$dscaju     = $dato1['dscaju'];
				$detaju     = $dato1['detaju'];



				if ($cdgtipaju_actual==$cdgtipaju) {   // si el codigo de famiia es distinto al siguiente
				

				$totalajuste   = $dato1['totalajuste'];

				
				$subtotalajuste=$subtotalajuste+$totalajuste ;

		
	
	  } 	    //  cierra codigo de familia distinto 	
	

          if ($contador==0){ 
?>
  <tr>
    <td></td>
    <td><strong> <font size=2 ><?php echo $cdgtipaju." - ".$dscaju ?></font></strong></td>
    <td><font size=2 ></font></td>
    <td><font size=1 align="right"></font></td>
    <td><font size=1 align="left"></font></td>
  </tr>
  <?php
  
       
		 } 
		 

	if ($cdgtipaju <>  $cdgtipaju_actual  ){ 
				
			 $cdgtipaju_actual = $dato1['cdgtipaju'];
				 
?>
  <tr> </tr>
  <tr>
    <td></td>
    <td align="left"><strong><font size=1 >Subtotal Ajuste</font></strong></td>
    <td align="left"><strong><font size=1 >&nbsp;</font></strong></td>
    <td align="center"><strong><font size=1 >
      <?php //echo number_format($cantidadfam, 2) ?>
    </font></strong></td>
    <td align="right"><strong><font size=1 ><?php echo number_format($subtotalajuste,2)?></font></strong></td>
  </tr>
  <tr> </tr>
  <tr>
    <td></td>
    <td align="center">&nbsp;</td>
    <td align="left">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
  <tr> </tr>
  <tr>
    <td width="5%"></td>
    <td width="24%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>N. AJUSTE</strong></span></td>
    <td width="12%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA</strong></span></td>
    <td width="50%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>DETALLE</strong></span></td>
    <td width="9%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>MONTO</strong></span></td>
  </tr>
  <tr>
    <td></td>
    <td><strong> <font size=2 ><?php echo $cdgtipaju." - ".$dscaju ?></font></strong></td>
    <td><font size=2 ></font></td>
    <td><font size=1 align="right"></font></td>
    <td><font size=1 align="left"></font></td>
  </tr>
  <?php	
				$cantidadfam =0;
				$importefam  =0;
				$mondesfam   =0;
				$impuestofam =0;
				$totalfam    =0;
				$total       =0;
				$subexefam   =0;
				$subgrafam   =0;
				$subtotalajuste=0;

				
				$totalajuste   = $dato1['totalajuste'];
				

				
				$subtotalajuste=$subtotalajuste+$totalajuste ;
			

	 } 		   
 ?>
  <tr>
    <td></td>
    <td align="left"><font size=1 ><?php echo "N.".$dato1["numaju"] ?></font></td>
    <?php	  //$dscart = substr($dato1["dscart"], 0,40);  ?>
    <td align="left"><font size=1 ><?php echo $fecfac ?></font></td>
    <td align="center"><font size=1 ><?php echo $detaju ?></font></td>
    <td align="right"><font size=1 ><?php echo number_format($totalajuste,2)?></font></td>


  </tr>
  <?php
      $contador++;
  	        
		 } 
 ?>
  <tr>
    <td></td>
    <td align="left"><strong><font size=1 >Subtotal Ajuste</font></strong></td>
    <td align="left"><strong><font size=1 >&nbsp;</font></strong></td>
    <td align="center"><strong><font size=1 >
      <?php //echo number_format($cantidadfam, 2) ?>
    </font></strong></td>
    <td align="right"><strong><font size=1 ><?php echo number_format($subtotalajuste,2)?></font></strong></td>


  </tr>
  <tr> </tr>
  <tr>
    <td></td>
    <td align="center">&nbsp;</td>
    <td align="left">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>

  </tr>
  <tr> </tr>
  <?php	 
  



 $consulta2=("SELECT SUM(totalajuste) AS totalajuste, SUM(subtot) AS subtot, SUM(impven) AS impven, ajustes_inventario.cdgtipaju, ajustes_inventario.borrado,ajustes_inventario.numaju,ajustes_inventario.detaju ,ajustes_inventario.fecaju , tipo_ajuste.dscaju FROM ajustes_inventario, tipo_ajuste WHERE   ajustes_inventario.cdgtipaju=tipo_ajuste.cdgtipaju AND ajustes_inventario.borrado=0    AND ".$where );

 
	if ($resultado2 = $mysqli->query($consulta2)) 

  	 while ($dato = $resultado2->fetch_assoc()) {
  						
        //$importe   = $dato["importe"];
       // $mondes    = $dato["mondes"];
        //$impuesto  = $dato["impuesto"];
		$totalajuste  = $dato["totalajuste"];
					
      //  $total=$importe - $mondes + $impuesto;
	

		$totalajuste = number_format($totalajuste,2);
	
	} 				
  
  ?>
</table>
<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
    <td width="1%"></td>
    <td width="20%"  align="center"bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
  </tr>
 <tr>
    <td width="1%"></td>
    <td width="20%"  align="left" bgcolor="" ><span class="Estilo4"><strong>Total</strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong></strong></span></td>
    <td width="20%" align="center" bgcolor="" ><span class="Estilo4"><strong> <?php echo $totalajuste?> </strong></span></td>
  </tr>
</table>






</body>
</html>