1
0

customer.php 6.2 KB

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