<!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>
<?php
include "../../conn.php";
include "../../general.php";

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

?>
<?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 = 33;

//responsable
$result2 = mysqli_query($link, "select a.codPerson as cod_persona, concat(b.fldFirstName,' ', b.fldLastName) 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);	

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['cliente'];
	}
	mysqli_free_result($resultcli);
} //fin if cliente

//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, a.txtDescription as glosa, a.cosComponentKey as cod_nv
from
txsSalesLedger a, txsAuthorizationControl b
where 
a.codAuthorizationControl = b.pkyAuthorizationControl and 
b.codCompanyBranch = '$cod_sucursal' and 
$valor_cliente 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' 
order by nro_fac, fecha;");
		
$numero_filas = mysqli_num_rows($result);
$var = ceil($numero_filas/$filas_por_pagina); //redondea hacia arriba

$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'];
	$matriz_ventas [$i][15] = $row['glosa'];
	$matriz_ventas [$i][16] = $row['cod_nv'];
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);


$total_fac_par = 0;
$total_ice_par = 0;
$total_exe_par = 0;
$total_neto_par = 0;
$total_cred_par = 0;

$total_fac = 0;
$total_ice = 0;
$total_exe = 0;
$total_neto = 0;
$total_cred = 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;
for($i = 1; $i <= $var; $i++) {//for
?>
<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>
<br />

<table border="1" align="center" cellpadding="2" width="990" height="600">
    <TR> 
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Fecha</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>NIT/CI</b></TD>
    <TD align="center" width="430" style='border-bottom: solid 1px'><b>Nombre o Raz&oacute;n Social del Comprador</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="80" style='border-bottom: solid 1px'><b>Cod. Control</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Total Factura</b></TD>
    <TD align="center" width="45" style='border-bottom: solid 1px'><b>Total ICE</b></TD>
    <TD align="center" width="45" style='border-bottom: solid 1px'><b>Importe Excento</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Importe Neto</b></TD>
    <TD align="center" width="55" style='border-bottom: solid 1px'><b>Deb. Fiscal IVA</b></TD>
    <TD align="center" width="10" style='border-bottom: solid 1px'><b>Estado</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>", $matriz_ventas [$j][4]);//fecha
		} else { 
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>&nbsp;</td>");//fecha vacia
		}
	} else {
		if($matriz_ventas [$j][4] != NULL) {
		printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
		} else { 
		printf("<tr><td align='center'>&nbsp;</td>");//fecha vacia
		}
	}//fin if color
  }//fin filas
	if($matriz_ventas [$j][14] == 'A') {
	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,100);
	if($matriz_ventas [$j][14] == 'A') {
		$texto = 'ANULADA';
	}
	printf("<td align='left'>%s</td>", $texto);//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
	if($matriz_ventas [$j][14] == 'A') {
		$matriz_ventas [$j][5] = 0;
	}
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][5]);//cod control
	
	if($matriz_ventas [$j][9] == 0) {//if si es 0
		printf("<td align='right'>%s</td>", NULL);//total factura
		printf("<td align='right'>%s</td>", NULL);//ice
		printf("<td align='right'>%s</td>", NULL);//excento
		printf("<td align='right'>%s</td>", NULL);//total neto
		printf("<td align='right'>%s</td>", NULL);//credito fiscal IVA
		printf("<td align='center'>%s</td></tr>", NULL);//estado A o V
		
	} else { //else si es 0
		if($matriz_ventas [$j][14] == 'A') {//if si es A
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//total factura
			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));//total neto
			printf("<td align='right'>%s</td>", number_format(0,$n_decimales));//credito fiscal IVA
			printf("<td align='center'>%s</td></tr>", $matriz_ventas [$j][14]);//estado A o V
		} else {//else si es A
			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></tr>", $matriz_ventas [$j][14]);//estado A o V
		}//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 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] == 'V'){
	$total_fac_par = $total_fac_par + $matriz_ventas [$j][9];
	$total_ice_par = $total_ice_par + $matriz_ventas [$j][10];
	$total_exe_par = $total_exe_par + $matriz_ventas [$j][11];
	$total_neto_par = $total_neto_par + $matriz_ventas [$j][12];
	$total_cred_par = $total_cred_par + $matriz_ventas [$j][13];
	
	$total_fac = $total_fac + $matriz_ventas [$j][9];
	$total_ice = $total_ice + $matriz_ventas [$j][10];
	$total_exe = $total_exe + $matriz_ventas [$j][11];
	$total_neto = $total_neto + $matriz_ventas [$j][12];
	$total_cred = $total_cred + $matriz_ventas [$j][13];
	}
    $color++;
	$j++;
	$q++;
}//fin while
	
?>

<tr>
	<td colspan="4" 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_neto_par,$n_decimales); ?></td>
    <td align="right" style='border-top: solid 1px'><b><?php echo number_format($total_cred_par,$n_decimales); ?></td>
    <td style='border-top: solid 1px'>&nbsp;</td>
</tr>
<tr>
	<td colspan="4" 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_neto,$n_decimales); ?></td>
    <td align="right"><b><?php echo number_format($total_cred,$n_decimales); ?></td>
    <td>&nbsp;</td>
</tr>
</table>

<?php
$total_fac_par = 0;
$total_ice_par = 0;
$total_exe_par = 0;
$total_neto_par = 0;
$total_cred_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><b>Empresa: <?php echo $nombre_empresa; ?><br/>NIT: <?php echo $mi_nit; ?></b>
</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><br/>
<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="25%"><b>Cliente: </b><?php echo $nom_cliente; ?></TD>
        <TD width="25%"><b>Sucursal: </b><?php echo $sucursal; ?></TD>
        <TD width="25%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="25%"><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; ?>" 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" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>NIT/CI</b></TD>
    <TD align="center"><b>Nombre o Raz&oacute;n Social de la Factura</b></TD>
    <TD align="center"><b>Nro. Factura</b></TD>
    <TD align="center"><b>Nro. Autorizaci&oacute;n</b></TD>
    <TD align="center" width="90"><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>Glosa</b></TD>
    <TD align="center"><b>Generado por</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$j = 1;
$total_fac = 0;
$total_ice = 0;
$total_exe = 0;
$total_neto = 0;
$total_cred = 0;
while ($j <= $fin) { //while
  if($matriz_ventas [$j][14] == 'V') {
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_ventas [$j][4]);//fecha
	}//fin if color
  
	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
	$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' 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='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
	$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='right'><a href='../../ModuleSales/VoucherSalesNote/index.php?pkySalesNote=$codigo_nota_venta&user=$usuario' title='Nota de Venta'>%s</a></td></tr>", $matriz_ventas [$j][16]);//generado por nota de venta
	
	$total_fac = $total_fac + $matriz_ventas [$j][9];
	$total_ice = $total_ice + $matriz_ventas [$j][10];
	$total_exe = $total_exe + $matriz_ventas [$j][11];
	$total_neto = $total_neto + $matriz_ventas [$j][12];
	$total_cred = $total_cred + $matriz_ventas [$j][13];
	$color++;
	}
    
	$j++;

}//fin while

?>
 
<tr BGCOLOR='#ffcccc'><td colspan="6"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_fac,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_ice,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_exe,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_neto,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cred,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
         

<?php
}// fin if mostrar 1

} //fin if 2

?>

</body>
</html>
