import_venues.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * Imports venues from a CSV file for a customer
  4. *
  5. * Each location MUST have at least:
  6. *
  7. * 1. Internal ID
  8. * 2. City
  9. * 3. Postal Code
  10. */
  11. use Carbon\CarbonImmutable;
  12. require "../initialise.php";
  13. if($argc < 3) {
  14. echo sprintf("Usage: php %s [PATH] [CUSTOMER ID]\n", basename(__FILE__));
  15. exit(0);
  16. }
  17. $script = array_shift($argv);
  18. $path = array_shift($argv);
  19. $customer_id = array_shift($argv);
  20. $dry_run = false;
  21. $archive_missing = false;
  22. $region_code = "GB";
  23. while($argv) {
  24. $argument = array_shift($argv);
  25. switch($argument) {
  26. case "--archive-missing":
  27. echo "Archive missing ON\n";
  28. $archive_missing = true;
  29. break;
  30. case "--region-code":
  31. $region_code = array_shift($argv);
  32. break;
  33. case "--dry-run":
  34. $dry_run = true;
  35. break;
  36. default:
  37. throw new RuntimeException("Invalid argument: " . $argument);
  38. }
  39. }
  40. if(!file_exists($path)) {
  41. echo $path . " not found.\n";
  42. exit(1);
  43. }
  44. $customer = Customer::get($customer_id);
  45. if(!$customer) {
  46. echo "Customer not found.\n";
  47. exit(1);
  48. }
  49. $missing_locations = [];
  50. foreach($customer->getLocations() as $location) {
  51. $missing_locations[$location->id] = $location;
  52. }
  53. echo $customer->name . "\n";
  54. echo sprintf("Region Code: %s\n", $region_code);
  55. if($dry_run) {
  56. echo "Dry run\n";
  57. }
  58. $confirm = trim(readline("Confirm Y/N: "));
  59. if($confirm !== "Y") {
  60. echo "Aborted.\n";
  61. exit(0);
  62. }
  63. $handle = fopen($path, "rb");
  64. date_default_timezone_set("UTC");
  65. for($i = 0; $data = fgetcsv($handle); $i++) {
  66. $row_number = $i + 1;
  67. $data = array_map("trim", $data);
  68. if($i === 0) {
  69. // Header
  70. foreach($data as $index => $field_name) {
  71. switch($field_name) {
  72. case "Internal ID": $internal_id_column = $index; break;
  73. case "Name": $name_column = $index; break;
  74. case "Legal Entity Name": $legal_entity_name_column = $index; break;
  75. case "Bar Name": $bar_name_column = $index; break;
  76. case "Address": $address_column = $index; break;
  77. case "Address 1": $address_1_column = $index; break;
  78. case "Address 2": $address_2_column = $index; break;
  79. case "City": $city_column = $index; break;
  80. case "County": $county_column = $index; break;
  81. case "Postcode":
  82. case "Post Code":
  83. case "Postal Code": $postal_code_column = $index; break;
  84. case "State": $state_column = $index; break;
  85. case "Country": $country_column = $index; break;
  86. case "Email": $email_column = $index; break;
  87. case "Phone": $phone_column = $index; break;
  88. case "Opening Hours": $opening_hours_column = $index; break;
  89. case "Closing Hours": $closing_hours_column = $index; break;
  90. case "Shop Left": $shop_left_column = $index; break;
  91. case "Shop Right": $shop_right_column = $index; break;
  92. case "Brand": $brand_column = $index; break;
  93. case "Region": $region_column = $index; break;
  94. case "Region Name": $region_name_column = $index; break;
  95. case "Area Manager": $area_manager_column = $index; break;
  96. case "Area Manager Name": $area_manager_name_column = $index; break;
  97. case "Area Manager Email": $area_manager_email_column = $index; break;
  98. case "Operations Manager Name": $operations_manager_name_column = $index; break;
  99. case "Archived": $archived_column = $index; break;
  100. case "Closed": $closed_column = $index; break;
  101. case "Latitude": $latitude_column = $index; break;
  102. case "Longitude": $longitude_column = $index; break;
  103. case "Ignore":
  104. break;
  105. default:
  106. throw new RuntimeException("Unknown field name: {$field_name}");
  107. }
  108. }
  109. continue;
  110. }
  111. $internal_id = isset($internal_id_column) ? $data[$internal_id_column] : null;
  112. $city = isset($city_column) ? $data[$city_column] : null;
  113. $legal_entity_name = isset($legal_entity_name_column) ? $data[$legal_entity_name_column] : null;
  114. $bar_name = isset($bar_name_column) ? $data[$bar_name_column] : null;
  115. $region = isset($region_column) ? $data[$region_column] : null;
  116. $area_manager = isset($area_manager_column) ? $data[$area_manager_column] : null;
  117. $opening_hours = isset($opening_hours_column) ? $data[$opening_hours_column] : null;
  118. $closing_hours = isset($closing_hours_column) ? $data[$closing_hours_column] : null;
  119. $name = isset($name_column) ? $data[$name_column] : null;
  120. $address = isset($address_column) ? $data[$address_column] : null;
  121. $address_1 = isset($address_1_column) ? $data[$address_1_column] : null;
  122. $postal_code = isset($postal_code_column) ? $data[$postal_code_column] : null;
  123. $email = isset($email_column) ? $data[$email_column] : null;
  124. $latitude = isset($latitude_column) ? $data[$latitude_column] : null;
  125. $longitude = isset($longitude_column) ? $data[$longitude_column] : null;
  126. $address_2 = isset($address_2_column) ? $data[$address_2_column] : null;
  127. $county = isset($county_column) ? $data[$county_column] : null;
  128. $phone = isset($phone_column) ? $data[$phone_column] : null;
  129. $archived = isset($archived_column) ? $data[$archived_column] : null;
  130. $closed = isset($closed_column) ? $data[$closed_column] : null;
  131. $brand = isset($brand_column) ? $data[$brand_column] : null;
  132. $area_manager_name = isset($area_manager_name_column) ? $data[$area_manager_name_column] : null;
  133. $area_manager_email = isset($area_manager_email_column) ? $data[$area_manager_email_column] : null;
  134. $region_name = isset($region_name_column) ? $data[$region_name_column] : null;
  135. $operations_manager_name = isset($operations_manager_name_column) ? $data[$operations_manager_name_column] : null;
  136. if($address) {
  137. // Try to determine address parts from address
  138. $parts = explode(",", $address);
  139. $parts = array_map("trim", $parts);
  140. $postal_code = array_pop($parts);
  141. $address_1 = array_shift($parts);
  142. $count = count($parts);
  143. if($count === 2) [$address_2, $city] = $parts;
  144. else if($count === 1) [$city] = $parts;
  145. }
  146. if(empty($internal_id)) {
  147. continue;
  148. }
  149. if(isset($address_2) && !isset($city)) {
  150. $city = $address_2;
  151. $address_2 = null;
  152. }
  153. if(empty($city) && empty($postal_code)) {
  154. echo sprintf("Skipping row %d as there is no city or postal code\n", $row_number);
  155. continue;
  156. }
  157. echo "Importing $internal_id - $address_1 ($city) ($bar_name) ... ";
  158. $location = Location::queryOne([
  159. "customer_id" => $customer_id,
  160. "internal_id" => $internal_id
  161. ]);
  162. if(!$location) {
  163. $location = new Location();
  164. $location->customer_id = $customer_id;
  165. $location->internal_id = $internal_id;
  166. }
  167. if(!empty($email)) $location->email = $email;
  168. if(!empty($latitude)) $location->latitude = $latitude;
  169. if(!empty($longitude)) $location->longitude = $longitude;
  170. if(!empty($address_2)) $location->address_2 = $address_2;
  171. if(!empty($county)) $location->county = $county;
  172. if(!empty($phone)) $location->phone = $phone;
  173. if(!empty($brand)) $location->brand = $brand;
  174. if(!empty($area_manager_name)) $location->area_manager_name = $area_manager_name;
  175. if(!empty($area_manager_email)) $location->area_manager_email = $area_manager_email;
  176. if(!empty($region_name)) $location->region_name = $region_name;
  177. if(!empty($operations_manager_name)) $location->operations_manager_name = $operations_manager_name;
  178. if(!empty($city)) $location->city = $city;
  179. if(!empty($legal_entity_name)) $location->legal_entity_name = $legal_entity_name;
  180. if(!empty($region)) $location->region = $region;
  181. if(!empty($area_manager)) $location->area_manager = $area_manager;
  182. if(!empty($opening_hours) || !empty($closing_hours)) {
  183. $hours_parts = [];
  184. if($opening_hours) $hours_parts[] = "Opening Hours: $opening_hours";
  185. if($closing_hours) $hours_parts[] = "Closing Hours: $closing_hours";
  186. $location->opening_hours = implode("\n", $hours_parts);
  187. }
  188. if(!empty($name)) {
  189. $location->name = $name;
  190. if($city && stripos($name, $city) === false) {
  191. $location->name .= " ($city)";
  192. }
  193. if($bar_name) {
  194. $location->name .= " ($bar_name)";
  195. }
  196. }
  197. if(!empty($address_1) && !empty($city)) {
  198. $location->address_1 = str_replace(", $city", "", $address_1);
  199. }
  200. if(!empty($postal_code)) {
  201. $location->postal_code = $postal_code;
  202. }
  203. if($archived !== null) {
  204. $location->archived = $archived ? 1 : 0;
  205. }
  206. if($closed !== null) {
  207. $location->closed = $closed ? 1 : 0;
  208. }
  209. if(!$dry_run && !$location->latitude) {
  210. for($attempt = 0; $attempt < 2; $attempt++) {
  211. switch($attempt) {
  212. default:
  213. case 0: $query = $location->getDisplayAddress(); break;
  214. case 1: $query = $postal_code; break;
  215. }
  216. try {
  217. $results = Geolocation::geocode($query, $region_code);
  218. if($results) {
  219. $result = $results[0];
  220. $location->latitude = $result->geometry->location->lat;
  221. $location->longitude = $result->geometry->location->lng;
  222. break;
  223. }
  224. }
  225. catch(Exception $e) {
  226. echo $e->getMessage() . ": $query ... ";
  227. }
  228. }
  229. }
  230. if($dry_run) {
  231. $success = true;
  232. }
  233. else {
  234. $success = $location->commit();
  235. }
  236. if(!$success) {
  237. throw new RuntimeException("Commit failed.");
  238. }
  239. if(isset($missing_locations[$location->id])) {
  240. unset($missing_locations[$location->id]);
  241. }
  242. echo "OK\n";
  243. }
  244. // Archive missing locations
  245. if($archive_missing) {
  246. foreach($missing_locations as $missing_location) {
  247. $missing_location->archived = 1;
  248. $missing_location->commit();
  249. }
  250. }
  251. // Write CSV on missing locations in future projects
  252. $handle = fopen("tests_for_missing_locations.csv", "wb");
  253. fputcsv($handle, ["Project", "Location ID", "Location", "Internal ID"]);
  254. $missing_location_ids = array_column($missing_locations, "id");
  255. $dt_now = CarbonImmutable::now();
  256. $dt_threshold = $dt_now->addDays(90);
  257. $projects = Project::query([
  258. "customer_id" => $customer->id,
  259. "dts_start" => Constraint::between((string) $dt_now, (string) $dt_threshold)
  260. ], [
  261. "sort" => "dts_start"
  262. ]);
  263. foreach($projects as $project) {
  264. $tests = Test::query([
  265. "project_id" => $project->id,
  266. "location_id" => $missing_location_ids
  267. ]);
  268. foreach($tests as $test) {
  269. $location = $test->getLocation();
  270. fputcsv($handle, [
  271. $project->name,
  272. $location->id,
  273. $location->name,
  274. $location->internal_id
  275. ]);
  276. }
  277. }