Advertisement
aadddrr

pu_temp_summary_purch_20170809_3

Aug 8th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Modified by Adrian, Jul 31, 2017, mengubah order pada summary_monthly_purchasing_temp menjadi ORDER BY C.ref_doc_date DESC, C.create_datetime DESC
  2. --meng-comment kondisi AND A.item_amount > 0
  3. --po_balance_invoice_id diambil dari produk paling awal dalam PO terakhir per tanggal
  4.  
  5. CREATE OR REPLACE FUNCTION pu_temp_summary_purch(bigint, character varying, bigint, character varying, character varying, bigint)
  6.   RETURNS void AS
  7. $BODY$
  8. DECLARE
  9.     pTenantId           ALIAS FOR $1;
  10.     pSessionId          ALIAS FOR $2;
  11.     pOuId               ALIAS FOR $3;
  12.     pYearMonth          ALIAS FOR $4;
  13.     pDatetime           ALIAS FOR $5;
  14.     pUserId             ALIAS FOR $6;
  15.  
  16.     vReceiveGoodsDocTypeId  bigint;
  17.     vPrevYearMonth      character varying(6);
  18.     vDefaultCurrCode    character varying(10);
  19.     vDefaultPrice       numeric;
  20.     vDefaultGlCurrCode  character varying(100);
  21.     vRoundingMode       character varying(10);
  22. BEGIN
  23.    
  24.     vReceiveGoodsDocTypeId := 111;
  25.     vDefaultCurrCode := '-99';
  26.     vDefaultPrice := 0;
  27.    
  28.     vDefaultGlCurrCode := f_get_value_system_config_by_param_code(pTenantId,'ValutaBuku');
  29.     vRoundingMode := f_get_value_system_config_by_param_code(pTenantId, 'rounding.mode.non.tax');
  30.    
  31.     SELECT TO_CHAR(TO_DATE(pYearMonth,'YYYYMM') - interval '1 Month','YYYYMM') INTO vPrevYearMonth;
  32.    
  33.     INSERT INTO pu_temp_summary_monthly_latest_purchasing
  34.         (session_id, tenant_id, ou_id, year_month_date,
  35.          product_id, curr_code, nett_price, gross_price,
  36.          gl_curr_code, gl_nett_price, gl_gross_price,
  37.          ref_doc_type_id, ref_id, ref_doc_no, ref_doc_date,
  38.          version, create_datetime, create_user_id, update_datetime, update_user_id)
  39.     SELECT pSessionId, tenant_id, ou_id, pYearMonth,
  40.            product_id, curr_code, nett_price, gross_price,
  41.            gl_curr_code, gl_nett_price, gl_gross_price,
  42.            ref_doc_type_id, ref_id, ref_doc_no, ref_doc_date,
  43.            0, pDatetime, pUserId, pDatetime, pUserId
  44.     FROM pu_temp_summary_monthly_latest_purchasing
  45.     WHERE ou_id = pOuId
  46.     AND tenant_id = pTenantId
  47.     AND year_month_date = vPrevYearMonth;
  48.    
  49.     ANALYZE pu_temp_summary_monthly_latest_purchasing;
  50.    
  51.     INSERT INTO pu_temp_summary_monthly_latest_purchasing
  52.         (session_id, tenant_id, ou_id, year_month_date,
  53.          product_id, curr_code, nett_price, gross_price,
  54.          gl_curr_code, gl_nett_price, gl_gross_price,
  55.          ref_doc_type_id, ref_id, ref_doc_no, ref_doc_date,
  56.          version, create_datetime, create_user_id, update_datetime, update_user_id)
  57.     SELECT pSessionId, A.tenant_id, pOuId, pYearMonth,
  58.            A.product_id, vDefaultCurrCode, vDefaultPrice, vDefaultPrice,
  59.            vDefaultGlCurrCode, vDefaultPrice, vDefaultPrice,
  60.            -99, -99, ' ', ' ',
  61.            0, pDatetime, pUserId, pDatetime, pUserId
  62.     FROM m_product A
  63.     WHERE A.tenant_id = pTenantId
  64.     AND NOT EXISTS (
  65.         SELECT 1 FROM pu_temp_summary_monthly_latest_purchasing B
  66.         WHERE B.ou_id = pOuId
  67.         AND B.tenant_id = pTenantId
  68.         AND B.year_month_date = pYearMonth
  69.         AND B.product_id = A.product_id
  70.     )
  71.     AND NOT EXISTS (
  72.         SELECT 1 FROM m_ext_product C
  73.         WHERE C.product_id = A.product_id
  74.     );
  75.    
  76.     ANALYZE pu_temp_summary_monthly_latest_purchasing;
  77.    
  78.     WITH summary_monthly_purchasing_temp AS (
  79.         SELECT A.tenant_id, A.ou_id, F.year_month_date,
  80.            B.product_id, A.curr_code AS curr_code,
  81.            CASE WHEN vRoundingMode = 'RD'
  82.             THEN TRUNC(SUM(A.item_amount) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, A.curr_code))
  83.             ELSE ROUND(SUM(A.item_amount) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, A.curr_code))
  84.            END AS nett_price,
  85.            CASE WHEN vRoundingMode = 'RD'
  86.             THEN TRUNC(SUM(A.item_amount + COALESCE(G.tax_amount, 0)) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, A.curr_code))
  87.             ELSE ROUND(SUM(A.item_amount + COALESCE(G.tax_amount, 0)) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, A.curr_code))
  88.            END AS gross_price,
  89.            CASE WHEN vRoundingMode = 'RD'
  90.             THEN TRUNC(SUM(A.item_amount) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, vDefaultGlCurrCode)) * f_commercial_rate(pTenantId, A.ref_doc_date, A.curr_code, vDefaultGlCurrCode)
  91.             ELSE ROUND(SUM(A.item_amount) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, vDefaultGlCurrCode)) * f_commercial_rate(pTenantId, A.ref_doc_date, A.curr_code, vDefaultGlCurrCode)
  92.            END AS gl_nett_price,
  93.            CASE WHEN vRoundingMode = 'RD'
  94.             THEN TRUNC(SUM(A.item_amount + COALESCE(G.tax_amount, 0)) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, vDefaultGlCurrCode)) * f_commercial_rate(pTenantId, A.ref_doc_date, A.curr_code, vDefaultGlCurrCode)
  95.             ELSE ROUND(SUM(A.item_amount + COALESCE(G.tax_amount, 0)) / SUM(A.qty_rcv_po), f_get_digit_decimal_doc_curr(-99, vDefaultGlCurrCode)) * f_commercial_rate(pTenantId, A.ref_doc_date, A.curr_code, vDefaultGlCurrCode)
  96.             END AS gl_gross_price,
  97.            A.ref_doc_type_id, A.ref_id, A.ref_doc_no, A.ref_doc_date
  98.         FROM pu_po_balance_invoice A
  99.         INNER JOIN pu_receive_goods_item B ON A.ref_item_id = B.receive_goods_item_id
  100.         INNER JOIN dt_date F ON A.ref_doc_date = F.string_date
  101.         LEFT OUTER JOIN pu_po_balance_invoice_tax G
  102.             ON A.tenant_id = G.tenant_id
  103.             AND A.ou_id = G.ou_id
  104.             AND A.partner_id = G.partner_id
  105.             AND A.ref_doc_type_id = G.ref_doc_type_id
  106.             AND A.ref_id = G.ref_id
  107.             AND A.ref_item_id = G.ref_item_id
  108.         WHERE A.ref_doc_type_id = vReceiveGoodsDocTypeId
  109.         AND A.item_amount > 0
  110.         AND F.year_month_date = pYearMonth
  111.         AND A.ou_id = pOuId
  112.         AND A.tenant_id = pTenantId
  113.         AND A.po_balance_invoice_id = (
  114.             SELECT C.po_balance_invoice_id
  115.             FROM pu_po_balance_invoice C
  116.             INNER JOIN pu_receive_goods_item D ON C.ref_item_id = D.receive_goods_item_id
  117.             INNER JOIN dt_date E ON C.ref_doc_date = E.string_date
  118.             INNER JOIN pu_po_item F ON D.ref_id = F.po_item_id
  119.             WHERE D.product_id = B.product_id
  120.             --AND E.year_month_date = F.year_month_date
  121.             AND C.ref_doc_date <= A.ref_doc_date
  122.             AND C.ou_id = A.ou_id
  123.             AND C.ref_doc_type_id = vReceiveGoodsDocTypeId
  124.             AND C.tenant_id = pTenantId
  125.             ORDER BY C.ref_doc_date DESC, C.create_datetime DESC, F.line_no ASC
  126.             LIMIT 1
  127.         )
  128.         GROUP BY A.tenant_id, A.ou_id, F.year_month_date, B.product_id, A.curr_code, A.ref_doc_type_id, A.ref_id, A.ref_doc_no, A.ref_doc_date
  129.     )
  130.     UPDATE pu_temp_summary_monthly_latest_purchasing Z
  131.     SET curr_code = Y.curr_code, nett_price = Y.nett_price, gross_price = Y.gross_price,
  132.         gl_curr_code = vDefaultGlCurrCode, gl_nett_price = Y.gl_nett_price, gl_gross_price = Y.gl_gross_price,
  133.         ref_doc_type_id = Y.ref_doc_type_id, ref_id = Y.ref_id, ref_doc_no = Y.ref_doc_no, ref_doc_date = Y.ref_doc_date,
  134.         version = Z.version + 1, update_datetime = pDatetime, update_user_id = pUserId
  135.     FROM summary_monthly_purchasing_temp Y
  136.     WHERE Y.tenant_id = Z.tenant_id
  137.     AND Y.year_month_date = Z.year_month_date
  138.     AND Y.product_id = Z.product_id
  139.     AND Y.ou_id = Z.ou_id
  140.     AND Z.session_id = pSessionId;
  141.    
  142.     ANALYZE pu_temp_summary_monthly_latest_purchasing;
  143.    
  144.     DELETE FROM pu_temp_summary_monthly_latest_purchasing A
  145.     WHERE A.tenant_id = pTenantId
  146.     AND A.year_month_date = pYearMonth
  147.     AND A.ou_id = pOuId
  148.     AND A.curr_code = vDefaultCurrCode
  149.     AND A.session_id = pSessionId;
  150.    
  151. END;
  152. $BODY$
  153.   LANGUAGE plpgsql VOLATILE
  154.   COST 100;
  155.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement