| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * @property int $id
- * @property int $customer_id
- * @property string $stripe_customer_id
- * @property string $name
- * @property string $legal_entity_name
- * @property string $address_1
- * @property string $address_2
- * @property string $city
- * @property string $county
- * @property string $postal_code
- * @property string $state
- * @property string $country
- * @property string $email
- * @property string $phone
- * @property string $opening_hours
- * @property string $notes
- * @property string $shop_left
- * @property string $shop_right
- * @property string $brand
- * @property string $region
- * @property string $region_name
- * @property string $internal_id
- * @property string $area_manager
- * @property string $area_manager_name
- * @property string $area_manager_email
- * @property string $operations_manager_name
- * @property string $operations_manager_email
- * @property string $gaming_manager_name
- * @property string $gaming_manager_email
- * @property string $senior_gaming_manager_name
- * @property string $senior_gaming_manager_email
- * @property int $machine_count
- * @property int $annual_test_count
- * @property bool $do_not_test
- * @property bool $closed
- * @property bool $archived
- * @property double $latitude
- * @property double $longitude
- * @property string $dts_verified
- */
- class Location extends Model {
- protected static $table = "location";
- protected static $id_field = "id";
- protected static $fillable_fields = [
- "customer_id", "stripe_customer_id",
- "name", "legal_entity_name",
- "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone",
- "opening_hours", "notes",
- "shop_left", "shop_right",
- "brand",
- "region", "region_name",
- "internal_id",
- "area_manager", "area_manager_name", "area_manager_email",
- "operations_manager_name", "operations_manager_email",
- "gaming_manager_name", "gaming_manager_email",
- "senior_gaming_manager_name", "senior_gaming_manager_email",
- "machine_count", "annual_test_count",
- "do_not_test", "closed", "archived",
- "latitude", "longitude",
- "dts_verified"
- ];
- // Object methods
- /**
- * @param array $types
- * @return bool
- * @throws Exception
- */
- public function assignVenueTypes(array $types): bool {
- $links = LocationVenueType::query(["location_id" => $this->id]);
- foreach($links as $link) {
- $link->delete();
- }
- foreach($types as $type) {
- $link = new LocationVenueType;
- $link->location_id = $this->id;
- $link->venue_type_id = $type->id;
- $link->commit();
- }
- return true;
- }
- /**
- * @return VenueType[]
- * @throws Exception
- */
- public function getVenueTypes(): array {
- $links = LocationVenueType::query(["location_id" => $this->id]);
- $ids = array_column($links, "venue_type_id");
- return VenueType::query(["id" => $ids]);
- }
- /**
- * @return Customer
- * @throws Exception
- */
- public function getCustomer(): Customer {
- return Customer::get($this->customer_id);
- }
- public function getDirectory() {
- $directory = ROOT_DIR . "/files/locations/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
- $parts = [];
- foreach($fields as $field) {
- $parts[] = $this->$field;
- }
- return implode($glue, array_filter($parts));
- }
- public function getGeolocation() {
- return [
- doubleval($this->latitude),
- doubleval($this->longitude)
- ];
- }
- /**
- * @return LocationFile[]
- * @throws Exception
- */
- public function getPhotos() {
- return LocationFile::query([
- "location_id" => $this->id,
- "type" => LocationFile::TYPE_PHOTO
- ]);
- }
- /**
- * @return Test[]
- * @throws Exception
- */
- public function getTests(array $nvp = []): array {
- $nvp = array_merge($nvp, [
- "location_id" => $this->id,
- "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
- ]);
- return Test::query($nvp, [
- "sort" => "dts_start",
- "order" => "desc",
- ]);
- }
- /**
- * @return Test[]
- * @throws Exception
- */
- public function getCompletedTests(array $nvp = []): array {
- $nvp = array_merge($nvp, [
- "location_id" => $this->id,
- "status" => Test::STATUS_COMPLETED,
- ]);
- return Test::query($nvp, [
- "sort" => "dts_start",
- "order" => "desc",
- ]);
- }
- /**
- * @param object $old_data
- * @param array $modified_fields
- * @return bool
- * @throws Exception
- */
- public function logUpdate(object $old_data, array $modified_fields): bool {
- if(empty($modified_fields)) return true;
- $entry = new LocationUpdateLogEntry;
- $entry->location_id = $this->id;
- $entry->user_id = Core::$user->id;
- $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
- $entry->dts = Util::getDateTime();
- return $entry->commit();
- }
- /**
- * @return LocationUpdateLogEntry[]
- * @throws Exception
- */
- public function getUpdateLogEntries() {
- return LocationUpdateLogEntry::query([
- "location_id" => $this->id,
- ], [
- "sort" => "dts",
- "order" => "desc",
- ]);
- }
- /**
- * @param object $old_data
- * @param array $modified_fields
- * @return array
- */
- private function getModifiedArray(object $old_data, array $modified_fields): array {
- $new_data = $this->getModelData();
- $modified = [];
- foreach($modified_fields as $field) {
- $modified[] = [
- "field" => $field,
- "before" => $old_data->$field,
- "after" => $new_data->$field,
- ];
- }
- return $modified;
- }
- public function isArchived(): bool {
- return (bool) $this->archived;
- }
- /**
- * @return LocationNote[]
- * @throws Exception
- */
- public function getNotes(): array {
- return LocationNote::query([
- "location_id" => $this->id,
- "dts_deleted" => null,
- ], [
- "sort" => "id",
- "order" => "desc",
- ]);
- }
- }
|