customer.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. use Carbon\CarbonImmutable;
  3. /**
  4. * @property int $id
  5. * @property string $name
  6. * @property string $address_1
  7. * @property string $address_2
  8. * @property string $city
  9. * @property string $county
  10. * @property string $postal_code
  11. * @property string $country
  12. * @property string $email
  13. * @property string $cc_email_json
  14. * @property string $phone
  15. * @property string $invoice_contact_name
  16. * @property bool $email_after_test
  17. * @property bool $email_locations_after_test
  18. * @property string $internal_id_field_name
  19. * @property string $region_field_name
  20. * @property string $area_manager_field_name
  21. * @property int $year_cycle_month
  22. * @property int $show_in_map_overview
  23. * @property string $config_json
  24. */
  25. class Customer extends Model {
  26. protected static $table = "customer";
  27. protected static $id_field = "id";
  28. protected static $fillable_fields = ["name", "address_1", "address_2",
  29. "city", "county", "postal_code", "country", "email", "cc_email_json", "phone",
  30. "invoice_contact_name",
  31. "email_after_test", "email_locations_after_test",
  32. "internal_id_field_name", "region_field_name", "area_manager_field_name",
  33. "year_cycle_month",
  34. "show_in_map_overview",
  35. "config_json",
  36. ];
  37. // Static methods
  38. /**
  39. * @return Customer
  40. * @throws Exception
  41. */
  42. public static function getForAdmin(): ?Customer {
  43. $user = Core::$user;
  44. $customers = $user->getCustomers();
  45. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  46. else if(count($customers) == 1) {
  47. $customer = array_shift($customers);
  48. }
  49. else {
  50. $customer_id = http::value("customer_id", "int");
  51. if(!$customer_id) {
  52. $page_title = "Select Customer";
  53. View::includeOnce("header", compact("page_title"));
  54. View::include("customer_select", compact("customers"));
  55. View::includeOnce("footer");
  56. exit;
  57. }
  58. $customer = Customer::get($customer_id);
  59. // Enforce restriction if user cannot access locations
  60. if(!$user->canAccessLocations()) {
  61. $link = UserCustomer::get(Core::$user->id, $customer_id);
  62. if(!$link) $customer = null;
  63. }
  64. }
  65. if(empty($customer)) throw new Exception("Customer not found.");
  66. return $customer;
  67. }
  68. // Object methods
  69. /**
  70. * @param array $query
  71. * @return Location[]
  72. * @throws Exception
  73. */
  74. public function getLocations(array $query = []): array {
  75. $nvp = array_merge(["customer_id" => $this->id], $query);
  76. return Location::query($nvp, ["sort" => "name"]);
  77. }
  78. /**
  79. * @return Project[]
  80. * @throws Exception
  81. */
  82. public function getProjects(array $nvp = []): array {
  83. $nvp = array_merge($nvp, ["customer_id" => $this->id]);
  84. return Project::query($nvp, [
  85. "sort" => "dts_deadline",
  86. "order" => "desc"
  87. ]);
  88. }
  89. public function getFinishedProjects() {
  90. $projects = array_filter($this->getProjects(), function(Project $p) {
  91. return $p->isFinished();
  92. });
  93. return array_values($projects);
  94. }
  95. /**
  96. * @return Template[]
  97. * @throws Exception
  98. */
  99. public function getTemplates() {
  100. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  101. }
  102. /**
  103. * @return string
  104. */
  105. public function getTestResultsUrl(): string {
  106. $slug = Util::slugWithId($this->name, $this->id);
  107. $url = "/account/test-results/{$slug}";
  108. return $url;
  109. }
  110. public function getYearCycleDates(): array {
  111. $month = $this->year_cycle_month;
  112. if(!$month) $month = 1;
  113. $current_year = date("Y");
  114. $current_month = date("n");
  115. if($current_month >= $month) {
  116. $min_dts = sprintf("%d-%02d-01", $current_year, $month);
  117. $max_dts = sprintf("%d-%02d-01", $current_year + 1, $month);
  118. }
  119. else {
  120. $min_dts = sprintf("%d-%02d-01", $current_year - 1, $month);
  121. $max_dts = sprintf("%d-%02d-01", $current_year, $month);
  122. }
  123. $min_time = strtotime($min_dts);
  124. $max_time = strtotime($max_dts) - 1;
  125. return [
  126. date("Y-m-d", $min_time),
  127. date("Y-m-d", $max_time),
  128. ];
  129. }
  130. public function getYearCycleConstraint(string $tz): Constraint {
  131. [$min_date, $max_date] = $this->getYearCycleDates();
  132. $dt_min = CarbonImmutable::parse($min_date, $tz)->startOfDay()->utc();
  133. $dt_max = CarbonImmutable::parse($max_date, $tz)->endOfDay()->utc();
  134. return Constraint::between($dt_min, $dt_max);
  135. }
  136. public function getDisplayYearCycle(): string {
  137. [$date_start, $date_end] = $this->getYearCycleDates();
  138. $dt_start = CarbonImmutable::parse($date_start);
  139. $dt_end = CarbonImmutable::parse($date_end);
  140. return sprintf("%s - %s", $dt_start->format("d/m/Y"), $dt_end->format("d/m/Y"));
  141. }
  142. public function getLogoPath(): ?string {
  143. $slug = Util::slug($this->name);
  144. $slug = str_replace("-", "_", $slug);
  145. $path = sprintf("public/img/customer_logos/%s_logo.png", $slug);
  146. if(file_exists(ROOT_DIR . "/" . $path)) return $path;
  147. return null;
  148. }
  149. /**
  150. * @return Test[]
  151. * @throws Exception
  152. */
  153. public function getLatestTestByLocation(): array {
  154. $output = [];
  155. $tests = Test::query([
  156. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  157. "location_id" => array_column($this->getLocations(), "id")
  158. ], ["sort" => "dts_start", "order" => "asc"]);
  159. foreach($tests as $test) {
  160. $output[$test->location_id] = $test;
  161. }
  162. return $output;
  163. }
  164. /**
  165. * @param string $field
  166. * @return string[]
  167. * @throws Exception
  168. */
  169. public function getLocationFieldValues(string $field): array {
  170. $names = [];
  171. foreach($this->getLocations() as $location) {
  172. $names[] = $location->$field;
  173. }
  174. $names = array_values(array_unique(array_filter($names)));
  175. usort($names, Util::getStringSorter());
  176. return $names;
  177. }
  178. /**
  179. * @return string[]
  180. * @throws Exception
  181. */
  182. public function getBrands(): array {
  183. return $this->getLocationFieldValues("brand");
  184. }
  185. /**
  186. * @return string[]
  187. * @throws Exception
  188. */
  189. public function getRegions(): array {
  190. return $this->getLocationFieldValues("region");
  191. }
  192. /**
  193. * @return string[]
  194. * @throws Exception
  195. */
  196. public function getAreaManagerNames(): array {
  197. return $this->getLocationFieldValues("area_manager");
  198. }
  199. /**
  200. * @return string[]
  201. * @throws Exception
  202. */
  203. public function getGamingManagerNames(): array {
  204. return $this->getLocationFieldValues("gaming_manager_name");
  205. }
  206. /**
  207. * @return string[]
  208. * @throws Exception
  209. */
  210. public function getSeniorGamingManagerNames(): array {
  211. return $this->getLocationFieldValues("senior_gaming_manager_name");
  212. }
  213. /**
  214. * @return string[]
  215. * @throws Exception
  216. */
  217. public function getOperationsManagerNames(): array {
  218. return $this->getLocationFieldValues("operations_manager_name");
  219. }
  220. public function getConfig(): object {
  221. return json_decode($this->config_json) ?: new stdClass();
  222. }
  223. public function getBrandLabel(int $count): string {
  224. $default_labels = [__("Brand"), __("Brands")];
  225. $index = ($count === 1) ? 0 : 1;
  226. return $this->getConfig()->brandLabels[$index] ?? $default_labels[$index];
  227. }
  228. public function getRegionLabel(int $count): string {
  229. $default_labels = [__("Region"), __("Regions")];
  230. $index = ($count === 1) ? 0 : 1;
  231. return $this->getConfig()->regionLabels[$index] ?? $default_labels[$index];
  232. }
  233. public function getAreaManagerLabel(int $count): string {
  234. $default_labels = [__("Area Manager"), __("Area Managers")];
  235. $index = ($count === 1) ? 0 : 1;
  236. return $this->getConfig()->areaManagerLabels[$index] ?? $default_labels[$index];
  237. }
  238. public function getOperationsManagerLabel(int $count): string {
  239. $default_labels = [__("Operations Manager"), __("Operations Managers")];
  240. $index = ($count === 1) ? 0 : 1;
  241. return $this->getConfig()->operationsManagerLabels[$index] ?? $default_labels[$index];
  242. }
  243. public function getGamingManagerLabel(int $count): string {
  244. $default_labels = [__("Gaming Manager"), __("Gaming Managers")];
  245. $index = ($count === 1) ? 0 : 1;
  246. return $this->getConfig()->gamingManagerLabels[$index] ?? $default_labels[$index];
  247. }
  248. public function getSeniorGamingManagerLabel(int $count): string {
  249. $default_labels = [__("Senior Gaming Manager"), __("Senior Gaming Managers")];
  250. $index = ($count === 1) ? 0 : 1;
  251. return $this->getConfig()->seniorGamingManagerLabels[$index] ?? $default_labels[$index];
  252. }
  253. public function getCcEmailAddresses(): array {
  254. $addresses = json_decode($this->cc_email_json);
  255. return is_array($addresses) ? $addresses : [];
  256. }
  257. public function setCcEmailAddresses(array $addresses): void {
  258. foreach($addresses as $address) {
  259. $valid = filter_var($address, FILTER_VALIDATE_EMAIL);
  260. if(!$valid) {
  261. throw new RuntimeException(__("%s is not a valid email address.", $address));
  262. }
  263. }
  264. $this->cc_email_json = json_encode($addresses);
  265. }
  266. public function isBillingEnabled(): bool {
  267. $config = $this->getConfig();
  268. return !empty($config->billingEnabled);
  269. }
  270. }