forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroupService.php
141 lines (128 loc) · 5.86 KB
/
GroupService.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
<?php
/**
* GroupService.php
* @package openemr
* @link http://www.open-emr.org
* @author Stephen Nielson <stephen@nielson.org>
* @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
* @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\Uuid\UuidMapping;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Validators\ProcessingResult;
class GroupService extends BaseService
{
const PRACTITIONER_TABLE = "users";
/**
* Default constructor.
*/
public function __construct()
{
UuidRegistry::createMissingUuidsForTables([self::PRACTITIONER_TABLE]);
UuidMapping::createMissingResourceUuids("Group", self::PRACTITIONER_TABLE);
parent::__construct(self::PRACTITIONER_TABLE);
}
public function getUuidFields(): array
{
return ['uuid', 'puuid'];
}
/**
* Searches for patient provider groups
* @param array $search
* @param bool $isAndCondition
* @return ProcessingResult
*/
public function searchPatientProviderGroups($search = array(), $isAndCondition = true)
{
// we inner join on status in case we ever decide to add a status property (and layers above this one can rely
// on the property without changing code).
$sql = "SELECT
patient_provider_groups.uuid
,patient_provider_groups.provider_id
,patient_provider_groups.provider_fname
,patient_provider_groups.provider_mname
,patient_provider_groups.provider_lname
,patient_provider_groups.puuid
,patient_provider_groups.patient_title
,patient_provider_groups.patient_fname
,patient_provider_groups.patient_mname
,patient_provider_groups.patient_lname
FROM (
SELECT
uuid_mapping.target_uuid AS pruuid
,uuid_mapping.uuid
,users.id AS provider_id
,users.fname AS provider_fname
,users.lname AS provider_lname
,users.mname AS provider_mname
,patients.uuid AS puuid
,patients.title AS patient_title
,patients.fname AS patient_fname
,patients.mname AS patient_mname
,patients.lname AS patient_lname
FROM
uuid_mapping
JOIN
users ON uuid_mapping.target_uuid = users.uuid
LEFT JOIN patient_data AS patients ON patients.providerID = users.id
WHERE users.npi IS NOT NULL and users.npi != '' AND uuid_mapping.resource = 'Group'
) patient_provider_groups ";
$whereClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereClause->getFragment();
$sqlBindArray = $whereClause->getBoundValues();
$statementResults = QueryUtils::sqlStatementThrowException($sql, $sqlBindArray);
$processingResult = $this->hydratePatientProviderSearchResultsFromQueryResource($statementResults);
return $processingResult;
}
private function hydratePatientProviderSearchResultsFromQueryResource($queryResource)
{
$processingResult = new ProcessingResult();
$recordsByUuid = [];
$recordFields = array_combine($this->getFields(), $this->getFields());
$previousNameColumns = ['previous_name_prefix', 'previous_name_first', 'previous_name_middle'
, 'previous_name_last', 'previous_name_suffix', 'previous_name_enddate'];
$previousNamesFields = array_combine($previousNameColumns, $previousNameColumns);
$orderedList = [];
while ($row = sqlFetchArray($queryResource)) {
$dbRecord = $this->createResultRecordFromDatabaseResult($row);
$recordUuid = $dbRecord['uuid'];
if (!isset($recordsByUuid[$recordUuid])) {
$providerName = $dbRecord['provider_fname'] ?? "";
$providerName .= !empty($dbRecord['provider_mname']) ? " " . $dbRecord['provider_mname'] : "";
$providerName .= !empty($dbRecord['provider_lname']) ? " " . $dbRecord['provider_lname' ] : "";
if (empty($providerName)) {
$providerName = "(Provider Name Unknown)";
}
$groupName = $providerName . " " . xl("Patients");
$record = [
'uuid' => $recordUuid
,'name' => $groupName
,'patients' => []
];
$orderedList[] = $recordUuid;
} else {
$record = $recordsByUuid[$recordUuid];
}
if (!empty($dbRecord['puuid'])) {
$patientName = $dbRecord['patient_title'] ?? "";
$patientName .= !empty($dbRecord['patient_fname']) ? " " . $dbRecord['patient_fname'] : "";
$patientName .= !empty($dbRecord['patient_mname']) ? " " . $dbRecord['patient_mname'] : "";
$patientName .= !empty($dbRecord['patient_lname']) ? " " . $dbRecord['patient_lname'] : "";
$record['patients'][] = [
'uuid' => $dbRecord['puuid']
,'name' => $patientName
];
}
// now let's grab our history
$recordsByUuid[$recordUuid] = $record;
}
foreach ($orderedList as $uuid) {
$patient = $recordsByUuid[$uuid];
$processingResult->addData($patient);
}
return $processingResult;
}
}