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

Add support for filtering by various location entities in reports

Alan Lam 2 лет назад
Родитель
Сommit
8da035a363

+ 43 - 9
controllers/reports.php

@@ -19,7 +19,9 @@ $page_title = "$customer->name Reports";
 Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title);
 Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title);
 
 
 $date_range = http::value("date_range");
 $date_range = http::value("date_range");
-if(!$date_range) $date_range = "ytd";
+if(!$date_range) {
+	$date_range = "ytd";
+}
 
 
 $date_resolution = http::value("date_resolution");
 $date_resolution = http::value("date_resolution");
 
 
@@ -27,7 +29,9 @@ $date_start = http::value("date_start");
 $date_end = http::value("date_end");
 $date_end = http::value("date_end");
 
 
 $exclude_project_ids = http::value("exclude_project_ids", "int");
 $exclude_project_ids = http::value("exclude_project_ids", "int");
-if(!is_array($exclude_project_ids)) $exclude_project_ids = [];
+if(!is_array($exclude_project_ids)) {
+	$exclude_project_ids = [];
+}
 
 
 $location_ids = http::value("location_ids");
 $location_ids = http::value("location_ids");
 if(!empty($location_ids)) {
 if(!empty($location_ids)) {
@@ -38,6 +42,15 @@ if(!empty($location_ids)) {
 
 
 $retests = http::value("retests", "int");
 $retests = http::value("retests", "int");
 
 
+$location_filters = [
+	["field" => "region", "values" => http::value("regions", "trim")],
+	["field" => "area_manager", "values" => http::value("area_managers", "trim")],
+	["field" => "operations_manager_name", "values" => http::value("operations_managers", "trim")],
+	["field" => "gaming_manager_name", "values" => http::value("gaming_managers", "trim")],
+	["field" => "senior_gaming_manager_name", "values" => http::value("senior_gaming_managers", "trim")],
+	["field" => "brand", "values" => http::value("brands", "trim")],
+];
+
 if(!is_array($location_ids)) {
 if(!is_array($location_ids)) {
 	$location_ids = [];
 	$location_ids = [];
 }
 }
@@ -87,21 +100,27 @@ if($submit) {
 		// Get all projects
 		// Get all projects
 		$projects = Project::query([
 		$projects = Project::query([
 			"customer_id" => $customer->id,
 			"customer_id" => $customer->id,
-			"id" => (new Constraint("NOT_IN"))->setValues(...$exclude_project_ids)
+			"id" => Constraint::notIn($exclude_project_ids)
 		]);
 		]);
 
 
 		$project_ids = array_intersect($all_project_ids, array_column($projects, "id"));
 		$project_ids = array_intersect($all_project_ids, array_column($projects, "id"));
 
 
 		// Enforce unique template ID in selection
 		// Enforce unique template ID in selection
 		$template_ids = array_unique(array_column($projects, "template_id"));
 		$template_ids = array_unique(array_column($projects, "template_id"));
-		if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
+		if(count($template_ids) > 1) {
+			$form_exception->addError("Projects with different templates have been selected.");
+		}
 
 
 		// Check template exists
 		// Check template exists
 		$template_id = array_shift($template_ids);
 		$template_id = array_shift($template_ids);
 		$template = Template::get($template_id);
 		$template = Template::get($template_id);
-		if(!$template) $form_exception->addError("Template cannot be found.");
+		if(!$template) {
+			$form_exception->addError("Template cannot be found.");
+		}
 
 
-		if($form_exception->hasErrors()) throw $form_exception;
+		if($form_exception->hasErrors()) {
+			throw $form_exception;
+		}
 
 
 		// Get all completed tests
 		// Get all completed tests
 		$test_nvp = [
 		$test_nvp = [
@@ -182,6 +201,24 @@ if($submit) {
 
 
 		$tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
 		$tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
 
 
+		// Load locations
+		$locations = Location::load(array_column($tests, "location_id"));
+
+		// Filter by location fields
+		$tests = array_filter($tests, static function(Test $test) use ($location_filters) {
+			$location = $test->getLocation();
+			foreach($location_filters as $filter) {
+				$field = $filter["field"];
+				$values = $filter["values"];
+
+				if($values && !in_array($location->$field, $values)) {
+					return false;
+				}
+			}
+
+			return true;
+		});
+
 		// Get test values
 		// Get test values
 		$test_ids = array_column($tests, "id");
 		$test_ids = array_column($tests, "id");
 		$test_values = TestValue::query([
 		$test_values = TestValue::query([
@@ -203,9 +240,6 @@ if($submit) {
 		// Load users
 		// Load users
 		User::load(array_column($tests, "tester_id"));
 		User::load(array_column($tests, "tester_id"));
 
 
-		// Load locations
-		$locations = Location::load(array_column($tests, "location_id"));
-
 		// Get region and area manager IDs
 		// Get region and area manager IDs
 		$regions = array_filter(array_column($locations, "region"));
 		$regions = array_filter(array_column($locations, "region"));
 		$area_managers = array_filter(array_column($locations, "area_manager"));
 		$area_managers = array_filter(array_column($locations, "area_manager"));

+ 2 - 0
engine/core.php

@@ -31,6 +31,8 @@ class Core {
 	public static $stylesheets = [
 	public static $stylesheets = [
 		"public/css/bootstrap.min.css",
 		"public/css/bootstrap.min.css",
 		"public/css/daterangepicker.css",
 		"public/css/daterangepicker.css",
+		"public/css/select2.min.css",
+		"public/css/select2-bootstrap4.min.css",
 	];
 	];
 
 
 	/**
 	/**

+ 63 - 0
engine/customer.php

@@ -193,4 +193,67 @@ class Customer extends Model {
 		return $output;
 		return $output;
 	}
 	}
 
 
+	/**
+	 * @param string $field
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getLocationFieldValues(string $field): array {
+		$names = [];
+		foreach($this->getLocations() as $location) {
+			$names[] = $location->$field;
+		}
+		$names = array_values(array_unique(array_filter($names)));
+		usort($names, Util::getStringSorter());
+		return $names;
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getBrands(): array {
+		return $this->getLocationFieldValues("brand");
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getRegions(): array {
+		return $this->getLocationFieldValues("region");
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getAreaManagerNames(): array {
+		return $this->getLocationFieldValues("area_manager");
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getGamingManagerNames(): array {
+		return $this->getLocationFieldValues("gaming_manager_name");
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getSeniorGamingManagerNames(): array {
+		return $this->getLocationFieldValues("senior_gaming_manager_name");
+	}
+
+	/**
+	 * @return string[]
+	 * @throws Exception
+	 */
+	public function getOperationsManagerNames(): array {
+		return $this->getLocationFieldValues("operations_manager_name");
+	}
+
 }
 }

+ 3 - 1
engine/location.php

@@ -50,7 +50,9 @@ class Location extends Model {
 		"address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone",
 		"address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone",
 		"opening_hours", "notes",
 		"opening_hours", "notes",
 		"shop_left", "shop_right",
 		"shop_left", "shop_right",
-		"brand", "region", "region_name", "internal_id",
+		"brand",
+		"region", "region_name",
+		"internal_id",
 		"area_manager", "area_manager_name", "area_manager_email",
 		"area_manager", "area_manager_name", "area_manager_email",
 		"operations_manager_name", "operations_manager_email",
 		"operations_manager_name", "operations_manager_email",
 		"gaming_manager_name", "gaming_manager_email",
 		"gaming_manager_name", "gaming_manager_email",

+ 1 - 1
engine/test.php

@@ -497,7 +497,7 @@ class Test extends Model {
 	 * @return Location
 	 * @return Location
 	 * @throws Exception
 	 * @throws Exception
 	 */
 	 */
-	public function getLocation(): ?Location {
+	public function getLocation(): Location {
 		return Location::get($this->location_id);
 		return Location::get($this->location_id);
 	}
 	}
 
 

+ 1 - 1
engine/test_result_set.php

@@ -12,7 +12,7 @@ class TestResultSet {
 	/**
 	/**
 	 * @param Test[] $tests
 	 * @param Test[] $tests
 	 */
 	 */
-	public function setTests(array $tests) {
+	public function setTests(array $tests): void {
 		$this->tests = $tests;
 		$this->tests = $tests;
 	}
 	}
 
 

+ 6 - 0
engine/util.php

@@ -400,4 +400,10 @@ class Util {
 		return $value;
 		return $value;
 	}
 	}
 
 
+	public static function getStringSorter(): Closure {
+		return static function($a, $b) {
+			return strnatcasecmp($a, $b);
+		};
+	}
+
 }
 }

+ 1 - 0
initialise.php

@@ -99,4 +99,5 @@ Core::addScript("public/js/bootstrap.min.js");
 Core::addScript("public/js/clipboard.min.js");
 Core::addScript("public/js/clipboard.min.js");
 Core::addScript("public/js/moment.min.js");
 Core::addScript("public/js/moment.min.js");
 Core::addScript("public/js/daterangepicker.js");
 Core::addScript("public/js/daterangepicker.js");
+Core::addScript("public/js/select2.min.js");
 Core::addScript("public/js/app.js");
 Core::addScript("public/js/app.js");

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
public/css/select2-bootstrap4.min.css


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
public/css/select2.min.css


+ 8 - 0
public/js/app.js

@@ -83,6 +83,14 @@ $(function() {
 		new Chart(this, object);
 		new Chart(this, object);
 	});
 	});
 
 
+	$.fn.select2.defaults.set("theme", "bootstrap4");
+
+	$("select[data-toggle='select2']").each((index, element) => {
+		console.log(element);
+		const $el = $(element);
+		$el.select2();
+	});
+
 });
 });
 
 
 var maps = [];
 var maps = [];

Разница между файлами не показана из-за своего большого размера
+ 1 - 0
public/js/select2.min.js


+ 58 - 4
views/reports.php

@@ -56,6 +56,13 @@ if($date_start && $date_end) {
 	$date_range_value = "$min_day/$min_month/$min_year - $max_day/$max_month/$max_year";
 	$date_range_value = "$min_day/$min_month/$min_year - $max_day/$max_month/$max_year";
 }
 }
 
 
+$brands = $customer->getBrands();
+$regions = $customer->getRegions();
+$area_managers = $customer->getAreaManagerNames();
+$operations_managers = $customer->getOperationsManagerNames();
+$gaming_managers = $customer->getGamingManagerNames();
+$senior_gaming_managers = $customer->getSeniorGamingManagerNames();
+
 ?>
 ?>
 <script>
 <script>
 let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
 let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
@@ -94,7 +101,7 @@ let projectData = <?=json_encode($project_table_data)?>;
 				<div class="form-group">
 				<div class="form-group">
 					<label class="form-label" for="date-range">
 					<label class="form-label" for="date-range">
 						Date Range
 						Date Range
-					</label><br / >
+					</label><br />
 					<?php
 					<?php
 					foreach($date_ranges as $value => $display) {
 					foreach($date_ranges as $value => $display) {
 						$checked = ($value === $date_range) ? "checked" : "";
 						$checked = ($value === $date_range) ? "checked" : "";
@@ -150,13 +157,56 @@ let projectData = <?=json_encode($project_table_data)?>;
 						?>
 						?>
 					</select>
 					</select>
 				</div>
 				</div>
-				<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
-					Submit
-				</button>
+
+				<?php
+				/**
+				 * @param string $name
+				 * @param string $id
+				 * @param string $label
+				 * @param array $values
+				 * @return void
+				 * @throws Exception
+				 */
+				$render_location_entity_filter = static function(string $name, string $id, string $label, array $values): void {
+					if(!$values) {
+						return;
+					}
+
+					$request_name = preg_replace("/\[]/", "", $name);
+					$selected_values = http::value($request_name, "trim") ?? [];
+
+					?>
+					<div class="form-group">
+						<label for="<?=$id?>"><?=htmlspecialchars($label)?></label>
+						<select class="form-control" name="<?=$name?>" id="<?=$id?>" data-toggle="select2" data-placeholder="<?=htmlspecialchars(__("Select"))?>" multiple>
+							<?php
+							foreach($values as $value) {
+								$selected = in_array($value, $selected_values) ? "selected" : ""
+								?>
+								<option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($value)?></option>
+								<?php
+							}
+							?>
+						</select>
+					</div>
+					<?php
+				};
+				?>
 			</div>
 			</div>
 
 
 			<div class="col-md">
 			<div class="col-md">
+
+				<?php
+				$render_location_entity_filter("regions[]", "regions", __("Regions"), $regions);
+				$render_location_entity_filter("area_managers[]", "area_managers", __("Area Managers"), $area_managers);
+				$render_location_entity_filter("operations_managers[]", "operation_managers", __("Operations Managers"), $operations_managers);
+				$render_location_entity_filter("gaming_managers[]", "gaming_managers", __("Gaming Managers"), $gaming_managers);
+				$render_location_entity_filter("senior_gaming_managers[]", "senior_gaming_managers", __("Senior Gaming Managers"), $senior_gaming_managers);
+				$render_location_entity_filter("brands[]", "brands", __("Brands"), $brands);
+				?>
+
 				<div class="form-group">
 				<div class="form-group">
+
 					<label class="d-block">
 					<label class="d-block">
 						Exclude Projects
 						Exclude Projects
 					</label>
 					</label>
@@ -187,6 +237,10 @@ let projectData = <?=json_encode($project_table_data)?>;
 				</div>
 				</div>
 			</div>
 			</div>
 		</div>
 		</div>
+
+		<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
+			Submit
+		</button>
 	</form>
 	</form>
 
 
 	<?php
 	<?php

Некоторые файлы не были показаны из-за большого количества измененных файлов