location.php 5.4 KB

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