Advertisement
daxruiz

prueba cliente

Mar 7th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. declare @date datetime= '20230226', @clientid int = 1;
  2.  
  3. DECLARE @accountStatus as table
  4. (
  5. Id int identity(1,1),
  6. Description varchar(500),
  7. Credit money,
  8. Debit money,
  9. Saldo money,
  10. Factor int
  11. )
  12.  
  13. insert into @accountStatus
  14. (Description, Credit, Debit, Saldo, Factor)
  15. SELECT top 1 'Saldo inicial', 0 , 0, t.prepaid_balance, 1
  16. FROM historicalClientNetPosition t
  17. where CAST(t.net_position_date as date) = cast(@date as date)
  18.  
  19. insert into @accountStatus
  20. (Description, Credit, Debit, Saldo, Factor)
  21. select
  22. tt.description AS Description,
  23. CASE transaction_Type WHEN 100 THEN SUM(AMOUNT) ELSE 0 END Credit,
  24. CASE transaction_Type WHEN 100 THEN 0 ELSE SUM(AMOUNT) END Debit,
  25. 0 as saldo,
  26. CASE transaction_Type WHEN 100 THEN 1 ELSE -1 END Factor
  27. from (
  28. SELECT 1 as type, client_id, float_deposit_date transaction_Date, 100 transaction_Type, amount_deposited amount, reference
  29. FROM floatDeposits
  30. where cast(float_deposit_date as date) > cast(@date as date) and client_id = @clientId
  31. UNION
  32. select 2 as type,client_id, deposit_date transaction_Date, 100 transaction_Type, amount_deposited amount, reference
  33. from deposits
  34. where cast(deposit_date as date) > cast(@date as date) and client_id = @clientId
  35. UNION
  36. SELECT 3 as type,client_id, transaction_date, transaction_type, amount, reference
  37. FROM registerPaidIncome
  38. where cast(transaction_date as date) > cast(@date as date) and client_id = @clientId
  39. UNION
  40. select 4 as type,client_id, transaction_date, transaction_type, amount, reference
  41. from registerUnPaidIncome
  42. where cast(transaction_date as date) > cast(@date as date) and client_id = @clientId
  43. )A
  44. INNER JOIN transactionsType tt on tt.transaction_type_id = A.transaction_Type
  45. GROUP BY tt.description,tt.priority, A.transaction_Type
  46. ORDER BY tt.priority DESC
  47.  
  48. declare @i int = 1, @rows int = (select count(*) from @accountStatus);
  49.  
  50. while @i <= @rows
  51. begin
  52.  
  53. if @i > 1
  54. begin
  55. declare @saldoAnterior as money = 0;
  56.  
  57. select top 1 @saldoAnterior = t.Saldo from @accountStatus t where t.Id < @I order by Id desc;
  58.  
  59. update @accountStatus
  60. set Saldo = @saldoAnterior + case when Factor = 1 then Credit else (Debit * -1) end
  61. where Id = @i
  62. end
  63. set @i = @i + 1;
  64. end
  65.  
  66. insert into @accountStatus
  67. (Description, Credit, Debit, Saldo, Factor)
  68. SELECT top 1 'Saldo Final', 0 , 0, t.Saldo, 1
  69. FROM @accountStatus t
  70. order by t.Id desc
  71.  
  72. select Id, Description, Credit, Debit, Saldo, Factor
  73. from @accountStatus
  74. order by Id
  75.  
  76.  
  77.  
  78. SELECT 1 as type, client_id, float_deposit_date transaction_Date, 100 transaction_Type, amount_deposited amount, reference
  79. FROM floatDeposits
  80. --where cast(float_deposit_date as date) > cast(@date as date) and client_id = @clientId
  81. UNION
  82. select 2 as type,client_id, deposit_date transaction_Date, 100 transaction_Type, amount_deposited amount, reference
  83. from deposits
  84. --where cast(deposit_date as date) > cast(@date as date) and client_id = @clientId
  85. UNION
  86. SELECT 3 as type,client_id, transaction_date, transaction_type, amount, reference
  87. FROM registerPaidIncome
  88. --where cast(transaction_date as date) > cast(@date as date) and client_id = @clientId
  89. UNION
  90. select 4 as type,client_id, transaction_date, transaction_type, amount, reference
  91. from registerUnPaidIncome
  92. --where cast(transaction_date as date) > cast(@date as date) and client_id = @clientId
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement