Modify Shopping Cart to Include Sale Tax

Project Description

We use Anuko ecommerce solution to handle online sales. We need to modify the shopping cart and order notifications to include sales tax.

Completion Notes

Let's see what this project requires. Apparently, we need to modify the database structure to include the sales tax, modify Java code in the content management system to handle it, and modify presentation pages to show the tax.

Project Plan

Our project plan is, therefore, may look like this:

  1. Modify the database.
  2. Modify Java code.
  3. Modify presentation pages.
  4. Test.
  5. Deploy and monitor incoming orders for any issues.

Modifying the Database Structure

As the tax percentage may be different depending on order destination (for example, local orders vs international), we need to accommodate for that. One way is to create another table for tax rates, depending on destination, and then reference it in the shipping_algorithms table.

Table tax_rates

/* Table for sales tax rates that can be referenced in shipping algorithms. */
create table tax_rates (
  id integer not null primary key,
  name varchar(100) not null,        -- a name for tax rate, ex: NO_TAX or NEW_ZEALAND
  tax_percent numeric(18,2) not null -- tax rate
);

Table shipping_algorithms

/* Shipping algorithms stores the names of shipping calculation algorithms we can use.
   Examples: NZ_ACROSS_TOWN, NZ_WITHIN_ISLAND, NZ_NATIONWIDE.
   A delivery area may associate itself with a shipping algorithm.
   Algorithms are hard-coded in Java code and use params defined for merchant.
 */
create table shipping_algorithms (
  id integer not null primary key,
  name varchar(100) not null, -- name for the algorithm
  tax_rate_id integer references tax_rates(id) on delete set null, -- tax rate id
  num_params smallint default 0, -- number of parameters the algorithm requires
  descr_part0 varchar(100), -- algorithm description part for param0
  descr_part1 varchar(100), -- algorithm description part for param1
  descr_part2 varchar(100), -- algorithm description part for param2
  descr_part3 varchar(100), -- algorithm description part for param3
  descr_part4 varchar(100), -- algorithm description part for param4
  descr_part5 varchar(100), -- algorithm description part for param5
  descr_part6 varchar(100), -- algorithm description part for param6
  descr_part7 varchar(100), -- algorithm description part for param7
  descr_part8 varchar(100), -- algorithm description part for param8
  descr_part9 varchar(100)  -- algorithm description part for param9 
);

The modification above adds the tax_rate_id field to the shipping_algorithms table and can be done like so:

alter table shipping_algorithms add tax_rate_id integer references tax_rates(id) on delete set null;

Let's also insert a couple of rows into tax_rates table so that we have something to work with:

insert into tax_rates values(1, 'NO_TAX', 0.00);
insert into tax_rates values(2, 'NEW_ZEALAND', 15.00);

Now we can modify shipping algorithms to reference tax rates with something similar to below.

update shipping_algorithms set tax_rate_id = 1 where id = 2;
update shipping_algorithms set tax_rate_id = 2 where id = 1;
update shipping_algorithms set tax_rate_id = 2 where id = 9;

Not all merchants need to use tax. How do we handle it? By adding the use_tax flag in the eshops table and setting it for shops that need it.

alter table eshops add use_tax char(1) default 'F';
update eshops set use_tax = 'F';
update eshops set use_tax = 'T' where site_id = 10023;

We also have to introduce the tax field into the orders table.

alter table orders add tax numeric(18,2);

Now we have the database ready and may proceed with CMS Java code modifications.

Modifying CMS Code

In this part of the project, we modify Java code to handle the tax. Classes that need to be adjusted are:


We do the necessary modifications to these classes so that the sales tax calculation takes place and is reflected in the total for the order.

Modifying Presentation Parts

Now we need to modify view_chart.htm presentation part so that we display the sales tax to a user, when it is not zero.

  // Print tax.
  if (cart.getTaxVal() > 0) {
    out.println('<tr><th colspan="5" align="right">GST:</th>');
    out.println('<td align="right">'+cart.getTax()+'</td></tr>');
  }
The above code adds a row with GST value when applicable.

We also have to change checkout_paypal.htm because the older way of doing things is no longer giving us a correct total amount.

--- C:/Users/Nik/AppData/Local/Temp/checkout_paypal.htm-revBASE.svn004.tmp.htm	Sun May 24 23:49:31 2015
+++ C:/svn/internal/web_parts/cms/htm/checkout_paypal.htm	Fri Jan 29 20:27:21 2016
@@ -20,8 +20,7 @@ if (request.getMethod().equalsIgnoreCase('POST') &
   out.println('<input name="item_number" value="'+cart.getOrderNumber()+'" type="hidden">');
   out.println('<input name="item_name" value="Order # '+cart.getOrderNumber()+'" type="hidden">');
   out.println('<input name="currency_code" value="'+site.getProperty("PREF_CURRENCY")+'" type="hidden">');
-  out.println('<input name="amount" value="'+cart.getSubtotalWithDiscount()+'" type="hidden">');
-  out.println('<input name="shipping" value="'+cart.getShippingCharge()+'" type="hidden">');
+  out.println('<input name="amount" value="'+cart.getTotal()+'" type="hidden">');
   out.println('<input type="hidden" name="return" value="http://www.doitcontractors.com/root/thanks.htm">');
   out.println('<input type="hidden" name="rm" value="2">');
   out.println('<noscript>');

Testing

Now it is a good time to do a local testing to see if everything is working as expected, the sales tax is applied where necessary, and there are no rounding errors or other problems.

Deployment

In this final stage of the project we deploy the application to production servers using the following 3 steps:


You can leave a comment on this project, or post a new project for consideration.