Calculating Sales Tax and Totals

We are going to add some enhancements to this receipt file now. We need to make it automatically calculate the subtotal, the tax and the totals each record generates. First we will work with the tax field.

We'll use the control file we built earlier to hold system generated unique numbers to help us with the tax calculation. This time, we will add a field to that file which will hold a static (or relatively static) piece of information. In this case, we want to store the tax rate for our state. When a charge is made for these rental videos, we can calculate the appropriate tax amount and add it to the total charge.

Go into Define Files on the "vidctrl" file.

Add the following field (#2).

images\File0117.gif

Press ESC to save your work.

Press X to finish the file design and enter a Y to "Create a screen" prompt. If you forget to do this, the old Screen 0 does not have this newly added field on it, and you will not be able to enter the tax rate.

images\File0118.gif

When you are done adding the field and creating the default screen, go into IUA on the "vidctrl" file. Choose Screen 0 and go to record #1 and update it as follows.

images\File0119.gif

Press ESC to save your work. Return to the main menu.

Let's add the processing to calculate tax. We will do this calculation when adding or modifying the charges on any receipt. The calculation will be done on the INPUT processing table, however, we can grab the tax rate from the control file on the automatic table. We will arrange the code to do this lookup only one time for each IUA session and store the tax rate in a global dummy field that will be available throughout the whole session. First, the AUTO part of this processing combination.

Go into Define Processing on the "vidrec" file. Choose AUT0 processing.

Enter the following code. Hint: Push down line 1 first using F3 and enter the new line 1. Then add the subroutine "gettax" starting on line 8 shown on the next screen.

images\File0120.gif

Press NxtPage and finish the AUTO code.

Enter the following processing. (Lines 8-11)

images\File0121.gif

Press ESC to save your work.

Choose INPUT processing.

We will build another subroutine to do the "totals" calculation for each record. It will add up the charges for each line item and put that value into the "Subtotal" field. It will also calculate the tax. The subtotal and tax will be added together to show the total charges. Payments will be subtracted from total charges to calculate the Balance_Due.

This could all be easily done on one processing line, but it will be shown here spread out across several lines so that each part of the routine can be easily understood. There will be a mix-and-match of field names and field numbers to aid in showing these calculations as clearly as possible.

Enter the following code as shown.

images\File0122.gif

This will take care of the actual calculations for all the totaling to be done on each record.

Now, we will write the code which tells the program "when" to do these calculations.

Incidentally, filePro will "close up" blank lines on a processing table when you save it. This does not hurt anything, assuming that you know the blank line was there and are aware that it will go away. Programmers often use blank lines to separate subroutines graphically while they are coding them and then let filePro close them up as the table is being stored. You can do this too, just be sure that the code from the routine on top will never fall "accidentally" to the routine below. In this case, there is a RETURN on line 14, there is no fear of this subroutine running into one below it. But if it is easier for you to enter code in separate clearly distinct pieces, do so. For example, you can enter the next "when-to-do-it" code starting on line 16. FilePro will close up the blank space when you save your work.

Enter the following code.

images\File0123.gif

Press ESC to save your work. (Remember, next time you look, line 15 will be "sucked up" and line 16 will be living there.)

Once again, it is time to try out the program. Incidentally, this is not an abnormal way of building a filePro (or any) application program. You design a little bit and then test it out. Design a little more and test it out. As you go, various modules of the program become solidified and need less work or enhancement. It would be difficult to write a completely functional program without testing at various points along the way... not unless you are Mozart reborn and think it all up in your head first, then just write it all down.

We will enter a known account code, and then enter in some fake catalog numbers and charges. Notice that the charges and tax are correctly calculated at each appropriate "trigger".

Enter the account code 103 and press ENTER.

images\File0124.gif

When you are done experimenting, leave IUA. It does not matter whether you save the record or not.

Did you notice that when you entered the Account# and the fields popped in, your cursor was left in the Last Name field instead of in the first Catalog# field? This is not very elegant. It would be better to have the cursor go directly into that field upon retrieving the account information.

To do this, Enter the following "fix" at line 9 of the INPUT table for "vidrec".

images\File0125.gif

This simple change tells filePro to put your cursor into field 17 on the current screen instead of letting it go into the next field on the cursor path (which is what happened when this was an END command.) You will see this new better behavior later when we do some more testing.