""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   PRINTING BILLS WHEN THE RATE VARIES DEPENDING ON USAGE
   """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   PRODUCT   :  R:BASE                  VERSION      :  3.1
   CATEGORY  :  PROGRAMMING             SUBCATEGORY  :  TOOLS
   """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   Here's a report that prints monthly bills by account when the hourly
   rate varies depending on customer usage during the month--the more
   hours used, the lower the hourly rate.
 
   The database has two tables--USAGE (with accounts, service dates, and
   hours used), and RATES (with the rate schedule). The problem is how do
   you get a correct total bill amount for all accounts when you can't
   determine the correct rate until after you add up all the hours for a
   given account.
 
 
   Sample Data in USAGE
   """"""""""""""""""""
   ACCOUNT    SERVICE  QUANTITY
   -------    -------- --------
   AA-0001    12/01/90    2
   AA-0002    12/02/90    3
   AA-0001    12/03/90    6
   AA-0002    12/03/90    1
   AA-0001    12/04/90    8
 
 
   Rate Schedule in RATES
   """"""""""""""""""""""
   H_MIN      H_MAX      H_RATE
   -----      -----      ------
     0         10        $15.00
    10         20        $14.50
    20         50        $14.00
    50        100        $13.00
   100    1000000        $12.00
 
 
   Step by Step Instructions
   """""""""""""""""""""""""
 
   STEP 1--Before defining the report issue the following SET VAR command
   to initialize the variables:
 
     SET VAR vtotalprev CURRENCY = $0, vacctotal CURRENCY = $0, +
       vcorrectbill CURRENCY = $0, vtotalbill CURRENCY = $0
 
   STEP 2--Bring up Reports, name the report, and base it on USAGE.
   Define the following expressions in this order:
 
     vsumhours = SUM OF quantity
     vrate = h_rate IN rates WHERE h_min <= .vsumhours AND h_max > .vsumhours
     vaccprev CURRENCY = .vtotalbill
     vacctotal = (.vrate * .vsumhours)
     vtotalbill = (.vtotalbill + .vacctotal)
     vtotalprev = (IFEQ(.vaccprev,0,.vcorrectbill,.vtotalprev))
     vcorrectbill = (IFEQ(.vaccprev,0,(.vtotalprev + .vacctotal),
       (.vtotalprev + (.vtotalbill - .vaccprev))))
 
   STEP 3--Define one breakpoint on ACCOUNT, and reset the variables
   VSUMHOURS, VACCTOTAL, and VTOTALBILL at that breakpoint.
 
   STEP 4--Locate VACCTOTAL in the break footer section to print the
   account total, and locate VCORRECTBILL in the report footer section to
   print the correct total bill for all accounts.
 
   STEP 5--Each time before printing the report, use the SET VAR shown in
   step 1 to initialize VTOTALPREV, VACCTOTAL, VCORRECTBILL, and
   VTOTALBILL.
 
 
   Final Report Output
   """""""""""""""""""
   The final report might look like this:
 
     Account AA-0001
            Hours     Service Date
              2          12/01/90
              6          12/03/90
              8          12/04/90
            =====
             16 hours at $14.50 per hour: $232.00
 
     Account AA-0002
            Hours     Service Date
              3          12/02/90
              1          12/03/90
            =====
              4 hours at $15.00 per hour:  $60.00
 
     Total billings:    $292.00
 
   This works because a report, unlike a form, evaluates its expressions
   only once for each row and evaluates them in the order they appear. By
   playing with the expression order, you can have a variable (VACCPREV)
   save a value from the previous row (.VTOTALBILL) to use in lower
   expressions (VTOTALPREV and VCORRECTBILL).