forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsuranceCompanyService.php
453 lines (410 loc) · 16.4 KB
/
InsuranceCompanyService.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
453
<?php
/**
* InsuranceCompanyService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @author Brady Miller <[email protected]>
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2018 Matthew Vita <[email protected]>
* @copyright Copyright (c) 2018 Brady Miller <[email protected]>
* @copyright Copyright (c) 2024 Care Management Solutions, Inc. <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Database\SqlQueryException;
use OpenEMR\Common\Logging\SystemLogger;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\{
AddressService,
PhoneNumberService,
Search\FhirSearchWhereClauseBuilder,
Search\SearchFieldException
};
use OpenEMR\Validators\InsuranceCompanyValidator;
use OpenEMR\Validators\ProcessingResult;
class InsuranceCompanyService extends BaseService
{
private const INSURANCE_TABLE = "insurance_companies";
private $insuranceCompanyValidator;
private $addressService = null;
private $phoneNumberService = null;
public const TYPE_FAX = 5;
public const TYPE_WORK = 2;
/**
* @var null | array $cqm_sops cached CQM SOPS
*/
private $cqm_sops = null;
/**
* @var null | array $types cached insurance types
*/
private $types = null;
/**
* @var null | array $claim_types cached claim types
*/
private $claim_types = null;
/**
* Default constructor.
*/
public function __construct()
{
$this->addressService = new AddressService();
$this->phoneNumberService = new PhoneNumberService();
UuidRegistry::createMissingUuidsForTables([self::INSURANCE_TABLE]);
$this->insuranceCompanyValidator = new InsuranceCompanyValidator();
parent::__construct(self::INSURANCE_TABLE);
}
public function getInsuranceDisplayName($insuranceId)
{
$searchResults = $this->search(['id' => $insuranceId]);
$insuranceCompany = null;
if ($searchResults->hasData()) {
$insuranceCompany = $searchResults->getData()[0];
}
if (!empty($insuranceCompany)) {
return self::getDisplayNameForInsuranceRecord($insuranceCompany);
} else {
return "";
}
}
public static function getDisplayNameForInsuranceRecord($insuranceCompany)
{
switch ($GLOBALS['insurance_information']) {
case '1':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ")";
break;
case '2':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ", " . $insuranceCompany['zip'] . ")";
break;
case '3':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ", " . $insuranceCompany['state'] . ")";
break;
case '4':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ", " . $insuranceCompany['state'] .
", " . $insuranceCompany['zip'] . ")";
break;
case '5':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ", " . $insuranceCompany['city'] .
", " . $insuranceCompany['state'] . ", " . $insuranceCompany['zip'] . ")";
break;
case '6':
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['line1'] . ", " . $insuranceCompany['line2'] . ", " . $insuranceCompany['city'] .
", " . $insuranceCompany['state'] . ", " . $insuranceCompany['zip'] . ", " . $insuranceCompany['cms_id'] . ")";
break;
case '7':
preg_match("/\d+/", $insuranceCompany['line1'], $matches);
$returnval = $insuranceCompany['name'] . " (" . $insuranceCompany['zip'] .
"," . $matches[0] . ")";
break;
case '0':
default:
$returnval = $insuranceCompany['name'];
break;
}
return $returnval;
}
public function getUuidFields(): array
{
return ['uuid'];
}
public function search($search, $isAndCondition = true)
{
$sql = " SELECT i.id,";
$sql .= " i.uuid,";
$sql .= " i.name,";
$sql .= " i.attn,";
$sql .= " i.cms_id,";
$sql .= " i.ins_type_code,";
$sql .= " i.x12_receiver_id,";
$sql .= " i.x12_default_partner_id,";
$sql .= " x12.x12_default_partner_name,";
$sql .= " i.alt_cms_id,";
$sql .= " i.inactive,work_number.work_id,fax_number.fax_id,";
$sql .= " CONCAT(
COALESCE(work_number.country_code,'')
,COALESCE(work_number.area_code,'')
,COALESCE(work_number.prefix,'')
, work_number.number
) AS work_number,";
$sql .= " CONCAT(
COALESCE(fax_number.country_code,'')
,COALESCE(fax_number.area_code,'')
,COALESCE(fax_number.prefix,'')
, fax_number.number
) AS fax_number,";
$sql .= " a.line1,";
$sql .= " a.line2,";
$sql .= " a.city,";
$sql .= " a.state,";
$sql .= " a.zip,";
$sql .= " a.plus_four,";
$sql .= " a.country,";
$sql .= " i.date_created,";
$sql .= " i.last_updated";
$sql .= " FROM insurance_companies i ";
$sql .= " LEFT JOIN (SELECT line1,line2,city,state,zip,plus_four,country,foreign_id FROM addresses) a ON i.id = a.foreign_id";
// the foreign_id here is a globally unique sequence so there is no conflict.
// I don't like the assumption here as it should be more explicit what table we are pulling
// from since OpenEMR mixes a bunch of paradigms. I initially worried about data corruption as phone_numbers
// foreign id could be ambigious here... but since the sequence is globally unique @see \generate_id() we can
// join here safely...
$sql .= " LEFT JOIN (
SELECT id AS work_id,foreign_id,country_code, area_code, prefix, number
FROM phone_numbers WHERE number IS NOT NULL AND type = " . self::TYPE_WORK . "
) work_number ON i.id = work_number.foreign_id";
$sql .= " LEFT JOIN (
SELECT id AS fax_id,foreign_id,country_code, area_code, prefix, number
FROM phone_numbers WHERE number IS NOT NULL AND type = " . self::TYPE_FAX . "
) fax_number ON i.id = fax_number.foreign_id";
$sql .= " LEFT JOIN (
SELECT id AS x12_default_partner_id, name AS x12_default_partner_name
FROM x12_partners
) x12 ON i.x12_default_partner_id = x12.x12_default_partner_id";
$processingResult = new ProcessingResult();
try {
$whereFragment = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereFragment->getFragment();
$records = QueryUtils::fetchRecords($sql, $whereFragment->getBoundValues());
if (!empty($records)) {
foreach ($records as $row) {
$resultRecord = $this->createResultRecordFromDatabaseResult($row);
$processingResult->addData($resultRecord);
}
}
} catch (SqlQueryException $exception) {
// we shouldn't hit a query exception
(new SystemLogger())->error($exception->getMessage(), ['trace' => $exception->getTraceAsString()]);
$processingResult->addInternalError("Error selecting data from database");
} catch (SearchFieldException $exception) {
(new SystemLogger())->error(
$exception->getMessage(),
['trace' => $exception->getTraceAsString(),
'field' => $exception->getField()]
);
$processingResult->setValidationMessages([$exception->getField() => $exception->getMessage()]);
}
return $processingResult;
}
public function getAll($search = array(), $isAndCondition = true)
{
// Validating and Converting UUID to ID
if (isset($search['id'])) {
$isValidcondition = $this->insuranceCompanyValidator->validateId(
'uuid',
self::INSURANCE_TABLE,
$search['id'],
true
);
if ($isValidcondition !== true) {
return $isValidcondition;
}
$uuidBytes = UuidRegistry::uuidToBytes($search['id']);
$search['id'] = $this->getIdByUuid($uuidBytes, self::INSURANCE_TABLE, "id");
}
$sqlBindArray = array();
$sql = " SELECT i.id,";
$sql .= " i.uuid,";
$sql .= " i.name,";
$sql .= " i.attn,";
$sql .= " i.cms_id,";
$sql .= " i.ins_type_code,";
$sql .= " i.x12_receiver_id,";
$sql .= " i.x12_default_partner_id,";
$sql .= " i.alt_cms_id,";
$sql .= " i.inactive,";
$sql .= " a.line1,";
$sql .= " a.line2,";
$sql .= " a.city,";
$sql .= " a.state,";
$sql .= " a.zip,";
$sql .= " a.country";
$sql .= " FROM insurance_companies i";
$sql .= " LEFT JOIN addresses a ON i.id = a.foreign_id";
if (!empty($search)) {
$sql .= ' AND ';
$whereClauses = array();
foreach ($search as $fieldName => $fieldValue) {
array_push($whereClauses, $fieldName . ' = ?');
array_push($sqlBindArray, $fieldValue);
}
$sqlCondition = ($isAndCondition == true) ? 'AND' : 'OR';
$sql .= implode(' ' . $sqlCondition . ' ', $whereClauses);
}
$statementResults = sqlStatement($sql, $sqlBindArray);
$processingResult = new ProcessingResult();
while ($row = sqlFetchArray($statementResults)) {
$row = $this->createResultRecordFromDatabaseResult($row);
$processingResult->addData($row);
}
return $processingResult;
}
public function getOneById($id)
{
// TODO: this should be refactored to use getAll but its selecting all the columns and for backwards
// compatibility we will leave this here.
$sql = "SELECT * FROM insurance_companies WHERE id=?";
return sqlQuery($sql, array($id));
}
public function getOne($uuid): ProcessingResult
{
return $this->getAll(['uuid' => $uuid]);
}
public function getInsuranceTypesCached()
{
if ($this->types !== null) {
return $this->types;
}
$this->types = $this->getInsuranceTypes();
return $this->types;
}
public function getInsuranceTypes()
{
$types = [];
$type = sqlStatement("SELECT `type` FROM `insurance_type_codes`");
$i = 0;
while ($row = sqlFetchArray($type)) {
$i++;
$types[$i] = $row['type'];
}
return $types;
}
public function getInsuranceClaimTypesCached()
{
if ($this->claim_types !== null) {
return $this->claim_types;
}
$this->claim_types = $this->getInsuranceClaimTypes();
return $this->claim_types;
}
public function getInsuranceClaimTypes()
{
$claim_types = [];
$claim_type = sqlStatement("SELECT `claim_type` FROM `insurance_type_codes`");
$i = 0;
while ($row = sqlFetchArray($claim_type)) {
$i++;
$claim_types[$i] = $row['claim_type'];
}
return $claim_types;
}
public function getInsuranceCqmSopCached()
{
if ($this->cqm_sops !== null) {
return $this->cqm_sops;
}
$this->cqm_sops = $this->getInsuranceCqmSop();
return $this->cqm_sops;
}
public function getInsuranceCqmSop()
{
$cqm_sop = sqlStatement(
"SELECT distinct code, description FROM `valueset` WHERE `valueset` = '2.16.840.1.114222.4.11.3591';"
);
$cqm_sops = [];
while ($row = sqlFetchArray($cqm_sop)) {
$cqm_sops[$row['code']] = $row['description'];
}
return $cqm_sops;
}
public function insert($data)
{
// insurance companies need to use sequences table since they share the
// addresses table with pharmacies
// I don't like actually inserting a raw id... yet if we don't allow for this
// it makes it very hard for any kind of data import that needs to maintain the same id.
if (empty($data["id"])) {
$data["id"] = generate_id();
}
$freshId = $data['id'];
$sql = " INSERT INTO insurance_companies SET";
$sql .= " id=?,";
$sql .= " name=?,";
$sql .= " attn=?,";
$sql .= " cms_id=?,";
$sql .= " ins_type_code=?,";
$sql .= " x12_receiver_id=?,";
$sql .= " x12_default_partner_id=?,";
$sql .= " alt_cms_id=?,";
$sql .= " cqm_sop=?";
// throws an exception if the record doesn't insert
QueryUtils::sqlInsert(
$sql,
array(
$freshId,
$data["name"],
$data["attn"],
$data["cms_id"],
$data["ins_type_code"],
$data["x12_receiver_id"],
$data["x12_default_partner_id"] ?? '',
$data["alt_cms_id"],
$data["cqm_sop"] ?? null,
)
);
if (!empty($data["city"] ?? null) && !empty($data["state"] ?? null)) {
$this->addressService->insert($data, $freshId);
}
if (!empty($data["phone"] ?? null)) {
$this->phoneNumberService->insert($data, $freshId);
}
return $freshId;
}
public function update($data, $iid)
{
$sql = " UPDATE insurance_companies SET";
$sql .= " name=?,";
$sql .= " attn=?,";
$sql .= " cms_id=?,";
$sql .= " ins_type_code=?,";
$sql .= " x12_receiver_id=?,";
$sql .= " x12_default_partner_id=?,";
$sql .= " alt_cms_id=?,";
$sql .= " cqm_sop=?";
$sql .= " WHERE id = ?";
$insuranceResults = sqlStatement(
$sql,
array(
$data["name"],
$data["attn"],
$data["cms_id"],
$data["ins_type_code"],
$data["x12_receiver_id"],
$data["x12_default_partner_id"] ?? null,
$data["alt_cms_id"],
$data["cqm_sop"] ?? null,
$iid
)
);
if (!$insuranceResults) {
return false;
}
$addressesResults = $this->addressService->update($data, $iid);
if (!$addressesResults) {
return false;
}
// no point in updating the phone if there is no phone record...
if (!empty($data['phone'])) {
$phoneNumberResults = $this->phoneNumberService->update($data, $iid);
if (!$phoneNumberResults) {
return false;
}
}
return $iid;
}
/**
* Return an array of insurance companies with the same payer id
*
* @param $cms_id Insurance company payer id (assigned by clearinghouses)
* @return Array Insurance company data payload.
*/
public function getAllByPayerID($cms_id)
{
$insuranceCompanyResult = $this->search(['cms_id' => $cms_id]);
if ($insuranceCompanyResult->hasData()) {
$result = $insuranceCompanyResult->getData();
}
return $result;
}
}