Advertisement
aadddrr

r_report_sales_invoice_mlm_generated

Jul 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION r_report_sales_invoice_mlm_generated(bigint, bigint, character varying, character varying, bigint)
  2.   RETURNS SETOF refcursor AS
  3. $BODY$
  4.     DECLARE
  5.         pRefHeaderSalesInvoiceMlm       REFCURSOR := 'refHeaderSalesInvoiceMlm';
  6.         pRefSalesInvoiceMlm         REFCURSOR := 'refSalesInvoiceMlm';
  7.    
  8.         pOuId                   ALIAS FOR $1;
  9.         pTenantId               ALIAS FOR $2;
  10.         pSessionId              ALIAS FOR $3;
  11.         pYearMonth              ALIAS FOR $4;
  12.         pPartnerId              ALIAS FOR $5;
  13.        
  14.         vYes                    character varying := 'Y';
  15.         vEmptyString                character varying := '';
  16.         vEmptyBigintValue           bigint := -99;
  17.            
  18.         vDoDocTypeId                bigint;
  19.         vDoMlmDocTypeId             bigint;
  20.         vReturDocTypeId             bigint;
  21.         vBuybackDocTypeId           bigint;
  22.         vReturnNoteDocTypeId            bigint;
  23.         vDoRecieptDocTypeId         bigint;
  24.         vFilterPartner              character varying := '';
  25.         vFilterPartnerDoAssembly        character varying := '';
  26.         vDateFrom               character varying := '';
  27.         vDateTo                 character varying := '';
  28.         vReportTitle                character varying := 'REPORT HASIL GENERATE SALES INVOICE';
  29.        
  30. BEGIN
  31.     vDoDocTypeId := 311;
  32.     vDoMlmDocTypeId := 313;
  33.     vReturDocTypeId := 562;
  34.     vBuybackDocTypeId := 563;
  35.     vReturnNoteDocTypeId := 502;
  36.     vDoRecieptDocTypeId := 526;
  37.  
  38.     IF pPartnerId <> vEmptyBigintValue THEN
  39.         vFilterPartner := 'AND A.partner_id = ' || pPartnerId;
  40.         vFilterPartnerDoAssembly := ' AND D.partner_id = ' || pPartnerId;
  41.     END IF;
  42.        
  43.     --membuat dateFrom dan dateEnd
  44.     vDateFrom := pYearMonth || '01';
  45.     SELECT TO_CHAR(TO_DATE(MAX(pYearMonth),'YYYYMM') + interval '1 Month - 1 day','YYYYMMDD') INTO vDateTo;
  46.  
  47.     --inisialisasi pRefHeaderSalesInvoiceMlm
  48.     OPEN pRefHeaderSalesInvoiceMlm FOR
  49.         SELECT vReportTitle AS report_title, f_get_partner_code(pPartnerId) AS partner_code, f_get_partner_name(pPartnerId) AS partner_name,
  50.             pYearMonth AS year_month_period, vDateFrom, vDateTo;
  51.     RETURN NEXT pRefHeaderSalesInvoiceMlm;
  52.  
  53.     --memastikan tabel temp kosong
  54.     DELETE FROM tt_report_sales_invoice_generated WHERE session_id = pSessionId;
  55.     DELETE FROM tt_item_penjualan_by_period WHERE session_id = pSessionId;
  56.    
  57.     --insert data ke tt_item_penjualan_by_period dengan function f_get_data_penjualan_by_period
  58.     PERFORM f_get_data_penjualan_by_period(pSessionId, pTenantId, pOuId, vDateFrom, vDateTo);
  59.    
  60.     -- DO MLM
  61.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  62.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  63.     WITH komisi_do_mlm AS (
  64.         SELECT A.doc_id, (SUM(A.qty * F.price_after_disc) * E.commission_percentage * 0.01) AS komisi
  65.         FROM tt_item_penjualan_by_period A
  66.         INNER JOIN sl_do_mlm D
  67.             ON A.doc_id = D.do_mlm_id
  68.         INNER JOIN sl_so_mlm E
  69.             ON D.ref_id = E.so_mlm_id
  70.         INNER JOIN sl_so_mlm_balance_product F
  71.             ON E.so_mlm_id = F.so_mlm_id
  72.             AND A.product_id = F.product_id
  73.         LEFT JOIN sl_so_mlm_cost G
  74.             ON E.so_mlm_id = G.so_mlm_id
  75.         WHERE A.session_id = $1
  76.             AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3
  77.         GROUP BY A.doc_id, E.commission_percentage
  78.     )
  79.     SELECT '''|| pSessionId ||''',  A.doc_type_id, A.doc_no, A.doc_date, E.doc_no, E.doc_date, H.komisi, COALESCE(G.add_amount, 0) AS add_amount,  
  80.         A.product_catalog_id, A.product_id, A.qty, F.gross_sell_price, F.discount_member_percentage, F.discount_promo_percentage,
  81.         F.price_after_disc, (A.qty * F.gross_sell_price) AS total_harga_katalog, (A.qty * F.price_after_disc) AS total_harga_netto
  82.     FROM tt_item_penjualan_by_period A
  83.     INNER JOIN sl_do_mlm D
  84.         ON A.doc_id = D.do_mlm_id
  85.     INNER JOIN sl_so_mlm E
  86.         ON D.ref_id = E.so_mlm_id
  87.     INNER JOIN sl_so_mlm_balance_product F
  88.         ON E.so_mlm_id = F.so_mlm_id
  89.         AND A.product_id = F.product_id
  90.     LEFT JOIN sl_so_mlm_cost G
  91.         ON E.so_mlm_id = G.so_mlm_id
  92.     LEFT JOIN komisi_do_mlm H
  93.         ON A.doc_id = H.doc_id 
  94.     WHERE A.session_id = $1
  95.     AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  96.     USING pSessionId, vDoMlmDocTypeId, pYearMonth;
  97.  
  98.     -- Retur SO MLM
  99.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  100.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  101.     WITH komisi_retur_mlm AS (
  102.         SELECT A.doc_id, (SUM(A.qty * I.price_after_disc) * H.commission_percentage * 0.01) AS komisi
  103.         FROM tt_item_penjualan_by_period A
  104.         INNER JOIN in_inventory E
  105.             ON A.doc_id = E.inventory_id
  106.             AND A.doc_type_id = E.doc_type_id
  107.         INNER JOIN in_inventory_return_buyback_mlm F
  108.             ON E.inventory_id = F.inventory_id
  109.             AND A.product_id = F.product_id
  110.         INNER JOIN sl_so_mlm H
  111.             ON E.ref_id = H.so_mlm_id
  112.             AND E.ref_doc_type_id = H.doc_type_id
  113.         INNER JOIN sl_so_mlm_balance_product I
  114.             ON I.so_mlm_id = H.so_mlm_id
  115.             AND I.product_id = A.product_id
  116.         WHERE A.session_id = $1
  117.             AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3
  118.         GROUP BY A.doc_id, H.commission_percentage
  119.     )
  120.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, H.doc_no, H.doc_date, J.komisi, 0, A.product_catalog_id, A.product_id,  
  121.         A.qty, I.gross_sell_price , I.discount_member_percentage, I.discount_promo_percentage,
  122.         I.price_after_disc, (I.gross_sell_price * A.qty) AS total_harga_katalog, (I.price_after_disc * A.qty) AS total_harga_netto
  123.     FROM tt_item_penjualan_by_period A
  124.     INNER JOIN in_inventory E
  125.         ON A.doc_id = E.inventory_id
  126.         AND A.doc_type_id = E.doc_type_id
  127.     INNER JOIN in_inventory_return_buyback_mlm F
  128.         ON E.inventory_id = F.inventory_id
  129.         AND A.product_id = F.product_id
  130.     INNER JOIN sl_so_mlm H
  131.         ON E.ref_id = H.so_mlm_id
  132.         AND E.ref_doc_type_id = H.doc_type_id
  133.     INNER JOIN sl_so_mlm_balance_product I
  134.         ON I.so_mlm_id = H.so_mlm_id
  135.         AND I.product_id = A.product_id
  136.     LEFT JOIN komisi_retur_mlm J
  137.         ON A.doc_id = J.doc_id
  138.     WHERE A.session_id = $1
  139.     AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  140.     USING pSessionId, vReturDocTypeId, pYearMonth;
  141.    
  142.  
  143.     -- Buyback
  144.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  145.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  146.     WITH komisi_buyback AS (
  147.         SELECT A.doc_id, (SUM(A.qty * I.price_after_disc) * H.commission_percentage * 0.01) AS komisi
  148.         FROM tt_item_penjualan_by_period A
  149.         INNER JOIN in_inventory E
  150.             ON A.doc_id = E.inventory_id AND A.doc_type_id = E.doc_type_id
  151.         INNER JOIN in_inventory_return_buyback_mlm F
  152.             ON E.inventory_id = F.inventory_id
  153.             AND A.product_id = F.product_id
  154.         INNER JOIN sl_so_mlm H
  155.             ON E.ref_id = H.so_mlm_id
  156.             AND E.ref_doc_type_id = H.doc_type_id
  157.         INNER JOIN sl_so_mlm_balance_product I
  158.             ON I.so_mlm_id = H.so_mlm_id
  159.             AND I.product_id = A.product_id
  160.         WHERE A.session_id = $1
  161.             AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3
  162.         GROUP BY A.doc_id, H.commission_percentage
  163.     )
  164.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, H.doc_no, H.doc_date, J.komisi, 0, A.product_catalog_id, A.product_id,  
  165.         A.qty, I.gross_sell_price , I.discount_member_percentage, I.discount_promo_percentage,
  166.         I.price_after_disc, (I.gross_sell_price * A.qty) AS total_harga_katalog, (I.price_after_disc * A.qty) AS total_harga_netto
  167.     FROM tt_item_penjualan_by_period A
  168.     INNER JOIN in_inventory E
  169.         ON A.doc_id = E.inventory_id AND A.doc_type_id = E.doc_type_id
  170.     INNER JOIN in_inventory_return_buyback_mlm F
  171.         ON E.inventory_id = F.inventory_id
  172.         AND A.product_id = F.product_id
  173.     INNER JOIN sl_so_mlm H
  174.         ON E.ref_id = H.so_mlm_id
  175.         AND E.ref_doc_type_id = H.doc_type_id
  176.     INNER JOIN sl_so_mlm_balance_product I
  177.         ON I.so_mlm_id = H.so_mlm_id
  178.         AND I.product_id = A.product_id
  179.     LEFT JOIN komisi_buyback J
  180.         ON A.doc_id = J.doc_id
  181.     WHERE A.session_id = $1
  182.         AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  183.     USING pSessionId, vBuybackDocTypeId, pYearMonth;
  184.  
  185.     --return note
  186.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  187.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  188.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, H.doc_no, H.doc_date, 0, 0, A.product_catalog_id, A.product_id,  
  189.         A.qty, (I.gross_sell_price - discount_amount) AS price_after_disc, 0, 0, (I.gross_sell_price - discount_amount) AS price_after_disc,
  190.         (A.qty * (I.gross_sell_price - discount_amount)) AS total_price_after_disc, (A.qty * (I.gross_sell_price - discount_amount)) AS total_price_after_disc
  191.     FROM tt_item_penjualan_by_period A
  192.     INNER JOIN in_inventory D
  193.         ON A.doc_id = D.inventory_id
  194.         AND A.doc_type_id = D.doc_type_id
  195.     INNER JOIN sl_do G
  196.         ON D.ref_id = G.do_id
  197.         AND A.doc_id = G.do_id
  198.     INNER JOIN sl_so H
  199.         ON G.ref_id = H.so_id
  200.     INNER JOIN sl_so_item I
  201.         ON H.so_id = I.so_id
  202.         AND A.product_id = I.product_id
  203.     WHERE A.session_id = $1
  204.     AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  205.     USING pSessionId, vReturnNoteDocTypeId, pYearMonth;
  206.  
  207.     --DO
  208.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  209.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  210.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, E.doc_no, E.doc_date, 0, COALESCE(G.add_amount, 0), A.product_catalog_id, A.product_id,
  211.         A.qty, (F.gross_sell_price - discount_amount) AS price_after_disc, 0, 0, (F.gross_sell_price - discount_amount) AS price_after_disc,
  212.         (A.qty * (F.gross_sell_price - discount_amount)) AS total_price_after_disc, (A.qty * (F.gross_sell_price - discount_amount)) AS total_price_after_disc
  213.     FROM tt_item_penjualan_by_period A
  214.     INNER JOIN sl_do D
  215.         ON A.doc_id = D.do_id
  216.     INNER JOIN sl_so E
  217.         ON D.ref_id = E.so_id
  218.     INNER JOIN sl_so_item F
  219.         ON E.so_id = F.so_id
  220.         AND A.product_id = F.product_id
  221.     LEFT JOIN sl_so_cost G
  222.         ON E.so_id = G.so_id
  223.     WHERE A.session_id = $1
  224.     AND A.doc_type_id = $2 ' || vFilterPartner || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  225.     USING pSessionId, vDoDocTypeId, pYearMonth;
  226.    
  227.     -- Added By Julius, 22 Sept 2017
  228.     -- DO PRODUCT ASSEMBLY
  229.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  230.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  231.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, E.doc_no, E.doc_date, 0, COALESCE(G.add_amount, 0), D.product_catalog_id, H.parent_product_id,
  232.         B.qty_dlv_int, (F.price_so - 0) AS price_after_disc, 0, 0, (F.price_so - 0) AS price_after_disc,
  233.         (B.qty_dlv_int * (F.price_so - 0)) AS total_price_after_disc, (B.qty_dlv_int * (F.price_so - 0)) AS total_price_after_disc
  234.     FROM sl_do A
  235.     INNER JOIN sl_do_item B
  236.         ON A.do_id = B.do_id
  237.     INNER JOIN sl_do_child_item C
  238.         ON B.do_item_id = C.do_item_id
  239.     INNER JOIN tt_item_penjualan_by_period D
  240.         ON A.do_id = D.doc_id
  241.         AND C.product_id = D.product_id
  242.     INNER JOIN sl_so E
  243.         ON A.ref_id = E.so_id
  244.     INNER JOIN sl_so_balance_invoice F
  245.         ON E.so_id = F.so_id
  246.         AND B.do_item_id = F.ref_item_id
  247.     LEFT JOIN sl_so_cost G
  248.         ON E.so_id = G.so_id
  249.     INNER JOIN m_product_assembly H
  250.         ON C.product_id = H.child_product_id
  251.         AND B.product_id = H.parent_product_id
  252.     WHERE D.session_id = $1
  253.     AND D.doc_type_id = $2 ' || vFilterPartnerDoAssembly || ' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  254.     USING pSessionId, vDoDocTypeId, pYearMonth;
  255.  
  256.     --DO RECIEPT
  257.     EXECUTE 'INSERT INTO tt_report_sales_invoice_generated (session_id, doc_type_id, doc_no, doc_date, so_no, so_date, komisi, additional_cost,
  258.         product_catalog_id, product_id, qty, harga_katalog, disc_member, disc_promo, harga_netto, total_harga_katalog, total_harga_netto)
  259.     SELECT '''|| pSessionId ||''', A.doc_type_id, A.doc_no, A.doc_date, G.doc_no, G.doc_date, 0, 0, A.product_catalog_id, A.product_id,
  260.         A.qty, (H.gross_sell_price - discount_amount) AS price_after_disc, 0, 0, (H.gross_sell_price - discount_amount) AS price_after_disc,
  261.         (A.qty * (H.gross_sell_price - discount_amount)) AS total_price_after_disc, (A.qty * (H.gross_sell_price - discount_amount)) AS total_price_after_disc
  262.     FROM tt_item_penjualan_by_period A
  263.     INNER JOIN in_do_receipt D
  264.         ON A.doc_id = D.do_receipt_id
  265.     INNER JOIN in_do_receipt_item E
  266.         ON D.do_receipt_id = E.do_receipt_id
  267.         AND A.product_id = E.product_id
  268.     INNER JOIN sl_do F
  269.         ON D.ref_id = F.do_id AND D.ref_doc_type_id = 311
  270.     INNER JOIN sl_so G
  271.         ON F.ref_id = G.so_id AND F.ref_doc_type_id = 301
  272.     INNER JOIN sl_so_item H
  273.         ON G.so_id = H.so_id
  274.     WHERE A.session_id = $1
  275.     AND A.doc_type_id = $2 ' || vFilterPartner ||' AND SUBSTR(A.doc_date, 1, 6) = $3;'
  276.     USING pSessionId, vDoRecieptDocTypeId, pYearMonth;
  277.  
  278.    
  279.     /* NOTE: untuk Zona HK, ditemukan kasus:
  280.      * Jika nilai catalog_price0 dan catalog_price1 sama, maka zona yang akan muncul adalah zona yang terlebih dahulu masuk ke kondisi CASE WHEN
  281.      * Untuk saat ini, sementara ditentukan jika nilainya sama, akan dibuat menjadi HK (catalog_price0)
  282.      */
  283.     OPEN pRefSalesInvoiceMlm FOR
  284.         SELECT f_get_doc_desc(A.doc_type_id) AS doc_desc, A.doc_no, A.doc_date, A.so_no, A.so_date, A.komisi, A.additional_cost, COALESCE(C.product_catalog_code,'-') AS product_catalog_code,
  285.             COALESCE(Z.catalog_name,'-') AS catalog_name, B.product_code, B.product_name, A.qty, A.harga_katalog, A.disc_member, A.disc_promo, A.harga_netto,
  286.             A.total_harga_katalog, A.total_harga_netto,
  287.             CASE
  288.                 WHEN A.harga_katalog = COALESCE(C.catalog_price0,0) THEN 'HK'
  289.                 WHEN A.harga_katalog = COALESCE(C.catalog_price,0) THEN 'HK1'
  290.                 WHEN A.harga_katalog = COALESCE(C.catalog_price2,0) THEN 'HK2'
  291.             ELSE 'OTHER-HK' END AS zona_hk
  292.         FROM tt_report_sales_invoice_generated A
  293.         INNER JOIN m_product B
  294.             ON A.product_id = B.product_id
  295.         LEFT JOIN m_product_catalog C
  296.             ON B.product_id = C.product_id
  297.             AND A.product_catalog_id = C.product_catalog_id
  298.         LEFT JOIN m_catalog Z
  299.             ON C.catalog_id = Z.catalog_id
  300.         WHERE session_id = pSessionId
  301.         GROUP BY f_get_doc_desc(A.doc_type_id), A.doc_no, A.doc_date, A.so_no, A.so_date, A.komisi, A.additional_cost, COALESCE(C.product_catalog_code,'-'),
  302.             COALESCE(Z.catalog_name,'-'), B.product_code, B.product_name, A.qty, A.harga_katalog, A.disc_member, A.disc_promo, A.harga_netto,
  303.             A.total_harga_katalog, A.total_harga_netto,A.harga_katalog,COALESCE(C.catalog_price0,0),COALESCE(C.catalog_price,0),COALESCE(C.catalog_price2,0), C.product_catalog_code
  304.         ORDER BY f_get_doc_desc(A.doc_type_id), A.doc_date, A.doc_no, COALESCE(Z.catalog_name,'-'), C.product_catalog_code ASC;
  305.     RETURN NEXT pRefSalesInvoiceMlm;
  306.    
  307.     DELETE FROM tt_report_sales_invoice_generated WHERE session_id = pSessionId;
  308.     DELETE FROM tt_item_penjualan_by_period WHERE session_id = pSessionId;
  309. END;
  310. $BODY$
  311.   LANGUAGE plpgsql VOLATILE
  312.   COST 100
  313.   ROWS 1000;
  314. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement