fnc_curr_item_bal.sql 1.1 KB

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