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 (8).asp
<%Option Explicit%>
<!--#include file="dbfunctions.asp"-->
<!--#include file="apputils.asp"-->
<!--#include file="customutils.asp"-->
<%DisableCache%>

<%
' (SS,v7,02/10/20) added Local and Non-Local to range
' (SS,v8,11/10/20) added past year feature to see stats for previous years started from 2014
' (SS,v9,08/11/20) addded Date Paid
' (SS,15/01/2021) Rolling Days combo, replaced 3, 7, 14, 21, 28, 30 with 1 to 30
%>

<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>

<%
CustomOrderingCheckDailyQuota(True)
%>

<form method="get" class="form-inline" >

<%
Dim LPastDays, LRange, LRollingDays
LPastDays = ParseInt(Trim(CleanSQLStr(Request("pastdays"))))
If LPastDays <= 0 Then ' default to 32
  LPastDays = 32
ElseIf LPastDays > 3650 Then ' max 10 years
  LPastDays = 3650
End If
LRange = CleanSQLStr(Request("range"))
If LRange = "" Then LRange = "All"

' (SS,3/6/20)
LRollingDays = ParseInt(Trim(CleanSQLStr(Request("rollingdays"))))
If LRollingDays <= 0 Then ' default to 14
  LRollingDays = 14
ElseIf LRollingDays > 30 Then ' max 30 days
  LRollingDays = 30
End If

' (SS,3/6/20)
Dim LWeekly
LWeekly = L_CheckedValue("weekly", False)

' (SS,8/11/20)
Dim LDatePaid
LDatePaid = L_CheckedValue("datepaid", False)

' (SS,11/10/20)
Dim LYear, i, LCurrentYear
LCurrentYear = Year(Now())
LYear = ParseInt(Trim(CleanSQLStr(Request("year"))))
If LYear <= 0 Then LYear = LCurrentYear ' default to current year

%>
  <div class="form-group">   
    <label for="year">Year</label>  
    <select class="form-control" name="year" id="year" onchange="this.form.submit()">
<%For i = LCurrentYear To 2014 Step -1%>
      <option value="<%=i%>"><%=i%></option>
<%Next%>      
    </select>    
  </div> 

  &nbsp; 

  <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>
      <option value="NonLocalMode">Non Local Mode</option>      
      <option value="LocalMode">Local Mode</option>
    </select>
  </div>

  &nbsp;
  
  <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>
  
  &nbsp;

    <div class="form-group">   
    <label for="rollingdays">Rolling Days</label>  
    <select class="form-control" name="rollingdays" id="rollingdays" onchange="this.form.submit()">
<% ' (SS,15/1/21) removed 3, 7, 14, 21, 28, 30, replaced with 1 to 30
For i = 1 To 30%>
      <option value="<%=i%>"><%=i%></option>
<%Next%>
    </select>    
  </div>

  &nbsp;&nbsp;  
  
  <label class="checkbox-inline">
    <input type="checkbox" class="form-check-input" id="weekly" name="weekly" value="yes" onchange="this.form.submit()">Weekly
  </label>
  
  <label class="checkbox-inline">
    <input type="checkbox" class="form-check-input" id="datepaid" name="datepaid" value="yes" onchange="this.form.submit()">Date Paid
  </label>    
  <!--&nbsp; <button type="submit" class="btn btn-default">Submit</button>  -->
  

</form>

<script>
$('#range option[value=<%=LRange%>]').attr('selected','selected');
$('#pastdays option[value=<%=LPastDays%>]').attr('selected','selected');
$('#rollingdays option[value=<%=LRollingDays%>]').attr('selected','selected');
$('#year option[value=<%=LYear%>]').attr('selected','selected');
$('#weekly').prop('checked', <%=L_BoolToJS(LWeekly)%>);
$('#datepaid').prop('checked', <%=L_BoolToJS(LDatePaid)%>);
</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", "RadiatorQuota3")
  ' (SS,11/10/20) replaced RadiatorQuota3 with RadiatorQuota4
  LSQL = GetSettingMemo("SQLStore", "RadiatorQuota4")
  
  ' (SS,8/11/20) if Date Paid ticked then use DateTimePaid instead of DateTimeOrdered
  If LDatePaid Then
    LSQL = ReplaceStr(LSQL, "DateTimeOrdered", "DateTimePaid")
  End If
  
  ' 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)))"
  
  ' (SS,11/10/20) years ago feature
  Dim LYearsAgo
  LYearsAgo = LCurrentYear - LYear
  LStartDate = "SUBDATE(DATE_SUB(CURDATE() - INTERVAL " & LYearsAgo & " YEAR, INTERVAL @days DAY), WEEKDAY(DATE_SUB(CURDATE() - INTERVAL " & LYearsAgo & " YEAR, INTERVAL @days DAY)))"
  
  LStartDate = ReplaceStr(LStartDate, "@days", CStr(LDays))
  
  ' (SS,11/10/20) added due to new years ago feature
  Dim LEndDate
  LEndDate = "CURDATE() - INTERVAL " & LYearsAgo & " YEAR" 
  
  LSQL = ReplaceStr(LSQL, "@startdate", LStartDate)  
  
  ' (SS,11/10/20)
  LSQL = ReplaceStr(LSQL, "@enddate", LEndDate)
  
  ' (SS,28/5/20)
  Dim LRangeCriteria
  If LRange = "Sold" Then
    LRangeCriteria = "AND NOT Exchange"
  ElseIf LRange = "Exchanges" Then
    LRangeCriteria = "AND Exchange"
  ElseIf LRange = "Exchanges" Then
    LRangeCriteria = "AND Exchange"
  ' (SS,2/10/20) added Local and Non-Local
  ElseIf LRange = "LocalMode" Then
    LRangeCriteria = "AND LocalMode"
  ElseIf LRange = "NonLocalMode" Then    
    LRangeCriteria = "AND NOT LocalMode"      
  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`" &_
  ' (SS,28/5/20) removed 3 day, added 14 and 30 day, also monthly cumulative and predictive
  ' (SS,3/6/20) added 6 and 5 day for 14 and 30 day rolling average
  LSQL = "SELECT t1.Day, t1.DateFormatted AS Date, t1.Orders, t1.Latest AS `Latest~Time`, t1.Rads, "
  ' (SS,3/6/20) made optional depending on checkbox ticked
  If LRollingDays > 0 Then
    LSQL = LSQL &_
    " ROUND(AVG(t2.Rads), 0) AS `" & LRollingDays & "-Day~Rolling~Avg.`," &_
    " ROUND(AVG(t2.Rads) * 7 / 6, 0) AS `" & LRollingDays & "-Day~Rolling~Avg.~6 day wk.`," &_
    " ROUND(AVG(t2.Rads) * 7 / 5, 0) AS `" & LRollingDays & "-Day~Rolling~Avg.~5 day wk.`,"
  End If
  If LWeekly Then
    LSQL = LSQL &_
    " t1.WeeklyCumulative AS `Weekly~Cumul.`," &_ 
    " t1.WeeklyPredictive AS `Weekly~Pred.`,"
  End If
  LSQL = LSQL &_
    " t1.MonthlyCumulative AS `Monthly~Cumul.`," &_
    " t1.MonthlyPredictive AS `Monthly~Pred.`" &_ 
    " FROM tmp_radiator_quota t1" &_
    " JOIN tmp_radiator_quota AS t2" &_
    "  ON t2.Date BETWEEN DATE_SUB(t1.Date, INTERVAL " & LRollingDays - 1 & " DAY) AND t1.Date" &_
    " GROUP BY 1, 2" &_
    " ORDER BY t1.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>

<%

' (SS,3/6/20) returns true or false for given checkbox value, non-blank means True, "" is False
Function L_CheckedValue(AName, ADefault)  
  Dim LValue, LResult
  LValue = Request(AName)
  If LValue = "" Then
    LResult = ADefault
  Else
    LResult = True ' LValue = "yes"
  End If
  L_CheckedValue = LResult
End Function

' (SS,3/6/20) returns True or False as a string "true" or "false" for javascript
Function L_BoolToJS(AValue)
  If AValue Then
    L_BoolToJS = "true"
  Else
    L_BoolToJS = "false"
  End If
End Function

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 class=""small"">" & 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
%>