Explorar el Código

Add test counts to customer CSV export

Alan Lam hace 3 años
padre
commit
e90f13483b

+ 10 - 6
engine/location.php

@@ -121,11 +121,13 @@ class Location extends Model {
 	 * @return Test[]
 	 * @throws Exception
 	 */
-	public function getTests() {
-		return Test::query([
+	public function getTests(array $nvp = []): array {
+		$nvp = array_merge($nvp, [
 			"location_id" => $this->id,
 			"status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
-		], [
+		]);
+
+		return Test::query($nvp, [
 			"sort" => "dts_start",
 			"order" => "desc",
 		]);
@@ -135,11 +137,13 @@ class Location extends Model {
 	 * @return Test[]
 	 * @throws Exception
 	 */
-	public function getCompletedTests() {
-		return Test::query([
+	public function getCompletedTests(array $nvp = []): array {
+		$nvp = array_merge($nvp, [
 			"location_id" => $this->id,
 			"status" => Test::STATUS_COMPLETED,
-		], [
+		]);
+
+		return Test::query($nvp, [
 			"sort" => "dts_start",
 			"order" => "desc",
 		]);

+ 7 - 3
engine/template.php

@@ -49,6 +49,10 @@ class Template extends Model {
 	 */
 	public function exportTestsCSV(string $processing_file, array $tests = []): void {
 
+		// Get customer
+		$customer = $this->getCustomer();
+		$year_cycle_constraint = $customer->getYearCycleConstraint(TIMEZONE);
+
 		// Preload
 		Project::load(array_column($tests, "project_id"));
 		Location::load(array_column($tests, "location_id"));
@@ -61,7 +65,7 @@ class Template extends Model {
 		$config = json_decode($this->getJSON());
 
 		$fields = array_merge($config->beforeTest, $config->duringTest);
-		/** @noinspection PhpUnusedLocalVariableInspection */
+
 		$fields = array_filter($fields, static function($field) {
 			if(!isset($field->name)) return false;
 			if($field->type === "file") return false;
@@ -90,10 +94,10 @@ class Template extends Model {
 	}
 
 	/**
-	 * @return Customer|null
+	 * @return Customer
 	 * @throws Exception
 	 */
-	public function getCustomer(): ?Customer {
+	public function getCustomer(): Customer {
 		return Customer::get($this->customer_id);
 	}
 

+ 19 - 6
template_processing/age_verification/project_customer_csv.php

@@ -1,15 +1,25 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var object[] $fields
+ * @var resource $handle
+ * @var Test[] $tests
+ * @var Constraint $year_cycle_constraint
+ */
 
 $field_names = [
 	"Test Result", "Test Void",
 	"Location Name", "Address", "Region", "Internal ID", "Area Manager",
 
 	"Date",
+	"All-Time Test Count", "Current Year Period Test Count"
 ];
 
 foreach($fields as $field) {
 	$customer_export = $field->customerExport ?? true;
-	if(!$customer_export) continue;
+	if(!$customer_export) {
+		continue;
+	}
 
 	$field_names[] = $field->label;
 }
@@ -17,12 +27,14 @@ foreach($fields as $field) {
 fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
-
 	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);
 
+	$total_test_count = $location->getCompletedTests();
+	$current_test_count = $location->getCompletedTests(["dts_start" => $year_cycle_constraint]);
+
 	$test_date = "";
 	$test_start = "";
 	$test_end = "";
@@ -36,7 +48,7 @@ foreach($tests as $test) {
 
 	$output = [];
 
-	if(!is_null($test->pass)) {
+	if($test->pass !== null) {
 
 		$result = $test->pass ? "Pass" : "Fail";
 
@@ -66,6 +78,8 @@ foreach($tests as $test) {
 	$output[] = $location->internal_id;
 	$output[] = $location->area_manager;
 	$output[] = $test_date;
+	$output[] = $total_test_count;
+	$output[] = $current_test_count;
 
 	foreach($fields as $field) {
 
@@ -73,7 +87,7 @@ foreach($tests as $test) {
 		if(!$customer_export) continue;
 
 		$field_name = $field->name;
-		$value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
+		$value = $test->$field_name ?? ($values[$field->name] ?? "");
 
 		if(!empty($field->displayStrings)) {
 			$index = array_search($value, $field->values);
@@ -86,4 +100,3 @@ foreach($tests as $test) {
 	fputcsv($handle, $output);
 }
 
-?>