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/castironradiatorcentre/changes prev/2024-02-29 (copies)/radiator-quota - Copy (4).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" />
<style>
.table td, .table th {
   text-align: center;   
}
.table2 th {
   text-align: right;   
}
</style>
</head>
<body>

<div class="container">

<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
LPastDays = ParseInt(Trim(CleanSQLStr(Request("pastdays"))))
If LPastDays = 0 Then LPastDays = 65
%>

<div class="form-group">
  <label for="pastdays">Past Days</label>
  <input type="text" class="form-control" id="pastdays" name="pastdays" style="width: 70px;" value="<%=LPastDays%>">
  <button type="submit" class="btn btn-default">Submit</button>
</div>

</form>

<%
  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
  LSQL = GetSettingMemo("SQLStore", "RadiatorQuota")
  
  ' 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
  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,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(t3.Radiators), 0) AS `3-Day~Rolling Average`, ROUND(AVG(t2.Radiators), 0) AS `7-Day~Rolling Average`, t1.WeeklyCumulative AS `Weekly~Cumulative`" &_  
    " 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 2 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
%>