Преглед изворни кода

Add test and retest counts on locations page

Alan Lam пре 4 година
родитељ
комит
d16730303d
5 измењених фајлова са 88 додато и 8 уклоњено
  1. 29 4
      controllers/locations.php
  2. 10 1
      engine/customer.php
  3. 16 0
      engine/model.php
  4. 28 0
      views/location_list.php
  5. 5 3
      views/locations.php

+ 29 - 4
controllers/locations.php

@@ -15,8 +15,10 @@ $customer_slug = Util::slug($customer->name);
 
 $sorts = [
 	"name" => "Name",
-	"last-added" => "Last Added",
 	"internal-id" => "Internal ID",
+	"last-added" => "Most recently added first",
+	"test-count" => "Highest test count first",
+	"retest-count" => "Highest retest count first",
 ];
 
 $page_title = "$customer->name Locations";
@@ -78,16 +80,39 @@ switch($sort) {
 		$query_sort = "id";
 		$order = "desc";
 		break;
+
+	case "test-count":
+		$query_sort = "test_count";
+		$order = "desc";
+		break;
+
+	case "retest-count":
+		$query_sort = "retest_count";
+		$order = "desc";
+		break;
+
 	default:
 		$query_sort = "name";
 		$order = "asc";
 		break;
 }
 
-$locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
+[$date_start, $date_end] = $customer->getYearCycleDates();
+
+$sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count` 
+	FROM `location` 
+    LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end' 
+	WHERE `customer_id`='$customer->id' GROUP BY `location`.`id`
+	ORDER BY $query_sort $order
+";
+
+$locations = Location::findBySql($sql);
+
+
+//$locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
 
-if($sort == "internal-id") {
-	usort($locations, function($a, $b) {
+if($sort === "internal-id") {
+	usort($locations, static function(Location $a, Location $b) {
 		return strnatcasecmp($a->internal_id, $b->internal_id);
 	});
 }

+ 10 - 1
engine/customer.php

@@ -1,5 +1,7 @@
 <?php
 
+use Carbon\CarbonImmutable;
+
 /**
  * @property int $id
  * @property string $name
@@ -119,7 +121,6 @@ class Customer extends Model {
 	}
 
 	public function getYearCycleDates(): array {
-
 		$month = $this->year_cycle_month;
 		if(!$month) $month = 1;
 
@@ -142,7 +143,15 @@ class Customer extends Model {
 			date("Y-m-d", $min_time),
 			date("Y-m-d", $max_time),
 		];
+	}
+
+	public function getDisplayYearCycle(): string {
+		[$date_start, $date_end] = $this->getYearCycleDates();
+
+		$dt_start = CarbonImmutable::parse($date_start);
+		$dt_end = CarbonImmutable::parse($date_end);
 
+		return sprintf("%s - %s", $dt_start->format("d/m/Y"), $dt_end->format("d/m/Y"));
 	}
 
 	public function getLogoPath(): ?string {

+ 16 - 0
engine/model.php

@@ -309,6 +309,22 @@ abstract class Model {
 		return static::findByValueString($vs, $options);
 	}
 
+	/**
+	 * @param string $sql
+	 * @return static[]
+	 * @throws Exception
+	 */
+	public static function findBySql(string $sql): array {
+		$objects = [];
+
+		$result = DB::query($sql);
+		while($row = DB::fetchObject($result)) {
+			$objects[] = new static($row);
+		}
+
+		return $objects;
+	}
+
 	/**
 	 * @param string $vs
 	 * @param array $options_array

+ 28 - 0
views/location_list.php

@@ -1,7 +1,23 @@
 <?php
 /**
+ * @var Customer $customer
  * @var Location[] $locations
  */
+
+[$date_start, $date_end] = $customer->getYearCycleDates();
+
+$counts = [];
+
+if($locations) {
+	$location_ids_csv = implode(", ", array_column($locations, "id"));
+
+	$sql = "SELECT `location_id`, COUNT(`id`) AS `test_count`, SUM(IF(`retest` = 1, 1, 0)) AS `retest_count` FROM `test` WHERE `location_id` IN ($location_ids_csv) AND `dts_start` BETWEEN '$date_start' AND '$date_end' GROUP BY `location_id`";
+	$result = DB::query($sql);
+	while($row = DB::fetchAssoc($result)) {
+		$counts[$row["location_id"]] = $row;
+	}
+}
+
 ?>
 <div class="table-responsive limit-height-2">
 	<table class="table table-sm table-sm-text table-hover">
@@ -11,6 +27,8 @@
 				<th>Name / Address</th>
 				<th>Legal Entity</th>
 				<th>Internal ID</th>
+				<th>Tests</th>
+				<th>Retests</th>
 				<th></th>
 			</tr>
 		</thead>
@@ -19,6 +37,10 @@
 		foreach($locations as $location) {
 			$class = "";
 			if($location->isArchived()) $class = "text-muted";
+
+			$test_count = $counts[$location->id]["test_count"] ?? 0;
+			$retest_count = $counts[$location->id]["retest_count"] ?? 0;
+
 			?>
 			<tr class="<?=$class?>">
 				<td class="text-right pr-3">
@@ -47,6 +69,12 @@
 				<td>
 					<?=htmlspecialchars($location->internal_id)?>
 				</td>
+				<td class="text-right">
+					<?=htmlspecialchars(number_format($test_count))?>
+				</td>
+				<td class="text-right">
+					<?=htmlspecialchars(number_format($retest_count))?>
+				</td>
 				<td class="text-right align-middle">
 					<a class="btn btn-sm btn-outline-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
 				</td>

+ 5 - 3
views/locations.php

@@ -5,7 +5,7 @@
  * @var int $include_archived
  * @var int $include_closed
  * @var int $filter_count
- * @var array $sorts
+ * @var string[] $sorts
  * @var string $sort
  * @var Location[] $locations
  */
@@ -151,8 +151,10 @@ var customerId = <?=$customer->id?>;
 	}
 	?>
 
-	<div class="row mb-3">
+	<div class="row mb-3 align-items-center">
 		<div class="col-sm">
+			<strong><?=htmlspecialchars(__("Year Cycle:"))?></strong>
+			<?=htmlspecialchars($customer->getDisplayYearCycle())?>
 		</div>
 		<div class="col-sm-auto">
 			<div class="input-group">
@@ -176,7 +178,7 @@ var customerId = <?=$customer->id?>;
 	</div>
 
 	<?php
-	View::include("location_list", compact("locations"));
+	View::include("location_list", compact("customer", "locations"));
 	?>
 </div>