aadddrr

r_analysis_sales_nett_profit_with_monthly_average_ctgr_produ

Aug 29th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Adrian, Aug 18, 2017
  3.  * Report Analysis Sales Gross Profit with Monthly Average By Category Product
  4.  * Dibuat berdasarkan Report Analysis Sales Gross Profit,
  5.  * tetapi harga beli diambil dari in_summary_monthly_cogs,
  6.  * harga jual yang diambil adalah harga nett,
  7.  * akhir rekap adalah bulan yang dipilih,
  8.  * semua transaksi diambil dari sl_so_balance_invoice,
  9.  * hanya bisa memilih periode tanpa tanggal awal dan akhir,
  10.  * dan di-group berdasarkan category product
  11.  */
  12.  
  13. CREATE OR REPLACE FUNCTION r_analysis_sales_nett_profit_with_monthly_average_ctgr_product(character varying, bigint, character varying, bigint, bigint, bigint, character varying)
  14.   RETURNS SETOF refcursor AS
  15. $BODY$
  16. DECLARE
  17.     pRefHeader              REFCURSOR := 'refHeader';
  18.     pRefDetail              REFCURSOR := 'refDetail';
  19.     pSessionId              ALIAS FOR $1;
  20.     pTenantId               ALIAS FOR $2;
  21.     pYearMonthDate          ALIAS FOR $3;
  22.     pUserId                 ALIAS FOR $4;
  23.     pCtgrProductId          ALIAS FOR $5;
  24.     pOuId                   ALIAS FOR $6;
  25.     pDatetime               ALIAS FOR $7;
  26.        
  27.     vEmptyValue             character varying(1);
  28.     vAwalTahun              character varying(6);
  29.     vAkhirRekap             character varying(6);
  30.     vAllId                  bigint;
  31.     vEmptyId                bigint;
  32.     vRoundingMode           character varying(5);
  33.     vValutaBuku             character varying(5);
  34.    
  35.     vFilterCtgrProduct      character varying;
  36.    
  37.     vTotalCurrentProfitPercentage   numeric;
  38.     vTotalBeforeProfitPercentage    numeric;
  39.    
  40. BEGIN
  41.    
  42.     vEmptyValue := '';
  43.     vAllId := -99;
  44.     vEmptyId := -99;
  45.     vAwalTahun := SUBSTRING(pYearMonthDate, 1, 4) || '01';
  46.     vValutaBuku := 'IDR';
  47.     vFilterCtgrProduct := ' ';
  48.    
  49.     SELECT f_get_value_system_config_by_param_code(pTenantId, 'rounding.mode.non.tax') INTO vRoundingMode;
  50.    
  51.     SELECT f_get_value_system_config_by_param_code(pTenantId, 'ValutaBuku') INTO vValutaBuku;
  52.    
  53.     vAkhirRekap := pYearMonthDate;
  54.    
  55.     IF pCtgrProductId <> vAllId THEN
  56.         vFilterCtgrProduct = ' AND F.ctgr_product_id = ' || pCtgrProductId || ' ';
  57.     END IF;
  58.    
  59.     DELETE FROM tr_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  60.     DELETE FROM tr_current_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  61.     DELETE FROM tr_rekap_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  62.     DELETE FROM tr_sales_gross_profit_by_ctgr_product_for_output WHERE session_id = pSessionId;
  63.    
  64.     /*
  65.      * ambil semua so balance invoice di main business unit
  66.      * jika transaksi itu ada item assembly ambil berdasarkan core product id nya
  67.      * ambil juga harga beli terakhir dari item itu sesuai dengan tahun bulan    
  68.      */
  69.    
  70.     --DELIVERY ORDER dan DO RECEIPT
  71.     EXECUTE '
  72.         INSERT INTO tr_sales_gross_profit_by_ctgr_product(
  73.                session_id, tenant_id, ou_id, year_month_date, partner_id, partner_code, partner_name,
  74.                ctgr_product_id, ctgr_product_code, ctgr_product_name,
  75.                doc_type_id, doc_no, doc_date,
  76.                product_id, curr_code, qty, nett_sell_price,
  77.                nett_amount_item,
  78.                monthly_price_curr_code, monthly_price_amount
  79.         )
  80.         SELECT $1, A.tenant_id, E.ou_bu_id, SUBSTRING(A.ref_doc_date, 1, 6), $6, $7, $7,
  81.                F.ctgr_product_id, f_get_ctgr_product_code(F.ctgr_product_id), f_get_ctgr_product_name(F.ctgr_product_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),
  84.                SUM(A.item_amount),
  85.                A.curr_code, 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 F ON B.product_id = F.product_id
  92.         WHERE A.tenant_id = $2
  93.         AND SUBSTRING(A.ref_doc_date, 1, 6) BETWEEN $3 AND $4
  94.         AND E.ou_bu_id = $5 '
  95.         || vFilterCtgrProduct ||
  96.         ' GROUP BY A.tenant_id, E.ou_bu_id, A.ref_doc_date, F.ctgr_product_id, A.ref_doc_type_id, A.ref_doc_no, B.product_id,
  97.         A.curr_code, A.qty_dlv_so, A.price_so, D.tax_price '
  98.     USING pSessionId, pTenantId, vAwalTahun, pYearMonthDate, pOuId, vEmptyId, vEmptyValue;
  99.    
  100.     ANALYZE tr_sales_gross_profit_by_ctgr_product;
  101.    
  102.     --RETURN NOTE
  103.     EXECUTE '
  104.         INSERT INTO tr_sales_gross_profit_by_ctgr_product(
  105.                session_id, tenant_id, ou_id, year_month_date, partner_id, partner_code, partner_name,
  106.                ctgr_product_id, ctgr_product_code, ctgr_product_name,
  107.                doc_type_id, doc_no, doc_date,
  108.                product_id, curr_code, qty, nett_sell_price,
  109.                nett_amount_item,
  110.                monthly_price_curr_code, monthly_price_amount
  111.         )
  112.         SELECT $1, A.tenant_id, E.ou_bu_id, SUBSTRING(A.ref_doc_date, 1, 6), $6, $7, $7,
  113.                F.ctgr_product_id, f_get_ctgr_product_code(F.ctgr_product_id), f_get_ctgr_product_name(F.ctgr_product_id),
  114.                A.ref_doc_type_id, A.ref_doc_no, A.ref_doc_date,
  115.                B.product_id, A.curr_code, A.qty_dlv_so * -1, (A.price_so),
  116.                SUM(A.item_amount) * -1,
  117.                A.curr_code, 0
  118.         FROM sl_so_balance_invoice A
  119.         INNER JOIN in_inventory_item B ON A.ref_item_id = B.ref_item_id
  120.         INNER JOIN in_inventory C ON A.ref_doc_type_id = C.doc_type_id AND C.inventory_id = A.ref_id AND C.inventory_id = B.inventory_id
  121.         INNER JOIN in_balance_do_item G ON B.ref_item_id = G.do_item_id
  122.         INNER JOIN sl_so_item D ON G.so_item_id = D.so_item_id
  123.         INNER JOIN m_ou_structure E ON A.ou_id = E.ou_id
  124.         INNER JOIN m_product F ON B.product_id = F.product_id
  125.         WHERE A.tenant_id = $2
  126.         AND SUBSTRING(A.ref_doc_date, 1, 6) BETWEEN $3 AND $4
  127.         AND E.ou_bu_id = $5 '
  128.         || vFilterCtgrProduct ||
  129.         ' GROUP BY A.tenant_id, E.ou_bu_id, A.ref_doc_date, F.ctgr_product_id, A.ref_doc_type_id, A.ref_doc_no, B.product_id,
  130.         A.curr_code, A.qty_dlv_so, A.price_so, D.tax_price '
  131.     USING pSessionId, pTenantId, vAwalTahun, pYearMonthDate, pOuId, vEmptyId, vEmptyValue;
  132.    
  133.     ANALYZE tr_sales_gross_profit_by_ctgr_product;
  134.        
  135.     --Update harga beli dari in_summary_monthly_cogs
  136.     UPDATE tr_sales_gross_profit_by_ctgr_product B
  137.     SET monthly_price_curr_code = COALESCE(A.curr_code, B.curr_code),
  138.         monthly_price_amount = (CASE WHEN COALESCE(qty_total, 0) = 0 OR COALESCE(amount_total, 0) = 0 THEN
  139.             0
  140.         ELSE
  141.             COALESCE(A.amount_total/A.qty_total, 0)
  142.         END)
  143.     FROM in_summary_monthly_cogs A
  144.     INNER JOIN m_ou_structure C ON C.ou_bu_id = A.ou_id
  145.     WHERE B.tenant_id = A.tenant_id
  146.         AND B.product_id = A.product_id
  147.         AND B.year_month_date = A.date_year_month
  148.         AND C.ou_id = B.ou_id;
  149.        
  150.     ANALYZE tr_sales_gross_profit_by_ctgr_product;
  151.    
  152.     /*
  153.      * update nilai harga beli terakhir menggunakan valuta penjualan (gunakan kurs komersial)
  154.  
  155.     UPDATE tr_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
  156.     WHERE monthly_price_curr_code <> vValutaBuku
  157.     AND session_id = pSessionId AND monthly_price_curr_code <> '';   */
  158.    
  159.     UPDATE tr_sales_gross_profit_by_ctgr_product SET nett_amount_item = nett_amount_item * f_commercial_rate(tenant_id, doc_date, curr_code, vValutaBuku), curr_code = vValutaBuku
  160.     WHERE curr_code <> vValutaBuku
  161.     AND session_id = pSessionId AND curr_code <> '';
  162.    
  163.     /*
  164.      * pindahkan data tahun bulan yang diminta (sudah direkap per partner)
  165.      */
  166.     INSERT INTO tr_current_sales_gross_profit_by_ctgr_product (
  167.         session_id, tenant_id, ou_id, partner_id, partner_code, partner_name,
  168.         ctgr_product_id, ctgr_product_code, ctgr_product_name,
  169.         curr_code, current_sales_amount, current_monthly_price_amount, current_gross_profit
  170.     )
  171.     SELECT pSessionId, tenant_id, ou_id, vEmptyId, vEmptyValue, vEmptyValue,
  172.         ctgr_product_id, ctgr_product_code, ctgr_product_name,
  173.         curr_code, SUM(nett_amount_item) AS current_sales_amount, SUM(monthly_price_amount * qty) AS current_monthly_price_amount, 0
  174.     FROM tr_sales_gross_profit_by_ctgr_product
  175.     WHERE session_id = pSessionId
  176.     AND year_month_date = pYearMonthDate
  177.     --AND doc_date BETWEEN pTglAwal AND pTglAkhir
  178.     GROUP BY tenant_id, ou_id, ctgr_product_id, ctgr_product_code, ctgr_product_name, curr_code;
  179.    
  180.     ANALYZE tr_current_sales_gross_profit_by_ctgr_product;
  181.    
  182.     /*
  183.      * pindahkan data sebelum tahun bulan yang diminta (sudah direkap per partner)
  184.      */
  185.     INSERT INTO tr_rekap_sales_gross_profit_by_ctgr_product (
  186.         session_id, tenant_id, ou_id, partner_id, partner_code, partner_name,
  187.         ctgr_product_id, ctgr_product_code, ctgr_product_name,
  188.         curr_code, rekap_sales_amount, rekap_monthly_price_amount, rekap_gross_profit
  189.     )
  190.     SELECT pSessionId, tenant_id, ou_id, vEmptyId, vEmptyValue, vEmptyValue,
  191.         ctgr_product_id, ctgr_product_code, ctgr_product_name,
  192.         curr_code, SUM(nett_amount_item) AS rekap_sales_amount, SUM(monthly_price_amount * qty) AS rekap_monthly_price_amount, 0
  193.     FROM tr_sales_gross_profit_by_ctgr_product
  194.     WHERE session_id = pSessionId
  195.     --AND year_month_date <> pYearMonthDate
  196.     GROUP BY tenant_id, ou_id, ctgr_product_id, ctgr_product_code, ctgr_product_name, curr_code;
  197.    
  198.     ANALYZE tr_current_sales_gross_profit_by_ctgr_product;
  199.    
  200.     /*
  201.      * update data gross profit untuk bulan yang diminta
  202.      */
  203.     UPDATE tr_current_sales_gross_profit_by_ctgr_product SET current_gross_profit = current_sales_amount - current_monthly_price_amount  
  204.     WHERE session_id = pSessionId;
  205.    
  206.     ANALYZE tr_rekap_sales_gross_profit_by_ctgr_product;
  207.    
  208.     /*
  209.      * update data gross profit untuk sebelum bulan yang diminta
  210.      */
  211.     UPDATE tr_rekap_sales_gross_profit_by_ctgr_product SET rekap_gross_profit = rekap_sales_amount - rekap_monthly_price_amount  
  212.     WHERE session_id = pSessionId;
  213.    
  214.     ANALYZE tr_current_sales_gross_profit_by_ctgr_product;
  215.    
  216.     INSERT INTO tr_sales_gross_profit_by_ctgr_product_for_output
  217.         (session_id, tenant_id, ou_id, partner_id, partner_code, partner_name,
  218.          ctgr_product_id, ctgr_product_code, ctgr_product_name,
  219.          curr_code,
  220.          current_sales_amount, current_gross_profit,
  221.          rekap_sales_amount, rekap_gross_profit)
  222.     SELECT pSessionId, A.tenant_id, ou_id, vEmptyId, vEmptyValue, vEmptyValue,
  223.            A.ctgr_product_id, A.ctgr_product_code, A.ctgr_product_name,
  224.            A.curr_code,
  225.            A.current_sales_amount, A.current_gross_profit,
  226.            0, 0
  227.     FROM tr_current_sales_gross_profit_by_ctgr_product A
  228.     WHERE A.session_id = pSessionId;
  229.    
  230.     ANALYZE tr_rekap_sales_gross_profit_by_ctgr_product;
  231.    
  232.     UPDATE tr_sales_gross_profit_by_ctgr_product_for_output
  233.     SET rekap_sales_amount = A.rekap_sales_amount, rekap_gross_profit = A.rekap_gross_profit
  234.     FROM tr_rekap_sales_gross_profit_by_ctgr_product A
  235.     WHERE A.session_id = pSessionId
  236.         AND tr_sales_gross_profit_by_ctgr_product_for_output.partner_id = A.partner_id
  237.         AND tr_sales_gross_profit_by_ctgr_product_for_output.ctgr_product_id = A.ctgr_product_id;
  238.    
  239.     ANALYZE tr_rekap_sales_gross_profit_by_ctgr_product;
  240.    
  241.     INSERT INTO tr_sales_gross_profit_by_ctgr_product_for_output
  242.         (session_id, tenant_id, ou_id, partner_id, partner_code, partner_name,
  243.          ctgr_product_id, ctgr_product_code, ctgr_product_name,
  244.          curr_code,
  245.          current_sales_amount, current_gross_profit,
  246.          rekap_sales_amount, rekap_gross_profit)
  247.     SELECT pSessionId, A.tenant_id, A.ou_id, vEmptyId, vEmptyValue, vEmptyValue,
  248.            A.ctgr_product_id, A.ctgr_product_code, A.ctgr_product_name,
  249.            A.curr_code,
  250.            0, 0,
  251.            A.rekap_sales_amount, A.rekap_gross_profit
  252.     FROM tr_rekap_sales_gross_profit_by_ctgr_product A
  253.     WHERE A.session_id = pSessionId
  254.     AND ROW(A.partner_id, A.ctgr_product_id) NOT IN ( SELECT B.partner_id, B.ctgr_product_id FROM tr_sales_gross_profit_by_ctgr_product_for_output B WHERE B.session_id = pSessionId );
  255.    
  256.     ANALYZE tr_sales_gross_profit_by_ctgr_product_for_output;
  257.    
  258.     SELECT CASE
  259.                 WHEN SUM(A.current_sales_amount) = 0 THEN 0
  260.                 ELSE COALESCE (SUM(A.current_gross_profit)/SUM(A.current_sales_amount), 0)
  261.            END,
  262.            CASE
  263.                 WHEN SUM(A.rekap_sales_amount) = 0 THEN 0
  264.                 ELSE COALESCE (SUM(A.rekap_gross_profit)/SUM(A.rekap_sales_amount), 0)
  265.            END
  266.     FROM tr_sales_gross_profit_by_ctgr_product_for_output A
  267.     INTO vTotalCurrentProfitPercentage, vTotalBeforeProfitPercentage;
  268.    
  269.     /*
  270.      * return header
  271.      */
  272.     Open pRefHeader FOR
  273.     SELECT A.fullname AS username, B.ou_name AS ou_name, pYearMonthDate AS current_month_year, vAwalTahun AS start_month_year, vAkhirRekap AS end_month_year,
  274.     vValutaBuku AS valuta_buku, pDatetime AS datetime,
  275.     vTotalCurrentProfitPercentage AS total_current_profit_percentage, vTotalBeforeProfitPercentage AS total_before_profit_percentage
  276.     FROM t_user A
  277.     INNER JOIN t_ou B ON A.tenant_id = B.tenant_id
  278.     WHERE user_id = pUserId
  279.     AND ou_id = pOuId;
  280.    
  281.     RETURN NEXT pRefHeader;
  282.  
  283.     Open pRefDetail FOR
  284.     SELECT A.ctgr_product_id AS ctgr_product_id, A.ctgr_product_code AS ctgr_product_code, A.ctgr_product_name AS ctgr_product_name,
  285.            A.curr_code AS curr_code,
  286.            A.current_sales_amount AS current_sales, A.current_gross_profit AS current_gross_profit, A.current_gross_profit AS current_gross_profit,
  287.            CASE
  288.                 WHEN A.current_sales_amount = 0 THEN 0
  289.                 ELSE COALESCE (A.current_gross_profit/A.current_sales_amount, 0)
  290.            END AS current_profit_percentage,
  291.            A.rekap_sales_amount AS before_sales, A.rekap_gross_profit AS before_gross_profit,
  292.            CASE
  293.                 WHEN A.rekap_sales_amount = 0 THEN 0
  294.                 ELSE COALESCE (A.rekap_gross_profit/A.rekap_sales_amount, 0)
  295.            END AS before_profit_percentage
  296.     FROM tr_sales_gross_profit_by_ctgr_product_for_output A
  297.     WHERE A.session_id = pSessionId
  298.     ORDER BY curr_code, ctgr_product_name;
  299.    
  300.     RETURN NEXT pRefDetail;
  301.  
  302.     DELETE FROM tr_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  303.     DELETE FROM tr_current_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  304.     DELETE FROM tr_rekap_sales_gross_profit_by_ctgr_product WHERE session_id = pSessionId;
  305.     DELETE FROM tr_sales_gross_profit_by_ctgr_product_for_output WHERE session_id = pSessionId;
  306.    
  307. END;
  308. $BODY$
  309.   LANGUAGE plpgsql VOLATILE
  310.   COST 100
  311.   ROWS 1000;
  312.   /
Add Comment
Please, Sign In to add comment