bug fixes in contrib
[openemr.git] / services / FacilityService.php
blobfb995ebc7e42e918ae0428af4a291fb0545bdfd8
1 <?php
2 /**
3 * FacilityService
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Matthew Vita <matthewvita48@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services;
17 use OpenEMR\Common\Utils\QueryUtils;
18 use Particle\Validator\Validator;
20 class FacilityService
22 /**
23 * Default constructor.
25 public function __construct()
29 public function validate($facility)
31 $validator = new Validator();
33 $validator->required('name')->lengthBetween(2, 255);
34 $validator->required('phone')->lengthBetween(3, 30);
35 $validator->required('city')->lengthBetween(2, 255);
36 $validator->required('state')->lengthBetween(2, 50);
37 $validator->required('street')->lengthBetween(2, 255);
38 $validator->required('postal_code')->lengthBetween(2, 11);
39 $validator->required('email')->email();
40 $validator->required('fax')->lengthBetween(3, 30);
41 $validator->optional('country_code')->lengthBetween(2, 30);
42 $validator->optional('federal_ein')->lengthBetween(2, 15);
43 $validator->optional('website')->url();
44 $validator->optional('color')->lengthBetween(4, 7);
45 $validator->optional('service_location')->numeric();
46 $validator->optional('billing_location')->numeric();
47 $validator->optional('accepts_assignment')->numeric();
48 $validator->optional('pos_code')->numeric();
49 $validator->optional('domain_identifier')->lengthBetween(2, 60);
50 $validator->optional('attn')->lengthBetween(2, 65);
51 $validator->optional('tax_id_type')->lengthBetween(2, 31);
52 $validator->optional('primary_business_entity')->numeric();
53 $validator->optional('facility_npi')->lengthBetween(2, 15);
54 $validator->optional('facility_code')->lengthBetween(2, 31);
55 $validator->optional('facility_taxonomy')->lengthBetween(2, 15);
57 return $validator->validate($facility);
60 public function getAll()
62 return $this->get(array("order" => "ORDER BY FAC.name ASC"));
65 public function getPrimaryBusinessEntity($options = null)
67 if (!empty($options) && !empty($options["useLegacyImplementation"])) {
68 return $this->getPrimaryBusinessEntityLegacy();
71 $args = array(
72 "where" => "WHERE FAC.primary_business_entity = 1",
73 "data" => null,
74 "limit" => 1
77 if (!empty($options) && !empty($options["excludedId"])) {
78 $args["where"] .= " AND FAC.id != ?";
79 $args["data"] = $options["excludedId"];
80 return $this->get($args);
83 return $this->get($args);
86 public function getAllServiceLocations($options = null)
88 $args = array(
89 "where" => null,
90 "order" => "ORDER BY FAC.name ASC"
93 if (!empty($options) && !empty($options["orderField"])) {
94 $args["order"] = "ORDER BY FAC." . escape_sql_column_name($options["orderField"], array("facility")) . " ASC";
97 $args["where"] = "WHERE FAC.service_location = 1";
99 return $this->get($args);
102 public function getPrimaryBillingLocation()
104 return $this->get(array(
105 "order" => "ORDER BY FAC.billing_location DESC, FAC.id DESC",
106 "limit" => 1
110 public function getAllBillingLocations()
112 return $this->get(array(
113 "where" => "WHERE FAC.billing_location = 1",
114 "order" => "ORDER BY FAC.id ASC"
118 public function getById($id)
120 return $this->get(array(
121 "where" => "WHERE FAC.id = ?",
122 "data" => array($id),
123 "limit" => 1
127 public function getFacilityForUser($userId)
129 return $this->get(array(
130 "where" => "WHERE USER.id = ?",
131 "data" => array($userId),
132 "join" => "JOIN users USER ON FAC.id = USER.facility_id",
133 "limit" => 1
137 public function getFacilityForUserFormatted($userId)
139 $facility = $this->getFacilityForUser($userId);
141 if (!empty($facility)) {
142 $formatted = "";
143 $formatted .= $facility["name"];
144 $formatted .= "\n";
145 $formatted .= $facility["street"];
146 $formatted .= "\n";
147 $formatted .= $facility["city"];
148 $formatted .= "\n";
149 $formatted .= $facility["state"];
150 $formatted .= "\n";
151 $formatted .= $facility["postal_code"];
153 return array("facility_address" => $formatted);
156 return array("facility_address" => "");
159 public function getFacilityForEncounter($encounterId)
161 return $this->get(array(
162 "where" => "WHERE ENC.encounter = ?",
163 "data" => array($encounterId),
164 "join" => "JOIN form_encounter ENC ON FAC.id = ENC.facility_id",
165 "limit" => 1
169 public function update($data)
171 $sql = " UPDATE facility SET";
172 $sql .= " name=?,";
173 $sql .= " phone=?,";
174 $sql .= " fax=?,";
175 $sql .= " street=?,";
176 $sql .= " city=?,";
177 $sql .= " state=?,";
178 $sql .= " postal_code=?,";
179 $sql .= " country_code=?,";
180 $sql .= " federal_ein=?,";
181 $sql .= " website=?,";
182 $sql .= " email=?,";
183 $sql .= " color=?,";
184 $sql .= " service_location=?,";
185 $sql .= " billing_location=?,";
186 $sql .= " accepts_assignment=?,";
187 $sql .= " pos_code=?,";
188 $sql .= " domain_identifier=?,";
189 $sql .= " attn=?,";
190 $sql .= " tax_id_type=?,";
191 $sql .= " primary_business_entity=?,";
192 $sql .= " facility_npi=?,";
193 $sql .= " facility_code=?,";
194 $sql .= " facility_taxonomy=?";
195 $sql .= " WHERE id=?";
197 return sqlStatement(
198 $sql,
199 array(
200 $data["name"],
201 $data["phone"],
202 $data["fax"],
203 $data["street"],
204 $data["city"],
205 $data["state"],
206 $data["postal_code"],
207 $data["country_code"],
208 $data["federal_ein"],
209 $data["website"],
210 $data["email"],
211 $data["color"],
212 $data["service_location"],
213 $data["billing_location"],
214 $data["accepts_assignment"],
215 $data["pos_code"],
216 $data["domain_identifier"],
217 $data["attn"],
218 $data["tax_id_type"],
219 $data["primary_business_entity"],
220 $data["facility_npi"],
221 $data["facility_code"],
222 $data["facility_taxonomy"],
223 $data["fid"]
228 public function insert($data)
230 $sql = " INSERT INTO facility SET";
231 $sql .= " name=?,";
232 $sql .= " phone=?,";
233 $sql .= " fax=?,";
234 $sql .= " street=?,";
235 $sql .= " city=?,";
236 $sql .= " state=?,";
237 $sql .= " postal_code=?,";
238 $sql .= " country_code=?,";
239 $sql .= " federal_ein=?,";
240 $sql .= " website=?,";
241 $sql .= " email=?,";
242 $sql .= " color=?,";
243 $sql .= " service_location=?,";
244 $sql .= " billing_location=?,";
245 $sql .= " accepts_assignment=?,";
246 $sql .= " pos_code=?,";
247 $sql .= " domain_identifier=?,";
248 $sql .= " attn=?,";
249 $sql .= " tax_id_type=?,";
250 $sql .= " primary_business_entity=?,";
251 $sql .= " facility_npi=?,";
252 $sql .= " facility_code=?,";
253 $sql .= " facility_taxonomy=?";
255 return sqlInsert(
256 $sql,
257 array(
258 $data["name"],
259 $data["phone"],
260 $data["fax"],
261 $data["street"],
262 $data["city"],
263 $data["state"],
264 $data["postal_code"],
265 $data["country_code"],
266 $data["federal_ein"],
267 $data["website"],
268 $data["email"],
269 $data["color"],
270 $data["service_location"],
271 $data["billing_location"],
272 $data["accepts_assignment"],
273 $data["pos_code"],
274 $data["domain_identifier"],
275 $data["attn"],
276 $data["tax_id_type"],
277 $data["primary_business_entity"],
278 $data["facility_npi"],
279 $data["facility_code"],
280 $data["facility_taxonomy"]
285 public function updateUsersFacility($facility_name, $facility_id)
287 $sql = " UPDATE users SET";
288 $sql .= " facility=?";
289 $sql .= " WHERE facility_id=?";
291 return sqlStatement($sql, array($facility_name, $facility_id));
295 * Shared getter for the various specific facility getters.
297 * @param $map - Query information.
298 * @return array of associative arrays | one associative array.
300 private function get($map)
302 $sql = " SELECT FAC.id,";
303 $sql .= " FAC.name,";
304 $sql .= " FAC.phone,";
305 $sql .= " FAC.fax,";
306 $sql .= " FAC.street,";
307 $sql .= " FAC.city,";
308 $sql .= " FAC.state,";
309 $sql .= " FAC.postal_code,";
310 $sql .= " FAC.country_code,";
311 $sql .= " FAC.federal_ein,";
312 $sql .= " FAC.website,";
313 $sql .= " FAC.email,";
314 $sql .= " FAC.service_location,";
315 $sql .= " FAC.billing_location,";
316 $sql .= " FAC.accepts_assignment,";
317 $sql .= " FAC.pos_code,";
318 $sql .= " FAC.x12_sender_id,";
319 $sql .= " FAC.attn,";
320 $sql .= " FAC.domain_identifier,";
321 $sql .= " FAC.facility_npi,";
322 $sql .= " FAC.facility_taxonomy,";
323 $sql .= " FAC.tax_id_type,";
324 $sql .= " FAC.color,";
325 $sql .= " FAC.primary_business_entity,";
326 $sql .= " FAC.facility_code,";
327 $sql .= " FAC.extra_validation";
328 $sql .= " FROM facility FAC";
330 return QueryUtils::selectHelper($sql, $map);
333 private function getPrimaryBusinessEntityLegacy()
335 return $this->get(array(
336 "order" => "ORDER BY FAC.billing_location DESC, FAC.accepts_assignment DESC, FAC.id ASC",
337 "limit" => 1