Thursday, May 17, 2018

XLA events XLA headers , XLA lines and AP payments join

with event as (
                  select /*+ materialize */
                       xe.entity_id ,
                       sum(xl.accounted_cr) accounted_dr,
             avg(case when xe.event_type_code in ('PAYMENT CREATED','PAYMENT CANCELLED') then 1 else 0 end) ex_type,
                         max(xh.accounting_date) event_date
                  from
                       xla_events xe,
                       xla_ae_headers xh,
                       xla_ae_lines xl,
                       gl_ledgers lg
                  where /*xe.event_date <= to_date('30/04/2018', 'dd/mm/yyyy')
                        and */xh.accounting_date <= to_date('30/04/2018', 'dd/mm/yyyy')
                        and xe.event_status_code= decode(xe.event_type_code, 'PAYMENT CANCELLED', xe.event_status_code, 'P')
                        and xe.process_status_code='P'
                        and xh.event_id=xe.event_id
                        and xh.ae_header_id=xl.ae_header_id
                        and xl.accounting_class_code='CASH'
                        and xh.ledger_id=lg.ledger_id
                        and lg.ledger_category_code='PRIMARY'
                      --  and xe.entity_id=64139354
                  group by xe.entity_id
                  )
select /*+ materialize */
                   aip.invoice_id,
                 
                  xe.entity_id,
                   xe.event_id,
                   xe.event_date,
                   e.event_date,
                   aip.amount,
                   accounted_dr
 
                 
                 
              from
                    xla_events xe inner join ap_invoice_payments_all aip
                                             on xe.event_id=aip.accounting_event_id
                     
                                  inner join event e
                                             on  xe.entity_id=e.entity_id
                                           
                                       
                                           and e.event_date<=to_date('30/04/2018', 'dd/mm/yyyy')
                                  inner join ap_checks_all ca       
                                             on aip.check_id=ca.check_id
                                  left join  gl_daily_rates dr
                                             on ca.currency_code=dr.from_currency
                                             and dr.to_currency='USD'
                                             and xe.event_date=dr.conversion_date

No comments:

Post a Comment