lt1-purch-sum-bal.sql 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. truncate table dbt_list_sum;
  2. truncate table dbt_list_type1;
  3. drop procedure if exists lt1_purch_sum_bal;
  4. DELIMITER //
  5. create procedure lt1_purch_sum_bal( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  6. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  7. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  8. _where varchar(386), _having varchar(386), _orderby varchar(64),
  9. _branch int, _storage int)
  10. SQL SECURITY INVOKER
  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,
  42. d1, d2, d3, d4, d5, d6,
  43. c2, c3, c4, order_by)
  44. select unix_timestamp(), _listToken, mx.id, mx.id, mx.company_name,
  45. bal_amt, 0, 0, 0, 0, 0,
  46. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  47. from
  48. ( select supplier_id, bal_amt
  49. from dbr_bal_supplier
  50. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  51. ) as last_mbal
  52. inner join dbr_company as mx on mx.id = last_mbal.supplier_id
  53. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  54. where cgr.cgroup_code between _sbet1 and _ebet1
  55. and mx.company_name between _sbet2 and _ebet2
  56. and company_class = 'BB';
  57. -- 매출/반품 합계 --
  58. insert into dbt_list_sum
  59. ( created_on, list_token, h_id, id, c1,
  60. d1, d2, d3, d4, d5, d6,
  61. c2, c3, c4, order_by)
  62. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  63. 0, sum(purch_sum*cast((purch_status+1)/2 as int)), sum(purch_sum*cast(-1*(purch_status-1)/2 as int)), 0, 0, 0,
  64. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  65. from
  66. ( select id, purch_no, purch_date
  67. from dbr_purch
  68. where purch_date between _sdate and _edate
  69. ) as sbhd
  70. inner join dbr_purch_bd as sbbd on sbhd.id = sbbd.purch_id
  71. inner join dbr_porder_bd as mnbd on mnbd.id = sbbd.porder_bd_id
  72. inner join dbr_porder as mnhd on mnhd.id = mnbd.porder_id
  73. inner join dbr_item as itm on mnbd.item_id = itm.id
  74. inner join dbr_deal_type as deal on deal.id = mnhd.deal_type_id
  75. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  76. from dbr_company as cmp
  77. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  78. where cgr.cgroup_code between _sbet1 and _ebet1
  79. and cmp.company_name between _sbet2 and _ebet2
  80. and company_class = 'BB'
  81. ) as cmp on cmp.id = mnhd.supplier_id
  82. where branch_id = _branch
  83. group by cmp.id;
  84. -- 입금/입금할인 합계 --
  85. insert into dbt_list_sum
  86. ( created_on, list_token, h_id, id, c1,
  87. d1, d2, d3, d4, d5, d6,
  88. c2, c3, c4, order_by)
  89. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  90. 0, 0, 0, sum(slip_amt*cast((collect_status+1)/2 as int)),
  91. sum(slip_amt*cast(-1*(collect_status-1)/2 as int)), 0,
  92. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  93. from
  94. ( select id, acc_date, acc_slip_no, deal_type_id,
  95. bill_column1, bill_type, slip_amt, company_id
  96. from dbr_acc_slip
  97. where ( deal_type_id = 22 or deal_type_id = 24 ) -- 출금과 출금할인만
  98. and acc_date between _sdate and _edate
  99. and branch_id = _branch
  100. ) as mnhd
  101. inner join dbr_deal_type as deal on deal.id = mnhd.deal_type_id
  102. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  103. from dbr_company as cmp
  104. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  105. where cgr.cgroup_code between _sbet1 and _ebet1
  106. and cmp.company_name between _sbet2 and _ebet2
  107. and company_class = 'BB'
  108. ) as cmp on cmp.id = mnhd.company_id
  109. group by cmp.id;
  110. -- dbt_list_type1 으로 집계 --
  111. insert into dbt_list_type1
  112. ( created_on, list_token, h_id, id, c1,
  113. d1, d2, d3, d4, d5, d6,
  114. c2, c3, c4, order_by)
  115. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1),
  116. sum(d1),
  117. sum(d2),
  118. sum(d3),
  119. sum(d4),
  120. sum(d5),
  121. sum(d1) + sum(d2) + sum(d3) - sum(d4) - sum(d5),
  122. min(c2), min(c3), min(c4), min(order_by)
  123. from dbt_list_sum as mx
  124. where list_token = _listToken
  125. group by mx.id
  126. having min(mx.id) is not null
  127. order by order_by asc;
  128. END;
  129. //
  130. DELIMITER ;