Просмотр исходного кода

Fix mismatching counts in Account page for customer view

Alan Lam 7 лет назад
Родитель
Сommit
2f38a65072
4 измененных файлов с 123 добавлено и 75 удалено
  1. 4 1
      controllers/account.php
  2. 58 73
      controllers/reports.php
  3. 7 1
      engine/customer.php
  4. 54 0
      engine/test_result_set.php

+ 4 - 1
controllers/account.php

@@ -36,9 +36,12 @@ if($user->isCustomer()) {
 		$tests = Test::query([
 			"project_id" => $project_ids,
 			"status" => Test::STATUS_COMPLETED,
-			"dts_start" => (new Constraint("BETWEEN"))->setValues(...$year_cycle)
 		]);
 
+		$test_result_set = new TestResultSet($tests);
+
+		$tests = $test_result_set->filterByDateRange($start, $end);
+
 		$test_count = count($tests);
 
 		$pass_fail_tests = array_filter($tests, function(Test $test) {

+ 58 - 73
controllers/reports.php

@@ -111,66 +111,21 @@ if($submit) {
 		}
 
 		$tests = Test::query($test_nvp);
-		$test_count = count($tests);
 
-		// Get test values
-		$test_ids = array_column($tests, "id");
-		$test_values = TestValue::query([
-			"test_id" => $test_ids,
-		]);
-
-		$test_values_map = [];
-		foreach($test_values as $test_value) {
-			$test_id = $test_value->test_id;
-			$name = $test_value->name;
-
-			if(!isset($test_values_map[$test_id])) {
-				$test_values_map[$test_id] = new stdClass;
-			}
-
-			$test_values_map[$test_id]->{$name} = $test_value->value;
-		}
-
-		// Load users
-		User::load(array_column($tests, "tester_id"));
-
-		// Load locations
-		$locations = Location::load(array_column($tests, "location_id"));
-
-		// Get region and area manager IDs
-		$regions = array_filter(array_column($locations, "region"));
-		$area_managers = array_filter(array_column($locations, "area_manager"));
-
-		uksort($regions, "strnatcasecmp");
-		uksort($area_managers, "strnatcasecmp");
-
-		foreach($regions as $region) {
-			$region_data[$region] = [];
-		}
-
-		foreach($area_managers as $area_manager) {
-			$area_manager_data[$area_manager] = [];
-		}
-
-		$has_regions = !empty($region_data);
-		$has_area_managers = !empty($area_manager_data);
-
-		foreach($locations as $location) {
-			if($has_regions && !$location->region) $locations_missing_region[] = $location;
-			if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
-		}
+		$test_result_set = new TestResultSet($tests);
 
 		// Calculate date ranges
 		$min_year = date("Y");
 		$max_year = date("Y");
 
-		$min_unix = strtotime("$min_year-01-01 UTC");
-		$max_unix = strtotime("$max_year-12-31 23:59:59 UTC");
-
 		switch($date_range) {
 			case "ytd":
+			default:
 				// Nothing to do
 				$date_range_label = "YTD";
+
+				$min_dts = "$min_year-01-01";
+				$max_dts = "$max_year-12-31 23:59:59";
 				break;
 
 			case "unbound":
@@ -184,7 +139,12 @@ if($submit) {
 					if($year < $min_year) $min_year = $year;
 					if($year > $max_year) $max_year = $year;
 				}
+
 				$date_range_label = "$min_year - $max_year";
+
+				$min_dts = "$min_year-01-01";
+				$max_dts = "$max_year-12-31 23:59:59";
+
 				break;
 
 			case "custom":
@@ -198,44 +158,69 @@ if($submit) {
 				list($min_year, $min_month, $min_day) = explode("-", $date_start);
 				list($max_year, $max_month, $max_day) = explode("-", $date_end);
 
-				$min_unix = strtotime("{$date_start} 00:00:00 UTC");
-				$max_unix = strtotime("{$date_end} 23:59:59 UTC");
+				$min_dts = "{$date_start} 00:00:00";
+				$max_dts = "{$date_end} 23:59:59";
 
-				$date_range_label = date("d/m/Y", $min_unix) . " - " . date("d/m/Y", $max_unix);
+				$date_range_label = "{$min_day}/{$min_month}/{$min_year} - {$max_day}/{$max_month}/{$max_year}";
 
 				if(preg_match("/01-01$/", $date_start) && preg_match("/12-31$/", $date_end)) {
+					$date_range_label = $min_year == $max_year ? $min_year : "{$min_year} - {$max_year}";
+				}
 
-					if($min_year == $max_year) {
-						$date_range_label = $min_year;
-					}
-					else {
-						$date_range_label = "{$min_year} - {$max_year}";
-					}
+				break;
+		}
 
-				}
+		$tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
 
+		// Get test values
+		$test_ids = array_column($tests, "id");
+		$test_values = TestValue::query([
+			"test_id" => $test_ids,
+		]);
 
-				break;
+		$test_values_map = [];
+		foreach($test_values as $test_value) {
+			$test_id = $test_value->test_id;
+			$name = $test_value->name;
+
+			if(!isset($test_values_map[$test_id])) {
+				$test_values_map[$test_id] = new stdClass;
+			}
+
+			$test_values_map[$test_id]->{$name} = $test_value->value;
 		}
 
-		// Check if date range is same year
-		$same_year = ($min_year == $max_year);
+		// Load users
+		User::load(array_column($tests, "tester_id"));
+
+		// Load locations
+		$locations = Location::load(array_column($tests, "location_id"));
 
-		// Filter tests based on date range
-		$tests = array_filter($tests, function(Test $test) use ($min_unix, $max_unix) {
+		// Get region and area manager IDs
+		$regions = array_filter(array_column($locations, "region"));
+		$area_managers = array_filter(array_column($locations, "area_manager"));
 
-			$project = $test->getProject();
+		uksort($regions, "strnatcasecmp");
+		uksort($area_managers, "strnatcasecmp");
 
-			$dts = $project->dts_report ?: $test->dts_start;
+		foreach($regions as $region) {
+			$region_data[$region] = [];
+		}
 
-			$dt = new DateTime($dts . " UTC");
-			$unix = $dt->format("U");
+		foreach($area_managers as $area_manager) {
+			$area_manager_data[$area_manager] = [];
+		}
 
-			if($unix < $min_unix) return false;
-			if($unix > $max_unix) return false;
+		$has_regions = !empty($region_data);
+		$has_area_managers = !empty($area_manager_data);
 
-			return true;
-		});
+		foreach($locations as $location) {
+			if($has_regions && !$location->region) $locations_missing_region[] = $location;
+			if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
+		}
+
+		// Check if date range is same year
+		$same_year = ($min_year == $max_year);
 
 		// Empty data objects hook
 		require $template->getProcessingFilePath("reports_create_empty_objects.php");

+ 7 - 1
engine/customer.php

@@ -122,7 +122,13 @@ class Customer extends Model {
 			$max_dts = sprintf("%d-%02d-01", $current_year, $month);
 		}
 
-		return [$min_dts, $max_dts];
+		$min_time = strtotime($min_dts);
+		$max_time = strtotime($max_dts) - 1;
+
+		return [
+			date("Y-m-d", $min_time),
+			date("Y-m-d", $max_time),
+		];
 
 	}
 

+ 54 - 0
engine/test_result_set.php

@@ -0,0 +1,54 @@
+<?php
+
+class TestResultSet {
+
+	/** @var Test[] */
+	private $tests = [];
+
+	public function __construct(array $tests = []) {
+		$this->setTests($tests);
+	}
+
+	/**
+	 * @param Test[] $tests
+	 */
+	public function setTests(array $tests) {
+		$this->tests = $tests;
+	}
+
+	/**
+	 * @return Test[]
+	 */
+	public function getTests(): array {
+		return $this->tests;
+	}
+
+	/**
+	 * @param string $dts_start Start date/time in local time
+	 * @param string $dts_end End date/time in local time
+	 * @return Test[]
+	 */
+	public function filterByDateRange(string $dts_start, string $dts_end): array {
+		$min_unix = strtotime($dts_start);
+		$max_unix = strtotime($dts_end);
+
+		$tests = array_filter($this->tests, function(Test $test) use ($min_unix, $max_unix) {
+
+			$project = $test->getProject();
+
+			$dts = $project->dts_report ?: $test->dts_start;
+
+			$dt = new DateTime($dts . " UTC");
+			$unix = $dt->format("U");
+
+			if($unix < $min_unix) return false;
+			if($unix > $max_unix) return false;
+
+			return true;
+		});
+
+		$this->tests = array_values($tests);
+		return $this->tests;
+	}
+
+}