USE [GRPAN]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--exec dbo.DYN_RPT_BALANCEDEPRUEBAPORRANGODEFECHAS2 '20190201','20190307',1,'2335','2345'
-- Este no genera cuentas sin saldo ni movimiento
create procedure [dbo].[DYN_RPT_BALANCEDEPRUEBAPORRANGODEFECHAS2] @FINICIAL datetime
, @FFINAL datetime
, @LIBRO varchar(100)
, @COUNT1 varchar(50)
, @COUNT2 varchar(50)
as
/*
Descripcion: Genera un reporte de balance financiero por rango de fechas tomando como base las tablas de acumulado de
periodo de GP (GL1x110) mas los movimientos financieros (GLx0000).
Procedimiento:
Para la porción de saldo inicial que se toma de los acumulados se necesita conocer:
cual es el periodo inmediatamente anterior al inicio del rango de fechas, y,
cuando fue el último año cerrado para solo tomar un periodo 0
Del movimiento se toma desde la fecha final del último periodo acumulado hasta la fecha final del rango y dependiendo
de la fecha del movimiento se clasifica como saldo inicial o como debitos o créditos del periodo
*/
begin
set nocount on;
exec dbo.DYN_SRPT_ARBOLPUC_NSAPUC
declare @UltAnoCerrado smallint = 0
, @PeriodoInicial int
, @AnoInicial int
, @AnoUltimoBBF int = 0
set @UltAnoCerrado = (
select max(YEAR1)
from SY40101 with (nolock)
where HISTORYR = 1
)
select top 1 @PeriodoInicial = PERIODID
, @AnoInicial = YEAR1
from SY40100 with (nolock)
where SERIES = 0
and PERDENDT < @FINICIAL
order by PERIODDT desc
select top 1 @AnoUltimoBBF = HSTYEAR
from GL30000 with (nolock)
where PERIODID = 0
and HSTYEAR <= @AnoInicial
order by HSTYEAR desc
select IND = GL.ACTINDX
, DESCRIPCION = max(ACTDESCR)
, DEBITO = sum(DB)
, CREDITO = sum(CR)
, SALDOINI = sum(SI)
, CTA = max(B.ACTNUMBR_1)
, ACTNUMST = max(ACTNUMST)
, ACTDESCR = max(ACTDESCR)
, ACTNUMBR_1 = max(B.ACTNUMBR_1)
, ACTNUMBR_2 = max(B.ACTNUMBR_2)
, ACTNUMBR_3 = max(B.ACTNUMBR_3)
, ACTNUMBR_4 = max(B.ACTNUMBR_4)
, ACTNUMBR_5 = max(B.ACTNUMBR_5)
, ACTNUMBR_6 = max(B.ACTNUMBR_6)
, ACTNUMBR_7 = max(B.ACTNUMBR_7)
, ACTNUMBR_8 = max(B.ACTNUMBR_8)
, ACTNUMBR_9 = max(B.ACTNUMBR_9)
, ACTNUMBR_10 = max(B.ACTNUMBR_10)
, PUCCODE = max(B.PUCCODE)
, max(PUC1.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_1
, max(PUC2.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_2
, max(PUC3.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_3
, max(PUC4.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_4
, max(PUC5.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_5
, max(PUC6.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_6
, max(PUC7.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_7
, max(PUC8.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_8
, max(PUC9.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_9
, max(PUC10.nsa_Descripcion_Codigo) as DESC_ACTNUMBR_10
from (
-- Tome saldo de periodo inicial de años abiertos o cerrados
select GL10110.ACTINDX
, sum(PERDBLNC) SI
, convert(numeric(19, 5), 0) DB
, convert(numeric(19, 5), 0) CR
, 'SIPO' origen
from GL10110 with(nolock)
inner join GL00105 with(nolock)
on GL00105.ACTINDX = GL10110.ACTINDX
and GL00105.ACTNUMST between @COUNT1 and @COUNT2
where Ledger_ID in ( select Param from DYN_FUN_SEPARADORCOMAS(@LIBRO, ',') )
and @AnoInicial > @UltAnoCerrado
and ( YEAR1 < @AnoInicial
or ( YEAR1 = @AnoInicial and PERIODID <= @PeriodoInicial)
)
group by GL10110.ACTINDX
union all
select GL10111.ACTINDX
, sum(PERDBLNC) SI
, convert(numeric(19, 5), 0) DB
, convert(numeric(19, 5), 0) CR
, 'SIPH' origen
from GL10111 with (nolock)
inner join GL00105 with(nolock)
on GL00105.ACTINDX = GL10111.ACTINDX
and GL00105.ACTNUMST between @COUNT1 and @COUNT2
where Ledger_ID in ( select Param from DYN_FUN_SEPARADORCOMAS(@LIBRO, ',') )
and @AnoInicial <= @UltAnoCerrado
and YEAR1 >= @AnoUltimoBBF
and ( YEAR1 < @AnoInicial
or ( YEAR1 = @AnoInicial and PERIODID <= @PeriodoInicial)
)
group by GL10111.ACTINDX
union all
-- Tome movimientos
select GL30000.ACTINDX
, sum(case when TRXDATE < @FINICIAL then DEBITAMT - CRDTAMNT else 0 end) SI
, sum(case when TRXDATE >= @FINICIAL then DEBITAMT else 0 end) DB
, sum(case when TRXDATE >= @FINICIAL then CRDTAMNT else 0 end) CR
, 'SIMH' origen
from GL30000 with (nolock)
inner join GL00105 with(nolock)
on GL00105.ACTINDX = GL30000.ACTINDX
and GL00105.ACTNUMST between @COUNT1 and @COUNT2
where Ledger_ID in ( select Param from DYN_FUN_SEPARADORCOMAS(@LIBRO, ',') )
and TRXDATE <= @FFINAL
and PERIODID <> 0
and ( HSTYEAR > @AnoInicial
or ( HSTYEAR = @AnoInicial and PERIODID > @PeriodoInicial)
)
group by GL30000.ACTINDX
union all
select GL20000.ACTINDX
, sum(case when TRXDATE < @FINICIAL then DEBITAMT - CRDTAMNT else 0 end) SI
, sum(case when TRXDATE >= @FINICIAL then DEBITAMT else 0 end) DB
, sum(case when TRXDATE >= @FINICIAL then CRDTAMNT else 0 end) CR
, 'SIMO' origen
from GL20000 with (nolock)
inner join GL00105 with(nolock)
on GL00105.ACTINDX = GL20000.ACTINDX
and GL00105.ACTNUMST between @COUNT1 and @COUNT2
where Ledger_ID in ( select Param from DYN_FUN_SEPARADORCOMAS(@LIBRO, ',') )
and TRXDATE <= @FFINAL
and PERIODID <> 0
and ( OPENYEAR > @AnoInicial
or ( OPENYEAR = @AnoInicial and PERIODID > @PeriodoInicial)
)
group by GL20000.ACTINDX
) GL
inner join GL00100
on GL00100.ACTINDX = GL.ACTINDX
inner join GL00105
on GL00105.ACTINDX = GL.ACTINDX
left join nsaPUC_GL00100
on nsaPUC_GL00100.ACTINDX = GL.ACTINDX
left join ##DYN_TABLE_ARBOLPUC B
on nsaPUC_GL00100.PUCCODE = B.PUCCODE
left join nsaPUC_GL10000 PUC1
on B.ACTNUMBR_1 = REPLACE(PUC1.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC2
on B.ACTNUMBR_2 = REPLACE(PUC2.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC3
on B.ACTNUMBR_3 = REPLACE(PUC3.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC4
on B.ACTNUMBR_4 = REPLACE(PUC4.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC5
on B.ACTNUMBR_5 = REPLACE(PUC5.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC6
on B.ACTNUMBR_6 = REPLACE(PUC6.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC7
on B.ACTNUMBR_7 = REPLACE(PUC7.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC8
on B.ACTNUMBR_8 = REPLACE(PUC8.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC9
on B.ACTNUMBR_9 = REPLACE(PUC9.nsa_Codigo, '-', '')
left join nsaPUC_GL10000 PUC10
on B.ACTNUMBR_10 = REPLACE(PUC10.nsa_Codigo, '-', '')
group by GL.ACTINDX
having sum(DB) <> 0
or sum(CR) <> 0
or sum(SI) <> 0
order by ACTNUMST
end
GO
Me muestra ERROR EN B.ACNUMBR_1, B.ACNUMBR_2, B.ACNUMBR_3, B.ACNUMBR_4, B.ACNUMBR_5, B.ACNUMBR_6, B.ACNUMBR_7, B.ACNUMBR_8, B.ACNUMBR_9, B.ACNUMBR_10, B.PUCCODE
ME DICE QUE
THE MULTI-PART IDENTIFIER B.ACNUMBR_1, B.ACNUMBR_2, B.ACNUMBR_3, B.ACNUMBR_4, B.ACNUMBR_5, B.ACNUMBR_6, B.ACNUMBR_7, B.ACNUMBR_8, B.ACNUMBR_9, B.ACNUMBR_10, B.PUCCODE COULD NOT BE BOUND.