lt1-accounting-cash-sum-bal.sql 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. truncate table dbt_list_sum;
  2. truncate table dbt_list_type1;
  3. drop procedure if exists lt1_accounting_cash_sum_bal;
  4. -- 일단 현금계정만 추가함.
  5. DELIMITER //
  6. create procedure lt1_accounting_cash_sum_bal( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  7. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  8. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  9. _where varchar(386), _having varchar(386), _orderby varchar(64),
  10. _branch int, _storage int)
  11. SQL SECURITY INVOKER
  12. BEGIN
  13. Declare _sbal_date varchar(8);
  14. Declare _ebal_date varchar(8);
  15. Declare _sbet1 varchar(64);
  16. Declare _ebet1 varchar(64);
  17. Declare _sbet2 varchar(64);
  18. Declare _ebet2 varchar(64);
  19. if _is_first_date(_sdate) then
  20. set _sbal_date = '19000101';
  21. set _ebal_date = '19000101';
  22. else
  23. set _sbal_date = _sdate;
  24. set _ebal_date = _edate;
  25. end if;
  26. if _s1 != '' and _e1 != '' then
  27. set _sbet1 = _s1;
  28. set _ebet1 = _e1;
  29. else
  30. set _sbet1 = '!';
  31. set _ebet1 = '駾';
  32. end if;
  33. if _s2 != '' and _e2 != '' then
  34. set _sbet2 = _s2;
  35. set _ebet2 = _e2;
  36. else
  37. set _sbet2 = '!';
  38. set _ebet2 = '駾';
  39. end if;
  40. -- 전월 잔액 --
  41. insert into dbt_list_sum
  42. ( created_on, list_token, h_id, id, c1, c2,
  43. d1, d2, d3, d4,
  44. order_by )
  45. -- 이월 현금 잔액 --
  46. select unix_timestamp(), _listToken, mx.id, mx.id, title_code, title_name,
  47. bal_amt, 0, 0, 0,
  48. title_code
  49. from
  50. ( select acc_title_id, (dr_bal_amt - cr_bal_amt) as bal_amt
  51. from dbr_bal_acc_title
  52. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  53. ) as last_mbal
  54. inner join dbr_acc_title as mx on mx.id = last_mbal.acc_title_id
  55. where ( title_code = '1101' );
  56. -- 일단 현금계정만 추가함.
  57. -- 입출금 합계 --
  58. insert into dbt_list_sum
  59. ( created_on, list_token, h_id, id, c1, c2,
  60. d1, d2, d3, d4,
  61. order_by )
  62. select unix_timestamp(), _listToken, 1, 1, '1101', '현금',
  63. 0, sum(slip_amt*cast((collect_status+1)/2 as int)), sum(slip_amt*cast(-1*(collect_status-1)/2 as int)), sum(slip_amt*collect_status),
  64. '1101'
  65. from
  66. ( select id, branch_id, deal_type_id, slip_amt
  67. from dbr_acc_slip
  68. where acc_date between _sdate and _edate
  69. and deal_type_id between 21 and 25
  70. ) as sbhd
  71. inner join dbr_deal_type as deal on deal.id = sbhd.deal_type_id
  72. where branch_id = _branch
  73. group by branch_id;
  74. -- dbt_list_type1 으로 집계 --
  75. insert into dbt_list_type1
  76. ( created_on, list_token, h_id, id, c1, c2,
  77. d1, d2, d3, d4,
  78. order_by )
  79. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1), min(c2),
  80. sum(d1),
  81. sum(d2),
  82. sum(d3),
  83. sum(d1) + sum(d2) - sum(d3),
  84. min(order_by)
  85. from dbt_list_sum as mx
  86. where list_token = _listToken
  87. group by mx.id
  88. having min(mx.id) is not null
  89. order by order_by asc;
  90. END;
  91. //
  92. DELIMITER ;