<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Libro de Ventas</title>
<?php
include "../../conn.php";
include "../../general.php";
include "../../token.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$cod_sucursal=$_POST["cod_sucursal"];
$mostrar=$_POST["mostrar"];
$cliente=$_POST["cliente"];
$cod_contexto=$_POST["contexto"];

if(!empty($_POST['combinar_con'])) {
	$combinar_mat = array ();
	// Contar numero de checkboxes seleccionados.
	$contar_combinar = count($_POST['combinar_con']);
	$txt_combinar = '[Combinado con '.$contar_combinar.' Sucursal(es)]';
	// Bucle para vaciar los checkboxes seleccionados.
	$i = 1;
	foreach($_POST['combinar_con'] as $seleccion) {
	$combinar_mat[$i] = $seleccion;
	$i++;
	}
	$fin_comb = $i - 1;
} else {
	$fin_comb = 0;
	$txt_combinar = '';
}

//vemos si hay combinacion de sucursales
if($fin_comb == 0) { //if comb
	$valor_combinacion = 'b.codCompanyBranch = '.$cod_sucursal.' and ';
} else { //else if comb
	$valor_combinacion = 'b.codCompanyBranch = '.$cod_sucursal.' or ';
	for($i = 1; $i <= $fin_comb; $i++) {
		if($i != $fin_comb) {
		$valor_combinacion = $valor_combinacion.'b.codCompanyBranch = '.$combinar_mat[$i].' or ';
		} else {
		$valor_combinacion = $valor_combinacion.'b.codCompanyBranch = '.$combinar_mat[$i];	
		}
	}
	$valor_combinacion ='('.$valor_combinacion.') and ';
} //fin if comb

?>
<?php if($mi_cod_pais == 1) { //if bolivia?>

<?php if($mostrar == 0) { ?>
<link href="../../libros.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="print">
@page {size: letter landscape;}
DIV {page-break-after: always;}
</style>

<?php } else { ?>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />
<?php } ?> 
</head>
<?php

if (empty($_POST["fechaini"]))
{
    $fechaini=$_GET["fechaini"];
} else {
	$fechaini=$_POST["fechaini"];
}

if (empty($_POST["fechafin"]))
{
    $fechafin=$_GET["fechafin"];
} else {
	$fechafin=$_POST["fechafin"];
}
/*
if (empty($_POST["usuario"]))
{
    $usuario=$_GET["usuario"];
} else {
	$usuario=$_POST["usuario"];
}
*/

$n_decimales = 2;

?>
<body>
<?php


//Inicio reporte Libro de Ventas

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

//responsable
$result2 = mysqli_query($link, "select a.codPerson as cod_persona, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as persona, b.fldAlternativeKey as ci 
from 
txsTaxesConfig a, agnPerson b
where 
a.codPerson = b.pkyPerson and 
b.sysEnabled = 1 and b.sysAuthorized = 1;");
while ($row = mysqli_fetch_array($result2)) {
	$nombre_responsable = $row['persona'];
	$ci_responsable = $row['ci'];
}
mysqli_free_result($result2);	

//datos de la sucursal
$resultsuc = mysqli_query($link, "select a.pkyCompanyBranch as cod_suc, a.codCompany as cod_emp, a.fldName as sucursal, a.fldID as nro_suc, a.codCity as cod_ciu, b.fldName as ciudad, a.fldPhone as tel, a.fldAddress as direccion
from agnCompanyBranch a, regCity b 
where 
a.pkyCompanyBranch = '$cod_sucursal' and 
a.codCompany = 2 and 
a.codCity = b.pkyCity and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
order by a.fldID;");
while ($row = mysqli_fetch_array($resultsuc)) {
	$sucursal = $row['sucursal'];
	$id_sucursal = $row['nro_suc'];
	$dir_sucursal = $row['direccion'];
}
mysqli_free_result($resultsuc);	
/*
//vemos si hay combinacion de sucursales
if($fin_comb == 0) { //if comb
	$valor_combinacion = 'b.codCompanyBranch = '.$cod_sucursal.' and ';
} else { //else if comb
	$valor_combinacion = 'b.codCompanyBranch = '.$cod_sucursal.' or ';
	for($i = 1; $i <= $fin_comb; $i++) {
		if($i != $fin_comb) {
		$valor_combinacion = $valor_combinacion.'b.codCompanyBranch = '.$combinar_mat[$i].' or ';
		} else {
		$valor_combinacion = $valor_combinacion.'b.codCompanyBranch = '.$combinar_mat[$i];	
		}
	}
	$valor_combinacion ='('.$valor_combinacion.') and ';
} //fin if comb
*/

if ($cliente == 0) { //if cliente
    $valor_cliente = ' ';
	$nom_cliente = 'Todos';
  } else { 
    $valor_cliente = ' a.codClient = '.$cliente.' and ';
	//nombre de cliente
	$resultcli = mysqli_query($link, "select pkyCompany as codigo, fldName as cliente
from agnCompany
where
fldIsClient = 1 and
pkyCompany = '$cliente' and 
sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultcli)) {
	$nom_cliente = $row['codigo'].' - '.$row['cliente'];
	}
	mysqli_free_result($resultcli);
} //fin if cliente

//contexto
if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto = ' ';
} else {
	$resultcontx = mysqli_query($link, "select pkyContext as cod_contexto, fldName as contexto, codCostCenter as cod_cc 
	from srvContext
	where 
	pkyContext = '$cod_contexto';");
		while ($row = mysqli_fetch_array($resultcontx)) {   
			$nom_contexto = $row['cod_contexto'].' - '.$row['contexto'];
		}
		mysqli_free_result($resultcontx);
	$valor_contexto = ' a.sysContext = '.$cod_contexto.' and ';	
}

//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, a.fldDiscount as descuento, a.codStatus as cod_estado, a.txtDescription as glosa, a.cosComponentKey as cod_nv, b.codCompanyBranch as cod_suc, c.fldID as nro_suc, c.fldName as sucursal, d.fldName as ciudad
from
txsSalesLedger a, txsAuthorizationControl b, agnCompanyBranch c, regCity d
where 
a.codAuthorizationControl = b.pkyAuthorizationControl and 
b.codCompanyBranch = c.pkyCompanyBranch and 
c.codCity = d.pkyCity and 
$valor_combinacion  
$valor_cliente 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' 
order by fecha, nro_auto, nro_fac;");
		
//$numero_filas = mysqli_num_rows($result);

$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] = round($row['total_bruto'],$n_decimales);
	$matriz_ventas [$i][10] = round($row['ice'],$n_decimales);
	$matriz_ventas [$i][11] = round($row['excento'],$n_decimales);
	$matriz_ventas [$i][12] = round($row['total_neto'],$n_decimales);
	$matriz_ventas [$i][13] = round($row['debito'],$n_decimales);
	$matriz_ventas [$i][14] = $row['cod_estado'];
	$matriz_ventas [$i][15] = $row['glosa'];
	$matriz_ventas [$i][16] = $row['cod_nv'];
	$matriz_ventas [$i][17] = round($row['descuento'],$n_decimales);
	if($row['descuento'] <= 0) { 
		$matriz_ventas [$i][17] = 0;
	}
	if($row['cod_estado'] == 1){ $matriz_ventas [$i][18] = 'V'; }
	if($row['cod_estado'] == 2){ $matriz_ventas [$i][18] = 'A'; }
	if($row['cod_estado'] == 3){ $matriz_ventas [$i][18] = 'E'; }
	if($row['cod_estado'] == 4){ $matriz_ventas [$i][18] = 'N'; }
	if($row['cod_estado'] == 5){ $matriz_ventas [$i][18] = 'C'; }
	$matriz_ventas [$i][19] = $row['nro_suc'];
	$matriz_ventas [$i][20] = $row['sucursal'];
	$matriz_ventas [$i][21] = $row['ciudad'];
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);

//listado de facturas no usadas
$result22 = mysqli_query($link, "select b.fldEmissionDate as fecha, b.fldAuthorization as autorizacion, b.fldNextNumber as nro_ini, b.fldEndNumber as nro_fin 
from txsAuthorizationControl b
where 
$valor_combinacion 
b.fldEmissionDate >= '$fechaini' and
b.fldEmissionDate <= '$fechafin' and 
b.fldClosed = 1 and 
b.sysEnabled = 1 and b.sysAuthorized = 1;");

$numero_no_usadas = mysqli_num_rows($result22);

while ($row = mysqli_fetch_array($result22)) {
	$fecha_no_usadas = $row['fecha'];
	$auto_no_usadas = $row['autorizacion'];
	$nro_ini_no_usadas = $row['nro_ini'];
	$nro_fin_no_usadas = $row['nro_fin'];
}
mysqli_free_result($result22);

if($numero_no_usadas == 1) {//if no usadas
	$p = $fin + 1;
	for($n = $nro_ini_no_usadas; $n <= $nro_fin_no_usadas; $n++) {//for n
		$matriz_ventas [$p][1] = 0; //codigo
		$matriz_ventas [$p][2] = $n; //nro fac
		$matriz_ventas [$p][3] = $auto_no_usadas; //autorizacion
		$matriz_ventas [$p][4] = $fecha_no_usadas; //fecha
		$matriz_ventas [$p][5] = 0; //cod control
		$matriz_ventas [$p][6] = 0; //cod cliente
		$matriz_ventas [$p][7] = 'NO UTILIZADA'; //cliente
		$matriz_ventas [$p][8] = 0; //NIT
		$matriz_ventas [$p][9] = 0; //total bruto
		$matriz_ventas [$p][10] = 0; //ice
		$matriz_ventas [$p][11] = 0; //exento
		$matriz_ventas [$p][12] = 0; //total neto
		$matriz_ventas [$p][13] = 0; //debito
		$matriz_ventas [$p][14] = 4; //cod estado
		$matriz_ventas [$p][15] = NULL; //glosa
		$matriz_ventas [$p][16] = 0; //cod nv
		$matriz_ventas [$p][17] = 0; //descuento
		$matriz_ventas [$p][18] = 'N'; //estado
		$p++;	
	}//fin for n
	$fin = $p - 1;
}//fin if no usadas

$numero_filas = $fin;
$var = ceil($numero_filas/$filas_por_pagina); //redondea hacia arriba

$total_fac_par = 0;
$total_ice_par = 0;
$total_exe_par = 0;
$total_tasa0_par = 0;
$total_subtotal_par = 0;
$total_desc_par = 0;
$total_base_debito_par = 0;
$total_debito_par = 0;

$total_fac = 0;
$total_ice = 0;
$total_exe = 0;
$total_tasa0 = 0;
$total_subtotal = 0;
$total_desc = 0;
$total_base_debito = 0;
$total_debito = 0;

if($numero_filas == 0) {//if $numero_filas
$i = 1;
$var = 1;
$filas_por_pagina = 28;
}//fin if $numero_filas

//este if es para mostrar con el formato de libro de ventas
if($mostrar == 0) { //mostrar 0

$j = 1;
$nu = 1;
for($i = 1; $i <= $var; $i++) {//for
?>
<?php if($mostrar_logo_reportes_formato == 0) { //if formato 0 ?>
<table border="0" align="center" cellpadding="2" width="990">
<tr>
	<td width="315"><?php echo $usuario; ?></td>
    <td width="320" align="center"><b>LIBRO DE VENTAS IVA</b></td>
    <td width="315" align="right"><b>Folio: <?php echo $i; ?> de <?php echo $var; ?></b></td>
</tr>
<tr>
	<td width="315">FACTURAS COMPUTARIZADAS</td>
    <td width="320" align="center">DEL <?php echo date("d/m/Y",strtotime($fechaini)); ?> AL <?php echo date("d/m/Y",strtotime($fechafin)); ?></td>
    <td width="315" align="right">CIFRAS EN BOLIVIANOS</td>
</tr>
</table>
<table border="1" align="center" cellpadding="2" width="990">
<tr>
	<td width="485"><b>Nombre o Raz&oacute;n Social: </b><?php echo $nombre_empresa; ?></td>
    <td width="485"><b>NIT: </b><?php echo $mi_nit; ?></td>
</tr>
<tr>
	<td width="485"><b>No. Sucursal: </b><?php echo $id_sucursal; ?></td>
    <td width="485"><b>Direcci&oacute;n: </b><?php echo $dir_sucursal; ?></td>
</tr>
</table>
<?php } //fin if formato 0 ?>

<?php if($mostrar_logo_reportes_formato == 1) { //if formato 1 ?>
<table border="0" align="center" cellpadding="2" width="990">
<tr>
	<td rowspan="2" width="15%"><img src="../../Images/logo.jpg" alt="Logo" width="100" height="40" /></td>
	<td width="315">&nbsp;</td>
    <td width="320" align="center"><b>LIBRO DE VENTAS IVA</b></td>
    <td width="315" align="right"><b>Folio: <?php echo $i; ?> de <?php echo $var; ?></b></td>
</tr>
<tr>
	<td width="315">FACTURAS COMPUTARIZADAS</td>
    <td width="320" align="center">DEL <?php echo date("d/m/Y",strtotime($fechaini)); ?> AL <?php echo date("d/m/Y",strtotime($fechafin)); ?></td>
    <td width="315" align="right">CIFRAS EN BOLIVIANOS</td>
</tr>
</table>
<table border="1" align="center" cellpadding="2" width="990">
<tr>
	<td width="485"><b>Nombre o Raz&oacute;n Social: </b><?php echo $nombre_empresa; ?></td>
    <td width="485"><b>NIT: </b><?php echo $mi_nit; ?></td>
</tr>
<tr>
	<td width="485"><b>No. Sucursal: </b><?php echo $id_sucursal; ?></td>
    <td width="485"><b>Direcci&oacute;n: </b><?php echo $dir_sucursal; ?></td>
</tr>
</table>
<?php } //fin if formato 1 ?>
<br />

<table border="1" align="center" cellpadding="2" width="990" height="600">
    <TR> 
    <TD align="center" width="10" style='border-bottom: solid 1px'><b>No.</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Fecha factura</b></TD>
    <TD align="center" width="40" style='border-bottom: solid 1px'><b>Nro. Factura</b></TD>
    <TD align="center" width="65" style='border-bottom: solid 1px'><b>Nro. Autorizaci&oacute;n</b></TD>
    <TD align="center" width="10" style='border-bottom: solid 1px'><b>Estado</b></TD>
    <TD align="center" width="45" style='border-bottom: solid 1px'><b>NIT/CI cliente</b></TD>
    <TD align="center" width="335" style='border-bottom: solid 1px'><b>Nombre o Raz&oacute;n Social</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Importe total de venta<br />A</b></b></TD>
    <TD align="center" width="50" style='border-bottom: solid 1px'><b>Importe ICE/IEDH/ tasas<br />B</b></b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Exportaciones y operaciones exentas<br />C</b></b></TD>
    <TD align="center" width="50" style='border-bottom: solid 1px'><b>Ventas gravadas a tasa cero<br />D</b></b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Subtotal<br />E = A - B - C - D</b></b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Descuentos, bonificaciones y rebajas otorgadas<br />F</b></b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Importe base para d&eacute;bito fiscal<br />G = E - F</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>D&eacute;bito fiscal<br />H = G * 13%</b></TD>
    <TD align="center" width="65" style='border-bottom: solid 1px'><b>Cod. Control</b></TD>
    </TR>

<?php
$color = 2;
$q = 1;//
while ($q <= $filas_por_pagina) { //while
    
  if($numero_filas == 0) {//if filas
	printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
  } else { //else filas
    if($color % 2 == 0) { //if color
		if($matriz_ventas [$j][4] != NULL) {
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $nu);//numero
		} else { 
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>&nbsp;</td>");//numero vacio
		}
	} else {
		if($matriz_ventas [$j][4] != NULL) {
		printf("<tr><td align='center'>%s</td>", $nu);//numero
		} else { 
		printf("<tr><td align='center'>&nbsp;</td>");//numero vacio
		}
	}//fin if color
  }//fin filas
    printf("<td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	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='center'>%s</td>", $matriz_ventas [$j][18]);//estado V,A,E,N,C
	if($matriz_ventas [$j][14] == 2 || $matriz_ventas [$j][14] == 3 || $matriz_ventas [$j][14] == 4) {
	printf("<td align='right'>%s</td>", 0);//nit
	} else {
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][8]);//nit
	}
	$texto = $matriz_ventas [$j][7];//cliente
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	$texto = substr($texto,0,150);
	if($matriz_ventas [$j][14] == 2) { $texto = 'ANULADA'; }
	if($matriz_ventas [$j][14] == 3) { $texto = 'EXTRAVIADA'; }
	if($matriz_ventas [$j][14] == 4) { $texto = 'NO UTILIZADA'; }
	printf("<td align='left'>%s</td>", $texto);//cliente
	
	
	if($matriz_ventas [$j][18] == NULL) {//if si es 0
		printf("<td align='right'>%s</td>", NULL);//total venta
		printf("<td align='right'>%s</td>", NULL);//ice
		printf("<td align='right'>%s</td>", NULL);//excento
		printf("<td align='right'>%s</td>", NULL);//tasa cero
		printf("<td align='right'>%s</td>", NULL);//subtotal
		printf("<td align='right'>%s</td>", NULL);//descuento
		printf("<td align='right'>%s</td>", NULL);//imp para cred  fis
		printf("<td align='right'>%s</td>", NULL);//credito fiscal IVA
		printf("<td align='center'>%s</td></tr>", NULL);//cod control
		
	} else { //else si es 0
		if($matriz_ventas [$j][14] == 2 || $matriz_ventas [$j][14] == 3 || $matriz_ventas [$j][14] == 4) {//if si es A
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//total venta
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//ice
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//excento
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//tasa cero
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//subtotal
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//descuento
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//imp para cred fis
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//credito fiscal IVA
			printf("<td align='right'>%s</td></tr>", number_format(0,0));//cod control
		} else {//else si es A
			$imp_total_venta = $matriz_ventas [$j][9] + $matriz_ventas [$j][17];
			printf("<td align='right'>%s</td>", number_format($imp_total_venta,$n_decimales));//total venta
			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(0,$n_decimales));//tasa cero
			$subtotal = $imp_total_venta - $matriz_ventas [$j][10] - $matriz_ventas [$j][11] - 0;
			printf("<td align='right'>%s</td>", number_format($subtotal,$n_decimales));//subtotal
			printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][17],$n_decimales));//descuento
			$imp_base_debito_fis = $imp_total_venta - $matriz_ventas [$j][17];
			printf("<td align='right'>%s</td>", number_format($imp_base_debito_fis,$n_decimales));//imp para cred fis
			printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][13],$n_decimales));//debito fiscal IVA
			printf("<td align='right'>%s</td></tr>", $matriz_ventas [$j][5]);//cod control
		}//fin if si es A
	}//fin if si es 0
			

		
	if($numero_filas == 0 && $j == 14) {
		printf("<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td align='right' colspan='5'><font size='16'><b>SIN MOVIMIENTO</b></font></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
	}
	
	if($matriz_ventas [$j][14] == 1 || $matriz_ventas [$j][14] == 5){
	$total_fac_par = $total_fac_par + $imp_total_venta;
	$total_ice_par = $total_ice_par + $matriz_ventas [$j][10];
	$total_exe_par = $total_exe_par + $matriz_ventas [$j][11];
	$total_tasa0_par = $total_tasa0_par + 0;
	$total_subtotal_par = $total_subtotal_par + $subtotal;
	$total_desc_par = $total_desc_par + $matriz_ventas [$j][17];
	$total_base_debito_par = $total_base_debito_par + $imp_base_debito_fis;
	$total_debito_par = $total_debito_par + $matriz_ventas [$j][13];
	
	$total_fac = $total_fac + $imp_total_venta;
	$total_ice = $total_ice + $matriz_ventas [$j][10];
	$total_exe = $total_exe + $matriz_ventas [$j][11];
	$total_tasa0 = $total_tasa0 + 0;
	$total_subtotal = $total_subtotal + $subtotal;
	$total_desc = $total_desc + $matriz_ventas [$j][17];
	$total_base_debito = $total_base_debito + $imp_base_debito_fis;
	$total_debito = $total_debito + $matriz_ventas [$j][13];
	}
    $color++;
	$j++;
	$q++;
	$nu++;
}//fin while
	
?>

<tr>
	<td colspan="5" align="left" style='border-top: solid 1px'><b>Nombre del Responsable: </b><?php echo strtr(strtoupper($nombre_responsable), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");; ?></td>
    <td colspan="2" align="left" style='border-top: solid 1px'><b>TOTALES PARCIALES:</b></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_fac_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_ice_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_exe_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_tasa0_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_subtotal_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_desc_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_base_debito_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_debito_par,$n_decimales); ?></td>
    <td style='border-top: solid 1px'>&nbsp;</td>
</tr>
<tr>
	<td colspan="5" align="left"><b>C.I.: </b><?php echo $ci_responsable; ?></td>
    <td colspan="2" align="left"><b>TOTALES GENERALES:</b></td>
    <td align="right"><b><?php echo number_format($total_fac,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_ice,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_exe,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_tasa0,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_subtotal,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_desc,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_base_debito,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_debito,$n_decimales); ?></td>
    <td>&nbsp;</td>
</tr>
</table>

<?php
$total_fac_par = 0;
$total_ice_par = 0;
$total_exe_par = 0;
$total_tasa0_par = 0;
$total_subtotal_par = 0;
$total_desc_par = 0;
$total_base_debito_par = 0;
$total_debito_par = 0;
	if($i != $var) {//if $i
	?>
	<div>
	</div>
	<?php
	}//fin if $i
}//fin for

}// fin if mostrar 0


//este if es para mostrar con el formato detalle de facturas
if($mostrar == 1) { //mostrar 1

?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Facturas Ventas</b></font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="33%"><b>Cliente: </b><?php echo $nom_cliente; ?></TD>
        <TD width="33%"><b>Sucursal: </b><?php echo $id_sucursal; ?> - <?php echo $sucursal; ?> <?php echo $txt_combinar; ?></TD>
        <TD width="33%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_moneda_principal; ?></TD>
        </TR>
    </table>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br />

<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
 	<TD align="center"><b>No.</b></TD>
    <TD align="center" width="65"><b>Fecha factura</b></TD>
    <TD align="center"><b>Nro. Factura</b></TD>
    <TD align="center"><b>Nro. Autorizaci&oacute;n</b></TD>
    <TD align="center"><b>Estado</b></TD>
    <TD align="center"><b>NIT/CI cliente</b></TD>
    <TD align="center"><b>Nombre o Raz&oacute;n Social</b></TD>
    <TD align="center"><b>Importe total de venta<br />A</b></b></TD>
    <TD align="center"><b>Importe ICE/IEDH/ tasas<br />B</b></b></TD>
    <TD align="center"><b>Exportaciones y operaciones exentas<br />C</b></b></TD>
    <TD align="center"><b>Ventas gravadas a tasa cero<br />D</b></b></TD>
    <TD align="center"><b>Subtotal<br />E = A - B - C - D</b></b></TD>
    <TD align="center"><b>Descuentos, bonificaciones y rebajas otorgadas<br />F</b></b></TD>
    <TD align="center"><b>Importe base para d&eacute;bito fiscal<br />G = E - F</b></TD>
    <TD align="center"><b>D&eacute;bito fiscal<br />H = G * 13%</b></TD>
    <TD align="center" width="90"><b>Cod. Control</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Sucursal</b></TD>
    <?php if ($editar_factura_venta == 1) { ?>
    <TD align="center"><b>Editar</b></TD>
    <?php } ?>
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$j = 1;
$nu = 1;
$total_fac = 0;
$total_ice = 0;
$total_exe = 0;
$total_tasa0 = 0;
$total_subtotal = 0;
$total_desc = 0;
$total_base_debito = 0;
$total_debito = 0;
while ($j <= $fin) { //while
  if($matriz_ventas [$j][14] == 1 || $matriz_ventas [$j][14] == 5) {
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $nu);//numero
	} else {
	printf("<tr><td align='center'>%s</td>", $nu);//numero
	}//fin if color
	
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	$codigo_factura = $matriz_ventas [$j][1];
	printf("<td align='right'><a href='../../ModuleTaxes/VoucherSalesLedger/index.php?pkySalesLedger=$codigo_factura&sheetSize=$formato_factura&head=$muestra_cabecera&user=$usuario&token=$token' title='Factura'>%s</a></td>", $matriz_ventas [$j][2]);//nro. factura
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][3]);//nro. auto
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][18]);//estado V,A,E,N,C
  	printf("<td align='right'>%s</td>", $matriz_ventas [$j][8]);//nit
	$texto = $matriz_ventas [$j][7];//cliente
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	//$texto = substr($texto,0,50);
	
	printf("<td align='left'>%s</td>", $texto);//cliente
	
	$imp_total_venta = $matriz_ventas [$j][9] + $matriz_ventas [$j][17];
	printf("<td align='right'>%s</td>", number_format($imp_total_venta,$n_decimales));//total venta
	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(0,$n_decimales));//tasa cero
	$subtotal = $imp_total_venta - $matriz_ventas [$j][10] - $matriz_ventas [$j][11] - 0;
	printf("<td align='right'>%s</td>", number_format($subtotal,$n_decimales));//subtotal
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][17],$n_decimales));//descuento
	$imp_base_debito_fis = $imp_total_venta - $matriz_ventas [$j][17];
	printf("<td align='right'>%s</td>", number_format($imp_base_debito_fis,$n_decimales));//imp para cred fis
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][13],$n_decimales));//debito fiscal IVA
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][5]);//cod control
	$glosa2 = $matriz_ventas [$j][15];
	$glosa2 = nl2br($glosa2);//quita salto de linea 
	$glosa2 = str_replace('\n','<br />',$glosa2);//reemplaza para salto de linea
	printf("<td>%s</td>", $glosa2);//glosa
	$codigo_nota_venta = $matriz_ventas [$j][16];
	printf("<td align='center'><a href='../../ModuleSales/VoucherSalesNote/index.php?pkySalesNote=$codigo_nota_venta&user=$usuario&token=$token' title='Nota de Venta'>%s</a></td>", $matriz_ventas [$j][16]);//generado por nota de venta
	printf("<td align='left'>%s - %s (%s)</td>", $matriz_ventas [$j][19], $matriz_ventas [$j][20], $matriz_ventas [$j][21]);//sucursal
	
	if ($editar_factura_venta == 1) {
		printf("<td align='center'><a href='../../ModuleTaxes/ReportTaxesSalesLedger/edit.php?pkySalesNote=$codigo_nota_venta&pkySalesLedger=$codigo_factura&action=0&cont=-1&user=$usuario&token=$token' title='Editar Nombre y Glosa en Factura y Glosa en Nota de Venta'>Editar</a></td>");//editar nombre y glosa en factura y glosa en nota de venta
	}
	echo '</tr>';
	$total_fac = $total_fac + $imp_total_venta;
	$total_ice = $total_ice + $matriz_ventas [$j][10];
	$total_exe = $total_exe + $matriz_ventas [$j][11];
	$total_tasa0 = $total_tasa0 + 0;
	$total_subtotal = $total_subtotal + $subtotal;
	$total_desc = $total_desc + $matriz_ventas [$j][17];
	$total_base_debito = $total_base_debito + $imp_base_debito_fis;
	$total_debito = $total_debito + $matriz_ventas [$j][13];
	
	$color++;
	}
    $nu++;
	$j++;

}//fin while

?>
 
<tr BGCOLOR='#ffcccc'><td colspan="7"><b>Total</b></td>
<td align="right"><b><?php echo number_format($total_fac,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_ice,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_exe,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_tasa0,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_subtotal,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_desc,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_base_debito,$n_decimales); ?></td>
<td align="right"><b><?php echo number_format($total_debito,$n_decimales); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<?php if ($editar_factura_venta == 1) { ?>
<td>&nbsp;</td>
<?php } ?>
</tr>
</tbody>
</table>
         

<?php
}// fin if mostrar 1

} //fin if 2

} //fin if bolivia
?>


<?php if($mi_cod_pais == 176) { //if paraguay ?>

<?php if($mostrar == 0) { ?>
<link href="../../libros176.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="print">
@page {size: letter;}
DIV {page-break-after: always;}
</style>

<?php } else { ?>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />
<?php } ?> 
</head>
<?php

if (empty($_POST["fechaini"]))
{
    $fechaini=$_GET["fechaini"];
} else {
	$fechaini=$_POST["fechaini"];
}

if (empty($_POST["fechafin"]))
{
    $fechafin=$_GET["fechafin"];
} else {
	$fechafin=$_POST["fechafin"];
}
/*
if (empty($_POST["usuario"]))
{
    $usuario=$_GET["usuario"];
} else {
	$usuario=$_POST["usuario"];
}
*/

//$n_decimales = 0;
//$n_decimales176 = 0;

?>
<body>
<?php


//Inicio reporte Libro de Ventas

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

//datos de la sucursal
$resultsuc = mysqli_query($link, "select a.pkyCompanyBranch as cod_suc, a.codCompany as cod_emp, a.fldName as sucursal, a.fldID as nro_suc, a.codCity as cod_ciu, b.fldName as ciudad, a.fldPhone as tel, a.fldAddress as direccion
from agnCompanyBranch a, regCity b 
where 
a.pkyCompanyBranch = '$cod_sucursal' and 
a.codCompany = 2 and 
a.codCity = b.pkyCity and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
order by a.fldID;");
while ($row = mysqli_fetch_array($resultsuc)) {
	$sucursal = $row['sucursal'];
	$id_sucursal = $row['nro_suc'];
	$dir_sucursal = $row['direccion'];
}
mysqli_free_result($resultsuc);	

if ($cliente == 0) { //if cliente
    $valor_cliente = ' ';
	$valor_cliente2 = ' ';
	$nom_cliente = 'Todos';
  } else { 
    $valor_cliente = ' a.codClient = '.$cliente.' and ';
	$valor_cliente2 = ' a.codCompany = '.$cliente.' and ';
	//nombre de cliente
	$resultcli = mysqli_query($link, "select pkyCompany as codigo, fldName as cliente
from agnCompany
where
fldIsClient = 1 and
pkyCompany = '$cliente' and 
sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultcli)) {
	$nom_cliente = $row['codigo'].' - '.$row['cliente'];
	}
	mysqli_free_result($resultcli);
} //fin if cliente

//contexto
if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto = ' ';
} else {
	$resultcontx = mysqli_query($link, "select pkyContext as cod_contexto, fldName as contexto, codCostCenter as cod_cc 
	from srvContext
	where 
	pkyContext = '$cod_contexto';");
		while ($row = mysqli_fetch_array($resultcontx)) {   
			$nom_contexto = $row['cod_contexto'].' - '.$row['contexto'];
		}
		mysqli_free_result($resultcontx);
	$valor_contexto = ' a.sysContext = '.$cod_contexto.' and ';	
}

//listado de facturas ventas
$result = mysqli_query($link, "select fin2.codigo as codigo, fin2.nro_fac as nro_fac, fin2.nro_auto as nro_auto, fin2.fecha as fecha, fin2.cod_cli as cod_cli, fin2.cliente as cliente, fin2.ruc as ruc, fin2.total_bruto as total_bruto, fin2.total_neto as total_neto, fin2.descuento as descuento, fin2.cod_estado as cod_estado, fin2.glosa as glosa, fin2.comp_gen as comp_gen, fin2.cod_nv as cod_nv, fin2.cod_moneda as cod_moneda, fin2.cod_tc as cod_tc, fin2.cod_tipo as cod_tipo, fin2.monto_10 as monto_10, fin2.iva_10 as iva_10, fin2.monto_5 as monto_5, fin2.iva_5 as iva_5, fin2.exento as exento, fin2.cod_tipo_doc as cod_tipo_doc
from 
(select fin.codigo as codigo, fin.nro_fac as nro_fac, fin.nro_auto as nro_auto, fin.fecha as fecha, fin.cod_cli as cod_cli, fin.cliente as cliente, fin.ruc as ruc, fin.total_bruto as total_bruto, fin.total_neto as total_neto, fin.descuento as descuento, fin.cod_estado as cod_estado, fin.glosa as glosa, fin.comp_gen as comp_gen, fin.cod_nv as cod_nv, fin.cod_moneda as cod_moneda, fin.cod_tc as cod_tc, fin.cod_tipo as cod_tipo, sum(fin.monto_10) as monto_10, sum(fin.iva_10) as iva_10, sum(fin.monto_5) as monto_5, sum(fin.iva_5) as iva_5, sum(fin.exento) as exento, fin.cod_tipo_doc as cod_tipo_doc
from
(select t.codigo as codigo, t.nro_fac as nro_fac, t.nro_auto as nro_auto, t.fecha as fecha, t.cod_cli as cod_cli, t.cliente as cliente, t.ruc as ruc, t.total_bruto as total_bruto, t.total_neto as total_neto, t.descuento as descuento, t.cod_estado as cod_estado, t.glosa as glosa, t.comp_gen as comp_gen, t.cod_nv as cod_nv, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, t.cod_tipo as cod_tipo, t.monto_10 as monto_10,  t.iva_10 as iva_10, t.monto_5 as monto_5, t.iva_5 as iva_5, t.exento as exento, t.cod_tipo_doc as cod_tipo_doc
from
(select a.pkySalesLedger as codigo, a.fldNumber as nro_fac, 
a.fldAuthorization as nro_auto, a.fldDate as fecha, a.codClient as cod_cli, a.fldName as cliente, a.fldNIT as ruc, a.fldGrossTotal as total_bruto, a.fldNetTotal as total_neto, a.fldDiscount as descuento, a.codStatus as cod_estado, a.txtDescription as glosa, a.fldParentComponent as comp_gen, a.cosComponentKey as cod_nv, c.codCurrency as cod_moneda, c.codExchangeRate as cod_tc, c.codType as cod_tipo,  
case when d.fldTax = 10 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) else 0 end as monto_10, 
case when d.fldTax = 10 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) else 0 end as iva_10,
case when d.fldTax = 5 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) else 0 end as monto_5, 
case when d.fldTax = 5 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) else 0 end as iva_5,
case when d.fldExempt <> 0 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*(d.fldExempt/100) else 0 end as exento, 1 as cod_tipo_doc 
from
txsSalesLedger a, txsAuthorizationControl b, slsSalesNote c, slsSalesNoteDetail d 
where 
a.codAuthorizationControl = b.pkyAuthorizationControl and 
a.cosComponentKey = c.pkySalesNote and 
c.pkySalesNote = d.codSalesNote and 
$valor_combinacion  
$valor_cliente 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin') t 
where 
t.cod_moneda = '$cod_moneda_principal' 

union all 

select t.codigo as codigo, t.nro_fac as nro_fac, t.nro_auto as nro_auto, t.fecha as fecha, t.cod_cli as cod_cli, t.cliente as cliente, t.ruc as ruc, t.total_bruto * tc.fldRate as total_bruto, t.total_neto * tc.fldRate as total_neto, t.descuento * tc.fldRate as descuento, t.cod_estado as cod_estado, t.glosa as glosa, t.comp_gen as comp_gen, t.cod_nv as cod_nv, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, t.cod_tipo as cod_tipo, t.monto_10 * tc.fldRate as monto_10, t.iva_10 * tc.fldRate as iva_10, t.monto_5 * tc.fldRate as monto_5, t.iva_5 * tc.fldRate as iva_5, t.exento * tc.fldRate as exento, t.cod_tipo_doc as cod_tipo_doc
from
(select a.pkySalesLedger as codigo, a.fldNumber as nro_fac, 
a.fldAuthorization as nro_auto, a.fldDate as fecha, a.codClient as cod_cli, a.fldName as cliente, a.fldNIT as ruc, a.fldGrossTotal as total_bruto, a.fldNetTotal as total_neto, a.fldDiscount as descuento, a.codStatus as cod_estado, a.txtDescription as glosa, a.fldParentComponent as comp_gen, a.cosComponentKey as cod_nv, c.codCurrency as cod_moneda, c.codExchangeRate as cod_tc, c.codType as cod_tipo,  
case when d.fldTax = 10 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) else 0 end as monto_10, 
case when d.fldTax = 10 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) else 0 end as iva_10,
case when d.fldTax = 5 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) else 0 end as monto_5, 
case when d.fldTax = 5 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) else 0 end as iva_5,
case when d.fldExempt <> 0 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*(d.fldExempt/100) else 0 end as exento, 1 as cod_tipo_doc 
from
txsSalesLedger a, txsAuthorizationControl b, slsSalesNote c, slsSalesNoteDetail d 
where 
a.codAuthorizationControl = b.pkyAuthorizationControl and 
a.cosComponentKey = c.pkySalesNote and 
c.pkySalesNote = d.codSalesNote and 
$valor_combinacion 
$valor_cliente 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin') t, actExchangeRateDetail tc 
where 
tc.codTargetCurrency = t.cod_moneda and
tc.codBaseCurrency = '$cod_moneda_principal' and
tc.codExchangeRate = t.cod_tc) fin 
group by fin.codigo


UNION ALL


select fin.codigo as codigo, fin.nro_fac as nro_fac, fin.nro_auto as nro_auto, fin.fecha as fecha, fin.cod_cli as cod_cli, fin.cliente as cliente, fin.ruc as ruc, fin.total_bruto as total_bruto, fin.total_neto as total_neto, fin.descuento as descuento, fin.cod_estado as cod_estado, fin.glosa as glosa, fin.comp_gen as comp_gen, fin.cod_nv as cod_nv, fin.cod_moneda as cod_moneda, fin.cod_tc as cod_tc, fin.cod_tipo as cod_tipo, sum(fin.monto_10) as monto_10, sum(fin.iva_10) as iva_10, sum(fin.monto_5) as monto_5, sum(fin.iva_5) as iva_5, sum(fin.exento) as exento, fin.cod_tipo_doc as cod_tipo_doc
from
(select t.codigo as codigo, t.nro_fac as nro_fac, t.nro_auto as nro_auto, t.fecha as fecha, t.cod_cli as cod_cli, t.cliente as cliente, t.ruc as ruc, t.total_bruto as total_bruto, t.total_neto as total_neto, t.descuento as descuento, t.cod_estado as cod_estado, t.glosa as glosa, t.comp_gen as comp_gen, t.cod_nv as cod_nv, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, t.cod_tipo as cod_tipo, t.monto_10 as monto_10,  t.iva_10 as iva_10, t.monto_5 as monto_5, t.iva_5 as iva_5, t.exento as exento, t.cod_tipo_doc as cod_tipo_doc
from
(select a.pkyDiscountsDevolutions as codigo, a.fldNumber as nro_fac, 
a.fldAuthorization as nro_auto, a.fldDate as fecha, a.codCompany as cod_cli, a.fldName as cliente, a.fldNIT as ruc, a.fldGrossTotal * -1 as total_bruto, a.fldNetTotal * -1 as total_neto, 0 as descuento, 0 as cod_estado, a.txtDescription as glosa, a.fldParentComponent as comp_gen, a.cosComponentKey as cod_nv, a.codCurrency as cod_moneda, a.codExchangeRate as cod_tc, 1 as cod_tipo,  
case when d.fldTax = 10 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) * -1 else 0 end as monto_10, 
case when d.fldTax = 10 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) * -1 else 0 end as iva_10,
case when d.fldTax = 5 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) * -1 else 0 end as monto_5, 
case when d.fldTax = 5 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) * -1 else 0 end as iva_5,
case when d.fldExempt <> 0 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*(d.fldExempt/100) * -1 else 0 end as exento, 2 as cod_tipo_doc
from
txsDiscountsDevolutions a, txsAuthorizationControl b, txsDiscountsDevolutionsDetail d
where 
a.fldAuthorization = b.fldAuthorization and 
a.pkyDiscountsDevolutions = d.codDiscountsDevolutions and 
a.fldIsReceived = 0 and 
$valor_combinacion  
$valor_cliente2 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin') t 
where 
t.cod_moneda = '$cod_moneda_principal' 

union all 

select t.codigo as codigo, t.nro_fac as nro_fac, t.nro_auto as nro_auto, t.fecha as fecha, t.cod_cli as cod_cli, t.cliente as cliente, t.ruc as ruc, t.total_bruto * tc.fldRate as total_bruto, t.total_neto * tc.fldRate as total_neto, t.descuento * tc.fldRate as descuento, t.cod_estado as cod_estado, t.glosa as glosa, t.comp_gen as comp_gen, t.cod_nv as cod_nv, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, t.cod_tipo as cod_tipo, t.monto_10 * tc.fldRate as monto_10, t.iva_10 * tc.fldRate as iva_10, t.monto_5 * tc.fldRate as monto_5, t.iva_5 * tc.fldRate as iva_5, t.exento * tc.fldRate as exento, t.cod_tipo_doc as cod_tipo_doc
from
(select a.pkyDiscountsDevolutions as codigo, a.fldNumber as nro_fac, 
a.fldAuthorization as nro_auto, a.fldDate as fecha, a.codCompany as cod_cli, a.fldName as cliente, a.fldNIT as ruc, a.fldGrossTotal * -1 as total_bruto, a.fldNetTotal * -1 as total_neto, 0 as descuento, 0 as cod_estado, a.txtDescription as glosa, a.fldParentComponent as comp_gen, a.cosComponentKey as cod_nv, a.codCurrency as cod_moneda, a.codExchangeRate as cod_tc, 1 as cod_tipo,  
case when d.fldTax = 10 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) * -1 else 0 end as monto_10, 
case when d.fldTax = 10 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) * -1 else 0 end as iva_10,
case when d.fldTax = 5 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100) * -1 else 0 end as monto_5, 
case when d.fldTax = 5 then ((d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*((100-d.fldExempt)/100))*(d.fldTax/100) * -1 else 0 end as iva_5,
case when d.fldExempt <> 0 then (d.fldTotalNetPrice/(((d.fldTax/100)*((100-d.fldExempt)/100))+1))*(d.fldExempt/100) * -1 else 0 end as exento, 2 as cod_tipo_doc
from
txsDiscountsDevolutions a, txsAuthorizationControl b, txsDiscountsDevolutionsDetail d
where 
a.fldAuthorization = b.fldAuthorization and 
a.pkyDiscountsDevolutions = d.codDiscountsDevolutions and 
a.fldIsReceived = 0 and 
$valor_combinacion 
$valor_cliente2 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin') t, actExchangeRateDetail tc 
where 
tc.codTargetCurrency = t.cod_moneda and
tc.codBaseCurrency = '$cod_moneda_principal' and
tc.codExchangeRate = t.cod_tc) fin 
group by fin.codigo) fin2
order by fin2.fecha, fin2.nro_fac;");
		
//$numero_filas = mysqli_num_rows($result);

$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'];
	$matriz_ventas [$i][5] = $row['cod_cli'];
	$matriz_ventas [$i][6] = $row['cliente'];
	$matriz_ventas [$i][7] = $row['ruc'];
	$matriz_ventas [$i][8] = $row['total_bruto'];
	$matriz_ventas [$i][9] = $row['total_neto'];
	$matriz_ventas [$i][10] = $row['descuento'];
	$matriz_ventas [$i][11] = $row['cod_estado'];
	$matriz_ventas [$i][12] = $row['glosa'];
	$matriz_ventas [$i][13] = $row['comp_gen'];
	$matriz_ventas [$i][14] = $row['cod_nv'];
	$matriz_ventas [$i][15] = $row['cod_moneda'];
	$matriz_ventas [$i][16] = $row['cod_tc'];
	$matriz_ventas [$i][17] = $row['cod_tipo'];
	$matriz_ventas [$i][18] = $row['monto_10'];
	$matriz_ventas [$i][19] = $row['monto_5'];
	$matriz_ventas [$i][20] = $row['exento'];
	$matriz_ventas [$i][21] = 0; //retenciones
	$matriz_ventas [$i][22] = 'CO'; //CO CR
	//if($row['cod_tipo'] == 1){ $matriz_ventas [$i][22] = 'CO'; }
	if($row['cod_tipo'] == 5){ $matriz_ventas [$i][22] = 'CR'; }
	$matriz_ventas [$i][23] = $row['iva_10'];
	$matriz_ventas [$i][24] = $row['iva_5'];
	$matriz_ventas [$i][25] = $row['cod_tipo_doc'];
	/*
	if($row['cod_estado'] == 1){ $matriz_ventas [$i][18] = 'V'; }
	if($row['cod_estado'] == 2){ $matriz_ventas [$i][18] = 'A'; }
	if($row['cod_estado'] == 3){ $matriz_ventas [$i][18] = 'E'; }
	if($row['cod_estado'] == 4){ $matriz_ventas [$i][18] = 'N'; }
	if($row['cod_estado'] == 5){ $matriz_ventas [$i][18] = 'C'; }
	*/
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);

$numero_filas = $fin;
$var = ceil($numero_filas/$filas_por_pagina); //redondea hacia arriba

$total_imp_10_par = 0;
$total_iva_10_par = 0;
$total_imp_5_par = 0;
$total_iva_5_par = 0;
$total_ex_par = 0;
$total_tot_par = 0;
$total_ret_par = 0;

$total_imp_10 = 0;
$total_iva_10 = 0;
$total_imp_5 = 0;
$total_iva_5 = 0;
$total_ex = 0;
$total_tot = 0;
$total_ret = 0;

$total_gen3_imp_10 = 0;
$total_gen3_iva_10 = 0;
$total_gen3_imp_5 = 0;
$total_gen3_iva_5 = 0;
$total_gen3_ex = 0;
$total_gen3_tot = 0;

$total_deb_imp_10 = 0;
$total_deb_iva_10 = 0;
$total_deb_imp_5 = 0;
$total_deb_iva_5 = 0;
$total_deb_ex = 0;
$total_deb_tot = 0;

$total_co_imp_10 = 0;
$total_co_iva_10 = 0;
$total_co_imp_5 = 0;
$total_co_iva_5 = 0;
$total_co_ex = 0;
$total_co_tot = 0;

$total_cr_imp_10 = 0;
$total_cr_iva_10 = 0;
$total_cr_imp_5 = 0;
$total_cr_iva_5 = 0;
$total_cr_ex = 0;
$total_cr_tot = 0;

//verifico que la fecha inicial y la fecha final son el primer y ultimo del mismo mes
$d1 = date("d",strtotime($fechaini));
$d2 = date("d",strtotime($fechafin));
$m1 = date("m",strtotime($fechaini));
$m2 = date("m",strtotime($fechafin));
$a1 = date("Y",strtotime($fechaini));
$a2 = date("Y",strtotime($fechafin));
$dif = diasMes($fechaini);
if($d1 == 1 && $m1 == $m2 && $a1 == $a2 && $dif == $d2) {
	$nombre_mes = nombreMes($m1);	
} else {
	$nombre_mes = NULL;	
}

if($numero_filas == 0) {//if $numero_filas
$i = 1;
$var = 1;
$filas_por_pagina = 62;
}//fin if $numero_filas

//este if es para mostrar con el formato de libro de ventas
if($mostrar == 0) { //mostrar 0

$j = 1;
//$nu = 1;
for($i = 1; $i <= $var; $i++) {//for
?>
<table border="0" align="center" cellpadding="2" width="990">
<tr>
	<td rowspan="6" width="200">
	<img src="../../Images/logo.jpg" />
	</td>
</tr>
<tr>
	<td width="670"><b><font style="font-size:14px"><?php echo $nombre_empresa; ?></font></b></td>
    <td width="120">Fecha: <?php echo date("d/m/Y",strtotime($fecha_hoy)); ?></td>
</tr>
<tr>
    <td><?php echo $dir_sucursal; ?></td>
    <td>Hora: <?php echo date("H:i:s"); ?></td>
</tr>
<tr>
	<td>RUC: <?php echo $mi_nit; ?></td>
    <td>&nbsp;</td>
</tr>

<tr>
	<td width="990" colspan="2">&nbsp;</td>
</tr>
<tr>
    <td width="990" colspan="2">LIBRO VENTAS LEY 125/91 &nbsp;
    <?php if($nombre_mes == NULL) { ?>
    DEL <?php echo date("d/m/Y",strtotime($fechaini)); ?> AL <?php echo date("d/m/Y",strtotime($fechafin)); ?>
    <?php } else { ?>
    MES DE: <?php echo strtoupper($nombre_mes); ?>/<?php echo $a1; ?>
    <?php } ?>
    </td>
</tr>
<!--
<tr>
	<td width="485"><b>No. Sucursal: </b><?php echo $id_sucursal; ?></td>
    <td width="485"><b>Direcci&oacute;n: </b><?php echo $dir_sucursal; ?></td>
</tr>
-->
</table>

<br />

<table border="1" align="center" cellpadding="2" width="990" height="1200">
    <TR> 
    <TD align="center" colspan="2" style='border-bottom: solid 1px'><b>DOCUMENTO</b></TD>
    <TD align="center" colspan="2" style='border-bottom: solid 1px'><b>CLIENTE</b></TD>
    <TD align="center" colspan="2" style='border-bottom: solid 1px'><b>10%</b></TD>
    <TD align="center" colspan="2" style='border-bottom: solid 1px'><b>5%</b></TD>
    <TD align="center">&nbsp;</TD>
    <TD align="center">&nbsp;</TD>
    <TD align="center">&nbsp;</TD>
    <?php if($numero_filas != 0) { ?>
    <TD align="center" width="10" rowspan="2" style='border: none'>&nbsp;</TD>
    <?php } ?>
    </TR>
    <TR>
    <TD align="center" width="10" style='border-bottom: solid 1px'><b>D&Iacute;A</b></TD>
    <TD align="center" width="40" style='border-bottom: solid 1px'><b>N&Uacute;MERO</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>RUC</b></TD>
    <TD align="center" width="300" style='border-bottom: solid 1px'><b>RAZ&Oacute;N SOCIAL/NOMBRE(S)</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>GRAVADAS</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>IVA</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>GRAVADAS</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>IVA</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>EXENTAS</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>TOTAL</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>RETENCIONES</b></TD>
    </TR>
    
    <?php if($i > 1) { //if tras ?>
		
    <tr>
    <td colspan="4" align="center" style='border-bottom: dashed 1px'><b>TRANSPORTE... </b></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_imp_10_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_iva_10_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_imp_5_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_iva_5_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_ex_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_tot_par,$n_decimales176); ?></td>
    <td align="right" style='border-bottom: dashed 1px'><b><?php echo number_format($total_ret_par,$n_decimales176); ?></td>
    <td style='border: none'>&nbsp;</td>
    </tr>
    
    <?php } //fin if tras ?>

<?php
/*
$total_imp_10_par = 0;
$total_iva_10_par = 0;
$total_imp_5_par = 0;
$total_iva_5_par = 0;
$total_ex_par = 0;
$total_tot_par = 0;
$total_ret_par = 0;
*/
$color = 2;
$q = 1;//
while ($q <= $filas_por_pagina) { //while
    
  if($numero_filas == 0) {//if filas
	//printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	printf("<tr><td align='center'>&nbsp;</td>");//fecha vacio
  } else { //else filas
    if($color % 2 == 0) { //if color
		if($matriz_ventas [$j][4] != NULL) {
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", date("d",strtotime($matriz_ventas [$j][4])));//fecha dia
		} else { 
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>&nbsp;</td>");//fecha vacio
		}
	} else {
		if($matriz_ventas [$j][4] != NULL) {
		printf("<tr><td align='center'>%s</td>", date("d",strtotime($matriz_ventas [$j][4])));//fecha dia
		} else { 
		printf("<tr><td align='center'>&nbsp;</td>");//fecha vacio
		}
	}//fin if color
  }//fin filas
  
    $texto = $matriz_ventas [$j][6];//proveedor
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	$texto = substr($texto,0,150);
	
    printf("<td align='right'>%s</td>", $matriz_ventas [$j][2]);//nro. factura
	if($matriz_ventas [$j][11] == 2 || $matriz_ventas [$j][11] == 3 || $matriz_ventas [$j][11] == 4) {//if si es A
		printf("<td align='right'>XXX</td>");//ruc
		$texto = "FACTURA ANULADA";
		$matriz_ventas [$j][18] = 0;
		$matriz_ventas [$j][23] = 0;
		$matriz_ventas [$j][19] = 0;
		$matriz_ventas [$j][24] = 0;	
		$matriz_ventas [$j][20] = 0;
		$total_final = 0;
		$matriz_ventas [$j][21] = 0;
	} else {
		printf("<td align='right'>%s</td>", $matriz_ventas [$j][7]);//ruc
	}//fin if si es A
	
	printf("<td align='left'>%s</td>", $texto);//proveedor
	
	$importe_10 = round($matriz_ventas [$j][18],$n_decimales176);
	$iva_10 = round($matriz_ventas [$j][23],$n_decimales176);
	$importe_5 = round($matriz_ventas [$j][19],$n_decimales176);
	$iva_5 = round($matriz_ventas [$j][24],$n_decimales176);	
	$exento = round($matriz_ventas [$j][20],$n_decimales176);
	$total_final = $importe_10 + $iva_10 + $importe_5 + $iva_5 + $exento;
	$retencion = round($matriz_ventas [$j][21],$n_decimales176);
	
	
	if($matriz_ventas [$j][11] == NULL) {//if si es 0
	printf("<td align='right'>%s</td>", NULL);//gravadas 10
	printf("<td align='right'>%s</td>", NULL);//iva 10
	printf("<td align='right'>%s</td>", NULL);//gravadas 5
	printf("<td align='right'>%s</td>", NULL);//iva 5
	printf("<td align='right'>%s</td>", NULL);//exenta
	printf("<td align='right'>%s</td>", NULL);//total
	printf("<td align='right'>%s</td></tr>", NULL);//retenciones
		
	} else { //else si es 0
	printf("<td align='right'>%s</td>", number_format($importe_10,$n_decimales176));//gravadas 10
	printf("<td align='right'>%s</td>", number_format($iva_10,$n_decimales176));//iva 10
	printf("<td align='right'>%s</td>", number_format($importe_5,$n_decimales176));//gravadas 5
	printf("<td align='right'>%s</td>", number_format($iva_5,$n_decimales176));//iva 5
	printf("<td align='right'>%s</td>", number_format($exento,$n_decimales176));//exento
	printf("<td align='right'>%s</td>", number_format($total_final,$n_decimales176));//total final
	printf("<td align='right'>%s</td>", number_format($retencion,$n_decimales176));//retencion
	printf("<td align='left'>%s</td></tr>", $matriz_ventas [$j][22]);//co, cr
	
	$total_imp_10_par = $total_imp_10_par + $importe_10;
	$total_iva_10_par = $total_iva_10_par + $iva_10;
	$total_imp_5_par = $total_imp_5_par + $importe_5;
	$total_iva_5_par = $total_iva_5_par + $iva_5;
	$total_ex_par = $total_ex_par + $exento;
	$total_tot_par = $total_tot_par + $total_final;
	$total_ret_par = $total_ret_par + $retencion;
	
	$total_imp_10 = $total_imp_10 + $importe_10;
	$total_iva_10 = $total_iva_10 + $iva_10;
	$total_imp_5 = $total_imp_5 + $importe_5;
	$total_iva_5 = $total_iva_5 + $iva_5;
	$total_ex = $total_ex + $exento;
	$total_tot = $total_tot + $total_final;
	$total_ret = $total_ret + $retencion;
	
	if($importe_10 < 0 || $importe_5 < 0 || $exento < 0) { //if gen3
	$total_deb_10 = $total_deb_10 + $importe_10;
	$total_deb_iva_10 = $total_deb_iva_10 + $iva_10;
	$total_deb_5 = $total_deb_5 + $importe_5;
	$total_deb_iva_5 = $total_deb_iva_5 + $iva_5;
	$total_deb_ex = $total_deb_ex + $exento;
	$total_deb_tot = $total_deb_tot + $total_final;
	} else {
	$total_gen3_10 = $total_gen3_10 + $importe_10;
	$total_gen3_iva_10 = $total_gen3_iva_10 + $iva_10;
	$total_gen3_5 = $total_gen3_5 + $importe_5;
	$total_gen3_iva_5 = $total_gen3_iva_5 + $iva_5;
	$total_gen3_ex = $total_gen3_ex + $exento;
	$total_gen3_tot = $total_gen3_tot + $total_final;
	}//fin if gen3
	
	if($matriz_ventas [$j][22] == 'CO') { //if cocr
	$total_co_10 = $total_co_10 + $importe_10;
	$total_co_iva_10 = $total_co_iva_10 + $iva_10;
	$total_co_5 = $total_co_5 + $importe_5;
	$total_co_iva_5 = $total_co_iva_5 + $iva_5;
	$total_co_ex = $total_co_ex + $exento;
	$total_co_tot = $total_co_tot + $total_final;
	} else {
	$total_cr_10 = $total_cr_10 + $importe_10;
	$total_cr_iva_10 = $total_cr_iva_10 + $iva_10;
	$total_cr_5 = $total_cr_5 + $importe_5;
	$total_cr_iva_5 = $total_cr_iva_5 + $iva_5;
	$total_cr_ex = $total_cr_ex + $exento;
	$total_cr_tot = $total_cr_tot + $total_final;
	}//fin if cocr
	//echo $total_gen3_10.' - '.$importe_10.'<br/>';
	
	}//fin if si es 0
			

		
	if($numero_filas == 0 && $j == 32) {
		printf("<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td align='center' colspan='6'><font size='16'><b>SIN MOVIMIENTO</b></font></td><td>&nbsp;</td><td>&nbsp;</td></tr>");
	}
	
	$color++;
	$j++;
	$q++;
}//fin while
	
?>
<?php if($i != $var) {//if comp ?>
<tr>
<td colspan="4" align="center" style='border-top: dashed 1px'><b>TRANSPORTE... </b></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_imp_10_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_iva_10_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_imp_5_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_iva_5_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_ex_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_tot_par,$n_decimales176); ?></td>
<td align="right" style='border-top: dashed 1px'><b><?php echo number_format($total_ret_par,$n_decimales176); ?></td>
<td style='border: none'>&nbsp;</td>
</tr>
<?php } else {//else if comp ?>
<tr>
<td colspan="4" align="center" style='border-top: solid 1px'><b>TOTALES: </b></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_imp_10,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_iva_10,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_imp_5,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_iva_5,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_ex,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_tot,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_ret,$n_decimales176); ?></td>
<?php if($numero_filas != 0) { ?>
<td style='border: none'>&nbsp;</td>
<?php } ?>
</tr>
<!--
<tr>
<td colspan="4" align="center" style='border-top: solid 1px'><b>REDONDEO: </b></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_imp_10,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_iva_10,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_imp_5,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_iva_5,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_ex,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_tot,$n_decimales176); ?></td>
<td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_ret,$n_decimales176); ?></td>
<?php if($numero_filas != 0) { ?>
<td style='border: none'>&nbsp;</td>
<?php } ?>
</tr>
-->
<?php }//fin if comp ?>

</table>

<table border="0" align="center" cellpadding="2" width="990">
<tr>
<td align="right">P&aacute;gina: <?php echo $i; ?> de <?php echo $var; ?></td>
</tr>
</table>

<?php
	if($i != $var) {//if $i
	?>
		<div>
		</div>
	<?php
	}//fin if $i
}//fin for

?>
<br/><br/>
<table border="1" align="center" cellpadding="2" width="700">
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>EXENTAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>TOTAL</b></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS CONTADO: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_iva_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_iva_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_ex,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_tot,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS CR&Eacute;DITO: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_iva_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_iva_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_ex,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_cr_tot,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL GENERAL: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_10+$total_cr_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_iva_10+$total_cr_iva_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_5+$total_cr_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_iva_5+$total_cr_iva_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_ex+$total_cr_ex,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_co_tot+$total_cr_tot,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>EXENTAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>TOTAL</b></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS 10%: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_iva_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_10+$total_gen3_iva_10,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS 5%: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_iva_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_5+$total_gen3_iva_5,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL VENTAS EXENTAS: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format(0,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_ex,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_ex,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL GENERAL: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_iva_10,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_iva_5,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_ex,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_gen3_tot,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL NOTAS DE CR&Eacute;DITO EMITIDAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 10%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>GRAVADAS 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>IVA 5%</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>EXENTAS</b></td>
<td align="center" width="55" style='border-top: solid 1px'><b>TOTAL</b></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>NOTAS DE CR&Eacute;DITO EMITIDAS: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_10*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_iva_10*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_5*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_iva_5*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_ex*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_tot*-1,$n_decimales176); ?></td>
</tr>
<tr>
<td align="right" width="200" style='border-top: solid 1px'><b>TOTAL GENERAL: </b></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_10*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_iva_10*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_5*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_iva_5*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_ex*-1,$n_decimales176); ?></td>
<td align="right" width="55" style='border-top: solid 1px'><b><?php echo number_format($total_deb_tot*-1,$n_decimales176); ?></td>
</tr>
</table>
<?php

}// fin if mostrar 0


//este if es para mostrar con el formato detalle de facturas
if($mostrar == 1) { //mostrar 1

?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Facturas Ventas</b></font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="33%"><b>Cliente: </b><?php echo $nom_cliente; ?></TD>
        <TD width="33%"><b>Sucursal: </b><?php echo $sucursal; ?></TD>
        <TD width="33%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_moneda_principal; ?></TD>
        </TR>
    </table>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br />

<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
 	<TD align="center"><b>No.</b></TD>
    <TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>N&uacute;mero</b></TD>
    <TD align="center"><b>RUC</b></TD>
    <TD align="center"><b>Raz&oacute;n Social/Nombre(s)</b></TD>
    <TD align="center"><b>Gravadas 10%</b></TD>
    <TD align="center"><b>IVA 10%</b></TD>
    <TD align="center"><b>Gravadas 5%</b></TD>
    <TD align="center"><b>IVA 5%</b></TD>
    <TD align="center"><b>Exentas</b></TD>
    <TD align="center"><b>Total</b></TD>
    <TD align="center"><b>Retenciones</b></TD>
    <TD align="center"><b>CO/CR</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Generado por</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$j = 1;
$nu = 1;
$total_imp_10 = 0;
$total_iva_10 = 0;
$total_imp_5 = 0;
$total_iva_5 = 0;
$total_ex = 0;
$total_tot = 0;
$total_ret = 0;

$total_gen3_imp_10 = 0;
$total_gen3_iva_10 = 0;
$total_gen3_imp_5 = 0;
$total_gen3_iva_5 = 0;
$total_gen3_ex = 0;
$total_gen3_tot = 0;

$total_deb_imp_10 = 0;
$total_deb_iva_10 = 0;
$total_deb_imp_5 = 0;
$total_deb_iva_5 = 0;
$total_deb_ex = 0;
$total_deb_tot = 0;

$total_co_imp_10 = 0;
$total_co_iva_10 = 0;
$total_co_imp_5 = 0;
$total_co_iva_5 = 0;
$total_co_ex = 0;
$total_co_tot = 0;

$total_cr_imp_10 = 0;
$total_cr_iva_10 = 0;
$total_cr_imp_5 = 0;
$total_cr_iva_5 = 0;
$total_cr_ex = 0;
$total_cr_tot = 0;

while ($j <= $fin) { //while
  if($matriz_ventas [$j][11] == 1 || $matriz_ventas [$j][11] == 0) {
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $nu);//numero
	} else {
	printf("<tr><td align='center'>%s</td>", $nu);//numero
	}//fin if color
	
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	$codigo_factura = $matriz_ventas [$j][1];
	if($matriz_ventas [$j][11] == 1) {
	printf("<td align='right'><a href='../../ModuleTaxes/VoucherSalesLedger/index.php?pkySalesLedger=$codigo_factura&sheetSize=$formato_factura&head=$muestra_cabecera&user=$usuario&token=$token' title='Factura'>%s</a></td>", $matriz_ventas [$j][2]);//nro. factura
	} else {
	printf("<td align='right'><a href='../../ModuleTaxes/VoucherDiscountsDevolutions/index.php?pkyDiscountsDevolutions=$codigo_factura&user=$usuario&token=$token' title='Nota Descuentos / Devoluciones'>%s</a></td>", $matriz_ventas [$j][2]);//nota debito/credito	
	}
  	printf("<td align='right'>%s</td>", $matriz_ventas [$j][7]);//ruc
	$texto = $matriz_ventas [$j][6];//cliente
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	//$texto = substr($texto,0,50);
	
	printf("<td align='left'>%s</td>", $texto);//cliente
	
	$importe_10 = round($matriz_ventas [$j][18],$n_decimales176);
	$iva_10 = round($matriz_ventas [$j][23],$n_decimales176);
	$importe_5 = round($matriz_ventas [$j][19],$n_decimales176);
	$iva_5 = round($matriz_ventas [$j][24],$n_decimales176);	
	$exento = round($matriz_ventas [$j][20],$n_decimales176);
	$total_final = $importe_10 + $iva_10 + $importe_5 + $iva_5 + $exento;
	$retencion = round($matriz_ventas [$j][21],$n_decimales176);
	
	printf("<td align='right'>%s</td>", number_format($importe_10,$n_decimales176));//gravadas 10
	printf("<td align='right'>%s</td>", number_format($iva_10,$n_decimales176));//iva 10
	printf("<td align='right'>%s</td>", number_format($importe_5,$n_decimales176));//gravadas 5
	printf("<td align='right'>%s</td>", number_format($iva_5,$n_decimales176));//iva 5
	printf("<td align='right'>%s</td>", number_format($exento,$n_decimales176));//exento
	printf("<td align='right'>%s</td>", number_format($total_final,$n_decimales176));//total final
	printf("<td align='right'>%s</td>", number_format($retencion,$n_decimales176));//retencion
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][22]);//co, cr
	$glosa2 = $matriz_ventas [$j][12];
	$glosa2 = nl2br($glosa2);//quita salto de linea 
	$glosa2 = str_replace('\n','<br />',$glosa2);//reemplaza para salto de linea
	printf("<td>%s</td>", $glosa2);//glosa
	$codigo_nota_venta = $matriz_ventas [$j][14];
	printf("<td align='right'><a href='../../ModuleSales/VoucherSalesNote/index.php?pkySalesNote=$codigo_nota_venta&user=$usuario&token=$token' title='Nota de Venta'>%s</a></td></tr>", $matriz_ventas [$j][14]);//generado por nota de venta
	
	$total_imp_10 = $total_imp_10 + $importe_10;
	$total_iva_10 = $total_iva_10 + $iva_10;
	$total_imp_5 = $total_imp_5 + $importe_5;
	$total_iva_5 = $total_iva_5 + $iva_5;
	$total_ex = $total_ex + $exento;
	$total_tot = $total_tot + $total_final;
	$total_ret = $total_ret + $retencion;
	
	if($importe_10 < 0 || $importe_5 < 0 || $exento < 0) { //if gen3
	$total_deb_10 = $total_deb_10 + $importe_10;
	$total_deb_iva_10 = $total_deb_iva_10 + $iva_10;
	$total_deb_5 = $total_deb_5 + $importe_5;
	$total_deb_iva_5 = $total_deb_iva_5 + $iva_5;
	$total_deb_ex = $total_deb_ex + $exento;
	$total_deb_tot = $total_deb_tot + $total_final;
	} else {
	$total_gen3_10 = $total_gen3_10 + $importe_10;
	$total_gen3_iva_10 = $total_gen3_iva_10 + $iva_10;
	$total_gen3_5 = $total_gen3_5 + $importe_5;
	$total_gen3_iva_5 = $total_gen3_iva_5 + $iva_5;
	$total_gen3_ex = $total_gen3_ex + $exento;
	$total_gen3_tot = $total_gen3_tot + $total_final;
	}//fin if gen3
	
	if($matriz_ventas [$j][22] == 'CO') { //if cocr
	$total_co_10 = $total_co_10 + $importe_10;
	$total_co_iva_10 = $total_co_iva_10 + $iva_10;
	$total_co_5 = $total_co_5 + $importe_5;
	$total_co_iva_5 = $total_co_iva_5 + $iva_5;
	$total_co_ex = $total_co_ex + $exento;
	$total_co_tot = $total_co_tot + $total_final;
	} else {
	$total_cr_10 = $total_cr_10 + $importe_10;
	$total_cr_iva_10 = $total_cr_iva_10 + $iva_10;
	$total_cr_5 = $total_cr_5 + $importe_5;
	$total_cr_iva_5 = $total_cr_iva_5 + $iva_5;
	$total_cr_ex = $total_cr_ex + $exento;
	$total_cr_tot = $total_cr_tot + $total_final;
	}//fin if cocr
	
	$color++;
	}
    $nu++;
	$j++;

}//fin while

?>
 
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL</b></td>
<td align="right"><b><?php echo number_format($total_imp_10,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_iva_10,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_imp_5,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_iva_5,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_ex,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_tot,$n_decimales176); ?></td>
<td align="right"><b><?php echo number_format($total_ret,$n_decimales176); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>

</tr>
<tr><td align="center" colspan="15"><b>TOTAL VENTAS</b></td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL VENTAS CONTADO: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_iva_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_iva_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_ex,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_tot,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL VENTAS CR&Eacute;DITO: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_iva_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_iva_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_ex,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cr_tot,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL GENERAL: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_10+$total_cr_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_iva_10+$total_cr_iva_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_5+$total_cr_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_iva_5+$total_cr_iva_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_ex+$total_cr_ex,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_co_tot+$total_cr_tot,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td align="center" colspan="15"><b>TOTAL VENTAS</b></td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL VENTAS 10%: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_iva_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_10+$total_gen3_iva_10,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL VENTAS 5%: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_iva_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_5+$total_gen3_iva_5,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL VENTAS EXENTAS: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format(0,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_ex,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_ex,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL GENERAL: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_iva_10,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_iva_5,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_ex,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_gen3_tot,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td align="center" colspan="15"><b>TOTAL NOTAS DE CR&Eacute;DITO RECIBIDAS</b></td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>NOTAS DE D&Eacute;BITO: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_10*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_iva_10*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_5*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_iva_5*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_ex*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_tot*-1,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr BGCOLOR='#ffcccc'><td colspan="5"><b>TOTAL GENERAL: </b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_10*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_iva_10*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_5*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_iva_5*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_ex*-1,$n_decimales176)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_deb_tot*-1,$n_decimales176)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

</tbody>
</table>
         

<?php
}// fin if mostrar 1

} //fin if 2

} //fin if paraguay
?>

</body>
</html>
