Skip to content

Instantly share code, notes, and snippets.

@timsayshey
Created July 27, 2012 14:50
Show Gist options
  • Save timsayshey/3188466 to your computer and use it in GitHub Desktop.
Save timsayshey/3188466 to your computer and use it in GitHub Desktop.
Model or Conroller or Service or ???
<cfscript> //Needed for code coloring
/**
* @hint This is the Shop controller which handles the shopping cart
*/
component extends="Controller" {
/**
* @hint Runs before functions
*/
public function init()
{
super.init(); // Include Parent Controller's Init Function
}
/**
* @hint Return a query loaded with all details of product from database
*/
public function getCart()
{
var shopCart = {}; // Return a struct that contains a query with all item info including extended totals, subtotal, and ???
var item = "";
var columns = "";
var columnValue = ArrayNew(1);
var subTotal = 0;
var i = 0;
var j = 0;
shopCart.cartContents = QueryNew(""); // Create an empty query inside the structure for the item info
shopCart.subTotal = 0;
createCart(); // Make sure a cart exists
//writeDump(createCart); abort;
// Get an empty item query
qItem = model("Item").findOne(
include="ItemPrice,itemStatus",
cache=true,
returnAs="struct"
);
// Create an empty bean and use it to populate the query columns, then add the additional columns we need (quantity, extendedPrice)
columns = listToArray(structKeyList(qItem));
for(j=1; j lte arrayLen(columns); j++)
{
queryAddColumn(shopCart.cartContents,columns[j],columnValue);
}
queryAddColumn(shopCart.cartContents,'Quantity',columnValue);
queryAddColumn(shopCart.cartContents,'ExtendedPrice',columnValue);
// Add row for each item in CLIVAR list; add everything from DB; calculate and add extended pricing
for(i=1; i lte listLen(client.cartItemList); i++)
{
//instance.itemBean.setCurrentItemById(ListGetAt(client.cartItemList,i));
item = model("Item").findOne(
include="ItemPrice,itemStatus",
cache=true,
returnAs="struct",
where="id = '#ListGetAt(client.cartItemList,i)#'"
);
columns = structKeyList(item);
//writeDump(qItem);abort;
queryAddRow(shopCart.cartContents);
for(j=1;j lte listLen(columns); j++)
{
querySetCell(shopCart.cartContents,listGetAt(columns,j),item[listGetAt(columns,j)],i);
}
querySetCell(shopCart.cartContents,"Quantity",ListGetAt(client.cartQtyList,i));
shopCartContentsItem = shopCart.cartContents.itemprice[i];
querySetCell(shopCart.cartContents,"ExtendedPrice",ListGetAt(client.cartQtyList,i)*REReplace(shopCartContentsItem.price,'[^\d\.]*',''));
shopCart.subTotal += shopCart.cartContents.extendedPrice[i];
//shopCart.cartContents.extendedPrice[i] = dollarFormat(shopCart.cartContents.extendedPrice[i]);
}
return shopCart;
}
/**
* @hint Adds submitted item to cart
*/
public function add(string itemCode="", string itemQty="")
{
// If arguments are empty then set them
if(isDefined("params.itemCode") && isDefined("params.itemQty") && !len(arguments.itemCode) && !len(arguments.itemQty))
{
arguments.itemCode = params.itemCode;
arguments.itemQty = params.itemQty;
}
var iId = getIdFromCode(arguments.itemCode);
var iQty = arguments.itemQty;
var currentListPos = 0;
// No negative quantities
if (iQty le 0)
{
iQty = Abs(iQty);
}
// Make sure a cart exists
createCart();
// Check for itemId in cart list
currentListPos = ListFind(client.cartItemList,iId);
// If not in the cart, add it
if (currentListPos eq 0)
{
client.cartItemList = ListAppend(client.cartItemList,iId);
client.cartQtyList = ListAppend(client.cartQtyList,iQty);
}
else // item is already in the cart, so change the quantity
{
listSetAt(client.cartQtyList,currentListPos,iQty);
}
// Update currentListPos
currentListPos = ListFind(client.cartItemList, iId);
// remove item from the cart. This should be an UPDATE
if (iQty eq 0)
{
client.cartItemList = ListDeleteAt(client.cartItemList, currentListPos);
client.cartQtyList = ListDeleteAt(client.cartQtyList, currentListPos);
}
else // update cart quantity
{
client.cartQtyList = ListSetAt(client.cartQtyList, currentListPos, iQty);
}
// writeDump(getCart()); abort;
//params.viewOverride = "test";
redirectTo(controller="show",action="cart");
}
/**
* @hint This method removes a single item from the cart
*/
public function remove(required string itemCode)
{
add(arguments.itemCode,0);
}
/**
* @hint This method empties the cart
*/
public function emptyCart()
{
client.cartItemList="";
client.cartQtyList="";
}
/**
* @hint This method returns a structure containing the total weight, volume and quantity of items currently in the shopping cart
*/
public function cartData()
{
var current = getCart();
var result = structNew();
var i=0;
result.CartWeightUPS = 0;
result.UPSPackages = 0;
result.CartWeightUPSAdd = 0;
result.UPSAddPackages = 0;
result.CartWeightUSPS = 0;
result.USPSPackages = 0;
result.CartWeightUSPSAdd = 0;
result.USPSAddPackages = 0;
result.CartWeight = 1.25; // Default starting weight to account for packing materials
result.CartCube = 0;
result.CartQuantity = 0;
for (i=1;i lte current.cartcontents.recordCount; i++) {
// Process out all of the weights and volumes while totaling the list
if (current.cartContents.UPSShipStatID[i] NEQ 3)
{
result.CartWeightUPS += current.cartContents.Weight[i] * current.cartContents.Quantity[i];
result.UPSPackages = 1;
}
else
{
result.CartWeightUPSAdd = result.CartWeightUPS+(70 * current.cartContents.Quantity[i]);
result.UPSAddPackages += current.cartContents.Quantity[i];
}
if (current.cartContents.USPSShipStatID[i] NEQ 5)
{
result.CartWeightUSPS += current.cartContents.Weight[i] * current.cartContents.Quantity[i];
result.USPSPackages = 1;
}
else
{
result.CartWeightUSPSAdd = result.CartWeightUSPS+(15 * current.cartContents.Quantity[i]);
result.USPSAddPackages += current.cartContents.Quantity[i];
}
result.CartWeight += current.cartContents.Weight[i] * current.cartContents.Quantity[i];
result.CartCube += current.cartContents.Volume[i] * current.cartContents.Quantity[i];
}
result.CartQuantity = current.cartcontents.recordCount;
return result;
}
/**
* @hint This method changes item quantities based on the struct passed to it
*/
public function updateCartQtys(required itemQtys)
{
for (item in itemQtys)
{
add(item,itemQtys[item]);
}
}
// Private Functions
/**
* @hint Returns true if a cart already exists in the CLIENT scope
*/
private function cartExists()
{
var retVal = false;
if (structKeyExists(client,"cartItemList"))
{
retVal = true;
}
return retVal;
}
/**
* @hint Creates cart in client scope if it does not exist. Returns TRUE if a cart had to be created, FALSE if not.
*/
private function createCart()
{
var created=false;
// Create a cart if it doesn't exist
if (!structKeyExists(client,"cartItemList"))
{
client.cartItemList = "";
client.cartQtyList = "";
created=true;
}
return created;
}
/**
* @hint Returns a query of cart information stored in the CLIENT scope
*/
private function getRawCart()
{
var shopCart = QueryNew("Id,Quantity");
var i = 0;
var limit = 0;
// Make sure a cart exists
createCart();
limit = ListLen(client.cartItemList);
// Add row for each item in CLIVAR list
for(i=1; i lte limit; i++)
{
QueryAddRow(shopCart);
QuerySetCell(shopCart,"Id",ListGetAt(client.cartItemList,i));
QuerySetCell(shopCart,"Quantity",ListGetAt(client.cartQtyList,i));
}
return shopCart;
}
/**
* @hint Returns the item id in the database of the given product code
*/
private function getIdFromCode(any itemCode)
{
var qItem = model("Item").findOne(where="itemCode = '#arguments.itemCode#'", cache=true, returnAs="query");
return qItem.id;
}
}
</cfscript> //Needed for code coloring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment