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

Merge branch 'feature/sync-venues' into develop

Alan Lam 7 лет назад
Родитель
Сommit
0029afb796
1 измененных файлов с 107 добавлено и 0 удалено
  1. 107 0
      tasks/sync_venues_by_number_of_machines.php

+ 107 - 0
tasks/sync_venues_by_number_of_machines.php

@@ -0,0 +1,107 @@
+<?php
+
+require "../initialise.php";
+
+function readInteger($prompt = ""): int {
+	return (int) readline($prompt);
+}
+
+function readString($prompt = ""): string {
+	return (string) readline($prompt);
+}
+
+try {
+	$customer_id = readInteger("Enter customer ID: ");
+	$customer = Customer::get($customer_id);
+	if(!$customer) throw new Exception("Customer not found.");
+
+	echo "\n";
+	echo "CUSTOMER:\n";
+	echo $customer->name, "\n";
+	echo "\n";
+
+	$csv_path = readString(sprintf("Enter CSV path, relative to /%s: ", basename(getcwd())));
+	if(!file_exists($csv_path)) throw new Exception("{$csv_path} not found.");
+
+	$ids_to_delete = [];
+
+	$skipped_locations_has_machines = 0;
+	$skipped_locations_has_test = 0;
+	$skipped_locations_unknown = 0;
+
+	$internal_id_index = null;
+	$number_of_machines_index = null;
+
+	$handle = fopen($csv_path, "r");
+
+	$headers = fgetcsv($handle);
+	foreach($headers as $index => $header) {
+		switch($header) {
+			case "Internal ID":
+				$internal_id_index = $index;
+				break;
+			case "Number of Machines":
+				$number_of_machines_index = $index;
+				break;
+		}
+	}
+
+	if($internal_id_index === null) throw new Exception("Internal ID column not found.");
+	if($number_of_machines_index === null) throw new Exception("Number of Machines column not found.");
+
+	while($data = fgetcsv($handle)) {
+		$internal_id = $data[$internal_id_index];
+		$number_of_machines = $data[$number_of_machines_index];
+
+		if($number_of_machines > 0) {
+			$skipped_locations_has_machines++;
+			continue;
+		}
+
+		$location = Location::queryOne([
+			"customer_id" => $customer->id,
+			"internal_id" => $internal_id,
+		]);
+		if(!$location) {
+			$skipped_locations_unknown++;
+			continue;
+		}
+
+		$tests = $location->getTests();
+		if(count($tests) > 0) {
+			$skipped_locations_has_test++;
+			continue;
+		}
+
+		$ids_to_delete[] = $location->id;
+	}
+
+	echo "\n";
+
+	echo "REPORT:\n";
+	echo count($ids_to_delete) . " locations will be deleted\n";
+	echo $skipped_locations_unknown . " locations not in database and will be ignored\n";
+	echo $skipped_locations_has_test . " locations have no machines with at least one test and will not be deleted\n";
+	echo $skipped_locations_has_machines . " locations have machines and will not be deleted\n";
+
+	echo "\n";
+
+	$confirm = readString("Confirm [Y/N]: ");
+
+	if($confirm === "Y") {
+		echo "Deleting ...";
+		foreach($ids_to_delete as $id) {
+			$location = Location::get($id);
+			$location->delete();
+		}
+		echo "Done\n";
+	}
+
+	echo "Finished";
+
+}
+catch(Exception $e) {
+	echo "Error: " . $e->getMessage();
+}
+
+echo "\n";