fnc_curr_item_bal.sql 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. drop procedure if exists fnc_curr_item_bal;
  2. DELIMITER //
  3. create procedure fnc_curr_item_bal( _curr_date varchar(8), _storage int, _target int)
  4. SQL SECURITY INVOKER
  5. BEGIN
  6. Declare _last_amt decimal(20,4) default "0.0000";
  7. Declare _last_bad decimal(20,4) default "0.0000";
  8. Declare _sum_amt decimal(20,4) default "0.0000";
  9. Declare _sum_bad decimal(20,4) default "0.0000";
  10. Declare _bal_date varchar(8);
  11. if _is_first_date(_curr_date) then
  12. set _bal_date = '19000101';
  13. else
  14. set _bal_date = _curr_date;
  15. end if;
  16. select bal_qty, bad_bal_qty
  17. into _last_amt, _last_bad
  18. from dbr_bal_item
  19. where item_id = _target
  20. and yyyy_mm = _prev_month(_curr_date) and storage_id = _storage;
  21. -- 당월 변동잔액
  22. select sum(io_qty*stock_status), sum(io_qty*badstk_status)
  23. into _sum_amt, _sum_bad
  24. from dbr_stock_io
  25. inner join dbr_deal_type as deal on deal.id = deal_type_id
  26. where item_id = _target
  27. and io_date between _first_date(_bal_date) and _bal_date
  28. and storage_id = _storage
  29. group by item_id;
  30. select _last_amt + _sum_amt as c1, _last_bad + _sum_bad as c2;
  31. END;
  32. //
  33. DELIMITER ;
  34. call fnc_curr_item_bal('20230228', 1, 1) ;