Bläddra i källkod

Add "Project" filter to reports

Alan Lam 2 år sedan
förälder
incheckning
a739e620c6

+ 2 - 1
composer.json

@@ -14,7 +14,8 @@
 	"ext-readline": "*",
 	"ext-simplexml": "*",
     "nesbot/carbon": "^2.50",
-	"ext-bcmath": "*"
+	"ext-bcmath": "*",
+	"ext-intl": "*"
   },
   "config": {
     "platform": {

+ 36 - 12
controllers/reports.php

@@ -1,5 +1,4 @@
-<?php /** @noinspection PhpIncludeInspection */
-/** @noinspection PhpUnhandledExceptionInspection */
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 Core::addScript("public/js/app-reports.js");
 
@@ -7,7 +6,9 @@ Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/reports", "Reports");
 
 $customer = Customer::getForAdmin();
-if(!$customer) throw new Exception("You do not have permission to access this page.");
+if(!$customer) {
+	throw new RuntimeException("You do not have permission to access this page.");
+}
 
 $customer_slug = Util::slug($customer->name);
 $customer_slug = str_replace("-", "_", $customer_slug);
@@ -42,6 +43,8 @@ if(!empty($location_ids)) {
 
 $exclude_categories = http::value("exclude_categories");
 
+$project_id = http::value("project_id", "int");
+
 $retests = http::value("retests", "int");
 
 $location_filters = [
@@ -101,13 +104,24 @@ if($submit) {
 	try {
 		Core::validateCSRFToken($submit);
 
-		$form_exception = new FormException;
+		$form_exception = new FormException();
 
-		// Get all projects
-		$projects = Project::query([
-			"customer_id" => $customer->id,
-			"id" => Constraint::notIn($exclude_project_ids)
-		]);
+		// Specific project
+		if($project_id) {
+			$projects = Project::query([
+				"customer_id" => $customer->id,
+				"id" => $project_id
+			]);
+			if(!$projects) {
+				$form_exception->addError("Project not found.");
+			}
+		}
+		else {
+			$projects = Project::query([
+				"customer_id" => $customer->id,
+				"id" => Constraint::notIn($exclude_project_ids)
+			]);
+		}
 
 		$project_ids = array_intersect($all_project_ids, array_column($projects, "id"));
 
@@ -212,7 +226,14 @@ if($submit) {
 				break;
 		}
 
-		$tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
+		if($project_id) {
+			$tests = $test_result_set->getTests();
+			$project = $projects[0];
+			$date_range_label = $project->getDisplayReportMonth();
+		}
+		else {
+			$tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
+		}
 
 		// Load locations
 		$locations = Location::load(array_column($tests, "location_id"));
@@ -342,10 +363,13 @@ if($submit) {
 
 		}
 
+		/**
+		 * @var object $empty_data
+		 */
+
 		foreach($keys as $index => $key) {
 			$label = $key_labels[$index];
 
-			/** @var object $empty_data */
 			$data[$key] = clone $empty_data;
 			$data[$key]->label = $label;
 
@@ -518,7 +542,7 @@ if($submit) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("reports", compact("customer", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "exclude_project_ids", "location_ids", "retests", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "locations_missing_operations_manager", "locations_missing_brand", "region_data", "area_manager_data", "brand_data", "operations_manager_data", "key_labels", "has_pass_fail", "template_config", "exclude_categories"));
+View::include("reports", compact("customer", "project_id", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "exclude_project_ids", "location_ids", "retests", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "locations_missing_operations_manager", "locations_missing_brand", "region_data", "area_manager_data", "brand_data", "operations_manager_data", "key_labels", "has_pass_fail", "template_config", "exclude_categories"));
 View::includeOnce("footer");
 
 file_put_contents("periodic_data.bin", serialize($data));

+ 22 - 4
controllers/reports_presentation.php

@@ -16,8 +16,10 @@
  * @var string $action
  */
 
+use Carbon\CarbonImmutable;
 use PhpOffice\PhpPresentation\PhpPresentation;
 use PhpOffice\PhpPresentation\IOFactory;
+use PhpOffice\PhpPresentation\Shape\RichText\Run;
 use PhpOffice\PhpPresentation\Style\Alignment;
 
 $presentation = new PhpPresentation(); // 960x720
@@ -42,7 +44,7 @@ if($retests !== null) {
 	}
 }
 
-$title = sprintf("%s %s %s Tests (%s)", $customer->name, $template->name, date("F Y"), $retests_title);
+$dt_now = CarbonImmutable::now(TIMEZONE);
 
 if($has_logo) {
 	$shape = $slide->createDrawingShape();
@@ -66,11 +68,27 @@ else {
 		  ->setWidth(940)
 		  ->setOffsetX(10)
 		  ->setOffsetY(315);
-	$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
 }
 
-$run = $shape->createTextRun($title);
-$run->getFont()->setBold(true)->setSize(16);
+$title_lines = [
+	$customer->name,
+	$template->name,
+	$date_range_label,
+	$retests_title,
+	"",
+	__("Report Generated at %s", $dt_now->format("d/m/Y H:i")),
+];
+
+foreach($title_lines as $title_line) {
+	$paragraph = $shape->getActiveParagraph();
+	$paragraph->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
+
+	$run = new Run($title_line);
+	$run->getFont()->setBold(true)->setSize(16);
+	$paragraph->addText($run);
+
+	$paragraph = $shape->createParagraph();
+}
 
 $create_summary_slide = static function(array $options) use ($presentation, $charts, $date_range_label, $total_data) {
 

+ 8 - 0
engine/chart.php

@@ -37,6 +37,7 @@ class Chart {
 
 	private $id = "";
 	private $title = "Untitled";
+	private bool $isPeriodic = false;
 	private $type = "bar";
 	private $datasets = [];
 	private $printDatasets = [];
@@ -570,6 +571,13 @@ class Chart {
 		return $this->categories;
 	}
 
+	public function isPeriodic(): bool {
+		return $this->isPeriodic;
+	}
+
+	public function setIsPeriodic(bool $isPeriodic): void {
+		$this->isPeriodic = $isPeriodic;
+	}
 
 
 }

+ 11 - 1
engine/chart_generator.php

@@ -378,6 +378,7 @@ class ChartGenerator {
 		}
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addXAxis([
 			"type" => "category",
 			"stacked" => true,
@@ -499,6 +500,7 @@ class ChartGenerator {
 		}
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addXAxis([
 			"type" => "category",
 			"stacked" => true,
@@ -543,7 +545,7 @@ class ChartGenerator {
 
 	public function getPeriodicEyeContactChart(): Chart {
 		return $this->getPeriodicBooleanChart(
-			"Eye Contact by {$this->resolutionName}",
+			"Eye Contact",
 			$this->getEyeContactRates()
 		);
 	}
@@ -581,6 +583,7 @@ class ChartGenerator {
 
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->setType("bar");
 		$chart->setTitle("{$title} by {$this->resolutionName}");
 		$chart->setLabels($this->xDataLabels);
@@ -682,6 +685,7 @@ class ChartGenerator {
 		$counts = array_merge($male_counts, $female_counts);
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addYAxis([
 			"id" => "y-axis-1",
 			"tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
@@ -705,6 +709,7 @@ class ChartGenerator {
 		$pass_rate_max = ceil(max($pass_rates) / 10) * 10;
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addYAxis([
 			"id" => "y-axis-1",
 			"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
@@ -800,6 +805,7 @@ class ChartGenerator {
 		$counts = array_merge($mcm_counts, $mcf_counts, $fcm_counts, $fcf_counts);
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addYAxis([
 			"id" => "y-axis-1",
 			"tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
@@ -850,6 +856,7 @@ class ChartGenerator {
 		}
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addYAxis([
 			"id" => "y-axis-1",
 			"tick_config" => ["min" => 0, "max" => 100],
@@ -976,6 +983,7 @@ class ChartGenerator {
 		}
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addXAxis([
 			"type" => "category",
 			"scale_label_config" => [
@@ -1356,6 +1364,7 @@ class ChartGenerator {
 		$yes_counts = array_column($values, 1);
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->setType("bar");
 		$chart->setTitle("{$title} by {$this->resolutionName}");
 		$chart->setLabels($this->xDataLabels);
@@ -1372,6 +1381,7 @@ class ChartGenerator {
 	private function getPeriodicPassRateChart(string $title, array $yes_rates, array $no_rates, ?array $null_rates = null): Chart {
 
 		$chart = new Chart();
+		$chart->setIsPeriodic(true);
 		$chart->addYAxis([
 			"tick_config" => ["min" => 0, "max" => 100],
 		]);

+ 0 - 4
template_processing/age_verification_greene_king/reports_build_charts.php

@@ -142,8 +142,6 @@ if($generator->hasBrandData()) {
 	$charts[] = $periodic_brand_pass_rate_chart;
 }
 
-
-
 if($generator->hasOperationsManagerData()) {
 	$om_pass_rate_chart = $generator->getOperationsManagerPassRateChart();
 	$om_pass_rate_chart->setCategories(["All", "Gaming", "Operations Manager"]);
@@ -154,8 +152,6 @@ if($generator->hasOperationsManagerData()) {
 	$charts[] = $periodic_om_pass_rate_chart;
 }
 
-
-
 $charts = array_filter($charts, static function(Chart $chart) use ($exclude_categories) {
 	foreach($exclude_categories as $exclude_category) {
 		$categories = $chart->getCategories();

+ 55 - 10
views/reports.php

@@ -9,6 +9,8 @@
  * @var array $exclude_categories
  * @var object $template_config
  *
+ * @var int $project_id
+ *
  * @var string $date_range
  * @var string $date_resolution
  * @var string $date_start
@@ -61,13 +63,34 @@ if($date_start && $date_end) {
 	$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();
+$collator = new Collator("en_GB");
+
+if($charts) {
+	$brands = array_keys($brand_data);
+	$regions = array_keys($region_data);
+	$area_managers = array_keys($area_manager_data);
+	$operations_managers = array_keys($operations_manager_data);
+}
+else {
+	$brands = $customer->getBrands();
+	$regions = $customer->getRegions();
+	$area_managers = $customer->getAreaManagerNames();
+	$operations_managers = $customer->getOperationsManagerNames();
+}
+
 $gaming_managers = $customer->getGamingManagerNames();
 $senior_gaming_managers = $customer->getSeniorGamingManagerNames();
 
+$collator->sort($brands);
+$collator->sort($regions);
+$collator->sort($area_managers);
+$collator->sort($operations_managers);
+
+$project_options = [];
+foreach($project_table_data as $project_data) {
+	$project_options[$project_data["id"]] = $project_data["name"];
+}
+
 ?>
 <script>
 let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
@@ -103,6 +126,17 @@ let projectData = <?=json_encode($project_table_data)?>;
 
 		<div class="row">
 			<div class="col-md">
+
+				<div class="form-group">
+					<label for="project"><?=htmlspecialchars(__("Project"))?></label>
+					<select class="form-control" name="project_id" id="project">
+					    <option value=""><?=htmlspecialchars(__("Select Project"))?></option>
+						<?php
+						HtmlHelper::renderOptions($project_options, $project_id);
+						?>
+					</select>
+				</div>
+
 				<div class="form-group">
 					<label class="form-label" for="date-range">
 						Date Range
@@ -182,13 +216,11 @@ let projectData = <?=json_encode($project_table_data)?>;
 							<?php
 						}
 						?>
+						<input type="hidden" name="exclude_categories[]" value="none" />
 					</div>
 					<?php
 				}
-				?>
 
-
-				<?php
 				/**
 				 * @param string $name
 				 * @param string $id
@@ -402,6 +434,9 @@ let projectData = <?=json_encode($project_table_data)?>;
 				<div class="list-group limit-height-2" style="position: sticky; top: 7rem;">
 				<?php
 				foreach($charts as $index => $chart) {
+					if($project_id && $chart->isPeriodic()) {
+						continue;
+					}
 					?>
 					<a class="list-group-item list-group-item-action" href="#chart-<?=$index?>">
 						<?=htmlspecialchars($chart->getTitle())?>
@@ -414,6 +449,10 @@ let projectData = <?=json_encode($project_table_data)?>;
 			<div class="col-md-8">
 			<?php
 			foreach($charts as $index => $chart) {
+				if($project_id && $chart->isPeriodic()) {
+					continue;
+				}
+
 				if($index > 0) {
 					?>
 					<br />
@@ -447,16 +486,22 @@ let projectData = <?=json_encode($project_table_data)?>;
 			?>
 			<form id="form-<?=$format?>" method="post" action="/account/reports/export-<?=$format?>" data-rendered="0">
 				<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
-				<input type="hidden" name="customer_id" value="<?=intval($customer->id)?>" />
+				<input type="hidden" name="customer_id" value="<?=$customer->id?>" />
+				<input type="hidden" name="project_id" value="<?=$project_id?>" />
 				<input type="hidden" name="date_range" value="<?=htmlspecialchars($date_range)?>" />
 				<input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
 				<input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
 				<input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
 				<input type="hidden" name="retests" value="<?=htmlspecialchars($retests)?>" />
 				<?php
-				foreach($exclude_project_ids as $project_id) {
+				foreach($exclude_project_ids as $exclude_project_id) {
+					?>
+					<input type="hidden" name="exclude_project_ids[]" value="<?=$exclude_project_id?>" />
+					<?php
+				}
+				foreach($exclude_categories as $exclude_category) {
 					?>
-					<input type="hidden" name="exclude_project_ids[]" value="<?=intval($project_id)?>" />
+					<input type="hidden" name="exclude_categories[]" value="<?=$exclude_category?>" />
 					<?php
 				}
 				?>