1
0

sync_venues_by_number_of_machines.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. require "../initialise.php";
  3. function readInteger($prompt = ""): int {
  4. return (int) readline($prompt);
  5. }
  6. function readString($prompt = ""): string {
  7. return (string) readline($prompt);
  8. }
  9. try {
  10. $customer_id = readInteger("Enter customer ID: ");
  11. $customer = Customer::get($customer_id);
  12. if(!$customer) throw new Exception("Customer not found.");
  13. echo "\n";
  14. echo "CUSTOMER:\n";
  15. echo $customer->name, "\n";
  16. echo "\n";
  17. $csv_path = readString(sprintf("Enter CSV path, relative to /%s: ", basename(getcwd())));
  18. if(!file_exists($csv_path)) throw new Exception("{$csv_path} not found.");
  19. $ids_to_delete = [];
  20. $skipped_locations_has_machines = 0;
  21. $skipped_locations_has_test = 0;
  22. $skipped_locations_unknown = 0;
  23. $internal_id_index = null;
  24. $number_of_machines_index = null;
  25. $handle = fopen($csv_path, "r");
  26. $headers = fgetcsv($handle);
  27. foreach($headers as $index => $header) {
  28. switch($header) {
  29. case "Internal ID":
  30. $internal_id_index = $index;
  31. break;
  32. case "Number of Machines":
  33. $number_of_machines_index = $index;
  34. break;
  35. }
  36. }
  37. if($internal_id_index === null) throw new Exception("Internal ID column not found.");
  38. if($number_of_machines_index === null) throw new Exception("Number of Machines column not found.");
  39. while($data = fgetcsv($handle)) {
  40. $internal_id = $data[$internal_id_index];
  41. $number_of_machines = $data[$number_of_machines_index];
  42. if($number_of_machines > 0) {
  43. $skipped_locations_has_machines++;
  44. continue;
  45. }
  46. $location = Location::queryOne([
  47. "customer_id" => $customer->id,
  48. "internal_id" => $internal_id,
  49. ]);
  50. if(!$location) {
  51. $skipped_locations_unknown++;
  52. continue;
  53. }
  54. $tests = $location->getTests();
  55. if(count($tests) > 0) {
  56. $skipped_locations_has_test++;
  57. continue;
  58. }
  59. $ids_to_delete[] = $location->id;
  60. }
  61. echo "\n";
  62. echo "REPORT:\n";
  63. echo count($ids_to_delete) . " locations will be deleted\n";
  64. echo $skipped_locations_unknown . " locations not in database and will be ignored\n";
  65. echo $skipped_locations_has_test . " locations have no machines with at least one test and will not be deleted\n";
  66. echo $skipped_locations_has_machines . " locations have machines and will not be deleted\n";
  67. echo "\n";
  68. $confirm = readString("Confirm [Y/N]: ");
  69. if($confirm === "Y") {
  70. echo "Deleting ...";
  71. foreach($ids_to_delete as $id) {
  72. $location = Location::get($id);
  73. $location->delete();
  74. }
  75. echo "Done\n";
  76. }
  77. echo "Finished";
  78. }
  79. catch(Exception $e) {
  80. echo "Error: " . $e->getMessage();
  81. }
  82. echo "\n";