| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- /**
- * @property int $id
- * @property string $name
- * @property string $address_1
- * @property string $address_2
- * @property string $city
- * @property string $county
- * @property string $postal_code
- * @property string $country
- * @property string $email
- * @property string $phone
- * @property string $invoice_contact_name
- * @property bool $email_after_test
- * @property bool $email_locations_after_test
- * @property string $internal_id_field_name
- * @property string $region_field_name
- * @property string $area_manager_field_name
- *
- * @method Customer get(int $id)
- */
- class Customer extends Model {
- protected static $table = "customer";
- protected static $id_field = "id";
- protected static $fillable_fields = ["name", "address_1", "address_2",
- "city", "county", "postal_code", "country", "email", "phone",
- "invoice_contact_name",
- "email_after_test", "email_locations_after_test",
- "internal_id_field_name", "region_field_name", "area_manager_field_name",
- ];
- // Static methods
- public static function getForAdmin() {
- $customers = Core::$user->getCustomers();
- if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
- else if(count($customers) == 1) {
- $customer = array_shift($customers);
- }
- else {
- $customer_id = http::value("customer_id", "int");
- if(!$customer_id) {
- $page_title = "Select Customer";
- View::includeOnce("header", compact("page_title"));
- View::include("customer_select", compact("customers"));
- View::includeOnce("footer");
- exit;
- }
- $customer = Customer::get($customer_id);
- // Enforce restriction if CUSTOMER or lower
- if(Core::$user->level < User::LEVEL_ADMIN) {
- $link = UserCustomer::get(Core::$user->id, $customer_id);
- if(!$link) $customer = false;
- }
- }
- if(empty($customer)) throw new Exception("Customer not found.");
- return $customer;
- }
- // Object methods
- public function getLocations($query = []) {
- $nvp = array_merge(["customer_id" => $this->id], $query);
- return Location::query($nvp, ["sort" => "name"]);
- }
- /**
- * @return Project[]
- */
- public function getProjects() {
- return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
- }
- /**
- * @return Template[]
- */
- public function getTemplates() {
- return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
- }
- public function getTestResultsUrl(): string {
- $slug = Util::slugWithId($this->name, $this->id);
- $url = "/account/test-results/{$slug}";
- return $url;
- }
- }
|