forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FacilityService.php
452 lines (401 loc) · 15.8 KB
/
FacilityService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* FacilityService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @author Brady Miller <[email protected]>
* @author Sherwin Gaddis <[email protected]>
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2018 Matthew Vita <[email protected]>
* @copyright Copyright (c) 2018 Brady Miller <[email protected]>
* @copyright Copyright (c) 2020 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\SqlQueryException;
use OpenEMR\Common\Logging\SystemLogger;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\Search\SearchModifier;
use OpenEMR\Services\Search\StringSearchField;
use OpenEMR\Services\Search\TokenSearchField;
use OpenEMR\Validators\FacilityValidator;
use OpenEMR\Validators\ProcessingResult;
use OpenEMR\Events\Facility\FacilityCreatedEvent;
use OpenEMR\Events\Facility\FacilityUpdatedEvent;
use Particle\Validator\Validator;
class FacilityService extends BaseService
{
private $facilityValidator;
private const FACILITY_TABLE = "facility";
/**
* Default constructor.
*/
public function __construct()
{
parent::__construct(self::FACILITY_TABLE);
UuidRegistry::createMissingUuidsForTables([self::FACILITY_TABLE]);
$this->facilityValidator = new FacilityValidator();
}
public function getUuidFields(): array
{
return ['uuid'];
}
public function validate($facility)
{
$validator = new Validator();
$validator->required('name')->lengthBetween(2, 255);
$validator->required('phone')->lengthBetween(3, 30);
$validator->required('city')->lengthBetween(2, 255);
$validator->required('state')->lengthBetween(2, 50);
$validator->required('street')->lengthBetween(2, 255);
$validator->required('postal_code')->lengthBetween(2, 11);
$validator->required('email')->email();
$validator->required('fax')->lengthBetween(3, 30);
$validator->optional('country_code')->lengthBetween(2, 30);
$validator->optional('federal_ein')->lengthBetween(2, 15);
$validator->optional('website')->url();
$validator->optional('color')->lengthBetween(4, 7);
$validator->optional('service_location')->numeric();
$validator->optional('billing_location')->numeric();
$validator->optional('accepts_assignment')->numeric();
$validator->optional('pos_code')->numeric();
$validator->optional('domain_identifier')->lengthBetween(2, 60);
$validator->optional('attn')->lengthBetween(2, 65);
$validator->optional('tax_id_type')->lengthBetween(2, 31);
$validator->optional('primary_business_entity')->numeric();
$validator->optional('facility_npi')->lengthBetween(2, 15);
$validator->optional('facility_code')->lengthBetween(2, 31);
$validator->optional('facility_taxonomy')->lengthBetween(2, 15);
$validator->optional('iban')->lengthBetween(2, 34);
return $validator->validate($facility);
}
public function getAllFacility()
{
return $this->get(array("order" => "ORDER BY FAC.name ASC"));
}
public function getPrimaryBusinessEntity($options = null)
{
if (!empty($options) && !empty($options["useLegacyImplementation"])) {
return $this->getPrimaryBusinessEntityLegacy();
}
$searchArgs = ['primary_business_entity' => new StringSearchField('primary_business_entity', [1], SearchModifier::EXACT)];
if (!empty($options) && !empty($options["excludedId"])) {
$searchArgs['id'] = new TokenSearchField('id', $options['excludedId']);
$searchArgs['id']->setModifier(SearchModifier::NOT_EQUALS_EXACT);
}
$results = $this->search($searchArgs);
if (!empty($results->getData())) {
$pbe_results = $results->getData();
return array_pop($pbe_results);
}
return null;
}
public function getAllServiceLocations($options = null)
{
$args = array(
"where" => null,
"order" => "ORDER BY FAC.name ASC"
);
if (!empty($options) && !empty($options["orderField"])) {
$args["order"] = "ORDER BY FAC." . escape_sql_column_name($options["orderField"], array("facility")) . " ASC";
}
$args["where"] = "WHERE FAC.service_location = 1";
return $this->get($args);
}
public function getPrimaryBillingLocation()
{
$record = $this->get(array(
"order" => "ORDER BY FAC.billing_location DESC, FAC.id DESC",
"limit" => 1
));
return $record;
}
public function getAllBillingLocations()
{
return $this->get(array(
"where" => "WHERE FAC.billing_location = 1",
"order" => "ORDER BY FAC.id ASC"
));
}
public function getById($id)
{
if (empty($id)) {
// Not okay to throw exception here. Most UI are pulldowns which init to empty.
return false;
}
// $id has to be a string for TokenSearchField()
$result = $this->search(['id' => new TokenSearchField('id', (string) $id, false)]);
if (!empty($result->getData())) {
$facility_result = $result->getData();
$facility = array_pop($facility_result);
return $facility;
}
return null;
}
public function getFacilityForUser($userId)
{
$record = $this->get(array(
"where" => "WHERE USER.id = ?",
"data" => array($userId),
"join" => "JOIN users USER ON FAC.id = USER.facility_id",
"limit" => 1
));
return $record;
}
public function getFacilityForUserFormatted($userId)
{
$facility = $this->getFacilityForUser($userId);
if (!empty($facility)) {
$formatted = "";
$formatted .= $facility["name"];
$formatted .= "\n";
$formatted .= $facility["street"];
$formatted .= "\n";
$formatted .= $facility["city"];
$formatted .= "\n";
$formatted .= $facility["state"];
$formatted .= "\n";
$formatted .= $facility["postal_code"];
return array("facility_address" => $formatted);
}
return array("facility_address" => "");
}
public function getFacilityForEncounter($encounterId)
{
$record = $this->get(array(
"where" => "WHERE ENC.encounter = ?",
"data" => array($encounterId),
"join" => "JOIN form_encounter ENC ON FAC.id = ENC.facility_id",
"limit" => 1
));
return $record;
}
public function updateFacility($data)
{
$dataBeforeUpdate = $this->getById($data['id']);
$query = $this->buildUpdateColumns($data);
$sql = " UPDATE facility SET ";
$sql .= $query['set'];
$sql .= " WHERE id = ?";
array_push($query['bind'], $data['id']);
$result = sqlStatement(
$sql,
$query['bind']
);
$facilityUpdatedEvent = new FacilityUpdatedEvent($dataBeforeUpdate, $data);
$GLOBALS["kernel"]->getEventDispatcher()->dispatch($facilityUpdatedEvent, FacilityUpdatedEvent::EVENT_HANDLE, 10);
return $result;
}
public function insertFacility($data)
{
$query = $this->buildInsertColumns($data);
$sql = " INSERT INTO facility SET ";
$sql .= $query['set'];
$facilityId = sqlInsert(
$sql,
$query['bind']
);
$facilityCreatedEvent = new FacilityCreatedEvent(array_merge($data, ['id' => $facilityId]));
$GLOBALS["kernel"]->getEventDispatcher()->dispatch($facilityCreatedEvent, FacilityCreatedEvent::EVENT_HANDLE, 10);
return $facilityId;
}
public function updateUsersFacility($facility_name, $facility_id)
{
$sql = " UPDATE users SET";
$sql .= " facility=?";
$sql .= " WHERE facility_id=?";
return sqlStatement($sql, array($facility_name, $facility_id));
}
/**
* Shared getter for the various specific facility getters.
* NOTE: if a limit of 1 is specified the associative array is returned
*
* @param $map - Query information.
* @return array of associative arrays | one associative array.
*/
private function get($map)
{
try {
$sql = " SELECT FAC.id,";
$sql .= " FAC.uuid,";
$sql .= " FAC.name,";
$sql .= " FAC.phone,";
$sql .= " FAC.fax,";
$sql .= " FAC.street,";
$sql .= " FAC.city,";
$sql .= " FAC.state,";
$sql .= " FAC.postal_code,";
$sql .= " FAC.country_code,";
$sql .= " FAC.federal_ein,";
$sql .= " FAC.website,";
$sql .= " FAC.email,";
$sql .= " FAC.service_location,";
$sql .= " FAC.billing_location,";
$sql .= " FAC.accepts_assignment,";
$sql .= " FAC.pos_code,";
$sql .= " FAC.x12_sender_id,";
$sql .= " FAC.attn,";
$sql .= " FAC.domain_identifier,";
$sql .= " FAC.facility_npi,";
$sql .= " FAC.facility_taxonomy,";
$sql .= " FAC.tax_id_type,";
$sql .= " FAC.color,";
$sql .= " FAC.primary_business_entity,";
$sql .= " FAC.facility_code,";
$sql .= " FAC.extra_validation,";
$sql .= " FAC.mail_street,";
$sql .= " FAC.mail_street2,";
$sql .= " FAC.mail_city,";
$sql .= " FAC.mail_state,";
$sql .= " FAC.mail_zip,";
$sql .= " FAC.oid,";
$sql .= " FAC.iban,";
$sql .= " FAC.info,";
$sql .= " FAC.inactive";
$sql .= " FROM facility FAC";
$records = self::selectHelper($sql, $map);
$returnRecords = [];
if (!empty($records)) {
// base service method returns just the associative array which messes with our methods for LIMIT etc.
if (!empty($map['limit']) && $map['limit'] == 1) {
$returnRecords = $this->createResultRecordFromDatabaseResult($records);
} else {
foreach ($records as $record) {
$returnRecords[] = $this->createResultRecordFromDatabaseResult($record);
}
}
}
return $returnRecords;
} catch (SqlQueryException $exception) {
(new SystemLogger())->error($exception->getMessage(), ['trace' => $exception->getTraceAsString()]);
throw $exception;
}
}
private function getPrimaryBusinessEntityLegacy()
{
$record = $this->get(array(
"order" => "ORDER BY FAC.billing_location DESC, FAC.accepts_assignment DESC, FAC.id ASC",
"limit" => 1
));
return $record;
}
public function getAllWithIds(array $ids)
{
$idField = new TokenSearchField('id', $ids);
return $this->search(['id' => $idField]);
}
/**
* Returns a list of facilities matching optional search criteria.
* Search criteria is conveyed by array where key = field/column name, value = field value.
* If no search criteria is provided, all records are returned.
*
* @param $search search array parameters
* @param $isAndCondition specifies if AND condition is used for multiple criteria. Defaults to true.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getAll($search = array(), $isAndCondition = true)
{
$querySearch = [];
if (!empty($search)) {
if (isset($search['uuid'])) {
$querySearch['uuid'] = new TokenSearchField('uuid', $search['uuid']);
unset($search['uuid']);
}
foreach ($search as $field => $value) {
if (isset($search[$field])) {
$querySearch[$field] = new StringSearchField($field, $search[$field], SearchModifier::EXACT, $isAndCondition);
}
}
}
return $this->search($querySearch, $isAndCondition);
}
/**
* Returns a single facility record by facility uuid.
* @param $uuid - The facility uuid identifier in string format.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getOne($uuid): ProcessingResult
{
$processingResult = new ProcessingResult();
$isValid = $this->facilityValidator->validateId('uuid', self::FACILITY_TABLE, $uuid, true);
if ($isValid !== true) {
return $isValid;
}
return $this->search(['uuid' => new TokenSearchField('uuid', $uuid, true)]);
}
/**
* Inserts a new facility record.
*
* @param $data The facility fields (array) to insert.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function insert($data)
{
$processingResult = $this->facilityValidator->validate(
$data,
FacilityValidator::DATABASE_INSERT_CONTEXT
);
if (!$processingResult->isValid()) {
return $processingResult;
}
$data['uuid'] = (new UuidRegistry(['table_name' => self::FACILITY_TABLE]))->createUuid();
$query = $this->buildInsertColumns($data);
$sql = " INSERT INTO " . self::FACILITY_TABLE . " SET ";
$sql .= $query['set'];
$results = sqlInsert(
$sql,
$query['bind']
);
if ($results) {
$processingResult->addData(array(
'id' => $results,
'uuid' => UuidRegistry::uuidToString($data['uuid'])
));
} else {
$processingResult->addInternalError("error processing SQL Insert");
}
return $processingResult;
}
/**
* Updates an existing facility record.
*
* @param $uuid - The facility uuid identifier in string format used for update.
* @param $data - The updated facility data fields
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function update($uuid, $data)
{
if (empty($data)) {
$processingResult = new ProcessingResult();
$processingResult->setValidationMessages("Invalid Data");
return $processingResult;
}
$data["uuid"] = $uuid;
$processingResult = $this->facilityValidator->validate(
$data,
FacilityValidator::DATABASE_UPDATE_CONTEXT
);
if (!$processingResult->isValid()) {
return $processingResult;
}
$query = $this->buildUpdateColumns($data);
$sql = " UPDATE " . self::FACILITY_TABLE . " SET ";
$sql .= $query['set'];
$sql .= " WHERE `uuid` = ?";
$uuidBinary = UuidRegistry::uuidToBytes($uuid);
array_push($query['bind'], $uuidBinary);
$sqlResult = sqlStatement($sql, $query['bind']);
if (!$sqlResult) {
$processingResult->addErrorMessage("error processing SQL Update");
} else {
$processingResult = $this->getOne($uuid);
}
return $processingResult;
}
}