lt1-stock-io-sum-bal-bf-turbo.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. truncate table dbt_list_type1;
  2. drop procedure if exists lt1_stock_io_sum_bal;
  3. DELIMITER //
  4. create procedure lt1_stock_io_sum_bal( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  5. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  6. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  7. _where varchar(386), _having varchar(386), _orderby varchar(64),
  8. _branch int, _storage int)
  9. SQL SECURITY INVOKER
  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. Declare _sbet3 varchar(64);
  18. Declare _ebet3 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. if _s3 != '' and _e3 != '' then
  41. set _sbet3 = _s3;
  42. set _ebet3 = _e3;
  43. else
  44. set _sbet3 = '!';
  45. set _ebet3 = '힣';
  46. end if;
  47. -- 전월 재고 --
  48. -- bad_qty 는 Delta 만 보여줌.
  49. insert into dbt_list_sum
  50. ( created_on, list_token, h_id, id, c1, c2, c3,
  51. d1, d2, d3, d4, d5,
  52. c4, c5, order_by )
  53. select unix_timestamp(), _listToken, mx.id, mx.id, item_code, item_name, sub_name,
  54. bal_qty, 0, 0, 0, 0,
  55. igroup_name, igroup_code, item_code
  56. from ( select id, item_code, item_name, sub_name, igroup_id
  57. from dbr_item as itm
  58. where item_code between _sbet2 and _ebet2
  59. and isnt_stkio = '0' and is_material = '0'
  60. ) as mx
  61. inner join ( select item_id, bal_qty
  62. from dbr_bal_item
  63. where yyyy_mm = _prev_month(_sdate) and storage_id = _storage
  64. ) as bal on mx.id = bal.item_id
  65. inner join ( select id, igroup_code, igroup_name
  66. from dbr_igroup
  67. where igroup_code between _sbet1 and _ebet1
  68. ) as igr on mx.igroup_id = igr.id
  69. order by item_code asc;
  70. -- 당월 수불 합계 --
  71. insert into dbt_list_sum
  72. ( created_on, list_token, h_id, id, c1, c2, c3,
  73. d1, d2, d3,
  74. d4, d5,
  75. c4, c5, order_by )
  76. select unix_timestamp(), _listToken, mx.id, mx.id, item_code, item_name, sub_name,
  77. 0, sum(io_qty*cast((stock_status+1)/2 as int)), sum(io_qty*cast(-1*(stock_status-1)/2 as int)),
  78. 0, sum(io_qty*badstk_status),
  79. igroup_name, igroup_code, item_code
  80. from ( select id, item_code, item_name, sub_name, igroup_id
  81. from dbr_item as itm
  82. where item_code between _sbet2 and _ebet2
  83. and isnt_stkio = '0' and is_material = '0'
  84. ) as mx
  85. inner join ( select item_id, io_qty, stock_status, badstk_status
  86. from dbr_stock_io as stk
  87. inner join dbr_deal_type as deal on deal.id = stk.deal_type_id
  88. where io_date between _sdate and _edate
  89. and storage_id = _storage
  90. ) as stk on mx.id = stk.item_id
  91. inner join ( select id, igroup_code, igroup_name
  92. from dbr_igroup
  93. where igroup_code between _sbet1 and _ebet1
  94. ) as igr on mx.igroup_id = igr.id
  95. group by stk.item_id
  96. order by item_code asc;
  97. -- dbt_list_type1 으로 집계 --
  98. insert into dbt_list_type1
  99. ( created_on, list_token, h_id, id, c1, c2, c3,
  100. d1, d2, d3, d4, d5,
  101. c4, c5, order_by)
  102. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1), min(c2), min(c3),
  103. sum(d1),
  104. sum(d2),
  105. sum(d3),
  106. sum(d1) + sum(d2) - sum(d3),
  107. sum(d5),
  108. min(c4), min(c5), min(order_by)
  109. from dbt_list_sum as mx
  110. where list_token = _listToken
  111. group by mx.id
  112. having min(mx.id) is not null
  113. order by order_by asc;
  114. END;
  115. //
  116. DELIMITER ;