1
0

customer.php 7.8 KB

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