pl1_stock_io_ledger_detail.sql 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. truncate table dbt_list_type1;
  2. truncate table dbt_list_sum;
  3. drop procedure if exists pl1_stock_io_ledger_detail;
  4. DELIMITER //
  5. create procedure pl1_stock_io_ledger_detail( _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. _having varchar(386), _orderby varchar(64),
  9. _branch int, _storage int, _member_company_id int, _lt_filter int)
  10. SQL SECURITY INVOKER
  11. BEGIN
  12. Declare _sbal_date varchar(8);
  13. Declare _ebal_date varchar(8);
  14. Declare _first_bal decimal(20,4);
  15. Declare _first_bad_bal decimal(20,4);
  16. Declare _first_bal_caption varchar(20);
  17. -- Declare _first_id int;
  18. -- Declare _last_id int;
  19. Declare _line_id int;
  20. Declare _line_badstk_status varchar(1);
  21. Declare _line_in decimal(20,4) default "0.0000";
  22. Declare _line_out decimal(20,4) default "0.0000";
  23. Declare _line_bal decimal(20,4) default "0.0000";
  24. Declare _line_bad_bal decimal(20,4) default "0.0000";
  25. Declare _in_sum decimal(20,4) default "0.0000";
  26. Declare _out_sum decimal(20,4) default "0.0000";
  27. Declare _company_name varchar(64);
  28. Declare _item_code varchar(21);
  29. Declare _item_sub_name varchar(128);
  30. Declare _not_found boolean;
  31. Declare _line_cur cursor for -- c5:badstk_status
  32. select t_id, c5, d2, d3 from dbt_list_type1 where list_token = _listToken
  33. order by order_by asc;
  34. Declare continue handler for
  35. not found set _not_found = true;
  36. if _is_first_date(_sdate) then
  37. set _sbal_date = '19000101';
  38. set _ebal_date = '19000101';
  39. else
  40. set _sbal_date = _sdate;
  41. set _ebal_date = _edate;
  42. end if;
  43. -- insert for begin_bals
  44. select begin_bal_qty, bad_begin_bal_qty
  45. into _first_bal, _first_bad_bal
  46. from dbr_bal_item
  47. where yyyy_mm = substring(_sdate, 1, 6) and storage_id = _storage and item_id = _lt_filter;
  48. insert into dbt_list_type1 ( created_on, list_token, c3, d4, d5, order_by)
  49. values ( unix_timestamp(), _listToken, '이월재고', _first_bal, _first_bad_bal, '19000101');
  50. -- insert for stock_io
  51. insert into dbt_list_type1 ( created_on, list_token,
  52. c1, c2, c3, c4, c5,
  53. d1, d2, d3, order_by)
  54. select unix_timestamp(), _listToken,
  55. io_date, deal_name, concat(slip_no,' / ', stk.seq_no ), company_name, if(stk.bad_in_qty+stk.bad_out_qty = 0, '0', '1'),
  56. io_prc, stk.in_qty, stk.out_qty, concat( io_date, '-', slip_no, '-', seq_no)
  57. from dbr_stock_io as stk
  58. where io_date between _sdate and _edate and storage_id = _storage and stk.item_id = _lt_filter;
  59. -- compute line_bal
  60. set _line_bal = _first_bal;
  61. set _line_bad_bal = _first_bad_bal;
  62. open _line_cur;
  63. loop1: loop
  64. fetch _line_cur into _line_id, _line_badstk_status, _line_in, _line_out;
  65. if _not_found then leave loop1; end if;
  66. set _in_sum = _in_sum + ifnull(_line_in,0);
  67. set _out_sum = _out_sum + ifnull(_line_out,0);
  68. if _line_badstk_status = '0' then
  69. set _line_bal = ifnull(_line_bal,0) + ifnull(_line_in,0) - ifnull(_line_out,0);
  70. else
  71. set _line_bad_bal = ifnull(_line_bad_bal,0) + ifnull(_line_in,0) - ifnull(_line_out,0);
  72. end if;
  73. update dbt_list_type1 set d4 = _line_bal, d5 = _line_bad_bal where t_id = _line_id;
  74. end loop loop1;
  75. close _line_cur;
  76. -- write list_sum
  77. select item_code, concat(item_name,' / ',sub_name ) into _item_code, _item_sub_name from dbr_item where id = _lt_filter;
  78. insert into dbt_list_sum
  79. ( created_on, list_token, c1, c2, c3, c4, d1, d2, d3, d4)
  80. values (unix_timestamp(), _listToken, _item_code, _item_sub_name, _sdate, _edate,
  81. _first_bal, _in_sum, _out_sum, _line_bal);
  82. END;
  83. //
  84. DELIMITER ;