1
0

location.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property string $name
  6. * @property string $legal_entity_name
  7. * @property string $address_1
  8. * @property string $address_2
  9. * @property string $city
  10. * @property string $county
  11. * @property string $postal_code
  12. * @property string $state
  13. * @property string $country
  14. * @property string $email
  15. * @property string $phone
  16. * @property string $opening_hours
  17. * @property string $notes
  18. * @property string $shop_left
  19. * @property string $shop_right
  20. * @property string $brand
  21. * @property string $region
  22. * @property string $region_name
  23. * @property string $internal_id
  24. * @property string $area_manager
  25. * @property string $area_manager_name
  26. * @property string $area_manager_email
  27. * @property string $operations_manager_name
  28. * @property string $operations_manager_email
  29. * @property string $gaming_manager_name
  30. * @property string $gaming_manager_email
  31. * @property string $senior_gaming_manager_name
  32. * @property string $senior_gaming_manager_email
  33. * @property int $machine_count
  34. * @property int $annual_test_count
  35. * @property bool $do_not_test
  36. * @property bool $closed
  37. * @property bool $archived
  38. * @property double $latitude
  39. * @property double $longitude
  40. * @property string $dts_verified
  41. */
  42. class Location extends Model {
  43. protected static $table = "location";
  44. protected static $id_field = "id";
  45. protected static $fillable_fields = [
  46. "customer_id", "name", "legal_entity_name",
  47. "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone",
  48. "opening_hours", "notes",
  49. "shop_left", "shop_right",
  50. "brand",
  51. "region", "region_name",
  52. "internal_id",
  53. "area_manager", "area_manager_name", "area_manager_email",
  54. "operations_manager_name", "operations_manager_email",
  55. "gaming_manager_name", "gaming_manager_email",
  56. "senior_gaming_manager_name", "senior_gaming_manager_email",
  57. "machine_count", "annual_test_count",
  58. "do_not_test", "closed", "archived",
  59. "latitude", "longitude",
  60. "dts_verified"
  61. ];
  62. // Object methods
  63. /**
  64. * @param array $types
  65. * @return bool
  66. * @throws Exception
  67. */
  68. public function assignVenueTypes(array $types): bool {
  69. $links = LocationVenueType::query(["location_id" => $this->id]);
  70. foreach($links as $link) {
  71. $link->delete();
  72. }
  73. foreach($types as $type) {
  74. $link = new LocationVenueType;
  75. $link->location_id = $this->id;
  76. $link->venue_type_id = $type->id;
  77. $link->commit();
  78. }
  79. return true;
  80. }
  81. /**
  82. * @return VenueType[]
  83. * @throws Exception
  84. */
  85. public function getVenueTypes(): array {
  86. $links = LocationVenueType::query(["location_id" => $this->id]);
  87. $ids = array_column($links, "venue_type_id");
  88. return VenueType::query(["id" => $ids]);
  89. }
  90. /**
  91. * @return Customer
  92. * @throws Exception
  93. */
  94. public function getCustomer(): Customer {
  95. return Customer::get($this->customer_id);
  96. }
  97. public function getDirectory() {
  98. $directory = ROOT_DIR . "/files/locations/" . $this->id;
  99. if(!file_exists($directory)) mkdir($directory, 0777, true);
  100. return $directory;
  101. }
  102. public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
  103. $parts = [];
  104. foreach($fields as $field) {
  105. $parts[] = $this->$field;
  106. }
  107. return implode($glue, array_filter($parts));
  108. }
  109. public function getGeolocation() {
  110. return [
  111. doubleval($this->latitude),
  112. doubleval($this->longitude)
  113. ];
  114. }
  115. /**
  116. * @return LocationFile[]
  117. * @throws Exception
  118. */
  119. public function getPhotos() {
  120. return LocationFile::query([
  121. "location_id" => $this->id,
  122. "type" => LocationFile::TYPE_PHOTO
  123. ]);
  124. }
  125. /**
  126. * @return Test[]
  127. * @throws Exception
  128. */
  129. public function getTests(array $nvp = []): array {
  130. $nvp = array_merge($nvp, [
  131. "location_id" => $this->id,
  132. "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
  133. ]);
  134. return Test::query($nvp, [
  135. "sort" => "dts_start",
  136. "order" => "desc",
  137. ]);
  138. }
  139. /**
  140. * @return Test[]
  141. * @throws Exception
  142. */
  143. public function getCompletedTests(array $nvp = []): array {
  144. $nvp = array_merge($nvp, [
  145. "location_id" => $this->id,
  146. "status" => Test::STATUS_COMPLETED,
  147. ]);
  148. return Test::query($nvp, [
  149. "sort" => "dts_start",
  150. "order" => "desc",
  151. ]);
  152. }
  153. /**
  154. * @param object $old_data
  155. * @param array $modified_fields
  156. * @return bool
  157. * @throws Exception
  158. */
  159. public function logUpdate(object $old_data, array $modified_fields): bool {
  160. if(empty($modified_fields)) return true;
  161. $entry = new LocationUpdateLogEntry;
  162. $entry->location_id = $this->id;
  163. $entry->user_id = Core::$user->id;
  164. $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
  165. $entry->dts = Util::getDateTime();
  166. return $entry->commit();
  167. }
  168. /**
  169. * @return LocationUpdateLogEntry[]
  170. * @throws Exception
  171. */
  172. public function getUpdateLogEntries() {
  173. return LocationUpdateLogEntry::query([
  174. "location_id" => $this->id,
  175. ], [
  176. "sort" => "dts",
  177. "order" => "desc",
  178. ]);
  179. }
  180. /**
  181. * @param object $old_data
  182. * @param array $modified_fields
  183. * @return array
  184. */
  185. private function getModifiedArray(object $old_data, array $modified_fields): array {
  186. $new_data = $this->getModelData();
  187. $modified = [];
  188. foreach($modified_fields as $field) {
  189. $modified[] = [
  190. "field" => $field,
  191. "before" => $old_data->$field,
  192. "after" => $new_data->$field,
  193. ];
  194. }
  195. return $modified;
  196. }
  197. public function isArchived(): bool {
  198. return (bool) $this->archived;
  199. }
  200. /**
  201. * @return LocationNote[]
  202. * @throws Exception
  203. */
  204. public function getNotes(): array {
  205. return LocationNote::query([
  206. "location_id" => $this->id,
  207. "dts_deleted" => null,
  208. ], [
  209. "sort" => "id",
  210. "order" => "desc",
  211. ]);
  212. }
  213. }