customer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. */
  20. class Customer extends Model {
  21. protected static $table = "customer";
  22. protected static $id_field = "id";
  23. protected static $fillable_fields = ["name", "address_1", "address_2",
  24. "city", "county", "postal_code", "country", "email", "phone",
  25. "invoice_contact_name",
  26. "email_after_test", "email_locations_after_test",
  27. "internal_id_field_name", "region_field_name", "area_manager_field_name",
  28. ];
  29. // Static methods
  30. /**
  31. * @return Customer
  32. * @throws Exception
  33. */
  34. public static function getForAdmin(): ?Customer {
  35. $customers = Core::$user->getCustomers();
  36. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  37. else if(count($customers) == 1) {
  38. $customer = array_shift($customers);
  39. }
  40. else {
  41. $customer_id = http::value("customer_id", "int");
  42. if(!$customer_id) {
  43. $page_title = "Select Customer";
  44. View::includeOnce("header", compact("page_title"));
  45. View::include("customer_select", compact("customers"));
  46. View::includeOnce("footer");
  47. exit;
  48. }
  49. $customer = Customer::get($customer_id);
  50. // Enforce restriction if CUSTOMER or lower
  51. if(Core::$user->level < User::LEVEL_ADMIN) {
  52. $link = UserCustomer::get(Core::$user->id, $customer_id);
  53. if(!$link) $customer = null;
  54. }
  55. }
  56. if(empty($customer)) throw new Exception("Customer not found.");
  57. return $customer;
  58. }
  59. // Object methods
  60. /**
  61. * @param array $query
  62. * @return Location[]
  63. * @throws Exception
  64. */
  65. public function getLocations(array $query = []): array {
  66. $nvp = array_merge(["customer_id" => $this->id], $query);
  67. return Location::query($nvp, ["sort" => "name"]);
  68. }
  69. /**
  70. * @return Project[]
  71. * @throws Exception
  72. */
  73. public function getProjects() {
  74. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  75. }
  76. /**
  77. * @return Template[]
  78. * @throws Exception
  79. */
  80. public function getTemplates() {
  81. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  82. }
  83. /**
  84. * @return string
  85. */
  86. public function getTestResultsUrl(): string {
  87. $slug = Util::slugWithId($this->name, $this->id);
  88. $url = "/account/test-results/{$slug}";
  89. return $url;
  90. }
  91. }