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

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