File: D:/web/castironradiatorcentre/changes prev/2024-02-29 (copies)/radiator-quota - Copy (6).asp
<%Option Explicit%>
<!--#include file="dbfunctions.asp"-->
<!--#include file="apputils.asp"-->
<!--#include file="customutils.asp"-->
<%DisableCache%>
<html>
<head>
<title>Radiator Quota</title>
<link href="css/all.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery.min.js"></script>
<style>
.table td, .table th {
text-align: center;
}
.table2 th {
text-align: right;
}
</style>
</head>
<body>
<div class="container-fluid">
<h1>Daily Radiator Quota</h1>
<table class="table table2 table-responsive table-sm table-bordered table-striped" style="width: auto;">
<%
CustomOrderingCheckDailyQuota(True)
%>
</table>
<form action="radiator-quota.asp" method="get" class="form-inline" >
<%
Dim LPastDays, LRange
LPastDays = ParseInt(Trim(CleanSQLStr(Request("pastdays"))))
If LPastDays <= 0 Then ' default to 64
LPastDays = 64
ElseIf LPastDays > 3650 Then ' max 10 years
LPastDays = 3650
End If
LRange = CleanSQLStr(Request("range"))
%>
<div class="form-group">
<label for="pastdays">Past Days</label>
<select class="form-control" name="pastdays" id="pastdays" onchange="this.form.submit()">
<option value="32">32</option>
<option value="64">64</option>
<option value="96">96</option>
<option value="128">128</option>
<option value="183">183</option>
<option value="366">366</option>
<option value="732">732</option>
</select>
</div>
<div class="form-group">
<label for="range"> Range</label>
<select class="form-control" name="range" id="range" onchange="this.form.submit()">
<option value="All">All</option>
<option value="Sold">Sold</option>
<option value="Exchanges">Exchanges</option>
</select>
</div>
<!-- <button type="submit" class="btn btn-default">Submit</button> -->
</form>
<script>
$('#pastdays option[value=<%=LPastDays%>]').attr('selected','selected');
$('#range option[value=<%=LRange%>]').attr('selected','selected');
</script>
<%
Dim LSQL
' (SS,24/4/20) changed from past 31 to 60 days
'LSQL = _
' "SELECT DAYNAME(o.DateTimeOrdered) AS Day, DATE(o.DateTimeOrdered) AS Date, SUM(od.Qty) AS Radiators, COUNT(DISTINCT o.OrderNo) AS Orders " &_
' "FROM orderdetails od " &_
' "INNER JOIN orders o ON o.OrderNo = od.OrderNo " &_
' "INNER JOIN products p ON p.ProductID = od.ProductID " &_
' "LEFT JOIN product_attributes pa ON pa.ProductID = od.ProductID AND pa.AttributeID = 1 " &_
' "WHERE " &_
' "DATE(o.DateTimeOrdered) BETWEEN DATE_SUB(CURDATE(), INTERVAL 31 DAY) AND CURDATE() AND " &_
' "(o.Status = 'COMPLETED' OR o.Status = 'PAYMENT RECEIVED' OR o.Status = 'AWAITING PAYMENT') AND " &_
' "AttributeValue = 'Radiator' AND SubproductOrderDetailID IS NULL " &_
' "GROUP BY Date " &_
' "ORDER BY Date DESC"
' above is older version which doesn't show empty days, following links to common calendar table to show all days
LSQL =_
"SELECT DAYNAME(c.CalendarDate) AS Day, c.CalendarDate AS Date, COALESCE(a.Radiators, 0) AS Radiators, COALESCE(a.Orders, 0) AS Orders, DATE_FORMAT(a.Latest, '%H:%i') AS Latest " &_
"FROM common.Calendar c " &_
"LEFT JOIN " &_
"( " &_
"SELECT DATE(o.DateTimeOrdered) AS Date, SUM(od.Qty) AS Radiators, COUNT(DISTINCT o.OrderNo) AS Orders, MAX(o.DateTimeOrdered) AS Latest " &_
"FROM orderdetails od " &_
"INNER JOIN orders o ON o.OrderNo = od.OrderNo " &_
"INNER JOIN products p ON p.ProductID = od.ProductID " &_
"LEFT JOIN product_attributes pa ON pa.ProductID = od.ProductID AND pa.AttributeID = 1 " &_
"WHERE " &_
"DATE(o.DateTimeOrdered) BETWEEN DATE_SUB(CURDATE(), INTERVAL 60 DAY) AND CURDATE() AND " &_
"(o.Status = 'COMPLETED' OR o.Status = 'PAYMENT RECEIVED' OR o.Status = 'AWAITING PAYMENT') AND " &_
"AttributeValue = 'Radiator' AND SubproductOrderDetailID IS NULL " &_
"GROUP BY Date " &_
") a ON a.Date = c.CalendarDate " &_
"WHERE c.CalendarDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 60 DAY) AND CURDATE() " &_
"ORDER BY Date DESC"
' (SS,25/4/20) replaced above with new SQL held in settings (easy to store by copying and pasting from EMS MySQL Manager)
' this new version adds a WeeklyCumulative field
' (SS,29/4/20) COALESCE used around Radiators and Orders (i.e. null to 0) to ensure accurate 7 day moving average calculation
' (SS,28/5/20) new version with monthly cumulative and range selector
LSQL = GetSettingMemo("SQLStore", "RadiatorQuota2")
' SET &days = 373;
' SET @startdate = SUBDATE(DATE_SUB(CURDATE(), INTERVAL @days DAY), WEEKDAY(DATE_SUB(CURDATE(), INTERVAL @days DAY)));
' ODBC only allows one SQL statement, so doing a replace here, (could get around this some other way, even add a stored function or view) (can't use views, they don't allow more than one statement)
' ODBC might have an option to get around this
Dim LDays, LStartDate
'LDays = 500
'LDays = LPastDays + 6 ' (SS,1/5/20) added 6 to allow for the 7 day rolling average calculation
LDays = LPastDays + 31 ' (SS,29/5/20) added 31 to allow for the monthly calculation
LStartDate = "SUBDATE(DATE_SUB(CURDATE(), INTERVAL @days DAY), WEEKDAY(DATE_SUB(CURDATE(), INTERVAL @days DAY)))"
LStartDate = ReplaceStr(LStartDate, "@days", CStr(LDays))
LSQL = ReplaceStr(LSQL, "@startdate", LStartDate)
' (SS,28/5/20)
Dim LRangeCriteria
If LRange = "Sold" Then
LRangeCriteria = "AND NOT Exchange"
ElseIf LRange = "Exchanges" Then
LRangeCriteria = "AND Exchange"
Else
LRangeCriteria = ""
End If
LSQL = ReplaceStr(LSQL, "AND Exchange = Exchange", LRangeCriteria)
' (SS,29/4/20) added 7 Day Moving Average, had to create temp table (later permanent table due to bug in MySQL)
' couldn't use temporary table due to a bug in MySQL which results in "Can't reopen table: 't1'"
' see https://bugs.mysql.com/bug.php?id=10327
ExecuteQuery("DROP TABLE IF EXISTS tmp_radiator_quota")
ExecuteQuery("CREATE TABLE tmp_radiator_quota " & LSQL)
' help from https://www.sisense.com/en-gb/blog/rolling-average/
' (SS,1/5/20) replaced t1.Date - 6 with DATE_SUB(t1.Date, INTERVAL 6 DAY) (it was showing incorrect value for 7DayRollingAverage)
' (SS,1/5/20) added 3 day rolling average
'LSQL = "SELECT t1.Day, t1.Date, t1.Radiators, t1.Orders, t1.Latest AS `Latest~Time`, t1.WeeklyCumulative AS `Weekly~Cumulative`, ROUND(AVG(t3.Radiators), 0) AS `3-Day~Rolling Average`, ROUND(AVG(t2.Radiators), 0) AS `7-Day~Rolling Average`" &_
LSQL = "SELECT t1.Day, t1.Date, t1.Orders, t1.Latest AS `Latest~Time`, t1.Radiators, ROUND(AVG(t2.Radiators), 0) AS `7-Day~Rolling~Average`, ROUND(AVG(t3.Radiators), 0) AS `14-Day~Rolling~Average`, t1.WeeklyCumulative AS `Weekly~Cumulative`, t1.MonthlyCumulative AS `Monthly~Cumulative`, t1.MonthlyPredictive AS `Monthly~Predictive`" &_
" FROM tmp_radiator_quota t1" &_
" JOIN tmp_radiator_quota AS t2" &_
" ON t2.Date BETWEEN DATE_SUB(t1.Date, INTERVAL 6 DAY) AND t1.Date" &_
" JOIN tmp_radiator_quota as t3" &_
" ON t3.Date BETWEEN DATE_SUB(t1.Date, INTERVAL 13 DAY) AND t1.Date" &_
" GROUP BY 1, 2" &_
" ORDER BY Date DESC" &_
" LIMIT " & LPastDays ' (SS,1/5/20) show the actual number of days instead of the extra 6 days which will have the wrong calc
L_ShowTable LSQL
%>
<%Finalise%>
</div>
</body>
</html>
<%
Sub L_ShowTable(ASQL)
OpenQuery(ASQL)
L_ShowTableHeader
Do While Not EndOfQuery
L_ShowTableRecord
NextQueryRecord
Loop
L_ShowTableFooter
End Sub
Sub L_ShowTableHeader
' for each field
Dim LoRS
Set LoRS = oRS ' *** oRS is a global defined in dbfunctions.asp perhaps create a function to return this, or a create a different solutions
%>
<table class="table table-responsive table-sm table-bordered table-striped" style="width: auto;">
<thead>
<tr>
<%
Dim i, LFieldName, LFieldCount
LFieldCount = oRS.Fields.Count
For i = 0 To LFieldCount - 1
LFieldName = LoRS.Fields.Item(i).Name
' (SS,1/5/20) added ~ for wordwrap onto multiple lines
Dim n, aSplit, LOutput
LOutput = ""
aSplit = Split(LFieldName, "~")
If IsArray(aSplit) Then
For n = 0 to UBound(aSplit)
If LOutput <> "" Then LOutput = LOutput & "<br>"
LOutput = LOutput & aSplit(n)
Next
End If
Response.Write "<th>" & LOutput & "</th>"
Next
%>
</tr>
</thead>
<tbody>
<%
End Sub
Sub L_ShowTableRecord
' for each field
Dim LoRS
Set LoRS = oRS ' *** oRS is a global defined in dbfunctions.asp perhaps create a function to return this, or a create a different solutions
%>
<tr>
<%
Dim i, LFieldName, LFieldValue, LFieldCount
LFieldCount = oRS.Fields.Count
For i = 0 To LFieldCount - 1
LFieldName = LoRS.Fields.Item(i).Name
LFieldValue = LoRS.Fields.Item(i)
Response.Write "<td>" & LFieldValue & "</td>"
Next
%>
</tr>
<%
End Sub
Sub L_ShowTableFooter
%>
</tbody>
</table>
<%
End Sub
%>