<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_ajustes_Detallado.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  borrado=0  AND ".$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 Detallado de Ajuste 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="10"  ><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="10" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="10" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="10"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Detallado de Ajuste de Inventario</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="10"   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="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CANTIDAD</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. EXENTO</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. GRAVADO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUBTOTAL</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>DESCUENTO</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>IMPUESTO</strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TOTAL</strong></font></span></td>

  </tr>
  
<?PHP

				$cantidadfam=0; 
				$importefam=0;
				$mondesfam=0;
				$impuestofam=0;
				$totalfam=0;
				$subexefam=0;
				$subgrafam=0;
				

			
 $consulta2="SELECT   ajustes_inventario.cdgtipaju, ajustes_inventario.borrado,ajustes_inventario.numaju,ajustes_inventario.detaju ,ajustes_inventario.fecaju , tipo_ajuste.dscaju,ajustes_inventario_lineas.codigo,ajustes_inventario_lineas.dscart,SUM(ajustes_inventario_lineas.cantidad) AS cantidad,SUM(ajustes_inventario_lineas.preivi) AS preivi,SUM(ajustes_inventario_lineas.importe) AS importe,SUM(ajustes_inventario_lineas.dcto) AS dcto,SUM(ajustes_inventario_lineas.mondes) AS mondes,SUM(ajustes_inventario_lineas.impuesto) AS impuesto FROM ajustes_inventario,ajustes_inventario_lineas, tipo_ajuste WHERE ajustes_inventario.codaju=ajustes_inventario_lineas.codajuste AND ajustes_inventario.cdgtipaju=tipo_ajuste.cdgtipaju AND ajustes_inventario.borrado=0   AND ".$where. " GROUP BY ajustes_inventario.cdgtipaju, ajustes_inventario_lineas.codigo    " ;
 
 
 

				  		
		
		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['codigo'];
	            $cdgtipaju     = $dato1['cdgtipaju'];
				$dscaju     = $dato1['dscaju'];
				$detaju     = $dato1['detaju'];


				if ($cdgtipaju_actual==$cdgtipaju) {   // si el codigo de famiia es distinto al siguiente
				
				$cantidad   = $dato1['cantidad'];
				$importe    = $dato1['importe'];
				$mondes     = $dato1['mondes'];
				$impuesto   = $dato1['impuesto'];
				$SubTotal   = $importe - $mondes ;
						
				
				$total=$importe + $impuesto - $mondes ;
			
				$cantidadfam=$cantidadfam+$cantidad; 
				$importefam=$importefam+$importe;
				$mondesfam=$mondesfam+$mondes;
				$impuestofam=$impuestofam+$impuesto;
				$totalfam=$importefam-$mondesfam+$impuestofam;

	   $subexe =  0;
	   $subgra =  0;
	  
	  	if ($impuesto > 0) {  
		
		$subgra =  $importe;

		 
		 	} else {
			
					$subexe =  $importe;
			}			
						
			$subexefam=$subexefam+$subexe;
			$subgrafam=$subgrafam+$subgra;
				
	
	  } 	    //  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>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></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 Ajustes</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></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($subexefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subgrafam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondesfam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php  echo number_format($impuestofam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalfam,2) ?></font></strong></td>
 </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>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
 </tr>
 
    </tr>
  <tr>
      <td width="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CANTIDAD</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. EXENTO</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. GRAVADO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUBTOTAL</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>DESCUENTO</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>IMPUESTO</strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TOTAL</strong></font></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>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>


      </tr>
		
	  <?php	
				$cantidadfam =0;
				$importefam  =0;
				$mondesfam   =0;
				$impuestofam =0;
				$totalfam    =0;
				$total       =0;
				$subexefam   =0;
				$subgrafam   =0;

				$cantidad   = $dato1['cantidad'];
				$importe    = $dato1['importe'];
				$mondes     = $dato1['mondes'];
				$impuesto   = $dato1['impuesto'];
				$SubTotal   = $importe - $mondes ;
				
				$total=$importe + $impuesto - $mondes ;
			
				$cantidadfam=$cantidadfam+$cantidad; 
				$importefam=$importefam+$importe;
				$mondesfam=$mondesfam+$mondes;
				$impuestofam=$impuestofam+$impuesto;
				$totalfam=$importefam-$mondesfam+$impuestofam;
			
				$subexe  = 0;
				$subgra  = 0;
	
					if ($impuesto > 0) {  
					
	$subgra =  $importe;
		
	
	 	} else {
			
					$subexe =  $importe;
			}			
					 
				$subexefam=$subexefam+$subexe;
				$subgrafam=$subgrafam+$subgra;
	 } 		   
 ?> 
  
  <tr>
   <td></td>
   <td align="left"><font size=1 ><?php echo "cod.".$dato1["codigo"] ?></font></td>
   	<?php	  $dscart = substr($dato1["dscart"], 0,40);  ?>	
   <td align="left"><font size=1 ><?php echo $dscart ?></font></td>
   <td align="center"><font size=1 ><?php echo $dato1["cantidad"] ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subexe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subgra,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($importe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($mondes,2) ?></font></td>
   <td align="right"><font size=1 ><?php  echo number_format($impuesto,2) ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($total,2) ?></font></td>

 </tr>
 
  <?php
      $contador++;
  	        
		 } 
 ?> 		
  
	 </tr>
 <tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Ajustes</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></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($subexefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subgrafam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondesfam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php  echo number_format($impuestofam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalfam,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>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
 </tr>

 
 	<tr>
    </tr>
	<?php	 
  
  // $consulta2= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, factulinea.cdgfam, familias.cdgfam FROM factulinea, familias WHERE borrado=0 AND  factulinea.cdgfam = familias.cdgfam  AND ".$where );
   
   
 $consulta2="SELECT SUM(ajustes_inventario.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,ajustes_inventario_lineas.codigo,ajustes_inventario_lineas.dscart,SUM(ajustes_inventario_lineas.cantidad) AS cantidad,SUM(ajustes_inventario_lineas.preivi) AS preivi,SUM(ajustes_inventario_lineas.importe) AS importe,SUM(ajustes_inventario_lineas.dcto) AS dcto,SUM(ajustes_inventario_lineas.mondes) AS mondes,SUM(ajustes_inventario_lineas.impuesto) AS impuesto FROM ajustes_inventario,ajustes_inventario_lineas, tipo_ajuste WHERE ajustes_inventario.codaju=ajustes_inventario_lineas.codajuste AND 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"];
		$cantidad  = $dato["cantidad"];
					
        $total=$importe - $mondes + $impuesto;
	

		$cantidad = number_format($cantidad,3);
	
	} 				
  
  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

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

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL AJUSTES</font></strong></td>
   <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
      <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importe , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondes, 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total , 2); ?></font></strong></td>
 </tr>
</table>






</body>
</html>