Advertisement
henikseptiana15

Update data pu_po_balance_item

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