customer.php 3.8 KB

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