<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=FACTURA_COMPRAS.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"];
	   
	 $cedcli    = $_GET["cedcli"];
	 $fecini    = $_GET["fechainicio"];
	 $fecfin    = $_GET["fechafin"];
	 $cdgtipven = $_GET["combotipoventa"];
	 $cdgcaj    = $_GET["cdgcaj"];
	 $cdgtipocxp    = $_GET["cdgtipocxp"];
	 $combotipomoneda    = $_GET["combotipomoneda"];
	 
	 
	 
	 
	$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 ********************
	 

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

$where="1=1";
//if ($cedcli <> "") { $where.=" AND numcedemi like '%".$cedcli."%'"; }
if ($cedcli <> "") { $where.=" AND x.numcedemi ='".$cedcli."'"; }

if ($cdgtipocxp <> "") { $where.=" AND x.cdgcxp ='".$cdgtipocxp."'"; }



if (($fechainicio<>"") and ($fechafin<>"")) {

 		//	$dias=1;
//
//		$inicio= "-".$dias." day" ;
//			$fechainicio = strtotime ( $inicio , strtotime ( $fechainicio ) ) ;
//		$fechainicio = date ( 'Y-m-d' , $fechainicio );
//
//			$fin= "+".$dias." day" ;
//	 	$fechafin = strtotime ( $fin , strtotime ($fechafin ) ) ;
//			$fechafin = date ( 'Y-m-d' , $fechafin );
			
	 $where.=" AND x.feccorcomp between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND x.feccorcomp>='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND x.feccorcomp<='".$fechafin."'";
		}
	}
}

 if ($cdgtipocxp=="") {
	 
	 $nomcomusu="TODOS LOS COMPROBANTES";
	 
	     } else {


	   $query_codcaj="SELECT * FROM tipo_cxp  WHERE cdgcxp=$cdgtipocxp ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["dsccxp"];
     }
	 
	 
			
?>
<!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 de Comprobantes Aceptados</title>
<style type="text/css">
<!--
.Estilo4 {font-size: medium}
-->
</style>
</head>
<body>




	
	<?php 

if (!isset($_GET["compra"])) { $compra=0; } else  {  $compra=$_GET["compra"]; } 

$xml="../Comprobantes_mensajes/correos/".$compra;

//$xml="correos/".$nombreArch;

    $xml = simplexml_load_file($xml);
	

	$clave     = $xml->Clave;
	$clave     = substr($clave,-19,10);
	$fecemi    = $xml->FechaEmision;
	$cedrec    = $xml->Receptor -> Identificacion ->Numero;
	$dscnomemi        = $xml->Emisor -> Nombre;
	$numcedemi        = $xml->Emisor -> Identificacion -> Numero;
	
	$totcomp   = $xml->ResumenFactura -> TotalComprobante;
	$totimp    = $xml->ResumenFactura -> TotalImpuesto;
	$totdes    = $xml->ResumenFactura -> TotalDescuentos;
	$subtot    = $xml->ResumenFactura -> TotalVenta;
   // print_r($xml);
	
	$subtot1   = floatval($subtot);
	$totdes1   = floatval($totdes);
	$totimp1   = floatval($totimp);
	$totcomp1  = floatval($totcomp);
	
	
	$flete  = '0';
	$otros  = $xml->ResumenFactura ->  TotalOtrosCargos;
	$totcomp   = $xml->ResumenFactura -> TotalComprobante;
	

	$fecha     = substr($fecemi,0,10);
    $fecha     = implode("-", array_reverse(explode("-", $fecha))); 
	
	$Descuento  = 0;
	$impuesto = 0;
?>
	<table width="92%" border="0" cellspacing="0" cellpadding="0">
     <tr>
      <td><div align="center"></div></td>
      <td colspan="8"  ><div align="center"><font size=5 >FACTURA ELECTRONICA N. <?php echo $clave; ?></font></div>
     </td>
    </tr>
	<tr>
      <td><div align="center"></div></td>
      <td colspan="8"  ><div align="left"><font size=2 >PROVEEDOR. <?php echo $dscnomemi; ?>  CEDULA. <?php echo $numcedemi; ?></font></div></td>
    </tr>
		<tr>
      <td><div align="center"></div></td>
      <td colspan="8"  ><div align="left"><font size=2 >FECHA FAC. <?php echo $fecha; ?></font></div></td>
    </tr>

	</table>
	
	<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <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>CANT</strong></font></span></td>	
    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>DESCRIPCION</strong></font></span></td>	
	 <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>TARIFA </strong></font></span></td>	
	<td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>IVA</strong></font></span></td>	
    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>PRECIO/U</strong></font></span></td>
	<td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>DESCUENTO</strong></font></span></td>
	<td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>SUB TOTAL</strong></font></span></td>
	 </tr>
	
	<?php 

	 foreach($xml->DetalleServicio->LineaDetalle as $item){
		 
		 $cdgbar   = $item->CodigoComercial->Codigo;
		 	 
		 $Detalle = $item->Detalle;
	 	 $cdgbar= str_replace(' ', '', $cdgbar);
		 $cant   = $item->Cantidad;	
		 if ($item->Descuento->MontoDescuento>0){$Descuento = $item->Descuento->MontoDescuento;}

		 $PrecioUnitario   = $item->PrecioUnitario;
		 
		// $MontoTotal = $item->PrecioUnitario;
			 $impuesto = 0;
			 // $cdgtar   = $item->Impuesto->CodigoTarifa;
			//	$SubTotal = $item->SubTotal;
			$SubTotalLinea = $item->SubTotal;
		 	//	$impuesto = floatval($impuesto);
			//	$SubTotal = floatval($SubTotal);

		 	 if ($item->Impuesto->Monto >=0){   // si el impuesto es mayor a 0

			
		 		$impuesto = $item->Impuesto->Monto;
			  	$cdgtar   = $item->Impuesto->CodigoTarifa;
				$Tarifa= $item->Impuesto->Tarifa;
				

	}
		 
			echo "<br>";
	
	?>	
	 <tr>
	<td align="right"><font size=1 ></font></td>
	<td align="left"><font size=1 ><?php echo "cod ".$cdgbar ?></font></td>
	<td align="right"><font size=1 ><?php echo $cant ?></font></td>
   <td align="left"><font size=1 ><?php echo $Detalle ?></font></td>
   <td align="right"><font size=1 ><?php echo $Tarifa ?></font></td>
   <td align="right"><font size=1 ><?php echo $impuesto ?></font></td>
     <td align="right"><font size=1 ><?php echo $PrecioUnitario ?></font></td>
   <td align="right"><font size=1 ><?php echo $Descuento ?></font></td>
    <td align="right"><font size=1 ><?php echo $SubTotalLinea ?></font></td>
   </tr>

	
	<?php 

	}  // fin for



?>	

 <tr>
 <td></td>
  </tr>
 <tr>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ></font></strong></td>
     <td align="right"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 >SUB TOTAL</font></strong></td>
    <td align="right"><strong><font size=1 ><?php echo number_format( $subtot1,2,".",",")?></font></strong></td>
   </tr>
   
   
   
    <tr>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="left">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right"><strong><font size=1 >DESCUENTO</font></strong></td>
      <td align="right"><?php echo number_format($totdes1,2,".",",")?></td>
    </tr>
    <tr>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ></font></strong></td>
     <td align="right"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 >IMPUESTO</font></strong></td>
    <td align="right"><strong><font size=1 ><?php echo  number_format($totimp1,2,".",",")?></font></strong></td>
   </tr>	
   
    <tr>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
	<td align="right"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ></font></strong></td>
     <td align="right"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 >TOTAL</font></strong></td>
    <td align="right"><strong><font size=1 ><?php echo  number_format($totcomp1,2,".",",")?></font></strong></td>
   </tr>
</table>



</body>
</html>