#!/bin/ksh
# fundreport, 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 funds with activity this week
########################################################################
function Report_this_weeks_fund_activity {

dist DAMT by FUND in fund into tmp1wk${1} where Wk eq ${1} ;
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 funds year to date
########################################################################
function Report_fund_activity_ytd {

dist DAMT by FUND in fund into tmp3wk${1} where Wk ge 6 and Wk le ${1} ;
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 "FUND REPORT FOR WEEK ${1}" >> fundtmp${1} ;
cat fundwk${1} >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "" >> fundtmp${1} ;
print "FUND REPORT YEAR-TO-DATE" >> fundtmp${1} ;
cat fundtotal${1} >> fundtmp${1} ;

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

} # end function Output_fund_report  

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

if [ "$#" -ne 1 ]
then
     echo "Usage: fundreport WeekNumber"
     echo "ie. fundreport 2 (generate deposit slip for week 2)"
     exit 1 ;
fi ;

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

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 fundwk${1} ;
rm fundtotal${1} ;
rm fundtmp${1} ;

exit 0
