<?php
// inicio muestra detalle, es el mismo que en el report cambia la variable tipo=3
//solo en este archivo esta la opcion de quitar los titulos no en el detalle

//verificar el tipo de usuario 3 es normal (no muestra CxP) para el detalle
$resultuser = mysqli_query($link, "select pkyUser as cod_user, codType as cod_tipo 
from accUser
where fldLogin = '$usuario_det' and
sysEnabled = 1 and sysAuthorized = 1;");
		
while ($row = mysqli_fetch_array($resultuser)) {
$cod_usuario_det = $row['cod_user'];
$cod_tipo_usuario_det = $row['cod_tipo'];
}
mysqli_free_result($resultuser);

if($cod_tipo_usuario_det == 3) { 
	$valor_ver_cxp_det = ' a.sysVisible <> 1 and ';
	$valor_ver_cxp_det2 = ' g.sysVisible <> 1 and ';
} else {
	$valor_ver_cxp_det = ' ';
	$valor_ver_cxp_det2 = ' ';
}

if($rep == 8 && $tipo == 3) { // if 3
 
 //nombre otra moneda
$resultotra = mysqli_query($link, "SELECT fldName as otra_moneda
from
actCurrency
where
pkyCurrency = '$otra_moneda_det' and
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultotra)) {
	$nom_otra_moneda_det = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);
	
//nombre concepto de CxP
if ($con_cxp_det == 0) { // if 2.0
		$nom_con_cxp_det = 'Todos';
		$valor_con_cxp_det = ' ';
		$valor_con_cxp_det2 = ' ';
	} else {
		//nombre con cxp
		if ($con_cxp_det == -1) { // if 2.0.1
		$nom_con_cxp_det = 'Solo Concepto de CxC';
		} else { // else if 2.0.1
		$result2 = mysqli_query($link, "select pkyAccountsPayableConcept as cod_con_cxp, fldName as concepto 
								from payAccountsPayableConcept
								where
								pkyAccountsPayableConcept = '$con_cxp_det' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {
			$nom_con_cxp_det = $row['concepto'];
		}
		mysqli_free_result($result2);
		} //fin if 2.0.1
		$valor_con_cxp_det = ' a.codConcept = ' .$con_cxp_det. ' and ';
		$valor_con_cxp_det2 = ' g.codConcept = ' .$con_cxp_det. ' and ';
	} //fin if 2.0	
	
//nombre concepto de CxC
if ($con_cxc_det == 0) { // if 2.1
		$nom_con_cxc_det = 'Todos';
		$valor_con_cxc_det = ' ';
		$valor_con_cxc_det2 = ' ';
	} else {
		//nombre con cxc
		if ($con_cxc_det == -1) { // if 2.1.1
		$nom_con_cxc_det = 'Solo Concepto de CxP';
		} else { // else if 2.1.1
		$result3 = mysqli_query($link, "select pkyAccountsReceivableConcept as cod_con_cxc, fldName as concepto 
								from colAccountsReceivableConcept
								where
								pkyAccountsReceivableConcept = '$con_cxc_det' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_con_cxc_det = $row['concepto'];
		}
		mysqli_free_result($result3);
		} //fin if 2.1.1
		$valor_con_cxc_det = ' a.codConcept = ' .$con_cxc_det. ' and ';
		$valor_con_cxc_det2 = ' g.codConcept = ' .$con_cxc_det. ' and ';
	} //fin if 2.1	

// orden detalle por fecha (desordenado pero con saldos), por cuenta (agrupado por cuenta)
if($ordendet == 0) { 
	$txt_orden_det = 'Fecha';
	$valor_orden = 'order by final.fecha, final.orden, final.codigo';
} else {
	$txt_orden_det = 'Cuenta';
	$valor_orden = 'order by final.codigo_orden, final.orden, final.codigo';
}

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


 if($empresa == 1) { //if 3.1
  
 // detalle empresa
  $result = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where
pkyCompany = '$nombredet' and
sysEnabled = 1 and sysAuthorized = 1;");
 
while ($row = mysqli_fetch_array($result)) {
	$nomempdet = $row['codigo'].' - '.$row['empresa'];
}
mysqli_free_result($result);


 $result2 = mysqli_query($link, "select final.codigo as codigo, final.nrodoc as nrodoc, final.fecha as fecha, final.fecha_pago as fecha_pagocob, final.concepto as concepto, final.concepto_gasto as concepto_pagocob, final.via as via_pagocob, final.empresa as empresa, final.moneda as moneda, final.glosa as glosa, final.cxp as total, final.tipo as tipo, final.tasa as tasa, final.total as total_om, final.orden as orden, final.codigo_orden as codigo_orden, final.papa as papa, final.cod_papa as cod_papa
from
(select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_pago as fecha_pago, otro.concepto as concepto, otro.concepto_gasto as concepto_gasto, otro.via as via, otro.empresa as empresa, otro.moneda as moneda, otro.glosa as glosa, otro.cxp as cxp, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxp as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, 
null as concepto_gasto, null as via, d.fldName as empresa, f.fldName as moneda, 
a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, 
a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnCompany d, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codSupplier = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codSupplier = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, null as concepto_gasto, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnCompany d, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codSupplier = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codSupplier = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_cobro as fecha_cobro, otro.concepto as concepto, otro.concepto_ingreso as concepto_ingreso, otro.via as via, otro.empresa as empresa, otro.moneda as moneda, otro.glosa as glosa, otro.cxc as cxc,
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxc as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, c.fldName as concepto_ingreso, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where 
$valor_con_cxc_det
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, c.fldName as concepto_ingreso, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where 
$valor_con_cxc_det
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_pago as via_pago, otro.empresa as empresa, 
otro.moneda_cxp as moneda, otro.glosa as glosa, otro.pago as pago,
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxp as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.pago as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp,
t.empresa as empresa, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnCompany d, actCurrency f, 
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codClient = d.pkyCompany and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empresa as empresa, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnCompany d, actCurrency f, 
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codClient = d.pkyCompany and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxp = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_cobro as via_cobro, otro.empresa as empresa, 
otro.moneda_cxc as moneda, otro.glosa as glosa, 
otro.cobro as cobro, otro.tipo as tipo, otro.tc as tc, 
otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxc as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.cobro as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empresa as empresa, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, d.fldName as empresa,
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency

union all

select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empresa as empresa, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, d.fldName as empresa,
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxc = t.otra_moneda) otro) final
$valor_orden;");
 ?>
 <!--
<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_det; ?></b>
</td></tr></table><br/>
-->
<?php if($quitar == 0) { ?>
<p><center><font color="#000099"><b>Detalle Estado de Cuentas</b></font></center></p>
<?php } ?>
<!--
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="50%"><b>Empresa: </b><?php echo $nomempdet; ?></TD>
        <TD width="50%"><b>Ordenado por: </b><?php echo $txt_orden_det; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechainidet; ?> <b>a Fecha: </b><?php echo $fechafindet; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></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="left" cellpadding="2" width="90%">
	<TR BGCOLOR='#ffcccc'> 
    <?php if($ordendet == 0) { // if orden ?>
    <?php if($quitar == 0) { //if quitar?>
 	<TD align="left" colspan="16"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php } else { //else if quitar?>
    <TD align="left" colspan="7"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php } //fin if quitar ?>
	<?php } else { // if else orden ?>
    <TD align="left" colspan="15"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    <TD align="right"><b><?php echo number_format($totaldet,$n_decimales); ?></b></TD></TR> 
    <?php if($quitar == 0) { //if quitar?>
    <thead>
    <TR> 
    <TD align="center"><b>Tipo</b></TD>
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Nro. Doc.</b></TD>
    <TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center" width="65"><b>Fecha Pago/Cobro</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto Pago/Cobro</b></TD>
    <TD align="center"><b>Via Pago/Cobro</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Tasa</b></TD>
    <TD align="center"><b>Debito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <TD align="center"><b>Cr&eacute;dito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php if($ordendet == 0) { // if orden ?>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    </TR>
    </thead>
    <?php }//fin if quitar ?>
    <tbody class="marcafila">

<?php
$color2 = 2;
$suma_total2 = 0;
$suma_periodo = 0;
$suma_deb = 0;
$suma_cred = 0;
$saldo_det = $totaldet;
  
while ($row = mysqli_fetch_array($result2)) { //while
    
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	$usuario = $usuario_det;
	include "../../generadopor.php"; 
	
	if($color2 % 2 == 0) { //if color
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $row['tipo']);//tipo	
		} //fin if orden
	} else {
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} //fin if color
	
	if($row['orden'] == 1) { //if orden 1
		$codigo_cxc = $row['codigo'];
		printf("<td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_cxc&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//codigo
	} //fin if orden 1
	if($row['orden'] == 2) { //if orden 2
		$cod_cobro = $row['codigo'];
		$tipo2 = $row['via_pagocob'];
		if($tipo2 == 'Liquidacion') {//if liquidacion
			printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
		} else {//else liquidacion
			printf("<td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
		}//fin if liquidacion
	} //fin if orden 2
	if($row['orden'] == 3) { //if orden 3
		$codigo_cxp = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$codigo_cxp&user=$usuario_det' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo	
	}//fin if orden 3
	if($row['orden'] == 4) { //if orden 4
		$cod_pago = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_pago&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_pago);//codigo	
	} //fin if orden 4
	
	printf("<td align='center'>%s</td>", $row['nrodoc']);//nro doc
	printf("<td align='right'>%s</td>", $row['fecha']);//fecha
	if($quitar == 0) { //if quitar 1
	printf("<td align='right'>%s</td>", $row['fecha_pagocob']);//fecha pago o cobro
	}//fin if quitar 1
	printf("<td>%s</td>", $row['concepto']);//concepto 
	if($quitar == 0) { //if quitar 2
	printf("<td>%s</td>", $row['concepto_pagocob']);//concepto pago o cobro
	printf("<td>%s</td>", $row['via_pagocob']);//via pago o cobro
	printf("<td>%s</td>", $row['empresa']);//empresa
	printf("<td>%s</td>", $row['glosa']);//glosa
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	} //fin if quitar 2
	printf("<td>%s</td>", $row['moneda']);//moneda
	
	if($row['total'] >= 0) {
		printf("<td align='right'>%s</td>", number_format($row['total'],$n_decimales));//total
	} else {
		printf("<td align='right'>%s</td>", number_format($row['total']*-1,$n_decimales));//total
	}
	if($quitar == 0) {//if quitar 3
	printf("<td>%s</td>", number_format($row['tasa'], $n_tipocambio));//tasa
	
	if($row['orden'] == 1 || $row['orden'] == 4) {//if cxc y pago
		printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//total otra moneda debito
		printf("<td>&nbsp;</td>");//total otra moneda credito
		$suma_deb = $suma_deb + $row['total_om'];
	} //fin if cxc y pago
	if($row['orden'] == 2 || $row['orden'] == 3) { //if cobro y cxp
		printf("<td>&nbsp;</td>");//total otra moneda debito
		printf("<td align='right'>%s</td>", number_format($row['total_om']*-1,$n_decimales));//total otra moneda credito
		$suma_cred = $suma_cred + $row['total_om']*-1;
	} //fin if cobro y cxp
	}//fin if quitar 3
	
  if($ordendet == 0) { //if orden
    $saldo_det = $saldo_det + $row['total_om'];
	printf("<td align='right'>%s</td>", number_format($saldo_det,$n_decimales));//saldo
  } //fin if orden
  
	echo '</tr>';
	$suma_periodo = $suma_periodo + $row['total_om'];
	$color2++;
	
	//muestra el detalle de la nota de venta
	
	if($row['orden'] == 1 && $ventadetalledet == 1 && strcmp($row['papa'],'ComponentSalesNote') == 0) { //if venta detalle orden 1.1
		$cod_venta = $row['cod_papa'];
		
		if($ordendet == 0) { // if orden 0.0
			if($quitar == 0) { //if quitar
			echo '<tr><td colspan="16">';	
			include 'detail2.php';
			echo '</td></tr>';
			echo '<tr BGCOLOR="#66ff99"><td colspan="17">&nbsp;</td></tr>';	
			} else { //else if quitar
			echo '<tr><td colspan="8">';	
			include 'detail2.php';
			echo '</td></tr>';
			echo '<tr BGCOLOR="#66ff99"><td colspan="8">&nbsp;</td></tr>';	
			}//fin if quitar
		} else { // else if orden 0.0
		echo '<tr><td colspan="15">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="15">&nbsp;</td></tr>';	
		} //fin if orden 0.0
			if($quitar == 0) { //if quitar
			echo '<TR> 
			<TD align="center"><b>Tipo</b></TD>
			<TD align="center"><b>Cod.</b></TD>
			<TD align="center"><b>Nro. Doc.</b></TD>
			<TD align="center" width="65"><b>Fecha</b></TD>
			<TD align="center" width="65"><b>Fecha Pago/Cobro</b></TD>
			<TD align="center"><b>Concepto</b></TD>
			<TD align="center"><b>Concepto Pago/Cobro</b></TD>
			<TD align="center"><b>Via Pago/Cobro</b></TD>
			<TD align="center"><b>Empresa</b></TD>
			<TD align="center"><b>Glosa</b></TD>
			<TD align="center"><b>Gen. Por</b></TD>
			<TD align="center"><b>Mon.</b></TD>
			<TD align="center"><b>Monto</b></TD>
			<TD align="center"><b>Tasa</b></TD>
			<TD align="center"><b>Debito en '.$nom_otra_moneda_det.'</b></TD>
			<TD align="center"><b>Cr&eacute;dito en '.$nom_otra_moneda_det.'</b></TD>';
			if($ordendet == 0) { // if orden
			echo '<TD align="center"><b>Saldo en '.$nom_otra_moneda_det.'</b></TD>';
			}// fin if orden
			echo '</TR>';
			} //fin if quitar
	} //fin if venta detalle orden 1.1
	
}//fin while
	mysqli_free_result($result2);
	$suma_total2 = $suma_periodo + $totaldet; 
	
 ?>
<TR BGCOLOR='#ffcccc'> 
<?php if($quitar == 0) { // if quitar 1 ?>
 <TD align="left" colspan="14"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_deb,$n_decimales); ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_cred,$n_decimales); ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="16"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total2,$n_decimales); ?></b></TD>
 
 <?php } else { // else if quitar 1 ?>
 
 <TD align="left" colspan="7"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="7"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total2,$n_decimales); ?></b></TD>
 <?php } //fin if quitar 1?>
 </TR> 
</tbody>
</table>
  <?php
 } //fin if 3.1
 
 if($empresa == 0) { //if 3.2
 
 // detalle empleado
 
 $result = mysqli_query($link, "select pkyPerson as codigo, case when fldMiddleName is NULL then concat(fldLastName,', ',fldFirstName) else concat(fldLastName,' ',fldMiddleName,', ',fldFirstName) end as empleado
from agnPerson
where
pkyPerson = '$nombredet' and
sysEnabled = 1 and sysAuthorized = 1;");
 
while ($row = mysqli_fetch_array($result)) {
	$nomempdet = $row['codigo'].' - '.$row['empleado'];
}
mysqli_free_result($result);

 $result2 = mysqli_query($link, "select final.codigo as codigo, final.nrodoc as nrodoc, final.fecha as fecha, final.fecha_pago as fecha_pagocob, final.concepto as concepto, final.concepto_gasto as concepto_pagocob, final.via as via_pagocob, final.empleado as empleado, final.moneda as moneda, final.glosa as glosa, final.cxp as total, final.tipo as tipo, final.tasa as tasa, final.total as total_om, final.orden as orden, final.codigo_orden as codigo_orden, final.papa as papa, final.cod_papa as cod_papa
from
(select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_pago as fecha_pago, otro.concepto as concepto, otro.concepto_gasto as concepto_gasto, otro.via as via, otro.empleado as empleado, otro.moneda as moneda, otro.glosa as glosa, otro.cxp as cxp, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxp as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, 
null as concepto_gasto, null as via, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, f.fldName as moneda, a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, 
a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnPerson e, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, null as concepto_gasto, null as via, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, f.fldName as moneda, a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnPerson e, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_cobro as fecha_cobro, otro.concepto as concepto, otro.concepto_ingreso as concepto_ingreso, otro.via as via, otro.empleado as empleado, otro.moneda as moneda, otro.glosa as glosa, otro.cxc as cxc, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxc as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, c.fldName as concepto_ingreso, null as via, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c, agnPerson e, actCurrency f
where 
$valor_con_cxc_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa 
from (
select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, c.fldName as concepto_ingreso, null as via, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnPerson e, actCurrency f
where 
$valor_con_cxc_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_pago as via_pago, otro.empleado as empleado, 
otro.moneda_cxp as moneda, otro.glosa as glosa, otro.pago as pago, 
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxp as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.pago as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empleado as empleado,
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnPerson e, actCurrency f,
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codEmployee = e.pkyPerson and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empleado as empleado, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnPerson e, actCurrency f,
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codEmployee = e.pkyPerson and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxp = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_cobro as via_cobro, otro.empleado as empleado, 
otro.moneda_cxc as moneda, otro.glosa as glosa, 
otro.cobro as cobro, otro.tipo as tipo, otro.tc as tc, 
otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxc as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.cobro as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empleado as empleado, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where
$valor_con_cxc_det2  
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empleado as empleado, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxc = t.otra_moneda) otro) final
$valor_orden;");
 ?>
<!--
<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_det; ?></b>
</td></tr></table><br/>
-->
<?php if($quitar == 0) { ?>
<p><center><font color="#000099"><b>Detalle Estado de Cuentas</b></font></center></p>
<?php } ?>
<!--
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="50%"><b>Empleado: </b><?php echo $nomempdet; ?></TD>
        <TD width="50%"><b>Ordenado por: </b><?php echo $txt_orden_det; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechainidet; ?> <b>a Fecha: </b><?php echo $fechafindet; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></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="left" cellpadding="2" width="90%">
	<TR BGCOLOR='#ffcccc'> 
 	<?php if($ordendet == 0) { // if orden ?>
    <?php if($quitar == 0) { //if quitar?>
 	<TD align="left" colspan="16"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php } else { //else if quitar?>
    <TD align="left" colspan="7"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php } //fin if quitar ?>
	<?php } else { // if else orden ?>
    <TD align="left" colspan="15"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    <TD align="right"><b><?php echo number_format($totaldet,$n_decimales); ?></b></TD></TR> 
    <?php if($quitar == 0) { //if quitar?>
 	<thead>
    <TR> 
    <TD align="center"><b>Tipo</b></TD>
 	<TD align="center"><b>Cod.</b></TD> 
    <TD align="center"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
   	<TD align="center" width="65"><b>Fecha Pago/Cobro</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto Pago/Cobro</b></TD>
    <TD align="center"><b>Via Pago/Cobro</b></TD>
    <TD align="center"><b>Empleado</b></TD>
	<TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Tasa</b></TD>
    <TD align="center"><b>Debito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <TD align="center"><b>Cr&eacute;dito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php if($ordendet == 0) { // if orden ?>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    </TR>
    </thead>
    <?php }//fin if quitar ?>
    <tbody class="marcafila">

<?php
$color2 = 2;
$suma_total2 = 0;
$suma_periodo = 0;
$suma_deb = 0;
$suma_cred = 0;
$saldo_det = $totaldet;
  
while ($row = mysqli_fetch_array($result2)) { //while
	
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	$usuario = $usuario_det;
	include "../../generadopor.php"; 
		
	if($color2 % 2 == 0) { //if color
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} else {
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} //fin if color
	
	if($row['orden'] == 1) { //if orden 1
		$codigo_cxc = $row['codigo'];
		printf("<td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_cxc&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//codigo
	} //fin if orden 1
	if($row['orden'] == 2) { //if orden 2
		$cod_cobro = $row['codigo'];
		$tipo2 = $row['via_pagocob'];
		if($tipo2 == 'Liquidacion') {//if liquidacion
			printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
		} else {//else liquidacion
			printf("<td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
		}//fin if liquidacion
	} //fin if orden 2
	if($row['orden'] == 3) { //if orden 3
		$codigo_cxp = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$codigo_cxp&user=$usuario_det' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo	
	}//fin if orden 3
	if($row['orden'] == 4) { //if orden 4
		$cod_pago = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_pago&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_pago);//codigo	
	} //fin if orden 4
	
	printf("<td align='center'>%s</td>", $row['nrodoc']);//nro doc
	printf("<td align='right'>%s</td>", $row['fecha']);//fecha
	if($quitar == 0) { //if quitar 1
	printf("<td align='right'>%s</td>", $row['fecha_pagocob']);//fecha pago o cobro
	}//fin if quitar 1
	printf("<td>%s</td>", $row['concepto']);//concepto
	if($quitar == 0) { //if quitar 2
	printf("<td>%s</td>", $row['concepto_pagocob']);//concepto pago o cobro
	printf("<td>%s</td>", $row['via_pagocob']);//via pago o cobro
	printf("<td>%s</td>", $row['empleado']);//empleado
	printf("<td>%s</td>", $row['glosa']);//glosa
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	} //fin if quitar 2
	printf("<td>%s</td>", $row['moneda']);//moneda
	
	if($row['total'] >= 0) {
		printf("<td align='right'>%s</td>", number_format($row['total'],$n_decimales));//total
	} else {
		printf("<td align='right'>%s</td>", number_format($row['total']*-1,$n_decimales));//total
	}
	
	if($quitar == 0) {//if quitar 3
	printf("<td>%s</td>", number_format($row['tasa'], $n_tipocambio));//tasa
	
	if($row['orden'] == 1 || $row['orden'] == 4) {//if cxc y pago
		printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//total otra moneda debito
		printf("<td>&nbsp;</td>");//total otra moneda credito
		$suma_deb = $suma_deb + $row['total_om'];
	} //fin if cxc y pago
	if($row['orden'] == 2 || $row['orden'] == 3) { //if cobro y cxp
		printf("<td>&nbsp;</td>");//total otra moneda debito
		printf("<td align='right'>%s</td>", number_format($row['total_om']*-1,$n_decimales));//total otra moneda credito
		$suma_cred = $suma_cred + $row['total_om']*-1;
	} //fin if cobro y cxp
	}//fin if quitar 3
		
	if($ordendet == 0) { //if orden
    $saldo_det = $saldo_det + $row['total_om'];
	printf("<td align='right'>%s</td>", number_format($saldo_det,$n_decimales));//saldo
    } //fin if orden
		
	echo '</tr>';
	$suma_periodo = $suma_periodo + $row['total_om'];
	$color2++;
	
	//muestra el detalle de la nota de venta
	
	if($row['orden'] == 1 && $ventadetalledet == 1 && strcmp($row['papa'],'ComponentSalesNote') == 0) { //if venta detalle orden 1.1
		$cod_venta = $row['cod_papa'];
		
		if($ordendet == 0) { // if orden 0.0
			if($quitar == 0) { //if quitar
			echo '<tr><td colspan="16">';	
			include 'detail2.php';
			echo '</td></tr>';
			echo '<tr BGCOLOR="#66ff99"><td colspan="17">&nbsp;</td></tr>';	
			} else { //else if quitar
			echo '<tr><td colspan="8">';	
			include 'detail2.php';
			echo '</td></tr>';
			echo '<tr BGCOLOR="#66ff99"><td colspan="8">&nbsp;</td></tr>';	
			}//fin if quitar
		} else { // else if orden 0.0
		echo '<tr><td colspan="15">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="15">&nbsp;</td></tr>';	
		} //fin if orden 0.0
			if($quitar == 0) { //if quitar
			echo '<TR> 
			<TD align="center"><b>Tipo</b></TD>
			<TD align="center"><b>Cod.</b></TD>
			<TD align="center"><b>Nro. Doc.</b></TD>
			<TD align="center" width="65"><b>Fecha</b></TD>
			<TD align="center" width="65"><b>Fecha Pago/Cobro</b></TD>
			<TD align="center"><b>Concepto</b></TD>
			<TD align="center"><b>Concepto Pago/Cobro</b></TD>
			<TD align="center"><b>Via Pago/Cobro</b></TD>
			<TD align="center"><b>Empleado</b></TD>
			<TD align="center"><b>Glosa</b></TD>
			<TD align="center"><b>Gen. Por</b></TD>
			<TD align="center"><b>Mon.</b></TD>
			<TD align="center"><b>Monto</b></TD>
			<TD align="center"><b>Tasa</b></TD>
			<TD align="center"><b>Debito en '.$nom_otra_moneda_det.'</b></TD>
			<TD align="center"><b>Cr&eacute;dito en '.$nom_otra_moneda_det.'</b></TD>';
			if($ordendet == 0) { // if orden
			echo '<TD align="center"><b>Saldo en '.$nom_otra_moneda_det.'</b></TD>';
			}// fin if orden
			echo '</TR>';
			}//fin if quitar
	} //fin if venta detalle orden 1.1
	
}//fin while
	mysqli_free_result($result2);
	$suma_total2 = $suma_periodo + $totaldet; 
 ?>
<TR BGCOLOR='#ffcccc'> 
<?php if($quitar == 0) { // if quitar 1 ?>
 <TD align="left" colspan="14"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_deb,$n_decimales); ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_cred,$n_decimales); ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="16"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total2,$n_decimales); ?></b></TD>
 
 <?php } else { // else if quitar 1 ?>
 
 <TD align="left" colspan="7"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="7"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total2,$n_decimales); ?></b></TD>
 <?php } //fin if quitar 1?>
 </TR> 
</tbody>
</table>
  <?php
 } //fin if 3.2
 
} //fin if 3
?>
