Simplified code to generate img tag rather than iframe.
[openemr.git] / library / DBC_cfunctions.php
blobcf33ae68716e78ba701a6d81d0719413922722dd
1 <?php
2 /**
3 * COMMON DBC DUTCH SYSTEM
4 * several functions used in DBC
5 *
6 * NOTE: these are functions that are used in DBC without the need of httpd server
7 * in other words, the function can be used by scripts (e.g. cron scripts) w/out
8 * requiring sessions or something else
9 *
10 * @author Cristian NAVALICI
11 * @version 1.0
14 require_once(dirname(__FILE__) . '/sql.inc');
17 //-----------------------------------------------------------------------------
18 /**
19 * RETURN AGE OF THE DBC
21 * return the number of days between opening date and a specified date
22 * if not specified, we use current date
24 * @param int $dbcid - id for dbc
25 * @param string $cdate - closing date (mysql form YYYY-MM-DD)
26 * @return int days
28 function df_dbc_age($dbcid, $cdate = 0){
29 if ( !$dbcid ) return FALSE;
31 if ( !$cdate ) $cdate = date('Y-m-d');
33 // if date is not in the valid form, then use the current
34 $ard = split('-', $cdate);
35 $y = (int)$ard[0]; $m = $ard[1]; $d = $ard[2];
36 if ( !checkdate($m, $d, $y) ) $cdate = date('Y-m-d');
38 $dia = content_diagnose($dbcid);
39 $odate = $dia['ax_odate'];
41 $difference = (strtotime($cdate) - strtotime($odate))/(24*60*60);
43 return (int)$difference;
47 //-----------------------------------------------------------------------------
48 /**
49 * CONTENT FOR A SPECIFIED DBC
51 * @param int $axid - id for diagnose
52 * @return array - contains all info for a diagnose
54 function content_diagnose($dbcid = 0){
55 if ( !$dbcid ) return FALSE;
57 $qc = sprintf("SELECT * FROM cl_axes WHERE ax_id = %d ", $dbcid);
58 $rez = mysql_query($qc) or die(mysql_error());
59 return mysql_fetch_array($rez);
62 //-----------------------------------------------------------------------------
63 /**
64 * DUPLICATE DBC
66 * closes a dbc (modify the flag) and open a new one with the same content
67 * THE CLOSING OPERATION ITSELF IS NOT DONE BY THIS! (use close_dbc with $follow = 1)
68 * for the new one, the opening date will be one day ahead (due of a restrain in validation stuff)
70 * @param array|int $dbc - old dbc | $dbcid
71 * @return int $dbc id - new id
73 function duplicate_dbc($dbc = 0) {
75 if ( !$dbc ) return FALSE;
77 // if it's integer, than obtain the content
78 if ( !is_array($dbc) ) {
79 $dbc = content_diagnose($dbc);
82 mysql_query("START TRANSACTION");
84 $cdate1 = ( $_SESSION['eind'] ) ? $_SESSION['eind'] : date('Y-m-d');
85 $cdate2 = '9999-12-31'; // mysql default
87 $odate1 = $dbc['ax_odate'];
88 $odate2 = date ('Y-m-d', (strtotime($cdate1) + 86400)); // one day ahead
91 // insert a new one
92 $qi = sprintf("INSERT INTO cl_axes (ax_ztn, ax_open, ax_as1, ax_as2, ax_as3, ax_as4, ax_as5, ax_odate, ax_cdate, ax_sti)
93 VALUES ('%s', %d,'%s','%s','%s','%s','%s','%s','%s','%s')",
94 $dbc['ax_ztn'],
95 1,
96 $dbc['ax_as1'],
97 $dbc['ax_as2'],
98 $dbc['ax_as3'],
99 $dbc['ax_as4'],
100 $dbc['ax_as5'],
101 $odate2,
102 $cdate2,
104 mysql_query($qi) or die (msqyl_error());
105 //echo "$qi \n";
106 $newid = mysql_insert_id();
108 // =====================
109 // close the old one
110 $qu = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s' WHERE ax_id = %d", $cdate1, $dbc['ax_id']);
111 mysql_query($qu) or die(mysql_error());
112 //echo "$qu \n";
114 // update the related tables (cl_circuit_dbc)
115 $qc = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d ", $dbc['ax_id']);
116 $rc = mysql_query($qc) or die(mysql_error());
117 $circuit = mysql_fetch_array($rc);
119 $qdc = sprintf("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES (%d, %d)", $circuit['ccd_circuitcode'], $newid);
120 mysql_query($qdc) or die(mysql_error());
122 mysql_query("COMMIT");
124 return $newid;
128 //-----------------------------------------------------------------------------
130 * CRON LOG
132 * logs an event in cron functions
134 * @param string - string to be written
135 * @return void
137 function df_cronlog($string){
138 $file = '/tmp/DBC_cron.log';
139 if ( !$h = fopen($file, 'ab') ) {
140 echo "Cannot create file ($filename)";
141 exit;
144 $content = date('d-m-Y H:i') . " $string \r\n";
146 // WRITE DATA TO FILE
147 if ( fwrite($h, $content) === FALSE ) {
148 echo "Cannot write to file ($filename)";
149 exit;
152 fclose($h);
156 //-----------------------------------------------------------------------------
158 * FIND THE PATIENT FOR A GIVEN DBC / GIVEN ZTN
160 * @param int $dbcid
161 * @param int $ztnid
162 * @return int patientid
164 function what_patient($dbcid = 0, $ztnid = 0) {
165 if ( !$dbcid && !$ztnid ) return FALSE;
167 if ( $dbcid ) {
168 $q = sprintf("SELECT cn_pid FROM cl_careroute_numbers ccn JOIN cl_axes ca ON ccn.cn_ztn = ca.ax_ztn
169 WHERE ca.ax_id = %d ", $dbcid);
170 } else {
171 $q = sprintf("SELECT cn_pid FROM cl_careroute_numbers WHERE cn_ztn = '%s'", $ztn);
174 $r = mysql_query($q) or die(mysql_error());
175 if ( mysql_num_rows($r) ) {
176 $row = mysql_fetch_array($r);
177 return $row['cn_pid'];
178 } else {
179 return 0;
184 //-----------------------------------------------------------------------------
186 * DBC GET MAIN DIAGNOSE
188 * return main diagnose for a given dbc
190 * @param int $dbcid
191 * @return string - the code for the diagnose (as*)
193 function df_get_main_diagnose($dbcid = 0){
194 if ( !$dbcid ) return 0;
196 $dbc = content_diagnose($dbcid);
197 $as1 = unserialize($dbc['ax_as1']);
198 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
199 $as2 = unserialize($dbc['ax_as2']);
200 $as2c = $as2['content'];
202 // look for the main diagnose
203 $counter = 1; $mainstr = '';
204 foreach ( $as1c as $a) {
205 if ( $counter == $mainpos ) $mainstr = $a;
206 $counter++;
209 // if it's not in the first array, we look further for it
210 if ( !$mainstr ) {
211 foreach ( $as2c as $a) {
212 if ( $counter == $mainpos ) $mainstr = $a['code'];
213 $counter++;
217 return $mainstr;
220 //-----------------------------------------------------------------------------
222 * GET THE LAST ENCOUNTER FOR A PID
224 * gets the last encounter for a patient; if month and year are provided,
225 * compare if the last enc is before this date and return the comparision result
227 * @param int $pid - patient id
228 * @return array (date => date itself, bool => TRUE|FALSE)
230 function last_encounter($pid, $month = '01', $year = '2008') {
231 $q = sprintf("SELECT MAX(pc_eventDate) AS maxdate FROM openemr_postcalendar_events
232 WHERE pc_pid = %d AND pc_apptstatus = '@'", $pid);
234 $r = mysql_query($q) or die(mysql_error());
236 while ( $row = mysql_fetch_array($r) ) {
237 $lastenc = $row['maxdate'];
238 if ( $lastenc ) $result['bool'] = ( $lastenc <= "$year-$month-31" ); else $result['bool'] = FALSE;
239 $result['date'] = $lastenc;
242 return $result;
247 //-----------------------------------------------------------------------------
249 * THERE ARE ANY FUTURE EVENTS?
251 * finds out if there are future events for a patient after a specified date
253 * @param int $pid - patient id
254 * @param string $date - date
255 * @return string - the last event
257 function future_events($pid, $date) {
258 $q = sprintf("SELECT MAX(pc_eventDate) AS maxdate FROM openemr_postcalendar_events
259 WHERE pc_pid = %d AND pc_eventDate >= '%s'", $pid, $date);
260 $r = mysql_query($q) or die(mysql_error());
262 if ( mysql_num_rows($r) ) {
263 $row = mysql_fetch_array($r);
264 return $row['maxdate'];
265 } else return 0;
267 //-----------------------------------------------------------------------------