lt1-purch-sum-bal.sql 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. BEGIN
  11. Declare _sbal_date varchar(8);
  12. Declare _ebal_date varchar(8);
  13. Declare _sbet1 varchar(64);
  14. Declare _ebet1 varchar(64);
  15. Declare _sbet2 varchar(64);
  16. Declare _ebet2 varchar(64);
  17. if _is_first_date(_sdate) then
  18. set _sbal_date = '19000101';
  19. set _ebal_date = '19000101';
  20. else
  21. set _sbal_date = _sdate;
  22. set _ebal_date = _edate;
  23. end if;
  24. if _s1 != '' and _e1 != '' then
  25. set _sbet1 = _s1;
  26. set _ebet1 = _e1;
  27. else
  28. set _sbet1 = '!';
  29. set _ebet1 = '힣';
  30. end if;
  31. if _s2 != '' and _e2 != '' then
  32. set _sbet2 = _s2;
  33. set _ebet2 = _e2;
  34. else
  35. set _sbet2 = '!';
  36. set _ebet2 = '힣';
  37. end if;
  38. -- 전월 잔액 --
  39. insert into dbt_list_sum
  40. ( created_on, list_token, h_id, id, c1,
  41. d1, d2, d3, d4, d5, d6,
  42. c2, c3, c4, order_by)
  43. select unix_timestamp(), _listToken, mx.id, mx.id, mx.company_name,
  44. bal_amt, 0, 0, 0, 0, 0,
  45. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  46. from
  47. ( select supplier_id, bal_amt
  48. from dbr_bal_supplier
  49. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  50. ) as last_mbal
  51. inner join dbr_company as mx on mx.id = last_mbal.supplier_id
  52. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  53. where cgr.cgroup_code between _sbet1 and _ebet1
  54. and mx.company_name between _sbet2 and _ebet2
  55. and company_class = 'BB';
  56. -- 매출/반품 합계 --
  57. insert into dbt_list_sum
  58. ( created_on, list_token, h_id, id, c1,
  59. d1, d2, d3, d4, d5, d6,
  60. c2, c3, c4, order_by)
  61. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  62. 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,
  63. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  64. from
  65. ( select id, purch_no, purch_date
  66. from dbr_purch
  67. where purch_date between _sdate and _edate
  68. ) as sbhd
  69. inner join dbr_purch_bd as sbbd on sbhd.id = sbbd.purch_id
  70. inner join dbr_porder_bd as mnbd on mnbd.id = sbbd.porder_bd_id
  71. inner join dbr_porder as mnhd on mnhd.id = mnbd.porder_id
  72. inner join dbr_item as itm on mnbd.item_id = itm.id
  73. inner join dbr_deal_type as deal on deal.id = mnhd.deal_type_id
  74. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  75. from dbr_company as cmp
  76. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  77. where cgr.cgroup_code between _sbet1 and _ebet1
  78. and cmp.company_name between _sbet2 and _ebet2
  79. and company_class = 'BB'
  80. ) as cmp on cmp.id = mnhd.supplier_id
  81. where branch_id = _branch
  82. group by cmp.id;
  83. -- 입금/입금할인 합계 --
  84. insert into dbt_list_sum
  85. ( created_on, list_token, h_id, id, c1,
  86. d1, d2, d3, d4, d5, d6,
  87. c2, c3, c4, order_by)
  88. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  89. 0, 0, 0, sum(slip_amt*cast((collect_status+1)/2 as int)),
  90. sum(slip_amt*cast(-1*(collect_status-1)/2 as int)), 0,
  91. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, company_no)
  92. from
  93. ( select id, acc_date, acc_slip_no, deal_type_id,
  94. bill_column1, bill_type, slip_amt, company_id
  95. from dbr_acc_slip
  96. where ( deal_type_id = 22 or deal_type_id = 24 ) -- 출금과 출금할인만
  97. and acc_date between _sdate and _edate
  98. and branch_id = _branch
  99. ) as mnhd
  100. inner join dbr_deal_type as deal on deal.id = mnhd.deal_type_id
  101. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  102. from dbr_company as cmp
  103. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  104. where cgr.cgroup_code between _sbet1 and _ebet1
  105. and cmp.company_name between _sbet2 and _ebet2
  106. and company_class = 'BB'
  107. ) as cmp on cmp.id = mnhd.company_id
  108. group by cmp.id;
  109. -- dbt_list_type1 으로 집계 --
  110. insert into dbt_list_type1
  111. ( created_on, list_token, h_id, id, c1,
  112. d1, d2, d3, d4, d5, d6,
  113. c2, c3, c4, order_by)
  114. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1),
  115. sum(d1),
  116. sum(d2),
  117. sum(d3),
  118. sum(d4),
  119. sum(d5),
  120. sum(d1) + sum(d2) + sum(d3) - sum(d4) - sum(d5),
  121. min(c2), min(c3), min(c4), min(order_by)
  122. from dbt_list_sum as mx
  123. where list_token = _listToken
  124. group by mx.id
  125. having min(mx.id) is not null
  126. order by order_by asc;
  127. END;
  128. //
  129. DELIMITER ;