瀏覽代碼

Update import venues script

Alan Lam 3 年之前
父節點
當前提交
2bf5701992
共有 2 個文件被更改,包括 71 次插入 和 3 次删除
  1. 6 0
      engine/constraint.php
  2. 65 3
      tasks/import_venues.php

+ 6 - 0
engine/constraint.php

@@ -65,6 +65,12 @@ class Constraint {
 		return new Constraint("IS_NOT_NULL");
 	}
 
+	public static function lessThan(string $value): Constraint {
+		$constraint = new Constraint("LESS_THAN");
+		$constraint->setValues($value);
+		return $constraint;
+	}
+
 	/**
 	 * Constraint constructor.
 	 * @param string $type

+ 65 - 3
tasks/import_venues.php

@@ -10,6 +10,8 @@
  * 3. Postal Code
  */
 
+use Carbon\CarbonImmutable;
+
 require "../initialise.php";
 
 if($argc < 4) {
@@ -17,13 +19,26 @@ if($argc < 4) {
 	exit(0);
 }
 
-[$script, $path, $customer_id, $region_code] = $argv;
-
 $script = array_shift($argv);
 $path = array_shift($argv);
 $customer_id = array_shift($argv);
 $region_code = array_shift($argv);
 
+$archive_missing = false;
+
+while($argv) {
+	$argument = array_shift($argv);
+	switch($argument) {
+		case "--archive-missing":
+			echo "Archive missing ON\n";
+			$archive_missing = true;
+			break;
+
+		default:
+			throw new RuntimeException("Invalid argument: " . $argument);
+	}
+}
+
 if(!file_exists($path)) {
 	echo $path . " not found.\n";
 	exit(1);
@@ -35,6 +50,11 @@ if(!$customer) {
 	exit(1);
 }
 
+$missing_locations = [];
+foreach($customer->getLocations() as $location) {
+	$missing_locations[$location->id] = $location;
+}
+
 $confirm = trim(readline($customer->name . ": Confirm Y/N: "));
 if($confirm !== "Y") {
 	echo "Aborted.\n";
@@ -241,7 +261,49 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 	}
 
 	$success = $location->commit();
-	if(!$success) throw new Exception("Commit failed.");
+	if(!$success) {
+		throw new RuntimeException("Commit failed.");
+	}
+
+	if(isset($missing_locations[$location->id])) {
+		unset($missing_locations[$location->id]);
+	}
+
 	echo "OK\n";
+}
+
+// Archive missing locations
+if($archive_missing) {
+	foreach($missing_locations as $missing_location) {
+		$missing_location->archive = 1;
+		$missing_location->commit();
+	}
+}
+
+// Write CSV on missing locations in future projects
+$handle = fopen("tests_for_missing_locations.csv", "wb");
+fputcsv($handle, ["Project", "Location"]);
 
+$missing_location_ids = array_column($missing_locations, "id");
+
+$dt_now = CarbonImmutable::now();
+$dt_threshold = $dt_now->addDays(90);
+
+$projects = Project::query([
+	"customer_id" => $customer->id,
+	"dts_start" => Constraint::between((string) $dt_now, (string) $dt_threshold)
+], [
+	"sort" => "dts_start"
+]);
+
+foreach($projects as $project) {
+	$tests = Test::query([
+		"project_id" => $project->id,
+		"location_id" => $missing_location_ids
+	]);
+
+	foreach($tests as $test) {
+		$location = $test->getLocation();
+		fputcsv($handle, [$project->name, $location->name]);
+	}
 }