<!--- 
 Filename: StoreCart.cfm (save as StoreCart.cfm)
 Created by: Nate Weiss (NMW)
 Purpose: Displays the current user's shopping cart
 Please Note Depends on the <CF_ShoppingCart> custom tag
--->

<!--- Show header images, etc., for Online Store --->
<cfinclude template="StoreHeader.cfm">

<!--- If MerchID was passed in URL --->
<cfif isDefined("URL.addMerchID")>
  <!--- Add item to user's cart data, via custom tag --->
  <cf_ShoppingCart
  action="Add"
  merchID="#URL.addMerchID#">

<!--- If user is submitting cart form ---> 
<cfelseif isDefined("FORM.merchID")>
  <!--- For each MerchID on Form, Update Quantity --->
  <cfloop list="#FORM.merchID#" index="thisMerchID">
    <!--- Update Quantity, via Custom Tag --->
    <cf_ShoppingCart
    action="Update"
    merchID="#thisMerchID#"
    quantity="#FORM['Quant_#thisMerchID#']#">
  </cfloop>
 
  <!--- If user submitted form via "Checkout" button, --->
  <!--- send on to Checkout page after updating cart. --->
  <cfif isDefined("FORM.isCheckingOut")>
    <cflocation url="../28/StoreCheckout.cfm">
  </cfif>
</cfif>

<!--- Get current cart contents, via Custom Tag --->
<cf_ShoppingCart
 action="List"
 returnVariable="getCart">
 

<!--- Stop here if user's cart is empty --->
<cfif getCart.recordCount eq 0>
 There is nothing in your cart.
 <cfabort>
</cfif>


<!--- Retrieve items in user's cart from database --->
<cfquery name="getMerch" datasource="ows">
 SELECT MerchID, MerchPrice
 FROM Merchandise
 WHERE MerchID IN (#valueList(getCart.MerchID)#)
</cfquery> 

<!--- Use QofQ to join queried records against cart data --->
<!--- The SubTotal column is Quantity times MerchPrice --->
<cfquery name="getPrices" dbtype="query">
 SELECT 
 GetMerch.MerchID AS MerchID,
 GetCart.Quantity AS Quantity,
 (GetCart.Quantity * GetMerch.MerchPrice) AS SubTotal
 FROM GetMerch, GetCart
 WHERE GetMerch.MerchID = GetCart.MerchID
</cfquery>

<!--- Use QofQ again to get a grand total of all items --->
<cfquery name="getTotal" dbtype="query">
 SELECT SUM(SubTotal) AS GrandTotal
 FROM GetPrices
</cfquery>

<!--- Create form that submits to this template --->
<cfform action="#CGI.script_name#">
 <table>
 <tr>
 <th colspan="2" bgcolor="silver">Your Shopping Cart</th>
 </tr>
 <!--- For each piece of merchandise --->
 <cfloop query="getPrices">
 <tr>
 <td>
 <!--- Show this piece of merchandise --->
 <cf_MerchDisplay
 merchID="#getPrices.MerchID#"
 showAddLink="No">
 </td> 
 <td>
 <!--- Display Quantity in Text entry field --->
 <cfoutput>
 Quantity: 
 <cfinput type="Hidden" name="merchID" 
 value="#GetPrices.MerchID#">
 <cfinput type="Text" size="3" 
 name="Quant_#getPrices.MerchID#" 
 value="#getPrices.Quantity#"><br>
 Subtotal: #lsCurrencyFormat(subTotal)# 
 </cfoutput>
 </td>
 </tr>
 </cfloop>
 
 <tr>
 <td></td>
 <td>
 <!--- Display Grand Total for all items in cart --->
 <cfoutput>
 <b>Total: #lsCurrencyFormat(getTotal.GrandTotal)#</b>
 </cfoutput>
 </td>
 </tr>
 </table>
 
 <!--- Submit button to update quantities --->
 <cfinput type="submit" name="submit" value="Update Quantities">

 <!--- Submit button to Check out --->
 <cfinput type="submit" value="Checkout" name="isCheckingOut">
</cfform>



<!--- Convert current cart contents to comma-sep list --->	
<cfset currentMerchList = valueList(getCart.MerchID)>

<!--- Run query to suggest other items for user to buy --->
<cfquery name="getSimilar" datasource="#APPLICATION.dataSource#"
 cachedWithin="#createTimeSpan(0,0,5,0)#" maxrows="3">
 <!--- We want all items NOT in user's cart... --->
 SELECT ItemID
 FROM MerchandiseOrdersItems
 WHERE ItemID NOT IN (#currentMerchList#)
 <!--- ...but that *were* included in other orders --->
 <!--- along with items now in the user's cart... --->
 AND OrderID IN 
 (SELECT OrderID FROM MerchandiseOrdersItems
 WHERE ItemID IN (#currentMerchList#)
 <!--- ...not including this user's past orders! --->
 <cfif isDefined("SESSION.auth.contactID")>
 AND OrderID NOT IN
 (SELECT OrderID FROM MerchandiseOrders
 WHERE ContactID = #SESSION.auth.contactID#)
 </cfif> )
</cfquery>

<!--- If at least one "similar" item was found --->
<cfif getSimilar.recordCount GT 0>
 <p>People who have purchased items in your 
 cart have also bought the following:<br>

 <!--- For each similar item, display it, via Custom Tag --->
 <!--- (show five suggestions at most) --->
 <cfloop query="getSimilar">
   <cf_MerchDisplay merchID="#getSimilar.ItemID#">
 </cfloop>
</cfif>
