Advertisement
henikseptiana15

recalculate reserved stock

May 28th, 2021 (edited)
1,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  -- Script untuk recalculate reserved stock
  3.  
  4.  -- Create table temporary
  5.   CREATE TEMPORARY TABLE temp_trx_of_reserved_stock (
  6.      session_id character varying (100) NOT NULL,
  7.      data_type character varying (50) NOT NULL,
  8.      product_name character varying (100),
  9.      product_id bigint,
  10.      doc_no character varying (50),
  11.      doc_date character varying (8),
  12.      qty numeric     
  13.  );
  14.  
  15.  -- # Insert transaksi-transaksi yang mempengaruhi reserved stock ke table temp
  16.     -- ambil booking reserved dari settlement (belum di DO maupun lagi proses verify)
  17.      INSERT INTO temp_trx_of_reserved_stock (
  18.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  19.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_sos_non_do_non_verify' AS data_type,
  20.         f_get_product_name(B.product_id) AS product_name, B.product_id, A.doc_no, A.doc_date, B.qty_settlement  
  21.      FROM sl_so_settlement A
  22.          INNER JOIN sl_so_settlement_item B ON A.so_settlement_id = B.so_settlement_id
  23.          WHERE EXISTS(
  24.          SELECT 1
  25.          FROM sl_so_settlement C
  26.          INNER JOIN sl_so X ON X.so_id = C.ref_id AND X.doc_type_id = C.ref_doc_type_id
  27.          WHERE A.tenant_id = C.tenant_id
  28.          AND A.so_settlement_id = C.so_settlement_id
  29.          AND X.status_doc <> 'V'
  30.          AND NOT EXISTS (SELECT 1 FROM sl_do D WHERE C.ref_id = D.ref_id AND C.ref_doc_type_id = D.ref_doc_type_id AND D.status_doc = 'R' )
  31.          AND NOT EXISTS (SELECT 1 FROM sl_so_status E WHERE X.so_id = E.so_id AND E.status_do = 'V' )
  32.          )
  33.      AND A.tenant_id = 10
  34.      AND A.ou_id = 10
  35.      AND B.qty_settlement > 0
  36.      ORDER BY f_get_product_name(B.product_id);
  37.    
  38.     -- Ambil bookingan reserved dari verifikasi DO (sudah diverify /scan lengkap dan belum sampai di DO)
  39.      INSERT INTO temp_trx_of_reserved_stock (
  40.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  41.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_verify_non_do' AS data_type,
  42.         f_get_product_name(B.product_id) AS product, B.product_id, A.doc_no, A.doc_date, B.qty_verify
  43.      FROM in_verification_header A
  44.      INNER JOIN in_verification_item B ON A.verification_id = B.verification_id
  45.      INNER JOIN sl_so X ON X.so_id = A.doc_id AND X.doc_type_id = A.doc_type_id
  46.      WHERE A.tenant_id = 10
  47.      AND B.qty_verify > 0
  48.      AND X.ou_id = 10
  49.      AND X.status_doc <> 'V'
  50.      AND NOT EXISTS (SELECT 1 FROM sl_do D WHERE X.so_id = D.ref_id AND X.doc_type_id = D.ref_doc_type_id  AND D.status_doc = 'R' )
  51.      AND EXISTS (SELECT 1 FROM sl_so_status E WHERE X.so_id = E.so_id AND E.status_do = 'V' )
  52.      UNION ALL
  53.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_verify_non_do' AS data_type,
  54.         f_get_product_name(B.product_id) AS product, B.product_id, A.ref_doc_no AS doc_no, A.ref_doc_date AS doc_date,
  55.      B.qty_current_scan  AS qty_verify
  56.      FROM in_scanner_header A
  57.      INNER JOIN in_scanner_item B ON A.scanner_header_id = B.scanner_header_id
  58.      INNER JOIN sl_so X ON X.so_id = A.ref_doc_id AND X.doc_type_id = A.ref_doc_type_id
  59.      WHERE A.tenant_id = 10
  60.      AND B.qty_current_scan > 0 AND B.scan_status = 'SL'
  61.      AND X.ou_id = 10
  62.      AND X.status_doc <> 'V'
  63.      AND NOT EXISTS (SELECT 1 FROM sl_do D WHERE X.so_id = D.ref_id AND X.doc_type_id = D.ref_doc_type_id  AND D.status_doc = 'R' )
  64.      AND EXISTS (SELECT 1 FROM sl_so_status E WHERE X.so_id = E.so_id AND E.status_do = 'V' )
  65.      ORDER BY product;
  66.    
  67.     -- Ambil qty reserved dari cancel sob, dan sudah disettlement (belum di DO maupun lagi proses di verify)
  68.      INSERT INTO temp_trx_of_reserved_stock (
  69.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  70.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_cancel_so_with_settle' AS data_type,
  71.         f_get_product_name(F.product_id), F.product_id, A.doc_no, A.doc_date, -1*F.qty_settlement AS qty_cancel
  72.      FROM sl_manage_so A
  73.      INNER JOIN sl_manage_so_item B ON A.manage_so_id = B.manage_so_id
  74.      INNER JOIN sl_so_item C ON B.so_item_id = C.so_item_id
  75.      INNER JOIN sl_so D ON C.so_id = D.so_id
  76.      INNER JOIN sl_so_settlement E ON D.so_id = E.ref_id AND D.doc_type_id = E.ref_doc_type_id
  77.      INNER JOIN sl_so_settlement_item F ON E.so_settlement_id = F.so_settlement_id AND C.product_id = F.product_id
  78.      WHERE A.tenant_id = 10
  79.      AND A.ou_id = 10
  80.      AND A.doc_type_id = 392
  81.      AND A.status_doc = 'R'
  82.      AND D.status_doc <> 'V'
  83.      AND F.qty_settlement > 0
  84.      AND NOT EXISTS (SELECT 1 FROM sl_do X WHERE E.ref_id = X.ref_id AND E.ref_doc_type_id = X.ref_doc_type_id AND X.status_doc = 'R')
  85.      AND NOT EXISTS (SELECT 1 FROM sl_so_status Y WHERE A.so_id = Y.so_id AND Y.status_do = 'V' )
  86.      ORDER BY f_get_product_name(F.product_id);
  87.  
  88.     -- ambil booking reserved dari sob yang belum di settle
  89.      INSERT INTO temp_trx_of_reserved_stock (
  90.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  91.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_sob_non_settle' AS data_type,
  92.         f_get_product_name(B.product_id), B.product_id, A.doc_no, A.doc_date, B.qty_int AS qty  
  93.      FROM sl_so A
  94.      INNER JOIN sl_so_item B ON A.so_id = B.so_id
  95.      WHERE NOT EXISTS(
  96.          SELECT 1
  97.          FROM sl_so_settlement C
  98.          WHERE A.tenant_id = C.tenant_id
  99.          AND A.so_id = C.ref_id AND A.doc_type_id = C.ref_doc_type_id
  100.          )
  101.      AND A.tenant_id = 10
  102.      AND A.ou_id = 10
  103.      AND A.status_doc IN ('R' , 'I')
  104.      AND A.workflow_status <> 'CHANGES REQUESTED'
  105.      ORDER BY f_get_product_name(B.product_id);
  106.    
  107.     -- ambil booking reserved dari REXO (belum di EXO maupun lagi proses verify)
  108.      INSERT INTO temp_trx_of_reserved_stock (
  109.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  110.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_rexo_non_exo_non_verify' AS data_type,
  111.         f_get_product_name(B.product_id), B.product_id, A.doc_no, A.doc_date, B.qty
  112.      FROM in_in_out_exchange A
  113.      INNER JOIN in_in_out_exchange_item B ON A.in_out_exchange_id = B.in_out_exchange_id
  114.      INNER JOIN in_in_out_exchange_status C ON C.in_out_exchange_id = A.in_out_exchange_id
  115.      WHERE A. tenant_id = 10
  116.      AND A.ou_id = 10
  117.      AND A.doc_type_id = 599
  118.      AND C.status_exchange_out NOT IN ('V', 'Y')
  119.      ORDER BY f_get_product_name(B.product_id);
  120.    
  121.     -- Ambil bookingan reserved dari verifikasi EXO (sudah diverify /scan lengkap dan belum sampai di EXO)
  122.      INSERT INTO temp_trx_of_reserved_stock (
  123.         session_id, data_type, product_name, product_id, doc_no, doc_date, qty)
  124.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_verify_non_exo' AS data_type,
  125.         f_get_product_name(B.product_id) AS product, B.product_id, A.doc_no, A.doc_date, B.qty_verify
  126.      FROM in_verification_header A
  127.      INNER JOIN in_verification_item B ON A.verification_id = B.verification_id
  128.      INNER JOIN in_in_out_exchange X ON X.in_out_exchange_id = A.doc_id AND X.doc_type_id = A.doc_type_id
  129.      WHERE A.tenant_id = 10
  130.      AND B.qty_verify > 0
  131.      AND X.ou_id = 10
  132.      AND X.doc_type_id = 599
  133.      AND NOT EXISTS (SELECT 1 FROM in_in_out_exchange D
  134.                     WHERE A.doc_id = D.req_id AND A.doc_type_id = D.req_doc_type_id AND D.doc_type_id = 565 AND D.status_doc = 'R' )
  135.      AND EXISTS (SELECT 1 FROM in_in_out_exchange_status E
  136.                     WHERE X.in_out_exchange_id = E.in_out_exchange_id AND E.status_exchange_out = 'V' )
  137.      UNION ALL
  138.      SELECT 'RECALCULATE-RESERVEDSTOCK' AS session_id, 'qty_verify_non_exo' AS data_type,
  139.         f_get_product_name(B.product_id) AS product, B.product_id, A.ref_doc_no AS doc_no, A.ref_doc_date AS doc_date,
  140.      B.qty_current_scan  AS qty_verify
  141.      FROM in_scanner_header A
  142.      INNER JOIN in_scanner_item B ON A.scanner_header_id = B.scanner_header_id
  143.      INNER JOIN in_in_out_exchange X ON X.in_out_exchange_id = A.ref_doc_id AND X.doc_type_id = A.ref_doc_type_id
  144.      WHERE A.tenant_id = 10
  145.      AND B.qty_current_scan > 0AND B.scan_status = 'SL'
  146.      AND X.ou_id = 10
  147.      AND X.doc_type_id = 599
  148.      AND NOT EXISTS (SELECT 1 FROM in_in_out_exchange D
  149.                     WHERE A.doc_id = D.req_id AND A.doc_type_id = D.req_doc_type_id AND D.doc_type_id = 565 AND D.status_doc = 'R' )
  150.      AND EXISTS (SELECT 1 FROM in_in_out_exchange_status E
  151.                     WHERE X.in_out_exchange_id = E.in_out_exchange_id AND E.status_exchange_out = 'V' )
  152.      ORDER BY product;
  153.      
  154.      -- Apakah ada qty reserved stock yang tidak sesuai
  155.      -- ( qty in_product_balance_stock_reserved vs qty transaksi )
  156.      WITH stock_reserved_in_trx AS (
  157.         SELECT product_id, SUM(qty) AS qty
  158.         FROM temp_trx_of_reserved_stock
  159.         WHERE session_id = 'RECALCULATE-RESERVEDSTOCK'
  160.         GROUP BY product_id
  161.      )
  162.      SELECT A.product_id, C.product_code, C.product_name,
  163.         A.qty AS qty_reserved_stock, COALESCE(B.qty, 0) AS qty_trx,
  164.         (A.qty - COALESCE(B.qty, 0)) AS qty_diff
  165.      FROM in_product_balance_stock_reserved A
  166.      INNER JOIN m_product C ON A.product_id = C.product_id
  167.      LEFT JOIN stock_reserved_in_trx B ON A.product_id = B.product_id
  168.      WHERE (A.qty - COALESCE(B.qty, 0)) <> 0
  169.      ORDER BY C.product_name;
  170.      
  171.      -- backup data product balance stock reserved sebelum diupdate
  172.      CREATE TABLE in_product_balance_stock_reserved_20210528 AS
  173.         SELECT * FROM in_product_balance_stock_reserved;
  174.        
  175.      -- Recalculate qty reserved stock berdasarkan total qty transaksi
  176.      -- hanya untuk product yg qty reserved stock nya tidak sesuai
  177.      BEGIN;
  178.          WITH data_temp_reserved_stock AS (
  179.             SELECT A.product_id, A.qty AS qty_reserved_stock, COALESCE(B.qty_trx, 0) AS qty_trx
  180.             FROM in_product_balance_stock_reserved A
  181.             LEFT JOIN (  
  182.                 SELECT product_id, SUM(qty) AS qty_trx
  183.                 FROM temp_trx_of_reserved_stock
  184.                 WHERE session_id = 'RECALCULATE-RESERVEDSTOCK'
  185.                 GROUP BY product_id
  186.             ) B ON A.product_id = B.product_id
  187.             WHERE (A.qty - COALESCE(B.qty_trx, 0)) <> 0
  188.          )
  189.          UPDATE in_product_balance_stock_reserved A
  190.          SET qty = B.qty_trx,
  191.              version = A.version+1,
  192.              update_user_id = -2,
  193.              update_datetime = TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMMDDHH24MISS')
  194.          FROM data_temp_reserved_stock B
  195.          WHERE A.product_id = B.product_id;
  196.      --ROLLBACK;
  197.      END;
  198.      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement