Advertisement
widana

Laporan Rekap Stock

Jul 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION rpt.r_report_rekap_stock(character varying, BIGINT, BIGINT, character varying)
  2.   RETURNS SETOF refcursor AS
  3. $BODY$
  4. DECLARE
  5.     pRefHeader                  REFCURSOR := 'refHeader';
  6.     pRefDetail                  REFCURSOR := 'refDetail';
  7.    
  8.     pSessionId                  ALIAS FOR $1;
  9.     pRecordOwnerId              ALIAS FOR $2;
  10.     pProductId                  ALIAS FOR $3;
  11.     pDate                       ALIAS FOR $4;
  12.    
  13.     vEmptyId                    bigint := -99;
  14.     vReceiveGoodsDocTypeId      bigint := 111;
  15.     vPosDocTypeId               bigint := 390;
  16.     vAdjDocTypeId               bigint := 521;
  17.     vReturnNoteDocTypeId        bigint := 502;
  18.  
  19.     vEmpty                      character varying := '';
  20.     vNo                         character varying := 'N';
  21.     vYes                        character varying := 'Y';
  22.    
  23.     vYearMonth                  character varying := '';
  24.     vFilerProduct               character varying := '';
  25.  
  26. BEGIN
  27.    
  28.     DELETE FROM tt_rekap_saldo_qty WHERE session_id = pSessionId;
  29.    
  30.     SELECT substring(pDate, 1, 6) INTO vYearMonth;
  31.    
  32.     OPEN pRefHeader FOR
  33.         SELECT f_get_record_owner_name(pRecordOwnerId) AS record_owner_name, CASE WHEN pProductId <> vEmptyId
  34.             THEN concat(f_get_product_code(pProductId), ' - ', f_get_product_name(pProductId)) ELSE 'ALL' END AS product,
  35.             pDate AS date;
  36.     RETURN NEXT pRefHeader;
  37.    
  38.     IF pProductId <> vEmptyId THEN
  39.         vFilerProduct := ' AND A.product_id =  '''|| pProductId ||'''';
  40.     END IF;
  41.    
  42.     -- Init Saldo Awal Qty
  43.     EXECUTE '
  44.         INSERT INTO tt_rekap_saldo_qty (
  45.             session_id, record_owner_id, doc_type_id, product_id,
  46.             init_qty, buy_qty, sell_qty, adj_qty, return_qty
  47.         )
  48.         SELECT $1, A.record_owner_id, $2, A.product_id,
  49.             A.qty, 0, 0, 0, 0
  50.         FROM in_summary_monthly_qty A
  51.         WHERE A.year_month <= $3
  52.             ' || vFilerProduct ||'
  53.             AND A.record_owner_id = $4'
  54.     USING pSessionId, vEmptyId, vYearMonth, pRecordOwnerId;
  55.        
  56.     -- Init Qty Beli
  57.     EXECUTE '
  58.         INSERT INTO tt_rekap_saldo_qty (
  59.             session_id, record_owner_id, doc_type_id, product_id,
  60.             init_qty, buy_qty, sell_qty, adj_qty, return_qty
  61.         )
  62.         SELECT $1, A.record_owner_id, $5, A.product_id,
  63.             0, COALESCE(SUM(qty), 0), 0, 0, 0
  64.         FROM in_log_product_balance_stock A
  65.         WHERE A.record_owner_id = $2
  66.             AND A.ref_doc_date <= $3
  67.             AND A.ref_doc_type_id = $4
  68.             ' || vFilerProduct ||'
  69.         GROUP BY A.record_owner_id, A.product_id'
  70.     USING pSessionId, pRecordOwnerId, pDate, vReceiveGoodsDocTypeId, vEmptyId;
  71.    
  72.     -- Init Qty Penjualan
  73.     EXECUTE '
  74.         INSERT INTO tt_rekap_saldo_qty (
  75.             session_id, record_owner_id, doc_type_id, product_id,
  76.             init_qty, buy_qty, sell_qty, adj_qty, return_qty
  77.         )
  78.         SELECT $1, A.record_owner_id, $5, A.product_id,
  79.             0, 0, COALESCE(SUM(qty), 0), 0, 0
  80.         FROM in_log_product_balance_stock A
  81.         WHERE A.record_owner_id = $2
  82.             AND A.ref_doc_date <= $3
  83.             AND A.ref_doc_type_id = $4
  84.             ' || vFilerProduct ||'
  85.         GROUP BY A.record_owner_id, A.product_id'
  86.     USING pSessionId, pRecordOwnerId, pDate, vPosDocTypeId, vEmptyId;
  87.  
  88.     -- init Adj Stock
  89.     EXECUTE '
  90.         INSERT INTO tt_rekap_saldo_qty (
  91.             session_id, record_owner_id, doc_type_id, product_id,
  92.             init_qty, buy_qty, sell_qty, adj_qty, return_qty
  93.         )
  94.         SELECT $1, A.record_owner_id, $5, A.product_id,
  95.             0, 0, 0, COALESCE(SUM(qty), 0), 0
  96.         FROM in_log_product_balance_stock A
  97.         WHERE A.record_owner_id = $2
  98.             AND A.ref_doc_date <= $3
  99.             AND A.ref_doc_type_id = $4
  100.             ' || vFilerProduct ||'
  101.         GROUP BY A.record_owner_id, A.product_id'
  102.     USING pSessionId, pRecordOwnerId, pDate, vAdjDocTypeId, vEmptyId;
  103.    
  104.     -- Init retur Beli
  105.     EXECUTE '
  106.         INSERT INTO tt_rekap_saldo_qty (
  107.             session_id, record_owner_id, doc_type_id, product_id,
  108.             init_qty, buy_qty, sell_qty, adj_qty, return_qty
  109.         )
  110.         SELECT $1, A.record_owner_id, $5, A.product_id,
  111.             0, 0, 0, 0, COALESCE(SUM(qty), 0)
  112.         FROM in_log_product_balance_stock A
  113.         WHERE A.record_owner_id = $2
  114.             AND A.ref_doc_date <= $3
  115.             AND A.ref_doc_type_id = $4
  116.             ' || vFilerProduct ||'
  117.         GROUP BY A.record_owner_id, A.product_id'
  118.     USING pSessionId, pRecordOwnerId, pDate, vReturnNoteDocTypeId, vEmptyId;
  119.    
  120.     OPEN pRefDetail FOR
  121.         SELECT Z.*,  init_qty+buy_qty+sell_qty+adj_qty+return_qty AS final_qty,
  122.             0 AS avg_hpp
  123.         FROM (
  124.             SELECT f_get_product_code(A.product_id) AS product_code, f_get_product_name(A.product_id) AS product_name,
  125.                 COALESCE(SUM(init_qty), 0) AS init_qty, COALESCE(SUM(buy_qty), 0) AS buy_qty,
  126.                 COALESCE(SUM(sell_qty), 0) AS sell_qty, COALESCE(SUM(adj_qty), 0) AS adj_qty,
  127.                 COALESCE(SUM(return_qty), 0) AS return_qty
  128.             FROM tt_rekap_saldo_qty A
  129.             WHERE session_id = pSessionId
  130.             GROUP BY A.product_id
  131.         ) Z
  132.         ORDER BY product_name ASC;
  133.     RETURN NEXT pRefDetail;
  134.    
  135.     DELETE FROM tt_rekap_saldo_qty WHERE session_id = pSessionId;
  136.    
  137. END;
  138. $BODY$
  139.   LANGUAGE plpgsql VOLATILE
  140.   SET search_path = public, mstr, ptg, htg, pb, pj, batch
  141.   COST 100;
  142.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement