customer.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * @method Customer get(int $id)
  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. ];
  31. // Static methods
  32. public static function getForAdmin() {
  33. $customers = Core::$user->getCustomers();
  34. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  35. else if(count($customers) == 1) {
  36. $customer = array_shift($customers);
  37. }
  38. else {
  39. $customer_id = http::value("customer_id", "int");
  40. if(!$customer_id) {
  41. $page_title = "Select Customer";
  42. View::includeOnce("header", compact("page_title"));
  43. View::include("customer_select", compact("customers"));
  44. View::includeOnce("footer");
  45. exit;
  46. }
  47. $customer = Customer::get($customer_id);
  48. // Enforce restriction if CUSTOMER or lower
  49. if(Core::$user->level < User::LEVEL_ADMIN) {
  50. $link = UserCustomer::get(Core::$user->id, $customer_id);
  51. if(!$link) $customer = false;
  52. }
  53. }
  54. if(empty($customer)) throw new Exception("Customer not found.");
  55. return $customer;
  56. }
  57. // Object methods
  58. public function getLocations($query = []) {
  59. $nvp = array_merge(["customer_id" => $this->id], $query);
  60. return Location::query($nvp, ["sort" => "name"]);
  61. }
  62. /**
  63. * @return Project[]
  64. */
  65. public function getProjects() {
  66. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  67. }
  68. /**
  69. * @return Template[]
  70. */
  71. public function getTemplates() {
  72. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  73. }
  74. }