location.php 6.0 KB

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