#!/bin/ksh
# speslip, a program to generate a bank deposit slip for a given week from
# a database of names and a database of contributions
# %W%

set -u ; # exit on unset shell variable

# Global variables

typeset -R9 CHECKS="0"    # the total of checks returned by function get_checks
typeset -R9 CASH_TOT="0"  # the total of all cash returned by function get_cash
typeset -R9 CURRENCY="0"  # the total currency amount entered by user
typeset -R9 COINS="0"     # the total coins amount entered by user
typeset -R9 TOTAL="0"     # the total of currency + coins + checks
typeset  input="0"        # a dummy global to hold input read from user
typeset  temper="0"       # a dummy global to hold last value of variable
typeset -R9 DELTA="1"        # the difference between db cash and counted cash




######################################################################
# Get the total donations paid by cash from the fund for a given week
######################################################################
function get_cash {

if [ "$#" -ne 1 ]
then
     echo "Usage: get_cash WeekNumber"
     exit 1
fi ;

stat DAMT in fund where S eq 0 and CKnum eq 0 and Wk eq $1 and FUND le 0\
| tail -1  | awk -f cash.awk

} # end of function get_cash



######################################################################
# Get the total donations paid by check from the fund for a given week
######################################################################
function get_checks {

if [ "$#" -ne 1 ]
then
     echo "Usage: get_checks WeekNumber"
     exit 1
fi ;

stat SAMT in fund where S le 1 and CKnum lt 0 and Wk eq $1 \
| tail -1  | awk -f cash.awk

} # end of function get_checks


######################################################################
# Get the total donations from the fund for a given week
######################################################################
function get_total {

if [ "$#" -ne 1 ]
then
     echo "Usage: get_total WeekNumber"
     exit 1
fi ;

stat DAMT in fund where Wk eq $1 and FUND lt 0 \
or Wk eq $1 and FUND gt 0 and CKnum lt 0 | tail -1  | awk -f cash.awk

} # end of function get_total


######################################################################
# Get the deposit record from the database
######################################################################
function get_deposit {

cp Dmaster Dmaster.tmp
cp Dmaster.132 Dmaster
index ID in master
index ID in fund
uselect Name_Last ID from master into namids
index ID in namids
uselect ID SAMT CKnum from fund into idamtck where S le 1 and CKnum lt 0 \
and Wk eq $1
index ID in idamtck
ujoin ID ID from namids idamtck into dep1
uselect CKnum Name_Last SAMT from dep1 into dep2
asort Name_Last in dep2 into dep3

} # end of function get_deposit  

######################################################################
# Output a deposit slip to file name speforwk${1}
######################################################################
function output_deposit {

uprint dep3 > speforwk$1
{
cat dep.Sheader 
date
} > dep.header.today
cat dep.header.today speforwk$1 > speforwktmp1
{
adjust -c < speforwktmp1 
} | expand > speforwktmp2
cat speforwktmp2 dep.trailer > speforwk${1} ;
sed "s/<<<<<<<<</${CHECKS}/" speforwk${1} > speforwktmp1 ;
sed "s/>>>>>>>>>/${COINS}/" speforwktmp1 > speforwktmp2 ;
sed "s/+++++++++/${CURRENCY}/" speforwktmp2 > speforwktmp1 ;
sed "s/;;;;;;;;;/${TOTAL}/" speforwktmp1 > speforwk${1} ;
print "" >> speforwk${1} ; # add a form feed at the end of the file

} # end of function nop output_deposit

######################################################################
#  Remove temporary files from directory and remove indexes on files
######################################################################
function clean_up {

`erase-index ID from master`
`erase-index ID from fund`
`erase-index ID from namids`
`erase-index ID from idamtck`
rm namids
rm Dnamids
rm dep1
rm Ddep1
rm dep2
rm Ddep2
rm dep3
rm Ddep3
rm idamtck
rm Didamtck
rm speforwktmp1 
rm speforwktmp2 
mv Dmaster.tmp Dmaster

} # end of function clean_up


############################################################################ 
# Read a currency  total from user and validate input
############################################################################ 
function Read_currency_amount {
while :
                do
    print -u2 ;
                temper=${CURRENCY} ;
                read input?"* Please enter the amount now., 
(Type 'a' to accept amount ${temper}):  " ;

   case ${input} in
       a) echo "${temper} is a valid amount." ;;
       [0-9]) CURRENCY=${input}.00 ;;
       [0-9][0-9]) CURRENCY=${input}.00 ;;
       [0-9][0-9][0-9]) CURRENCY=${input}.00 ;;
       [0-9][0-9][0-9][0-9]) CURRENCY=${input}.00 ;;
       [0-9][0-9][0-9][0-9][0-9]) CURRENCY=${input}.00 ;;
       [0-9][0-9][0-9][0-9][0-9][0-9]) CURRENCY=${input}.00 ;;
       [0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       [0-9][0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       [0-9][0-9][0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       [0-9][0-9][0-9][0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       [0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       [0-9][0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]) CURRENCY=${input} ;;
       *) print -u2 "${input} is not a valid amount." ;
          continue ;;
   esac ;
return 0
done ;
return 0 ;
} # End function Read_currency_amount 


############################################################################ 
# Read a coin total from user and validate input
############################################################################ 
function Read_coins_amount {
while :
                do
    print -u2 ;
                temper=${COINS} ;
                read input?"* Please enter the amount now., 
(Type 'a' to accept amount ${temper}, type q to quit program):  " ;

   case ${input} in
       a) echo "${temper} is a valid amount." ;;
       q) DELTA=0 ;; # Force an end to the program
       [0-9]) COINS=${input}.00 ;;
       [0-9][0-9]) COINS=${input}.00 ;;
       [0-9][0-9][0-9]) COINS=${input}.00 ;;
       [0-9][0-9][0-9][0-9]) COINS=${input}.00 ;;
       [0-9][0-9][0-9][0-9][0-9]) COINS=${input}.00 ;;
       [0-9][0-9][0-9][0-9][0-9][0-9]) COINS=${input}.00 ;;
       [0-9]\.[0-9][0-9]) COINS=${input} ;;
       [0-9][0-9]\.[0-9][0-9]) COINS=${input} ;;
       [0-9][0-9][0-9]\.[0-9][0-9]) COINS=${input} ;;
       [0-9][0-9][0-9][0-9]\.[0-9][0-9]) COINS=${input} ;;
       [0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]) COINS=${input} ;;
       [0-9][0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]) COINS=${input} ;;
       *) print -u2 "${input} is not a valid amount." ;
          continue ;;
   esac ;
return 0
done ;
return 0 ;
} # End function Read_coins_amount 


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

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

get_deposit ${1}

CASH_TOT=`get_cash ${1}`
print -u2 "The Cash total in the database for week ${1} is ${CASH_TOT}"

CHECKS=`get_checks ${1}`
TOTAL=`get_total ${1}`

until [ ${DELTA} -eq 0 ]
do
print -u2 
print -u2 "What is the currency amount?"
Read_currency_amount
print -u2 ${CURRENCY}
let "DELTA = ${CASH_TOT} - ${CURRENCY}"
print -u2 
print -u2 "What is the coins amount?"
Read_coins_amount
print -u2 ${COINS}
let "DELTA = ${DELTA} - ${COINS}"
print -u2 
print -u2 "The difference between database and counted cash is ${DELTA}"
print -u2 
done ;

output_deposit ${1}

clean_up
