Advertisement
henikseptiana15

Update data pu_po_balance_item

Jan 18th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. WITH get_po_balance_item AS(
  3.     -- Ambil data po balance item yg sudah di GR
  4.     SELECT A.po_item_id, A.qty_int_po, A.qty_int_rcv
  5.     FROM pu_po_balance_item A
  6.     WHERE A.qty_int_rcv <> 0
  7. ), get_data_qty_rcv_invalid AS(
  8.  
  9.     -- Dapatkan data receive goods dimana qty_rcv antara yg di dokumen GR dan di po balance item beda (pastikan summary qty rcv di dokumen GR <= qty po)
  10.     -- Ambil semua data GR apapun status doc nya
  11.     SELECT A.ref_id AS po_id, B.ref_id AS po_item_id,
  12.            SUM(B.qty_rcv_int) AS sum_qty_rcv, C.qty_int_po,
  13.            C.qty_int_rcv
  14.     FROM pu_receive_goods A
  15.     INNER JOIN pu_receive_goods_item B ON A.receive_goods_id = B.receive_goods_id
  16.     INNER JOIN get_po_balance_item C ON B.ref_id = C.po_item_id
  17.     GROUP BY A.ref_id, B.ref_id, C.qty_int_po, C.qty_int_rcv
  18.     HAVING SUM(B.qty_rcv_int) <> C.qty_int_rcv AND
  19.            SUM(B.qty_rcv_int) <= C.qty_int_po
  20.     ORDER BY A.ref_id, B.ref_id
  21. )
  22. UPDATE pu_po_balance_item A
  23. SET qty_rcv = B.sum_qty_rcv,
  24.     qty_int_rcv = B.sum_qty_rcv,
  25.     update_user_id = -3,
  26.     update_datetime = '20180118154300'
  27. FROM get_data_qty_rcv_invalid B
  28. WHERE A.po_item_id = B.po_item_id;
  29.  
  30. -- Update status item = I, jika dok GR nya ada yg status_doc nya <>  R
  31. UPDATE pu_po_balance_item A
  32. SET status_item = 'I',
  33.     update_user_id = -3,
  34.     update_datetime = '20180118154300'
  35. WHERE EXISTS(SELECT 1 FROM pu_receive_goods B, pu_receive_goods C
  36.          WHERE B.receive_goods_id = C.receive_goods_id AND
  37.            C.ref_id = A.po_item_id AND
  38.            B.status_doc <> 'R');
  39.            
  40. -- Update status item = R, jika dok GR nya status_doc nya =  R semua dan masih ada outstanding qty PO
  41. UPDATE pu_po_balance_item A
  42. SET status_item = 'R',
  43.     update_user_id = -3,
  44.     update_datetime = '20180118154300'
  45. WHERE NOT EXISTS(SELECT 1 FROM pu_receive_goods B, pu_receive_goods C
  46.              WHERE B.receive_goods_id = C.receive_goods_id AND
  47.                C.ref_id = A.po_item_id AND
  48.                B.status_doc <> 'R')
  49.       AND (A.qty_po - A.qty_cancel + A.qty_add - A.qty_rcv) > 0;
  50.  
  51. -- Update status item = F, jika dok GR nya status_doc nya =  R semua dan outstanding qty PO = 0
  52. UPDATE pu_po_balance_item A
  53. SET status_item = 'R',
  54.     update_user_id = -3,
  55.     update_datetime = '20180118154300'
  56. WHERE NOT EXISTS(SELECT 1 FROM pu_receive_goods B, pu_receive_goods C
  57.              WHERE B.receive_goods_id = C.receive_goods_id AND
  58.                C.ref_id = A.po_item_id AND
  59.                B.status_doc <> 'R')
  60.       AND (A.qty_po - A.qty_cancel + A.qty_add - A.qty_rcv) = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement