Advertisement
henikseptiana15

r_ledger_card_report

Jan 31st, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. CREATE OR REPLACE FUNCTION r_ledger_card_report(character varying, bigint, bigint, bigint, character varying, bigint, bigint, bigint, bigint, character varying, character varying, character varying, character varying, character varying, character varying)
  3.   RETURNS SETOF refcursor AS
  4. $BODY$
  5. DECLARE
  6.     pRefHeader          REFCURSOR := 'refHeader';
  7.     pRefDetail          REFCURSOR := 'refDetail';
  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.     pOuBuId             ALIAS FOR $6;
  14.     pOuBranchId         ALIAS FOR $7;
  15.     pOuSubBuId          ALIAS FOR $8;
  16.     pGroupCoaId         ALIAS FOR $9;
  17.     pOuRc               ALIAS FOR $10;
  18.     pSegment            ALIAS FOR $11;
  19.     pPeriodFrom         ALIAS FOR $12;
  20.     pPeriodTo           ALIAS FOR $13;
  21.     pMainAcc            ALIAS FOR $14;
  22.     pSubAcc             ALIAS FOR $15;
  23.    
  24.     vEmptyId            bigint := -99;
  25.     vEmptyString        character varying := '';
  26.     vEmptyAmount        numeric := 0;
  27.     vYes                character varying := 'Y';
  28.     vStatusDocTrx       character varying := '';
  29.     vStatusDocDraft     character varying := 'D';
  30.     vStatusDocRelease   character varying := 'R';
  31.     vSignDebt           character varying := 'D';
  32.     vSignCredit         character varying := 'C';
  33.     vTypeRateCom        character varying := 'COM';
  34.     vCurrGL             character varying(5);
  35.     vJoinOuRc           text := '';
  36.     vJoinSegment        text := '';
  37.     vFilterBranch       text := '';
  38.     vFilterSubBu        text := '';
  39.     vFilterGroupCoa     text := '';
  40.     vFilterOuRc         text := '';
  41.     vFilterSegment      text := '';
  42.     vFilterCoaMainAcc   text := '';
  43.     vFilterCoaSubAcc    text := '';
  44.     vRoundingDigit      integer;
  45.     vTypeSaldoAwal      character varying := 'SALDO AWAL';
  46.    
  47. BEGIN
  48.     vCurrGL := f_get_value_system_config_by_param_code(pTenantId,'ValutaBuku');
  49.    
  50.     DELETE FROM tr_gl_beginning_balance_ledger_card WHERE session_id = pSessionId;
  51.     DELETE FROM tr_gl_detail_ledger_card WHERE session_id = pSessionId;
  52.    
  53.     /* ambil nilai pembulatan*/
  54.     vRoundingDigit := CAST(f_get_value_system_config_by_param_code(pTenantId,'rounding.gl.amount') AS integer);
  55.    
  56.     /* vJoinOuRc, jika ouRc diisi */
  57.     IF (pOuRc <> vEmptyString) THEN
  58.         vJoinOuRc := '  INNER JOIN t_ou K ON B.ou_rc_id = K.ou_id
  59.                         INNER JOIN t_ou_type L ON K.ou_type_id = L.ou_type_id AND L.flg_accounting = '''|| vYes || '''';
  60.     END IF;
  61.    
  62.     /* vJoinSegment, jika segment diisi*/
  63.     IF (pSegment <> vEmptyString) THEN
  64.         vJoinSegment := ' INNER JOIN m_segment_coa M ON B.segmen_id = M.segment_coa_id ';
  65.     END IF;
  66.    
  67.     /* branchId <> -99*/
  68.     IF (pOuBranchId <> vEmptyId) THEN
  69.         vFilterBranch := ' AND B.ou_branch_id = ' || pOuBranchId;
  70.     END IF;
  71.    
  72.     /* subBuId <> -99*/
  73.     IF (pOuSubBuId <> vEmptyId) THEN
  74.         vFilterSubBu := ' AND B.ou_sub_bu_id =  ' || pOuSubBuId;
  75.     END IF;
  76.    
  77.     /* gorupCoaId <> -99*/
  78.     IF (pGroupCoaId <> vEmptyId) THEN
  79.         vFilterGroupCoa := ' AND E.group_coa_id = ' || pGroupCoaId;
  80.     END IF;
  81.    
  82.     /* jika ouRc diisi*/
  83.     IF (pOuRc <> vEmptyString) THEN
  84.         vFilterOuRc := ' AND UPPER(K.ou_name) LIKE UPPER(''%' ||  pOuRc || '%'')';
  85.     END IF;
  86.    
  87.     /* jika segment diisi*/
  88.     IF (pSegment <> vEmptyString) THEN
  89.         vFilterSegment := ' AND UPPER(M.segment_coa_name) LIKE UPPER(''%' || pSegment || '%'')';
  90.     END IF;
  91.    
  92.     /* jika main acc diisi*/
  93.     IF (pMainAcc <> vEmptyString) THEN
  94.         vFilterCoaMainAcc := ' AND E.main_acc LIKE ''' || pMainAcc || '%''';
  95.     END IF;
  96.  
  97.     /* jika sub acc diisi*/
  98.     IF (pSubAcc <> vEmptyString) THEN
  99.         vFilterCoaSubAcc := ' AND E.sub_acc LIKE ''' || pSubAcc || '%''';
  100.     END IF;
  101.    
  102.     /*
  103.      * ambil saldo awal ledger card
  104.      */
  105.     PERFORM gl_get_ledger_card_beginning_balance(pSessionId, pTenantId, pUserId, pRoleId, pDatetime, pOuBuId, pOuBranchId, pOuSubBuId, pGroupCoaId, pOuRc, pSegment, pPeriodFrom, pMainAcc, pSubAcc);
  106.    
  107.     /* ambil data detail yang, status doc = R
  108.      * 1) Ada transaksi tapi tidak ada saldo
  109.      * 2) Ada transaksi dan ada saldo awal
  110.      */
  111.     EXECUTE '
  112.     INSERT INTO tr_gl_detail_ledger_card(
  113.            session_id, tenant_id, ou_bu_id, doc_type_id, doc_type_desc,
  114.            doc_id, doc_no, doc_date, sign_journal, curr_code_trx, amount_trx,
  115.            journal_date, numerator_rate, denominator_rate, gl_curr_code,
  116.            gl_amount, ou_branch_id, branch_name, ou_sub_bu_id, sub_bu_name,
  117.            ou_rc_id, rc_name, segment_id, segment_name, coa_id, main_acc,
  118.            sub_acc, beginning_balance, sign_coa, coa_desc, remark, order_num)
  119.     WITH gl_journal_trx_details AS (
  120.         SELECT a.journal_trx_id, a.amount, a.gl_amount, a.sign_journal,
  121.               a.curr_code, a.journal_date, a.numerator_rate, a.denominator_rate,
  122.               a.gl_curr_code, a.ou_rc_id, a.segmen_id, a.coa_id, a.remark,
  123.               CASE WHEN a.ou_branch_id = $7 AND a.ou_sub_bu_id = $7 THEN x.ou_branch_id ELSE a.ou_branch_id END AS ou_branch_id,
  124.               CASE WHEN a.ou_branch_id = $7 AND a.ou_sub_bu_id = $7 THEN x.ou_sub_bu_id ELSE a.ou_sub_bu_id END AS ou_sub_bu_id    
  125.         FROM gl_journal_trx_item a
  126.         INNER JOIN gl_journal_trx x ON a.journal_trx_id = x.journal_trx_id
  127.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  128.         WHERE a.tenant_id = $2
  129.         AND p.year_month_date BETWEEN $3 AND $4
  130.           UNION ALL
  131.         SELECT b.journal_trx_id, b.amount, b.gl_amount, b.sign_journal,
  132.               b.curr_code, b.journal_date, b.numerator_rate, b.denominator_rate,
  133.               b.gl_curr_code, b.ou_rc_id, b.segmen_id, b.coa_id, b.remark,
  134.               CASE WHEN b.ou_branch_id = $7 AND b.ou_sub_bu_id = $7 THEN x.ou_branch_id ELSE b.ou_branch_id END AS ou_branch_id,
  135.               CASE WHEN b.ou_branch_id = $7 AND b.ou_sub_bu_id = $7 THEN x.ou_sub_bu_id ELSE b.ou_sub_bu_id END AS ou_sub_bu_id
  136.         FROM gl_journal_trx_mapping b
  137.         INNER JOIN gl_journal_trx x ON b.journal_trx_id = x.journal_trx_id  
  138.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  139.         WHERE b.tenant_id = $2
  140.         AND p.year_month_date BETWEEN $3 AND $4
  141.           UNION ALL
  142.         SELECT c.journal_trx_id, c.amount, c.gl_amount, c.sign_journal,
  143.               c.curr_code, c.journal_date, c.numerator_rate, C.denominator_rate,
  144.               c.gl_curr_code, c.ou_rc_id, c.segmen_id, c.coa_id, c.remark,
  145.               CASE WHEN c.ou_branch_id = $7 AND c.ou_sub_bu_id = $7 THEN x.ou_branch_id ELSE c.ou_branch_id END AS ou_branch_id,
  146.               CASE WHEN c.ou_branch_id = $7 AND c.ou_sub_bu_id = $7 THEN x.ou_sub_bu_id ELSE c.ou_sub_bu_id END AS ou_sub_bu_id
  147.         FROM gl_journal_trx_fx c
  148.         INNER JOIN gl_journal_trx x ON c.journal_trx_id = x.journal_trx_id
  149.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  150.         WHERE c.tenant_id = $2
  151.         AND p.year_month_date BETWEEN $3 AND $4
  152.     )
  153.    SELECT $1, $2, A.ou_bu_id, A.doc_type_id, f_get_doc_desc(A.doc_type_id) AS doc_type_desc,
  154.             A.doc_id, A.doc_no, A.doc_date, B.sign_journal, B.curr_code, B.amount,
  155.             B.journal_date, B.numerator_rate, B.denominator_rate, B.gl_curr_code,
  156.             B.gl_amount, B.ou_branch_id, f_get_ou_name(B.ou_branch_id) AS branch_name, B.ou_sub_bu_id,f_get_ou_name(B.ou_sub_bu_id) AS sub_bu_name,
  157.             B.ou_rc_id, f_get_ou_name(B.ou_rc_id) AS rc_name, B.segmen_id, f_get_segment_name(B.segmen_id) AS segment_name,
  158.             B.coa_id, E.main_acc, E.sub_acc, COALESCE(C.beginning_balance, 0) AS beginning_balance, E.sign_coa, E.coa_desc,
  159.             CASE WHEN (B.remark is null OR TRIM(B.remark) = '''') THEN A.remark ELSE B.remark END AS remark, 2
  160.     FROM gl_journal_trx A
  161.     INNER JOIN gl_journal_trx_details B ON A.journal_trx_id = B.journal_trx_id
  162.     INNER JOIN m_coa E ON B.coa_id = E.coa_id' ||
  163.     vJoinOuRc ||
  164.     vJoinSegment ||
  165.     ' LEFT OUTER JOIN tr_gl_beginning_balance_ledger_card C ON A.ou_bu_id = C.ou_bu_id AND A.ou_branch_id = C.ou_branch_id
  166.         AND A.ou_sub_bu_id = C.ou_sub_bu_id AND B.coa_id = C.coa_id AND B.ou_rc_id = C.ou_rc_id AND B.segmen_id = C.segment_id
  167.         AND C.session_id = $1
  168.     WHERE A.tenant_id = $2
  169.         AND A.status_doc = $5
  170.         AND A.ou_bu_id = $6' ||
  171.         vFilterBranch ||
  172.         vFilterSubBu ||
  173.         vFilterGroupCoa ||
  174.         vFilterOuRc ||
  175.         vFilterSegment ||
  176.         vFilterCoaMainAcc ||
  177.         vFilterCoaSubAcc ||
  178.     ' ORDER BY A.doc_no, A.doc_date ' USING pSessionId, pTenantId, pPeriodFrom, pPeriodTo, vStatusDocRelease, pOuBuId, vEmptyId;
  179.    
  180.     /* ambil data detail, status doc = D */
  181.     EXECUTE '
  182.     INSERT INTO tr_gl_detail_ledger_card(
  183.            session_id, tenant_id, ou_bu_id, doc_type_id, doc_type_desc,
  184.            doc_id, doc_no, doc_date, sign_journal, curr_code_trx, amount_trx,
  185.            journal_date, numerator_rate, denominator_rate, gl_curr_code,
  186.            gl_amount, ou_branch_id, branch_name, ou_sub_bu_id, sub_bu_name,
  187.            ou_rc_id, rc_name, segment_id, segment_name, coa_id, main_acc,
  188.            sub_acc, beginning_balance, sign_coa, coa_desc, remark, order_num)
  189.     WITH gl_journal_trx_details AS (
  190.         SELECT a.journal_trx_id, a.amount, a.gl_amount, a.sign_journal,
  191.               a.curr_code, a.journal_date, a.numerator_rate, a.denominator_rate,
  192.               a.gl_curr_code, a.ou_rc_id, a.segmen_id, a.coa_id, a.remark, a.type_rate,
  193.               CASE WHEN a.ou_branch_id = $10 AND a.ou_sub_bu_id = $10 THEN x.ou_branch_id ELSE a.ou_branch_id END AS ou_branch_id,
  194.               CASE WHEN a.ou_branch_id = $10 AND a.ou_sub_bu_id = $10 THEN x.ou_sub_bu_id ELSE a.ou_sub_bu_id END AS ou_sub_bu_id  
  195.         FROM gl_journal_trx_item a
  196.         INNER JOIN gl_journal_trx x ON a.journal_trx_id = x.journal_trx_id
  197.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  198.         WHERE a.tenant_id = $2
  199.         AND p.year_month_date BETWEEN $3 AND $4
  200.           UNION ALL
  201.         SELECT b.journal_trx_id, b.amount, b.gl_amount, b.sign_journal,
  202.               b.curr_code, b.journal_date, b.numerator_rate, b.denominator_rate,
  203.               b.gl_curr_code, b.ou_rc_id, b.segmen_id, b.coa_id, b.remark, b.type_rate,
  204.               CASE WHEN b.ou_branch_id = $10 AND b.ou_sub_bu_id = $10 THEN x.ou_branch_id ELSE b.ou_branch_id END AS ou_branch_id,
  205.               CASE WHEN b.ou_branch_id = $10 AND b.ou_sub_bu_id = $10 THEN x.ou_sub_bu_id ELSE b.ou_sub_bu_id END AS ou_sub_bu_id  
  206.         FROM gl_journal_trx_mapping b
  207.         INNER JOIN gl_journal_trx x ON b.journal_trx_id = x.journal_trx_id  
  208.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  209.         WHERE b.tenant_id = $2
  210.         AND p.year_month_date BETWEEN $3 AND $4
  211.           UNION ALL
  212.         SELECT c.journal_trx_id, c.amount, c.gl_amount, c.sign_journal,
  213.               c.curr_code, c.journal_date, c.numerator_rate, C.denominator_rate,
  214.               c.gl_curr_code, c.ou_rc_id, c.segmen_id, c.coa_id, c.remark, $8,
  215.               CASE WHEN c.ou_branch_id = $10 AND c.ou_sub_bu_id = $10 THEN x.ou_branch_id ELSE c.ou_branch_id END AS ou_branch_id,
  216.               CASE WHEN c.ou_branch_id = $10 AND c.ou_sub_bu_id = $10 THEN x.ou_sub_bu_id ELSE c.ou_sub_bu_id END AS ou_sub_bu_id  
  217.         FROM gl_journal_trx_fx c
  218.         INNER JOIN gl_journal_trx x ON c.journal_trx_id = x.journal_trx_id
  219.         INNER JOIN dt_date p ON x.doc_date = p.string_date
  220.         WHERE c.tenant_id = $2
  221.         AND p.year_month_date BETWEEN $3 AND $4
  222.     ), tt_gl_admin_journal_rate AS (
  223.         SELECT A.type_rate, A.journal_date, A.curr_code, $9, f_get_exchange_rate($2, A.type_rate, A.journal_date, A.curr_code, $9) AS data_exchange_rate
  224.         FROM (
  225.             SELECT A.journal_date, A.type_rate, A.curr_code
  226.             FROM gl_journal_trx_details A
  227.             GROUP BY journal_date, type_rate, curr_code
  228.         ) A
  229.     )
  230.    SELECT $1, $2, A.ou_bu_id, A.doc_type_id, f_get_doc_desc(A.doc_type_id) AS doc_type_desc,
  231.             A.doc_id, A.doc_no, A.doc_date, B.sign_journal, B.curr_code, B.amount,
  232.             B.journal_date, (D.data_exchange_rate).numerator_rate AS numerator_rate, (D.data_exchange_rate).denominator_rate AS denominator_rate, CASE WHEN (B.gl_curr_code is null OR TRIM(B.gl_curr_code) = '''') THEN $9 ELSE B.gl_curr_code END gl_curr_code,
  233.             ROUND(B.amount * (D.data_exchange_rate).numerator_rate / (D.data_exchange_rate).denominator_rate, $7) AS gl_amount,
  234.             B.ou_branch_id, f_get_ou_name(B.ou_branch_id) AS branch_name, B.ou_sub_bu_id, f_get_ou_name(B.ou_sub_bu_id) AS sub_bu_name,
  235.             B.ou_rc_id, f_get_ou_name(B.ou_rc_id) AS rc_name, B.segmen_id, f_get_segment_name(B.segmen_id) AS segment_name,
  236.             B.coa_id, E.main_acc, E.sub_acc, COALESCE(C.beginning_balance, 0) AS beginning_balance, E.sign_coa, E.coa_desc,
  237.             CASE WHEN (B.remark is null OR TRIM(B.remark) = '''') THEN A.remark ELSE B.remark END AS remark, 2
  238.     FROM gl_journal_trx A
  239.     INNER JOIN gl_journal_trx_details B ON A.journal_trx_id = B.journal_trx_id
  240.     INNER JOIN m_coa E ON B.coa_id = E.coa_id
  241.     INNER JOIN tt_gl_admin_journal_rate D ON D.type_rate=B.type_rate AND B.journal_date=D.journal_date AND D.curr_code=B.curr_code'||
  242.     vJoinOuRc ||
  243.     vJoinSegment ||
  244.     ' LEFT OUTER JOIN tr_gl_beginning_balance_ledger_card C ON A.ou_bu_id = C.ou_bu_id AND A.ou_branch_id = C.ou_branch_id
  245.         AND A.ou_sub_bu_id = C.ou_sub_bu_id AND B.coa_id = C.coa_id AND B.ou_rc_id = C.ou_rc_id AND B.segmen_id = C.segment_id
  246.         AND C.session_id = $1
  247.     WHERE A.tenant_id = $2
  248.         AND A.status_doc = $5
  249.         AND A.ou_bu_id = $6' ||
  250.         vFilterBranch ||
  251.         vFilterSubBu ||
  252.         vFilterGroupCoa ||
  253.         vFilterOuRc ||
  254.         vFilterSegment ||
  255.         vFilterCoaMainAcc ||
  256.         vFilterCoaSubAcc ||
  257.     ' ORDER BY A.doc_no, A.doc_date ' USING pSessionId, pTenantId, pPeriodFrom, pPeriodTo, vStatusDocDraft, pOuBuId, vRoundingDigit, vTypeRateCom, vCurrGL, vEmptyId;
  258.    
  259.        
  260.     /* ambil data detail yang tidak ada transaksi tapi ada saldo */
  261.     INSERT INTO tr_gl_detail_ledger_card(
  262.             session_id, tenant_id, ou_bu_id, doc_type_id, doc_type_desc,
  263.             doc_id, doc_no, doc_date, sign_journal, curr_code_trx, amount_trx,
  264.             journal_date, gl_curr_code,
  265.             gl_amount, ou_branch_id, branch_name, ou_sub_bu_id, sub_bu_name,
  266.             ou_rc_id, rc_name, segment_id, segment_name,
  267.             coa_id, main_acc, sub_acc, beginning_balance, sign_coa, coa_desc, order_num, remark)
  268.     SELECT pSessionId, pTenantId, A.ou_bu_id, vEmptyId, vEmptyString,
  269.             vEmptyId, vEmptyString, vEmptyString, L.sign_coa, vEmptyString, vEmptyAmount,
  270.             vEmptyString, vEmptyString,
  271.             vEmptyAmount, A.ou_branch_id, f_get_ou_name(A.ou_branch_id) AS branch_name, A.ou_sub_bu_id, f_get_ou_name(A.ou_sub_bu_id) AS sub_bu_name,
  272.             A.ou_rc_id, f_get_ou_name(A.ou_rc_id) AS rc_name, A.segment_id, f_get_segment_name(A.segment_id) AS segment_name,
  273.             A.coa_id, L.main_acc, L.sub_acc, COALESCE(A.beginning_balance, 0) AS beginning_balance, L.sign_coa, L.coa_desc, 1, vEmptyString
  274.     FROM tr_gl_beginning_balance_ledger_card A
  275.     INNER JOIN m_coa L ON A.coa_id = L.coa_id
  276.     WHERE NOT EXISTS (SELECT 1
  277.                        FROM tr_gl_detail_ledger_card B
  278.                        WHERE A.ou_bu_id = B.ou_bu_id
  279.                             AND A.ou_branch_id = B.ou_branch_id
  280.                             AND A.ou_sub_bu_id = B.ou_sub_bu_id
  281.                             AND A.coa_id = B.coa_id
  282.                             AND A.ou_rc_id = B.ou_rc_id
  283.                             AND A.segment_id = B.segment_id
  284.                             AND A.session_id = B.session_id)
  285.     AND A.tenant_id = pTenantId
  286.     AND A.session_id = pSessionId;                     
  287.    
  288.     /*
  289.     /* Insert data beginning balance yang ada transaksinya ambil dari table tr_gl_detail_ledger_card yang order_num= 2 */
  290.     INSERT INTO tr_gl_detail_ledger_card(
  291.         session_id, tenant_id, ou_bu_id, doc_type_id, doc_type_desc,
  292.         doc_id, doc_no, doc_date, sign_journal, curr_code_trx, amount_trx,
  293.         journal_date, gl_curr_code,
  294.         gl_amount, ou_branch_id, branch_name, ou_sub_bu_id, sub_bu_name,
  295.         ou_rc_id, rc_name, segment_id, segment_name, coa_id, main_acc,
  296.         sub_acc, beginning_balance, sign_coa, coa_desc, order_num, remark)
  297.     WITH data_saldo_awal_with_transaksi AS (
  298.         SELECT A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.branch_name, A.ou_sub_bu_id, A.sub_bu_name,
  299.             A.ou_rc_id, A.rc_name, A.segment_id, A.segment_name, A.coa_id, A.main_acc,
  300.             A.sub_acc, A.beginning_balance, A.sign_coa, A.coa_desc
  301.         FROM tr_gl_detail_ledger_card A
  302.         WHERE A.session_id = pSessionId
  303.         AND A.tenant_id = pTenantId
  304.         AND A.order_num = 2
  305.         GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.branch_name, A.ou_sub_bu_id, A.sub_bu_name,
  306.             A.ou_rc_id, A.rc_name, A.segment_id, A.segment_name, A.coa_id, A.main_acc,
  307.             A.sub_acc, A.beginning_balance, A.sign_coa, A.coa_desc
  308.     )    
  309.     SELECT pSessionId, A.tenant_id, A.ou_bu_id, vEmptyId, vTypeSaldoAwal,
  310.         vEmptyId, vEmptyString, vEmptyString, A.sign_coa, vEmptyString, vEmptyAmount,
  311.         vEmptyString, vEmptyString,
  312.         A.beginning_balance, A.ou_branch_id, A.branch_name, A.ou_sub_bu_id, A.sub_bu_name,
  313.         A.ou_rc_id, A.rc_name, A.segment_id, A.segment_name, A.coa_id, A.main_acc,
  314.         A.sub_acc, A.beginning_balance, A.sign_coa, A.coa_desc, 1, vEmptyString
  315.     FROM data_saldo_awal_with_transaksi A
  316.     ORDER BY A.coa_id;                 
  317.     */
  318.            
  319.     /* header result*/
  320.     Open pRefHeader FOR
  321.     SELECT pOuBuId AS ou_id, f_get_ou_name(pOuBuId) AS ou_name, pPeriodFrom AS period_from, pPeriodTo AS period_to, f_get_username(pUserId) AS username, pDatetime AS datetime;
  322.     RETURN NEXT pRefHeader;
  323.    
  324.     Open pRefDetail FOR
  325.     SELECT A.doc_type_id, A.doc_type_desc, A.doc_no, A.doc_date, A.sign_journal, A.curr_code_trx, A.amount_trx AS amount_trx, A.journal_date,
  326.             A.numerator_rate, A.denominator_rate, A.sign_coa, A.gl_curr_code, A.gl_amount AS gl_amount, A.ou_branch_id, A.branch_name,
  327.             A.ou_sub_bu_id, A.sub_bu_name, A.ou_rc_id, A.rc_name, A.segment_id, A.segment_name, A.coa_id,
  328.             A.main_acc, A.sub_acc, A.beginning_balance, A.coa_desc, A.remark,
  329.             CASE WHEN A.sign_journal = vSignDebt THEN A.gl_amount ELSE 0 END AS gl_amount_debt,
  330.             CASE WHEN A.sign_journal = vSignCredit THEN A.gl_amount ELSE 0 END AS gl_amount_credit,
  331.             (A.curr_code_trx || to_char(A.denominator_rate, '999,999.90') || ' = ' || A.gl_curr_code || to_char(A.numerator_rate, '999,999.90')) AS rate, A.order_num
  332.     FROM tr_gl_detail_ledger_card A
  333.     WHERE A.session_id = pSessionId
  334.         AND A.tenant_id = pTenantId
  335.     --ORDER BY A.branch_name, A.sub_bu_name, A.main_acc, A.sub_acc, A.rc_name, A.segment_name, A.coa_desc, A.doc_date, A.doc_no, A.curr_code_trx, A.gl_curr_code;
  336.     ORDER BY A.branch_name, A.sub_bu_name, A.coa_id, A.rc_name, A.segment_name, A.order_num, A.doc_date, A.doc_type_desc, A.doc_no ;
  337.    
  338.     RETURN NEXT pRefDetail ;
  339.    
  340.     DELETE FROM tr_gl_beginning_balance_ledger_card WHERE session_id = pSessionId;
  341.     DELETE FROM tr_gl_detail_ledger_card WHERE session_id = pSessionId;
  342. END;
  343. $BODY$
  344.   LANGUAGE plpgsql VOLATILE
  345.   COST 100
  346.   ROWS 1000;
  347. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement