4 * Handles the interactions with the patient_history table.
7 * @link http://www.open-emr.org
9 * @author Jerry Padgett <sjpadgett@gmail.com>
10 * @author Stephen Nielson <snielson@discoverandchange.com>
11 * @copyright Copyright (c) 2024 Care Management Solutions, Inc. <stephen.waite@cmsvt.com>
12 * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 namespace OpenEMR\Services
;
18 use OpenEMR\Common\Database\QueryUtils
;
19 use OpenEMR\Common\Logging\SystemLogger
;
20 use OpenEMR\Common\Uuid\UuidRegistry
;
21 use OpenEMR\Services\PatientService
;
23 class PatientNameHistoryService
extends BaseService
25 public const TABLE_NAME
= "patient_history";
26 public function __construct()
28 // note the constant for this table is PRIVATE and should either be protected or PUBLIC
29 parent
::__construct(self
::TABLE_NAME
);
32 public function deletePatientNameHistoryById($id)
34 $sql = "DELETE FROM patient_history WHERE id = ?";
35 return sqlQuery($sql, array($id));
39 * Create a previous patient name history
40 * Updates not allowed for this history feature.
42 * @param string $pid patient internal id
43 * @param array $record array values to insert
44 * @return int | false new id or false if name already exist
46 public function createPatientNameHistory($pid, $record)
48 // we should never be null here but for legacy reasons we are going to default to this
49 $createdBy = $_SESSION['authUserID'] ??
null; // we don't let anyone else but the current user be the createdBy
55 'history_type_key' => 'name_history',
56 'previous_name_prefix' => $record['previous_name_prefix'],
57 'previous_name_first' => $record['previous_name_first'],
58 'previous_name_middle' => $record['previous_name_middle'],
59 'previous_name_last' => $record['previous_name_last'],
60 'previous_name_suffix' => $record['previous_name_suffix'],
61 'previous_name_enddate' => $record['previous_name_enddate']
63 $sql = "SELECT pid FROM " . self
::TABLE_NAME
. " WHERE
65 history_type_key = ? AND
66 previous_name_prefix = ? AND
67 previous_name_first = ? AND
68 previous_name_middle = ? AND
69 previous_name_last = ? AND
70 previous_name_suffix = ? AND
71 previous_name_enddate = ?
73 $go_flag = QueryUtils
::fetchSingleValue($sql, 'pid', $insertData);
74 // return false which calling routine should understand as existing name record
75 if (!empty($go_flag)) {
78 // finish up the insert
79 $insertData['created_by'] = $createdBy;
80 $insertData['uuid'] = UuidRegistry
::getRegistryForTable(self
::TABLE_NAME
)->createUuid();
81 $insert = $this->buildInsertColumns($insertData);
82 $sql = "INSERT INTO " . self
::TABLE_NAME
. " SET " . $insert['set'];
84 return QueryUtils
::sqlInsert($sql, $insert['bind']);
88 public static function formatPreviousName($item)
91 $item['previous_name_enddate'] === '0000-00-00'
92 ||
$item['previous_name_enddate'] === '00/00/0000'
94 $item['previous_name_enddate'] = '';
96 $item['previous_name_enddate'] = oeFormatShortDate($item['previous_name_enddate']);
97 $name = ($item['previous_name_prefix'] ?
$item['previous_name_prefix'] . " " : "") .
98 $item['previous_name_first'] .
99 ($item['previous_name_middle'] ?
" " . $item['previous_name_middle'] . " " : " ") .
100 $item['previous_name_last'] .
101 ($item['previous_name_suffix'] ?
" " . $item['previous_name_suffix'] : "") .
102 ($item['previous_name_enddate'] ?
" " . $item['previous_name_enddate'] : "");
108 public function getPatientNameHistory($pid)
112 previous_name_prefix,
114 previous_name_middle,
116 previous_name_suffix,
117 previous_name_enddate
119 WHERE pid = ? AND history_type_key = ?";
120 $results = QueryUtils
::sqlStatementThrowException($sql, array($pid, 'name_history'));
122 while ($row = sqlFetchArray($results)) {
123 $row['formatted_name'] = $this->formatPreviousName($row);
131 public function getPatientNameHistoryById($pid, $id)
135 previous_name_prefix,
137 previous_name_middle,
139 previous_name_suffix,
140 previous_name_enddate
142 WHERE pid = ? AND id = ? AND history_type_key = ?";
143 $result = sqlQuery($sql, array($pid, $id, 'name_history'));
144 $result['formatted_name'] = $this->formatPreviousName($result);