customer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @property int $id
  4. * @property string $name
  5. * @property string $address_1
  6. * @property string $address_2
  7. * @property string $city
  8. * @property string $county
  9. * @property string $postal_code
  10. * @property string $country
  11. * @property string $email
  12. * @property string $phone
  13. * @property string $invoice_contact_name
  14. * @property bool $email_after_test
  15. * @property bool $email_locations_after_test
  16. * @property string $internal_id_field_name
  17. * @property string $region_field_name
  18. * @property string $area_manager_field_name
  19. * @property int $year_cycle_month
  20. * @property int $show_in_map_overview
  21. */
  22. class Customer extends Model {
  23. protected static $table = "customer";
  24. protected static $id_field = "id";
  25. protected static $fillable_fields = ["name", "address_1", "address_2",
  26. "city", "county", "postal_code", "country", "email", "phone",
  27. "invoice_contact_name",
  28. "email_after_test", "email_locations_after_test",
  29. "internal_id_field_name", "region_field_name", "area_manager_field_name",
  30. "year_cycle_month",
  31. "show_in_map_overview",
  32. ];
  33. // Static methods
  34. /**
  35. * @return Customer
  36. * @throws Exception
  37. */
  38. public static function getForAdmin(): ?Customer {
  39. $user = Core::$user;
  40. $customers = $user->getCustomers();
  41. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  42. else if(count($customers) == 1) {
  43. $customer = array_shift($customers);
  44. }
  45. else {
  46. $customer_id = http::value("customer_id", "int");
  47. if(!$customer_id) {
  48. $page_title = "Select Customer";
  49. View::includeOnce("header", compact("page_title"));
  50. View::include("customer_select", compact("customers"));
  51. View::includeOnce("footer");
  52. exit;
  53. }
  54. $customer = Customer::get($customer_id);
  55. // Enforce restriction if user cannot access locations
  56. if(!$user->canAccessLocations()) {
  57. $link = UserCustomer::get(Core::$user->id, $customer_id);
  58. if(!$link) $customer = null;
  59. }
  60. }
  61. if(empty($customer)) throw new Exception("Customer not found.");
  62. return $customer;
  63. }
  64. // Object methods
  65. /**
  66. * @param array $query
  67. * @return Location[]
  68. * @throws Exception
  69. */
  70. public function getLocations(array $query = []): array {
  71. $nvp = array_merge(["customer_id" => $this->id], $query);
  72. return Location::query($nvp, ["sort" => "name"]);
  73. }
  74. /**
  75. * @return Project[]
  76. * @throws Exception
  77. */
  78. public function getProjects() {
  79. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  80. }
  81. public function getFinishedProjects() {
  82. $projects = array_filter($this->getProjects(), function(Project $p) {
  83. return $p->isFinished();
  84. });
  85. return array_values($projects);
  86. }
  87. /**
  88. * @return Template[]
  89. * @throws Exception
  90. */
  91. public function getTemplates() {
  92. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getTestResultsUrl(): string {
  98. $slug = Util::slugWithId($this->name, $this->id);
  99. $url = "/account/test-results/{$slug}";
  100. return $url;
  101. }
  102. public function getYearCycleDates(): array {
  103. $month = $this->year_cycle_month;
  104. if(!$month) $month = 1;
  105. $current_year = date("Y");
  106. $current_month = date("n");
  107. if($current_month >= $month) {
  108. $min_dts = sprintf("%d-%02d-01", $current_year, $month);
  109. $max_dts = sprintf("%d-%02d-01", $current_year + 1, $month);
  110. }
  111. else {
  112. $min_dts = sprintf("%d-%02d-01", $current_year - 1, $month);
  113. $max_dts = sprintf("%d-%02d-01", $current_year, $month);
  114. }
  115. $min_time = strtotime($min_dts);
  116. $max_time = strtotime($max_dts) - 1;
  117. return [
  118. date("Y-m-d", $min_time),
  119. date("Y-m-d", $max_time),
  120. ];
  121. }
  122. public function getLogoPath(): ?string {
  123. $slug = Util::slug($this->name);
  124. $slug = str_replace("-", "_", $slug);
  125. $path = sprintf("public/img/customer_logos/%s_logo.png", $slug);
  126. if(file_exists(ROOT_DIR . "/" . $path)) return $path;
  127. return null;
  128. }
  129. }