diff --git a/public/refile/refile_month.php b/public/refile/refile_month.php
index b64050f..8f784fc 100755
--- a/public/refile/refile_month.php
+++ b/public/refile/refile_month.php
@@ -24,56 +24,113 @@
Refile Stats - Monthly
';
-// Step 1: Group data by $col4 and sum $col6
+// Helper: robust-ish month/year parsing to a sortable timestamp
+function monthYearToTimestamp(string $label): int {
+ $label = trim($label);
+
+ // Common trick: try parsing with a day in front (helps "January 2026", "Jan 2026", etc.)
+ $ts = strtotime("1 " . $label);
+ if ($ts !== false) return $ts;
+
+ // Try raw label (might already include a day)
+ $ts = strtotime($label);
+ if ($ts !== false) return $ts;
+
+ // Fallback: try MM/YYYY or MM-YYYY
+ if (preg_match('/^\s*(\d{1,2})[\/\-](\d{4})\s*$/', $label, $m)) {
+ $month = (int)$m[1];
+ $year = (int)$m[2];
+ if ($month >= 1 && $month <= 12) {
+ return mktime(0, 0, 0, $month, 1, $year);
+ }
+ }
+
+ // Last resort: shove unknown formats to the bottom
+ return 0;
+}
+
+// Step 1: Group data by month-year (Column4) and sum Column6
$dataByMonth = [];
-$grandTotal = 0; // overall total tracker
+$grandTotal = 0;
foreach ($rows as $row) {
- $col4 = isset($row->Column4) ? htmlspecialchars($row->Column4) : 'N/A';
- $col1 = isset($row->Column1) ? htmlspecialchars($row->Column1) : 'N/A';
+ $col4 = isset($row->Column4) ? htmlspecialchars((string)$row->Column4) : 'N/A';
+ $col1 = isset($row->Column1) ? htmlspecialchars((string)$row->Column1) : 'N/A';
$col6 = isset($row->Column6) ? floatval($row->Column6) : 0;
- // Store data under month-year
+ if (!isset($dataByMonth[$col4])) {
+ $dataByMonth[$col4] = [
+ 'ts' => monthYearToTimestamp($col4),
+ 'rows' => [],
+ 'subtotal' => 0
+ ];
+ }
+
$dataByMonth[$col4]['rows'][] = [
'month' => $col4,
'university' => $col1,
'total' => $col6
];
- // Add to monthly subtotal
- if (!isset($dataByMonth[$col4]['subtotal'])) {
- $dataByMonth[$col4]['subtotal'] = 0;
- }
$dataByMonth[$col4]['subtotal'] += $col6;
-
- // Add to grand total
$grandTotal += $col6;
}
+// Sort months descending by timestamp (most recent first)
+uasort($dataByMonth, function($a, $b) {
+ return ($b['ts'] <=> $a['ts']);
+});
+
// Display grand total above the table
echo "
Grand Total: " . number_format($grandTotal, 0) . "
";
// Step 2: Build the table
-echo '
+echo '
-| Month - Year |
+Month - Year |
Owning University |
-Totals |
+Totals |
-
-';
+';
+
+// Step 3: Collapsible month groups (Bootstrap collapse)
+// Most recent month open by default
+$monthIndex = 0;
-// Step 3: Display grouped data with subtotal rows
foreach ($dataByMonth as $month => $info) {
+ $collapseId = "monthCollapse_" . $monthIndex;
+ $isFirst = ($monthIndex === 0);
+ $showClass = $isFirst ? "show" : "";
+
+ // Month header row (toggle)
+ echo '
+
+
+ |
+
+ ' . htmlspecialchars($month) . '
+ Click to expand/collapse
+
+ |
+
+ ';
+
+ // Collapsible section for that month’s detail rows + subtotal
+ echo '';
+
foreach ($info['rows'] as $r) {
echo "
- | {$r['month']} |
+ |
{$r['university']} |
" . number_format($r['total'], 0) . " |
";
@@ -81,15 +138,20 @@
// Monthly subtotal row
echo "
- | Total for {$month} |
+ Total for " . htmlspecialchars($month) . " |
" . number_format($info['subtotal'], 0) . " |
";
+
+ echo '';
+
+ $monthIndex++;
}
-echo '
-
+echo '
View by Date Range
+
+