3 * COMMON DBC DUTCH SYSTEM
4 * several functions used in DBC
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
10 * @author Cristian NAVALICI
14 require_once(dirname(__FILE__
) . '/sql.inc');
17 //-----------------------------------------------------------------------------
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)
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
66 * close a dbc and open a new one with the same content
67 * for the new one, the opening date will be one day ahead (due of a restrain in validation stuff)
69 * @param array|int $dbc - old dbc | $dbcid
70 * @return int $dbc id - new id
72 function duplicate_dbc($dbc = 0) {
74 if ( !$dbc ) return FALSE;
76 // if it's integer, than obtain the content
77 if ( !is_array($dbc) ) {
78 $dbc = content_diagnose($dbc);
81 mysql_query("START TRANSACTION");
83 $odate1 = ( $_SESSION['eind'] ) ?
$_SESSION['eind'] : date('Y-m-d');
84 $odate2 = date ('Y-m-d', (strtotime($odate1) +
86400)); // one day ahead
85 $cdate = '9999-12-31'; // mysql default
88 $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)
89 VALUES ('%s', %d,'%s','%s','%s','%s','%s','%s','%s','%s')",
100 mysql_query($qi) or die (msqyl_error());
102 $newid = mysql_insert_id();
105 $qu = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s' WHERE ax_id = %d", $odate1, $dbc['ax_id']);
106 mysql_query($qu) or die(mysql_error());
109 // update the related tables (cl_circuit_dbc)
110 $qc = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d ", $dbc['ax_id']);
111 $rc = mysql_query($qc) or die(mysql_error());
112 $circuit = mysql_fetch_array($rc);
114 $qdc = sprintf("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES (%d, %d)", $circuit['ccd_circuitcode'], $newid);
115 mysql_query($qdc) or die(mysql_error());
117 mysql_query("COMMIT");
123 //-----------------------------------------------------------------------------
127 * logs an event in cron functions
129 * @param string - string to be written
132 function df_cronlog($string){
133 $file = '/tmp/DBC_cron.log';
134 if ( !$h = fopen($file, 'ab') ) {
135 echo "Cannot create file ($filename)";
139 $content = date('d-m-Y') . " $string \r\n";
141 // WRITE DATA TO FILE
142 if ( fwrite($h, $content) === FALSE ) {
143 echo "Cannot write to file ($filename)";
151 //-----------------------------------------------------------------------------
153 * FIND THE PATIENT FOR A GIVEN DBC / GIVEN ZTN
157 * @return int patientid
159 function what_patient($dbcid = 0, $ztnid = 0) {
160 if ( !$dbcid && !$ztnid ) return FALSE;
163 $q = sprintf("SELECT cn_pid FROM cl_careroute_numbers ccn JOIN cl_axes ca ON ccn.cn_ztn = ca.ax_ztn
164 WHERE ca.ax_id = %d ", $dbcid);
166 $q = sprintf("SELECT cn_pid FROM cl_careroute_numbers WHERE cn_ztn = '%s'", $ztn);
169 $r = mysql_query($q) or die(mysql_error());
170 if ( mysql_num_rows($r) ) {
171 $row = mysql_fetch_array($r);
172 return $row['cn_pid'];
179 //-----------------------------------------------------------------------------
181 * DBC GET MAIN DIAGNOSE
183 * return main diagnose for a given dbc
186 * @return string - the code for the diagnose (as*)
188 function df_get_main_diagnose($dbcid = 0){
189 if ( !$dbcid ) return 0;
191 $dbc = content_diagnose($dbcid);
192 $as1 = unserialize($dbc['ax_as1']);
193 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
194 $as2 = unserialize($dbc['ax_as2']);
195 $as2c = $as2['content'];
197 // look for the main diagnose
198 $counter = 1; $mainstr = '';
199 foreach ( $as1c as $a) {
200 if ( $counter == $mainpos ) $mainstr = $a;
204 // if it's not in the first array, we look further for it
206 foreach ( $as2c as $a) {
207 if ( $counter == $mainpos ) $mainstr = $a['code'];
216 //-----------------------------------------------------------------------------