<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Condicion_Pago.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"];
	   
  

 
		$cdgcaj      =$_GET["cdgcaj"];
		$cdgtipven   =$_GET["combotipoventa"];
		$fecini    = $_GET["fechainicio"];
		$fecfin    = $_GET["fechafin"];
		$mediopago    =$_GET["combotipopago"];

	 
	 
	$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  ;
	
 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 


$where  = "";

$venta="";

     if ($cdgtipven == "") {$venta="TODAS";}
else if ($cdgtipven == "1") {$venta="CONTADO";}
else if ($cdgtipven == "2")  {$venta="CREDITO";}


     if ($mediopago == "EFECTIVO") {$where.=" AND efectivo>0";}
else if ($mediopago == "TARJETAS") {$where.=" AND tarjetas>0";}
else if ($mediopago == "DOLARES")  {$where.=" AND dolares>0";}
else if ($mediopago == "DEPOSITOS")  {$where.=" AND depositos>0";}
else if ($mediopago == "SINPES"){$where.=" AND simpes>0";}



if ($cdgcaj <> "") { $where.=" AND cdgcaj='$cdgcaj'"; }
if ($cdgtipven <> "") { $where.=" AND cdgtipven='$cdgtipven'"; }

//if ($combotipopago <> "") { $where.=" AND cdgtipven='$combotipopago'"; }

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

 $where.=" ORDER BY numfactura DESC";


 if ($cdgcaj=="") {
	 
	 $nomcomusu="TODOS LOS CAJEROS";
	 
	     } else {


	   $query_codcaj="SELECT * FROM usuarios  WHERE codusu=$cdgcaj ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["nomcomusu"];
     }
					
?>
<!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 Modo de Pagos </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="11"  ><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="11" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="11" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="11"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Analitico de Proformas</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="11"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> -  <?php echo $nomcomusu; ?></font></div></td>
     </tr>

  <tr>
       <td></td>
       <td colspan="11"   border="1"><div align="center"><font size="3" >MEDIO DE PAGO <?php  echo $mediopago; ?> - <?php    echo $venta; ?></font></div></td>
     </tr>


  <tr>
	 
  <tr>
      <td width="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>Factura</strong></font></span></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>Cliente</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>Efectivo</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Tarjetas</strong></font></span></td> 
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Dolares </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>depositos </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sinpes </strong></font></span></td>

        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		<td width="20%" align="center" ><span class="Estilo4"><strong></strong></font></span></td>
	
   
  </tr>
  
<?PHP
			  
		$subefectivo = 0;
		$subtargetas = 0;
		$subdolares  = 0;
		$subdepositos  = 0;
		$subsimpes = 0;
		$subfactura = 0;
		$tipopago ="";
 

	    $consulta= "SELECT * FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'   ".$where;

if ($resultado = $mysqli->query($consulta)) 

    while ($fila = $resultado->fetch_assoc()) {
        $marcaestado   = $fila["estado"];
        $marcaborrado  = $fila["borrado"];
        $fecha         = $fila["fecha"];
		$fecha         = implode("-", array_reverse(explode("-", $fecha)));
		$numfactura    = $fila["numfactura"];
		$numfactura    = substr($numfactura, -10);
		$cdgcli        = $fila["cdgcli"];
		$dsccli        = $fila["dsccli"];
		$totalfactura  = $fila["totalfactura"];
		$conventa	   = $fila["conventa"];
		$horfac        = $fila["horfac"];
		
		$efectivo      = $fila["efectivo"];
		$tarjetas      = $fila["tarjetas"];
		$dolares       = $fila["dolares"];
		$depositos       = $fila["depositos"];
		$simpes     = $fila["simpes"];
		
				//$subefectivo = $efectivo + $subefectivo  ;
				$subtargetas = $tarjetas + $subtargetas  ;
				$subdolares =$dolares + $subdolares  ;
				$subdepositos  = $depositos + $subdepositos   ;
				$subsimpes =  $simpes + $subsimpes ;
				
				$subfactura = $totalfactura + $subfactura;
				
//echo $subefectivo;

/*si la factura es de contado***/		
if ($conventa==01){
		$efectivo=$totalfactura-$tarjetas-$dolares-$depositos-$simpes;
		
		$subefectivo=$efectivo+$subefectivo;
		
		
}
		
		

?>  

 <tr>
   <td></td>
   <td align="left"><font size=1 ><?php echo $numfactura?></font></td>
   <td align="center"><font size=1 ><?php echo $cdgcli?></font></td>
   <td align="left"><font size=1 ><?php echo $dsccli?></font></td>
   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="center"><font size=1 ><?php echo number_format($efectivo,2,".",",")?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($tarjetas,2,".",",")?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($dolares,2,".",",") ?></font></td>
   <td align="right"><font size=1 ><?php echo  number_format($depositos,2,".",",") ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($simpes,2,".",",")?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($totalfactura,2,".",",") ?></font></td>

    
 </tr>
  
  <?php
	 
  	}	        
	
		 
	/*	 
  
      $consulta2= ("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(destot) AS descuento , SUM(impven) AS impuesto, SUM(efectivo) AS efectivo ,SUM(tarjetas) AS tarjetas ,SUM(dolares) AS dolares , SUM(depositos) AS depositos ,SUM(simpes) AS simpes  FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'  ".$where );

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

  	 while ($dato = $resultado2->fetch_assoc()) {

				$exento_fac    = $dato['exento'];
				$gravado_fac   = $dato['gravado'];
				$subtotal_fac  = $dato['subtotal'];
				$descuento_fac = $dato['descuento'];
				$impuesto_fac  = $dato['impuesto'];
				$total_fac     = $dato['total'];
				
				
		$efectivo      = $dato["efectivo"];
		$tarjetas      = $dato["tarjetas"];
		$dolares       = $dato["dolares"];
		$depositos       = $dato["depositos"];
		$simpes     = $dato["simpes"];
	 } 		
  */
  ?>
  
</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 >TOTALES </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="right"><strong><font size=1 ><?php echo number_format($subefectivo , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subtargetas , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subdolares, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subdepositos , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subsimpes , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subfactura , 2); ?></font></strong></td>
 </tr>
</table>
<?php
	
	
	
	


?>
    <?php
	
	
/*
				$exento = $exento_fac - $exento_not +  $exento_deb  ;
				$gravado = $gravado_fac - $gravado_not + $gravado_deb  ;
				$subtotal =$subtotal_fac - $subtotal_not + $subtotal_deb ;
				$descuento  = $descuento_fac - $descuento_not + $descuento_deb   ;
				$impuesto =  $impuesto_fac - $impuesto_not +  $impuesto_deb ;
				$total = $total_fac - $total_not +  $total_deb ;*/
  ?>
  
  
  




</body>
</html>

 <?php