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

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