getCustomers(); if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed."); else if(count($customers) == 1) { $customer = array_shift($customers); } else { $customer_id = http::value("customer_id", "int"); if(!$customer_id) { $page_title = "Select Customer"; View::includeOnce("header", compact("page_title")); View::include("customer_select", compact("customers")); View::includeOnce("footer"); exit; } $customer = Customer::get($customer_id); // Enforce restriction if user cannot access locations if(!$user->canAccessLocations()) { $link = UserCustomer::get(Core::$user->id, $customer_id); if(!$link) $customer = null; } } if(empty($customer)) throw new Exception("Customer not found."); return $customer; } // Object methods /** * @param array $query * @return Location[] * @throws Exception */ public function getLocations(array $query = []): array { $nvp = array_merge(["customer_id" => $this->id], $query); return Location::query($nvp, ["sort" => "name"]); } /** * @return Project[] * @throws Exception */ public function getProjects() { return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]); } public function getFinishedProjects() { $projects = array_filter($this->getProjects(), function(Project $p) { return $p->isFinished(); }); return array_values($projects); } /** * @return Template[] * @throws Exception */ public function getTemplates() { return Template::query(["customer_id" => $this->id], ["sort" => "name"]); } /** * @return string */ public function getTestResultsUrl(): string { $slug = Util::slugWithId($this->name, $this->id); $url = "/account/test-results/{$slug}"; return $url; } public function getYearCycleDates(): array { $month = $this->year_cycle_month; if(!$month) $month = 1; $current_year = date("Y"); $current_month = date("n"); if($current_month >= $month) { $min_dts = sprintf("%d-%02d-01", $current_year, $month); $max_dts = sprintf("%d-%02d-01", $current_year + 1, $month); } else { $min_dts = sprintf("%d-%02d-01", $current_year - 1, $month); $max_dts = sprintf("%d-%02d-01", $current_year, $month); } $min_time = strtotime($min_dts); $max_time = strtotime($max_dts) - 1; return [ date("Y-m-d", $min_time), date("Y-m-d", $max_time), ]; } public function getYearCycleConstraint(string $tz): Constraint { [$min_date, $max_date] = $this->getYearCycleDates(); $dt_min = CarbonImmutable::parse($min_date, $tz)->startOfDay()->utc(); $dt_max = CarbonImmutable::parse($max_date, $tz)->endOfDay()->utc(); return Constraint::between($dt_min, $dt_max); } public function getDisplayYearCycle(): string { [$date_start, $date_end] = $this->getYearCycleDates(); $dt_start = CarbonImmutable::parse($date_start); $dt_end = CarbonImmutable::parse($date_end); return sprintf("%s - %s", $dt_start->format("d/m/Y"), $dt_end->format("d/m/Y")); } public function getLogoPath(): ?string { $slug = Util::slug($this->name); $slug = str_replace("-", "_", $slug); $path = sprintf("public/img/customer_logos/%s_logo.png", $slug); if(file_exists(ROOT_DIR . "/" . $path)) return $path; return null; } /** * @return Test[] * @throws Exception */ public function getLatestTestByLocation(): array { $output = []; $tests = Test::query([ "status" => Constraint::notEquals(Test::STATUS_CANCELLED), "location_id" => array_column($this->getLocations(), "id") ], ["sort" => "dts_start", "order" => "asc"]); foreach($tests as $test) { $output[$test->location_id] = $test; } return $output; } }