1
0

locations.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. use Exports\LocationCsvExport;
  3. use Exports\LocationKmlExport;
  4. use Searches\LocationSearch;
  5. Core::addScript("public/js/app-locations.js");
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/locations", "Locations");
  8. $customer = Customer::getForAdmin();
  9. if(!$customer) {
  10. throw new RuntimeException("You do not have permission to access this page.");
  11. }
  12. $user = Core::$user;
  13. $customer_slug = Util::slug($customer->name);
  14. $sorts = [
  15. "name" => "Name",
  16. "internal-id" => "Internal ID",
  17. "last-added" => "Most recently added first",
  18. "test-count" => "Highest test count first",
  19. "retest-count" => "Highest retest count first",
  20. ];
  21. $filter_options = [
  22. "" => "No Filter",
  23. "untested-current" => "Untested Only (Current Cycle)",
  24. "failed-current" => "Failed Only (Current Cycle)",
  25. "untested-all" => "Untested Only (All Time)",
  26. "failed-all" => "Failed Only (All Time)",
  27. ];
  28. $page_title = "$customer->name Locations";
  29. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  30. $nvp = [
  31. "customer_id" => $customer->id,
  32. "archived" => 0,
  33. "closed" => 0,
  34. ];
  35. $search = new LocationSearch($customer, $user);
  36. $id = http::value("id", "int");
  37. $search->setId($id);
  38. $ids = http::value("ids", "int");
  39. $search->setIds($ids);
  40. $internal_id = http::value("internal_id", "trim");
  41. $search->setInternalId($internal_id);
  42. $name = http::value("name", "trim");
  43. $search->setName($name);
  44. $legal_entity_name = http::value("legal_entity_name", "trim");
  45. $search->setLegalEntityName($legal_entity_name);
  46. $min_date = http::value("min_date");
  47. $search->setMinTestDate($min_date);
  48. $max_date = http::value("max_date");
  49. $search->setMaxTestDate($max_date);
  50. $brand_name = http::value("brand_name");
  51. $search->setBrandName($brand_name);
  52. $region_name = http::value("region_name");
  53. $search->setRegionName($region_name);
  54. $area_manager_name = http::value("area_manager_name");
  55. $search->setAreaManagerName($area_manager_name);
  56. $operations_manager_name = http::value("operations_manager_name");
  57. $search->setOperationsManagerName($operations_manager_name);
  58. $gaming_manager_name = http::value("gaming_manager_name");
  59. $search->setGamingManagerName($gaming_manager_name);
  60. $senior_gaming_manager_name = http::value("senior_gaming_manager_name");
  61. $search->setSeniorGamingManagerName($senior_gaming_manager_name);
  62. $include_archived = (int) http::value("include_archived");
  63. $search->setIncludeArchived($include_archived);
  64. $include_closed = (int) http::value("include_closed");
  65. $search->setIncludeClosed($include_closed);
  66. $filter = http::value("filter");
  67. $search->setFilter($filter);
  68. $filter_count = 0;
  69. $where_strings = [];
  70. if($min_date) {
  71. $filter_count++;
  72. }
  73. if($max_date) {
  74. $filter_count++;
  75. }
  76. $sort = http::value("sort");
  77. switch($sort) {
  78. case "last-added":
  79. $query_sort = "id";
  80. $order = "desc";
  81. break;
  82. case "test-count":
  83. $query_sort = "test_count";
  84. $order = "desc";
  85. break;
  86. case "retest-count":
  87. $query_sort = "retest_count";
  88. $order = "desc";
  89. break;
  90. default:
  91. $query_sort = "name";
  92. $order = "asc";
  93. break;
  94. }
  95. $locations = $search->run();
  96. $filter_count += $search->getFilterCount();
  97. $location_ids = array_column($locations, "id") ?: [0];
  98. $location_ids_csv = implode(", ", $location_ids);
  99. [$date_start, $date_end] = $customer->getYearCycleDates();
  100. $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count`
  101. FROM `location`
  102. LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end' AND `test`.`status`='" . Test::STATUS_COMPLETED . "'
  103. WHERE `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
  104. ORDER BY $query_sort $order
  105. ";
  106. $locations = Location::findBySql($sql);
  107. if($sort === "internal-id") {
  108. usort($locations, static function(Location $a, Location $b) {
  109. return strnatcasecmp($a->internal_id, $b->internal_id);
  110. });
  111. }
  112. $action = http::value("action");
  113. if(!empty($action)) {
  114. switch($action) {
  115. case "export":
  116. if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
  117. $export = new LocationCsvExport($locations);
  118. $export->export("$customer_slug-export.csv");
  119. exit();
  120. case "export-kml":
  121. $export = new LocationKmlExport($locations);
  122. $export->setCustomer($customer);
  123. $export->export("$customer_slug-export.kml");
  124. exit;
  125. }
  126. }
  127. View::includeOnce("header", compact("page_title"));
  128. View::include("locations", compact("customer", "search", "filter_count", "sorts", "sort", "filter_options", "locations"));
  129. View::include("modal_add_locations_to_project", compact("customer"));
  130. View::includeOnce("footer");