HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/web/circ.itp/wc-order-items.asp
<%@ LANGUAGE="VBScript" %>
<%
Option Explicit
Response.Buffer = True

' ==========================
' Authentication (shared with wc-orders.asp)
' ==========================
Const ADMIN_USERNAME = "woo"
Const ADMIN_PASSWORD = "woowoo#42"
Const COOKIE_NAME = "auth_token"

Dim expectedToken
Function SimpleHash(str)
    Dim i,h
    h=0
    For i=1 To Len(str)
        h=(h+Asc(Mid(str,i,1))*i) Mod 99999
    Next
    SimpleHash=CStr(h)
End Function
expectedToken = SimpleHash("secure-salt-" & ADMIN_PASSWORD)
If Request.Cookies(COOKIE_NAME) <> expectedToken Then
    Response.Status="403 Forbidden"
    Response.Write "<div class='text-danger p-3'>Access denied.</div>"
    Response.End
End If

' ==========================
' Helper Functions
' ==========================
' Null/blank -> 0 for safe numeric math
Function Nz(val)
    If IsNull(val) Or val = "" Then
        Nz = 0
    Else
        Nz = val
    End If
End Function

' VBScript doesn't have IIf; add a tiny helper
Function IIf(cond, t, f)
    If cond Then IIf = t Else IIf = f End If
End Function

' ==========================
' Get order ID
' ==========================
Dim orderId
orderId = CLng(0)
If IsNumeric(Request.QueryString("orderid")) Then orderId = CLng(Request.QueryString("orderid"))
If orderId = 0 Then
    Response.Write "<div class='text-danger p-3'>Invalid Order ID.</div>"
    Response.End
End If

' ==========================
' Database connection
' ==========================
Dim conn, rs, sql
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=MySQL_castironradcen;"

sql = "SELECT OrderItemID, ProductCode, ProductName, Qty, PriceEachBeforeDiscount, PriceEach, " & _
      "AccessoryPack, Option1Name, Option1Value, Option2Name, Option2Value " & _
      "FROM circ_penn.wc_order_items WHERE WooCommerceOrderID=" & orderId & _
      " ORDER BY OrderItemID"

Set rs = conn.Execute(sql)
%>

<div class="table-responsive">
<table class="table table-sm table-bordered align-middle">
<thead class="table-light align-middle">
<tr>
  <th>Item ID</th>
  <th>Product Code</th>
  <th>Product Name</th>
  <th class="text-end">Qty</th>
  <th class="text-end">
    Price Each<br>
    <small class="text-muted">(Before Discount)</small>
  </th>
  <th class="text-end">
    Price Each<br>
    <small class="text-muted">(After Discount)</small>
  </th>
  <th class="text-end">% Off</th>
  <th class="text-center">Accessory Pack</th>
  <th>Option 1</th>
  <th>Option 2</th>
</tr>
</thead>
<tbody>
<%
Dim accessoryDisplay, opt1, opt2
Dim totalQty, totalAfter, totalBefore
totalQty = 0
totalAfter = 0
totalBefore = 0

If Not rs.EOF Then
    Do Until rs.EOF
        ' Accessory tick
        If rs("AccessoryPack") = 1 Then accessoryDisplay = "<span class='tick'>&#10004;</span>" Else accessoryDisplay = ""

        ' Options formatting
        opt1 = ""
        If Trim(rs("Option1Name")&"") <> "" Then opt1 = "<strong>" & Server.HTMLEncode(rs("Option1Name")) & ":</strong><br>" & Server.HTMLEncode(rs("Option1Value"))
        opt2 = ""
        If Trim(rs("Option2Name")&"") <> "" Then opt2 = "<strong>" & Server.HTMLEncode(rs("Option2Name")) & ":</strong><br>" & Server.HTMLEncode(rs("Option2Value"))

        ' Numeric conversions (null-safe for math)
        Dim qty, priceEach, priceBefore, discountPct
        qty = CDbl(Nz(rs("Qty")))
        priceEach = CDbl(Nz(rs("PriceEach")))
        priceBefore = CDbl(Nz(rs("PriceEachBeforeDiscount")))

        ' Totals
        totalQty = totalQty + qty
        totalAfter = totalAfter + (qty * priceEach)
        totalBefore = totalBefore + (qty * priceBefore)

        ' Discount %
        If priceBefore > 0 Then
            discountPct = ((priceBefore - priceEach) / priceBefore) * 100
        Else
            discountPct = 0
        End If

        ' Display strings (blank if null)
        Dim priceBeforeDisplay, priceEachDisplay
        If IsNull(rs("PriceEachBeforeDiscount")) Or rs("PriceEachBeforeDiscount") = "" Then
            priceBeforeDisplay = ""
        Else
            priceBeforeDisplay = "£" & FormatNumber(rs("PriceEachBeforeDiscount"), 2)
        End If

        If IsNull(rs("PriceEach")) Or rs("PriceEach") = "" Then
            priceEachDisplay = ""
        Else
            priceEachDisplay = "£" & FormatNumber(rs("PriceEach"), 2)
        End If

        ' Output row
        Response.Write "<tr>"
        Response.Write "<td>" & rs("OrderItemID") & "</td>"
        Response.Write "<td>" & Server.HTMLEncode(rs("ProductCode")) & "</td>"
        Response.Write "<td>" & Server.HTMLEncode(rs("ProductName")) & "</td>"
        Response.Write "<td class='text-end'>" & qty & "</td>"
        Response.Write "<td class='text-end'>" & priceBeforeDisplay & "</td>"
        Response.Write "<td class='text-end'>" & priceEachDisplay & "</td>"
        Response.Write "<td class='text-end text-danger'>" & IIf(priceBefore>0, FormatNumber(discountPct,1)&"%","") & "</td>"
        Response.Write "<td class='text-center'>" & accessoryDisplay & "</td>"
        Response.Write "<td>" & opt1 & "</td>"
        Response.Write "<td>" & opt2 & "</td>"
        Response.Write "</tr>"

        rs.MoveNext
    Loop
Else
    Response.Write "<tr><td colspan='10' class='text-center text-muted py-3'>No items found for this order.</td></tr>"
End If
%>
</tbody>

<%
' ==========================
' Totals Footer
' ==========================
If totalQty > 0 Then
    Dim avgDiscount
    If totalBefore > 0 Then
        avgDiscount = ((totalBefore - totalAfter) / totalBefore) * 100
    Else
        avgDiscount = 0
    End If

    Response.Write "<tfoot class='table-light fw-bold'>"
    Response.Write "<tr>"
    Response.Write "<td colspan='3' class='text-end'>Totals:</td>"
    Response.Write "<td class='text-end'>" & totalQty & "</td>"
    Response.Write "<td class='text-end'>£" & FormatNumber(totalBefore,2) & "<br><span class='text-muted small'>(Before Discount)</span></td>"
    Response.Write "<td class='text-end'>£" & FormatNumber(totalAfter,2) & "<br><span class='text-muted small'>(After Discount)</span></td>"
    Response.Write "<td class='text-end text-danger'>" & FormatNumber(avgDiscount,1) & "%<br><span class='text-muted small'>(Avg)</span></td>"
    Response.Write "<td colspan='3'></td>"
    Response.Write "</tr>"
    Response.Write "</tfoot>"
End If

rs.Close:Set rs=Nothing
conn.Close:Set conn=Nothing
%>
</table>
</div>

<style>
.tick{color:#28a745;font-size:1.2em}
tfoot td{background-color:#f8f9fa!important;vertical-align:middle}
th small{font-size:0.7em;display:block;line-height:1}
</style>