| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- use Exports\LocationCsvExport;
- use Exports\LocationKmlExport;
- use Searches\LocationSearch;
- Core::addScript("public/js/app-locations.js");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/locations", "Locations");
- $customer = Customer::getForAdmin();
- if(!$customer) {
- throw new Exception("You do not have permission to access this page.");
- }
- $customer_slug = Util::slug($customer->name);
- $sorts = [
- "name" => "Name",
- "internal-id" => "Internal ID",
- "last-added" => "Most recently added first",
- "test-count" => "Highest test count first",
- "retest-count" => "Highest retest count first",
- ];
- $filter_options = [
- "" => "No Filter",
- "untested-current" => "Untested Only (Current Cycle)",
- "failed-current" => "Failed Only (Current Cycle)",
- "untested-all" => "Untested Only (All Time)",
- "failed-all" => "Failed Only (All Time)",
- ];
- $page_title = "$customer->name Locations";
- Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
- $nvp = [
- "customer_id" => $customer->id,
- "archived" => 0,
- "closed" => 0,
- ];
- $search = new LocationSearch($customer);
- $id = http::value("id", "int");
- $search->setId($id);
- $ids = http::value("ids", "int");
- $search->setIds($ids);
- $internal_id = http::value("internal_id", "trim");
- $search->setInternalId($internal_id);
- $name = http::value("name", "trim");
- $search->setName($name);
- $legal_entity_name = http::value("legal_entity_name", "trim");
- $search->setLegalEntityName($legal_entity_name);
- $min_date = http::value("min_date");
- $search->setMinTestDate($min_date);
- $max_date = http::value("max_date");
- $search->setMaxTestDate($max_date);
- $include_archived = (int) http::value("include_archived");
- $search->setIncludeArchived($include_archived);
- $include_closed = (int) http::value("include_closed");
- $search->setIncludeClosed($include_closed);
- $filter = http::value("filter");
- $search->setFilter($filter);
- $filter_count = 0;
- $where_strings = [];
- if($id) {
- $filter_count++;
- }
- if($internal_id) {
- $filter_count++;
- }
- if($name) {
- $filter_count++;
- }
- if($legal_entity_name) {
- $filter_count++;
- }
- if($include_archived) {
- $filter_count++;
- }
- if($include_closed) {
- $filter_count++;
- }
- if($min_date) {
- $filter_count++;
- }
- if($max_date) {
- $filter_count++;
- }
- if($filter) {
- $filter_count++;
- }
- $sort = http::value("sort");
- switch($sort) {
- case "last-added":
- $query_sort = "id";
- $order = "desc";
- break;
- case "test-count":
- $query_sort = "test_count";
- $order = "desc";
- break;
- case "retest-count":
- $query_sort = "retest_count";
- $order = "desc";
- break;
- default:
- $query_sort = "name";
- $order = "asc";
- break;
- }
- $locations = $search->run();
- $location_ids = array_column($locations, "id") ?: [0];
- $location_ids_csv = implode(", ", $location_ids);
- [$date_start, $date_end] = $customer->getYearCycleDates();
- $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count`
- FROM `location`
- LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end'
- WHERE `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
- ORDER BY $query_sort $order
- ";
- $locations = Location::findBySql($sql);
- if($sort === "internal-id") {
- usort($locations, static function(Location $a, Location $b) {
- return strnatcasecmp($a->internal_id, $b->internal_id);
- });
- }
- $action = http::value("action");
- if(!empty($action)) {
- switch($action) {
- case "export":
- if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
- $export = new LocationCsvExport($locations);
- $export->export("$customer_slug-export.csv");
- exit();
- case "export-kml":
- $export = new LocationKmlExport($locations);
- $export->setCustomer($customer);
- $export->export("$customer_slug-export.kml");
- exit;
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("locations", compact("customer", "search", "filter_count", "sorts", "sort", "filter_options", "locations"));
- View::include("modal_add_locations_to_project", compact("customer"));
- View::includeOnce("footer");
|