forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsuranceCompanyService.php
312 lines (280 loc) · 11 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
<?php
/**
* InsuranceCompanyService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <matthewvita48@gmail.com>
* @author Brady Miller <brady.g.miller@gmail.com>
* @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
* @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
* @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;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Services\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;
public const TYPE_FAX = 5;
public const TYPE_WORK = 2;
/**
* Default constructor.
*/
public function __construct()
{
$this->addressService = new AddressService();
UuidRegistry::createMissingUuidsForTables([self::INSURANCE_TABLE]);
$this->insuranceCompanyValidator = new InsuranceCompanyValidator();
parent::__construct(self::INSURANCE_TABLE);
}
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 .= " i.alt_cms_id,";
$sql .= " i.inactive,work_number.id as work_id,fax_number.id AS 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.country";
$sql .= " FROM insurance_companies i ";
$sql .= " JOIN 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,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,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";
$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 .= " 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 live 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 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 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 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
$freshId = generate_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=?";
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);
}
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"],
$data["alt_cms_id"],
$data["cqm_sop"],
$iid
)
);
if (!$insuranceResults) {
return false;
}
$addressesResults = $this->addressService->update($data, $iid);
if (!$addressesResults) {
return false;
}
return $iid;
}
}