<!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 Ventas</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"];
$mostrar=$_POST["mostrar"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$usuario = $_POST["usuario"];

$n_decimales = 2;

?>
<body>
<?php


//Inicio reporte Libro de Ventas

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 cliente ';
	} //fin if 2.1
		

		//listado de facturas ventas
		$result = mysqli_query($link, "select a.pkySalesLedger as codigo, a.fldNumber as nro_fac,
a.fldAuthorization as nro_auto, a.fldDate as fecha, 
a.fldControlCode as cod_control, a.codClient as cod_cli, 
a.fldName as cliente, a.fldNIT as nit, a.fldGrossTotal as total_bruto,
a.fldICE as ice, a.fldExempt as excento, a.fldNetTotal as total_neto,
a.fldTaxDebit as debito, case a.sysEnabled when 0 then 'A' else 'V' end as estado
from
txsSalesLedger a
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' 
$valor_orden;");
		

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

?>
<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 Ventas, 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>Cliente</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>Deb. Fiscal IVA</b></TD>
        <TD align="center"><b>Estado</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($mostrar != 0){ //if mostrar
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ventas [$j][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][1]);//cod
	}//fin if color
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][4]);//fecha
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][8]);//nit
	printf("<td align='left'>%s</td>", $matriz_ventas [$j][7]);//cliente
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][2]);//nro. factura
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][3]);//nro. auto
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][5]);//cod control
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][9],$n_decimales));//total factura
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][10],$n_decimales));//ice
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][11],$n_decimales));//excento
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][12],$n_decimales));//total neto
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][13],$n_decimales));//debito fiscal IVA
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][14]);//estado A o V
	if($matriz_ventas [$j][14] == 'V'){
	$suma_factura = $suma_factura + $matriz_ventas [$j][9];
	$suma_ice = $suma_ice + $matriz_ventas [$j][10];
    $suma_excento = $suma_excento + $matriz_ventas [$j][11];
	$suma_neto = $suma_neto + $matriz_ventas [$j][12];
	$suma_iva = $suma_iva + $matriz_ventas [$j][13];
	}
    $color++;
	$j++;
	
	} else { //else if mostrar
	if($matriz_ventas [$j][14] == 'V'){//if mostrar V
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ventas [$j][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][1]);//cod
	}//fin if color
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][4]);//fecha
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][8]);//nit
	printf("<td align='left'>%s</td>", $matriz_ventas [$j][7]);//cliente
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][2]);//nro. factura
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][3]);//nro. auto
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][5]);//cod control
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][9],$n_decimales));//total factura
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][10],$n_decimales));//ice
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][11],$n_decimales));//excento
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][12],$n_decimales));//total neto
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][13],$n_decimales));//debito fiscal IVA
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][14]);//estado A o V
	
	$suma_factura = $suma_factura + $matriz_ventas [$j][9];
	$suma_ice = $suma_ice + $matriz_ventas [$j][10];
    $suma_excento = $suma_excento + $matriz_ventas [$j][11];
	$suma_neto = $suma_neto + $matriz_ventas [$j][12];
	$suma_iva = $suma_iva + $matriz_ventas [$j][13];
	$color++;
	}//fin if mostrar V
    
	$j++;
	}//fin if else mostrar
  }//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><td>&nbsp;
</td></tr>
</table>
<?php

} //fin if 2

?>

</body>
</html>
