fix: ccda zip import and php warnings and deprecations (#7416)
[openemr.git] / library / amc.php
blobd36a21e3fe43fd1159d00e192155c4a30583ab20
1 <?php
3 // Copyright (C) 2011 Brady Miller <brady.g.miller@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
11 // This file contains functions to:
12 // --manage AMC items in the amc_misc_data sql table
13 // --support the AMC Tracking report
15 // Main function to process items in the amc_misc_data sql table
16 // Parameter:
17 // $amc_id - amc rule id
18 // $complete - boolean for whether to complete the date_completed row
19 // $mode - 'add' or 'remove'
20 // $patient_id - pid
21 // $object_category - specific item category (such as prescriptions, transactions etc.)
22 // $object_id - specific item id (such as encounter id, prescription id, etc.)
23 // $date_created - specifically for the uncomplete_safe mode only to ensure safe for duplicate entries.
24 function processAmcCall($amc_id, $complete, $mode, $patient_id, $object_category = '', $object_id = '0', $date_created = '')
27 // Ensure empty variables are set correctly
28 if (empty($object_category)) {
29 $object_category = '';
32 if (empty($date_created)) {
33 $date_created = '';
36 if (empty($object_id)) {
37 $object_id = '0';
40 // Ensure $complete is a boolean
41 // (since this is run via javascript/ajax, need to convert the boolean)
42 if (!($complete === true || $complete === false)) {
43 if ($complete === "true") {
44 $complete = true;
47 if ($complete === "false") {
48 $complete = false;
52 if ($mode == "add") {
53 amcAdd($amc_id, $complete, $patient_id, $object_category, $object_id);
56 if ($mode == "add_force") {
57 amcAddForce($amc_id, $complete, $patient_id, $object_category, $object_id);
58 } elseif ($mode == "remove") {
59 amcRemove($amc_id, $patient_id, $object_category, $object_id);
60 } elseif ($mode == "complete") {
61 amcComplete($amc_id, $patient_id, $object_category, $object_id);
62 } elseif ($mode == "complete_safe") {
63 amcCompleteSafe($amc_id, $patient_id, $object_category, $object_id, $date_created);
64 } elseif ($mode == "uncomplete") {
65 amcUnComplete($amc_id, $patient_id, $object_category, $object_id);
66 } elseif ($mode == "uncomplete_safe") {
67 amcUnCompleteSafe($amc_id, $patient_id, $object_category, $object_id, $date_created);
68 } elseif ($mode == "soc_provided") {
69 amcSoCProvided($amc_id, $patient_id, $object_category, $object_id);
70 } elseif ($mode == "no_soc_provided") {
71 amcNoSoCProvided($amc_id, $patient_id, $object_category, $object_id);
72 } else {
73 // do nothing
74 return;
78 // Function to add an item to the amc_misc_data sql table
79 // $amc_id - amc rule id
80 // $complete - boolean for whether to complete the date_completed row
81 // $patient_id - pid
82 // $object_category - specific item category (such as prescriptions, transactions etc.)
83 // $object_id - specific item id (such as encounter id, prescription id, etc.)
84 function amcAdd($amc_id, $complete, $patient_id, $object_category = '', $object_id = '0')
87 // Attempt to collect the item
88 $item = amcCollect($amc_id, $patient_id, $object_category, $object_id);
90 if (empty($item)) {
91 // does not yet exist, so add the item
92 $sqlBindArray = array($amc_id,$patient_id,$object_category,$object_id);
93 if ($complete) {
94 sqlStatement("INSERT INTO `amc_misc_data` (`amc_id`,`pid`,`map_category`,`map_id`,`date_created`,`date_completed`) VALUES(?,?,?,?,NOW(),NOW())", $sqlBindArray);
95 } else {
96 sqlStatement("INSERT INTO `amc_misc_data` (`amc_id`,`pid`,`map_category`,`map_id`,`date_created`) VALUES(?,?,?,?,NOW())", $sqlBindArray);
98 } else {
99 // already exist, so only ensure complete date is set (if applicable)
100 if ($complete) {
101 amcComplete($amc_id, $patient_id, $object_category, $object_id);
106 // Function to add an item to the amc_misc_data sql table
107 // This function will allow duplicates (unlike the above amcAdd function)
108 // $amc_id - amc rule id
109 // $complete - boolean for whether to complete the date_completed row
110 // $patient_id - pid
111 // $object_category - specific item category (such as prescriptions, transactions etc.)
112 // $object_id - specific item id (such as encounter id, prescription id, etc.)
113 function amcAddForce($amc_id, $complete, $patient_id, $object_category = '', $object_id = '0')
116 // add the item
117 $sqlBindArray = array($amc_id,$patient_id,$object_category,$object_id);
118 if ($complete) {
119 sqlStatement("INSERT INTO `amc_misc_data` (`amc_id`,`pid`,`map_category`,`map_id`,`date_created`,`date_completed`) VALUES(?,?,?,?,NOW(),NOW())", $sqlBindArray);
120 } else {
121 sqlStatement("INSERT INTO `amc_misc_data` (`amc_id`,`pid`,`map_category`,`map_id`,`date_created`) VALUES(?,?,?,?,NOW())", $sqlBindArray);
125 // Function to remove an item from the amc_misc_data sql table
126 // $amc_id - amc rule id
127 // $patient_id - pid
128 // $object_category - specific item category (such as prescriptions, transactions etc.)
129 // $object_id - specific item id (such as encounter id, prescription id, etc.)
130 function amcRemove($amc_id, $patient_id, $object_category = '', $object_id = '0')
132 sqlStatement("DELETE FROM `amc_misc_data` WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=?", array($amc_id,$patient_id,$object_category,$object_id));
135 // Function to complete an item from the amc_misc_data sql table
136 // $amc_id - amc rule id
137 // $patient_id - pid
138 // $object_category - specific item category (such as prescriptions, transactions etc.)
139 // $object_id - specific item id (such as encounter id, prescription id, etc.)
140 function amcComplete($amc_id, $patient_id, $object_category = '', $object_id = '0')
142 sqlStatement(
143 "UPDATE `amc_misc_data` SET `date_completed`=NOW() WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=? AND " .
144 dateEmptySql('date_completed', true),
145 array($amc_id,$patient_id,$object_category,$object_id)
149 // Function to complete an item from the amc_misc_data sql table
150 // As opposed to above function, this is safe for when expect to have duplicate entries
151 // $amc_id - amc rule id
152 // $patient_id - pid
153 // $object_category - specific item category (such as prescriptions, transactions etc.)
154 // $object_id - specific item id (such as encounter id, prescription id, etc.)
155 // $date_created - date created.
156 function amcCompleteSafe($amc_id, $patient_id, $object_category = '', $object_id = '0', $date_created = '')
158 sqlStatement("UPDATE `amc_misc_data` SET `date_completed`=NOW() WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=? AND" .
159 dateEmptySql('date_completed', true) .
160 "AND `date_created`=?", array($amc_id,$patient_id,$object_category,$object_id,$date_created));
163 // Function to remove completion date/flag from an item in the amc_misc_data sql table
164 // $amc_id - amc rule id
165 // $patient_id - pid
166 // $object_category - specific item category (such as prescriptions, transactions etc.)
167 // $object_id - specific item id (such as encounter id, prescription id, etc.)
168 function amcUnComplete($amc_id, $patient_id, $object_category = '', $object_id = '0')
170 sqlStatement("UPDATE `amc_misc_data` SET `date_completed`=NULL WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=?", array($amc_id,$patient_id,$object_category,$object_id));
173 // Function to remove completion date/flag from an item in the amc_misc_data sql table
174 // As opposed to above function, this is safe for when expect to have duplicate entries
175 // $amc_id - amc rule id
176 // $patient_id - pid
177 // $object_category - specific item category (such as prescriptions, transactions etc.)
178 // $object_id - specific item id (such as encounter id, prescription id, etc.)
179 // $date_created - date created.
180 function amcUnCompleteSafe($amc_id, $patient_id, $object_category = '', $object_id = '0', $date_created = '')
182 sqlStatement("UPDATE `amc_misc_data` SET `date_completed`=NULL WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=? AND `date_created`=?", array($amc_id,$patient_id,$object_category,$object_id,$date_created));
185 // Function to complete an item from the amc_misc_data sql table
186 // $amc_id - amc rule id
187 // $patient_id - pid
188 // $object_category - specific item category (such as prescriptions, transactions etc.)
189 // $object_id - specific item id (such as encounter id, prescription id, etc.)
190 function amcCollect($amc_id, $patient_id, $object_category = '', $object_id = '0')
192 return sqlQuery("SELECT * FROM `amc_misc_data` WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=?", array($amc_id,$patient_id,$object_category,$object_id));
195 // Function to support the AMC tracking report
196 // $amc_id - amc rule id
197 // $start - date start range
198 // $end - date end range
199 // $provider_id - provider id
200 function amcTrackingRequest($amc_id, $start = '', $end = '', $provider_id = '')
202 $where = '';
204 # Collect the patient list first (from the provider)
205 $patients = array();
206 if (empty($provider)) {
207 // Look at entire practice
208 $rez = sqlStatement("SELECT `pid`, `fname`, `lname` FROM `patient_data`");
209 for ($iter = 0; $row = sqlFetchArray($rez); $iter++) {
210 $patients[$iter] = $row;
212 } else {
213 // Look at one provider
214 $rez = sqlStatement("SELECT `pid`, `fname`, `lname` FROM `patient_data` " .
215 "WHERE providerID=?", array($provider));
216 for ($iter = 0; $row = sqlFetchArray($rez); $iter++) {
217 $patients[$iter] = $row;
221 $results = array();
222 foreach ($patients as $patient) {
223 $tempResults = array();
225 if ($amc_id == "send_sum_amc") {
226 $sqlBindArray = array();
227 array_push($sqlBindArray, $patient['pid']);
228 if (!(empty($start))) {
229 $where = " AND `date`>=? ";
230 array_push($sqlBindArray, $start);
233 if (!(empty($end))) {
234 $where .= " AND `date`<=? ";
235 array_push($sqlBindArray, $end);
238 $rez = sqlStatement("SELECT `id`, `date` FROM `transactions` WHERE `title` = 'LBTref' AND `pid` = ? $where ORDER BY `date` DESC", $sqlBindArray);
239 while ($res = sqlFetchArray($rez)) {
240 $amcCheck = amcCollect("send_sum_amc", $patient['pid'], "transactions", $res['id']);
241 if (empty($amcCheck)) {
242 // Records have not been sent, so send this back
243 array_push($tempResults, array("pid" => $patient['pid'], "fname" => $patient['fname'], "lname" => $patient['lname'], "date" => $res['date'], "id" => $res['id']));
246 } elseif ($amc_id == "provide_rec_pat_amc") {
247 $sqlBindArray = array();
248 array_push($sqlBindArray, $patient['pid']);
249 if (!(empty($start))) {
250 $where = " AND `date_created`>=? ";
251 array_push($sqlBindArray, $start);
254 if (!(empty($end))) {
255 $where .= " AND `date_created`<=? ";
256 array_push($sqlBindArray, $end);
259 $rez = sqlStatement("SELECT * FROM `amc_misc_data` WHERE `amc_id`='provide_rec_pat_amc' AND `pid`=? AND " .
260 dateEmptySql('date_completed', true) .
261 "$where ORDER BY `date_created` DESC", $sqlBindArray);
262 while ($res = sqlFetchArray($rez)) {
263 // Records have not been sent, so send this back
264 array_push($tempResults, array("pid" => $patient['pid'], "fname" => $patient['fname'], "lname" => $patient['lname'], "date" => $res['date_created']));
266 } elseif ($amc_id == "provide_sum_pat_amc") {
267 $sqlBindArray = array();
268 array_push($sqlBindArray, $patient['pid']);
269 if (!(empty($start))) {
270 $where = " AND `date`>=? ";
271 array_push($sqlBindArray, $start);
274 if (!(empty($end))) {
275 $where .= " AND `date`<=? ";
276 array_push($sqlBindArray, $end);
279 $rez = sqlStatement("SELECT `encounter`, `date` FROM `form_encounter` WHERE `pid`=? $where ORDER BY `date` DESC", $sqlBindArray);
280 while ($res = sqlFetchArray($rez)) {
281 $amcCheck = amcCollect("provide_sum_pat_amc", $patient['pid'], "form_encounter", $res['encounter']);
282 if (empty($amcCheck)) {
283 // Records have not been given, so send this back
284 array_push($tempResults, array("pid" => $patient['pid'], "fname" => $patient['fname'], "lname" => $patient['lname'], "date" => $res['date'], "id" => $res['encounter']));
287 } else {
288 // report nothing
289 return;
292 // process results
293 $results = array_merge($results, $tempResults);
296 // send results
297 return $results;
300 // The function returns the no. of business days between two dates and it skips the holidays
301 // $start in YYYY-MM-DD
302 // $end in YYYY-MM-DD
303 // $holiday is an array containing YYYY-MM-DD
304 function businessDaysDifference($startDate, $endDate, $holidays = array())
306 //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
307 //We add one to include both dates in the interval.
308 $days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
310 $no_full_weeks = floor($days / 7);
311 $no_remaining_days = fmod($days, 7);
313 //It will return 1 if it's Monday,.. ,7 for Sunday
314 $the_first_day_of_week = date("N", strtotime($startDate));
315 $the_last_day_of_week = date("N", strtotime($endDate));
317 //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
318 //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
319 if ($the_first_day_of_week <= $the_last_day_of_week) {
320 if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) {
321 $no_remaining_days--;
324 if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) {
325 $no_remaining_days--;
327 } else {
328 // (edit by Tokes to fix an edge case where the start day was a Sunday
329 // and the end day was NOT a Saturday)
331 // the day of the week for start is later than the day of the week for end
332 if ($the_first_day_of_week == 7) {
333 // if the start date is a Sunday, then we definitely subtract 1 day
334 $no_remaining_days--;
336 if ($the_last_day_of_week == 6) {
337 // if the end date is a Saturday, then we subtract another day
338 $no_remaining_days--;
340 } else {
341 // the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
342 // so we skip an entire weekend and subtract 2 days
343 $no_remaining_days -= 2;
347 //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
348 //---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
349 $workingDays = $no_full_weeks * 5;
350 if ($no_remaining_days > 0) {
351 $workingDays += $no_remaining_days;
354 //We subtract the holidays
355 foreach ($holidays as $holiday) {
356 $time_stamp = strtotime($holiday);
357 //If the holiday doesn't fall in weekend
358 if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate) && date("N", $time_stamp) != 6 && date("N", $time_stamp) != 7) {
359 $workingDays--;
363 return $workingDays;
366 // Function to set summary of care provided for a encounter/patient from the amc_misc_data sql table
367 // $amc_id - amc rule id
368 // $patient_id - pid
369 // $object_category - specific item category (such as prescriptions, transactions etc.)
370 // $object_id - specific item id (such as encounter id, prescription id, etc.)
371 function amcSoCProvided($amc_id, $patient_id, $object_category = '', $object_id = '0')
373 sqlStatement("UPDATE `amc_misc_data` SET `soc_provided` = NOW() WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=? ", array($amc_id,$patient_id,$object_category,$object_id));
375 // Function to set summary of care provided for a encounter/patient from the amc_misc_data sql table
376 // $amc_id - amc rule id
377 // $patient_id - pid
378 // $object_category - specific item category (such as prescriptions, transactions etc.)
379 // $object_id - specific item id (such as encounter id, prescription id, etc.)
380 function amcNoSoCProvided($amc_id, $patient_id, $object_category = '', $object_id = '0')
382 sqlStatement("UPDATE `amc_misc_data` SET `soc_provided` = NULL WHERE `amc_id`=? AND `pid`=? AND `map_category`=? AND `map_id`=? ", array($amc_id,$patient_id,$object_category,$object_id));