location.php 5.8 KB

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