<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Descuentos / Devoluciones</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 "../../class.nroenletraver2.php";
include "../../general.php";
include "../../token.php";

//index.php?pkyDiscountsDevolutions=1&user=Luigi&token=123456
$rep=$_GET["rep"];
$tipo=$_GET["tipo"];

$cod_dd = $_GET["pkyDiscountsDevolutions"];
//$mostrar_asiento_cc = $_GET["showCC"];
//$usuario = $_GET["user"];

$n_decimales = 2;
?>
<body>

<?php

// Inicio impresion descuentos / devoluciones

$result = mysqli_query($link, "select a.pkyDiscountsDevolutions as cod_dd, a.fldNumber as numero, a.fldAuthorization as nro_auto, a.fldDate as fecha, a.fldControlCode as cod_control, a.codCompany as cod_empresa, b.fldName as empresa, a.fldName as nombre, a.fldNIT as nit, a.fldGrossTotal as total_bruto, a.fldExempt as exento, a.fldNetTotal as monto_10, a.fldTaxCredit as iva_10, a.fldEmissionDate as fecha_emision, a.fldTaxCreditAlternative as iva_5, a.fldNetTotalAlternative as monto_5, a.codCurrency as cod_moneda, c.fldName as moneda, a.codExchangeRate as cod_tc, a.fldIsReceived as credito, a.codSalesLedger as cod_fac_venta, a.codPurchasesLedger as cod_fac_compra, a.codAccountReceivable as cod_cxc, a.codAccountPayable as cod_cxp, a.codAccountEntry as cod_asiento, a.codItemMovement as cod_mov_item, a.codPettyCashMovement as cod_mov_caja, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, a.txtDescription as glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto
from txsDiscountsDevolutions a, agnCompany b, actCurrency c, accGroup g, srvContext s
where 
a.pkyDiscountsDevolutions = '$cod_dd' and 
a.codCompany = b.pkyCompany and 
a.codCurrency = c.pkyCurrency and 
a.sysContext = s.pkyContext and 
a.sysAuthorizationGroup = g.pkyGroup;");
	while ($row = mysqli_fetch_array($result)) {
		$cod_dd = $row['cod_dd'];
		$numero = $row['numero'];
		$nro_auto = $row['nro_auto'];
		$fecha = $row['fecha'];
		$cod_control = $row['cod_control'];
		$cod_empresa = $row['cod_empresa'];
		$empresa = $row['empresa'];
		$nombre = $row['nombre'];
		$nit = $row['nit'];
		$total_bruto = $row['total_bruto'];
		$exento = $row['exento'];
		$monto_10 = $row['monto_10'];
		$iva_10 = $row['iva_10'];
		$fecha_emision = $row['fecha_emision'];
		$monto_5 = $row['monto_5'];
		$iva_5 = $row['iva_5'];
		$cod_moneda2 = $row['cod_moneda'];
		$moneda2 = $row['moneda'];
		$cod_tc2 = $row['cod_tc'];
		$credito = $row['credito'];
		if($credito == 1) {
			$nom_credito = 'Nota de Cr&eacute;dito';
			$txt_factura = 'Factura de Compra';
			$txt_cuenta = 'Cuenta x Cobrar';
		} else {
			$nom_credito = 'Nota de D&eacute;bito';
			$txt_factura = 'Factura de Venta';
			$txt_cuenta = 'Cuenta x Pagar';
		}
		$cod_fac_venta = $row['cod_fac_venta'];
		$cod_fac_compra = $row['cod_fac_compra'];
		$cod_cxc = $row['cod_cxc'];
		$cod_cxp = $row['cod_cxp'];
		$cod_asiento = $row['cod_asiento'];
		$cod_mov_item = $row['cod_mov_item'];
		$cod_mov_caja = $row['cod_mov_caja'];
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$usuario_reg = $row['usuario_reg'];
		$enabled = $row['enabled'];
		$authorized = $row['authorized'];
		$cod_contexto = $row['cod_contexto'];
		$contexto = $row['contexto'];

		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = $usuario_det;
		include "../../generadopor.php";
		
	}
	mysqli_free_result($result);

//detalle descuentos / devoluciones
$resultdet = mysqli_query($link, "select a.pkyDiscountsDevolutionsDetail as cod_det, a.codItem as cod_item, b.fldName as item, a.fldQuantity as cantidad, a.fldPrice as precio, a.fldDiscount as descuento, a.fldTotalNetPrice as total, a.fldExempt as exento 
from 
txsDiscountsDevolutionsDetail a, invItem b
where 
a.codDiscountsDevolutions = '$cod_dd' and 
a.codItem = b.pkyItem 
order by item;");


//factura de compra o venta
if($credito == 1) {
$resultfac = mysqli_query($link, "select a.pkyPurchasesLedger as cod_fac, a.fldNumber as numero, a.codType as cod_tipo_cv, b.fldName as tipo_cv 
from 
txsPurchasesLedger a, txsPurchaseInvoiceType b
where 
a.pkyPurchasesLedger = '$cod_fac_compra' and 
a.codType = b.pkyPurchaseInvoiceType;");
} else {
$resultfac = mysqli_query($link, "select a.pkySalesLedger as cod_fac, a.fldNumber as numero, NULL as cod_tipo_cv, NULL as tipo_cv  
from 
txsSalesLedger a
where 
pkySalesLedger = '$cod_fac_venta';");
}
while ($row = mysqli_fetch_array($resultfac)) {
	$codigo_fac = $row['cod_fac'];
	$numero_fac = $row['numero'];
	$cod_tipo_cv = $row['cod_tipo_cv'];
	$tipo_cv = $row['tipo_cv'];
}
mysqli_free_result($resultfac);


//tipo de cambio para la cabecera
$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$cod_tc2' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resulttc)) {
	$tc_fecha = $row['fecha'];
	$tc_hora = $row['hora'];
}
mysqli_free_result($resulttc);

$resulttcdet = mysqli_query($link, "select a.pkyExchangeRateDetail as cod_tc_det, a.codBaseCurrency as cod_mon_target, b.fldName as mon_target, b.txtDescription as nom_mon_target, a.codTargetCurrency as cod_mon_base, c.fldName as mon_base, c.txtDescription as nom_mon_base, a.fldRate as tasa
from
actExchangeRateDetail a, actCurrency b, actCurrency c
where
a.codExchangeRate = '$cod_tc2' and 
a.codBaseCurrency = '$cod_moneda_principal' and 
a.codBaseCurrency = b.pkyCurrency and 
a.codTargetCurrency = c.pkyCurrency 
order by cod_mon_target, cod_mon_base;");
$ma_tc = array();
$m = 1;
while ($row = mysqli_fetch_array($resulttcdet)) {
	$ma_tc [$m][1] = $row['mon_base'];
	$ma_tc [$m][2] = $row['mon_target'];
	$ma_tc [$m][3] = $row['tasa'];
	$m++;
}
mysqli_free_result($resulttcdet);
$fin_m = $m - 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><?php echo $nom_credito; ?></b></font></center></p>

<?php if($mi_cod_pais == 1) { //if bolivia ?>
<table border="1" align="center" cellpadding="2" width="100%">
    <TR> 
    <TD width="33%"><b>Cod. Reg.: </b><?php echo $cod_dd; ?></TD>
    <?php if($credito == 1) { ?>
    <TD width="33%"><b><?php echo $txt_factura; ?>: </b><a href="../../ModuleTaxes/VoucherSalesLedger/index.php?pkySalesLedger=<?php echo $codigo_fac; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Factura de Venta"><?php echo $codigo_fac; ?></a> - <?php echo $numero_fac; ?></TD>
    <?php } else { ?>
    <TD width="33%"><b><?php echo $txt_factura; ?>: </b><a href="../../ModuleTaxes/VoucherPurchasesLedger/index.php?pkyPurchasesLedger=<?php echo $codigo_fac; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Factura de Compra"><?php echo $codigo_fac; ?></a> - <?php echo $numero_fac; ?></TD>
    <?php } ?>
    <?php if($credito == 1) { ?>
    <TD width="33%"><b><?php echo $txt_cuenta; ?>: </b><a href="../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=<?php echo $cod_cxc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Cuenta por Cobrar"><?php echo $cod_cxc; ?></a></TD>
    <?php } else { ?>
    <TD width="33%"><b><?php echo $txt_cuenta; ?>: </b><a href="../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=<?php echo $cod_cxp; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Cuenta por Pagar"><?php echo $cod_cxp; ?></a></TD>
    <?php } ?>
    </TR>
    <TR>
    <TD><b>N&uacute;mero: </b><?php echo $numero; ?></TD>
    <TD><b>Autorizaci&oacute;n: </b><?php echo $nro_auto; ?></TD>
    <TD><b>C&oacute;digo de Control: </b><?php echo $cod_control; ?></TD>
    </TR>
    <TR>
    <TD><b>Nombre: </b><?php echo $nombre; ?></TD>
    <TD><b>NIT: </b><?php echo $nit; ?></TD>
    <TD><b>Total Bruto: </b><?php echo number_format($total_bruto,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>Total Neto: </b><?php echo number_format($total_neto,$n_decimales); ?></TD>
    <TD><b>Cr&eacute;dito Fiscal: </b><?php echo number_format($cred_fiscal,$n_decimales); ?></TD>
    <TD><b>Descuento: </b><?php echo number_format($descuento,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>ICE: </b><?php echo number_format($total_ice,$n_decimales); ?></TD>
    <TD><b>Exento: </b><?php echo number_format($excento,$n_decimales); ?></TD>
    <TD><b>Generado Por: </b><?php echo $papa_enlace; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Tipo de compra: </b><?php echo $cod_tipo_compra; ?> - <?php echo $tipo_compra; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Glosa: </b><?php echo $glosa; ?></TD>
    </TR>
</table>
<?php } //fin if bolivia ?>

<?php if($mi_cod_pais == 176) { //if paraguay ?>
<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>Cod. Reg.: </b><?php echo $cod_dd; ?></TD>
    <?php if($credito == 1) { ?>
    <TD width="33%"><b><?php echo $txt_factura; ?>: </b><a href="../../ModuleTaxes/VoucherPurchasesLedger/index.php?pkyPurchasesLedger=<?php echo $codigo_fac; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Factura de Compra"><?php echo $codigo_fac; ?></a> - <?php echo $numero_fac; ?></TD>
    <?php } else { ?>
    <TD width="33%"><b><?php echo $txt_factura; ?>: </b><a href="../../ModuleTaxes/VoucherSalesLedger/index.php?pkySalesLedger=<?php echo $codigo_fac; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Factura de Venta"><?php echo $codigo_fac; ?></a> - <?php echo $numero_fac; ?></TD>
    <?php } ?>
    
	<?php if($credito == 1) { ?>
    <TD width="33%"><b><?php echo $txt_cuenta; ?>: </b><a href="../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=<?php echo $cod_cxc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Cuenta por Cobrar"><?php echo $cod_cxc; ?></a></TD>
    <?php } else { ?>
    <TD width="33%"><b><?php echo $txt_cuenta; ?>: </b><a href="../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=<?php echo $cod_cxp; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Cuenta por Pagar"><?php echo $cod_cxp; ?></a></TD>
    <?php } ?>
    </TR>
    <TR>
    <TD><b>Fecha: </b><?php echo $fecha; ?></TD>
    <TD><b>Empresa: </b><?php echo $cod_empresa; ?> - <?php echo $empresa; ?></TD>
    <TD><b>Asiento: </b><a href="../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=<?php echo $cod_asiento; ?>&showCC=<?php echo $mostrar_asiento_cc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Asiento Contable"><?php echo $cod_asiento; ?></a></TD>
    </TR>
    <TR>
    <TD><b>N&uacute;mero: </b><?php echo $numero; ?></TD>
    <TD><b>Nombre: </b><?php echo $nombre; ?></TD>
    <TD><b>RUC: </b><?php echo $nit; ?></TD>
    </TR>
    <TR>
    <TD><b>Timbrado: </b><?php echo $nro_auto; ?></TD>
    <TD><b>Total Bruto: </b><?php echo number_format($total_bruto,$n_decimales); ?></TD>
    <TD><b>Exento: </b><?php echo number_format($exento,$n_decimales); ?></TD>
    </TR>
    <TR>
    </TR>
    <TR>
    <TD><b>C&oacute;digo de Control: </b><?php echo $cod_control; ?></TD>
    <TD><b>Gravable 10%: </b><?php echo number_format($monto_10,$n_decimales); ?></TD>
    <TD><b>Impuesto 10%: </b><?php echo number_format($iva_10,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>Vencimiento: </b><?php echo $fecha_emision; ?></TD>
    <TD><b>Gravable 5%: </b><?php echo number_format($monto_5,$n_decimales); ?></TD>
    <TD><b>Impuesto 5%: </b><?php echo number_format($iva_5,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>Moneda: </b><?php echo $moneda2; ?></TD>
	<TD><b>Generado Por: </b><?php echo $papa_enlace; ?></TD>
    <TD><b>Total Impuesto: </b><?php echo number_format($iva_10+$iva_5,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Tipo: </b><?php echo $cod_tipo_cv; ?> - <?php echo $tipo_cv; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Glosa: </b><?php echo $glosa; ?></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 $cod_tc2; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $cod_tc2; ?></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></tr>", 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>

<p><font color="#000099"><b>&nbsp;Detalle</b></font></p>
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
    <TD align="center"><b>Cod. Item</b></TD>
    <TD align="center"  width="400"><b>Item</b></TD>
    <TD align="center"><b>Cantidad</b></TD>
    <TD align="center"><b>Precio</b></TD>
    <TD align="center"><b>Descuento</b></TD>
    <TD align="center"><b>Precio Total</b></TD>
    <TD align="center"><b>Exento (%)</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	while ($row = mysqli_fetch_array($resultdet)) { //while
		$cod_det = $row['cod_det'];
		$cod_item_det = $row['cod_item'];
		$nom_item_det = $row['item'];
		$cantidad_det = $row['cantidad'];
		$precio_det = $row['precio'];
		$descuento_det = $row['descuento'];
		$total_det = $row['total'];
		$exento_det = $row['exento'];
		
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_item_det);//cod item
		} else {
		printf("<tr><td align='center'>%s</td>", $cod_item_det);//cod item
		} //cierra if color
		
		printf("<td align='left'>%s</td>", $nom_item_det);//item
		printf("<td align='right'>%s</td>", number_format($cantidad_det,0));//cant
		printf("<td align='right'>%s</td>", number_format($precio_det, $n_decimales));//precio
		printf("<td align='right'>%s</td>", number_format($descuento_det, $n_decimales));//descuento
		printf("<td align='right'>%s</td>", number_format($total_det, $n_decimales));//total precio
		printf("<td align='right'>%s</td></tr>", number_format($exento_det, $n_decimales));//exento
		$color++;
		}//fin while
	mysqli_free_result($resultdet);
	?>
</tbody>    
</table>


<?php } //fin if paraguay ?>
<br />
<?php
    if($enabled == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO ANULADO</font></td></tr></table>");
			}
	if($authorized == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}

?>

<br /><br /><br /><br /><br />
<table border="0" align="center" cellpadding="2" width="90%">
<tr>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
</tr><tr>
<td align="center">Elaborado por: <?php echo $usuario_reg; ?></td>
<td align="center"><?php echo $firma_contabilidad; ?></td>
<td align="center"><?php echo $firma_gerencia; ?></td>
<td align="center"><?php echo $firma_interesado; ?></td>
</tr><tr>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
</tr><tr>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
</tr>
</table>

<?php


?>
</body>
</html>
