1
0

customer.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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() {
  83. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  84. }
  85. public function getFinishedProjects() {
  86. $projects = array_filter($this->getProjects(), function(Project $p) {
  87. return $p->isFinished();
  88. });
  89. return array_values($projects);
  90. }
  91. /**
  92. * @return Template[]
  93. * @throws Exception
  94. */
  95. public function getTemplates() {
  96. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  97. }
  98. /**
  99. * @return string
  100. */
  101. public function getTestResultsUrl(): string {
  102. $slug = Util::slugWithId($this->name, $this->id);
  103. $url = "/account/test-results/{$slug}";
  104. return $url;
  105. }
  106. public function getYearCycleDates(): array {
  107. $month = $this->year_cycle_month;
  108. if(!$month) $month = 1;
  109. $current_year = date("Y");
  110. $current_month = date("n");
  111. if($current_month >= $month) {
  112. $min_dts = sprintf("%d-%02d-01", $current_year, $month);
  113. $max_dts = sprintf("%d-%02d-01", $current_year + 1, $month);
  114. }
  115. else {
  116. $min_dts = sprintf("%d-%02d-01", $current_year - 1, $month);
  117. $max_dts = sprintf("%d-%02d-01", $current_year, $month);
  118. }
  119. $min_time = strtotime($min_dts);
  120. $max_time = strtotime($max_dts) - 1;
  121. return [
  122. date("Y-m-d", $min_time),
  123. date("Y-m-d", $max_time),
  124. ];
  125. }
  126. public function getYearCycleConstraint(string $tz): Constraint {
  127. [$min_date, $max_date] = $this->getYearCycleDates();
  128. $dt_min = CarbonImmutable::parse($min_date, $tz)->startOfDay()->utc();
  129. $dt_max = CarbonImmutable::parse($max_date, $tz)->endOfDay()->utc();
  130. return Constraint::between($dt_min, $dt_max);
  131. }
  132. public function getDisplayYearCycle(): string {
  133. [$date_start, $date_end] = $this->getYearCycleDates();
  134. $dt_start = CarbonImmutable::parse($date_start);
  135. $dt_end = CarbonImmutable::parse($date_end);
  136. return sprintf("%s - %s", $dt_start->format("d/m/Y"), $dt_end->format("d/m/Y"));
  137. }
  138. public function getLogoPath(): ?string {
  139. $slug = Util::slug($this->name);
  140. $slug = str_replace("-", "_", $slug);
  141. $path = sprintf("public/img/customer_logos/%s_logo.png", $slug);
  142. if(file_exists(ROOT_DIR . "/" . $path)) return $path;
  143. return null;
  144. }
  145. /**
  146. * @return Test[]
  147. * @throws Exception
  148. */
  149. public function getLatestTestByLocation(): array {
  150. $output = [];
  151. $tests = Test::query([
  152. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  153. "location_id" => array_column($this->getLocations(), "id")
  154. ], ["sort" => "dts_start", "order" => "asc"]);
  155. foreach($tests as $test) {
  156. $output[$test->location_id] = $test;
  157. }
  158. return $output;
  159. }
  160. /**
  161. * @param string $field
  162. * @return string[]
  163. * @throws Exception
  164. */
  165. public function getLocationFieldValues(string $field): array {
  166. $names = [];
  167. foreach($this->getLocations() as $location) {
  168. $names[] = $location->$field;
  169. }
  170. $names = array_values(array_unique(array_filter($names)));
  171. usort($names, Util::getStringSorter());
  172. return $names;
  173. }
  174. /**
  175. * @return string[]
  176. * @throws Exception
  177. */
  178. public function getBrands(): array {
  179. return $this->getLocationFieldValues("brand");
  180. }
  181. /**
  182. * @return string[]
  183. * @throws Exception
  184. */
  185. public function getRegions(): array {
  186. return $this->getLocationFieldValues("region");
  187. }
  188. /**
  189. * @return string[]
  190. * @throws Exception
  191. */
  192. public function getAreaManagerNames(): array {
  193. return $this->getLocationFieldValues("area_manager");
  194. }
  195. /**
  196. * @return string[]
  197. * @throws Exception
  198. */
  199. public function getGamingManagerNames(): array {
  200. return $this->getLocationFieldValues("gaming_manager_name");
  201. }
  202. /**
  203. * @return string[]
  204. * @throws Exception
  205. */
  206. public function getSeniorGamingManagerNames(): array {
  207. return $this->getLocationFieldValues("senior_gaming_manager_name");
  208. }
  209. /**
  210. * @return string[]
  211. * @throws Exception
  212. */
  213. public function getOperationsManagerNames(): array {
  214. return $this->getLocationFieldValues("operations_manager_name");
  215. }
  216. public function getConfig(): object {
  217. return json_decode($this->config_json) ?: new stdClass();
  218. }
  219. public function getBrandLabel(int $count): string {
  220. $default_labels = [__("Brand"), __("Brands")];
  221. $index = ($count === 1) ? 0 : 1;
  222. return $this->getConfig()->brandLabels[$index] ?? $default_labels[$index];
  223. }
  224. public function getRegionLabel(int $count): string {
  225. $default_labels = [__("Region"), __("Regions")];
  226. $index = ($count === 1) ? 0 : 1;
  227. return $this->getConfig()->regionLabels[$index] ?? $default_labels[$index];
  228. }
  229. public function getAreaManagerLabel(int $count): string {
  230. $default_labels = [__("Area Manager"), __("Area Managers")];
  231. $index = ($count === 1) ? 0 : 1;
  232. return $this->getConfig()->areaManagerLabels[$index] ?? $default_labels[$index];
  233. }
  234. public function getOperationsManagerLabel(int $count): string {
  235. $default_labels = [__("Operations Manager"), __("Operations Managers")];
  236. $index = ($count === 1) ? 0 : 1;
  237. return $this->getConfig()->operationsManagerLabels[$index] ?? $default_labels[$index];
  238. }
  239. public function getGamingManagerLabel(int $count): string {
  240. $default_labels = [__("Gaming Manager"), __("Gaming Managers")];
  241. $index = ($count === 1) ? 0 : 1;
  242. return $this->getConfig()->gamingManagerLabels[$index] ?? $default_labels[$index];
  243. }
  244. public function getSeniorGamingManagerLabel(int $count): string {
  245. $default_labels = [__("Senior Gaming Manager"), __("Senior Gaming Managers")];
  246. $index = ($count === 1) ? 0 : 1;
  247. return $this->getConfig()->seniorGamingManagerLabels[$index] ?? $default_labels[$index];
  248. }
  249. public function getCcEmailAddresses(): array {
  250. $addresses = json_decode($this->cc_email_json);
  251. return is_array($addresses) ? $addresses : [];
  252. }
  253. public function setCcEmailAddresses(array $addresses): void {
  254. foreach($addresses as $address) {
  255. $valid = filter_var($address, FILTER_VALIDATE_EMAIL);
  256. if(!$valid) {
  257. throw new RuntimeException(__("%s is not a valid email address.", $address));
  258. }
  259. }
  260. $this->cc_email_json = json_encode($addresses);
  261. }
  262. }