hi, pls find below the detailed sp as requested;
CREATE PROCEDURE stmtproc(in:customercode decimal(9),
in:startdate date, in:enddate date, in:begindate date);
declare :opbalenddate varchar(10)
set :begindate = convert(:begindate,sql_date)
set :opbalenddate = convert(:opbalenddate,sql_date)
set :opbalenddate = :startdate - 1
declare :opbaldate date
set :opbaldate = :begindate + 1
/*empty report/statement file*/
delete from smultistatement;
/*detailed transactions*/
/*credits*/
/*receipts*/
begin
insert into smultistatement
(code, dated, savingsloancode, debit, credit, description, reportno, thetype)
select rec.code, rec.dated, svg.description, 0,
IF (rec.dated < usr.redenodate, rec.savingsdeposit/usr.baserate,
IF(rec.dated >= usr.redenodate, rec.savingsdeposit/cur.exchangerate,0)),
IF (rec.cheque = '' or rec.cheque is null,
'Receipt no '+convert(rec.receiptno,sql_char),
IF(rec.cheque is not null, 'Receipt/chq no '+convert(rec.receiptno,sql_char)
+' '+convert(rec.cheque,sql_char), 'xxx')), svg.reportno, 'D'
from receipt rec, currencies cur, usersetup usr, savings svg
where (rec.savingsdeposit > 0) and (rec.code = :customercode)
and (rec.dated between :startdate and :enddate)
and (rec.cleared = 'Y') and (rec.currencycode = cur.code)
and (rec.savingscode = svg.code);
end
/*debits*/
/*payment*/
begin
insert into smultistatement
(code, dated, savingsloancode, debit, credit, description, reportno, thetype)
select pay.code, pay.dated, svg.description,
(pay.savingswithdrawal+pay.withinterest), 0,
'Voucher# '+convert(pay.VoucherNo,sql_char), svg.reportno, 'D'
from payment pay, savings svg
where ((pay.savingswithdrawal+pay.withinterest)> 0)
and (pay.code = :customercode)
and (pay.dated between :startdate and :enddate)
and (pay.savingscode = svg.code);
end
/*end of detail transactions*/
/*compute opening balance*/
/*credits*/
delete from sharesdrcr;
delete from savingsdrcr;
insert into sharesdrcr
(code, savingscode, debit, credit)
select :customercode, svg.code, 0, 0
from savings svg;
/*receipts*/
begin
insert into savingsdrcr
(code, savingscode, debit, credit)
select rec.code, rec.savingscode, 0, sum(rec.savingsdeposit/usr.baserate)
from receipt rec, usersetup usr, savings svg
where (rec.savingsdeposit > 0) and (rec.code = :customercode)
and (rec.dated between :opbaldate and :opbalenddate)
and (rec.savingscode = svg.code)
group by rec.code, rec.savingscode;
end
/*end of credits*/
/*debits*/
/*payment*/
begin
insert into savingsdrcr
(code, savingscode, debit, credit)
select pay.code, pay.savingscode, sum(pay.savingswithdrawal), 0
from payment pay, savings svg
where (pay.code = :customercode)
and (pay.dated between :opbaldate and :opbalenddate)
and (pay.savingscode = svg.code)
group by pay.code, pay.savingscode;
end
/*end of debits*/
/*end of opening balance*/
/*update savings debits*/
update sharesdrcr shc set shc.debit = (select sum(sdc.debit)
from savingsdrcr sdc
where (shc.code = sdc.code) and (shc.savingscode = sdc.savingscode));
/*update savings credits*/
update sharesdrcr shc set shc.credit = (select sum(sdc.credit)
from savingsdrcr sdc
where (shc.code = sdc.code) and (shc.savingscode = sdc.savingscode));
/*credit balance*/
begin
insert into smultistatement
(code, dated, debit, credit, description, savingsloancode, reportno, thetype)
select :customercode, :opbalenddate, 0,(shc.credit - shc.debit),
'Bal b/f', svg.description, svg.reportno, 'D'
from sharesdrcr shc, savings svg
where (shc.credit > shc.debit) and shc.savingscode = svg.code;
end
/*debit balance*/
begin
insert into smultistatement
(code, dated, debit, credit, description, savingsloancode, reportno, thetype)
select :customercode, :opbalenddate, (shc.debit - shc.credit), 0,
'Bal b/f', svg.description, svg.reportno, 'D'
from sharesdrcr shc, savings svg
where (shc.credit < shc.debit) and shc.savingscode = svg.code;
end
/*zero balance*/
begin
insert into smultistatement
(code, dated, debit, credit, description, savingsloancode, reportno, thetype)
select :customercode, :opbalenddate, 0, 0, 'Bal b/f', svg.description, svg.reportno,
'D'
from sharesdrcr shc, savings svg
where (shc.credit = shc.debit) and shc.savingscode = svg.code;
end
/*END OF PROCESSING*/