Advertisement
aadddrr

Untitled

Aug 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Modified by Adrian, Aug 1, 2017
  3.  * monthly_price_amount diambil per year-month
  4.  */
  5.  
  6. CREATE OR REPLACE FUNCTION r_detail_analysis_sales_gross_profit(character varying, bigint, bigint, bigint, character varying,
  7. bigint, character varying, character varying, character varying)
  8.   RETURNS SETOF refcursor AS
  9. $BODY$
  10. DECLARE
  11.     pRefHeader              REFCURSOR := 'refHeader';
  12.     pRefDetail              REFCURSOR := 'refDetail';
  13.     pSessionId              ALIAS FOR $1;
  14.     pTenantId               ALIAS FOR $2;
  15.     pOuId                   ALIAS FOR $3;
  16.     pUserId                 ALIAS FOR $4;
  17.     pDatetime               ALIAS FOR $5;
  18.  
  19.     pPartnerId              ALIAS FOR $6;
  20.     pDateFrom               ALIAS FOR $7;
  21.     pDateTo                 ALIAS FOR $8;
  22.     pProductCodeName            ALIAS FOR $9;
  23.    
  24.        
  25.     vEmptyValue             character varying(1);
  26.     vAwalTahun              character varying(6);
  27.     vAkhirRekap             character varying(6);
  28.     vAllId                  bigint;
  29.     vEmptyId                bigint;
  30.     vRoundingMode               character varying(5);
  31.     vValutaBuku             character varying(5);
  32.     vYearMonth              character varying(6);
  33.     vFilterPartner              text := '';
  34.    
  35.     vCurrentYearMonth       character varying(6);
  36.     vEndYearMonth           character varying(6);
  37.  
  38.     /**
  39.      * 1. ambil semua so balance invoice di main business unit yang tidak di do receipt
  40.      * jika transaksi itu ada item assembly ambil berdasarkan core product id nya
  41.      * ambil juga harga beli terakhir dari item itu sesuai dengan tahun bulan
  42.      * 2. isi table report
  43.      * *belum selesai ini coi*   
  44.      */
  45.    
  46. BEGIN
  47.    
  48.     vEmptyValue := '';
  49.     vAllId := -99;
  50.     vEmptyId := -99;
  51.     vValutaBuku := 'IDR';
  52.    
  53.     SELECT f_get_value_system_config_by_param_code(pTenantId, 'rounding.mode.non.tax') INTO vRoundingMode;
  54.    
  55.     SELECT TO_CHAR(now(), 'YYYYMM') INTO vYearMonth;
  56.    
  57.     SELECT f_get_value_system_config_by_param_code(pTenantId, 'ValutaBuku') INTO vValutaBuku;
  58.    
  59.     DELETE FROM tr_detail_sales_gross_profit WHERE session_id = pSessionId;
  60.     DELETE FROM tt_report_detail_sales_gross_profit WHERE session_id = pSessionId;
  61.     DELETE FROM tt_input_data WHERE session_id = pSessionId;
  62.     DELETE FROM tt_output_data WHERE session_id = pSessionId;
  63.     DELETE FROM tt_in_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  64.     DELETE FROM tt_out_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  65.    
  66.     IF (pPartnerId <> vEmptyId) THEN
  67.         vFilterPartner := ' AND A.partner_id = ' || pPartnerId;
  68.     END IF;
  69.  
  70.     /**
  71.      * 1. ambil semua so balance invoice di main business unit yang tidak di do receipt
  72.      * jika transaksi itu ada item assembly ambil berdasarkan core product id nya
  73.      * ambil juga harga beli terakhir dari item itu sesuai dengan tahun bulan    
  74.      */
  75.     EXECUTE 'INSERT INTO tr_detail_sales_gross_profit(
  76.            session_id, tenant_id, ou_id, year_month_date, partner_id, partner_code, partner_name,
  77.            doc_type_id, doc_no, doc_date,
  78.            product_id, curr_code, qty, gross_sell_price, gross_item_amount,
  79.            monthly_price_curr_code, monthly_price_amount
  80.     )
  81.     SELECT $1, A.tenant_id, E.ou_bu_id, SUBSTRING(A.ref_doc_date, 1, 6), A.partner_id, f_get_partner_code(A.partner_id), f_get_partner_name(A.partner_id),
  82.            A.ref_doc_type_id, A.ref_doc_no, A.ref_doc_date,
  83.            B.product_id, A.curr_code, A.qty_dlv_so, (A.price_so + COALESCE(D.tax_price, 0)),
  84.            SUM(A.item_amount + f_get_so_balance_invoice_tax_amount_by_unique(A.tenant_id, A.ou_id, A.partner_id, A.ref_doc_type_id, A.ref_id, A.ref_item_id, A.do_receipt_item_id)),
  85.            $7, 0
  86.     FROM sl_so_balance_invoice A
  87.     INNER JOIN sl_do_item B ON A.ref_item_id = B.do_item_id
  88.     INNER JOIN sl_do C ON A.ref_doc_type_id = C.doc_type_id AND C.do_id = A.ref_id AND C.do_id = B.do_id
  89.     INNER JOIN sl_so_item D ON B.ref_id = D.so_item_id
  90.     INNER JOIN m_ou_structure E ON A.ou_id = E.ou_id
  91.     INNER JOIN m_product G ON B.tenant_id = G.tenant_id AND B.product_id = G.product_id
  92.     WHERE A.tenant_id = $2
  93.     AND A.ref_doc_date BETWEEN $3 AND $4
  94.     AND E.ou_bu_id = $6 '|| vFilterPartner ||
  95.     ' GROUP BY A.tenant_id, E.ou_bu_id, A.ref_doc_date, A.partner_id, A.ref_doc_type_id, A.ref_doc_no, B.product_id, A.curr_code, A.qty_dlv_so, A.price_so, D.tax_price '
  96.     USING pSessionId, pTenantId, pDateFrom, pDateTo, vEmptyId, pOuId, vEmptyValue;
  97.    
  98.     ANALYZE tr_detail_sales_gross_profit;
  99.    
  100.     /* v329 data yang diambil dari pu_monthly_price_product diubah
  101.      * menjadi ambil dari table  
  102.      * tt_out_latest_purchasing_price_by_date menggunakan
  103.      * pu_get_latest_purchasing_price_by_date yang sebelumnya datanya dimasukkan ke
  104.      * tt_in_latest_purchasing_price_by_date */
  105.    
  106.     SELECT MIN(year_month_date), MAX(year_month_date) FROM tr_detail_sales_gross_profit INTO vCurrentYearMonth, vEndYearMonth;
  107.    
  108.     --Modified by Adrian, Aug 1, 2017
  109.     --monthly_price_amount di-update per year-month
  110.     WHILE vCurrentYearMonth <= vEndYearMonth LOOP
  111.    
  112.         DELETE FROM tt_in_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  113.         DELETE FROM tt_out_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  114.    
  115.         INSERT INTO tt_in_latest_purchasing_price_by_date(session_id, tenant_id, ou_bu_id, doc_date, product_id)
  116.             SELECT pSessionId, pTenantId, B.ou_bu_id, A.doc_date, A.product_id  
  117.         FROM tr_detail_sales_gross_profit A
  118.             INNER JOIN m_ou_structure B ON A.ou_id = B.ou_id
  119.         WHERE A.tenant_id = pTenantId
  120.             AND A.session_id = pSessionId
  121.             AND A.year_month_date = vCurrentYearMonth
  122.         GROUP BY B.ou_bu_id, A.doc_date, A.product_id;
  123.            
  124.         PERFORM pu_get_latest_purchasing_price_by_date(pSessionId, pTenantId, vYearMonth, pOuId, pDatetime, pUserId);
  125.        
  126.         /*sebelum v329
  127.         UPDATE tr_detail_sales_gross_profit
  128.         --SET monthly_price_curr_code = A.curr_code, monthly_price_amount = ROUND(A.amount / A.qty)
  129.         SET monthly_price_curr_code = COALESCE(A.curr_code, tr_detail_sales_gross_profit.curr_code), monthly_price_amount = COALESCE(f_get_gross_price_from_nett_amount(A.amount, A.qty, A.flg_tax_amount, A.tax_percentage, vRoundingMode, f_get_digit_decimal_doc_curr(-99, A.curr_code)), 0)
  130.         FROM pu_monthly_price_product A
  131.         WHERE tr_detail_sales_gross_profit.session_id = pSessionId
  132.         AND tr_detail_sales_gross_profit.product_id = A.product_id
  133.         AND tr_detail_sales_gross_profit.year_month_date = A.year_month_date  
  134.         AND tr_detail_sales_gross_profit.ou_id = A.ou_id;*/
  135.        
  136.         ANALYZE tt_out_latest_purchasing_price_by_date;
  137.        
  138.         UPDATE tr_detail_sales_gross_profit B
  139.         --SET monthly_price_curr_code = A.curr_code, monthly_price_amount = ROUND(A.amount / A.qty)
  140.         SET monthly_price_curr_code = COALESCE(A.gl_purch_curr_code, B.curr_code), monthly_price_amount = COALESCE(A.gl_purch_gross_price, 0)
  141.         FROM tt_out_latest_purchasing_price_by_date A
  142.         INNER JOIN m_ou_structure C ON C.ou_bu_id = A.ou_bu_id
  143.         WHERE B.session_id = A.session_id
  144.         AND B.product_id = A.product_id
  145.         AND B.tenant_id = A.tenant_id
  146.         AND B.doc_date = A.doc_date  
  147.         AND B.year_month_date = vCurrentYearMonth
  148.         AND C.ou_id = B.ou_id;
  149.        
  150.         SELECT TO_CHAR(TO_DATE(vCurrentYearMonth,'YYYYMM') + interval '1 Month','YYYYMM') INTO vCurrentYearMonth;
  151.        
  152.         DELETE FROM tt_in_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  153.         DELETE FROM tt_out_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  154.        
  155.     END LOOP;
  156.    
  157.     ANALYZE tr_detail_sales_gross_profit;
  158.    
  159.     /*
  160.      * update nilai harga beli terakhir menggunakan valuta penjualan (gunakan kurs komersial)
  161.      
  162.     UPDATE tr_detail_sales_gross_profit SET monthly_price_amount = monthly_price_amount * f_commercial_rate(tenant_id, doc_date, monthly_price_curr_code, vValutaBuku), monthly_price_curr_code = vValutaBuku
  163.     WHERE monthly_price_curr_code <> vValutaBuku
  164.     AND session_id = pSessionId AND monthly_price_curr_code <> '';*/
  165.    
  166.     UPDATE tr_detail_sales_gross_profit SET gross_item_amount = gross_item_amount * f_commercial_rate(tenant_id, doc_date, curr_code, vValutaBuku), curr_code = vValutaBuku
  167.     WHERE curr_code <> vValutaBuku
  168.     AND session_id = pSessionId AND curr_code <> '';
  169.    
  170.     ANALYZE tr_detail_sales_gross_profit;
  171.    
  172.     /*
  173.      * pindahkan data tahun bulan yang diminta (sudah direkap per partner)
  174.      */
  175.     INSERT INTO tr_current_sales_gross_profit (
  176.         session_id, tenant_id, ou_id, product_id, partner_id, partner_code, partner_name, curr_code,
  177.         current_sales_amount, current_monthly_price_amount, current_gross_profit, qty
  178.     )
  179.     SELECT pSessionId, tenant_id, ou_id, product_id--, partner_id, partner_code, partner_name
  180.     , -99, '', ''
  181.     , curr_code,
  182.            SUM(gross_item_amount) AS current_sales_amount, SUM(monthly_price_amount * qty) AS current_monthly_price_amount, 0, SUM(qty)
  183.            --gross_item_amount AS current_sales_amount, monthly_price_amount * qty AS current_monthly_price_amount, 0, qty
  184.     FROM tr_detail_sales_gross_profit
  185.     WHERE session_id = pSessionId
  186.     --AND year_month_date = vYearMonth
  187.     AND doc_date BETWEEN pDateFrom AND pDateTo
  188.     GROUP BY tenant_id, ou_id, product_id--, partner_id, partner_code, partner_name
  189.     , curr_code;
  190.    
  191.     ANALYZE tr_current_sales_gross_profit;
  192.    
  193.     /*
  194.      * update data gross profit untuk bulan yang diminta
  195.      */
  196.     UPDATE tr_current_sales_gross_profit SET current_gross_profit = current_sales_amount - current_monthly_price_amount  
  197.     WHERE session_id = pSessionId;
  198.  
  199.     ANALYZE tr_detail_sales_gross_profit;
  200.  
  201.     /**
  202.     * isi table report
  203.     */
  204.     INSERT INTO tt_report_detail_sales_gross_profit(session_id, tenant_id,
  205.            ou_id, partner_id, partner_code, partner_name,
  206.            product_id, curr_code, gross_item_amount, margin, percentage)
  207.     SELECT A.session_id, A.tenant_id,
  208.         A.ou_id, A.partner_id, A.partner_code, A.partner_name,
  209.         A.product_id, A.curr_code, SUM(A.gross_item_amount), 0, 0
  210.     FROM tr_detail_sales_gross_profit A
  211.     WHERE A.session_id = pSessionId
  212.     GROUP BY A.session_id, A.tenant_id, A.ou_id, A.product_id, A.partner_id, A.partner_code, A.partner_name, A.curr_code;
  213.    
  214.     ANALYZE tt_report_detail_sales_gross_profit;
  215.    
  216.     /*
  217.      * return header
  218.      */
  219.     Open pRefHeader FOR
  220.     SELECT fullName AS full_name, f_get_ou_name(pOuId) AS ou_name,
  221.             CASE WHEN pPartnerId = vEmptyId THEN '(All)' ELSE f_get_partner_name(pPartnerId) END AS partner_name,
  222.             CASE WHEN pProductCodeName = vEmptyValue THEN '(All)' ELSE ('%'||pProductCodeName||'%') END AS product_code_name,
  223.             pDateFrom AS date_from, pDateTo AS date_to,  
  224.             f_get_username(pUserId) AS username, pDatetime AS datetime
  225.         FROM t_user
  226.         WHERE user_id = pUserId;
  227.    
  228.     RETURN NEXT pRefHeader;
  229.    
  230.     ANALYZE tr_current_sales_gross_profit;
  231.    
  232.     INSERT INTO tt_input_data
  233.     (session_id, id_data, data_group_1,
  234.     data_group_2, data_group_3, data_group_4, data_group_5, data_measure)
  235.     SELECT pSessionId, 'SALDO', A.curr_code,
  236.         A.qty::text, B.product_id::text, A.current_sales_amount::text, A.current_gross_profit::text, A.current_gross_profit::numeric
  237.     FROM tr_current_sales_gross_profit A
  238.     INNER JOIN m_product B ON A.product_id = B.product_id
  239.     WHERE A.session_id = pSessionId;
  240.  
  241.     /*INSERT INTO tt_input_data
  242.     (session_id, id_data, data_group_1,
  243.     data_group_2, data_group_3, data_group_4, data_group_5, data_measure)
  244.     SELECT pSessionId, 'SALDO', A.curr_code,
  245.         A.qty::text, B.product_id::text, A.gross_item_amount::text, (A.gross_item_amount - (A.monthly_price_amount * A.qty))::text, (A.gross_item_amount - (A.monthly_price_amount * A.qty))::numeric
  246.     FROM tr_detail_sales_gross_profit A
  247.     INNER JOIN m_product B ON A.product_id = B.product_id
  248.     WHERE A.session_id = pSessionId;*/
  249.    
  250.     PERFORM f_agregate_percentage(pSessionId, 'SALDO', ';', 3, 1, 2);
  251.  
  252.     IF TRIM(pProductCodeName) <> vEmptyValue THEN
  253.         Open pRefDetail FOR
  254.         SELECT A.data_group_1 AS curr_code,
  255.         A.data_group_2::numeric AS qty,
  256.         f_get_product_code(A.data_group_3::bigint) AS product_code,
  257.         f_get_product_name(A.data_group_3::bigint) AS product_name,
  258.         A.data_group_4::numeric AS sell_amount,
  259.         A.data_group_5::numeric AS margin,
  260.         round(A.percentage_measure, 2) AS percentage
  261.         FROM tt_output_data A
  262.         WHERE A.session_id = pSessionId
  263.         AND UPPER(A.data_group_2) LIKE '%' || UPPER(pProductCodeName) || '%' OR UPPER(A.data_group_3) LIKE '%' || UPPER(pProductCodeName) || '%'
  264.         ORDER BY curr_code, percentage, sell_amount, margin, product_code;
  265.     ELSE
  266.         Open pRefDetail FOR
  267.         SELECT A.data_group_1 AS curr_code,
  268.         A.data_group_2::numeric AS qty,
  269.         f_get_product_code(A.data_group_3::bigint) AS product_code,
  270.         f_get_product_name(A.data_group_3::bigint) AS product_name,
  271.         A.data_group_4::numeric AS sell_amount,
  272.         A.data_group_5::numeric AS margin,
  273.         round(A.percentage_measure, 2) AS percentage
  274.         FROM tt_output_data A
  275.         WHERE A.session_id = pSessionId
  276.         ORDER BY curr_code, percentage, sell_amount, margin, product_code;
  277.     END IF;
  278.    
  279.     RETURN NEXT pRefDetail;
  280.  
  281.     --DROP TABLE tt_output_data_detail_201706_x;
  282.     --CREATE TABLE tt_output_data_detail_201706_x AS SELECT * FROM tt_output_data;
  283.  
  284.     DELETE FROM tr_detail_sales_gross_profit WHERE session_id = pSessionId;
  285.     DELETE FROM tt_report_detail_sales_gross_profit WHERE session_id = pSessionId;
  286.     DELETE FROM tt_input_data WHERE session_id = pSessionId;
  287.     DELETE FROM tt_output_data WHERE session_id = pSessionId;
  288.     DELETE FROM tr_current_sales_gross_profit WHERE session_id = pSessionId;
  289.     DELETE FROM tt_in_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  290.     DELETE FROM tt_out_latest_purchasing_price_by_date WHERE session_id = pSessionId;
  291.    
  292. END;
  293. $BODY$
  294.   LANGUAGE plpgsql VOLATILE
  295.   COST 100
  296.   ROWS 1000;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement