1
0

customer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 getDisplayYearCycle(): string {
  124. [$date_start, $date_end] = $this->getYearCycleDates();
  125. $dt_start = CarbonImmutable::parse($date_start);
  126. $dt_end = CarbonImmutable::parse($date_end);
  127. return sprintf("%s - %s", $dt_start->format("d/m/Y"), $dt_end->format("d/m/Y"));
  128. }
  129. public function getLogoPath(): ?string {
  130. $slug = Util::slug($this->name);
  131. $slug = str_replace("-", "_", $slug);
  132. $path = sprintf("public/img/customer_logos/%s_logo.png", $slug);
  133. if(file_exists(ROOT_DIR . "/" . $path)) return $path;
  134. return null;
  135. }
  136. }