location.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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", "region", "region_name", "internal_id",
  51. "area_manager", "area_manager_name", "area_manager_email",
  52. "operations_manager_name", "operations_manager_email",
  53. "gaming_manager_name", "gaming_manager_email",
  54. "senior_gaming_manager_name", "senior_gaming_manager_email",
  55. "machine_count", "annual_test_count",
  56. "do_not_test", "closed", "archived",
  57. "latitude", "longitude",
  58. "dts_verified"
  59. ];
  60. // Object methods
  61. /**
  62. * @param array $types
  63. * @return bool
  64. * @throws Exception
  65. */
  66. public function assignVenueTypes(array $types): bool {
  67. $links = LocationVenueType::query(["location_id" => $this->id]);
  68. foreach($links as $link) {
  69. $link->delete();
  70. }
  71. foreach($types as $type) {
  72. $link = new LocationVenueType;
  73. $link->location_id = $this->id;
  74. $link->venue_type_id = $type->id;
  75. $link->commit();
  76. }
  77. return true;
  78. }
  79. /**
  80. * @return VenueType[]
  81. * @throws Exception
  82. */
  83. public function getVenueTypes(): array {
  84. $links = LocationVenueType::query(["location_id" => $this->id]);
  85. $ids = array_column($links, "venue_type_id");
  86. return VenueType::query(["id" => $ids]);
  87. }
  88. /**
  89. * @return Customer
  90. * @throws Exception
  91. */
  92. public function getCustomer(): Customer {
  93. return Customer::get($this->customer_id);
  94. }
  95. public function getDirectory() {
  96. $directory = ROOT_DIR . "/files/locations/" . $this->id;
  97. if(!file_exists($directory)) mkdir($directory, 0777, true);
  98. return $directory;
  99. }
  100. public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
  101. $parts = [];
  102. foreach($fields as $field) {
  103. $parts[] = $this->$field;
  104. }
  105. return implode($glue, array_filter($parts));
  106. }
  107. public function getGeolocation() {
  108. return [
  109. doubleval($this->latitude),
  110. doubleval($this->longitude)
  111. ];
  112. }
  113. /**
  114. * @return LocationFile[]
  115. * @throws Exception
  116. */
  117. public function getPhotos() {
  118. return LocationFile::query([
  119. "location_id" => $this->id,
  120. "type" => LocationFile::TYPE_PHOTO
  121. ]);
  122. }
  123. /**
  124. * @return Test[]
  125. * @throws Exception
  126. */
  127. public function getTests(array $nvp = []): array {
  128. $nvp = array_merge($nvp, [
  129. "location_id" => $this->id,
  130. "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
  131. ]);
  132. return Test::query($nvp, [
  133. "sort" => "dts_start",
  134. "order" => "desc",
  135. ]);
  136. }
  137. /**
  138. * @return Test[]
  139. * @throws Exception
  140. */
  141. public function getCompletedTests(array $nvp = []): array {
  142. $nvp = array_merge($nvp, [
  143. "location_id" => $this->id,
  144. "status" => Test::STATUS_COMPLETED,
  145. ]);
  146. return Test::query($nvp, [
  147. "sort" => "dts_start",
  148. "order" => "desc",
  149. ]);
  150. }
  151. /**
  152. * @param object $old_data
  153. * @param array $modified_fields
  154. * @return bool
  155. * @throws Exception
  156. */
  157. public function logUpdate(object $old_data, array $modified_fields): bool {
  158. if(empty($modified_fields)) return true;
  159. $entry = new LocationUpdateLogEntry;
  160. $entry->location_id = $this->id;
  161. $entry->user_id = Core::$user->id;
  162. $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
  163. $entry->dts = Util::getDateTime();
  164. return $entry->commit();
  165. }
  166. /**
  167. * @return LocationUpdateLogEntry[]
  168. * @throws Exception
  169. */
  170. public function getUpdateLogEntries() {
  171. return LocationUpdateLogEntry::query([
  172. "location_id" => $this->id,
  173. ], [
  174. "sort" => "dts",
  175. "order" => "desc",
  176. ]);
  177. }
  178. /**
  179. * @param object $old_data
  180. * @param array $modified_fields
  181. * @return array
  182. */
  183. private function getModifiedArray(object $old_data, array $modified_fields): array {
  184. $new_data = $this->getModelData();
  185. $modified = [];
  186. foreach($modified_fields as $field) {
  187. $modified[] = [
  188. "field" => $field,
  189. "before" => $old_data->$field,
  190. "after" => $new_data->$field,
  191. ];
  192. }
  193. return $modified;
  194. }
  195. public function isArchived(): bool {
  196. return (bool) $this->archived;
  197. }
  198. /**
  199. * @return LocationNote[]
  200. * @throws Exception
  201. */
  202. public function getNotes(): array {
  203. return LocationNote::query([
  204. "location_id" => $this->id,
  205. "dts_deleted" => null,
  206. ], [
  207. "sort" => "id",
  208. "order" => "desc",
  209. ]);
  210. }
  211. }