
#!/bin/ksh
# spereport, a script to generate a fund report for a given week or YTD.
#

set -u ; # exit on unset shell variable

########################################################################
# Report for one week all special funds with activity this week
########################################################################
function Report_this_weeks_fund_activity {

dist DAMT by FUND in fund into tmp1wk${1} where Wk eq ${1} and FUND lt 0;
ujoin -q Fund_Number name from Funds tmp1wk${1} into tmp2wk${1} ;
uprint Fund_Desc total in tmp2wk${1} into fundwk${1} ;

} # end function Report_this_weeks_fund_activity 

########################################################################
# Report for all special funds month to date
########################################################################
function Report_fund_activity_mtd {

dist DAMT by FUND in fund into tmp5wk${1} where Wk ge ${2} and Wk le ${1} \
and FUND lt 0;
ujoin -q Fund_Number name from Funds tmp5wk${1} into tmp6wk${1} ;
uprint Fund_Desc total in tmp6wk${1} into fundmonth${1} ;

} # end function Report_fund_activity_mtd 

########################################################################
# Report for all special funds year to date
########################################################################
function Report_fund_activity_ytd {

dist DAMT by FUND in fund into tmp3wk${1} where Wk ge 1 and Wk le 52 \
and FUND lt 0;
ujoin -q Fund_Number name from Funds tmp3wk${1} into tmp4wk${1} ;
uprint Fund_Desc total in tmp4wk${1} into fundtotal${1} ;

} # end function Report_fund_activity_ytd 

########################################################################
# Ouput fund report
########################################################################
function Output_fund_report {

{
cat fund.header
date
} > fundtmp${1} ;

print "" >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "SPECIAL FUND REPORT FOR WEEK ${1}" >> fundtmp${1} ;
cat fundwk${1} >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "SPECIAL FUND REPORT FROM WEEK ${2} TO WEEK ${1}" >> fundtmp${1} ;
cat fundmonth${1} >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "SPECIAL FUND REPORT YEAR-TO-DATE" >> fundtmp${1} ;
cat fundtotal${1} >> fundtmp${1} ;

adjust -c < fundtmp${1} | expand > sperptwk${1} ;
print "" >> sperptwk${1} ;

} # end function Output_fund_report  

########################################################################
# Main script body begins here
########################################################################

if [ "$#" -ne 2 ]
then
     echo "Usage: spereport Current_Week_Number Start_Of_Month_Week_Number"
     echo "ie. spereport 8 6 (generate deposit slip for week 8 where 6 is 1st wk)"
     exit 1 ;
fi ;

Report_this_weeks_fund_activity ${1} ;
Report_fund_activity_mtd ${1} ${2};
Report_fund_activity_ytd ${1} ;
Output_fund_report ${1} ${2};

rm tmp1wk${1} ;
rm Dtmp1wk${1} ;
rm tmp2wk${1} ;
rm Dtmp2wk${1} ;
rm tmp3wk${1} ;
rm Dtmp3wk${1} ;
rm tmp4wk${1} ;
rm Dtmp4wk${1} ;
rm tmp5wk${1} ;
rm Dtmp5wk${1} ;
rm tmp6wk${1} ;
rm Dtmp6wk${1} ;
rm fundwk${1} ;
rm fundmonth${1} ;
rm fundtotal${1} ;
rm fundtmp${1} ;

exit 0
