Showing posts with label PL/SQL. Show all posts
Showing posts with label PL/SQL. Show all posts

Saturday, 22 September 2018

PROJECT UNBILLED BALANCES- FUSION

This report will display all project unbilled details.


SELECT   ppa.project_id, ppa.segment1 project_number, 
         NVL ((SELECT pt.project_type
               FROM pjf_project_types_tl pt, pjf_projects_all_vl p
              WHERE p.project_id(+) = ppa.project_id
                AND p.project_type_id = pt.project_type_id),
            'N/A'
           ) project_type,
ppt.NAME project_name,
         ppt.description description,
         (SELECT ppm.resource_source_name
            FROM pjf_proj_team_members_v ppm
           WHERE 1 = 1
             AND project_role_id = 1
             AND project_id = ppa.project_id) manager_name,
         haou.NAME organization_name, pih.invoice_currency_code currency_code,
         SUM (NVL (prd.revenue_curr_amt, 0)) proj_todate_revenue_amt,
         SUM (NVL (ccdl.acct_amount, 0)) proj_todate_inv_amt,
         SUM (NVL (pild.trns_curr_billed_amt, 0)) unbilled_amt,
         pih.ra_invoice_number Open_Ar_inv,
         (SELECT TO_CHAR (MIN (e.completion_date),
                          'DD-MON-YYYY',
                          'NLS_DATE_LANGUAGE = american'
                         ) next_event_date
            FROM pjb_billing_events e
           WHERE e.project_id = ppa.project_id
             AND e.bill_trns_amount <> 0
             AND (e.bill_hold_flag = 'N' OR e.completion_date >= SYSDATE))
                                                               next_bill_date
                                                               --:p_as_of_date
                                                                             ,
         (SELECT   TO_CHAR (MAX (i.invoice_date),
                            'DD-MON-YYYY',
                            'NLS_DATE_LANGUAGE = american'
                           ) max_proj_inv_date
              FROM pjb_rev_distributions i
             WHERE i.linked_project_id = ppa.project_id
               AND i.invoice_date <= SYSDATE                   --:p_as_of_date
          --AND i.invoice_status_code = 'ACCEPTED'
          GROUP BY linked_project_id) last_date_invoiced,
         TO_CHAR (ppa.completion_date,
                  'DD-MON-YYYY',
                  'NLS_DATE_LANGUAGE = american'
                 ) completion_date
    FROM pjc_exp_items_all pei,
         pjf_exp_types_tl pet,
         pjf_projects_all_b ppa,
         pjf_projects_all_tl ppt,
         pjf_project_statuses_tl pps,
         pjf_proj_elements_b ppeb,
         pjf_proj_elements_tl ppet,
         pjb_bill_trxs pbt,
         pjb_invoice_headers pih,
         pjb_invoice_lines pil,
         pjb_inv_line_dists pild,
         pjb_rev_distributions prd,
         pjc_xla_ccdl_lines_adj_v ccdl,
         hr_all_organization_units haou
   WHERE 1 = 1
     AND pei.expenditure_type_id = pet.expenditure_type_id
     AND pei.project_id = ppa.project_id
     AND pei.project_id = ppt.project_id
     AND ppeb.project_id = ppt.project_id
     AND ppa.project_status_code = pps.project_status_code
     --AND pcpl.proj_element_id    =    ppeb.proj_element_id
     --AND pcpl.proj_element_id    =    ppet.proj_element_id
     AND pbt.linked_project_id = ppa.project_id
     AND pbt.transaction_id = pei.expenditure_item_id
     AND pild.bill_trx_id = pbt.bill_trx_id
     AND pil.invoice_line_id = pild.invoice_line_id
     AND pih.invoice_id = pild.invoice_id
     AND pih.invoice_id = pil.invoice_id
     AND prd.bill_trx_id = pbt.bill_trx_id
     AND prd.revenue_rate_source_id = pbt.revenue_rate_source_id
     AND pei.expenditure_item_id = ccdl.expenditure_item_id(+)
     AND ppeb.carrying_out_organization_id = haou.organization_id
     --AND ppa.segment1 = '11287'
GROUP BY ppa.project_id,
         ppa.segment1,
         ppt.NAME,
         ppt.description,
         haou.NAME,
         pih.invoice_currency_code,
         ppa.completion_date,

         pih.ra_invoice_number 

HCKT Hard Limit Reached Events in Fusion


Purpose of the report is to display project hours and hard-limit amount with interactive mode.


with XXANSR_PA_EVENTS_cte as (SELECT evt.ROWID as "ROW_ID",
          evt.event_id as "EVENT_ID",
          evt.project_id as "PROJECT_ID",
          prj.segment1 as "PROJECT_NUMBER",
          prjt.name as "PROJECT_NAME",
          prjt.description as "DESCRIPTION_MIR",
          evt.task_id as "TASK_ID",
          tsk.task_number as "TASK_NUMBER",
          tsk.task_name as "TASK_NAME",
          evt.event_num as "EVENT_NUM",
          --evt.event_type_code as "EVENT_TYPE",
          typl.event_type_name as "EVENT_TYPE",
          --DPL typ.event_type_classification as "EVENT_TYPE_CLASSIFICATION",
          evt.completion_date as "EVENT_DATE",
          evt.event_desc as "DESCRIPTION",
          prj.org_id as "OPERATING_UNIT",
          prj.carrying_out_organization_id as "PROJECT_ORG_ID",
          (SELECT name FROM hr_organization_units WHERE organization_id = prj.carrying_out_organization_id) as "PROJECT_OWNING_ORG",
          evt.organization_id as "ORGANIZATION_ID",
          hou.name as "ORGANIZATION_NAME",
          ctl.hard_limit_amount,
          --DPL evt.bill_amount as "BILL_AMOUNT",
          evt.revenue_amt as "REVENUE_AMOUNT",
          evt.bill_hold_flag as "BILL_HOLD_FLAG",
          --DPL evt.revenue_distributed_flag as "REVENUE_DISTRIBUTED_FLAG",
          --DPL prj.project_level_funding_flag as "PROJECT_LEVEL_FUNDING_FLAG",
          evt.attribute_category as "ATTRIBUTE_CATEGORY",
          evt.attribute1 as "ATTRIBUTE1",
          evt.attribute2 as "ATTRIBUTE2",
          evt.attribute3 as "ATTRIBUTE3",
          evt.attribute4 as "ATTRIBUTE4",
          evt.attribute5 as "ATTRIBUTE5",
          evt.attribute6 as "ATTRIBUTE6",
          evt.attribute7 as "ATTRIBUTE7",
          evt.attribute8 as "ATTRIBUTE8",
          evt.attribute9 as "ATTRIBUTE9",
          evt.attribute10 as "ATTRIBUTE10",
          --DPL evt.inventory_org_id,
          --DPL evt.inventory_item_id as "INVENTORY_ITEM_ID",
          evt.quantity_billed as "QUANTITY_BILLED",
          evt.uom_code as "UOM_CODE",
          evt.unit_price as "UNIT_PRICE",
          evt.reference1 as "REFERENCE1",
          evt.reference2 as "REFERENCE2",
          evt.reference3 as "REFERENCE3",
          evt.reference4 as "REFERENCE4",
          evt.reference5 as "REFERENCE5",
          evt.reference6 as "REFERENCE6",
          evt.reference7 as "REFERENCE7",
          evt.reference8 as "REFERENCE8",
          evt.reference9 as "REFERENCE9",
          evt.reference10 as "REFERENCE10",
          evt.last_update_date as "LAST_UPDATE_DATE",
          evt.last_updated_by as "LAST_UPDATED_BY",
          evt.creation_date as "CREATION_DATE",
          evt.created_by as "CREATED_BY",
          evt.last_update_login as "LAST_UPDATE_LOGIN",
          evt.bill_trns_currency_code as "BILL_TRANS_CURRENCY_CODE",
          evt.bill_trns_amount as "BILL_TRANS_AMOUNT",
          evt.contract_curr_amt as "BILL_TRANS_REV_AMOUNT",
          evt.contract_curr_code as "CONTRACT_CURR_CODE",
          evt.contract_curr_rate_type as "CONTRACT_CUUR_RATE_TYPE",
          --DPL c.user_conversion_type as "PROJECT_RATE_TYPE_NAME",
          evt.contract_curr_exchg_rate as "CONTRACT_CURR_EXCHG_RATE",
          evt.contract_curr_exchg_date as "CONTRACT_CURR_EXCHG_DATE",
          --DPL evt.project_bill_amount as "PROJFUNC_BILL_AMOUNT",
          --DPL evt.project_inv_exchange_rate as "PROJECT_INV_EXCHANGE_RATE",
          --DPL evt.project_inv_rate_date as "PROJECT_INV_RATE_DATE",
          evt.project_revenue_amt as "PROJECT_REVENUE_AMT",
          --DPL evt.project_rev_exchange_rate as "PROJECT_REV_EXCHANGE_RATE",
          --DPL evt.project_rev_rate_date as "PROJECT_REV_RATE_DATE",
          --DPL evt.projfunc_rev_rate_date,
          --DPL evt.projfunc_rate_type as "PROJFUNC_RATE_TYPE",
          --DPL c1.user_conversion_type as "PROJFUNC_RATE_TYPE_NAME",
          --DPL evt.projfunc_rate_date as "PROJFUNC_RATE_DATE",
          --DPL evt.projfunc_exchange_rate as "PROJFUNC_EXCHANGE_RATE",
          --DPL evt.projfunc_bill_amount,
          --DPL evt.projfunc_inv_exchange_rate,
          --DPL evt.projfunc_inv_rate_date,
          --DPL evt.projfunc_revenue_amount,
          --DPL evt.projfunc_rev_exchange_rate as "PROJFUNC_REV_EXCHANGE_RATE",
          --DPL evt.projfunc_rev_rate_date as "PROJFUNC_REV_RATE_DATE",
          --DPL evt.funding_rate_type as "FUNDING_RATE_TYPE",
          --DPL c2.user_conversion_type as "FUNDING_RATE_TYPE_NAME",
          --DPL evt.funding_rate_date as "FUNDING_RATE_DATE",
          --DPL evt.funding_exchange_rate as "FUNDING_EXCHANGE_RATE",
          evt.invoice_currency_code as "INVOICE_CURRENCY_CODE",
          evt.invoice_curr_rate_type as "INVOICE_CURR_RATE_TYPE",
          evt.invoice_curr_exchg_date as "INVOICE_CURR_EXCHG_DATE",
          evt.invoice_curr_exchg_rate as "INVOICE_CURR_EXCHG_RATE",
          --DPL evt.revproc_currency_code as "REVPROC_CURRENCY_CODE",
          --DPL evt.revproc_rate_type as "REVPROC_RATE_TYPE",
          --DPL evt.revproc_rate_date as "REVPROC_RATE_DATE",
          --DPL evt.revproc_exchange_rate as "REVPROC_EXCHANGE_RATE",
          --DPL evt.inv_gen_rejection_code as "INV_GEN_REJECTION_CODE",
          --DPL pl1.meaning as "INV_GEN_REJECTION_REASON",
          --DPL pl.meaning as "REV_GEN_REJECTION_REASON",
          prj.project_currency_code, -- as "PROJ_LEVEL_CURR_CODE",
          --DPL prj.project_bil_rate_date_code as "PROJ_LEVEL_RT_DT_COD",
          --DPL prj.project_bil_rate_type as "PROJ_LEVEL_RT_TYP",
          --DPL prj.project_bil_rate_date as "PROJ_LEVEL_RT_DT",
          --DPL prj.project_bil_exchange_rate as "PROJ_LEVEL_EXCH_RT",
          --DPL prj.projfunc_currency_code as "PROJ_LEVEL_FUNC_CURR_COD",
          --DPL prj.projfunc_bil_rate_date_code as "PROJ_LEVEL_FUNC_RT_DT_COD",
          --DPL prj.projfunc_bil_rate_type as "PROJ_LEVEL_FUNC_RT_TYP",
          --DPL prj.projfunc_bil_rate_date as "PROJ_LEVEL_FUNC_RT_DT",
          --DPL prj.projfunc_bil_exchange_rate as "PROJ_LEVEL_FUNC_EXCH_RT",
          --DPL prj.funding_rate_date_code as "PROJ_LEVEL_FUND_RT_DT_COD",
          --DPL prj.funding_rate_type as "PROJ_LEVEL_FUND_RT_TYP",
          --DPL prj.funding_rate_date as "PROJ_LEVEL_FUND_RT_DT",
          --DPL prj.funding_exchange_rate as "PROJ_LEVEL_FUND_EXCH_RT",
          --DPL prj.revproc_currency_code as "PROJ_LEVEL_REVPRO_CURR_COD",
          --DPL prj.invproc_currency_type as "PROJ_LEVEL_INVPRO_CURR_TYP",
          --DPL prj.multi_currency_billing_flag as "MULTI_CURRENCY_BILLING_FLAG",
          prj.project_status_code as "PROJECT_STATUS_CODE",
          --DPL evt.adjusting_revenue_flag as "ADJUSTING_REVENUE_FLAG",
          --DPL evt.zero_revenue_amount_flag as "ZERO_REVENUE_AMOUNT_FLAG",
          evt.non_updateable_flag as "NON_UPDATEABLE_FLAG",
          evt.audit_cost_plan_type_id as "AUDIT_COST_PLAN_TYPE_ID",
          evt.audit_rev_plan_type_id as "AUDIT_REV_PLAN_TYPE_ID",
          evt.revenue_hold_flag as "REVENUE_HOLD_FLAG",
          NULL as "REP_PROJFUNC_CURRENCY_CODE",
          NULL as "REP_PROJFUNC_RATE_TYPE",
          NULL as "REP_PROJFUNC_RATE_TYPE_NAME",
          TO_DATE(NULL) as "REP_PROJFUNC_RATE_DATE",
          TO_NUMBER(NULL) as "REP_PROJFUNC_EXCHANGE_RATE",
          TO_DATE(NULL) as "REP_PROJFUNC_REV_RATE_DATE",
          TO_NUMBER(NULL) as "REP_PROJFUNC_REV_EXCHANGE_RATE",
          TO_NUMBER(NULL) as "REP_PROJFUNC_REVENUE_AMOUNT",
          TO_DATE(NULL) as "REP_PROJFUNC_INV_RATE_DATE",
          TO_NUMBER(NULL) as "REP_PROJFUNC_INV_EXCHANGE_RATE",
          TO_NUMBER(NULL) as "REP_PROJFUNC_BILL_AMOUNT",
          --DPL evt.billed_flag as "BILLED_FLAG"
          prj.pm_product_code as "PM_PRODUCT_CODE",
  okh.contract_number "CONTRACT_NUMBER",
  okl.line_number "CONTRACT_LINE_NUMBER",
  flv.meaning "REVENUE_STATUS",
  evt.revenue_exception_flag "REVENUE_EXCEPTION",
  Round(evt.revenue_recognzd_percentage,2) "REVENUE_PERCENTAGE"
          --DPL prj.pm_event_reference as "PM_EVENT_REFERENCE"
          --DPL pl2.meaning as "PM_PRODUCT_CODE_DISP"
     FROM PJF_PROJECTS_ALL_B prj
    inner join PJF_PROJECTS_ALL_TL prjt
       on prjt.project_id = prj.project_id
    inner join OKC_K_HEADERS_ALL_B okh
       on okh.contract_number = prj.segment1
inner join OKC_K_LINES_B okl
   on okh.major_version=okl.major_version
   AND okh.id=okl.dnz_chr_id
    inner join PJB_BILLING_CONTROLS ctl 
       on ctl.contract_id = okh.id
    inner join PJB_BILLING_EVENTS evt
       on evt.contract_id = okh.id
      and evt.major_version = okh.major_version
  and evt.project_id=prj.project_id
      and ctl.contract_line_id=evt.contract_line_id
    inner join PJF_TASKS_V tsk
       on tsk.project_id = prj.project_id
   and tsk.task_id=evt.task_id
    inner join PJF_EVENT_TYPES_B typ
       on typ.event_type_id = evt.event_type_id
    inner join PJF_EVENT_TYPES_TL typl
   on typ.event_type_id = typl.event_type_id
    inner join hr_all_organization_units_tl hou
       on hou.organization_id = evt.organization_id
inner join fnd_lookup_values_tl flv
   on flv.lookup_type = 'PJB_EVT_REVENUE_RECOGNZD'
   AND flv.lookup_code=evt.revenue_recognzd_flag
    where evt.task_id IS NOT NULL    
      and hou.LANGUAGE = USERENV('LANG')      
   UNION ALL
   SELECT evt.rowid,
          evt.event_id,
          evt.project_id,
          prj.segment1,
          prjt.name,
          prjt.description,
          evt.task_id,
          NULL,
          NULL,
          evt.event_num,
          evt.event_type_code,
          --DPL et.event_type_classification,
          evt.completion_date,
          evt.event_desc,
          prj.org_id,
          prj.carrying_out_organization_id,
          (SELECT name FROM hr_organization_units hou WHERE hou.organization_id = prj.carrying_out_organization_id) as project_owning_org,
          evt.organization_id,
          hou.name,
          ctl.hard_limit_amount,          
          --DPL evt.bill_amount,
          evt.revenue_amt,
          evt.bill_hold_flag,
          --DPL evt.revenue_distributed_flag,
          --DPL prj.project_level_funding_flag,
          evt.attribute_category,
          evt.attribute1,
          evt.attribute2,
          evt.attribute3,
          evt.attribute4,
          evt.attribute5,
          evt.attribute6,
          evt.attribute7,
          evt.attribute8,
          evt.attribute9,
          evt.attribute10,
          --DPL evt.inventory_org_id,
          --DPL evt.inventory_item_id,
          evt.quantity_billed,
          evt.uom_code,
          evt.unit_price,
          evt.reference1,
          evt.reference2,
          evt.reference3,
          evt.reference4,
          evt.reference5,
          evt.reference6,
          evt.reference7,
          evt.reference8,
          evt.reference9,
          evt.reference10,
          evt.last_update_date,
          evt.last_updated_by,
          evt.creation_date,
          evt.created_by,
          evt.last_update_login,
          evt.bill_trns_currency_code,
          evt.bill_trns_amount,
          evt.contract_curr_amt as "BILL_TRANS_REV_AMOUNT",
          evt.contract_curr_code,
          evt.contract_curr_rate_type,
          --DPL c.user_conversion_type project_rate_type_name,
          evt.contract_curr_exchg_rate,
          evt.contract_curr_exchg_date,
          --DPL evt.project_bill_amount,
          --DPL evt.project_inv_exchange_rate,
          --DPL evt.project_inv_rate_date,
          evt.project_revenue_amt,
          --DPL evt.project_rev_exchange_rate,
          --DPL evt.project_rev_rate_date,
          --DPL evt.projfunc_currency_code,
          --DPL evt.projfunc_rate_type,
          --DPL c1.user_conversion_type projfunc_rate_type_name,
          --DPL evt.projfunc_rate_date,
          --DPL evt.projfunc_exchange_rate,
          --DPL evt.projfunc_bill_amount,
          --DPL evt.projfunc_inv_exchange_rate,
          --DPL evt.projfunc_inv_rate_date,
          --DPL evt.projfunc_revenue_amount,
          --DPL evt.projfunc_rev_exchange_rate,
          --DPL evt.projfunc_rev_rate_date,
          --DPL evt.funding_rate_type,
          --DPL c2.user_conversion_type funding_rate_type_name,
          --DPL evt.funding_rate_date,
          --DPL evt.funding_exchange_rate,
          evt.invoice_currency_code,
          evt.invoice_curr_rate_type,
          evt.invoice_curr_exchg_date ,
          evt.invoice_curr_exchg_rate,
          --DPL evt.revproc_currency_code,
          --DPL evt.revproc_rate_type,
          --DPL evt.revproc_rate_date,
          --DPL evt.revproc_exchange_rate,
          --DPL evt.inv_gen_rejection_code,
          --DPL pl1.meaning,
          --DPL pl.meaning,
          prj.project_currency_code,
          --DPL prj.project_bil_rate_date_code,
          --DPL prj.project_bil_rate_type,
          --DPL prj.project_bil_rate_date,
          --DPL prj.project_bil_exchange_rate,
          --DPL prj.projfunc_currency_code,
          --DPL prj.projfunc_bil_rate_date_code,
          --DPL prj.projfunc_bil_rate_type,
          --DPL prj.projfunc_bil_rate_date,
          --DPL prj.projfunc_bil_exchange_rate,
          --DPL prj.funding_rate_date_code,
          --DPL prj.funding_rate_type,
          --DPL prj.funding_rate_date,
          --DPL prj.funding_exchange_rate,
          --DPL prj.revproc_currency_code,
          --DPL prj.invproc_currency_type,
          --DPL prj.multi_currency_billing_flag,
          prj.project_status_code,
          --DPL evt.adjusting_revenue_flag,
          --DPL evt.zero_revenue_amount_flag,
          evt.non_updateable_flag,
          evt.audit_cost_plan_type_id,
          evt.audit_rev_plan_type_id,
          evt.revenue_hold_flag,
          NULL,
          NULL,
          NULL,
          TO_DATE (NULL),
          TO_NUMBER (NULL),
          TO_DATE (NULL),
          TO_NUMBER (NULL),
          TO_NUMBER (NULL),
          TO_DATE (NULL),
          TO_NUMBER (NULL),
          TO_NUMBER (NULL),
          --DPL evt.billed_flag,
          prj.pm_product_code,
  okh.contract_number "CONTRACT_NUMBER",
  okl.line_number "CONTRACT_LINE_NUMBER",
  flv.meaning "REVENUE_STATUS",
  evt.revenue_exception_flag "REVENUE_EXCEPTION",
  Round(evt.revenue_recognzd_percentage,2) "REVENUE_PERCENTAGE"
          --DPL evt.pm_event_reference,
          --DPL pl2.meaning
     FROM PJF_PROJECTS_ALL_B prj
    inner join PJF_PROJECTS_ALL_TL prjt
       on prjt.project_id = prj.project_id     
    inner join OKC_K_HEADERS_ALL_B okh
       on okh.contract_number = prj.segment1
inner join OKC_K_LINES_B okl
   on okh.major_version=okl.major_version
   AND okh.id=okl.dnz_chr_id
    inner join PJB_BILLING_CONTROLS ctl 
       on ctl.contract_id = okh.id
    inner join PJB_BILLING_EVENTS evt
       on evt.contract_id = okh.id
      and evt.major_version = okh.major_version
  and evt.project_id=prj.project_id
          and ctl.contract_line_id=evt.contract_line_id
    inner join PJF_TASKS_V tsk
       on tsk.project_id = prj.project_id
   and tsk.task_id=evt.task_id
    inner join PJF_EVENT_TYPES_B typ
       on typ.event_type_id = evt.event_type_id
    inner join hr_all_organization_units_tl hou
       on hou.organization_id = evt.organization_id
inner join fnd_lookup_values_tl flv
   on flv.lookup_type = 'PJB_EVT_REVENUE_RECOGNZD'
   AND flv.lookup_code=evt.revenue_recognzd_flag
    where evt.task_id IS NULL     
      and hou.LANGUAGE = USERENV('LANG'))
          
SELECT
    o259902.project_number,
    o259902.project_name,    
    o259902.task_number,
    o259902.task_name,
    o259902.event_type,
    o259902.event_date,
    o259902.organization_name,
o259902.bill_trans_amount,
    o259902.bill_trans_rev_amount,
    o259902.project_currency_code,
    o259902.hard_limit_amount,
o259902.contract_number,
o259902.contract_line_number,
o259902.revenue_status,
o259902.revenue_exception,
o259902.revenue_percentage,
o259902.event_num,
o259902.description,
o259902.project_revenue_amt,
(o259902.bill_trans_amount-o259902.bill_trans_rev_amount) Tot
FROM
    apps.xxansr_pa_events_cte o259902
WHERE 
        o259902.project_status_code = 'ACTIVE'
    AND o259902.hard_limit_amount is not null
    --TODO AND o259902.description = 'Hard Limit'
    AND o259902.bill_trans_rev_amount != '0'
Group by o259902.project_number,
    o259902.project_name,    
    o259902.task_number,
    o259902.task_name,
    o259902.event_type,
    o259902.event_date,
    o259902.organization_name,
    o259902.bill_trans_rev_amount,
    o259902.project_currency_code,
    o259902.hard_limit_amount,
o259902.contract_number,
o259902.contract_line_number,
o259902.revenue_status,
o259902.revenue_exception,
o259902.revenue_percentage,
o259902.event_num,
o259902.description,
o259902.project_revenue_amt,

o259902.bill_trans_amount

REVENUE TRANSFER REPORT (FUSION)

Purpose of this report is to derive all projects expenditure and event details with cost information.

SELECT   revenue_type, TO_CHAR (customer_id) customer_id, customer_name,
         TO_CHAR (project_id) project_id, project_number, contract_number,
         project_name, project_organization, industry, state, city, country,
         company, ACCOUNT,                                     --account_type,
                          account_name, department, department_name, region,
         FUNCTION,
         ROUND (SUM (project_curr_revenue_amt), 2) project_curr_revenue_amt,
         bill_transaction_currency,
         ROUND
            (SUM (bill_transaction_currency_rev),
             2
            ) bill_transaction_currency_rev,
         contract_currency_code,
         ROUND (SUM (cont_curr_revenue_amt_sum), 2) cont_curr_revenue_amt_sum,
         revenue_currency_code, ledger_currency_code,
         ROUND (SUM (ledger_curr_revenue_amt), 2) ledger_curr_revenue_amt,
         ROUND (SUM (revenue_curr_amt), 2) revenue_curr_amt,
         ROUND (SUM (usd_revenue_amount), 2) usd_revenue_amount,
         project_currency_code,
         ROUND (SUM (project_revenue_amount), 2) project_revenue_amount,
         projfunc_currency_code,
         ROUND (SUM (projfunc_revenue_amount), 2) projfunc_revenue_amount,
         planning_week, gl_period, gl_set_of_books_name, gl_line_dff_prj_num,
       
         --system_person_type,
         project_organization global_region, region_code, SOURCE,
         business_unit, ledger_name,
         project_organization reporting_organization
    FROM ((SELECT   revenue_type, customer_id, customer_name, project_id,
                    project_number, contract_number, project_name,
                    project_organization, industry, state, city, country,
                    company, ACCOUNT, account_name, department,
                    department_name, region, FUNCTION,
                    ROUND
                       (SUM (project_curr_revenue_amt),
                        2
                       ) project_curr_revenue_amt,
                    bill_transaction_currency,
                    ROUND
                       (SUM (bill_transaction_currency_rev),
                        2
                       ) bill_transaction_currency_rev,
                    contract_currency_code,
                    ROUND
                       (SUM (cont_curr_revenue_amt_sum),
                        2
                       ) cont_curr_revenue_amt_sum,
                    revenue_currency_code, ledger_currency_code,
                    ROUND
                       (SUM (ledger_curr_revenue_amt),
                        2
                       ) ledger_curr_revenue_amt,
                    ROUND (SUM (revenue_curr_amt), 2) revenue_curr_amt,
                    ROUND (SUM (usd_revenue_amount), 2) usd_revenue_amount,
                    project_currency_code,
                    ROUND
                         (SUM (project_revenue_amount),
                          2
                         ) project_revenue_amount,
                    projfunc_currency_code,
                    ROUND
                       (SUM (projfunc_revenue_amount),
                        2
                       ) projfunc_revenue_amount,
                    planning_week, gl_period, gl_set_of_books_name,
                    gl_line_dff_prj_num, global_region, region_code,
                    business_unit, SOURCE, ledger_name, code_combination_id
               FROM (SELECT   'Expenditure' revenue_type,
                              CAST
                                 (hca.cust_account_id AS CHARACTER (30)
                                 ) customer_id,
                              hp.party_name customer_name,
                              CAST
                                 (v225332622.project_id AS CHARACTER (30)
                                 ) project_id,
                              v225332622.project_number project_number,
                              okha.contract_number contract_number,
                              v225332622.project_name project_name,
                              haou.NAME project_organization,
                              hca.customer_class_code industry,
                              hp.state state, hp.city city,
                              hp.country country, gcc.segment1 company,
                              gcc.segment2 ACCOUNT,
                           
                              --gcc.account_type,
                              (SELECT fv.description
                                 FROM fnd_flex_values_vl fv,
                                      fnd_flex_value_sets fvs
                                WHERE fv.flex_value_set_id =
                                                         fvs.flex_value_set_id
                                  AND fvs.flex_value_set_name = 'Account XXX'
                                  AND fv.flex_value = gcc.segment2)
                                                                 account_name,
                              gcc.segment3 department,
                              (SELECT fv.description
                                 FROM fnd_flex_values_vl fv,
                                      fnd_flex_value_sets fvs
                                WHERE fv.flex_value_set_id =
                                                         fvs.flex_value_set_id
                                  AND fvs.flex_value_set_name =
                                                              'Department XXX'
                                  --'ANSR_GL_ACCOUNT'
                                  AND fv.flex_value = gcc.segment3)
                                                              department_name,
                              gcc.segment4 region, gcc.segment5 FUNCTION,
                              SUM
                                 (project_curr_revenue_amt
                                 ) project_curr_revenue_amt,
                              trns_currency_code bill_transaction_currency,
                              SUM
                                 (trns_curr_revenue_amt
                                 ) bill_transaction_currency_rev,
                              contract_currency_code,
                              SUM
                                 (NVL (cont_curr_revenue_amt, 0)
                                 ) cont_curr_revenue_amt_sum,
                              revenue_currency_code, ledger_currency_code,
                              SUM
                                 (ledger_curr_revenue_amt
                                 ) ledger_curr_revenue_amt,
                              SUM
                                 (v144634978.revenue_curr_amt
                                 ) revenue_curr_amt,
                              SUM
                                 (DECODE
                                     ('USD',
                                      v144634978.revenue_currency_code, NVL
                                                 (v144634978.revenue_curr_amt,
                                                  0
                                                 ),
                                      (  NVL (v144634978.revenue_curr_amt, 0)
                                       * (SELECT DISTINCT r.avg_rate
                                                     FROM gl_translation_rates r,
                                                          gl_sets_of_books s
                                                    WHERE s.set_of_books_id =
                                                             r.set_of_books_id
                                                      AND r.to_currency_code =
                                                                         'USD'
                                                      --AND s.set_of_books_id=gsb.set_of_books_id
                                                      AND s.currency_code =
                                                             v144634978.revenue_currency_code
                                                      AND r.period_name =
                                                             v144634978.gl_period
                                                      AND ROWNUM = 1)
                                      )
                                     )
                                 ) usd_revenue_amount,
                              v225332622.project_currency_code
                                                        project_currency_code,
                              SUM
                                 (v144634978.project_curr_revenue_amt
                                 ) project_revenue_amount,
                              pcdl.projfunc_currency_code
                                                       projfunc_currency_code,
                              pcdl.projfunc_raw_cost projfunc_revenue_amount,
                              (SELECT    'Q'
                                      || quarter_num
                                      || '-WK'
                                      || LPAD
                                            (TO_CHAR
                                                (DECODE
                                                    (period_num,
                                                     53, 14,
                                                     DECODE (MOD (period_num,
                                                                  13
                                                                 ),
                                                             0, 13,
                                                             MOD (period_num,
                                                                  13
                                                                 )
                                                            )
                                                    )
                                                ),
                                             2,
                                             '0'
                                            )
                                 FROM gl_periods gp
                                WHERE 1 = 1
                                  AND v144634978.gl_date BETWEEN start_date
                                                             AND end_date
                                  AND adjustment_period_flag = 'N'
                                  AND gp.period_set_name = gl.period_set_name)
                                                                planning_week,
                              v144634978.gl_period gl_period,
                              gsb.NAME gl_set_of_books_name,
                              NULL gl_line_dff_prj_num, NULL global_region,
                              (SELECT class_code
                                 FROM pjf_project_classes prc,
                                      pjf_class_codes_tl pct
                                WHERE prc.class_code_id = pct.class_code_id
                                  AND prc.project_id = v225332622.project_id)
                                                                  region_code,
                              (SELECT NAME
                                 FROM hr_operating_units
                                WHERE organization_id =
                                              v225332622.org_id)
                                                                business_unit,
                              (SELECT b.document_name
                                 FROM pjf_txn_sources_tl a,
                                      pjf_txn_document_tl b
                                WHERE a.transaction_source_id =
                                              v457612106.transaction_source_id
                                  AND b.document_id = v457612106.document_id)
                                                                       SOURCE,
                              v457612106.expenditure_item_id
                                                       AS expenditure_item_id,
                              v144634978.transaction_project_id
                                                    AS transaction_project_id,
                           
                              --v144634978.rev_distribution_id AS rev_distribution_id,
                              /*(Select meaning
                                 From fnd_lookup_values_tl
                                where lookup_type='PJF_PERSON_TYPE'
                                  and lookup_code=v457612106.person_type) system_person_type,*/
                              gl.NAME ledger_name, xal.code_combination_id
                         FROM (SELECT revenuedistributionpeo.bill_transaction_type_code,
                                      revenuedistributionpeo.rev_distribution_id,
                                      revenuedistributionpeo.transaction_id,
                                      revenuedistributionpeo.transaction_project_id,
                                      revenuedistributionpeo.revenue_curr_amt,
                                      revenuedistributionpeo.trns_curr_revenue_amt,
                                      gl_period_name gl_period, contract_id,
                                      major_version, project_curr_revenue_amt,
                                      gl_date, reversed_flag,
                                      line_num_reversed,
                                      revenue_currency_code,
                                      ledger_currency_code,
                                      ledger_curr_revenue_amt,
                                      contract_currency_code,
                                      cont_curr_revenue_amt,
                                      project_currency_code,
                                      trns_currency_code,
                                      (CASE
                                          WHEN bill_transaction_type_code =
                                                                          'EI'
                                             THEN transaction_id
                                       END
                                      ) AS expenditure_item_id
                                 FROM pjb_rev_distributions revenuedistributionpeo
                                WHERE 1 = 1) v144634978,
                              (SELECT projectbasepeo.project_id,
                                      projectbasepeo.segment1
                                                            AS project_number,
                                      ppt.NAME project_name,
                                      projectbasepeo.org_id,
                                      project_currency_code,
                                      projectbasepeo.attribute10 region_code,
                                      projectbasepeo.pm_product_code
                                                              pm_product_code
                                 FROM pjf_projects_all_b projectbasepeo,
                                      pjf_projects_all_tl ppt
                                WHERE projectbasepeo.project_id =
                                                                ppt.project_id) v225332622,
                              (SELECT expenditureitempeo.expenditure_item_id,
                                      expenditureitempeo.document_id,
                                      expenditureitempeo.project_id,
                                      expenditureitempeo.transaction_source_id,
                                      expenditureitempeo.person_type
                                                                  person_type,
                                      expenditureitempeo.original_dist_id
                                 FROM pjc_exp_items_all expenditureitempeo) v457612106,
                              pjc_cost_dist_lines_all pcdl,
                              okc_k_headers_all_b okha,
                              hz_cust_accounts hca,
                              hz_parties hp,
                              hr_all_organization_units haou,
                              fun_names_business_units_v fnbu,
                              gl_ledgers gl,
                              gl_sets_of_books gsb,
                              gl_code_combinations gcc,
                              xla_distribution_links xda,
                              xla_ae_lines xal
                        WHERE v144634978.transaction_project_id =
                                                         v225332622.project_id
                          AND v225332622.project_id = v457612106.project_id
                          AND v144634978.expenditure_item_id = v457612106.expenditure_item_id(+)
                          AND (((v144634978.transaction_project_id > 0)))
                          AND pcdl.reversed_flag IS NULL
                          AND pcdl.line_num_reversed IS NULL
                          AND v144634978.reversed_flag IS NULL
                          AND v144634978.line_num_reversed IS NULL
                          AND pcdl.expenditure_item_id =
                                                v457612106.expenditure_item_id
                          AND pcdl.transfer_status_code IN ('N')
                          AND v144634978.contract_id = okha.ID
                          AND v144634978.major_version = okha.major_version
                          AND okha.bill_to_acct_id = hca.cust_account_id(+)
                          --AND v144634978.bill_transaction_type_code ='EI'
                          AND hca.party_id = hp.party_id(+)
                          AND haou.organization_id = v225332622.org_id
                          AND fnbu.bu_id = v225332622.org_id
                          AND fnbu.primary_ledger_id = gl.ledger_id
                          AND gl.ledger_id = gsb.set_of_books_id
                          AND xda.source_distribution_id_num_1 =
                                                v144634978.rev_distribution_id
                          --AND PCDL.EXPENDITURE_ITEM_ID=XDA.SOURCE_DISTRIBUTION_ID_NUM_1(+)
                          AND xda.ae_line_num(+) = 2
                          AND xal.ae_header_id(+) = xda.ae_header_id
                          AND xal.ae_line_num(+) = xda.ae_line_num
                          AND gcc.code_combination_id =
                                 --xal.code_combination_id
                                 NVL
                                    (xal.code_combination_id,
                                     (CASE
                                         WHEN pcdl.acct_source_code = 'AP_INV'
                                            THEN (SELECT apd.dist_code_combination_id
                                                    FROM ap_invoice_distributions_all apd
                                                   WHERE apd.invoice_distribution_id =
                                                            v457612106.original_dist_id)
                                         ELSE NVL (pcdl.raw_cost_dr_ccid,
                                                   pcdl.raw_cost_cr_ccid
                                                  )
                                      END
                                     )
                                    )
                          AND xda.source_distribution_type =
                                               'Revenue - Expenditure Revenue'
                          AND xda.unrounded_accounted_cr IS NOT NULL
                          AND xal.ae_header_id = xda.ae_header_id
                          AND xal.ae_line_num = xda.ae_line_num
                          --AND gcc.code_combination_id =xal.code_combination_id
                          AND gcc.segment1 <> 'AA'
                          AND revenue_curr_amt IS NOT NULL
                          /*AND (    gcc.segment2 BETWEEN '410000' AND '549999'
                               AND gcc.segment2 NOT BETWEEN '500000' AND '528509'
                               AND gcc.segment2 NOT BETWEEN '528511' AND '529999'
                              )*/
                          AND v144634978.bill_transaction_type_code = 'EI'
--AND v225332622.project_number='TEST-GFY-DE'
--AND v457612106.expenditure_item_id=207232
                     GROUP BY hca.cust_account_id,
                              hp.party_name,
                              v225332622.project_id,
                              v225332622.project_number,
                              v225332622.project_name,
                              haou.NAME,
                              hca.customer_class_code,
                              hp.state,
                              hp.city,
                              gcc.segment1,
                              gcc.segment2,
                              --gcc.account_type,
                              gcc.segment3,
                              gcc.segment4,
                              gcc.segment5,
                              v144634978.trns_curr_revenue_amt,
                              v144634978.gl_period,
                              gl.ledger_id,
                              v225332622.project_currency_code,
                              v144634978.project_curr_revenue_amt,
                              pcdl.projfunc_currency_code,
                              pcdl.projfunc_raw_cost,
                              v144634978.gl_date,
                              gl.period_set_name,
                              v144634978.gl_period,
                              gsb.NAME,
                              region_code,
                              v225332622.org_id,
                              v144634978.revenue_curr_amt,
                              v457612106.expenditure_item_id,
                              v144634978.transaction_project_id,
                              xal.code_combination_id,
                              v225332622.pm_product_code,
                              v457612106.transaction_source_id,
                              v457612106.document_id,
                              hp.country,
                              okha.contract_number,
                              gl.NAME,
                              v144634978.trns_currency_code,
                              v144634978.contract_currency_code,
                              v144634978.revenue_currency_code,
                              v144634978.ledger_currency_code)
              WHERE transaction_project_id IS NOT NULL
                AND expenditure_item_id IS NOT NULL
           GROUP BY revenue_type,
                    customer_id,
                    customer_name,
                    project_id,
                    project_number,
                    project_name,
                    project_organization,
                    industry,
                    state,
                    city,
                    country,
                    company,
                    ACCOUNT,
                    --account_type,
                    account_name,
                    department,
                    department_name,
                    region,
                    FUNCTION,
                    project_currency_code,
                    projfunc_currency_code,
                    planning_week,
                    gl_period,
                    gl_set_of_books_name,
                    gl_line_dff_prj_num,
                    global_region,
                    region_code,
                    business_unit,
                    SOURCE,
                    code_combination_id,
                    contract_number,
                    --system_person_type
                    ledger_name,
                    bill_transaction_currency,
                    contract_currency_code,
                    revenue_currency_code,
                    ledger_currency_code,
                    project_currency_code,
                    projfunc_currency_code)
          UNION ALL
          (SELECT   revenue_type, customer_id, customer_name, project_id,
                    project_number, contract_number, project_name,
                    project_organization, industry, state, city, country,
                    company, ACCOUNT, account_name, department,
                    department_name, region, FUNCTION,
                    ROUND
                       (SUM (project_curr_revenue_amt),
                        2
                       ) project_curr_revenue_amt,
                    bill_transaction_currency,
                    ROUND
                       (SUM (bill_transaction_currency_rev),
                        2
                       ) bill_transaction_currency_rev,
                    contract_currency_code,
                    ROUND
                       (SUM (cont_curr_revenue_amt_sum),
                        2
                       ) cont_curr_revenue_amt_sum,
                    revenue_currency_code, ledger_currency_code,
                    ROUND
                       (SUM (ledger_curr_revenue_amt),
                        2
                       ) ledger_curr_revenue_amt,
                    ROUND (SUM (revenue_curr_amt), 2) revenue_curr_amt,
                    ROUND (SUM (usd_revenue_amount), 2) usd_revenue_amount,
                    project_currency_code,
                    ROUND
                         (SUM (project_revenue_amount),
                          2
                         ) project_revenue_amount,
                    projfunc_currency_code,
                    ROUND
                       (SUM (projfunc_revenue_amount),
                        2
                       ) projfunc_revenue_amount,
                    planning_week, gl_period, gl_set_of_books_name,
                    gl_line_dff_prj_num, global_region, region_code,
                    business_unit, SOURCE, ledger_name, code_combination_id
               FROM (SELECT   'Event' revenue_type,
                              CAST
                                 (hca.cust_account_id AS CHARACTER (30)
                                 ) customer_id,
                              hp.party_name customer_name,
                              CAST
                                 (ppa.project_id AS CHARACTER (30))
                                                                   project_id,
                              ppa.segment1 project_number,
                              okh.contract_number contract_number,
                              ppa.NAME project_name,
                              hou.NAME project_organization,
                              hca.customer_class_code industry,
                              hp.state state, hp.city city,
                              hp.country country, gcc.segment1 company,
                              gcc.segment2 ACCOUNT,
                              (SELECT fv.description
                                 FROM fnd_flex_values_vl fv,
                                      fnd_flex_value_sets fvs
                                WHERE fv.flex_value_set_id =
                                                         fvs.flex_value_set_id
                                  AND fvs.flex_value_set_name = 'Account XXX'
                                  --'ANSR_GL_ACCOUNT'
                                  AND fv.flex_value = gcc.segment2)
                                                                 account_name,
                              gcc.segment3 department,
                              (SELECT fv.description
                                 FROM fnd_flex_values_vl fv,
                                      fnd_flex_value_sets fvs
                                WHERE fv.flex_value_set_id =
                                                         fvs.flex_value_set_id
                                  AND fvs.flex_value_set_name =
                                                              'Department XXX'
                                  --'ANSR_GL_ACCOUNT'
                                  AND fv.flex_value = gcc.segment3)
                                                              department_name,
                              gcc.segment4 region, gcc.segment5 FUNCTION,
                              SUM
                                 (pe.project_revenue_amt
                                 ) project_curr_revenue_amt,
                              pe.bill_trns_currency_code
                                                    bill_transaction_currency,
                              SUM
                                 (NVL (pe.bill_trns_amount, 0)
                                 ) bill_transaction_currency_rev,
                              pe.contract_curr_code contract_currency_code,
                              SUM
                                 (NVL (pe.contract_curr_amt, 0)
                                 ) cont_curr_revenue_amt_sum,
                              pe.revenue_currency_code,
                              pe.ledger_currency_code,
                              SUM
                                 (NVL (pe.ledger_revenue_amt, 0)
                                 ) ledger_curr_revenue_amt,
                              SUM (NVL (pe.revenue_amt, 0)) revenue_curr_amt,
                              SUM
                                 (DECODE
                                     ('USD',
                                      pe.revenue_currency_code, pe.revenue_amt,
                                      pe.ledger_currency_code, pe.ledger_revenue_amt,
                                      pe.contract_curr_code, pe.contract_curr_amt,
                                      pe.project_currency_code, pe.project_revenue_amt,
                                        pe.revenue_amt          --pcrdl.amount
                                      * NVL
                                           ((SELECT gdr.conversion_rate
                                               FROM gl_daily_rates gdr
                                              WHERE gdr.conversion_type =
                                                                   'Corporate'
                                                AND gdr.to_currency = 'USD'
                                                AND gdr.from_currency =
                                                       pe.revenue_currency_code
                                                AND gdr.conversion_date =
                                                                   prd.gl_date),
                                            1
                                           )
                                     )
                                 ) usd_revenue_amount,
                              pe.project_currency_code project_currency_code,
                              SUM
                                 (pe.project_revenue_amt
                                 ) project_revenue_amount,
                              ppa.projfunc_currency_code
                                                       projfunc_currency_code,
                              NULL projfunc_revenue_amount,
                              (SELECT    'Q'
                                      || quarter_num
                                      || '-WK'
                                      || LPAD
                                            (TO_CHAR
                                                (DECODE
                                                    (period_num,
                                                     53, 14,
                                                     DECODE (MOD (period_num,
                                                                  13
                                                                 ),
                                                             0, 13,
                                                             MOD (period_num,
                                                                  13
                                                                 )
                                                            )
                                                    )
                                                ),
                                             2,
                                             '0'
                                            )
                                 FROM gl_periods gp
                                WHERE 1 = 1
                                  AND prd.gl_date BETWEEN start_date AND end_date
                                  AND adjustment_period_flag = 'N'
                                  AND gp.period_set_name = gl.period_set_name)
                                                                planning_week,
                              prd.gl_period gl_period,
                              gsb.NAME gl_set_of_books_name,
                              NULL gl_line_dff_prj_num, NULL global_region,
                              (SELECT class_code
                                 FROM pjf_project_classes prc,
                                      pjf_class_codes_tl pct
                                WHERE prc.class_code_id = pct.class_code_id
                                  AND prc.project_id = ppa.project_id)
                                                                  region_code,
                              (SELECT NAME
                                 FROM hr_operating_units
                                WHERE organization_id =
                                                     ppa.org_id)
                                                                business_unit,
                              'PA' SOURCE,
                                          --prd.revenue_curr_amt revenue_curr_amt,
                                          pe.event_id expenditure_item_id,
                              prd.transaction_project_id
                                                       transaction_project_id,
                              gl.NAME ledger_name,
                                                  --pe.event_desc,
                                                  xal.code_combination_id
                         FROM okc_k_headers_all_b okh,
                              -- OKC_K_LINES_B okl,
                              pjf_projects_all_vl ppa,
                              pjf_project_types_vl ppt,
                              hr_all_organization_units hou,
                              hr_all_organization_units hou1,
                              pjb_billing_events pe,
                              pjf_tasks_v tsk,
                              pjf_event_types_tl etyp,
                              hz_cust_accounts hca,
                              hz_parties hp,
                              ((SELECT revenuedistributionpeo.bill_transaction_type_code,
                                       revenuedistributionpeo.rev_distribution_id,
                                       revenuedistributionpeo.transaction_id,
                                       revenuedistributionpeo.transaction_project_id,
                                       revenuedistributionpeo.revenue_curr_amt,
                                       revenuedistributionpeo.trns_curr_revenue_amt,
                                       gl_period_name gl_period, contract_id,
                                       major_version,
                                       project_curr_revenue_amt, gl_date,
                                       reversed_flag, line_num_reversed,
                                       (CASE
                                           WHEN bill_transaction_type_code =
                                                                         'EVT'
                                              THEN transaction_id
                                        END
                                       ) AS event_id
                                  FROM pjb_rev_distributions revenuedistributionpeo
                                 WHERE 1 = 1)) prd,
                              gl_code_combinations gcc,
                              gl_ledgers gl,
                              gl_sets_of_books gsb,
                              fun_names_business_units_v fnbu,
                              xla_distribution_links xda,
                              xla_ae_lines xal,
                              pjc_exp_items_all pei
                        WHERE 1 = 1
                          AND ppa.project_type_id = ppt.project_type_id
                          AND hou.organization_id =
                                              ppa.carrying_out_organization_id
                          AND hou1.organization_id = pe.organization_id
                          AND pe.project_id = ppa.project_id
                          AND tsk.project_id = ppa.project_id
                          AND pe.task_id = tsk.task_id
                          AND pe.event_type_id = etyp.event_type_id
                          AND pe.event_id = prd.event_id(+)
                          AND okh.bill_to_acct_id = hca.cust_account_id(+)
                          AND NVL (prd.bill_transaction_type_code, 'EVT') =
                                                                         'EVT'
                          AND prd.reversed_flag IS NULL
                          AND prd.line_num_reversed IS NULL
                          AND hca.party_id = hp.party_id(+)
                          AND fnbu.bu_id = ppa.org_id
                          AND fnbu.primary_ledger_id = gl.ledger_id
                          AND gl.ledger_id = gsb.set_of_books_id
                          --AND pe.event_id=prd.event_id
                          AND okh.ID = pe.contract_id
                          AND NVL (xda.source_distribution_id_num_1(+),
                                   -99999) =
                                          NVL (prd.rev_distribution_id,
                                               -99999)
                          AND xal.ae_header_id = xda.ae_header_id
                          --AND xda.ae_line_num(+) <> 2
                          AND xal.ae_line_num(+) = xda.ae_line_num
                          AND gcc.code_combination_id =
                                                       xal.code_combination_id
                          --AND xda.unrounded_accounted_cr IS NOT NULL
                          AND xal.ae_header_id = xda.ae_header_id
                          AND xal.ae_line_num = xda.ae_line_num
                          --AND gcc.code_combination_id =xal.code_combination_id
                          AND gcc.segment1 <> 'AA'
                          AND gcc.segment3 <> '0000'
                          --Need to Check this condition
                          AND ppa.project_id = pei.project_id(+)
                          AND prd.transaction_id(+) = pei.expenditure_item_id
                     --AND ppa.segment1 in '10002'--('TEST-GFY-DE')                             -- 300000013862871
                     --AND okh.contract_number = '10002'
                     GROUP BY hca.cust_account_id,
                              hp.party_name,
                              ppa.project_id,
                              ppa.segment1,
                              okh.contract_number,
                              ppa.NAME,
                              hou.NAME,
                              hca.customer_class_code,
                              hp.state,
                              hp.city,
                              hp.country,
                              gcc.segment1,
                              gcc.segment2,
                              --gcc.account_type,
                              gcc.segment3,
                              gcc.segment4,
                              gcc.segment5,
                              pe.bill_trns_currency_code,
                              pe.contract_curr_code,
                              pe.revenue_currency_code,
                              pe.ledger_currency_code,
                              pe.project_currency_code,
                              ppa.projfunc_currency_code,
                              gl.period_set_name,
                              prd.gl_period,
                              gsb.NAME,
                              prd.gl_date,
                              ppa.org_id,
                              --pei.person_type,
                              prd.revenue_curr_amt,
                              pe.event_id,
                              pe.event_desc,
                              prd.transaction_project_id,
                              xal.code_combination_id,
                              prd.rev_distribution_id,
                              pei.transaction_source_id,
                              gl.NAME,
                              --pei.document_id,
                              xda.source_distribution_id_num_1
                     ORDER BY okh.contract_number, pe.event_id)
           GROUP BY revenue_type,
                    customer_id,
                    customer_name,
                    project_id,
                    project_number,
                    project_name,
                    project_organization,
                    industry,
                    state,
                    city,
                    country,
                    company,
                    ACCOUNT,
                    --account_type,
                    account_name,
                    department,
                    department_name,
                    region,
                    FUNCTION,
                    project_currency_code,
                    projfunc_currency_code,
                    planning_week,
                    gl_period,
                    gl_set_of_books_name,
                    gl_line_dff_prj_num,
                    global_region,
                    region_code,
                    business_unit,
                    SOURCE,
                    code_combination_id,
                    contract_number,
                    --system_person_type
                    ledger_name,
                    bill_transaction_currency,
                    contract_currency_code,
                    revenue_currency_code,
                    ledger_currency_code,
                    project_currency_code,
                    projfunc_currency_code)
          UNION ALL
          SELECT   'JE Adjustments' revenue_type,
                   CAST (0 AS CHARACTER (30)) customer_id,
                   'JE Adjustments' customer_name,
                   CAST (0 AS CHARACTER (30)) project_id, '0' project_number,
                   '0' contract_number,
                                        --js.user_je_source_name
                   '0' project_name,
                                    --jh.description || ':' || jl.description project_name,
                                    fv.description project_organization,
                   jh.external_reference industry, NULL state, NULL city,
                   NULL country, cc.segment1 company, cc.segment2 ACCOUNT,
                 
                   --cc.account_type,
                   (SELECT fv.description
                      FROM fnd_flex_values_vl fv,
                           fnd_flex_value_sets fvs
                     WHERE fv.flex_value_set_id = fvs.flex_value_set_id
                       AND fvs.flex_value_set_name = 'Account XXX'
                       --'ANSR_GL_ACCOUNT'
                       AND fv.flex_value = cc.segment2) account_name,
                   cc.segment3 department,
                   (SELECT fv.description
                      FROM fnd_flex_values_vl fv,
                           fnd_flex_value_sets fvs
                     WHERE fv.flex_value_set_id = fvs.flex_value_set_id
                       AND fvs.flex_value_set_name = 'Department XXX'
                       --'ANSR_GL_ACCOUNT'
                       AND fv.flex_value = cc.segment3) department_name,
                   cc.segment4 region, cc.segment5 FUNCTION,
                   SUM
                      ((  NVL ((NVL (jl.entered_dr, 0)
                                - NVL (jl.entered_cr, 0)
                               ),
                               0
                              )
                        * -1
                       )
                      ) project_curr_revenue_amt,
                   jl.currency_code bill_transaction_currency,
                   NULL bill_transaction_currency_rev,
                   NULL contract_currency_code,
                   NULL cont_curr_revenue_amt_sum, NULL revenue_currency_code,
                   gl.currency_code ledger_currency_code,
                   NULL ledger_curr_revenue_amt,
                   SUM (  NVL (NVL (jl.accounted_dr, jl.entered_dr), 0)
                        - (NVL (NVL (jl.accounted_cr, jl.entered_cr), 0))
                       ) revenue_curr_amt,
                 
                   -- Convert based on SOB Currency - Note: Accounted# are null when SOB Currency
                   -- is the same as Journal Currency
                   SUM
                      (  (  (  NVL (NVL (jl.accounted_dr, jl.entered_dr), 0)
                             - (NVL (NVL (jl.accounted_cr, jl.entered_cr), 0)
                               )
                            )
                          * NVL ((SELECT   gper.avg_rate
                                      FROM gl_lookups lk,
                                           gl_translation_rates gper
                                     WHERE lk.lookup_type =
                                                        'TRANSLATION_BAL_TYPE'
                                       AND lk.lookup_code = gper.actual_flag
                                       AND gper.period_name = jl.period_name
                                       AND gper.set_of_books_id =
                                                           gsb.set_of_books_id
                                       --AND gper.functional_currency=gsb.currency_code
                                       AND gper.to_currency_code = 'USD'
                                  GROUP BY gper.avg_rate,
                                           gper.period_name,
                                           gper.to_currency_code),
                                 1
                                )
                                                        --xxansr_utils_pkg.get_period_avg_rate
                         --                                 (gsb.currency_code,
                           --                                jl.period_name,
                             --                              'USD'
                               --                           )
                         )
                       * 1
                      ) usd_revenue_amount,
                   jl.currency_code project_currency_code,
                   NULL project_revenue_amount,
                   gsb.currency_code projfunc_currency_code,
                   SUM
                      (  (  NVL (NVL (jl.accounted_dr, jl.entered_dr), 0)
                          - (NVL (NVL (jl.accounted_cr, jl.entered_cr), 0))
                         )
                       * -1
                      ) projfunc_revenue_amount,
                   (SELECT    'Q'
                           || quarter_num
                           || '-WK'
                           || LPAD
                                  (TO_CHAR (DECODE (period_num,
                                                    53, 14,
                                                    DECODE (MOD (period_num,
                                                                 13
                                                                ),
                                                            0, 13,
                                                            MOD (period_num,
                                                                 13
                                                                )
                                                           )
                                                   )
                                           ),
                                   2,
                                   '0'
                                  )
                      FROM gl_periods
                     WHERE 1 = 1
                       AND period_set_name = 'XXX 4/4/5'
                                                        --period_type = '4_4_57130283831'
                       --'22'                         -- PA Periods
                       AND TRUNC (jl.effective_date) BETWEEN start_date
                                                         AND end_date
                       AND adjustment_period_flag = 'N') planning_week,
                 
                   --'JAN-05' gl_period,
                   jl.period_name gl_period, gsb.NAME gl_set_of_books_name,
                   jl.attribute1 gl_line_dff_prj_num, NULL global_region,
                   ' ' region_code, gsb.NAME business_unit,
                   js.je_source_name SOURCE,
                                            --NULL expenditure_item_id,
                                            --NULL transaction_project_id,
                                            gl.NAME ledger_name,
                   cc.code_combination_id
              FROM gl_je_headers jh,
                   gl_je_lines jl,
                   gl_code_combinations cc,
                   fnd_flex_values_vl fv,
                   fnd_flex_value_sets fvs,
                   gl_je_sources js,
                   gl_sets_of_books gsb,
                   gl_ledgers gl
             WHERE jl.je_header_id = jh.je_header_id
               --AND jh.je_header_id = 7327
               AND jh.ledger_id = gsb.set_of_books_id
               AND gl.ledger_id = gsb.set_of_books_id
               AND NVL (jh.status, 'X') = 'P'
               AND jh.je_source = js.je_source_name
               AND fv.flex_value_set_id = fvs.flex_value_set_id
               AND fvs.flex_value_set_name = 'Department XXX'
               --'ANSR_GL_DEPARTMENT'
               AND NVL (jh.je_category, 'x') != '1'
               AND fv.flex_value = cc.segment3
               AND jl.code_combination_id = cc.code_combination_id
               AND cc.account_type IN ('R', 'E')
               AND EXISTS (
                      SELECT 'X'
                        FROM gl_code_combinations
                       WHERE 1 = 1
                         AND account_type IN ('R', 'E')
--                              AND (   segment2 BETWEEN '410500' AND '433099'
                         --                              OR segment2 BETWEEN '532000' AND '547099'
                         --                             )
                         AND segment1 != 'AA'
                         AND code_combination_id = jl.code_combination_id)
               AND (    cc.segment2 BETWEEN '410000' AND '549999'
                    AND cc.segment2 NOT BETWEEN '500000' AND '528509'
                    AND cc.segment2 NOT BETWEEN '528511' AND '529999'
                   )
          GROUP BY js.user_je_source_name,
                   jh.description,
                   jl.description,
                   fv.description,
                   jh.external_reference,
                   cc.segment1,
                   cc.segment2,
                   cc.account_type,
                   cc.segment3,
                   cc.segment4,
                   cc.segment5,
                   jl.period_name,
                   gsb.set_of_books_id,
                   jl.currency_code,
                   gsb.currency_code,
                   jl.effective_date,
                   jl.period_name,
                   gsb.NAME,
                   jl.attribute1,
                   cc.segment1,
                   js.je_source_name,
                   cc.code_combination_id,
                   jl.period_name,
                   gl.currency_code,
                   gl.NAME)
   WHERE gl_period IN (:p_gl_period)
--     AND  ROWNUM <= 75001
     /*AND (    ACCOUNT BETWEEN '410000' AND '549999'
          AND ACCOUNT NOT BETWEEN '500000' AND '528509'
          AND ACCOUNT NOT BETWEEN '528511' AND '529999'
         )*/
GROUP BY revenue_type,
         customer_id,
         customer_name,
         project_id,
         project_number,
         contract_number,
         project_name,
         project_organization,
         industry,
         state,
         city,
         country,
         company,
         ACCOUNT,
         account_name,
         department,
         department_name,
         region,
         FUNCTION,
         bill_transaction_currency,
         contract_currency_code,
         revenue_currency_code,
         ledger_currency_code,
         project_currency_code,
         projfunc_currency_code,
         planning_week,
         gl_period,
         gl_set_of_books_name,
         gl_line_dff_prj_num,
         region_code,
         SOURCE,
         business_unit,
         ledger_name