Anda di halaman 1dari 2

-- Function: acc_gl_balance_transfer(character, character, refcursor)

-- DROP FUNCTION acc_gl_balance_transfer(character, character, refcursor);

CREATE OR REPLACE FUNCTION acc_gl_balance_transfer(


IN ls_year1 character,
IN ls_year2 character,
INOUT ref refcursor)
RETURNS refcursor AS
$BODY$
declare
ldt_date1 timestamp ;
ldt_date2 timestamp ;

begin
drop table if exists temp1;
drop table if exists temp2;
drop table if exists temp3 ;
select date_from,date_to into ldt_date1,ldt_date2 from year_master where
year_code = ls_year1 ;
create temp table temp1 as
SELECT cast(ls_year2 as char(4)) as year_code, branch_id||extension_id as
branch_id, gl_id,cast(0 as numeric(15,2)) as debit,cast(0 as numeric(15,2)) as
credit
FROM branch_extension_master a,gl_master where gl_id<>'0000';

update temp1 a set debit = a.debit+b.debit ,credit =a.credit+ b.credit from


acc_gl_balance b
where a.branch_id = b.branch_id and a.gl_id = b.gl_id and b.year_code =
ls_year1;

create temp table temp2 as


select branch_id,gl_id,sum(debit) as debit,sum(credit) as credit from
general_ledger_actual b
where txn_date>=ldt_date1 and txn_date<=ldt_date2 group by
branch_id,gl_id ;

update temp1 a set debit = a.debit+b.debit, credit = a.credit+b.credit from


temp2 b
where a.branch_id = b.branch_id and a.gl_id = b.gl_id ;

/*
insert into temp1
select ls_year2,branch_id,'0000',cash_closing,0 from branch_day_closing
a
where txn_date=(select max(txn_date) from branch_day_closing b where
a.branch_id = b.branch_id and txn_date<= ldt_date2) ;

Update temp1 a set debit = case when (a.debit + b.debit -


(a.credit+b.credit))> 0 then (a.debit + b.debit - (a.credit+b.credit)) else 0 end ,
credit = case when (a.debit + b.debit - (a.credit+b.credit))<0 then
((a.credit+b.credit)-(a.debit + b.debit)) else 0 end
from temp3 b where a.year_code = b.year_code and a.branch_id =
b.branch_id and a.gl_id = b.gl_id and a.gl_id = '0128';
*/
insert into acc_gl_balance
select a.year_code,a.branch_id,a.gl_id,case when (debit - credit) > 0 then
(debit- credit) else 0 end as debit ,
case when (debit- credit) < 0 then (credit - debit) else 0 end as
credit
from temp1 a,gl_master b,gl_group_master c where a.gl_id = b.gl_id and
b.group_id = c.group_id and c.basic_group_id in ('01','02')
and debit - credit <> 0 union
select ls_year2,branch_id,'0000',cash_closing,0 from branch_day_closing a
where txn_date=(select max(txn_date) from branch_day_closing b where
a.branch_id = b.branch_id and txn_date<= ldt_date2)
union
select a.year_code,a.branch_id,cast( a.gl_id as Char(4)) as gl_id,case when
sum(debit - credit) > 0 then sum(debit- credit) else 0 end as debit ,
case when sum(debit- credit) < 0 then sum(credit - debit) else 0 end as
credit
from temp1 a,gl_master b,gl_group_master c
where a.gl_id = b.gl_id and b.group_id = c.group_id and
c.basic_group_id in ('03','04')-- and a.branch_id = d.branch_id
and debit - credit <> 0 group by a.year_code,a.branch_id ,a.gl_id order
by branch_id,gl_id ;
open ref for select 'success' from year_master where status_code='Y';

return ;
End;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION acc_gl_balance_transfer(character, character, refcursor)
OWNER TO postgres;

Anda mungkin juga menyukai