<!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>Pantalla Reporte Libro de Compras</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php
include "../../conn.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$orden=$_POST["orden"];
$resultado=$_POST["resultado"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$usuario = $_POST["usuario"];

$n_decimales = 2;

?>
<body>
<?php


//Inicio reporte Libro de Compras

if($rep == 9 && $tipo == 1) { // if 2

  	if ($orden == 0) { // if 2.1
		$nom_orden = Fecha;
		$valor_orden = ' order by fecha ';
	} else {
		$nom_orden = Nombre;
		$valor_orden = ' order by proveedor ';
	} //fin if 2.1
		

		//listado de facturas compras
		$result = mysqli_query($link, "select a.pkyPurchasesLedger as codigo, a.fldNumber as nro_fac,
a.fldAuthorization as nro_auto, a.fldDate as fecha, 
a.fldControlCode as cod_control, a.codSupplier as cod_prov, 
a.fldName as proveedor, a.fldNIT as nit, a.fldGrossTotal as total_bruto,
a.fldICE as ice, a.fldExempt as excento, a.fldNetTotal as total_neto,
a.fldTaxCredit as credito
from
txsPurchasesLedger a
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 AND a.sysAuthorized = 1
$valor_orden;");
		

$matriz_compras = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_compras [$i][1] = $row['codigo'];
	$matriz_compras [$i][2] = $row['nro_fac'];
	$matriz_compras [$i][3] = $row['nro_auto'];
	$matriz_compras [$i][4] = $row['fecha'];
	$matriz_compras [$i][5] = $row['cod_control'];
	if($row['cod_control'] == NULL){
		$matriz_compras [$i][5] = 0;
	} else {
		$matriz_compras [$i][5] = $row['cod_control'];
	}
	$matriz_compras [$i][6] = $row['cod_pro'];
	$matriz_compras [$i][7] = $row['proveedor'];
	$matriz_compras [$i][8] = $row['nit'];
	$matriz_compras [$i][9] = $row['total_bruto'];
	$matriz_compras [$i][10] = $row['ice'];
	$matriz_compras [$i][11] = $row['excento'];
	$matriz_compras [$i][12] = $row['total_neto'];
	$matriz_compras [$i][13] = $row['credito'];
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);

//if($resultado == 0) { //if resultado
?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><b>Empresa: <?php echo $nombre_empresa; ?><br/>NIT: <?php echo $mi_nit; ?><br/>Usuario: <?php echo $usuario; ?></b>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?></b>
</td></tr></table><br/>
<p><center><font color="#000099" size="+1"><b>Libro de Compras, Ordenado por <?php echo $nom_orden; ?>, Expresado en Bs.<br/>De fecha <?php echo $fechaini; ?> A fecha <?php echo $fechafin; ?></b></font></center></p>

<table border="1" align="center" cellpadding="2" width="100%">
 		<TR> 
 		<TD align="center"><b>Cod.</b></TD>
        <TD align="center" width="65"><b>Fecha</b></TD>
    	<TD align="center"><b>NIT</b></TD>
        <TD align="center"><b>Proveedor</b></TD>
        <TD align="center"><b>Nro. Factura</b></TD>
        <TD align="center"><b>Nro. Autorizaci&oacute;n</b></TD>
		<TD align="center"><b>Cod. Control</b></TD>
        <TD align="center"><b>Total Factura</b></TD>
		<TD align="center"><b>Total ICE</b></TD>
        <TD align="center"><b>Importe Excento</b></TD>
		<TD align="center"><b>Importe Neto</b></TD>
        <TD align="center"><b>Cred. Fiscal IVA</b></TD>
        </TR>

  <?php
  $color = 2;
  $suma_factura = 0;
  $suma_ice = 0;
  $suma_excento = 0;
  $suma_neto = 0;
  $suma_iva = 0;
  
  $j = 1;
  while ($j <= $fin) { //while
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_compras [$j][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_compras [$j][1]);//cod
	}//fin if color
	printf("<td align='right'>%s</td>", $matriz_compras [$j][4]);//fecha
	printf("<td align='right'>%s</td>", $matriz_compras [$j][8]);//nit
	printf("<td align='left'>%s</td>", $matriz_compras [$j][7]);//proveedor
	printf("<td align='right'>%s</td>", $matriz_compras [$j][2]);//nro. factura
	printf("<td align='right'>%s</td>", $matriz_compras [$j][3]);//nro. auto
	printf("<td align='right'>%s</td>", $matriz_compras [$j][5]);//cod control
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][9],$n_decimales));//total factura
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][10],$n_decimales));//ice
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][11],$n_decimales));//excento
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][12],$n_decimales));//total neto
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][13],$n_decimales));//credito fiscal IVA
	
	$suma_factura = $suma_factura + $matriz_compras [$j][9];
	$suma_ice = $suma_ice + $matriz_compras [$j][10];
    $suma_excento = $suma_excento + $matriz_compras [$j][11];
	$suma_neto = $suma_neto + $matriz_compras [$j][12];
	$suma_iva = $suma_iva + $matriz_compras [$j][13];
    $color++;
	$j++;
  }//fin while
	
?>

<tr BGCOLOR='#ffcccc'><td colspan="7"><b>Totales</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_factura,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_ice,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_excento,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_neto,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_iva,$n_decimales)); ?>
</td></tr>
</table>
<?php

} //fin if 2

?>

</body>
</html>
