Monday, January 11, 2010

Ledger's Opening Balancs

Select period_name,
period_year,
period_num,
to_char(start_date,'yyyy/mm/dd') ||' 00:00:00'
into lv_period_name,
ln_period_year,
ln_period_num,
lv_start_date
from gl_periods
where upper(period_set_name) = 'Dec-95'
and adjustment_period_flag <> 'Y'
and :p_date_from between (to_char(start_date,'yyyy/mm/dd') ||' 00:00:00') and (to_char(end_date,'yyyy/mm/dd') ||' 00:00:00') ;

--- Getting Op Bal for the period

Select sum(nvl(gb.begin_balance_dr,0)-nvl(gb.begin_balance_cr,0))
into ln_month_op_bal
from gl_Balances gb,
gl_code_combinations gcc
where gb.period_name = lv_period_name
and gb.code_combination_id = gcc.code_combination_id
and gb.code_combination_id = :ccid
and gb.Ledger_id = :p_sob
and gb.currency_code = 'PKR'
and gb.actual_fLag = 'A' ;


Select nvl(sum(nvl(jgl.accounted_dr,0) - nvl(jgl.accounted_cr,0)),0)
into ln_amt
from gl_je_headers jgh ,
gl_je_lines jgl
where
jgh.Ledger_id = :p_sob
and jgh.Ledger_id =jgl.ledger_id
and jgh.status = 'P'
and jgh.actual_flag = 'A'
and jgh.je_header_id = jgl.je_header_id
and jgl.code_combination_id = :ccid
and ((to_char(jgh.default_effective_date,'yyyy/mm/dd') ||' 00:00:00') >= lv_start_date
and (to_char(jgh.default_effective_date,'yyyy/mm/dd') ||' 00:00:00') < :p_date_from) ;

return nvl(ln_amt,0) + nvl(ln_month_op_bal,0) ;

Exception
when no_data_found then
return 0 ;
when too_many_rows then
return 0 ;
when others then
return 0 ;

1 comment:

  1. Very well written query. You made my work easy & also saved my lots of time. I will use this one and change it according to my requirements.
    sap testing tools

    ReplyDelete