<!--- 
 Filename: StoreCart.cfm (save in chapter 28s folder)
 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>

<!--- 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="getCart">
   <tr>
   <td>
   <!--- Show this piece of merchandise --->
   <cf_MerchDisplay merchID="#getCart.MerchID#" showAddLink="No">
   </td> 
   <td>
   <!--- Display Quantity in Text entry field --->
   <cfoutput>
   Quantity: 
   <cfinput type="hidden" name="merchID" value="#getCart.merchID#">
   <cfinput type="text" size="3" name="quant_#getCart.MerchID#" 
   value="#getCart.Quantity#"><br>
   </cfoutput>
   </td>
   </tr>
 </cfloop>

 </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>
