Updated ACL help file, minor styling changes (#1590)
[openemr.git] / services / FacilityService.php
blob292562c1ec8910182f39fdd2883e9d40d1a67664
1 <?php
2 /**
3 * FacilityService
5 * Copyright (C) 2017 Matthew Vita <matthewvita48@gmail.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Matthew Vita <matthewvita48@gmail.com>
20 * @link http://www.open-emr.org
23 namespace OpenEMR\Services;
25 use OpenEMR\Common\Utils\QueryUtils;
27 class FacilityService
29 /**
30 * Default constructor.
32 public function __construct()
36 public function getAll()
38 return $this->get(array("order" => "ORDER BY FAC.name ASC"));
41 public function getPrimaryBusinessEntity($options = null)
43 if (!empty($options) && !empty($options["useLegacyImplementation"])) {
44 return $this->getPrimaryBusinessEntityLegacy();
47 $args = array(
48 "where" => "WHERE FAC.primary_business_entity = 1",
49 "data" => null,
50 "limit" => 1
53 if (!empty($options) && !empty($options["excludedId"])) {
54 $args["where"] .= " AND FAC.id != ?";
55 $args["data"] = $options["excludedId"];
56 return $this->get($args);
59 return $this->get($args);
62 public function getAllServiceLocations($options = null)
64 $args = array(
65 "where" => null,
66 "order" => "ORDER BY FAC.name ASC"
69 if (!empty($options) && !empty($options["orderField"])) {
70 $args["order"] = "ORDER BY FAC." . escape_sql_column_name($options["orderField"], array("facility")) . " ASC";
73 $args["where"] = "WHERE FAC.service_location = 1";
75 return $this->get($args);
78 public function getPrimaryBillingLocation()
80 return $this->get(array(
81 "order" => "ORDER BY FAC.billing_location DESC, FAC.id DESC",
82 "limit" => 1
83 ));
86 public function getAllBillingLocations()
88 return $this->get(array(
89 "where" => "WHERE FAC.billing_location = 1",
90 "order" => "ORDER BY FAC.id ASC"
91 ));
94 public function getById($id)
96 return $this->get(array(
97 "where" => "WHERE FAC.id = ?",
98 "data" => array($id),
99 "limit" => 1
103 public function getFacilityForUser($userId)
105 return $this->get(array(
106 "where" => "WHERE USER.id = ?",
107 "data" => array($userId),
108 "join" => "JOIN users USER ON FAC.name = USER.facility",
109 "limit" => 1
113 public function getFacilityForUserFormatted($userId)
115 $facility = $this->getFacilityForUser($userId);
117 if (!empty($facility)) {
118 $formatted = "";
119 $formatted .= $facility["name"];
120 $formatted .= "\n";
121 $formatted .= $facility["street"];
122 $formatted .= "\n";
123 $formatted .= $facility["city"];
124 $formatted .= "\n";
125 $formatted .= $facility["state"];
126 $formatted .= "\n";
127 $formatted .= $facility["postal_code"];
129 return array("facility_address" => $formatted);
132 return array("facility_address" => "");
135 public function getFacilityForEncounter($encounterId)
137 return $this->get(array(
138 "where" => "WHERE ENC.encounter = ?",
139 "data" => array($encounterId),
140 "join" => "JOIN form_encounter ENC ON FAC.id = ENC.facility_id",
141 "limit" => 1
145 public function update($data)
147 $sql = " UPDATE facility SET";
148 $sql .= " name='" . add_escape_custom($data["name"]) . "',";
149 $sql .= " phone='" . add_escape_custom($data["phone"]) . "',";
150 $sql .= " fax='" . add_escape_custom($data["fax"]) . "',";
151 $sql .= " street='" . add_escape_custom($data["street"]) . "',";
152 $sql .= " city='" . add_escape_custom($data["city"]) . "',";
153 $sql .= " state='" . add_escape_custom($data["state"]) . "',";
154 $sql .= " postal_code='" . add_escape_custom($data["postal_code"]) . "',";
155 $sql .= " country_code='" . add_escape_custom($data["country_code"]) . "',";
156 $sql .= " federal_ein='" . add_escape_custom($data["federal_ein"]) . "',";
157 $sql .= " website='" . add_escape_custom($data["website"]) . "',";
158 $sql .= " email='" . add_escape_custom($data["email"]) . "',";
159 $sql .= " color='" . add_escape_custom($data["color"]) . "',";
160 $sql .= " service_location='" . add_escape_custom($data["service_location"]) . "',";
161 $sql .= " billing_location='" . add_escape_custom($data["billing_location"]) . "',";
162 $sql .= " accepts_assignment='" . add_escape_custom($data["accepts_assignment"]) . "',";
163 $sql .= " pos_code='" . add_escape_custom($data["pos_code"]) . "',";
164 $sql .= " domain_identifier='" . add_escape_custom($data["domain_identifier"]) . "',";
165 $sql .= " attn='" . add_escape_custom($data["attn"]) . "',";
166 $sql .= " tax_id_type='" . add_escape_custom($data["tax_id_type"]) . "',";
167 $sql .= " primary_business_entity='" . add_escape_custom($data["primary_business_entity"]) . "',";
168 $sql .= " facility_npi='" . add_escape_custom($data["facility_npi"]) . "',";
169 $sql .= " facility_code='" . add_escape_custom($data["facility_code"]) . "',";
170 $sql .= " facility_taxonomy='" . add_escape_custom($data["facility_taxonomy"]) . "'";
171 $sql .= " WHERE id='" . add_escape_custom($data["fid"]) . "'";
173 return sqlStatement($sql);
176 public function insert($data)
178 $sql = " INSERT INTO facility SET";
179 $sql .= " name='" . add_escape_custom($data["name"]) . "',";
180 $sql .= " phone='" . add_escape_custom($data["phone"]) . "',";
181 $sql .= " fax='" . add_escape_custom($data["fax"]) . "',";
182 $sql .= " street='" . add_escape_custom($data["street"]) . "',";
183 $sql .= " city='" . add_escape_custom($data["city"]) . "',";
184 $sql .= " state='" . add_escape_custom($data["state"]) . "',";
185 $sql .= " postal_code='" . add_escape_custom($data["postal_code"]) . "',";
186 $sql .= " country_code='" . add_escape_custom($data["country_code"]) . "',";
187 $sql .= " federal_ein='" . add_escape_custom($data["federal_ein"]) . "',";
188 $sql .= " website='" . add_escape_custom($data["website"]) . "',";
189 $sql .= " email='" . add_escape_custom($data["email"]) . "',";
190 $sql .= " color='" . add_escape_custom($data["color"]) . "',";
191 $sql .= " service_location='" . add_escape_custom($data["service_location"]) . "',";
192 $sql .= " billing_location='" . add_escape_custom($data["billing_location"]) . "',";
193 $sql .= " accepts_assignment='" . add_escape_custom($data["accepts_assignment"]) . "',";
194 $sql .= " pos_code='" . add_escape_custom($data["pos_code"]) . "',";
195 $sql .= " domain_identifier='" . add_escape_custom($data["domain_identifier"]) . "',";
196 $sql .= " attn='" . add_escape_custom($data["attn"]) . "',";
197 $sql .= " tax_id_type='" . add_escape_custom($data["tax_id_type"]) . "',";
198 $sql .= " primary_business_entity='" . add_escape_custom($data["primary_business_entity"]) . "',";
199 $sql .= " facility_npi='" . add_escape_custom($data["facility_npi"]) . "',";
200 $sql .= " facility_code='" . add_escape_custom($data["facility_code"]) . "',";
201 $sql .= " facility_taxonomy='" . add_escape_custom($data["facility_taxonomy"]) . "'";
203 return sqlInsert($sql);
206 public function updateUsersFacility($facility_name, $facility_id)
208 $sql = " UPDATE users SET";
209 $sql .= " facility='" . add_escape_custom($facility_name) . "'";
210 $sql .= " WHERE facility_id='" . add_escape_custom($facility_id) . "'";
212 return sqlStatement($sql);
216 * Shared getter for the various specific facility getters.
218 * @param $map - Query information.
219 * @return array of associative arrays | one associative array.
221 private function get($map)
223 $sql = " SELECT FAC.id,";
224 $sql .= " FAC.name,";
225 $sql .= " FAC.phone,";
226 $sql .= " FAC.fax,";
227 $sql .= " FAC.street,";
228 $sql .= " FAC.city,";
229 $sql .= " FAC.state,";
230 $sql .= " FAC.postal_code,";
231 $sql .= " FAC.country_code,";
232 $sql .= " FAC.federal_ein,";
233 $sql .= " FAC.website,";
234 $sql .= " FAC.email,";
235 $sql .= " FAC.service_location,";
236 $sql .= " FAC.billing_location,";
237 $sql .= " FAC.accepts_assignment,";
238 $sql .= " FAC.pos_code,";
239 $sql .= " FAC.x12_sender_id,";
240 $sql .= " FAC.attn,";
241 $sql .= " FAC.domain_identifier,";
242 $sql .= " FAC.facility_npi,";
243 $sql .= " FAC.facility_taxonomy,";
244 $sql .= " FAC.tax_id_type,";
245 $sql .= " FAC.color,";
246 $sql .= " FAC.primary_business_entity,";
247 $sql .= " FAC.facility_code,";
248 $sql .= " FAC.extra_validation";
249 $sql .= " FROM facility FAC";
251 return QueryUtils::selectHelper($sql, $map);
254 private function getPrimaryBusinessEntityLegacy()
256 return $this->get(array(
257 "order" => "ORDER BY FAC.billing_location DESC, FAC.accepts_assignment DESC, FAC.id ASC",
258 "limit" => 1