Calculating Shipping and Tax
Introduction. Now
we will take a slight departure from our usual approach. I am
going to present two stored procedures, sp_GetShippingRate
and sp_GetTaxRate. But rather
than developing an ASP that uses them, I am going to present two code
snippets that will be used in the Payment.asp.
These snippets will be used to do the shipping and tax
computations. The Payment.asp will be presented in the next web
page within the Payment.asp.
The Jerke book uses a COM based object developed in Visual Basic to do these calculations. What you would do is create an ActiveX DLL in VB6 and then make an executable deployment with all of its dependencies and put it on the server. This DLL, Dynamic Link Library, could then be accessed from ASPs on the server. While this approach is very intelligent, I do not think we have the time for me to spend to develop it. |
Stored Procedure | Calling ASP |
sp_GetShippingRate sp_GetTaxRate |
Payment.asp |
The Stored Procedures. Since these are more familiar I will first list out the stored procedures that you should create using SQL and ASPs pretty much exactly like we have for the other stored procedures. The first file should be called sp_GetShippingRate.asp. |
<%@ Language=VBScript %> <% Option Explicit %> <!--#include File="adovbs.inc"--> <% ' Open a connection to our SQL Server database ' We will use the ADO Driver connection Dim connfoxFire, strSQLCreate Set connfoxFire = Server.CreateObject("ADODB.Connection") connfoxFire.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=YourUserName;UID=cis; PWD=csatqu" connfoxFire.Open ' Creating the SQL String to create the stored procedure strSQLCreate = "CREATE PROCEDURE sp_GetShippingRate AS " & _ "select * from shipping" connfoxFire.execute(strSQLCreate) |
While this actually returns all of the shipping rates
and the corresponding upper and lower limits from the table, it is used
as the basis of determining the exact shipping rate based on the number
of CDs that have been ordered in TaxShip.asp.
The second file should be called sp_GetTaxRate.asp. |
<%@ Language=VBScript %> <% Option Explicit %> <!--#include File="adovbs.inc"--> <% ' Open a connection to our SQL Server database ' We will use the ADO Driver connection Dim connfoxFire, strSQLCreate Set connfoxFire = Server.CreateObject("ADODB.Connection") connfoxFire.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=YourUserName;UID=cis; PWD=csatqu" connfoxFire.Open ' Creating the SQL String to create the stored procedure strSQLCreate = "CREATE PROCEDURE sp_GetTaxRate @chrState varchar(2) AS " & _ "select fltTaxRate from tax where chrState = @chrState" connfoxFire.execute(strSQLCreate) |
Like all of the other ASPs based on CREATE SQL commands, these files need to be uploaded to your web and then executed once. After they have been executed, you should get an error if you try to execute them again because the stored procedures should already be there. After these sp_name.asp files have been used they should be removed from your space on the server. |
The Tax Code Snippet. This snippet makes use of strState that contains the state of the shopper, and the intOrderTotal that contains the order total will be passed to the Tax snippet so that the tax can be obtained. |
' This tax snippet will be used in
Payment.asp
' Create an ADO database connection
Else
End If |
In truth, there are only two states that charge sales tax on such purchases, VA at 4.5% and TX at 8%. |
The Shipping Code Snippet. This snippet makes use of intQuantity representing the number of items being ordered is passed to the Shipping function so that the shipping costs, based purely on quantity ordered in this situation, can be returned. Often, the business logic associated with these computations is going to be more complex, but this approach still demonstrates a major approach to development. |
' The snippet will calculate the appropriate shipping ' Create an ADO database connection
Loop |
The shipping computations are actually based on quantities and reflected in the following table. With the way we have structured the code this could be easily enhanced. |
Lower Limit | Upper Limit | Total Cost |
1 item | 10 items | $2 for all |
11 items | 20 | $4 |
21 | 30 | $6 |
31 | - | $8 |