location.php 5.4 KB

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