<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Bancarizaci&oacute;n</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />
</head>

<?php
include "../../conn.php";
include "../../general.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$mostrar=$_POST["mostrar"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];

//$mostrar_asiento_cc = $_GET["showCC"];
$n_decimales = 2;


?>
<body>
<?php


//Inicio reporte Compras bancarizacion

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

//compras
if($mostrar == 0) { //if mostrar 0

$nom_mostrar = 'Compras';

//listado de facturas compras, ojo toma la moneda de la cxp para el pago

$result = mysqli_query($link, "select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nro_doc, a.fldDate as fecha, a.codSupplier as cod_empresa, e.fldName as proveedor, e.fldNIT as nit, 
case a.codCurrency when $cod_moneda_principal then a.fldTotal else (select a1.fldRate from actExchangeRateDetail a1 where a1.codExchangeRate = a.codExchangeRate and a1.codTargetCurrency = a.codCurrency and a1.codBaseCurrency = $cod_moneda_principal) * a.fldTotal end as monto_cxp, 
a.fldContract as nro_contrato, d.codDocumentType as cod_tipo, d.fldInvoice as cod_fac, 
c.pkyPayment as cod_pago, 
b.pkyPaymentDetail as cod_pago_det, c.codRoute as cod_tipo_pago, 
case a.codCurrency when $cod_moneda_principal then b.fldTotal else (select b1.fldRate from actExchangeRateDetail b1 where b1.codExchangeRate = c.codExchangeRate and b1.codTargetCurrency = a.codCurrency and b1.codBaseCurrency = $cod_moneda_principal) * b.fldTotal end as monto_pago, 
c.fldDate as fecha_pago, c.codBankMovement as cod_mov_banco, c.codCheck as cod_cheque, c.codType as cod_tipo_doc_pago, f.fldName as tipo_doc_pago, c.fldDocumentNumber as nro_doc_pago 
from
payAccountsPayable a inner join payPaymentDetail b on 
a.pkyAccountsPayable = b.codAccountsPayable, payPayment c, payAccountsPayableDetail d, agnCompany e, pchPurchaseType f   
where 
a.fldBanking = 1 and 
a.pkyAccountsPayable = d.codAccountsPayable and 
a.codSupplier = e.pkyCompany and 
c.codType = f.pkyPurchaseType and 
c.pkyPayment = b.codPayment and 
((a.fldDate >= '$fechaini' and a.fldDate <= '$fechafin') or
(c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin')) and 
c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin' and 
a.sysEnabled = 1 AND a.sysAuthorized = 1 and 
c.sysEnabled = 1 AND c.sysAuthorized = 1
order by fecha, proveedor, monto_cxp, fecha_pago, monto_pago;");

/*
select a.pkyPurchasesLedger as codigo, a.fldNumber as nro_fac,
a.fldAuthorization as nro_auto, a.fldDate as fecha, 
a.fldName as proveedor, a.fldNIT as nit, a.fldGrossTotal as total_bruto, 
a.fldNetTotal as total_neto, a.fldParentComponent as papa, 
a.cosComponentKey as cod_papa, c.pkyPayment as cod_pago, 
b.pkyPaymentDetail as cod_pago_det, c.codRoute as cod_tipo_pago, c.codCurrency as cod_moneda, b.fldTotal as monto_pago, c.fldDate as fecha_pago, c.codBankMovement as cod_mov_banco, c.codCheck as cod_cheque
from
txsPurchasesLedger a inner join payPaymentDetail b on 
a.cosComponentKey = b.codAccountsPayable, payPayment c 
where 
a.fldNetTotal >= 50000 and 
c.pkyPayment = b.codPayment and 
((a.fldDate >= '$fechaini' and a.fldDate <= '$fechafin') or
(c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin')) and 
c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin' and 
a.sysEnabled = 1 AND a.sysAuthorized = 1 and 
c.sysEnabled = 1 AND c.sysAuthorized = 1
order by fecha, proveedor, nro_auto
*/

$matriz_compras = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_compras [$i][1] = $row['codigo'];
	$matriz_compras [$i][2] = $row['fecha'];
	$matriz_compras [$i][3] = $row['cod_empresa'];
	$matriz_compras [$i][4] = $row['proveedor'];
	$matriz_compras [$i][5] = $row['nit'];
	$matriz_compras [$i][6] = round($row['monto_cxp'],$n_decimales);
	$matriz_compras [$i][7] = $row['nro_contrato'];
	$matriz_compras [$i][8] = $row['cod_tipo'];
	$matriz_compras [$i][9] = $row['cod_fac'];
	$matriz_compras [$i][10] = $row['cod_pago'];
	$matriz_compras [$i][11] = $row['cod_pago_det'];
	$matriz_compras [$i][12] = $row['cod_tipo_pago'];
	$matriz_compras [$i][13] = round($row['monto_pago'],$n_decimales);
	$matriz_compras [$i][14] = $row['fecha_pago'];
	$matriz_compras [$i][15] = NULL; //compras: factura = 1, retenciones = 2, inmuebles = 3
	$matriz_compras [$i][16] = 0; //nro doc para retenciones = 2 e inmuebles = 3
	$matriz_compras [$i][17] = $row['nro_contrato'];
	$matriz_compras [$i][18] = $row['cod_mov_banco'];
	$matriz_compras [$i][19] = $row['cod_cheque'];
	$matriz_compras [$i][20] = $row['tipo_doc_pago']; //tipo doc pago  1 - 10
	$matriz_compras [$i][21] = $row['nro_doc']; //nro factura
	$matriz_compras [$i][22] = $row['nro_doc']; //nro autorizacion
	
	$matriz_compras [$i][23] = 0; //nro cta banco pago
	$matriz_compras [$i][24] = 0; //monto acumulado pago
	$matriz_compras [$i][25] = 0; //nit banco pago
	$matriz_compras [$i][26] = 0; //nro doc pago (cheque o transf)
	$matriz_compras [$i][27] = $row['codigo']; //cod_cxp
	$nro_doc_pago = $row['nro_doc_pago'];
	$cod_cxp = $row['codigo'];
	/* tipo doc pago
	1 cheque de cualquier naturaleza
	2 orden de transferencia
	3 Ordenes de transferencia electronica de fondos
	4 transferencia de fondos
	5 tarjeta de débito
	6 tarjeta de credito
	7 tarjeta prepagada
	8 deposito en cuenta
	9 cartas de credito
	10 otros */
	/*	
	//para enlazar al que genero el asiento
	$papa_componente = $matriz_compras [$i][11];
	$papa_codigo = $matriz_compras [$i][10];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	$matriz_compras [$i][28] = $papa_enlace;
	*/
	
	//si es factura
	if($matriz_compras [$i][8] == 1) {// if 0
		$cod_fac_compra = $matriz_compras [$i][9];
		$resultfac = mysqli_query($link, "select fldNumber as nro_fac, fldAuthorization as nro_auto 
			from txsPurchasesLedger
			where
			pkyPurchasesLedger = '$cod_fac_compra';");
		while ($row = mysqli_fetch_array($resultfac)) {
			$matriz_compras [$i][21] = $row['nro_fac'];
			$matriz_compras [$i][22] = $row['nro_auto'];
		}
		mysqli_free_result($resultfac);
		$matriz_compras [$i][15] = 1;
	}// fin if 0
	
	//si es retencion
	if($matriz_compras [$i][8] == 3) {// if 01
		$matriz_compras [$i][22] = 4;
		$matriz_compras [$i][15] = 2;
	}// fin if 01
	
	
	if($matriz_compras [$i][18] != NULL) { //if 1
		$cod_mov_banco = $matriz_compras [$i][18];
		$resultcta = mysqli_query($link, "select b.fldNumber as cuenta, d.fldNIT as nit_banco 
				from tryBankMovement a, tryBankDetail b, tryBank c, agnCompany d
				where 
				a.codBankAccount = b.pkyBankDetail and 
				a.codBank = c.pkyBank and 
				c.codCompany = d.pkyCompany and 
				a.pkyBankMovement = '$cod_mov_banco';");
		while ($row = mysqli_fetch_array($resultcta)) {
			//$cuenta = $row['cuenta'];
			//dejar solo numeros
			//$matriz_compras [$i][23] = intval(preg_replace('/[^0-9]+/', '', $cuenta), 10); //nro cta banco pago 
			$matriz_compras [$i][23] = $row['cuenta']; //nro cta banco pago
			$matriz_compras [$i][25] = $row['nit_banco']; //nit banco pago
		}
		mysqli_free_result($resultcta);
		$matriz_compras [$i][26] = $nro_doc_pago; //nro doc pago (cheque o transf)
	}// fin if 1
	
	
	//$cod_cxp = $matriz_compras [$i][10];
	$fecha_pago = $matriz_compras [$i][14];
	$resultacum = mysqli_query($link, "select sum(case c.codCurrency when $cod_moneda_principal then a.fldTotal else (select b1.fldRate from actExchangeRateDetail b1 where b1.codExchangeRate = b.codExchangeRate and b1.codTargetCurrency = c.codCurrency and b1.codBaseCurrency = $cod_moneda_principal) * a.fldTotal end) as pagos_acumulados
	from payPaymentDetail a, payPayment b, payAccountsPayable c 
	where 
	a.codPayment = b.pkyPayment and 
	a.codAccountsPayable = '$cod_cxp' and 
	a.codAccountsPayable = c.pkyAccountsPayable and 
	b.fldDate <= '$fecha_pago' and 
	b.sysEnabled = 1 and b.sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultacum)) {
		$matriz_compras [$i][24] = $row['pagos_acumulados']; //monto acumulado pago
	}
	mysqli_free_result($resultacum);
	
	if($matriz_compras [$i][19] != NULL) { //if 2
		$cod_cheque = $matriz_compras [$i][19];
		$resultche = mysqli_query($link, "select b.fldNumber as cuenta, d.fldNIT as nit_banco, a.fldNumber as numero 
				from tryCheck a, tryBankDetail b, tryBank c, agnCompany d
				where 
				a.codLocalAccount = b.pkyBankDetail and 
				a.codBank = c.pkyBank and 
				c.codCompany = d.pkyCompany and 
				a.pkyCheck = '$cod_cheque';");
		while ($row = mysqli_fetch_array($resultche)) {
			//$cuenta = $row['cuenta'];
			//dejar solo numeros
			//$matriz_compras [$i][23] = intval(preg_replace('/[^0-9]+/', '', $cuenta), 10); //nro cta banco cobro 
			$matriz_compras [$i][23] = $row['cuenta']; //nro cta banco cobro 
			$matriz_compras [$i][25] = $row['nit_banco']; //nit banco cobro
			$matriz_compras [$i][26] = $row['numero']; //nro doc cobro cheque
		}
		mysqli_free_result($resultche);
	}// fin if 2
	
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);


?>
<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>Bancarizaci&oacute;n Compras</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>Mostrar: </b><?php echo $nom_mostrar; ?></TD>
        <TD width="33%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="33%"><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"><b>Modalidad Transacci&oacute;n</b></TD>
    <TD align="center" width="65"><b>Fecha de la Factura</b></TD>
    <TD align="center"><b>Tipo Transacci&oacute;n</b></TD>
    <TD align="center"><b>NIT Proveedor</b></TD>
    <TD align="center"><b>Nombre o Raz&oacute;n Social</b></TD>
    <TD align="center"><b>Nro. Factura</b></TD>
    <TD align="center"><b>Nro. Contrato</b></TD>
    <TD align="center"><b>Importe total de la compra</b></TD>
    <TD align="center"><b>Nro. Autorizaci&oacute;n</b></TD>
    <TD align="center"><b>Nro. Cta. Banco Pago</b></TD>
    <TD align="center"><b>Monto Pago</b></TD>
    <TD align="center"><b>Monto Pagos Acumulados</b></TD>
    <TD align="center"><b>NIT Banco Pago</b></TD>
    <TD align="center"><b>Nro. Doc. Pago (Cheque o Transf.)</b></TD>
    <TD align="center"><b>Tipo Doc. Pago</b></TD>
    <TD align="center"><b>Fecha Doc. Pago</b></TD>
    <!--<TD align="center"><b>Gen. Por</b></TD>-->
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$j = 1;
$mt = 2; //contado = 1, credito = 2
$total_comp = 0;
$total_pagos = 0;


	while ($j <= $fin) { //while
	   
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mt);//mod. trans.
	} else {
	printf("<tr><td align='center'>%s</td>", $mt);//mod. trans.
	}//fin if color
    
	printf("<td align='center'>%s</td>", $matriz_compras [$j][2]);//fecha
	printf("<td align='right'>%s</td>", $matriz_compras [$j][15]);//tipo trans.
	printf("<td align='right'>%s</td>", $matriz_compras [$j][5]);//nit
	$texto = $matriz_compras [$j][4];//proveedor
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	//$texto = substr($texto,0,150);
	printf("<td align='left'>%s</td>", $texto);//proveedor
	printf("<td align='right'>%s</td>", $matriz_compras [$j][21]);//nro. factura
	//printf("<td align='right'>%s</td>", $matriz_compras [$j][19]);//nro. doc.
		
	printf("<td align='right'>%s</td>", $matriz_compras [$j][17]);//nro. contrato
	
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][6],$n_decimales));//imp. total compra 
	printf("<td align='right'>%s</td>", $matriz_compras [$j][22]);//nro. auto
	
	//printf("<td align='right'>%s</td>", $matriz_compras [$j][19]);//nro. doc
	
	//pago
	printf("<td align='right'>%s</td>", $matriz_compras [$j][23]);// nro. cta. banco pago
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][13],$n_decimales));//monto pago
	//compara cod cxp y fecha pago si es igual resto
	if($matriz_compras [$j][14] == $matriz_compras [$j+1][14] && $matriz_compras [$j][27] == $matriz_compras [$j+1][27]) {
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][24] -$matriz_compras [$j+1][13],$n_decimales));//monto acumulado pago
	} else {
	printf("<td align='right'>%s</td>", number_format($matriz_compras [$j][24],$n_decimales));//monto acumulado pago
	}
	printf("<td align='right'>%s</td>", $matriz_compras [$j][25]);//nit banco pago
	printf("<td align='right'>%s</td>", $matriz_compras [$j][26]);//nro doc pago (cheque o transf)
	printf("<td align='left'>%s</td>", $matriz_compras [$j][20]);//tipo doc pago (1-10)
	printf("<td align='center'>%s</td></tr>", $matriz_compras [$j][14]);//fecha pago 
	//printf("<td align='center'>%s</td></tr>", $matriz_compras [$j][28]);//gen por
	
	$total_comp = $total_comp + round($matriz_compras [$j][6],$n_decimales);
	$total_pagos = $total_pagos + round($matriz_compras [$j][13],$n_decimales);

	$color++;
	$j++;
  
	} //fin while
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="7"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_comp,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_pagos,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>

<?php	
}// fin if mostrar 0


//ventas
if($mostrar == 1) { //if mostrar 1

$nom_mostrar = 'Ventas';

//listado de facturas ventas
//se adicionó a.cosComponentKey is not NULL para que sea solo por factura

$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nro_doc, a.fldDate as fecha, a.codClient as cod_empresa, e.fldName as proveedor, e.fldNIT as nit, 
case a.codCurrency when $cod_moneda_principal then a.fldTotal else (select a1.fldRate from actExchangeRateDetail a1 where a1.codExchangeRate = a.codExchangeRate and a1.codTargetCurrency = a.codCurrency and a1.codBaseCurrency = $cod_moneda_principal) * a.fldTotal end as monto_cxc, 
a.fldContract as nro_contrato, a.fldParentComponent as papa, a.cosComponentKey as cod_fac, 
c.pkyCollection as cod_cobro, 
b.pkyCollectionDetail as cod_cobro_det, c.codRoute as cod_tipo_pago, 
case a.codCurrency when $cod_moneda_principal then b.fldTotal else (select b1.fldRate from actExchangeRateDetail b1 where b1.codExchangeRate = c.codExchangeRate and b1.codTargetCurrency = a.codCurrency and b1.codBaseCurrency = $cod_moneda_principal) * b.fldTotal end as monto_cobro, 
c.fldDate as fecha_cobro, c.codBankMovement as cod_mov_banco, c.codCheck as cod_cheque, c.codType as cod_tipo_doc_cobro, f.fldName as tipo_doc_cobro, c.fldDocumentNumber as nro_doc_cobro 
from
colAccountsReceivable a inner join colCollectionDetail b on 
a.pkyAccountsReceivable = b.codAccountsReceivable, colCollection c, agnCompany e, pchPurchaseType f   
where 
a.cosComponentKey is not NULL and 
a.fldBanking = 1 and 
a.codClient = e.pkyCompany and 
c.codType = f.pkyPurchaseType and 
c.pkyCollection = b.codCollection and 
((a.fldDate >= '$fechaini' and a.fldDate <= '$fechafin') or
(c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin')) and 
c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin' and 
a.sysEnabled = 1 AND a.sysAuthorized = 1 and 
c.sysEnabled = 1 AND c.sysAuthorized = 1
order by fecha, proveedor, monto_cxc, fecha_cobro, monto_cobro;");


/*
select antiguo solo facturas de venta
select a.pkySalesLedger as codigo, a.fldNumber as nro_fac,
a.fldAuthorization as nro_auto, a.fldDate as fecha, 
a.fldName as cliente, a.fldNIT as nit, a.fldGrossTotal as total_bruto, 
a.fldNetTotal as total_neto, a.cosComponentKey as cod_nv, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, d.codAccountReceivable as cod_cxc, 
c.pkyCollection as cod_cobro, 
b.pkyCollectionDetail as cod_cobro_det, c.codRoute as cod_tipo_cobro, c.codCurrency as cod_moneda, b.fldTotal as monto_cobro, c.fldDate as fecha_cobro, c.codBankMovement as cod_mov_banco, c.codCheck as cod_cheque
from
txsSalesLedger a, slsSalesNote d inner join colCollectionDetail b on 
d.codAccountReceivable = b.codAccountsReceivable, colCollection c 
where 
a.fldNetTotal >= 50000 and 
a.cosComponentKey = d.pkySalesNote and 
c.pkyCollection = b.codCollection and 
((a.fldDate >= '$fechaini' and a.fldDate <= '$fechafin') or
(c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin')) and 
c.fldDate >= '$fechaini' and c.fldDate <= '$fechafin' and 
a.sysEnabled = 1 AND a.sysAuthorized = 1 and 
c.sysEnabled = 1 AND c.sysAuthorized = 1 
order by fecha, nro_fac
*/

$matriz_ventas = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_ventas [$i][1] = $row['codigo'];
	$matriz_ventas [$i][2] = $row['fecha'];
	$matriz_ventas [$i][3] = $row['cod_empresa'];
	$matriz_ventas [$i][4] = $row['proveedor'];
	$matriz_ventas [$i][5] = $row['nit'];
	$matriz_ventas [$i][6] = round($row['monto_cxc'],$n_decimales);
	$matriz_ventas [$i][7] = $row['nro_contrato'];
	$matriz_ventas [$i][8] = $row['papa'];
	$matriz_ventas [$i][9] = $row['cod_fac'];
	$matriz_ventas [$i][10] = $row['cod_cobro'];
	$matriz_ventas [$i][11] = $row['papa'];
	$matriz_ventas [$i][12] = $row['cod_tipo_cobro'];
	$matriz_ventas [$i][13] = round($row['monto_cobro'],$n_decimales);
	$matriz_ventas [$i][14] = $row['fecha_cobro'];
	$matriz_ventas [$i][15] = NULL; //ventas: factura = 1, retenciones = 2, inmuebles = 3
	$matriz_ventas [$i][16] = 0; //nro doc para retenciones = 2 e inmuebles = 3
	$matriz_ventas [$i][17] = $row['nro_contrato'];
	$matriz_ventas [$i][18] = $row['cod_mov_banco'];
	$matriz_ventas [$i][19] = $row['cod_cheque'];
	$matriz_ventas [$i][20] = $row['tipo_doc_cobro']; //tipo doc cobro  1 - 10
	$matriz_ventas [$i][21] = $row['nro_doc']; //nro factura
	$matriz_ventas [$i][22] = $row['nro_doc']; //nro autorizacion
	
	$matriz_ventas [$i][23] = 0; //nro cta banco cobro
	$matriz_ventas [$i][24] = 0; //monto acumulado cobro
	$matriz_ventas [$i][25] = 0; //nit banco cobro
	$matriz_ventas [$i][26] = 0; //nro doc cobro (cheque o transf)
	$matriz_ventas [$i][27] = $row['codigo']; //cod_cxc
	$nro_doc_cobro = $row['nro_doc_cobro'];
	$cod_cxc = $row['codigo'];
	
	/*
	$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['cliente'];
	$matriz_ventas [$i][6] = $row['nit'];
	$matriz_ventas [$i][7] = round($row['total_bruto'],$n_decimales);
	$matriz_ventas [$i][8] = round($row['total_neto'],$n_decimales);
	$matriz_ventas [$i][9] = $row['papa'];
	$matriz_ventas [$i][10] = $row['cod_papa'];
	$matriz_ventas [$i][11] = $row['papa'];
	$matriz_ventas [$i][12] = $row['cod_pago'];
	$matriz_ventas [$i][13] = $row['cod_pago_det'];
	$matriz_ventas [$i][14] = $row['cod_tipo_cobro'];
	$matriz_ventas [$i][15] = $row['cod_moneda'];
	$matriz_ventas [$i][16] = round($row['monto_cobro'],$n_decimales);;
	$matriz_ventas [$i][17] = $row['fecha_cobro'];
	$matriz_ventas [$i][18] = 1; //compras: factura = 1, retenciones = 2, inmuebles = 3
	$matriz_ventas [$i][19] = 0; //nro doc para retenciones = 2 e inmuebles = 3
	$matriz_ventas [$i][20] = 0; //nro contrato
	$matriz_ventas [$i][21] = $row['cod_mov_banco'];
	$matriz_ventas [$i][22] = $row['cod_cheque'];
	
	$matriz_ventas [$i][23] = $row['cod_cxc'];
	$matriz_ventas [$i][24] = 0; //nro cta banco cobro 
	$matriz_ventas [$i][25] = 0; //nit banco cobro
	$matriz_ventas [$i][26] = 0; //monto acumulado cobro
	$matriz_ventas [$i][27] = 0; //nro doc cobro transf
	
	
	//para enlazar al que genero el asiento
	$papa_componente = $matriz_ventas [$i][11];
	$papa_codigo = $matriz_ventas [$i][10];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	$matriz_ventas [$i][28] = $papa_enlace;
	
	$matriz_ventas [$i][29] = 10; //tipo doc cobro  1 - 10
	*/
	/*
	1 cheque de cualquier naturaleza
	2 orden de transferencia
	3 Ordenes de transferencia electronica de fondos
	4 transferencia de fondos
	5 tarjeta de débito
	6 tarjeta de credito
	7 tarjeta prepagada
	8 deposito en cuenta
	9 cartas de credito
	10 otros */
	
	//si es factura
	if($matriz_ventas [$i][8] == 'ComponentSalesNote') {// if 0
		$cod_fac_venta = $matriz_ventas [$i][9];
		$resultfac = mysqli_query($link, "select fldNumber as nro_fac, fldAuthorization as nro_auto 
			from txsSalesLedger
			where
			cosComponentKey = '$cod_fac_venta';");
		while ($row = mysqli_fetch_array($resultfac)) {
			$matriz_ventas [$i][21] = $row['nro_fac'];
			$matriz_ventas [$i][22] = $row['nro_auto'];
		}
		mysqli_free_result($resultfac);
		$matriz_ventas [$i][15] = 1;
	}// fin if 0
	
	//si es retencion
	if($matriz_ventas [$i][8] == NULL) {// if 01
		$matriz_ventas [$i][22] = 4;
		$matriz_ventas [$i][15] = 2;
	}// fin if 01
	
	if($matriz_ventas [$i][18] != NULL) { //if 1
		$cod_mov_banco = $matriz_ventas [$i][18];
		$resultcta = mysqli_query($link, "select b.fldNumber as cuenta, d.fldNIT as nit_banco 
				from tryBankMovement a, tryBankDetail b, tryBank c, agnCompany d
				where 
				a.codBankAccount = b.pkyBankDetail and 
				a.codBank = c.pkyBank and 
				c.codCompany = d.pkyCompany and 
				a.pkyBankMovement = '$cod_mov_banco';");
		while ($row = mysqli_fetch_array($resultcta)) {
			/*
			$cuenta = $row['cuenta'];
			//dejar solo numeros
			$matriz_ventas [$i][24] = intval(preg_replace('/[^0-9]+/', '', $cuenta), 10); //nro cta banco cobro 
			*/
			$matriz_ventas [$i][23] = $row['cuenta']; //nro cta banco cobro
			$matriz_ventas [$i][25] = $row['nit_banco']; //nit banco cobro
		}
		mysqli_free_result($resultcta);
		$matriz_ventas [$i][26] = $nro_doc_cobro; //nro doc cobro transf
	}// fin if 1
	
		
	$fecha_cobro = $matriz_ventas [$i][14];
	$resultacum = mysqli_query($link, "select sum(case c.codCurrency when $cod_moneda_principal then a.fldTotal else (select b1.fldRate from actExchangeRateDetail b1 where b1.codExchangeRate = b.codExchangeRate and b1.codTargetCurrency = c.codCurrency and b1.codBaseCurrency = $cod_moneda_principal) * a.fldTotal end) as cobros_acumulados
	from colCollectionDetail a, colCollection b, colAccountsReceivable c
	where 
	a.codCollection = b.pkyCollection and 
	a.codAccountsReceivable = '$cod_cxc' and 
	a.codAccountsReceivable = c.pkyAccountsReceivable and 
	b.fldDate <= '$fecha_cobro' and 
	b.sysEnabled = 1 and b.sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultacum)) {
		$matriz_ventas [$i][24] = $row['cobros_acumulados']; //monto acumulado cobro
	}
	mysqli_free_result($resultacum);
	
	if($matriz_ventas [$i][19] != NULL) { //if 2
		$cod_cheque = $matriz_ventas [$i][19];
		$resultche = mysqli_query($link, "select b.fldNumber as cuenta, d.fldNIT as nit_banco, a.fldNumber as numero 
				from tryCheck a, tryBankDetail b, tryBank c, agnCompany d, tryCheckDepositDetail e, tryCheckDeposit f
				where 
				a.pkyCheck = e.codCheck and 
				e.codCheckDeposit = f.pkyCheckDeposit and 
				f.codBankAccount = b.pkyBankDetail and 
				a.codBank = c.pkyBank and 
				c.codCompany = d.pkyCompany and 
				a.pkyCheck = '$cod_cheque';");
		while ($row = mysqli_fetch_array($resultche)) {
			/*
			$cuenta = $row['cuenta'];
			//dejar solo numeros
			$matriz_ventas [$i][24] = intval(preg_replace('/[^0-9]+/', '', $cuenta), 10); //nro cta banco cobro 
			*/
			$matriz_ventas [$i][23] = $row['cuenta']; //nro cta banco cobro
			$matriz_ventas [$i][25] = $row['nit_banco']; //nit banco cobro
			$matriz_ventas [$i][26] = $row['numero']; //nro doc cobro transf
		}
		mysqli_free_result($resultche);
	}// fin if 2
	
	
	
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);


?>
<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>Bancarizaci&oacute;n 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>Mostrar: </b><?php echo $nom_mostrar; ?></TD>
        <TD width="33%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="33%"><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"><b>Modalidad Transacci&oacute;n</b></TD>
    <TD align="center" width="65"><b>Fecha de la Factura</b></TD>
    <TD align="center"><b>Nro. Factura</b></TD>
    <TD align="center"><b>Importe total de la venta</b></TD>
    <TD align="center"><b>Nro. Contrato</b></TD>
    <TD align="center"><b>Nro. Autorizaci&oacute;n</b></TD>
    <TD align="center"><b>NIT Cliente</b></TD>
    <TD align="center"><b>Nombre o Raz&oacute;n Social</b></TD>
    <TD align="center"><b>Nro. Cta. Banco Cobro</b></TD>
    <TD align="center"><b>Monto Cobro</b></TD>
    <TD align="center"><b>Monto Cobros Acumulados</b></TD>
    <TD align="center"><b>NIT Banco Cobro</b></TD>
    <TD align="center"><b>Nro. Doc. Cobro (Cheque o Transf.)</b></TD>
    <TD align="center"><b>Tipo Doc. Cobro</b></TD>
    <TD align="center"><b>Fecha Doc. Cobro</b></TD>
    <!--<TD align="center"><b>Gen. Por</b></TD>-->
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$j = 1;
$mt = 2; //contado = 1, credito = 2
$total_ventas = 0;
$total_cobros = 0;


	while ($j <= $fin) { //while
	   
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mt);//mod. trans.
	} else {
	printf("<tr><td align='center'>%s</td>", $mt);//mod. trans.
	}//fin if color
    
	printf("<td align='center'>%s</td>", $matriz_ventas [$j][2]);//fecha
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][21]);//nro. factura
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][6],$n_decimales));//imp. total venta 
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][17]);//nro. contrato
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][22]);//nro. auto
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][5]);//nit
	$texto = $matriz_ventas [$j][4];//cliente
	$texto = strtr(strtoupper($texto), "àáâãäåæçèéêëìíîïðñòóôõöøùüú", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÜÚ");
	//$texto = substr($texto,0,150);
	printf("<td align='left'>%s</td>", $texto);//cliente
		
	//cobro
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][23]);// nro. cta. banco cobro
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][13],$n_decimales));//monto cobro
	//compara cod cxc y fecha cobro si es igual resto
	if($matriz_ventas [$j][14] == $matriz_ventas [$j+1][14] && $matriz_ventas [$j][27] == $matriz_ventas [$j+1][27]) {
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][24] -$matriz_ventas [$j+1][13],$n_decimales));//monto acumulado cobro
	} else {
	printf("<td align='right'>%s</td>", number_format($matriz_ventas [$j][24],$n_decimales));//monto acumulado cobro
	}
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][25]);//nit banco cobro
	printf("<td align='right'>%s</td>", $matriz_ventas [$j][26]);//nro doc cobro (cheque o transf)
	printf("<td align='left'>%s</td>", $matriz_ventas [$j][20]);//tipo doc cobro (1-10)
	printf("<td align='center'>%s</td></tr>", $matriz_ventas [$j][14]);//fecha cobro 
	//printf("<td align='center'>%s</td></tr>", $matriz_ventas [$j][28]);//gen por
	
	
	$total_ventas = $total_ventas + round($matriz_ventas [$j][6],$n_decimales);
	$total_cobros = $total_cobros + round($matriz_ventas [$j][13],$n_decimales);

	$color++;
	$j++;
  
	} //fin while
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_ventas,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="right"><?php printf("<b>%s</b>", number_format($total_cobros,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>

<?php	
}// fin if mostrar 1

} //fin if 2


?>


</body>
</html>
