<!--- 
  Filename:     OrderHistory2.cfm
  Created by:   Nate Weiss (NMW)
  Date Created: 3/5/2001
--->

<!--- Retrieve user's orders, based on ContactID --->
<CFQUERY NAME="GetOrders" DATASOURCE="#DataSource#">
  SELECT OrderID, OrderDate, 
    (SELECT Count(*) 
     FROM MerchandiseOrdersItems oi
     WHERE oi.OrderID = o.OrderID) AS ItemCount
  FROM MerchandiseOrders o
  WHERE ContactID = #SESSION.Auth.ContactID#
  ORDER BY OrderDate DESC
</CFQUERY>

<!--- Determine if a numeric OrderID was passed in URL --->
<CFSET ShowDetail = IsDefined("URL.OrderID") AND IsNumeric(URL.OrderID)>

<!--- If an OrderID was passed, get details for order --->
<!--- Query must check against ContactID for security --->
<CFIF ShowDetail>
  <CFQUERY NAME="GetDetail" DATASOURCE="#DataSource#">
    SELECT m.MerchName, oi.ItemPrice, oi.OrderQty
    FROM Merchandise m, MerchandiseOrdersItems oi 
    WHERE m.MerchID = oi.ItemID
    AND oi.OrderID  = #URL.OrderID#
    AND oi.OrderID IN 
      (SELECT o.OrderID FROM MerchandiseOrders o
       WHERE o.ContactID = #SESSION.Auth.ContactID#)
  </CFQUERY>
  
  <!--- If no Detail records, don't show detail --->
  <!--- User may be trying to "hack" URL params --->
  <CFIF GetDetail.RecordCount EQ 0>
    <CFSET ShowDetail = False>
  </CFIF>
</CFIF>


<HTML>
<HEAD>
  <TITLE>Your Order History</TITLE>
  
  <!--- Apply some simple CSS style formatting --->
  <STYLE TYPE="text/css">
    BODY {font-family:sans-serif;font-size:12px;color:navy}
    H2   {font-size:20px}
    TH   {font-family:sans-serif;font-size:12px;color:white;
          background:MediumBlue;text-align:left}
    TD   {font-family:sans-serif;font-size:12px}
  </STYLE>  
</HEAD>
<BODY>

<!--- Personalized message at top of page--->
<CFOUTPUT>
  <h2>Your Order History</h2>
  <P><B>Welcome back, #SESSION.Auth.FirstName#</B>!<BR>
  You have placed <B>#GetOrders.RecordCount#</B> 
  orders with us to date.</P>
</CFOUTPUT>


<!--- Display orders in a simple HTML table --->
<TABLE BORDER="1" WIDTH="300" CELLPADDING="5" CELLSPACING="2">
  <!--- Column headers --->
  <TR>
    <TH>Date Ordered</TH>
    <TH>Items</TH>
  </TR>

  <!--- Display each order as a table row --->
  <CFOUTPUT QUERY="GetOrders">
    <!--- Determine whether to show details for this order --->
    <!--- Show "Down" arrow if expanded, otherwise "Right" --->
    <CFSET IsExpanded = ShowDetail AND (GetOrders.OrderID EQ URL.OrderID)>
    <CFSET ArrowIcon  = IIF(IsExpanded, "'ArrowDown.gif'", "'ArrowRight.gif'")>
  
    <TR>
      <TD>
        <!--- Link to show order details, with arrow icon --->
        <A HREF="OrderHistory2.cfm?OrderID=#OrderID#">
          <IMG SRC="../images/#ArrowIcon#" WIDTH="16" HEIGHT="16" BORDER="0">
          #DateFormat(OrderDate, "mmmm d, yyyy")#
        </A>
      </TD>
      <TD>
        <B>#ItemCount#</B>
      </TD>
    </TR>

    <!--- Show details for this order, if appropriate --->    
    <CFIF IsExpanded>
      <CFSET OrderTotal = 0>
      <TR>
        <TD COLSPAN="2">
        
          <!--- Show Details within nested table --->
          <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0">
            <!--- Nested table's column headers --->
            <TR>
              <TH>Item</TH><TH>Qty</TH><TH>Price</TH>
            </TR>

            <!--- Show each ordered item as table row --->
            <CFLOOP QUERY="GetDetail">
              <CFSET OrderTotal = OrderTotal + ItemPrice>
              <TR>
                <TD>#MerchName#</TD>
                <TD>#OrderQty#</TD>
                <TD>#DollarFormat(ItemPrice)#</TD>
              </TR>
            </CFLOOP>

            <!--- Last row in nested table for total --->            
            <TR>
              <TD COLSPAN="2"><B>Total:</B></TD>
              <TD><B>#DollarFormat(OrderTotal)#</B></TD>
            </TR>
          </TABLE>
        </TD>
      </TR>
    </CFIF>
  </CFOUTPUT>
</TABLE>

</BODY>
</HTML>
