customer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $customers = Core::$user->getCustomers();
  38. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  39. else if(count($customers) == 1) {
  40. $customer = array_shift($customers);
  41. }
  42. else {
  43. $customer_id = http::value("customer_id", "int");
  44. if(!$customer_id) {
  45. $page_title = "Select Customer";
  46. View::includeOnce("header", compact("page_title"));
  47. View::include("customer_select", compact("customers"));
  48. View::includeOnce("footer");
  49. exit;
  50. }
  51. $customer = Customer::get($customer_id);
  52. // Enforce restriction if CUSTOMER or lower
  53. if(Core::$user->level < User::LEVEL_ADMIN) {
  54. $link = UserCustomer::get(Core::$user->id, $customer_id);
  55. if(!$link) $customer = null;
  56. }
  57. }
  58. if(empty($customer)) throw new Exception("Customer not found.");
  59. return $customer;
  60. }
  61. // Object methods
  62. /**
  63. * @param array $query
  64. * @return Location[]
  65. * @throws Exception
  66. */
  67. public function getLocations(array $query = []): array {
  68. $nvp = array_merge(["customer_id" => $this->id], $query);
  69. return Location::query($nvp, ["sort" => "name"]);
  70. }
  71. /**
  72. * @return Project[]
  73. * @throws Exception
  74. */
  75. public function getProjects() {
  76. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  77. }
  78. /**
  79. * @return Template[]
  80. * @throws Exception
  81. */
  82. public function getTemplates() {
  83. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getTestResultsUrl(): string {
  89. $slug = Util::slugWithId($this->name, $this->id);
  90. $url = "/account/test-results/{$slug}";
  91. return $url;
  92. }
  93. public function getYearCycleDates(): array {
  94. $month = $this->year_cycle_month;
  95. if(!$month) $month = 1;
  96. $current_year = date("Y");
  97. $current_month = date("n");
  98. if($current_month >= $month) {
  99. $min_dts = sprintf("%d-%02d-01", $current_year, $month);
  100. $max_dts = sprintf("%d-%02d-01", $current_year + 1, $month);
  101. }
  102. else {
  103. $min_dts = sprintf("%d-%02d-01", $current_year - 1, $month);
  104. $max_dts = sprintf("%d-%02d-01", $current_year, $month);
  105. }
  106. return [$min_dts, $max_dts];
  107. }
  108. public function getLogoPath(): ?string {
  109. $slug = Util::slug($this->name);
  110. $path = sprintf("public/img/customer_logos/%s_logo.png", $slug);
  111. if(file_exists(ROOT_DIR . "/" . $path)) return $path;
  112. return null;
  113. }
  114. }