fix: set default x12 partner for item in billing manager (#7513)
[openemr.git] / interface / forms / group_attendance / functions.php
blob345e3c0bbdeb8d3b6b06ed996c61b683ee906952
1 <?php
3 /**
4 * interface/forms/group_attendance/functions.php functions for form
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Shachar Zilbershlag <shaharzi@matrix.co.il>
9 * @author Amiel Elboim <amielel@matrix.co.il>
10 * @copyright Copyright (c) 2016 Shachar Zilbershlag <shaharzi@matrix.co.il>
11 * @copyright Copyright (c) 2016 Amiel Elboim <amielel@matrix.co.il>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once(dirname(__FILE__) . "/../../../library/api.inc.php");
16 require_once(dirname(__FILE__) . "/../../../library/forms.inc.php");
17 require_once(dirname(__FILE__) . "/../../../library/patient_tracker.inc.php");
19 /**
20 * Returns form_id of an existing attendance form for group encounter (if one already exists);
21 * @param $encounter
22 * @param $group_id
23 * @return array|null
25 function get_form_id_of_existing_attendance_form($encounter, $group_id)
27 $sql = "SELECT form_id FROM forms WHERE encounter = ? AND formdir = 'group_attendance' AND therapy_group_id = ? AND deleted = 0;";
28 $result = sqlQuery($sql, array($encounter, $group_id));
29 return $result;
32 /**
33 * Inserts participant data into DB
34 * @param $form_id
35 * @param $therapy_group
36 * @param $group_encounter_data
37 * @param $appt_data
39 function participant_insertions($form_id, $therapy_group, $group_encounter_data, $appt_data)
41 $patientData = $_POST['patientData'];
42 foreach ($patientData as $pid => $patient) {
43 //Insert into therapy_groups_participants_attendance table
44 insert_into_tgpa_table($form_id, $pid, $patient);
46 //Check if to create appt and encounter for each patient (if has certain status and 'bottom' submit was pressed, not 'add_patient' submit).
47 $create_for_patient = if_to_create_for_patient($patient['status']);
48 if ($create_for_patient) {
49 //Create encounter for each patient
50 $encounter_id = insert_patient_encounter($pid, $therapy_group, $group_encounter_data['date'], $patient, $appt_data['pc_aid']);
52 //Create appt for each patient (if there is appointment connected to encounter)
53 if (!empty($appt_data)) {
54 $pc_eid = insert_patient_appt($pid, $therapy_group, $appt_data['pc_aid'], $appt_data['pc_eventDate'], $appt_data['pc_startTime'], $patient);
55 manage_tracker_status($appt_data['pc_eventDate'], $appt_data['pc_startTime'], $pc_eid, $pid, $appt_data['pc_aid'], $patient['status'], $appt_data['pc_room'], $encounter_id);
61 /**
62 * Inserts data into therapy_groups_participant_attendance table
63 * @param $form_id
64 * @param $pid
65 * @param $participantData
67 function insert_into_tgpa_table($form_id, $pid, $participantData)
70 $sql_for_table_tgpa = "INSERT INTO therapy_groups_participant_attendance (form_id, pid, meeting_patient_comment, meeting_patient_status) " .
71 "VALUES(?,?,?,?);";
72 sqlStatement($sql_for_table_tgpa, array($form_id, $pid, $participantData['comment'], $participantData['status']));
75 /**
76 * Creates an appointment for patient from attendance form
77 * @param $pid
78 * @param $gid
79 * @param $pc_aid
80 * @param $pc_eventDate
81 * @param $pc_startTime
82 * @param $participantData
84 function insert_patient_appt($pid, $gid, $pc_aid, $pc_eventDate, $pc_startTime, $participantData)
86 $select_sql = "SELECT pc_eid FROM openemr_postcalendar_events WHERE pc_pid = ? AND pc_gid = ? AND pc_eventDate = ? AND pc_startTime = ?;";
87 $result = sqlStatement($select_sql, array($pid, $gid, $pc_eventDate, $pc_startTime));
88 $result_array = sqlFetchArray($result);
89 if ($result_array) {
90 $insert_sql = "UPDATE openemr_postcalendar_events SET pc_apptstatus = ? WHERE pc_eid = ?;";
91 sqlStatement($insert_sql, array($participantData['status'], $result_array['pc_eid']));
92 return $result_array['pc_eid'];
93 } else {
94 $insert_sql =
95 "INSERT INTO openemr_postcalendar_events " .
96 "(pc_catid, pc_aid, pc_pid, pc_gid, pc_title, pc_informant, pc_eventDate, pc_recurrspec, pc_startTime, pc_sharing, pc_apptstatus) " .
97 "VALUES (?, ?, ?, ?, 'Group Therapy', 1, ?, ?, ?, 0, ?); ";
98 $recurrspec = 'a:6:{s:17:"event_repeat_freq";s:1:"0";s:22:"event_repeat_freq_type";s:1:"0";s:19:"event_repeat_on_num";s:1:"1";s:19:"event_repeat_on_day";s:1:"0";s:20:"event_repeat_on_freq";s:1:"0";s:6:"exdate";s:0:"";}';
99 $sqlBindArray = array();
100 array_push($sqlBindArray, get_groups_cat_id(), $pc_aid, $pid, $gid, $pc_eventDate, $recurrspec, $pc_startTime, $participantData['status']);
101 $pc_eid = sqlInsert($insert_sql, $sqlBindArray);
102 return $pc_eid;
107 * Creates an encounter for patient from attendance form
108 * @param $pid
109 * @param $gid
110 * @param $group_encounter_date
111 * @param $participantData
112 * @param $pc_aid
114 function insert_patient_encounter($pid, $gid, $group_encounter_date, $participantData, $pc_aid)
116 $select_sql = "SELECT id, encounter FROM form_encounter WHERE pid = ? AND external_id = ? AND pc_catid = ? AND date = ?; ";
117 $result = sqlStatement($select_sql, array($pid, $gid, get_groups_cat_id(), $group_encounter_date));
118 $result_array = sqlFetchArray($result);
119 if ($result_array) {
120 $insert_sql = "UPDATE form_encounter SET reason = ? WHERE id = ?;";
121 sqlStatement($insert_sql, array($participantData['comment'], $result_array['id']));
122 return $result_array['encounter'];
123 } else {
124 $insert_encounter_sql =
125 "INSERT INTO form_encounter (date, reason, pid, encounter, pc_catid, provider_id, external_id) " .
126 "VALUES (?, ?, ?, ?, ?, ?, ?);";
127 $enc_id = generate_id();
128 $sqlBindArray = array();
129 $user = (is_null($pc_aid)) ? $_SESSION['authUserID'] : $pc_aid;
130 array_push($sqlBindArray, $group_encounter_date, $participantData['comment'], $pid, $enc_id, get_groups_cat_id(), $user, $gid);
131 $form_id = sqlInsert($insert_encounter_sql, $sqlBindArray);
133 global $userauthorized;
135 addForm($enc_id, "New Patient Encounter", $form_id, "newpatient", $pid, $userauthorized, $group_encounter_date, '', '', null);
137 return $enc_id;
142 * If the group encounter was created in relation to a group appointment, fetches the appointment relevant data.
143 * @param $encounter_id
144 * @return array
146 function get_appt_data($encounter_id)
148 $sql =
149 "SELECT ope.pc_aid, ope.pc_eventDate, ope.pc_startTime, ope.pc_room FROM form_groups_encounter as fge " .
150 "JOIN openemr_postcalendar_events as ope ON fge.appt_id = ope.pc_eid " .
151 "WHERE fge.encounter = ?;";
152 $result = sqlQuery($sql, array($encounter_id));
153 return $result;
156 function getGroupAttendance($form_id): array
158 $participants_sql = "SELECT tgpa.*, p.fname, p.lname " .
159 "FROM therapy_groups_participant_attendance as tgpa " .
160 "JOIN patient_data as p ON tgpa.pid = p.id " .
161 "WHERE tgpa.form_id = ?;";
162 $result = sqlStatement($participants_sql, array($form_id));
163 $participants = array();
164 while ($p = sqlFetchArray($result)) {
165 $participants[] = $p;
167 return $participants;
170 * Gets group encounter data
171 * @param $encounter_id
172 * @return array
174 function get_group_encounter_data($encounter_id)
176 $sql = "SELECT date FROM form_groups_encounter WHERE encounter = ?";
177 $result = sqlQuery($sql, array($encounter_id));
178 return $result;
182 * Checks if to create appointment and encounter for patient himself based on the status in the attendance form.
183 * [Note: `toggle_setting_1` in table `list_options` is used as a flag to know the statuses for which an appt or encounter should be created.]
184 * @param $status
185 * @return bool
187 function if_to_create_for_patient($status)
189 $sql = 'SELECT toggle_setting_1 FROM list_options WHERE list_id = \'attendstat\' AND toggle_setting_1 = 1 AND option_id = ?';
190 $to_create = sqlQuery($sql, array($status));
191 return $to_create;
194 function getAttendanceStatus($status)
196 $sql = 'SELECT title FROM list_options WHERE list_id = \'attendstat\' AND option_id = ?';
197 $result = sqlQuery($sql, array($status));
198 return $result['title'];
202 * Returns the number after the greatest id number in the table
203 * @param $table
204 * @return int
206 function largest_id_plus_one($table)
208 $maxId = largest_id($table);
209 if ($maxId) {
210 $newid = $maxId + 1;
211 } else {
212 $newid = 1;
215 return $newid;
219 * Returns greatest id number in the table
220 * @param $table
221 * @return mixed
223 function largest_id($table)
225 $res = sqlStatement("SELECT MAX(id) as largestId FROM `" . escape_table_name($table) . "`");
226 $getMaxid = sqlFetchArray($res);
227 return $getMaxid['largestId'];
231 function get_groups_cat_id()
233 $result = sqlQuery('SELECT pc_catid FROM openemr_postcalendar_categories WHERE pc_cattype = 3 AND pc_active = 1 LIMIT 1');
234 return !empty($result) ? $result['pc_catid'] : 0;