Advertisement
aadddrr

pu_temp_summary_purch_20170808

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