Advertisement
samuel025

Function Report Sales Invoice MF

May 5th, 2021
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION r_print_sales_invoice_for_mf(character varying, bigint, bigint, bigint, character varying, bigint)
  2.   RETURNS SETOF refcursor AS
  3. $BODY$
  4. DECLARE
  5.     pRefHeader              REFCURSOR := 'refHeader';
  6.     pRefDetail              REFCURSOR := 'refDetail';
  7.     pSessionId              ALIAS FOR $1;
  8.     pTenantId               ALIAS FOR $2;
  9.     pUserId                 ALIAS FOR $3;
  10.     pRoleId                 ALIAS FOR $4;
  11.     pDatetime               ALIAS FOR $5;
  12.     pInvoiceId              ALIAS FOR $6;
  13.  
  14.    
  15.     vDoDocTypeId                bigint;
  16.     vReportName                 character varying;
  17.     vDoNo                       character varying;
  18.     vAdditionalCost             numeric;
  19.     vDownPaymentAmount          numeric;
  20.     vDownPaymentAmountWithTax   numeric;
  21.     vTaxDownPaymentAmount       numeric;
  22.     vTotalAmount                numeric;
  23.     vFlgShowPpn                 character varying := 'Y';
  24.     vOuBuInfoReport             OU_INFO_REPORT%ROWTYPE;
  25.     result                      record;
  26.    
  27.     vRoundingModeTax            character varying(5);
  28.     vRoundingModeNonTax         character varying(5);
  29.     vRoundingScalePrintTotal    integer;
  30.     vNullRefId                  bigint := -99;
  31.     vEmptyString                character varying(1) :='';
  32.     vCtgrProductCode                character varying;
  33.     vCtgrProductName                character varying;
  34.     vYes                        character varying(1) :='Y';
  35.     vAll                        character varying;
  36.     vOuBuId                     bigint;
  37.     vOuPkp                      bigint;
  38.     vGovTaxAmount               numeric;
  39.  
  40.  
  41.  
  42.    
  43. BEGIN
  44.     vDoDocTypeId := 311;
  45.     vReportName := 'FormSalesInvoiceForMF';
  46.  
  47.      --> Ambil Ou PkP
  48.     SELECT COUNT(1) INTO vOuPkp
  49.     FROM sl_invoice A
  50.     INNER JOIN t_ou_legal B ON A.ou_id = B.ou_id
  51.     WHERE A.invoice_id = pInvoiceId
  52.         AND B.flg_pkp = 'Y' ;
  53.  
  54.     --> Ambil total amount
  55.     SELECT SUM(F.qty_dlv_so * F.price_so) AS total_amount INTO vTotalAmount
  56.     FROM sl_invoice_item  Z
  57.     INNER JOIN sl_invoice A ON Z.invoice_id = A.invoice_id
  58.     INNER JOIN sl_so B ON A.ref_id = B.so_id AND A.ref_doc_type_id = B.doc_type_id
  59.     INNER JOIN sl_do C ON B.so_id = C.ref_id
  60.     INNER JOIN sl_so_balance_invoice F ON A.tenant_id = F.tenant_id AND F.ou_id = A.ou_id AND F.ref_doc_type_id = vDoDocTypeId
  61.                 AND F.ref_id = C.do_id AND F.so_id = C.ref_id AND F.ref_item_id = Z.ref_item_id
  62.                 AND F.do_receipt_item_id = Z.do_receipt_item_id
  63.     WHERE A.tenant_id = pTenantId
  64.         AND A.invoice_id = pInvoiceId;
  65.  
  66.     Open pRefHeader FOR
  67.     SELECT  CASE WHEN vOuPkp = 1 THEN f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'ou_name_pkp')
  68.                     ELSE f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'ou_name_non_pkp') END AS ou_name,
  69.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'ou_phone') AS ou_phone,
  70.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'ou_mail') AS ou_mail,
  71.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'ou_web') AS ou_web,
  72.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'remark1') AS remark1,
  73.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'remark2') AS remark2,
  74.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'remark3') AS remark3,
  75.             f_get_report_parameter_config_value(pTenantId, vReportName, A.ou_id, 'remark4') AS remark4,
  76.             A.doc_no, A.doc_date, f_get_partner_name(C.partner_bill_to_id) AS customer_name, B.payment_amount AS payment,
  77.             (vTotalAmount - B.payment_amount) AS total_due
  78.     FROM sl_invoice A
  79.     INNER JOIN fi_allocation_ar_invoice B ON A.invoice_id = B.ref_id AND B.ref_doc_type_id = A.doc_type_id
  80.     INNER JOIN sl_so C ON A.ref_id = C.so_id
  81.     WHERE A.invoice_id = pInvoiceId;
  82.     RETURN NEXT pRefHeader;
  83.    
  84.     Open pRefDetail FOR
  85.     SELECT E.product_desc AS order, F.qty_dlv_so AS qty, F.price_so AS amount, (F.qty_dlv_so * F.price_so) AS sub_total,
  86.         D.partner_name AS partner_name, D.partner_address1 AS cust_address,
  87.         E.remark, C.doc_date AS doc_date, E.remark
  88.     FROM sl_invoice_item  Z
  89.     INNER JOIN sl_invoice A ON Z.invoice_id = A.invoice_id
  90.     INNER JOIN sl_so B ON A.ref_id = B.so_id AND A.ref_doc_type_id = B.doc_type_id
  91.     INNER JOIN sl_do C ON B.so_id = C.ref_id
  92.     INNER JOIN sl_quotation D ON B.ref_id = D.quotation_id
  93.     INNER JOIN sl_quotation_item E ON D.quotation_id = E.quotation_id
  94.     INNER JOIN sl_so_balance_invoice F ON A.tenant_id = F.tenant_id AND F.ou_id = A.ou_id AND F.ref_doc_type_id = vDoDocTypeId
  95.                 AND F.ref_id = C.do_id AND F.so_id = C.ref_id AND F.ref_item_id = Z.ref_item_id
  96.                 AND F.do_receipt_item_id = Z.do_receipt_item_id
  97.     WHERE A.tenant_id = pTenantId
  98.         AND A.invoice_id = pInvoiceId;
  99.  
  100.     RETURN NEXT pRefDetail;
  101.    
  102.  
  103. END;
  104. $BODY$
  105.   LANGUAGE plpgsql VOLATILE
  106.   COST 100
  107.   ROWS 1000;
  108.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement