Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. set ANSI_NULLS ON
  2. set QUOTED_IDENTIFIER ON
  3. GO
  4. ALTER procedure [dbo].[ADP_SP_GetBalanzaDetalles](
  5. @pfechaInicio as nvarchar (30),
  6. @pfechaFin as nvarchar (30),
  7. @pprimerNivel as int,
  8. @pultimoNivel as int,
  9. @pcantSubCtas as int,
  10. @psDB as nvarchar(254)=''
  11. )
  12.  
  13. as
  14.  
  15.  
  16.  
  17. declare @SQLString nvarchar (max)
  18. DECLARE @ParmDefinition nvarchar(500);
  19.  
  20. SET @ParmDefinition = N'@fechaInicio datetime, @fechaFin datetime, @primerNivel int, @ultimoNivel int, @cantSubCtas int';
  21.  
  22. set @SQLString = N'declare @saldoInicial table (
  23. SysAccountCode varchar (50) collate SQL_Latin1_General_CP850_CI_AS,
  24. saldoInicial decimal (16, 6)
  25. )
  26.  
  27.  
  28. declare @cargosAbonos table (
  29. SysAccountCode varchar (50) collate SQL_Latin1_General_CP850_CI_AS,
  30. Cargo decimal (19,6),
  31. Abono decimal (19, 6)
  32. )
  33.  
  34. declare @balanzaComp table (
  35. SysAccountCode varchar (50),
  36. AccountCode varchar (50),
  37. FatherNum varchar (50),
  38. Levels int,
  39. Nivel int,
  40. NumHijos int,
  41. SaldoInicial decimal (19, 6),
  42. Cargo decimal (19, 6),
  43. Abono decimal (19, 6),
  44. SaldoFinal decimal (19, 6),
  45. CodAgrup varchar (10)
  46. )
  47.  
  48. declare @LevelCount int,
  49. @LevelCount2 int,
  50. @NumHijos int
  51.  
  52.  
  53. --Obtiene el saldo inicial de las cuentas afectadas
  54. insert into @saldoInicial(SysAccountCode, SaldoInicial)
  55. select ac.AcctCode, sum (j1.Debit - j1.Credit) SaldoInicial
  56. from [' + @psDB+ '].dbo.OACT ac left outer join [' + @psDB + '].dbo.JDT1 j1 on j1.Account = ac.AcctCode
  57. where j1.RefDate < @fechaInicio
  58. group by ac.AcctCode
  59.  
  60. --Obtiene cargos y abonos de las cuentas afectadas
  61. insert into @cargosAbonos(SysAccountCode, Cargo, Abono)
  62. select j1.Account, sum (j1.Debit), sum (j1.Credit)
  63. from [' + @psDB+ '].dbo.OJDT jd inner join [' + @psDB+ '].dbo.JDT1 j1
  64. on jd.TransId = j1.TransId
  65. where j1.RefDate between @fechaInicio and @fechaFin
  66. and jd.AdjTran = ''N''
  67. group by j1.Account
  68.  
  69. insert into @balanzaComp(SysAccountCode, AccountCode, FatherNum, Levels, Nivel, NumHijos,
  70. SaldoInicial, Cargo, Abono, saldoFinal, CodAgrup)
  71. select ac.AcctCode, ac.FormatCode, isnull (ac2.FormatCode, ac2.AcctCode), ac.Levels, 1, 0, isnull (bB.saldoInicial, 0) saldoInicial,
  72. isnull (dC.Cargo, 0) Debit, isnull (dC.Abono, 0) Credit,
  73. isnull (bB.saldoInicial, 0) + isnull (dC.Cargo, 0) - isnull (dC.Abono, 0) FinalBalance,
  74. ac.U_ADP_CODAGRUP
  75. from [' + @psDB + '].dbo.OACT ac left outer join [' + @psDB+ '].dbo.OACT ac2 on ac.FatherNum = ac2.AcctCode
  76. left outer join @saldoInicial bB on ac.AcctCode = bB.SysAccountCode
  77. left outer join @cargosAbonos dC on ac.AcctCode = dC.SysAccountCode
  78. where ac.U_ADP_CODAGRUP is not null
  79.  
  80.  
  81. select @LevelCount = max (Levels) from [' + @psDB+ '].dbo.OACT
  82.  
  83. while @LevelCount >= 1
  84. begin
  85. update @balanzaComp
  86. set NumHijos = sB.NumHijos + 1
  87. from @balanzaComp tB inner join (select FatherNum, NumHijos
  88. from @balanzaComp
  89. where Levels = @LevelCount) sB on tB.SysAccountCode = sB.FatherNum
  90.  
  91. select @LevelCount = @LevelCount - 1
  92. end
  93.  
  94. update @balanzaComp
  95. set Nivel = abs((NumHijos - @cantSubCtas)) + 1,
  96. FatherNum = case when abs (NumHijos - @cantSubCtas) + 1 = 1 then NULL else FatherNum end
  97. where Levels between @primerNivel and @ultimoNivel
  98. and NumHijos <= @cantSubCtas
  99.  
  100.  
  101. --Contabiliza heredando las sumas de los hijos hacia los niveles padre
  102. select @LevelCount = max(Nivel)
  103. from @balanzaComp
  104. where Levels between @primerNivel and @ultimoNivel
  105. and NumHijos <= @cantSubCtas
  106.  
  107. while @LevelCount >= 1
  108. begin
  109. update @balanzaComp
  110. set SaldoInicial = sB.sumaInicial,
  111. Cargo = sB.sumaCargo,
  112. Abono = sB.sumaAbono,
  113. SaldoFinal = SB.sumaFinal
  114. from @balanzaComp tB inner join (select FatherNum, sum (saldoInicial) sumaInicial,
  115. sum (Cargo) sumaCargo, sum (Abono) sumaAbono,
  116. sum (SaldoFinal) sumaFinal
  117. from @balanzaComp
  118. where Nivel = @LevelCount
  119. and Levels between @primerNivel and @ultimoNivel
  120. and NumHijos <= @cantSubCtas
  121. group by FatherNum) sB on tB.SysAccountCode = sB.FatherNum
  122.  
  123. select @LevelCount = @LevelCount - 1
  124. end
  125.  
  126.  
  127. select SysAccountCode, isnull (AccountCode, SysAccountCode) Accountcode,
  128. FatherNum, Levels Nivel, NumHijos,
  129. Convert(Decimal(19, 2), LTrim(Str(SaldoInicial, 19, 2))) SaldoInicial,
  130. Convert(Decimal(19, 2), LTrim(Str(Cargo, 19, 2))) Cargo,
  131. Convert(Decimal(19, 2), LTrim(Str(Abono, 19, 2))) Abono,
  132. Convert(Decimal(19, 2), LTrim(Str(SaldoFinal, 19, 2))) SaldoFinal,
  133. CodAgrup
  134. from @balanzaComp
  135. where Levels between @primerNivel and @ultimoNivel
  136. and NumHijos <= @cantSubCtas
  137. order by 2'
  138.  
  139. print @SQLString
  140.  
  141. EXECUTE sp_executesql @SQLString, @ParmDefinition, @fechaInicio = @pfechaInicio, @fechaFin = @pfechaFin, @primerNivel = @pprimerNivel, @ultimoNivel = @pultimoNivel, @cantSubCtas = @pcantSubCtas;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement