locations.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Exception("You do not have permission to access this page.");
  11. }
  12. $customer_slug = Util::slug($customer->name);
  13. $sorts = [
  14. "name" => "Name",
  15. "internal-id" => "Internal ID",
  16. "last-added" => "Most recently added first",
  17. "test-count" => "Highest test count first",
  18. "retest-count" => "Highest retest count first",
  19. ];
  20. $page_title = "$customer->name Locations";
  21. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  22. $nvp = [
  23. "customer_id" => $customer->id,
  24. "archived" => 0,
  25. "closed" => 0,
  26. ];
  27. $search = new LocationSearch($customer);
  28. $id = http::value("id", "int");
  29. $search->setId($id);
  30. $ids = http::value("ids", "int");
  31. $search->setIds($ids);
  32. $internal_id = http::value("internal_id", "trim");
  33. $search->setInternalId($internal_id);
  34. $name = http::value("name", "trim");
  35. $search->setName($name);
  36. $legal_entity_name = http::value("legal_entity_name", "trim");
  37. $search->setLegalEntityName($legal_entity_name);
  38. $min_date = http::value("min_date");
  39. $search->setMinTestDate($min_date);
  40. $max_date = http::value("max_date");
  41. $search->setMaxTestDate($max_date);
  42. $include_archived = (int) http::value("include_archived");
  43. $search->setIncludeArchived($include_archived);
  44. $include_closed = (int) http::value("include_closed");
  45. $search->setIncludeClosed($include_closed);
  46. $hide_tested = (int) http::value("hide_tested");
  47. $search->setIncludeTested(!$hide_tested);
  48. $filter_count = 0;
  49. $where_strings = [];
  50. if($id) {
  51. $filter_count++;
  52. }
  53. if($internal_id) {
  54. $filter_count++;
  55. }
  56. if($name) {
  57. $filter_count++;
  58. }
  59. if($legal_entity_name) {
  60. $filter_count++;
  61. }
  62. if($include_archived) {
  63. $filter_count++;
  64. }
  65. if($include_closed) {
  66. $filter_count++;
  67. }
  68. if($min_date) {
  69. $filter_count++;
  70. }
  71. if($max_date) {
  72. $filter_count++;
  73. }
  74. if($hide_tested) {
  75. $filter_count++;
  76. }
  77. $sort = http::value("sort");
  78. switch($sort) {
  79. case "last-added":
  80. $query_sort = "id";
  81. $order = "desc";
  82. break;
  83. case "test-count":
  84. $query_sort = "test_count";
  85. $order = "desc";
  86. break;
  87. case "retest-count":
  88. $query_sort = "retest_count";
  89. $order = "desc";
  90. break;
  91. default:
  92. $query_sort = "name";
  93. $order = "asc";
  94. break;
  95. }
  96. $locations = $search->run();
  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'
  103. WHERE `customer_id`='$customer->id' AND `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", "include_archived", "include_closed", "hide_tested", "filter_count", "sorts", "sort", "locations"));
  129. View::include("modal_add_locations_to_project", compact("customer"));
  130. View::includeOnce("footer");