Thursday, January 16, 2020

Oracle r12 AP Query to determine if the Discount is being calculated properly

Hello,
We had an issue where the Discount on an Invoice was not being calculated properly.

For example, we had two lines - first line was a regular Item (Discountable) and the second line was Freight (not discountable).

Oracle was for some reason reducing the Freight amount from the Item instead of the Invoice Total, thus returning a reduced discount.

I wrote this query to find out all the lines where the "Discountable amount" was not equal to the Item total.

select i.invoice_id, i.invoice_num, i.invoice_date, i.invoice_amount,i.amount_paid,i.amount_applicable_to_discount,
il.item_total, il.other_total, t.name, i.source,i.creation_date--, trunc(sysdate) -700--, i.discount_amount_taken
from ap_invoices_all i, ( select * from
        (
            select 
                case 
                    when upper(line_type_lookup_code)= 'ITEM' then 'Item'
                    else 'Other'
                end line_type,
                invoice_id,
                amount
            from      ap_invoice_lines_all
            where  1=1
        ) t
        pivot
        (
            sum(amount)
         for (line_type )
         in ( 'Item' as Item_total,
               'Other' as Other_total 
              )
         ) 
         ) il, -- group by line_type_lookup_code
 ap_terms_tl t
where 1=1
and i.invoice_id =  il.invoice_id
and i.exclude_freight_from_discount = 'Y'
and i.disc_is_inv_less_tax_flag = 'N'
and trunc(i.creation_date) >= to_date('12/23/2019','mm/dd/yyyy') -- trunc(sysdate) -700
and nvl(i.amount_paid,0) <> 0
and i.amount_applicable_to_discount <> il.item_total
and i.terms_id = t.term_id
and il.other_total > 0
order by i.creation_date

Please feel free to share your code or if you have any suggestions to make it better.