| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?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";
|