The next enhancement to be added is a posting routine. We have a customer file "vidcust", and we want to store all the sales made to this customer in this file, as well as payments. An added benefit of doing this is that we can immediately see, by going to this file what the customer's current Balance_Due is.
For this posting routine to be robust and work under all situations, adding new receipts, modifying old receipts, deleting receipts, we need to make use of a small trick. We are going to take a "snapshot" of every record before the user does anything to it. This way, we can compare the snapshot of the "before" image with the way the record is left "after" the user is done updating it, and post any and all changes to the customer file.
The best place to take the "before" picture is the AUTO processing table, since this table always runs just after the record is retrieved from the disk and just before the user sees it on his screen.
We only need to snap a picture of two fields, the charges and the payments. If either or both of these change, we will send (post) those changes over to the customer file.
Enter the following code on line 2. This will save the "before" values of the charges and payments into "oc" and "op".
We will put the guts of the posting routine on the INPUT table for "vidrec".
First, add the following code on line 1.
Add the actual "post" subroutine as shown below.
An explanation of this code in English is fairly simple. When the user presses ESC to store the receipt record, the first thing to run is the "post" subroutine. It simply goes out to the appropriate customer record (using the unique account number for the lookup) and adds the difference between what is in the Charges field now (13) and what used to be in the Charges field ("oc") to the Charges field on the customer record. The same is true for the payments field. The difference between what is in the Payments field now (14) and what used to be in the Payments field ("op") is added to the Payments field in the customer record. Then the Balance_Due field of the customer record is recalculated.
This code will work whether the receipt is new or old. If the receipt is new, the snapshot value of charges and payments will be "0". The amount posted to the customer file will be the new value minus "0", which is the new value. If the receipt is old and the values are just being modified, only the difference will post. It always works under all possible situations.
Remember that each time the AUTO table runs, all regular dummy fields are cleared. The code we put on the AUTO table will reassign these dummies each time a new record is brought to the screen.
Try out this code in the receipts file.
Enter the following data.
When you are done, store this receipt.
Go to the customer file "vidcust" and see if the posting worked. You should see the following.
To finish the "post" routine, we must add a routine that will "back out" a receipt's previous postings if the entire receipt is deleted with the (D)elete key. This is an even simpler operation. Add the following code as an @key routine that does a lookup to the customer file and subtracts the charges on the receipt from the charges on the customer record, and subtracts the payments on the receipt from the payments on the customer record, then recalculates the Balance_Due on the customer record. Once the postings have been "reversed" or "backed out", the routine actually deletes the receipt record itself.
Enter the code from the following two screens into "vidrec/input" .
Try this code by deleting the receipt you just added. When you press D on this record, you will see:
Answer Y to this prompt and then exit out of the receipts file.
Go to the customer file and you should see that the record has been correctly updated.