aadddrr

r_print_sales_invoice_temporary_INDOCOM_20171204

Dec 3rd, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Function: r_print_sales_invoice_temporary(character varying, bigint, bigint, bigint, character varying, bigint, character varying)
  2.  
  3. -- DROP FUNCTION r_print_sales_invoice_temporary(character varying, bigint, bigint, bigint, character varying, bigint, character varying);
  4.  
  5. CREATE OR REPLACE FUNCTION r_print_sales_invoice_temporary(character varying, bigint, bigint, bigint, character varying, bigint, character varying)
  6.   RETURNS SETOF refcursor AS
  7. $BODY$
  8. DECLARE
  9.     pRefHeader              REFCURSOR := 'refHeader';
  10.     pRefDetail              REFCURSOR := 'refDetail';
  11.     pSessionId              ALIAS FOR $1;
  12.     pTenantId               ALIAS FOR $2;
  13.     pUserId                 ALIAS FOR $3;
  14.     pRoleId                 ALIAS FOR $4;
  15.     pDatetime               ALIAS FOR $5;
  16.     pInvoiceTempId          ALIAS FOR $6;
  17.     pFlgPkp                 ALIAS FOR $7;
  18.    
  19.     vDoDocTypeId                bigint;
  20.     vReportName                 character varying;
  21.     vDoNo                       character varying;
  22.     vAdditionalCost             numeric;
  23.     vDownPaymentAmount          numeric;
  24.     vDownPaymentAmountWithTax   numeric;
  25.     vTaxDownPaymentAmount       numeric;
  26.     vFlgShowPpn                 character varying := 'Y';
  27.     vOuBuInfoReport             OU_INFO_REPORT%ROWTYPE;
  28.     result                      record;
  29.     vAll                        character varying(5);
  30. BEGIN
  31.     vDoDocTypeId := 311;
  32.     vReportName := 'FormTemporarySalesInvoice';
  33.     vAll := 'ALL';
  34.    
  35.     SELECT f_get_ou_bu_info_report(pTenantId, A.ou_id) AS info
  36.     FROM sl_invoice_temp A
  37.     WHERE A.invoice_temp_id = pInvoiceTempId INTO result ;
  38.     vOuBuInfoReport := result.info;
  39.    
  40.     DELETE FROM tr_sl_invoice_item WHERE session_id = pSessionId;
  41.        
  42.     /* get delivery order number */
  43.     SELECT STRING_AGG(Z.doc_no, ' , ' ) INTO vDoNo
  44.     FROM (
  45.         SELECT B.doc_no
  46.         FROM sl_invoice_temp_item A
  47.           INNER JOIN sl_do B ON A.tenant_id = B.tenant_id AND A.ref_id = B.do_id
  48.         WHERE A.tenant_id = pTenantId
  49.           AND A.invoice_temp_id = pInvoiceTempId
  50.         GROUP BY B.do_id
  51.         ORDER BY B.doc_no) Z;
  52.  
  53.     /* insert into table tr_sl_invoice_item*/
  54.     INSERT INTO tr_sl_invoice_item(
  55.         session_id, tenant_id, product_code, product_name, qty_so, uom_name,
  56.         nett_sell_price, gross_sell_price, nett_item_amount, gross_item_amount,
  57.         tax_price, tax_amount, warranty, warranty_type_code, warranty_type_name)
  58.     SELECT pSessionId, pTenantId, D.product_code, D.product_name, G.qty_dlv_so, E.uom_name,
  59.         G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id) AS nett_sell_price,
  60.         (G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id) + f_tax_rounding(G.tenant_id, G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id), sl_get_tax_pct_from_so_balance_inv_tax(pTenantId, G.ou_id, G.partner_id, G.ref_doc_type_id, G.ref_id, G.ref_item_id, G.do_receipt_item_id))) AS gross_sell_price,
  61.         (G.qty_dlv_so * (G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id))) AS nett_item_amount,
  62.         (f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id) + G.price_so + f_tax_rounding(G.tenant_id, G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id), sl_get_tax_pct_from_so_balance_inv_tax(pTenantId, G.ou_id, G.partner_id, G.ref_doc_type_id, G.ref_id, G.ref_item_id, G.do_receipt_item_id))) * G.qty_dlv_so AS gross_item_amount,
  63.         f_tax_rounding(G.tenant_id, G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id), sl_get_tax_pct_from_so_balance_inv_tax(pTenantId, G.ou_id, G.partner_id, G.ref_doc_type_id, G.ref_id, G.ref_item_id, G.do_receipt_item_id)) AS tax_price,
  64.         f_tax_rounding(G.tenant_id, G.price_so + f_get_warranty_sell_price_for_sales_invoice_temp(A.invoice_temp_id,A.ref_id,A.ref_item_id), sl_get_tax_pct_from_so_balance_inv_tax(pTenantId, G.ou_id, G.partner_id, G.ref_doc_type_id, G.ref_id, G.ref_item_id, G.do_receipt_item_id)) * G.qty_dlv_so AS tax_amount,
  65.         (f_get_product_warranty_service(C.product_id) :: integer) AS warranty, COALESCE(K.warranty_type_code, ''), COALESCE(K.warranty_type_name, '')
  66.     FROM sl_invoice_temp_item A
  67.         INNER JOIN sl_invoice_temp H ON A.tenant_id = H.tenant_id AND A.invoice_temp_id = H.invoice_temp_id  
  68.         INNER JOIN sl_do B ON A.tenant_id = B.tenant_id AND A.ref_id = B.do_id
  69.         INNER JOIN sl_do_item C ON A.tenant_id = C.tenant_id AND A.ref_item_id = C.do_item_id
  70.         INNER JOIN m_product d ON A.tenant_id = D.tenant_id AND C.product_id = D.product_id
  71.         INNER JOIN sl_so_balance_invoice G ON A.tenant_id = G.tenant_id AND G.ou_id = H.ou_id AND G.ref_doc_type_id = vDoDocTypeId AND G.ref_id = B.do_id AND G.so_id = B.ref_id AND G.ref_item_id = A.ref_item_id AND G.do_receipt_item_id = A.do_receipt_item_id
  72.         INNER JOIN m_uom E ON A.tenant_id = E.tenant_id AND G.so_uom_id = E.uom_id
  73.         INNER JOIN sl_do_item I ON I.do_item_id = G.ref_item_id
  74.         LEFT OUTER JOIN sl_so_warranty_item J ON J.so_item_id = I.ref_id
  75.         LEFT OUTER JOIN m_warranty_type K ON K.warranty_type_id = J.warranty_type_id
  76.     WHERE A.tenant_id = pTenantId
  77.         AND A.invoice_temp_id = pInvoiceTempId;
  78.        
  79.     /* get vFlgShowPpn */
  80.     SELECT (CASE WHEN (pFlgPkp = 'Y' AND B.flg_show_inv_tax = 'Y') THEN 'Y' ELSE 'N' END) INTO vFlgShowPpn
  81.     FROM sl_invoice_temp A
  82.         INNER JOIN sl_so B ON A.tenant_id = B.tenant_id AND A.ref_id = B.so_id
  83.     WHERE A.invoice_temp_id = pInvoiceTempId;
  84.  
  85.     /* get additional cost in invoice */
  86.     SELECT COALESCE(SUM(A.add_amount), 0) INTO vAdditionalCost
  87.     FROM sl_invoice_temp_cost A
  88.     WHERE A.tenant_id = pTenantId
  89.         AND A.invoice_temp_id = pInvoiceTempId;
  90.    
  91.     /* get down payment amount in invoice */
  92.     SELECT COALESCE(SUM(A.alloc_amount), 0), COALESCE(SUM(A.alloc_amount + f_tax_rounding(A.tenant_id, A.alloc_amount, B.tax_percentage)),0), COALESCE(SUM(f_tax_rounding(A.tenant_id, A.alloc_amount, B.tax_percentage)),0) INTO vDownPaymentAmount, vDownPaymentAmountWithTax, vTaxDownPaymentAmount
  93.     FROM sl_invoice_temp_advance A
  94.     INNER JOIN sl_so_balance_advance_invoice B ON A.tenant_id = B.tenant_id AND A.ref_doc_type_id = B.ref_doc_type_id AND A.ref_id = B.ref_id
  95.     WHERE A.tenant_id = pTenantId
  96.         AND A.invoice_temp_id = pInvoiceTempId;
  97.    
  98.     IF(vFlgShowPpn='Y') THEN
  99.         Open pRefHeader FOR
  100.             SELECT A.invoice_temp_id, A.ou_id, A.ou_legal_id, COALESCE(vOuBuInfoReport.address1 || ' ' || vOuBuInfoReport.address2 || ' ' || vOuBuInfoReport.address3, '-') AS ou_addr_1,
  101.                 COALESCE(vOuBuInfoReport.city || ', ' || vOuBuInfoReport.state_or_province || ', ' || vOuBuInfoReport.zip_code, '-') AS ou_addr_2, A.partner_id, C.partner_name,
  102.                 f_get_report_partner_address_for_vat_out(A.tenant_id, A.partner_id) AS partner_addr,
  103.                 A.doc_type_id, A.inv_doc_no AS so_invoice_no, A.inv_doc_date AS so_invoice_date, A.due_date, A.ref_id, E.doc_no AS so_no,
  104.                 E.ext_doc_no AS po_no, A.salesman_id, F.partner_name AS sales_name, A.curr_code AS currency,
  105.                 A.remark, (CASE WHEN A.curr_code = 'IDR' THEN 'rupiah' ELSE A.curr_code END) AS curr_name, vFlgShowPpn AS flg_show_ppn,
  106.                 SUM(G.nett_item_amount) AS sub_total, vAdditionalCost AS additional_cost, (SUM(G.tax_amount)-vTaxDownPaymentAmount) AS ppn, vDownPaymentAmount AS dp_amount,
  107.                 (((SUM(G.nett_item_amount)+vAdditionalCost)-(vDownPaymentAmount+vTaxDownPaymentAmount)) + SUM(G.tax_amount)) AS total_with_tax,
  108.                 ((SUM(G.nett_item_amount)+vAdditionalCost)-vDownPaymentAmount) AS total_no_tax, vDoNo AS do_no,
  109.                 lower(terbilang(((SUM(G.nett_item_amount)+vAdditionalCost)-(vDownPaymentAmount+vTaxDownPaymentAmount)) + SUM(G.tax_amount))) AS total_name,
  110.                 f_get_report_parameter_config_value(pTenantId, vAll, A.ou_id, 'printed_ou_name') AS ou_name,
  111.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_desc_1') AS bank_acc_desc_1,
  112.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_desc_2') AS bank_acc_desc_2,
  113.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_1') AS bank_acc_1,
  114.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_2') AS bank_acc_2,
  115.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_swift_code_1') AS bank_swift_code_1,
  116.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_swift_code_2') AS bank_swift_code_2,
  117.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_branch_1') AS bank_branch_1,
  118.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_branch_2') AS bank_branch_2,
  119.                 vOuBuInfoReport.image_url AS path_logo_ou
  120.             FROM sl_invoice_temp A
  121.                 --INNER JOIN t_ou_legal B ON A.tenant_id = B.tenant_id AND A.ou_legal_id = B.ou_legal_id
  122.                 INNER JOIN m_partner C ON A.tenant_id = C.tenant_id AND A.partner_id = C.partner_id
  123.                 INNER JOIN sl_so E ON A.tenant_id = E.tenant_id AND A.ref_id = E.so_id
  124.                 INNER JOIN m_partner F ON A.tenant_id = F.tenant_id AND A.salesman_id = F.partner_id
  125.                 INNER JOIN tr_sl_invoice_item G ON A.tenant_id = G.tenant_id AND G.session_id = pSessionId
  126.             WHERE A.tenant_id = pTenantId
  127.                 AND A.invoice_temp_id = pInvoiceTempId
  128.             GROUP BY A.invoice_temp_id, A.ou_id, A.ou_legal_id, vOuBuInfoReport.address1, vOuBuInfoReport.address2, vOuBuInfoReport.address3, vOuBuInfoReport.city, vOuBuInfoReport.state_or_province, vOuBuInfoReport.zip_code,
  129.                     A.partner_id, C.partner_name, A.tenant_id, A.doc_type_id, A.doc_no, A.doc_date, A.due_date, A.ref_id, E.doc_no,
  130.                     E.ext_doc_no, A.salesman_id, F.partner_name, A.curr_code, A.remark, vFlgShowPpn, vAdditionalCost, vDownPaymentAmount, vDoNo;
  131.         RETURN NEXT pRefHeader;
  132.    
  133.         Open pRefDetail FOR
  134.             SELECT product_code, product_name, SUM(qty_so) AS qty, uom_name,
  135.                nett_sell_price AS unit_price, SUM(nett_item_amount) AS amount, warranty, warranty_type_code, warranty_type_name
  136.             FROM tr_sl_invoice_item
  137.             WHERE session_id = pSessionId
  138.                 AND tenant_id = pTenantId
  139.             GROUP BY product_code, product_name, uom_name, nett_sell_price, warranty, warranty_type_code, warranty_type_name
  140.             HAVING SUM(qty_so) > 0
  141.             ORDER BY product_code, product_name;
  142.         RETURN NEXT pRefDetail;
  143.     ELSE
  144.         Open pRefHeader FOR
  145.             SELECT A.invoice_temp_id, A.ou_id, A.ou_legal_id, COALESCE(vOuBuInfoReport.address1 || ' ' || vOuBuInfoReport.address2 || ' ' || vOuBuInfoReport.address3, '-') AS ou_addr_1,
  146.                 COALESCE(vOuBuInfoReport.city || ', ' || vOuBuInfoReport.state_or_province || ', ' || vOuBuInfoReport.zip_code, '-') AS ou_addr_2, A.partner_id, C.partner_name,
  147.                 f_get_report_partner_address_for_vat_out(A.tenant_id, A.partner_id) AS partner_addr,
  148.                 A.doc_type_id, A.inv_doc_no AS so_invoice_no, A.inv_doc_date AS so_invoice_date, A.due_date, A.ref_id, E.doc_no AS so_no,
  149.                 E.ext_doc_no AS po_no, A.salesman_id, F.partner_name AS sales_name, A.curr_code AS currency,
  150.                 A.remark, (CASE WHEN A.curr_code = 'IDR' THEN 'rupiah' ELSE A.curr_code END) AS curr_name, vFlgShowPpn AS flg_show_ppn,
  151.                 SUM(G.gross_item_amount) AS sub_total, vAdditionalCost AS additional_cost, SUM(G.tax_amount) AS ppn, vDownPaymentAmountWithTax AS dp_amount,
  152.                 (SUM(G.gross_item_amount) - vDownPaymentAmountWithTax) AS total_with_tax, SUM(G.nett_item_amount) AS total_no_tax, vDoNo AS do_no,
  153.                 lower(terbilang(SUM(G.gross_item_amount) - vDownPaymentAmountWithTax)) AS total_name,
  154.                 f_get_report_parameter_config_value(pTenantId, vAll, A.ou_id, 'printed_ou_name') AS ou_name,
  155.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_desc_1') AS bank_acc_desc_1,
  156.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_desc_2') AS bank_acc_desc_2,
  157.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_1') AS bank_acc_1,
  158.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_acc_2') AS bank_acc_2,
  159.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_swift_code_1') AS bank_swift_code_1,
  160.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_swift_code_2') AS bank_swift_code_2,
  161.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_branch_1') AS bank_branch_1,
  162.                 f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'bank_branch_2') AS bank_branch_2,
  163.                 vOuBuInfoReport.image_url AS path_logo_ou
  164.             FROM sl_invoice_temp A
  165.                 --INNER JOIN t_ou_legal B ON A.tenant_id = B.tenant_id AND A.ou_legal_id = B.ou_legal_id
  166.                 INNER JOIN m_partner C ON A.tenant_id = C.tenant_id AND A.partner_id = C.partner_id
  167.                 INNER JOIN sl_so E ON A.tenant_id = E.tenant_id AND A.ref_id = E.so_id
  168.                 INNER JOIN m_partner F ON A.tenant_id = F.tenant_id AND A.salesman_id = F.partner_id
  169.                 INNER JOIN tr_sl_invoice_item G ON A.tenant_id = G.tenant_id AND G.session_id = pSessionId
  170.             WHERE A.tenant_id = pTenantId
  171.                 AND A.invoice_temp_id = pInvoiceTempId
  172.             GROUP BY A.invoice_temp_id, A.ou_id, A.ou_legal_id, vOuBuInfoReport.address1, vOuBuInfoReport.address2, vOuBuInfoReport.address3, vOuBuInfoReport.city, vOuBuInfoReport.state_or_province, vOuBuInfoReport.zip_code,
  173.                     A.partner_id, C.partner_name, A.tenant_id, A.doc_type_id, A.doc_no, A.doc_date, A.due_date, A.ref_id, E.doc_no,
  174.                     E.ext_doc_no, A.salesman_id, F.partner_name, A.curr_code, A.remark, vFlgShowPpn, vAdditionalCost, vDownPaymentAmountWithTax, vDoNo;
  175.         RETURN NEXT pRefHeader;
  176.        
  177.         Open pRefDetail FOR
  178.             SELECT product_code, product_name, SUM(qty_so) AS qty, uom_name,
  179.                gross_sell_price AS unit_price, SUM(gross_item_amount) AS amount, warranty, warranty_type_code, warranty_type_name
  180.             FROM tr_sl_invoice_item
  181.             WHERE session_id = pSessionId
  182.                 AND tenant_id = pTenantId
  183.             GROUP BY product_code, product_name, uom_name, gross_sell_price, warranty, warranty_type_code, warranty_type_name
  184.             HAVING SUM(qty_so) > 0
  185.             ORDER BY product_code, product_name;
  186.         RETURN NEXT pRefDetail;
  187.     END IF;
  188.    
  189.     DELETE FROM tr_sl_invoice_item WHERE session_id = pSessionId;
  190.  
  191. END;
  192. $BODY$
  193.   LANGUAGE plpgsql VOLATILE
  194.   COST 100
  195.   ROWS 1000;
  196. ALTER FUNCTION r_print_sales_invoice_temporary(character varying, bigint, bigint, bigint, character varying, bigint, character varying)
  197.   OWNER TO sts;
Add Comment
Please, Sign In to add comment