<?php require_once('../Connections/cn_conexion.php'); ?>
<?php include_once("../includes/funciones.php");?>
<?php  mysql_query("SET NAMES 'utf8'"); ?>
<?php
$consulta=$_POST["consulta"];
$reporte_titulo=$_POST["reporte_titulo"];
$doc=$_POST["doc"];
$inicio=fecha_peru($_POST["inicio"]);
$fin=fecha_peru($_POST["fin"]);
switch($doc){
	case '0':
		$documento="Nº DOC";
		break;
	case 'f':
		$documento="FACTURA";
		break;
	case 'b':
		$documento="BOLETA";
		break;
	}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_docreporte = "".$consulta."";
$rs_docreporte = mysql_query($query_rs_docreporte, $cn_conexion) or die(mysql_error());
$row_rs_docreporte = mysql_fetch_assoc($rs_docreporte);
$totalRows_rs_docreporte = mysql_num_rows($rs_docreporte);
?>
<!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>SISTEMA DE FACTURACION</title>
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reporte.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
</head>

<body>
<table width="98%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="8"><h1><?php echo($reporte_titulo);?></h1></td>
  </tr>
  <tr>
    <td colspan="2">Relacion del</td>
    <td><?php echo($inicio." al ".$fin)?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="1%" align="center" bgcolor="#000066"><p  style="color:#fff">CANCELACIÓN</p></td>
    <td width="7%" bgcolor="#000066" align="center"><p  style="color:#fff"><?php echo($documento);?></p></td>
    <td width="30%" bgcolor="#000066"><p  style="color:#fff">CLIENTE</p></td>
    <td width="7%" bgcolor="#000066" align="center"><p  style="color:#fff">MONTO<br />
      EN S/</p></td>
    <td width="7%" bgcolor="#000066" align="center"><p  style="color:#fff">IMPUESTO<br />
      EN S/</p></td>
    <td width="12%" bgcolor="#000066" align="center"><p  style="color:#fff">TOTAL<br />
      EN S/</p></td>
    <td width="11%" bgcolor="#000066" align="center"><p  style="color:#fff">TOTAL<br />
      EN US $.</p></td>
    <td width="11%" bgcolor="#000066" align="center"><p style="color:#fff">EMISIÓN</span></td>
  </tr>
  <?php $i=1;?>
  <?php 
  	$suma_monto=0;
	$suma_impuesto=0;
	$suma_totalsoles=0;
	$suma_totaldolares=0;	
  ?>
  <?php do { ?>
  <?php 
				$res=$i%2;
				if($res==0){
					$color="#CDEBFC";
					}else{
						$color="#ffffff";
						}
				$monto=$row_rs_docreporte['monto'];
				$moneda=$row_rs_docreporte['moneda'];
				$tipocambio=$row_rs_docreporte['tipo_cambio'];
				$montodolares=montodolares($monto,$moneda,$tipocambio);
				$montosoles=round(montosoles($monto,$moneda,$tipocambio),2);
				$tipo_documento=$row_rs_docreporte['tipo_documento'];
				if($tipo_documento=="b"){
/*					$montoaux=$montosoles;
					$montosoles=round(($montosoles*100)/118,2);
					$igv=$montoaux-$montosoles;*/
					$igv=round(($montosoles*$row_rs_docreporte['igv'])/100,2);
					$totals=round($montosoles+$igv,2);
					$totald=round(montodolares($totals,$moneda,$tipocambio));					
					//$igv="";
					}else{
						$igv=round(($montosoles*$row_rs_docreporte['igv'])/100,2); 
						$totals=round($montosoles+$igv,2);
						$totald=round(montodolares($totals,$moneda,$tipocambio));			
						}
				if($row_rs_docreporte['hab_documento']=='0'){
					$classe_colortexto="enlace_cancelar";
					$clase_texto='style="color:#FF00FF"';
					}else{
						$classe_colortexto="enlace_normal";
						$clase_texto="";
						}
			?>
  <tr>
    <td width="80" height="20" align="center" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>>
      <?php $fechfin=$row_rs_docreporte['fecha_cancelado']; echo $fechfin; ?>
    </p></td>
    <td width="75" align="center" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>>
      <?php $numdoc=$row_rs_docreporte['num_documento']; echo $numdoc; ?>
    </p></td>
    <td width="200" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>>
      <?php $nomcli=$row_rs_docreporte['nom_cliente']; echo $nomcli; ?>
    </p></td>
    <td width="100" align="right" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>><?php $suma_monto+=$montosoles; 
	echo (number_format(decimals($montosoles),2));?>&nbsp;&nbsp;&nbsp;</p></td>
    <td width="100" align="right" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>><?php $suma_impuesto+=$igv;
	echo number_format(decimals($igv),2); ?>&nbsp;&nbsp;&nbsp;</p></td> 
    <td width="100" align="right" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>>
      <?php  echo number_format(decimals($totals),2); $suma_totalsoles+=$totals;?>
&nbsp;&nbsp;&nbsp; </p></td>
    <td width="100" align="right" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>><?php echo number_format(decimals($totald),2); $suma_totaldolares+=$totald;?>&nbsp;&nbsp;&nbsp;</p></td>
    
    <td width="120" align="center" style="background-color:<?php echo($color);?>"><p <?php echo($clase_texto);?>>
      <?php $fechemi=$row_rs_docreporte['fecha_emision']; echo $fechemi; ?>
    </p></td>
    </tr>
  <?php $i++;?>
  <?php } while ($row_rs_docreporte = mysql_fetch_assoc($rs_docreporte)); ?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="right"><?php echo(number_format(decimals($suma_monto),2));?>&nbsp;&nbsp;&nbsp;</td>
    <td align="right"><?php echo(number_format(decimals($suma_impuesto),2));?>&nbsp;&nbsp;&nbsp;</td>
    <td align="right"><?php echo(number_format(decimals($suma_totalsoles),2));?>&nbsp;&nbsp;&nbsp;</td>
    <td align="right"><?php echo(number_format(decimals($suma_totaldolares),2));?>&nbsp;&nbsp;&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($rs_docreporte);
?>
