customer.php 8.8 KB

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