Advertisement
henikseptiana15

Untitled

Mar 9th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION r_print_form_do_invoice(CHARACTER VARYING, BIGINT, BIGINT, BIGINT, CHARACTER VARYING, BIGINT, CHARACTER VARYING, CHARACTER VARYING)
  2.   RETURNS SETOF refcursor AS
  3. $BODY$
  4. DECLARE
  5.     pRefHeader              REFCURSOR := 'refHeader';
  6.     pRefDetail              REFCURSOR := 'refDetail';
  7.    
  8.     pSessionId              ALIAS FOR $1;
  9.     pTenantId                   ALIAS FOR $2;
  10.     pUserId                 ALIAS FOR $3;
  11.     pRoleId                 ALIAS FOR $4;
  12.     pDatetime               ALIAS FOR $5;
  13.     pDocId                      ALIAS FOR $6;
  14.     pFlgDoc                 ALIAS FOR $7;
  15.     pFlgShowPpn                 ALIAS FOR $8;
  16.    
  17.     vInvoiceId              BIGINT;
  18.     --vFlgShowPpn           CHARACTER VARYING := 'Y';
  19.     pFlgPkp                 CHARACTER VARYING := 'N';
  20.     vStatusReleased         CHARACTER VARYING := 'R';
  21.     vStatusDO               CHARACTER VARYING(1);
  22.     vInvoiceTempId          BIGINT := -99;
  23. BEGIN
  24.  
  25.     -- Delete table temporary  
  26.     DELETE FROM tt_form_do_invoice_header WHERE session_id = pSessionId;
  27.     DELETE FROM tt_form_do_invoice_item WHERE session_id = pSessionId;
  28.    
  29.     IF (pFlgDoc = 'INVOICE') THEN
  30.         -- Merupakan dokumen Sales Invoice
  31.        
  32.         -- ambil flg pkp
  33.         SELECT A.flg_pkp INTO pFlgPkp
  34.         FROM t_ou_legal A
  35.             INNER JOIN sl_invoice B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  36.         WHERE B.invoice_id = pDocId;
  37.        
  38.         -- cari flg show ppn berdasarkan SO
  39.         /*
  40.         SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  41.         FROM sl_invoice A
  42.             INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  43.         WHERE A.invoice_id = pDocId;
  44.         */
  45.        
  46.         -- Menjalankan function r_print_delivery_order_from_invoice
  47.         IF(pFlgShowPpn='Y') THEN
  48.             PERFORM r_print_delivery_order_from_invoice(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  49.         ELSE
  50.             PERFORM r_print_delivery_order_from_invoice_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  51.         END IF;
  52.     ELSIF (pFlgDoc = 'INVOICE_TEMP') THEN
  53.         -- Merupakan dokumen Temporary Sales Invoice
  54.        
  55.         -- Cek dulu apakah Temp Sales Invoice sudah dibuat Sales Invoice (invoice asli) atau belum
  56.         IF EXISTS (SELECT 1 FROM sl_invoice WHERE ref_inv_temp_id = pDocId) THEN
  57.             SELECT invoice_id INTO vInvoiceId FROM sl_invoice WHERE ref_inv_temp_id = pDocId;
  58.            
  59.             -- ambil flg pkp
  60.             SELECT A.flg_pkp INTO pFlgPkp
  61.             FROM t_ou_legal A
  62.                 INNER JOIN sl_invoice B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  63.             WHERE B.invoice_id = vInvoiceId;
  64.            
  65.             -- cari flg show ppn berdasarkan SO
  66.             /*
  67.             SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  68.             FROM sl_invoice A
  69.                 INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  70.             WHERE A.invoice_id = vInvoiceId;
  71.             */
  72.            
  73.             -- Menjalankan function r_print_delivery_order_from_invoice
  74.             IF(pFlgShowPpn='Y') THEN
  75.                 PERFORM r_print_delivery_order_from_invoice(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceId);
  76.             ELSE
  77.                 PERFORM r_print_delivery_order_from_invoice_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceId);
  78.             END IF;
  79.         ELSE
  80.        
  81.             -- ambil flg pkp
  82.             SELECT A.flg_pkp INTO pFlgPkp
  83.             FROM t_ou_legal A
  84.                 INNER JOIN sl_invoice_temp B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  85.             WHERE B.invoice_temp_id = pDocId;
  86.            
  87.             -- cari flg show ppn berdasarkan SO
  88.             /*
  89.             SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  90.             FROM sl_invoice_temp A
  91.                 INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  92.             WHERE A.invoice_temp_id = pDocId;
  93.             */
  94.            
  95.             -- Menjalankan function r_print_delivery_order_from_invoice_temp
  96.             IF(pFlgShowPpn='Y') THEN
  97.                 PERFORM r_print_delivery_order_from_invoice_temp(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  98.             ELSE
  99.                 PERFORM r_print_delivery_order_from_invoice_temp_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  100.             END IF;
  101.         END IF;
  102.        
  103.     ELSIF (pFlgDoc = 'DO') THEN
  104.         -- Merupakan dokumen delivery order
  105.         -- Cek Status doc DO
  106.         SELECT status_doc INTO vStatusDO FROM sl_do WHERE tenant_id = pTenantId AND do_id = pDocId;
  107.        
  108.         -- GET invoice_temp_id from DO
  109.         SELECT B.invoice_temp_id INTO vInvoiceTempId
  110.         FROM sl_do A
  111.         INNER JOIN sl_invoice_temp B ON A.ref_id = B.ref_id AND A.ref_doc_type_id = B.ref_doc_type_id
  112.         WHERE A.tenant_id = pTenantId AND
  113.               A.do_id = pDocId AND
  114.               B.status_doc = vStatusReleased;
  115.        
  116.         IF (vStatusDO = vStatusReleased and vInvoiceTempId != -99) THEN
  117.         -- UNTUK DO yg status_doc = R -> acuan datanya dari dokumen sales invoice temp    
  118.            
  119.             -- Cek dulu apakah Temp Sales Invoice sudah dibuat Sales Invoice (invoice asli) atau belum
  120.             IF EXISTS (SELECT 1 FROM sl_invoice WHERE ref_inv_temp_id = vInvoiceTempId) THEN
  121.                 SELECT invoice_id INTO vInvoiceId FROM sl_invoice WHERE ref_inv_temp_id = vInvoiceTempId;
  122.                
  123.                 -- ambil flg pkp
  124.                 SELECT A.flg_pkp INTO pFlgPkp
  125.                 FROM t_ou_legal A
  126.                     INNER JOIN sl_invoice B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  127.                 WHERE B.invoice_id = vInvoiceId;
  128.                
  129.                 -- cari flg show ppn berdasarkan SO
  130.                 /*
  131.                 SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  132.                 FROM sl_invoice A
  133.                     INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  134.                 WHERE A.invoice_id = vInvoiceId;
  135.                 */
  136.                
  137.                 -- Menjalankan function r_print_delivery_order_from_invoice
  138.                 IF(pFlgShowPpn='Y') THEN
  139.                     PERFORM r_print_delivery_order_from_invoice(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceId);
  140.                 ELSE
  141.                     PERFORM r_print_delivery_order_from_invoice_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceId);
  142.                 END IF;
  143.             ELSE
  144.            
  145.                 -- ambil flg pkp
  146.                 SELECT A.flg_pkp INTO pFlgPkp
  147.                 FROM t_ou_legal A
  148.                     INNER JOIN sl_invoice_temp B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  149.                 WHERE B.invoice_temp_id = vInvoiceTempId;
  150.                
  151.                 -- cari flg show ppn berdasarkan SO
  152.                 /*
  153.                 SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  154.                 FROM sl_invoice_temp A
  155.                     INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  156.                 WHERE A.invoice_temp_id = vInvoiceTempId;
  157.                 */
  158.                
  159.                 -- Menjalankan function r_print_delivery_order_from_invoice_temp
  160.                 IF(pFlgShowPpn='Y') THEN
  161.                     PERFORM r_print_delivery_order_from_invoice_temp(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceTempId);
  162.                 ELSE
  163.                     PERFORM r_print_delivery_order_from_invoice_temp_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, vInvoiceTempId);
  164.                 END IF;
  165.             END IF;
  166.         ELSE
  167.         -- UNTUK DO yg status_doc = I/D -> acuan datanya dari dokumen DO
  168.        
  169.             -- ambil flg pkp
  170.             SELECT A.flg_pkp INTO pFlgPkp
  171.             FROM t_ou_legal A
  172.                 INNER JOIN sl_do B ON A.tenant_id = B.tenant_id AND A.ou_id = B.ou_id
  173.             WHERE B.do_id = pDocId;
  174.            
  175.             -- cari flg show ppn berdasarkan SO
  176.             /*
  177.             SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  178.             FROM sl_do A
  179.                 INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  180.             WHERE A.do_id = pDocId;
  181.             */
  182.            
  183.             -- Menjalankan function r_print_form_delivery_order_escp
  184.             IF(pFlgShowPpn='Y') THEN
  185.                 PERFORM r_print_form_delivery_order_escp(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  186.             ELSE
  187.                 PERFORM r_print_form_delivery_order_escp_show_ppn_no(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pDocId);
  188.             END IF;
  189.        
  190.         END IF;
  191.        
  192.        
  193.     END IF;
  194.    
  195.     -- Return data header
  196.     OPEN pRefHeader FOR
  197.     SELECT A.doc_id, A.doc_no, A.doc_date, A.so_no || ' ' || f_get_create_datetime_so_print_do(A.so_id) AS so_no, A.po_no, A.ou_id, A.partner_ship_to_id, A.cust_ship_to_code,
  198.         A.cust_ship_to_name, A.partner_ship_address_id, A.cust_addr_1, A.cust_addr_2, A.cust_addr_3, A.cust_phone,
  199.         A.cust_fax, A.cust_city, B.partner_bill_to_id, f_get_partner_code(B.partner_bill_to_id) AS cust_bill_to_code,
  200.         f_get_partner_name(B.partner_bill_to_id) AS cust_bill_to_name, C.partner_address_id, C.address1 AS cust_bill_addr_1,
  201.         C.address2 AS cust_bill_addr_2, C.address3 AS cust_bill_addr_3, C.phone1 AS cust_bill_phone,
  202.         C.fax1 AS cust_bill_fax, C.city AS cust_bill_city, B.ext_doc_no AS so_ext_doc_no,
  203.         A.cust_npwp_name, A.cust_npwp_no, A.cust_npwp_date, regexp_replace(A.remark1, '\r|\n', ' ', 'g') AS remark1,
  204.         regexp_replace(A.remark2, '\r|\n', ' ', 'g') AS remark2,
  205.         A.username, A.rolename, A.datetime, A.date_of_datetime, A.time_of_datetime, ROUND(A.total_harga_akhir::Numeric)::Character Varying AS total_harga_akhir,
  206.         SUBSTRING (RPAD(f_get_last_remark_approved_so(A.so_id), 175, ' '), 1, 35) AS remark_approved_so_1,
  207.         SUBSTRING (RPAD(f_get_last_remark_approved_so(A.so_id), 175, ' '), 36, 35) AS remark_approved_so_2,
  208.         SUBSTRING (RPAD(f_get_last_remark_approved_so(A.so_id), 175, ' '), 71, 35) AS remark_approved_so_3,
  209.         SUBSTRING (RPAD(f_get_last_remark_approved_so(A.so_id), 175, ' '), 106, 35) AS remark_approved_so_4,
  210.         SUBSTRING (RPAD(f_get_last_remark_approved_so(A.so_id), 175, ' '), 141, 35) AS remark_approved_so_5
  211.     FROM tt_form_do_invoice_header A
  212.     INNER JOIN sl_so B ON A.so_id = B.so_id
  213.     LEFT OUTER JOIN m_partner_address C ON B.partner_bill_to_id = C.partner_id AND C.flg_official = 'Y'
  214.     WHERE A.session_id = pSessionId;
  215.    
  216.     RETURN NEXT pRefHeader;
  217.    
  218.     -- Return data item
  219.     OPEN pRefDetail FOR
  220.     SELECT product_id, product_code, product_name, brand_id, brand_code, brand_name,
  221.         qty, uom_id, uom_code, uom_name, ROUND(gross_sell_price::Numeric)::Character Varying AS gross_sell_price, ROUND(amount::Numeric)::Character Varying AS amount, sub_total_discount, tax_amount, nett_amount, SIZE, color, ordial
  222.     FROM tt_form_do_invoice_item
  223.     WHERE session_id = pSessionId
  224.     ORDER BY ordial, brand_code, product_name;
  225.    
  226.     RETURN NEXT pRefDetail;
  227.    
  228.     -- Delete table temporary
  229.     DELETE FROM tt_form_do_invoice_header WHERE session_id = pSessionId;
  230.     DELETE FROM tt_form_do_invoice_item WHERE session_id = pSessionId;
  231.  
  232.    
  233. END;
  234. $BODY$
  235.   LANGUAGE plpgsql VOLATILE
  236.   COST 100;
  237.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement