import_venues.php 9.2 KB

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