| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- use Carbon\CarbonImmutable;
- /**
- * @property int $id
- * @property string $name
- * @property string $address_1
- * @property string $address_2
- * @property string $city
- * @property string $county
- * @property string $postal_code
- * @property string $country
- * @property string $email
- * @property string $cc_email_json
- * @property string $phone
- * @property string $invoice_contact_name
- * @property bool $email_after_test
- * @property bool $email_locations_after_test
- * @property string $internal_id_field_name
- * @property string $region_field_name
- * @property string $area_manager_field_name
- * @property int $year_cycle_month
- * @property int $show_in_map_overview
- * @property string $config_json
- */
- class Customer extends Model {
- protected static $table = "customer";
- protected static $id_field = "id";
- protected static $fillable_fields = ["name", "address_1", "address_2",
- "city", "county", "postal_code", "country", "email", "cc_email_json", "phone",
- "invoice_contact_name",
- "email_after_test", "email_locations_after_test",
- "internal_id_field_name", "region_field_name", "area_manager_field_name",
- "year_cycle_month",
- "show_in_map_overview",
- "config_json",
- ];
- // Static methods
- /**
- * @return Customer
- * @throws Exception
- */
- public static function getForAdmin(): ?Customer {
- $user = Core::$user;
- $customers = $user->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;
- }
- /**
- * @param string $field
- * @return string[]
- * @throws Exception
- */
- public function getLocationFieldValues(string $field): array {
- $names = [];
- foreach($this->getLocations() as $location) {
- $names[] = $location->$field;
- }
- $names = array_values(array_unique(array_filter($names)));
- usort($names, Util::getStringSorter());
- return $names;
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getBrands(): array {
- return $this->getLocationFieldValues("brand");
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getRegions(): array {
- return $this->getLocationFieldValues("region");
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getAreaManagerNames(): array {
- return $this->getLocationFieldValues("area_manager");
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getGamingManagerNames(): array {
- return $this->getLocationFieldValues("gaming_manager_name");
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getSeniorGamingManagerNames(): array {
- return $this->getLocationFieldValues("senior_gaming_manager_name");
- }
- /**
- * @return string[]
- * @throws Exception
- */
- public function getOperationsManagerNames(): array {
- return $this->getLocationFieldValues("operations_manager_name");
- }
- public function getConfig(): object {
- return json_decode($this->config_json) ?: new stdClass();
- }
- public function getBrandLabel(int $count): string {
- $default_labels = [__("Brand"), __("Brands")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->brandLabels[$index] ?? $default_labels[$index];
- }
- public function getRegionLabel(int $count): string {
- $default_labels = [__("Region"), __("Regions")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->regionLabels[$index] ?? $default_labels[$index];
- }
- public function getAreaManagerLabel(int $count): string {
- $default_labels = [__("Area Manager"), __("Area Managers")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->areaManagerLabels[$index] ?? $default_labels[$index];
- }
- public function getOperationsManagerLabel(int $count): string {
- $default_labels = [__("Operations Manager"), __("Operations Managers")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->operationsManagerLabels[$index] ?? $default_labels[$index];
- }
- public function getGamingManagerLabel(int $count): string {
- $default_labels = [__("Gaming Manager"), __("Gaming Managers")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->gamingManagerLabels[$index] ?? $default_labels[$index];
- }
- public function getSeniorGamingManagerLabel(int $count): string {
- $default_labels = [__("Senior Gaming Manager"), __("Senior Gaming Managers")];
- $index = ($count === 1) ? 0 : 1;
- return $this->getConfig()->seniorGamingManagerLabels[$index] ?? $default_labels[$index];
- }
- public function getCcEmailAddresses(): array {
- $addresses = json_decode($this->cc_email_json);
- return is_array($addresses) ? $addresses : [];
- }
- public function setCcEmailAddresses(array $addresses): void {
- foreach($addresses as $address) {
- $valid = filter_var($address, FILTER_VALIDATE_EMAIL);
- if(!$valid) {
- throw new RuntimeException(__("%s is not a valid email address.", $address));
- }
- }
- $this->cc_email_json = json_encode($addresses);
- }
- }
|