Advertisement
henikseptiana15

Cek current stock

May 28th, 2021
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH beg_log_os_qty AS (
  2.     -- Ambil saldo awal stock per product
  3.     SELECT product_id, SUM(qty) AS qty
  4.     FROM in_summary_monthly_qty
  5.     WHERE date_year_month = '201610'
  6.     AND doc_type_id = -99
  7.     AND product_status = 'GOOD'
  8.     GROUP BY product_id
  9.     UNION ALL
  10.     -- totalkan qty per product dari data log stock
  11.     SELECT A.product_id, SUM(A.qty) AS qty
  12.     FROM in_log_product_balance_stock A
  13.     WHERE A.product_status = 'GOOD'
  14.     GROUP BY A.product_id
  15.     UNION ALL
  16.     -- totalkan qty barang keluar (adj stock) yg masih outstanding
  17.     SELECT B.product_id, SUM(B.qty_realization) AS qty
  18.     FROM in_inventory A
  19.     INNER JOIN in_inventory_item B ON A.inventory_id = B.inventory_id
  20.     WHERE A.doc_type_id IN (521, 522)
  21.     AND A.status_doc <> 'R'
  22.     AND B.qty_realization < 0
  23.     AND B.product_status = 'GOOD'
  24.     GROUP BY B.product_id
  25. ), summary_stock AS (
  26.     SELECT product_id, SUM(qty) AS qty_check_stock
  27.     FROM beg_log_os_qty
  28.     GROUP BY product_id
  29. )
  30. SELECT A.product_id, B.product_code, B.product_name,
  31.     COALESCE(A.qty_current_stock, 0) AS qty_current_stock,
  32.     COALESCE(C.qty_check_stock, 0) AS qty_check_stock,  
  33.     (COALESCE(A.qty_current_stock, 0) - COALESCE(C.qty_check_stock, 0)) AS selisih_stock
  34. FROM (
  35.     SELECT X.product_id, SUM(X.qty) AS qty_current_stock
  36.     FROM in_product_balance_stock X
  37.     WHERE X.product_status = 'GOOD'
  38.     GROUP BY X.product_id
  39. ) A
  40. INNER JOIN m_product B ON A.product_id = B.product_id
  41. FULL OUTER JOIN summary_stock C ON A.product_id = C.product_id
  42. WHERE (COALESCE(A.qty_current_stock, 0) - COALESCE(C.qty_check_stock, 0)) <> 0
  43. ORDER BY B.product_name;
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement