Advertisement
henikseptiana15

Cek current stock

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