File: D:/web/hyperflight/inxpress/inxpress - Copy (5).asp
<%@ Language="VBScript" %>
<%
Option Explicit
'===== Helper functions =====
Function IIf(expr, truePart, falsePart)
If expr Then IIf = truePart Else IIf = falsePart
End Function
Function FmtNum(val)
Dim n
If IsNull(val) Then FmtNum = "" : Exit Function
On Error Resume Next
n = CDbl(val)
On Error GoTo 0
If n = 0 Then FmtNum = "" Else FmtNum = FormatNumber(n, 2, -1, 0, -1)
End Function
Function NumVal(v)
If IsNull(v) Or Trim(v & "") = "" Then NumVal = 0 Else NumVal = CDbl(v)
End Function
Function SafeHTML(val)
On Error Resume Next
If IsNull(val) Then
SafeHTML = ""
Else
SafeHTML = Server.HTMLEncode(CStr(val))
End If
If Err.Number <> 0 Then
SafeHTML = ""
Err.Clear
End If
On Error GoTo 0
End Function
Function CleanServiceType(txt)
If IsNull(txt) Then CleanServiceType = "" : Exit Function
Dim s : s = Trim(CStr(txt))
If Right(s, 10) = " - Package" Then s = Left(s, Len(s) - 10)
CleanServiceType = Server.HTMLEncode(s)
End Function
Function CountryFlag(code)
If IsNull(code) Or Trim(code) = "" Then CountryFlag = "" : Exit Function
code = UCase(Trim(code))
CountryFlag = "<img src='/common/geoip/flags/" & code & ".svg' alt='" & code & _
"' style='width:20px;height:14px;border:1px solid #ccc;margin-right:4px;vertical-align:middle;'>"
End Function
'===== Config =====
Const PAGE_SIZE = 10000
Const DSN = "MySQL_hyperflight"
Dim conn : Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=" & DSN
'===== Filters =====
Dim searchText, carrierSel, invoiceSel, countrySel, sortField, sortDir, missingOrders, duplicateRefs
searchText = Trim(Request("search"))
carrierSel = Trim(Request("carrier"))
invoiceSel = Trim(Request("invoice"))
countrySel = Trim(Request("country"))
sortField = Trim(Request("sort"))
sortDir = UCase(Trim(Request("dir")))
missingOrders = (Request("missingorders") = "1")
duplicateRefs = (Request("duplicates") = "1")
If sortField = "" Then sortField = "shipment_id"
If sortDir <> "DESC" Then sortDir = "ASC"
Dim whereSQL : whereSQL = " WHERE 1=1"
If searchText <> "" Then whereSQL = whereSQL & " AND (s.Reference LIKE '%" & Replace(searchText,"'","''") & _
"%' OR s.ReceiverName LIKE '%" & Replace(searchText,"'","''") & "%')"
If carrierSel <> "" Then whereSQL = whereSQL & " AND s.Carrier='" & Replace(carrierSel,"'","''") & "'"
If invoiceSel <> "" Then whereSQL = whereSQL & " AND s.InvoiceNumber='" & Replace(invoiceSel,"'","''") & "'"
If countrySel <> "" Then whereSQL = whereSQL & " AND s.DestCountry='" & Replace(countrySel,"'","''") & "'"
If missingOrders Then whereSQL = whereSQL & " AND NOT EXISTS (SELECT 1 FROM orders o WHERE o.OrderNo=s.Reference)"
If duplicateRefs Then
whereSQL = whereSQL & " AND s.Reference <> '' AND s.Reference IN (" & _
"SELECT Reference FROM inxpress_shipments_parsed WHERE Reference <> '' GROUP BY Reference HAVING COUNT(*)>1)"
End If
Dim sortSQL : sortSQL = " ORDER BY s." & sortField & " " & sortDir
Dim sql : sql = "SELECT s.*, d.ShipmentDetails, d.ReceiverAddress, d.PiecesWeightDimensionsZone, d.Charges, " & _
"d.Total AS DetailTotal, d.Invoice_Number FROM inxpress_shipments_parsed s " & _
"LEFT JOIN inxpress_shipments d ON s.shipment_id=d.shipment_id" & _
whereSQL & sortSQL & " LIMIT " & PAGE_SIZE
Dim rs : Set rs = conn.Execute(sql)
Dim totalCount : totalCount = conn.Execute("SELECT COUNT(*) FROM inxpress_shipments_parsed s" & whereSQL)(0)
' Dropdowns
Dim rsC, rsI, rsCn
Set rsC = conn.Execute("SELECT DISTINCT Carrier FROM inxpress_shipments_parsed ORDER BY Carrier")
Set rsI = conn.Execute("SELECT DISTINCT InvoiceNumber FROM inxpress_shipments_parsed ORDER BY InvoiceNumber DESC")
Set rsCn = conn.Execute("SELECT DISTINCT DestCountry FROM inxpress_shipments_parsed ORDER BY DestCountry")
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>InXpress Shipments</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body{padding:20px;}
.table-sm th,.table-sm td{padding:0.4rem 0.5rem;}
.table thead th{position:sticky;top:0;background:#f8f9fa;}
tfoot td{font-weight:bold;background:#e9f7ef;}
td.num,th.num{text-align:right;}
tr.duplicate-row td{background-color:#fff6c9!important;}
.modal-lg{max-width:900px;}
th a.sort{text-decoration:none;color:inherit;}
th.active-sort{background:#e0f0ff!important;}
th.active-sort a.sort::after{content:" ▲";font-size:0.8em;}
th.active-sort.desc a.sort::after{content:" ▼";}
</style>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function autoSubmit(){document.getElementById('filterForm').submit();}
function showShipment(btn){
const d=btn.dataset;
document.getElementById('m_id').innerText=d.id;
document.getElementById('m_invoice').innerText=d.invoice;
document.getElementById('m_shipdet').innerText=d.shipdet;
document.getElementById('m_recv').innerText=d.recv;
document.getElementById('m_pieces').innerText=d.pieces;
document.getElementById('m_charges').innerText=d.charges;
document.getElementById('m_total').innerText=d.total;
new bootstrap.Modal(document.getElementById('shipmentModal')).show();
}
</script>
</head>
<body>
<div class="container-fluid">
<h3 class="mb-3">InXpress Shipments</h3>
<form id="filterForm" class="row row-cols-lg-auto g-2 align-items-center mb-3" method="get">
<div class="col"><input type="text" name="search" value="<%=SafeHTML(searchText)%>" class="form-control" placeholder="Search Reference or Receiver"></div>
<div class="col">
<select name="carrier" class="form-select" onchange="autoSubmit()">
<option value="">All Carriers</option>
<% Do Until rsC.EOF
Response.Write "<option value='" & SafeHTML(rsC("Carrier")) & "'"
If carrierSel=rsC("Carrier") Then Response.Write " selected"
Response.Write ">" & SafeHTML(rsC("Carrier")) & "</option>"
rsC.MoveNext : Loop : rsC.Close %>
</select>
</div>
<div class="col">
<select name="invoice" class="form-select" onchange="autoSubmit()">
<option value="">All Invoices</option>
<% Do Until rsI.EOF
Response.Write "<option value='" & SafeHTML(rsI("InvoiceNumber")) & "'"
If invoiceSel=rsI("InvoiceNumber") Then Response.Write " selected"
Response.Write ">" & SafeHTML(rsI("InvoiceNumber")) & "</option>"
rsI.MoveNext : Loop : rsI.Close %>
</select>
</div>
<div class="col">
<select name="country" class="form-select" onchange="autoSubmit()">
<option value="">All Countries</option>
<% Do Until rsCn.EOF
Response.Write "<option value='" & SafeHTML(rsCn("DestCountry")) & "'"
If countrySel=rsCn("DestCountry") Then Response.Write " selected"
Response.Write ">" & SafeHTML(rsCn("DestCountry")) & "</option>"
rsCn.MoveNext : Loop : rsCn.Close %>
</select>
</div>
<div class="col form-check">
<input class="form-check-input" type="checkbox" name="missingorders" value="1" id="missingorders" onchange="autoSubmit()" <%=IIf(missingOrders,"checked","")%>>
<label class="form-check-label" for="missingorders">Missing Orders</label>
</div>
<div class="col form-check">
<input class="form-check-input" type="checkbox" name="duplicates" value="1" id="duplicates" onchange="autoSubmit()" <%=IIf(duplicateRefs,"checked","")%>>
<label class="form-check-label" for="duplicates">Duplicate References</label>
</div>
<div class="col"><button class="btn btn-primary btn-sm">Filter</button> <a href="inxpress.asp" class="btn btn-secondary btn-sm">Reset</a></div>
</form>
<div class="table-responsive">
<table class="table table-sm table-striped table-bordered align-middle">
<thead><tr>
<%
Dim cols, headers, i, fieldName, headerName, sortClass
cols = Array("shipment_id","Carrier","ShipDate","Reference","ReceiverName","DestCountry","ServiceType","WeightKg","Length","BaseCharge","FuelSurcharge","OtherCharges","NetAmount","VATAmount","Total")
headers = Array("ID","Carrier","Ship Date","Reference","Receiver","Country","Service Type","Weight","Length","Base (£)","Fuel (£)","Other (£)","Net (£)","VAT (£)","Total (£)")
For i=0 To UBound(cols)
fieldName=cols(i):headerName=headers(i):sortClass=""
If sortField=fieldName Then sortClass="active-sort "&LCase(sortDir)
Dim numClass : numClass=""
If InStr(headerName,"£")>0 Or fieldName="WeightKg" Or fieldName="Length" Or fieldName="shipment_id" Then numClass="num"
If fieldName="Total" Then numClass=numClass&" total"
Dim linkDir, linkExtra
If sortDir = "ASC" Then linkDir = "DESC" Else linkDir = "ASC"
linkExtra = ""
If missingOrders Then linkExtra = linkExtra & "&missingorders=1"
If duplicateRefs Then linkExtra = linkExtra & "&duplicates=1"
Response.Write "<th class='" & sortClass & " " & numClass & "'>" & _
"<a class='sort' href='?sort=" & fieldName & _
"&dir=" & linkDir & _
"&search=" & Server.URLEncode(searchText) & _
"&carrier=" & Server.URLEncode(carrierSel) & _
"&invoice=" & Server.URLEncode(invoiceSel) & _
"&country=" & Server.URLEncode(countrySel) & _
linkExtra & "'>" & headerName & "</a></th>"
Next
%>
</tr></thead>
<tbody>
<%
Dim totalBase,totalFuel,totalOther,totalNet,totalVAT,totalGrand
totalBase=0:totalFuel=0:totalOther=0:totalNet=0:totalVAT=0:totalGrand=0
Do Until rs.EOF
Dim otherTotal : otherTotal=NumVal(rs("Handling"))+NumVal(rs("Insurance"))+NumVal(rs("AreaSurcharge"))+NumVal(rs("ProcessingFee"))+NumVal(rs("ResidentialFee"))+NumVal(rs("OtherCharges"))
totalBase=totalBase+NumVal(rs("BaseCharge"))
totalFuel=totalFuel+NumVal(rs("FuelSurcharge"))
totalOther=totalOther+otherTotal
totalNet=totalNet+NumVal(rs("NetAmount"))
totalVAT=totalVAT+NumVal(rs("VATAmount"))
totalGrand=totalGrand+NumVal(rs("Total"))
%>
<tr>
<td class="num">
<button type="button" class="btn btn-link p-0 m-0" onclick="showShipment(this)"
data-id="<%=rs("shipment_id")%>"
data-invoice="<%=SafeHTML(rs("Invoice_Number"))%>"
data-shipdet="<%=Replace(SafeHTML(rs("ShipmentDetails")),"""",""")%>"
data-recv="<%=Replace(SafeHTML(rs("ReceiverAddress")),"""",""")%>"
data-pieces="<%=Replace(SafeHTML(rs("PiecesWeightDimensionsZone")),"""",""")%>"
data-charges="<%=Replace(SafeHTML(rs("Charges")),"""",""")%>"
data-total="<%=FmtNum(rs("DetailTotal"))%>"><%=rs("shipment_id")%></button>
</td>
<td><%=SafeHTML(rs("Carrier"))%></td>
<td><%=Day(rs("ShipDate"))&"/"&Month(rs("ShipDate"))&"/"&Year(rs("ShipDate"))%></td>
<td><%=SafeHTML(rs("Reference"))%></td>
<td><%=SafeHTML(rs("ReceiverName"))%></td>
<td><%=CountryFlag(rs("DestCountry")) & SafeHTML(rs("DestCountry"))%></td>
<td><%=CleanServiceType(rs("ServiceType"))%></td>
<td class="num"><%=FmtNum(rs("WeightKg"))%></td>
<td class="num"><%=FmtNum(rs("Length"))%></td>
<td class="num"><%=FmtNum(rs("BaseCharge"))%></td>
<td class="num"><%=FmtNum(rs("FuelSurcharge"))%></td>
<td class="num"><%=FmtNum(otherTotal)%></td>
<td class="num"><%=FmtNum(rs("NetAmount"))%></td>
<td class="num"><%=FmtNum(rs("VATAmount"))%></td>
<td class="num total"><%=FmtNum(rs("Total"))%></td>
</tr>
<% rs.MoveNext : Loop %>
</tbody>
<tfoot><tr><td colspan="9" class="text-end">Totals:</td>
<td class="num"><%=FmtNum(totalBase)%></td><td class="num"><%=FmtNum(totalFuel)%></td>
<td class="num"><%=FmtNum(totalOther)%></td><td class="num"><%=FmtNum(totalNet)%></td>
<td class="num"><%=FmtNum(totalVAT)%></td><td class="num total"><%=FmtNum(totalGrand)%></td></tr></tfoot>
</table></div>
<p class="text-muted mt-2">Total records: <%=totalCount%></p>
</div>
<!-- Modal -->
<div class="modal fade" id="shipmentModal" tabindex="-1">
<div class="modal-dialog modal-lg"><div class="modal-content">
<div class="modal-header"><h5 class="modal-title">Shipment Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
<div class="modal-body">
<p><strong>ID:</strong> <span id="m_id"></span></p>
<p><strong>Invoice:</strong> <span id="m_invoice"></span></p>
<p><strong>Shipment Details:</strong><br><span id="m_shipdet"></span></p>
<p><strong>Receiver Address:</strong><br><span id="m_recv"></span></p>
<p><strong>Pieces / Weight / Dimensions / Zone:</strong><br><span id="m_pieces"></span></p>
<p><strong>Charges:</strong><br><span id="m_charges"></span></p>
<p><strong>Total:</strong> £<span id="m_total"></span></p>
</div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button></div>
</div></div>
</div>
</body>
</html>
<%
rs.Close:Set rs=Nothing
conn.Close:Set conn=Nothing
%>