Advertisement
aadddrr

r_analysis_sales_nett_profit_with_monthly_average_ctgr_produ

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