Simplified code to generate img tag rather than iframe.
[openemr.git] / library / DBC_functions.php
bloba1d175ae3d00c868003c8cd398d6c0af959e252b
1 <?php
2 /**
3 * DBC DUTCH SYSTEM
4 * several functions used in DBC
5 *
6 * the functions are used mainly with web sessions
7 * for 'independent' functions check DBC_cfunctions
9 * used with library/dropdown.js by interface/main/add_edit_event.php
10 * @author Cristian NAVALICI (lemonsoftware [..] gmail [.] com)
11 * @version 1.0 24-08-2007
14 if ( isset($_SESSION) ) {
15 // trick to make a difference between CLI and webspace
16 require_once(dirname(__FILE__) . '/../interface/globals.php');
19 if ( isset($_POST['code']) ) {
20 request_for_records($_POST['code']);
24 if ( isset($_POST['vcode']) ) {
25 verify_code($_POST['vcode']);
28 // if we edit the already entered activity in add_edit_event...
29 if ( isset($_POST['editactiv']) ) {
30 $_SESSION['editactiv'] = TRUE;
33 $GLOBALS['full_name_activity'] = '';
35 //-----------------------------------------------------------------------------
36 /**
37 * REQUEST FOR RECORDS
39 * @param string $parent_code - code to look for
42 function request_for_records($parent_code) {
43 if ( !$parent_code ) return;
44 $a = '';
45 mysql_query("SET NAMES utf8");
46 $q = sprintf("SELECT cl_activiteit_element AS elem, cl_activiteit_code AS code
47 FROM cl_activiteit WHERE cl_activiteit_groepcode = '%s' AND cl_activiteit_einddatum > '%s'
48 AND cl_activiteit_begindatum < '%s'", $parent_code, $_SESSION['event_date'], $_SESSION['event_date']);
50 $r = mysql_query($q) or die(mysql_error());
51 if ( mysql_num_rows($r) ) {
52 while ( $row = mysql_fetch_array($r) ) {
53 $a .="<option value='{$row['code']}'>{$row['elem']}</option>";
55 } else {
56 $a .= '<option value="0"></option>';
59 echo $a;
60 //echo dirname(__FILE__).'/../interface/globals.php';
65 //-----------------------------------------------------------------------------
66 /**
67 * VERIFY SELECTEERBAAR
69 * this field must be equal to 1 for saving into table
70 * @param int $vcode - element code
72 function verify_code($vcode, $where = 1) {
73 if ( $where == 1) {
74 $q = sprintf("SELECT cl_activiteit_selecteerbaar AS slbar, cl_activiteit_sysid AS sysid
75 FROM cl_activiteit WHERE cl_activiteit_code = '%s'", $vcode);
76 } elseif ( $where == 2) {
77 $q = sprintf("SELECT cl_diagnose_selecteerbaar AS slbar, cl_diagnose_sysid AS sysid
78 FROM cl_diagnose WHERE cl_diagnose_code = '%s'", $vcode);
81 $r = mysql_query($q) or die(mysql_error());
82 if ( mysql_num_rows($r) ) {
83 $row = mysql_fetch_array($r);
84 return ( $row['slbar'] == 0 ) ? FALSE : TRUE;
85 } else {
86 return FALSE;
90 //-----------------------------------------------------------------------------
91 /**
92 * WHAT SYSID/NAME
94 * find the sysid for a code in activiteit table
95 * OR the name based on a known sysid
96 * sysid must be unique in this table
98 * @param string $code - activity's code
99 * @param string $sysid - activity's sysid
100 * @return int|string|bool(false)
102 function what_sysid($code = 0, $sysid = 0) {
103 if (!$code && !$sysid) return FALSE;
105 if ( isset($_SESSION['event_date']) && $_SESSION['event_date']) {
106 $today = $_SESSION['event_date'];
107 } else {
108 $today = date('Y-m-d');
111 if ( $code ) {
112 $q = sprintf("SELECT cl_activiteit_sysid AS sysid
113 FROM cl_activiteit WHERE cl_activiteit_code = '%s' AND
114 cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'", $code, $today, $today);
115 } elseif ( $sysid ) {
116 $q = sprintf("SELECT cl_activiteit_beschrijving AS besc
117 FROM cl_activiteit WHERE cl_activiteit_sysid = %d AND
118 cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'", $sysid, $today, $today);
121 $r = mysql_query($q) or die(mysql_error());
122 if ( mysql_num_rows($r) ) {
123 $row = mysql_fetch_array($r);
124 if ( $code ) return $row['sysid'];
125 else if ($sysid) return $row['besc'];
126 } else {
127 return false;
133 //-----------------------------------------------------------------------------
135 * FULL NAME FOR A SYSID
137 * @param int $sysid (cl_activiteit_sysid)
138 * @return
140 function what_full_sysid($sysid) {
141 if ( !$sysid ) return FALSE;
142 $full_str = '';
144 $parent = what_groepcode_activiteit($sysid);
146 if ( $parent['parent'] ) {
147 $full_str .= what_sysid(0, $sysid) .' - '; // first sysid (not included in reccursive)
148 rec_parent_activiteit($parent['parent']); // produce the rest of the string
149 $full_str .= $GLOBALS['full_name_activity']; // concatenate the above 2 parts
150 } else {
151 // NO PARENT; THIS IS THE FIRST IN LINE
152 $full_str = what_sysid(0, $parent['sysid']);
155 return $full_str;
159 //-----------------------------------------------------------------------------
161 * WHAT CODE FOR AN ACTIVITY
163 * find the code for an activity
165 * @param string $sysid - activity's code
166 * @return string
168 function what_code_activity($sysid = 0) {
169 if ( !$sysid ) return FALSE;
171 $q = sprintf("SELECT * FROM cl_activiteit WHERE cl_activiteit_sysid = %d", $sysid);
172 $r = mysql_query($q) or die(mysql_error());
174 if ( mysql_num_rows($r) ) {
175 $row = mysql_fetch_array($r);
176 return $row['cl_activiteit_code'];
177 } else {
178 return '';
183 //-----------------------------------------------------------------------------
185 * THE PARENT RECORD CODE FOR A SYSID
187 * Look for the record with code / sysid
189 * @param int $sysid (cl_activiteit_sysid)
190 * @param string $code (cl_activiteit_code)
191 * @return array - contains the parent code and its sysid
193 function what_groepcode_activiteit($sysid = 0, $code = '') {
194 if ( !$sysid && !$code ) return FALSE;
195 $today = date('Y-m-d');
196 $rez = array('parent' => '', 'sysid' => '');
198 if ( $sysid ) {
199 $q = sprintf("SELECT cl_activiteit_groepcode AS clag
200 FROM cl_activiteit
201 WHERE cl_activiteit_sysid = %d AND cl_activiteit_begindatum < '%s' AND cl_activiteit_einddatum > '%s'", $sysid, $today, $today);
202 } else if ( $code ) {
203 $q = sprintf("SELECT cl_activiteit_groepcode AS clac, cl_activiteit_sysid AS clsy
204 FROM cl_activiteit
205 WHERE cl_activiteit_code = '%s' AND cl_activiteit_begindatum < '%s' AND cl_activiteit_einddatum > '%s' ", $code, $today, $today);
208 $r = mysql_query($q) or die(mysql_error());
209 if ( mysql_num_rows($r) ) {
210 $row = mysql_fetch_array($r);
211 if ( $sysid) {
212 $rez['parent'] = trim($row['clag']); $rez['sysid'] = (int)$sysid;
213 } else {
214 // array who contains 1 - the parent code 2 - the record sysid (used to find its name)
215 $rez['parent'] = trim($row['clac']); $rez['sysid'] = $row['clsy'];
218 return $rez;
219 } else {
220 return false;
226 //-----------------------------------------------------------------------------
228 * RECURSIVE FUNCTION
230 * @param string $parent - groepcode
231 * @return void
233 function rec_parent_activiteit($parent, $string = '') {
234 $next = what_groepcode_activiteit(0, $parent);
235 $string .= what_sysid(0, $next['sysid']) .' - ';
237 if ( $next['parent'] ) {
238 rec_parent_activiteit($next['parent'], $string);
239 } else {
240 // write the result in a SESSION VARIABLE
241 $GLOBALS['full_name_activity'] = $string; return 1;
246 //-----------------------------------------------------------------------------
248 * RETURNS THE RECORDS FOR LEVEL 1 (main activities)
250 * @param string $what ev - addeditevent, as - axes selection
251 * @param integer $gaf - used only for AS5
252 * @return array $result - contains arrays
254 function records_level1 ($what = 'ev', $gaf = 0) {
255 $result = array();
256 $today = date('Y-m-d');
257 mysql_query("SET NAMES utf8");
260 switch ( $what ) {
261 case 'ev' : // ADD EDIT EVENT
262 $q = sprintf("SELECT cl_activiteit_beschrijving, cl_activiteit_sysid, cl_activiteit_code, cl_activiteit_element
263 FROM cl_activiteit
264 WHERE cl_activiteit_hierarchieniveau = 1
265 AND cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'",
266 $_SESSION['event_date'], $_SESSION['event_date']);
267 break;
269 case 'as1' : // AXES CASE
270 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
271 FROM cl_diagnose
272 WHERE cl_diagnose_as = 1 AND cl_diagnose_hierarchieniveau = 2
273 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
274 ORDER BY cl_diagnose_element",
275 $today, $today);
276 break;
278 case 'as2' : // AXES CASE
279 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
280 FROM cl_diagnose
281 WHERE cl_diagnose_as = 2 AND cl_diagnose_hierarchieniveau = 2
282 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
283 ORDER BY cl_diagnose_element",
284 $today, $today);
285 break;
287 case 'as3' : // AXES CASE
288 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
289 FROM cl_diagnose
290 WHERE cl_diagnose_as = 3
291 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'",
292 $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
293 break;
295 case 'as4' : // AXES CASE
296 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
297 FROM cl_diagnose
298 WHERE cl_diagnose_as = 4
299 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
300 ORDER BY cl_diagnose_element",
301 $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
302 break;
304 case 'as5' : // AXES CASE
305 $groepcode = 'as5_0' . (int)$gaf;
306 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
307 FROM cl_diagnose
308 WHERE cl_diagnose_as = 5 AND cl_diagnose_groepcode = '%s'
309 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'",
310 $groepcode, $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
311 break;
314 $result[] = '';
315 $r = mysql_query($q) or die( mysql_error() );
316 while ( $row = mysql_fetch_array($r) ) {
317 $result[] = $row;
320 //echo "<pre>" . print_r($result, true) . "</pre>"; // debug
321 return $result;
324 //-----------------------------------------------------------------------------
326 * FIND SAVED ACTIVITY FOR AN EVENT
328 * @param $eid - event id
329 * @return string|bool(false) - sysid for that event|false
331 function what_activity($eid) {
332 $q = sprintf("SELECT activity_sysid FROM cl_event_activiteit WHERE event_id= %d ", $eid);
333 $r = mysql_query($q) or die(mysql_error());
334 if ( $r ) {
335 $row = mysql_fetch_array($r);
336 return $row['activity_sysid'];
337 } else {
338 return FALSE;
342 //-----------------------------------------------------------------------------
344 * RETURN THE NAME OF A DIAGNOSE
346 * @param string $ascode
347 * @return string
349 function what_as($ascode) {
350 mysql_query("SET NAMES utf8");
351 $q = sprintf("SELECT cl_diagnose_beschrijving FROM cl_diagnose WHERE cl_diagnose_code = '%s'", $ascode);
352 $r = mysql_query($q) or die( mysql_error() );
353 $row = mysql_fetch_array($r);
354 return $row['cl_diagnose_beschrijving'];
358 //-----------------------------------------------------------------------------
360 * GENERATE AN UNIQUE PATIENT NUMBER
362 * number user in DBC is generated at a new client
364 * @param int $pid - patient id
365 * @return none
367 function generate_id1250($pid) {
368 if ($pid) {
369 mysql_query("START TRANSACTION");
370 $date = date('Ymd');
371 // retrieve last allocated number for this day
372 // let's check if for today there is record
373 $qd = sprintf("SELECT aux_varc FROM cl_aux WHERE aux_id='dn_id1250'");
374 $rd = mysql_query($qd) or die(mysql_error());
375 $rez = mysql_fetch_array($rd);
377 // if strings are NOT equal, then we must update the date field with today value
378 // and reset the counter (aux_varn) to 0
379 if ( $date !== trim($rez['aux_varc']) ) {
380 $qc = sprintf("UPDATE cl_aux SET aux_varc = '%s', aux_varn = 0 WHERE aux_id='dn_id1250'", $date);
381 mysql_query($qc) or die(mysql_error());
384 $nq = mysql_query("SELECT aux_varn FROM cl_aux WHERE aux_id='dn_id1250'") or die(mysql_error());
385 $nrow = mysql_fetch_array($nq);
386 $current_number = $nrow['aux_varn'] + 1;
387 $current_number = strval($current_number);
389 // prepare new ID1250 number (date + 6 digits)
390 $np = str_pad($current_number, 6, '0', STR_PAD_LEFT);
391 $ns = $date . $np;
393 // as an extra caution measure, we check for openemrid and newly generated number
394 // in table; these must be unique
395 $check = sprintf("SELECT * FROM cl_patient_number WHERE pn_oemrid = %d OR pn_id1250 = '%s'",
396 $pid, $ns);
397 $rezcheck = mysql_query($check) or die(mysql_error());
398 if ( !mysql_num_rows($rezcheck) ) {
399 // insert into db
400 $q = sprintf("INSERT INTO cl_patient_number VALUES ('%d', '%s')", $pid, $ns);
401 mysql_query($q) or die(mysql_error());
403 // update auxiliary table
404 $qu = sprintf("UPDATE cl_aux SET aux_varn = aux_varn + 1 WHERE aux_id='dn_id1250'");
405 mysql_query($qu) or die(mysql_error());
406 } else {
407 echo '<script>alert("The generated number or patient id already exists!")</script>';
409 mysql_query("COMMIT");
410 }//if pid
414 //-----------------------------------------------------------------------------
416 * GET AN ID FOR A PATIENT
418 * @param string $whatid
419 * @param int $pid
421 function get_id($whatid, $pid) {
422 $q = FALSE;
423 switch ($whatid) {
424 case 'id1250':
425 $q = sprintf("SELECT pn_id1250 FROM cl_patient_number WHERE pn_oemrid = %d", $pid);
426 break;
429 if ( $q ) {
430 $res = mysql_query($q) or die(mysql_error());
431 $row = mysql_fetch_row($res);
432 return $row[0];
433 } else {
434 return FALSE;
438 //-----------------------------------------------------------------------------
440 * GENERATE A CARE ROUTE NUMBER - ID 1007
441 * (ZORGTRAJECTNUMMER)
443 * this function actually checks for an opened CRN (ZTN - zorgtrajectnummer),
444 * and returns it; only if it doesn't exist generate a new one
446 * @param int $pid
447 * @param string $eventdate
448 * @return string - CRN(ZTN) if exists; new CRN(ZTN)
450 function generate_id1007($pid, $eventdate) {
451 if ($pid) {
452 mysql_query("START TRANSACTION");
453 // check for previous opened CRN
454 $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $pid);
455 $rez = mysql_query($qc) or die(mysql_error());
456 if ( mysql_num_rows($rez) ) {
457 $row = mysql_fetch_array($rez);
458 return $row['cn_ztn'];
459 } else {
460 // generate a new one
461 // get last value from auxiliary table
462 $raux = mysql_query("SELECT aux_varn FROM cl_aux WHERE aux_id='dn_id1007'") or die(mysql_error());
463 $rez = mysql_fetch_array($raux);
464 $current_val = strval($rez['aux_varn'] + 1);
465 $ztn = str_pad($current_val, 7, '0', STR_PAD_LEFT);
467 // insert new values into cl_careroute_numbers table and set it to opened
468 $qi = sprintf("INSERT INTO cl_careroute_numbers(cn_ztn, cn_pid, cn_dopen, cn_open)
469 VALUES('%s', %d, '%s', %d)", $ztn, $pid, $eventdate, 1);
470 mysql_query($qi);
471 $newid = mysql_insert_id();
473 // update value from aux table
474 $qu = sprintf("UPDATE cl_aux SET aux_varn = aux_varn + 1 WHERE aux_id='dn_id1007'");
475 mysql_query($qu) or die(mysql_error());
477 return $newid;
479 mysql_query("COMMIT");
480 } // if pid
483 //-----------------------------------------------------------------------------
485 * SAVE DBC RECORDS
487 * @param none
488 * @return void
490 function save_dbc() {
491 if ( !vl_validate_diagnoses() ) {
492 echo '<script>alert("One or more diagnoses are NOT valid or the diagnoses are NOT UNIQUE!")</script>';
493 return FALSE;
496 if ( $_SESSION['save'] ) {
498 mysql_query("START TRANSACTION");
500 // check for an opened ztn
501 $opened_ztn = verify_ztn();
503 if ( !$opened_ztn ) $opened_ztn = generate_id1007($_SESSION['pid'], date('Y-m-d')); // if it's close, create a new one
505 // insert a new DBC Route
506 $today = date('Y-m-d');
507 if ( $opened_ztn ) {
508 // as a caution we check for posas. must be NOT null
509 if ( !$_SESSION['posas'] ) $_SESSION['posas'] = 1; // default value
511 // at as1 and as2 we must add radio postion for Main Diagnose
512 $as1arr['content'] = $_SESSION['as1'];
513 $as1arr['mainpos'] = (int)$_SESSION['posas'];
514 $as2arr['content'] = $_SESSION['as2'];
515 $as2arr['mainpos'] = (int)$_SESSION['posas'];
516 $circode = (int)$_SESSION['circuitcode'];
518 if ( $_SESSION['show_axid'] ) {
519 // ----------------------
520 // edit case
521 $q = sprintf("UPDATE cl_axes
522 SET ax_as1 = '%s', ax_as2 = '%s', ax_as3 = '%s', ax_as4 = '%s', ax_as5 = '%s' WHERE ax_id = %d",
523 serialize($as1arr), serialize($as2arr), serialize($_SESSION['as3']), serialize($_SESSION['as4']),
524 serialize($_SESSION['as5']), $_SESSION['show_axid'] );
525 mysql_query($q) or die(mysql_error());
527 // edit circuit-dbc connection
528 $icircode = ( vl_validate_circuit($circode) ) ? $circode : '1' ; // validate the circuit
529 $qcir = sprintf("INSERT INTO cl_circuit_dbc(ccd_dbcid, ccd_circuitcode) VALUES(%d, %d) ON DUPLICATE KEY
530 UPDATE ccd_circuitcode = %d", $_SESSION['show_axid'], $icircode, $icircode);
531 mysql_query($qcir) or die(mysql_error());
533 } else {
534 // ----------------------
535 // new case
537 // if it's the first DBC, open date is given by the event with @ flag (pc_apptstatus) after ZTN was opened)
538 if ( first_dbc($_SESSION['show_axid']) ) {
539 // open date for ztn
540 $qztn = sprintf("SELECT cn_dopen FROM cl_careroute_numbers WHERE cn_ztn = '%s' ", $opened_ztn);
541 $rztn = mysql_query($qztn) or die (mysql_error());
542 $ztarr = mysql_fetch_array($rztn); $ztnodate = $ztarr['cn_dopen'];
544 // find first event who is visit (with @ flag after $ztnodate)
545 $qv = sprintf("SELECT MIN(pc_eventDate) as mindate FROM openemr_postcalendar_events WHERE pc_eventDate >= '%s' AND pc_apptstatus = '@' AND pc_pid = %d ", $ztnodate, $_SESSION['pid']);
546 $rv = mysql_query($qv) or die (mysql_error());
548 $earr = mysql_fetch_array($rv);
549 $opendate = $earr['mindate'];
551 // no encounter found so we cannot insert a DBC
552 if ( !$opendate ) {
553 echo "<script>alert('No Encounter found! Please generate first at least one encounter.')</script>";
554 exit();
557 } else {
558 $opendate = $today;
561 $q = sprintf("INSERT INTO cl_axes (ax_ztn, ax_open, ax_as1, ax_as2, ax_as3, ax_as4, ax_as5, ax_odate)
562 VALUES('%s', %d, '%s', '%s', '%s', '%s', '%s', '%s')",
563 $opened_ztn, 1, serialize($as1arr), serialize ($as2arr), serialize ($_SESSION['as3']), serialize ($_SESSION['as4']), serialize ($_SESSION['as5']), $opendate);
564 mysql_query($q) or die(mysql_error()); // leave it here that next line to work
566 // new circuit-dbc connection
567 $rlast = mysql_insert_id();
568 $icircode = ( vl_validate_circuit($circode) ) ? $circode : '1' ; // validate the circuit
569 $qcir = sprintf("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES(%d, %d)", $icircode, $rlast);
570 mysql_query($qcir) or die(mysql_error());
572 } // new | edit case
574 } else {
575 echo '<script>alert("No ZTN selected!")</script>'; return FALSE;
578 // reset implied session variables
579 $_SESSION['save'] = TRUE;
580 for ($i = 1; $i <= 5; $i++) {
581 $_SESSION["as$i"] = '';
584 mysql_query("COMMIT");
586 return TRUE;
588 } //if
591 //-----------------------------------------------------------------------------
593 * VERIFY ZTN
595 * verify for a patient if a ZTN is already opened
596 * return the number of ztn if true, false otherwise
598 * @param $pid - optionally; if provided, we'll ignore the session var
599 * @return string $opened_ztn | bool false
601 function verify_ztn($pid = 0){
603 // patient id could be taken from a param or from a session var
604 $cn_pid = ( $pid ) ? $pid : $_SESSION['pid'];
606 // search for an opened ZTN (id1007)
607 $qc = sprintf("SELECT cn_ztn FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $cn_pid);
608 // echo $qc; // debug
609 $rez = mysql_query($qc) or die(mysql_error()); $opened_ztn = '';
610 if ( mysql_num_rows($rez) ) {
611 $row = mysql_fetch_array($rez);
612 $opened_ztn = $row['cn_ztn'];
613 return $opened_ztn;
614 } else {
615 return false;
620 //-----------------------------------------------------------------------------
622 * LIST ZTN's
624 * lists ZTN's from a patient
626 * @param int $mode 0-all 1-only opened 2-only closed
628 function lists_ztn($mode = 0){
629 switch ($mode) {
630 case 0: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $_SESSION['pid']); break;
631 case 1: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1' ORDER BY cn_dopen", $_SESSION['pid']); break;
632 case 2: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '0' ORDER BY cn_dopen", $_SESSION['pid']); break;
633 default: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $_SESSION['pid']); break;
636 $artn = array();
637 $rez = mysql_query($qc) or die(mysql_error());
638 if ( mysql_num_rows($rez) ) {
639 while ( $row = mysql_fetch_array($rez)) {
640 $artn[] = $row;
642 return $artn;
643 } else {
644 return false;
648 //-----------------------------------------------------------------------------
650 * LIST DIAGNOSES
652 * lists diagnoses for a ZTN
653 * if called for last, this means the open dbc
655 * @param string ax_odate DESC default, 'last' for the last one (ordered by date)
656 * @return array | bool(false)
658 function lists_diagnoses($order = 'ax_odate DESC'){
660 // obtain opened ztn
661 $ztn = verify_ztn();
662 if ( $order == 'last') {
663 $qc = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 1 ORDER BY ax_odate DESC ", $ztn);
664 } else {
665 $qc = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' ORDER BY %s ", $ztn, $order);
668 $artn = array();
669 $rez = mysql_query($qc) or die(mysql_error());
671 if ( mysql_num_rows($rez) ) {
672 while ( $row = mysql_fetch_array($rez)) {
673 $artn[] = $row;
675 return $artn;
676 } else {
677 return false;
681 //-----------------------------------------------------------------------------
683 * LAST DIAGNOSE
685 * specific function to return the last entered diagnose
686 * a.k.a. the open DBC
688 function last_diagnose() {
689 $arr = lists_diagnoses('last');
690 return $arr[0];
693 //-----------------------------------------------------------------------------
695 * ZORGTYPE CODES
697 * @param none
698 * @return array
700 function zorgtype_codes() {
701 mysql_query("SET NAMES utf8");
702 $check = first_dbc($_SESSION['show_axid']);
703 if ( $check ) { //first dbc
704 $qc = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_groepcode = 100 AND cl_zorgtype_selecteerbaar = 1");
705 } else {
706 $qc = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_groepcode = 200 AND cl_zorgtype_selecteerbaar = 1");
708 $rez = mysql_query($qc) or die(mysql_error());
710 $ra = array();
711 while ($r = mysql_fetch_assoc($rez)) {
712 $ra[] = $r;
715 return $ra;
718 //-----------------------------------------------------------------------------
720 * FIRST DBC?
722 * find if a dbc is the first , a 'follow-up' or there is no DBC yet.
723 * (the same function is in DBC_files)
725 * @param int $ax_id - dbc's id
726 * @return bool - true if it's the first, 0 - otherwise
728 function first_dbc($ax_id) {
729 // to be the first means there is only one DBC per open ZTN
730 $openztn = verify_ztn();
732 // look for all dbcs in a careroute
733 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s' ORDER BY ax_id", $openztn);
734 $rez = mysql_query($qz) or die(mysql_error());
736 while ( $row = mysql_fetch_array($rez) ) {
737 $arrdbc[] = $row['ax_id'];
740 // and now, the analysis:
741 // - first means the ax_id is the first in array
742 // - followup means the ax_id is NOT the first in array (because after the first, all dbcs
743 // are followups)
744 return ( $arrdbc[0] == $ax_id );
748 //-----------------------------------------------------------------------------
750 * STATUS FOR A ZTN
752 * find if a dbc is the first , a 'follow-up' or there is no DBC yet.
753 * retcode 0 - no ZTN open
754 * 1 - ZTN open, no DBC
755 * 2 - ZTN open, (just) one opened DBC
756 * 3 - ZTN open, (just) one closed DBC
757 * 4 - ZTN open, more DBC's
759 * @param none
760 * @return int - for $retcode see above
762 function ztn_status() {
764 // return the open ztn if any
765 $openztn = verify_ztn();
767 if ( $openztn ) {
768 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s'", $openztn);
769 $rez = mysql_query($qz) or die(mysql_error());
770 $rows = mysql_num_rows($rez);
772 switch ($rows) {
773 case 0: $retcode = 1; break; // no rows, means no DBC in ZTN
774 case 1:
775 $ro = mysql_fetch_array($rez);
776 $retcode = ( $ro['ax_open'] ) ? 2 : 3 ;
777 break; // just one row, means one DBC (initial) in ZTN
778 default: $retcode = 4; // any other numbers means more DBC's, so we have initial + follow's up
780 } else {
781 $retcode = 0;
784 return $retcode;
788 //-----------------------------------------------------------------------------
790 * REDENSLUITEN CODES
792 * @param none
793 * @return array - an array with all redensluiten codes
795 function reden_codes() {
796 if ( $_SESSION['eind'] ) {
797 mysql_query("SET NAMES utf8");
798 $eind = mysql_real_escape_string($_SESSION['eind']);
799 $qc = sprintf("SELECT * FROM cl_redensluiten clr WHERE clr.cl_redensluiten_begindatum < '%s' AND clr.cl_redensluiten_einddatum >'%s' ", $eind, $eind);
800 $rez = mysql_query($qc) or die(mysql_error());
802 $ra = array();
803 while ($r = mysql_fetch_assoc($rez)) {
804 $ra[] = $r;
807 return $ra;
808 }//if
811 //-----------------------------------------------------------------------------
813 * CHECK FOR 'SENT TO INSURER' CASE
815 * check if the open DBC was already sent to insurer or not
817 * @param none
818 * @return bool
820 function sent_to_insurer() {
821 $ldbc = last_diagnose(1); // return open DBC
822 return ( $ldbc['ax_sti'] ) ? TRUE : FALSE;
826 //-----------------------------------------------------------------------------
828 * LOAD OPEN DBC
830 * load the values from the open dbc into session variables
831 * to edit form
833 function load_dbc() {
834 $ldbc = last_diagnose(1);
835 $as1 = unserialize($ldbc['ax_as1']);
836 $_SESSION['as1'] = $as1['content']; $_SESSION['posas'] = (int)$as1['mainpos'];
838 $as2 = unserialize($ldbc['ax_as2']);
839 $_SESSION['as2'] = $as2['content'];
841 $_SESSION['as3'] = unserialize($ldbc['ax_as3']);
842 $_SESSION['as4'] = unserialize($ldbc['ax_as4']);
843 $_SESSION['as5'] = unserialize($ldbc['ax_as5']);
846 //-----------------------------------------------------------------------------
848 * CLOSE AN OPEN DBC
850 * @param int $follow - specify if a DBC is followed by another one
851 * created with the same content, or close + ZTN close
852 * @param int $stoornis - $_POST['stoornis']
853 * @param int $ztc - $_POST['ztc']
854 * @param int $rtc - $_POST['rtc']
855 * @param array $gaf - array with 2 elements (hoogste/eind)
856 * @param int $dbcid - dbcid if called from a script
857 * @param string $eind - closing date if called from a script
858 * @param int $patid - patient id if called from a script
859 * @return void
861 function close_dbc($follow = 0, $stoornis = 0, $ztc, $rtc, $gaf, $dbcid = 0, $eind = '', $patid = 0) {
862 mysql_query('START TRANSACTION');
864 $einddate = ( $dbcid ) ? $eind : $_SESSION['eind'];
865 $dbc_id = ( $dbcid ) ? $dbcid : $_SESSION['show_axid'];
866 $pid = ( $dbcid ) ? $patid : $_SESSION['pid'];
867 //echo "$dbcid / $stoornis / $ztc / $rtc / $gaf / $eind <br>"; // debug
868 // close the open dbc; also mark for vektis
869 //$q = sprintf("UPDATE cl_axes SET ax_open = 99, ax_cdate = '%s', ax_vkstatus = 1
870 $q = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s', ax_vkstatus = 1
871 WHERE ax_id = %d", $einddate, $dbc_id);
872 mysql_query($q) or die(mysql_error());
874 // update for the current
875 // NOTE: we must run this here because the following statement
876 // retrieves the content for DBC; otherwise the duplicated dbc will not contain
877 // the new $gaf array
879 update_gaf($dbc_id, $gaf);
881 // this MUST be run after update_gaf
882 $ldbc = content_diagnose($dbc_id);
884 // write the closing parameters from dbc_sluiten form
885 write_stoornis($stoornis, $dbc_id);
886 write_zorg($ztc, $dbc_id);
887 write_reden($rtc, $dbc_id);
889 // generate prestatie code
890 dt_prestatiecode($ztc, $stoornis, $dbc_id);
892 // open a new one if it is the case
893 if ( $follow ) {
894 $newid = duplicate_dbc($ldbc);
895 if ( !$dbcid ) $_SESSION['show_axid'] = $newid;
897 return $newid;
898 } else {
899 $qztn = sprintf("UPDATE cl_careroute_numbers SET cn_open = 0, cn_dclosed = '%s'
900 WHERE cn_pid = %d", $einddate, $pid);
901 mysql_query($qztn) or die(mysql_error());
903 if ( !$dbcid ) $_SESSION['show_axid'] = FALSE;
906 mysql_query('COMMIT');
910 //-----------------------------------------------------------------------------
912 * TOTAL TIME SPENT
914 * adds all direct, indirect + travel time from the first day of this DBC to the last day
915 * only for encounters (events with @ pc_apptstatus)
917 * @param int $dbc - if used instead of $_SESSION
918 * @return array - contains the three times (indirect, travel, total)
920 function total_time_spent($dbc = 0, $btime = '', $etime = '') {
921 // our big results
922 $total_time = 0 ; $indirect_time = 0 ; $travel_time = 0;
924 // DBC ID
925 // we have a session var or a given value?
926 $dbcid = ( $dbc ) ? $dbc : $_SESSION['show_axid'];
928 // begin date for DBC
929 if ( $btime ) {
930 $bd_dbc = $btime;
931 } else {
932 $cd = content_diagnose($dbcid);
933 $bd_dbc = $cd['ax_odate'];
936 // end date of DBC
937 if ( $etime ) {
938 $ed_dbc = $etime;
939 } else {
940 $ed_dbc = ( $_SESSION['eind'] ) ? $_SESSION['eind'] : date('Y-m-d');
943 // if we have a $dbc (given as arg) we must find a $pid
944 if ( $dbc ) {
945 $pid = what_patient($dbcid);
946 } else {
947 $pid = $_SESSION['pid'];
950 // also, we don't check for first_dbc if we have $btime
951 if ( $btime == '2008-01-01' || $etime == '2007-12-31' ) {
952 $check_first = FALSE;
953 } else {
954 $check_first = TRUE;
957 // find all events between DBC's dates and sum up total times
958 $q = sprintf("SELECT pc_eid, pc_duration FROM openemr_postcalendar_events
959 WHERE pc_pid = '%s' AND pc_eventDate >= '%s' AND pc_eventDate <= '%s' AND pc_apptstatus = '@' ",
960 $pid, $bd_dbc, $ed_dbc);
962 $r = mysql_query($q) or die(mysql_error());
964 while ($row = mysql_fetch_array($r)) {
965 $total_time += $row['pc_duration'];
967 // get indirect+travel time
968 $q1 = sprintf("SELECT * FROM cl_time_activiteit WHERE event_id = %d", $row['pc_eid']);
969 $r1 = mysql_query($q1) or die(mysql_error());
970 if ( mysql_num_rows($r1) ) {
971 $row1 = mysql_fetch_array($r1);
972 $indirect_time += $row1['indirect_time'];
973 $travel_time += $row1['travel_time'];
975 } // while
977 // if it is the first DBC we look for previous events
978 // which weren't included in DBC and add timing too
979 if ( $check_first && first_dbc($dbc) ) {
980 // begin date of ZTN
981 $r = lists_ztn(1);
982 $bdztn = $r[0]['cn_dopen'];
984 // find all events between DBC's dates and sum up total times
985 $q = sprintf("SELECT pc_eid, pc_duration FROM openemr_postcalendar_events
986 WHERE pc_pid = '%s' AND pc_eventDate > '%s' AND pc_eventDate < '%s'",
987 $pid, $bdztn, $bd_dbc);
988 $r = mysql_query($q) or die(mysql_error());
989 while ($row = mysql_fetch_array($r)) {
990 $total_time += $row['pc_duration'];
991 // get indirect+travel time
992 $q2 = sprintf("SELECT * FROM cl_time_activiteit WHERE event_id = %d", $row['pc_eid']);
993 $r2 = mysql_query($q2) or die(mysql_error());
994 if ( mysql_num_rows($r2) ) {
995 $row2 = mysql_fetch_array($r2);
996 $indirect_time += $row2['indirect_time'];
997 $travel_time += $row2['travel_time'];
999 } // while
1000 } // if
1002 $total_time /= 60 ; //transform it to minutes from seconds
1003 $total_time += $indirect_time + $travel_time;
1005 $time = array ('total_time' => $total_time, 'indirect_time' => $indirect_time, 'travel_time' => $travel_time);
1006 return $time;
1009 //-----------------------------------------------------------------------------
1011 * GENERATE STOORNIS(DISEASE) DROPDOWN
1013 * NOTE maybe not used anymore; there is an automatic procedure (DBC_decisiontree)
1015 * generates a dropdown for stoornis section in
1016 * close dbc module
1018 * @param string $odate - opening date for DBC (which is gonna be closed)
1019 * @return string
1021 function stoornis_dropdown($odate) {
1022 $s = '<select name="stoornis" id="stoornis">';
1024 mysql_query("SET NAMES utf8");
1025 $qc = sprintf("SELECT * FROM cl_productgroep clp
1026 WHERE clp.cl_productgroep_code_verblijf = 0
1027 AND clp.cl_productgroep_selecteerbaar = 1
1028 AND clp.cl_productgroep_begindatum < '%s' AND clp.cl_productgroep_einddatum > '%s' ", $odate, $odate);
1029 $rez = mysql_query($qc) or die(mysql_error());
1031 while ( $row = mysql_fetch_array($rez) ) {
1032 $s .= '<option value="' .$row['cl_productgroep_sysid']. '">' .$row['cl_productgroep_beschrijving']. '</option>';
1035 $s .= '</select>';
1036 return $s;
1039 //-----------------------------------------------------------------------------
1041 * GENERATE VERBLIJF(HOSPITAL STAY) DROPDOWN
1043 * generates a dropdown for verblijf section in close dbc module
1045 * @param return
1047 function verblijf_dropdown() {
1048 $s = '<select name="verblijf" id="verblijf">';
1049 $s .= '<option value="0">Geen Verblijf</option>';
1050 $qc = sprintf("SELECT * FROM cl_productgroep clp WHERE clp.cl_productgroep_code < 999
1051 AND clp.cl_productgroep_selecteerbaar = 1");
1052 $rez = mysql_query($qc) or die(mysql_error());
1054 while ( $row = mysql_fetch_array($rez) ) {
1055 $s .= '<option value="' .$row['cl_productgroep_sysid']. '">'.$row['cl_productgroep_beschrijving']. '</option>';
1058 $s .= '</select>';
1059 return $s;
1062 //-----------------------------------------------------------------------------
1064 * GENERATE CIRCUITCODE DROPDOWN
1066 * @param int - $sel the selected item
1067 * @return string
1069 function circuit_dropdown($sel = 0) {
1070 $today = date('Y-m-d');
1071 $s = '<select id="circuit" name="circuit">';
1073 mysql_query("SET NAMES utf8");
1074 $query = sprintf ("SELECT * FROM cl_circuit WHERE cl_circuit_begindatum < '%s' AND cl_circuit_einddatum > '%s'", $today, $today);
1075 $qr = mysql_query($query) or die (mysql_error());
1076 while ( $row = mysql_fetch_array($qr) ) {
1077 $selected = ( $row['cl_circuit_code'] == $sel ) ? 'selected="selected"' : '';
1078 $s .= '<option value="' .$row['cl_circuit_code']. '" ' .$selected. '>' .$row['cl_circuit_beschrijving']. '</option>';
1081 $s .= '</select>';
1082 return $s;
1085 //-----------------------------------------------------------------------------
1087 * HAS CIRCUIT
1089 * check if a dbc has a link with a circuit code in cl_circuit_dbc table
1091 * @param int $axid
1092 * @return int|bool - the circuit code or false
1094 function has_circuit($axid = 0) {
1095 if ( !$axid ) return 0;
1097 $qh = sprintf("SELECT * FROM cl_circuit_dbc WHERE ccd_dbcid = %d", $axid);
1098 $rh = mysql_query($qh) or die(mysql_error());
1100 $result = '';
1101 if ( mysql_num_rows($rh) ) {
1102 $row = mysql_fetch_array($rh);
1103 $result = $row['ccd_circuitcode'];
1104 } else {
1105 $result = 0;
1108 return $result;
1111 //-----------------------------------------------------------------------------
1113 * GET CIRCUIT CODE
1115 * return circuit code for a dbc
1117 * @param int dbc id
1118 * @param bool|int
1120 function get_circuitcode($dbcid) {
1121 if ( !$dbcid) return FALSE;
1123 mysql_query("SET NAMES utf8");
1124 $qc = sprintf("SELECT cl_circuit_beschrijving FROM cl_circuit clc
1125 JOIN cl_circuit_dbc clcd ON clcd.ccd_circuitcode = clc.cl_circuit_code
1126 WHERE clcd.ccd_dbcid = %d", $dbcid);
1127 $r = mysql_query($qc) or die(mysql_error());
1129 if ( mysql_num_rows($r) ) {
1130 $row = mysql_fetch_array($r);
1131 return $row['cl_circuit_beschrijving'];
1132 } else return FALSE;
1137 //-----------------------------------------------------------------------------
1139 * WRITE ZORG
1141 * write zorg sysid for a closing dbc
1143 * @param int - zorg sysid
1144 * @param int - $dbcid - dbc id
1145 * @return none
1147 function write_zorg($zsysid, $dbcid) {
1148 // validate the code
1149 $izsysid = ( vl_validate_zorg($zsysid) ) ? $zsysid : 1 ;
1151 $qz = sprintf("INSERT INTO cl_zorg_dbc VALUES(%d, %d)", $dbcid, $izsysid);
1152 $r = mysql_query($qz) or die(mysql_error());
1155 //-----------------------------------------------------------------------------
1157 * WRITE STOORNIS
1159 * save the value from stoornis dropdown
1161 * @param int - cl_productgroep_sysid
1162 * @param int - $dbcid - dbc id
1163 * @return none
1165 function write_stoornis($zsysid, $dbcid) {
1166 $qz = sprintf("INSERT INTO cl_productgroep_dbc VALUES(%d, %d)", $zsysid, $dbcid);
1167 $r = mysql_query($qz) or die(mysql_error());
1170 //-----------------------------------------------------------------------------
1172 * WRITE REDEN
1174 * @param int cl_redensluiten_code
1175 * @param int - $dbcid - dbc id
1176 * @return void
1178 function write_reden($rcode, $dbcid) {
1179 // validate the code
1180 $ircode = ( vl_validate_redencode($rcode) ) ? $rcode : 1 ;
1182 $qz = sprintf("INSERT INTO cl_redensluiten_dbc VALUES(%d, %d)", $ircode, $dbcid);
1183 $r = mysql_query($qz) or die(mysql_error());
1187 //-----------------------------------------------------------------------------
1189 * UPDATE GAF
1191 * When closing a DBC we still need two values from GAF dropdowns (middle and end).
1192 * The first one was on DBC opening.
1194 * @param int $dbcid
1195 * @param array $gaf
1196 * @return bool
1198 function update_gaf($dbcid, $gaf) {
1199 if ( !$dbcid ) return FALSE;
1201 $qz = sprintf("SELECT ax_as5 FROM cl_axes WHERE ax_id = %d", $dbcid);
1202 $rz = mysql_query($qz) or die(mysql_error());
1203 $rowrz = mysql_fetch_array($rz);
1205 // we update the missing values
1206 $unser = unserialize($rowrz['ax_as5']);
1207 $un['gaf1'] = $unser['gaf1'];
1208 $un['gaf2'] = mysql_real_escape_string($gaf['gaf2']);
1209 $un['gaf3'] = mysql_real_escape_string($gaf['gaf3']);
1210 $ser = serialize($un);
1211 // update the new values
1212 $qu = sprintf("UPDATE cl_axes SET ax_as5 = '%s' WHERE ax_id = %d", $ser, $dbcid);
1213 $ru = mysql_query($qu) or die(mysql_error());
1215 return TRUE;
1218 //-----------------------------------------------------------------------------
1220 * GET PRIMARY CARE PROVIDER INFOS
1222 * used at demographics_full
1223 * @param int patient_id
1224 * @return array
1226 function get_provider_DBC($pid = 0) {
1227 if ( !$pid ) return FALSE;
1228 $qz = sprintf("SELECT * FROM cl_providers WHERE pro_pid = %d", $pid);
1229 $r = mysql_query($qz) or die(mysql_error());
1230 return mysql_fetch_assoc($r);
1233 //-----------------------------------------------------------------------------
1235 * GET REFERER INFOS
1237 * used at demographics_full
1238 * @param int patient_id
1239 * @return array
1241 function get_referer_DBC($pid = 0) {
1242 if ( !$pid ) return FALSE;
1243 $qz = sprintf("SELECT * FROM cl_referers WHERE ref_pid = %d", $pid);
1244 $r = mysql_query($qz) or die(mysql_error());
1245 return mysql_fetch_assoc($r);
1249 //-----------------------------------------------------------------------------
1251 * RETURN JOB DESCRIPTION (BEROEP)
1253 * for a specified user
1255 * @param int user id
1256 * @param int $code - look for code or name field
1257 * @return string
1259 function what_beroep($pid = 0, $code = 0) {
1260 if ( !$pid ) return FALSE;
1262 $qz = sprintf("SELECT cl_beroep_element as cbe, cl_beroep_code as cbc
1263 FROM cl_beroep JOIN cl_user_beroep ON cl_beroep.cl_beroep_sysid = cl_user_beroep.cl_beroep_sysid
1264 WHERE cl_user_beroep.cl_beroep_userid = %d", $pid);
1265 $rz = mysql_query($qz) or die(mysql_error());
1266 $rez = mysql_fetch_assoc($rz);
1268 return ( $code ) ? $rez['cbc'] : $rez['cbe'];
1271 //-----------------------------------------------------------------------------
1273 * RETURN FULL DUTCH NAME
1275 * for a specified patient
1277 * @param int $pid - patient id
1278 * @return string $full_name
1280 function dutch_name($pid = 0) {
1281 if ( !$pid ) return FALSE;
1283 mysql_query("SET NAMES utf8");
1285 $qn = sprintf("SELECT fname, lname FROM patient_data WHERE id = %d", $pid);
1286 $rn = mysql_query($qn) or die(mysql_error());
1287 $rez = mysql_fetch_assoc($rn);
1289 $full_name = '';
1290 $first_name = $rez['fname'];
1291 $last_name = $rez['lname'];
1293 // then we look into patient_data_NL because it's not mandatory that there would be a record for this $pid
1294 // (so, don't use JOIN between these tables)
1295 $qn2 = sprintf("SELECT pdn_pxlast, pdn_pxlastpar, pdn_lastpar FROM patient_data_NL WHERE pdn_id = %d", $pid);
1296 $rn2 = mysql_query($qn2) or die(mysql_error());
1298 if ( mysql_num_rows($rn2) ) {
1299 $reznl = mysql_fetch_assoc($rn2);
1300 // partner is prefix + last name
1301 $partner = ( $reznl['pdn_lastpar'] ) ? $reznl['pdn_pxlastpar'] .' '. $reznl['pdn_lastpar'] .' - ' : '' ;
1302 $prefix = $reznl['pdn_pxlast'];
1303 // we make the full name
1304 $full_name = $first_name .' '. $partner . $prefix .' '. $last_name;
1305 } else {
1306 $full_name = $first_name .' '. $last_name;
1309 return $full_name;
1314 //-----------------------------------------------------------------------------
1316 * PREPARE STRINGS FOR UTF8
1318 * check if a string is UTF8 encoded; if not, convert it
1319 * (the string must be ISO-8859-1 because we use utf8_encode function)
1321 * @param string $string (utf8 or latin1)
1322 * @return string - converted utf8 string
1325 function sutf8($string)
1327 $rez = preg_match('%(?:
1328 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1329 |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1330 |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1331 |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1332 |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1333 |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1334 |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1335 )+%xs', $string);
1337 $newst = ( $rez ) ? $string : utf8_encode($string);
1338 return $newst;
1341 //-----------------------------------------------------------------------------
1343 * RETURN ALL THE ZTN's FOR A PATIENT
1345 * @param int $pid
1346 * @return array - contains all ztn's (closed, open) ; empty array if not ztn available
1348 function all_ztn($pid = 0) {
1349 if ( !$pid ) return FALSE;
1351 $result = array();
1352 $q = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $pid);
1353 $r = mysql_query($q) or die( mysql_error() );
1354 if ( mysql_num_rows($r) ) {
1355 while ( $row = mysql_fetch_array($r) ) {
1356 $result[] = $row;
1360 return $result;
1363 //-----------------------------------------------------------------------------
1365 * RETURN ALL THE DBC's FOR A PATIENT
1367 * using ztn id
1369 * @param int $ztn
1370 * @return array - contains all dbc's (closed, open, sent to insurer) ; empty array if not ztn available
1372 function all_dbc($ztnid = 0) {
1373 if ( !$ztnid ) return FALSE;
1375 $result = array();
1376 $q = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' ORDER BY ax_odate", $ztnid);
1377 $r = mysql_query($q) or die( mysql_error() );
1378 if ( mysql_num_rows($r) ) {
1379 while ( $row = mysql_fetch_array($r) ) {
1380 $result[] = $row;
1384 return $result;
1387 //-----------------------------------------------------------------------------
1389 * OPEN ZTN/DBC?
1391 * verify for a patient if a ZTN is already opened
1392 * if it is, verify further if there is any open DBC
1394 * DBC opened - DBC open - 2
1395 * DBC closed - Geen DBC - 1
1396 * ZTN closed - Dossier gesloten - 0
1398 * @param $pid patient id
1399 * @return array - a string + a value
1401 function has_ztndbc($pid = 0){
1402 if ( !$pid ) return FALSE;
1403 $result = '';
1405 // search for an opened ZTN (id1007)
1406 $qc = sprintf("SELECT cn_ztn FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $pid);
1407 $rez = mysql_query($qc) or die(mysql_error());
1408 if ( mysql_num_rows($rez) ) {
1409 $row = mysql_fetch_array($rez);
1410 $opened_ztn = $row['cn_ztn'];
1412 $qb = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = '1' ", $opened_ztn);
1413 $rezb = mysql_query($qb) or die(mysql_error());
1415 $result['str'] = ( mysql_num_rows($rezb) ) ? 'DBC open.' : 'Geen DBC!';
1416 $result['code'] = ( mysql_num_rows($rezb) ) ? 2 : 1;
1418 } else {
1419 $result['str'] = 'Dossier gesloten!'; $result['code'] = 0;
1422 return $result;
1426 //-----------------------------------------------------------------------------
1428 * BEROEP DROPDOWN
1430 * build the dropdown for providers jobs
1431 * name: beroep id: beroep
1433 * @param int $selected
1434 * @return void - just echo the string - html encoded
1436 function beroep_dropdown($selected = 0){
1438 $string = '<select name="beroep" id="beroep">';
1439 $today = date('Y-m-d');
1441 $q = sprintf(" SELECT cl_beroep_element AS cbe, cl_beroep_sysid AS cbs
1442 FROM cl_beroep
1443 WHERE cl_beroep_selecteerbaar = 1
1444 AND cl_beroep_einddatum > '%s' AND cl_beroep_begindatum < '%s'
1445 ORDER BY cbe", $today, $today);
1446 $r = mysql_query($q) or die(mysql_error());
1447 while ( $row = mysql_fetch_array($r) ) {
1448 $sufix = ( $selected == $row['cbs']) ? 'selected="selected"' : '';
1449 $string .= "<option value='{$row['cbs']}' $sufix>{$row['cbe']}</option>";
1452 $string .= '</select>';
1453 echo $string;
1457 //-----------------------------------------------------------------------------
1459 * ZORGTYPECODES DROPDOWN
1461 * build the dropdown for zorg types codes
1462 * in dbc closing section (dbc_close.php)
1464 * name="ztc" id="ztc"
1466 * @param none
1467 * @return void - just echo the string - html encoded
1469 function zorgtype_dropdown(){
1471 $string = '<select name="ztc" id="ztc">';
1472 $today = date('Y-m-d');
1474 $ztc = zorgtype_codes();
1476 $display = 1;
1477 foreach($ztc as $z) {
1478 // zorgtype validation ; we try to exclude 180104 if the activities validation failed
1479 if ( $z['cl_zorgtype_sysid'] == 180104 && !vl_zorgtype_104() ) {
1480 $display = 0;
1482 // zorgtype validation ; we try to exclude 180104 if the direct time > 180
1483 if ( $z['cl_zorgtype_sysid'] == 180104 && !vl_zorgtype_880() ) {
1484 $display = 0;
1486 // zorgtype validation ; we try to exclude 180106 if the activities total time validation failed
1487 if ( $z['cl_zorgtype_sysid'] == 180106 && !vl_zorgtype_106() ) {
1488 $display = 0;
1490 // zorgtype validation ; we try to exclude 180111 if the providers job test failed
1491 if ( $z['cl_zorgtype_sysid'] == 180111 && !vl_zorgtype_111 () ) {
1492 $display = 0;
1495 if ( $display )
1496 $string .= '<option value="' .$z['cl_zorgtype_sysid']. '">' .$z['cl_zorgtype_beschrijving']. '</option>';
1498 $display = 1;
1500 $string .= '</select>';
1502 echo $string;
1506 //-----------------------------------------------------------------------------
1508 * PATIENT AGE
1510 * return the patient age
1511 * (!) uses a function from OpenEMR
1513 * @param int $pid - patient ID
1514 * @return int - age in years
1516 function patient_age($pid = 0){
1517 if ( !$pid ) return FALSE;
1519 // retrieve DOB for the patient
1520 $q = sprintf("SELECT DOB FROM patient_data WHERE id = %d ", $pid);
1521 $r = mysql_query($q) or die(mysql_error());
1522 $row = mysql_fetch_array($r);
1524 $dob = $row['DOB'];
1525 if ( 0 == $dob ) {
1526 vl_log("Patient with ID: $pid doesn't have DOB!"); return FALSE;
1529 $dobn = str_replace('-','', $dob);
1530 $age = getPatientAge($dobn); // function from library/patient.inc
1532 // $age can contain strings like 6 month, 8 month for age < 2 years old
1533 if ( is_string($age) ) $age = 2;
1535 return $age;
1538 //-----------------------------------------------------------------------------
1540 * HAS BEGIN GAF
1542 * for some patients (with age < 4) we don't fill a begin GAF - AS5
1543 * so, if we close the DBC, we don't ask for the other 2 GAF if we don't have the first one.
1545 * @param int $axid - DBC id
1546 * @return bool - true if there is a begin GAF
1548 function has_beginGAF($axid = 0){
1549 if ( !$axid ) return FALSE;
1551 $dbc = content_diagnose($axid);
1552 $ax5 = unserialize($dbc['ax_as5']);
1554 return ( !empty($ax5) );
1558 //-----------------------------------------------------------------------------
1560 * DISPLAY LINKS
1562 * display links as Add/Edit DSM-IV, Close DSM-IV, etc... in coding.php (patient_file)
1564 * @param none
1565 * @return void
1567 function display_links(){
1568 $retcode = ztn_status(); // find the ZTN situation
1570 switch ( $retcode ) {
1571 case 0: $msg = 'No ZTN opened!'; break;
1572 case 1:
1573 case 3: $msg = '<dd><a class="text" href="javascript:selas()">Add DSM IV</a></dd>
1574 <dd><a class="text" href="#" id="closeztn">Close ZTN</a></dd>'; break;
1575 case 2: $msg = '<dd><a class="text" href="javascript:selas()">Edit DSM IV</a></dd>
1576 <dd><a class="text" href="javascript:selcl()">DBC Sluiten</a></dd>'; break;
1577 case 4: $msg = '<dd><a class="text" href="javascript:selfl()">Follow up display</a></dd>
1578 <dd><a class="text" href="javascript:selcl()">DBC Sluiten</a></dd>'; break;
1581 echo $msg;
1584 //-----------------------------------------------------------------------------
1586 * RETURN THE DBC'S W/OUT FUTURE EVENTS
1588 * return opened dbc's without future events
1590 * @param none
1591 * @return array
1593 function df_future_events(){
1594 $resarr = array(); // dummy array for result
1595 $date = date('Y-m-d');
1596 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1598 if ( mysql_num_rows($q) ) {
1599 while ( $row = mysql_fetch_array($q) ) {
1600 $count = 0;
1601 $pid = what_patient($row['ax_id']);
1602 $fe = mysql_query("SELECT COUNT(*) AS a FROM openemr_postcalendar_events WHERE pc_pid = $pid
1603 AND pc_eventDate > '$date' ") or die(mysql_error());
1604 $rfe = mysql_fetch_array($fe);
1605 $count = $rfe['a']; // how many future encounters
1607 if ( !$count ) { $row['pid'] = $pid; $resarr[] = $row; }
1609 } // while
1612 return $resarr;
1617 //-----------------------------------------------------------------------------
1619 * RETURN OPENED DBC'S WITH TOTAL TIME
1621 * the times are separated per years 2007/2008
1623 * @param none
1624 * @return array
1626 function df_allopendbc_wtimes(){
1627 $resarr = array(); // dummy array for result
1628 $today = date('Y-m-d');
1629 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1631 if ( mysql_num_rows($q) ) {
1632 $count = 1;
1633 while ( $row = mysql_fetch_array($q) ) {
1634 $odate = $row['ax_odate'];
1635 $resarr[$count]['dbcid'] = $row['ax_id'];
1636 $resarr[$count]['odate'] = $odate;
1638 if ( $odate <= '2007-12-31' ) {
1639 $times2007 = total_time_spent($row['ax_id'], $odate, '2007-12-31');
1640 $times2008 = total_time_spent($row['ax_id'], '2008-01-01', $today);
1641 $resarr[$count]['2007'] = $times2007['total_time'];
1642 $resarr[$count]['2008'] = $times2008['total_time'];
1643 } else {
1644 $times = total_time_spent($row['ax_id'], '2008-01-01', $today);
1645 $resarr[$count]['2007'] = 0;
1646 $resarr[$count]['2008'] = $times['total_time'];
1649 $pid = what_patient($row['ax_id']);
1650 $resarr[$count]['pid'] = $pid; // using $times as a returning array
1651 $count++;
1652 } // while
1654 //echo '<pre>' . print_r($resarr, TRUE) . '</pre>'; // debug
1655 return $resarr;
1659 //-----------------------------------------------------------------------------
1661 * RETURNS SELECTED ACTIVITY FROM THE FORM
1663 * it's about add_edit_event.php form
1664 * @param none
1665 * @return string
1667 function selected_ac() {
1668 // same logic as in javascript validation
1669 if ( $_POST['box5'] ) $ac = $_POST['box5'];
1670 elseif ( $_POST['box4']) $ac = $_POST['box4'];
1671 elseif ( $_POST['box3']) $ac = $_POST['box3'];
1672 elseif ( $_POST['box2']) $ac = $_POST['box2'];
1673 elseif ( $_POST['box1']) $ac = $_POST['box1'];
1675 return $ac;
1679 //-----------------------------------------------------------------------------
1681 * RETURN OPENED DBC'S WITH MONEY VALUES FOR EACH
1683 * it simulates a closing for opened DBC's
1685 * @param none
1686 * @return array
1688 function df_dbcvalues(){
1689 $dbcdata = array(); // dummy array for result
1690 $ztn = 180202 ; // default zorgtraject at closing ()
1691 $today = date('Y-m-d');
1692 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1694 if ( mysql_num_rows($q) ) {
1695 $count = 1;
1696 global $rfsum;
1697 while ( $row = mysql_fetch_array($q) ) {
1698 $dbcid = $row['ax_id'];
1699 $odate = $row['ax_odate'];
1701 $rfsum = 0; // reset the rfsum!
1702 dt_main(1, $dbcid, $today);
1703 $z = dt_whatproductgroep($rfsum, $odate);
1704 $pcode = $z['id'];
1706 $prestatie = dt_prestatiecode($ztn, $pcode, $dbcid, 1);
1708 $declaratie = df_declaratie($prestatie, $today);
1709 $tariff = df_tariff($declaratie, $today);
1711 $dedamo = ( vk_is_overloop_dbc($dbcid) ) ? vk_deduction($dbcid) : 0;
1713 $tariff_per = round (((int)$tariff * C417) / 10000);
1714 $tariff_final = $tariff_per - $dedamo;
1717 $dbcdata[$count]['pid'] = what_patient($dbcid);
1718 $dbcdata[$count]['dbcid'] = $dbcid;
1719 $dbcdata[$count]['rfsum'] = $rfsum;
1720 $dbcdata[$count]['pcode'] = $pcode;
1721 $dbcdata[$count]['prestatie'] = $prestatie;
1722 $dbcdata[$count]['declaratie'] = $declaratie;
1723 $dbcdata[$count]['tariff'] = $tariff_final;
1724 $dbcdata[$count]['odate'] = $row['ax_odate'];
1726 $count++;
1727 } // while
1730 return $dbcdata;
1735 //-----------------------------------------------------------------------------
1737 * DECLARATIE CODE
1739 * used by df_dbcvalues()
1741 * @param
1742 * @return
1744 function df_declaratie($pcode, $cdate) {
1745 $q = sprintf("SELECT cl_declaratiecode AS cd FROM cl_prestatiecode WHERE cl_dbc_prestatiecode = '%s'
1746 AND cl_prestatiecode_begindatum <= '%s' AND cl_prestatiecode_einddatum >= '%s' ", $pcode, $cdate, $cdate);
1747 $r = mysql_query($q) or die(mysql_error());
1749 if ( $row = mysql_fetch_array($r) ) {
1750 $retval = $row['cd'];
1751 } else {
1752 $retval = 0;
1755 return $retval;
1759 function df_tariff($decode, $date) {
1760 $q = sprintf("SELECT cl_dbc_tarief FROM cl_dbc_tarief WHERE cl_declaratiecode = '%s'
1761 AND cl_dbc_tarief_begindatum <= '%s' AND cl_dbc_tarief_einddatum >= '%s' ", $decode, $date, $date);
1762 $r = mysql_query($q) or die(mysql_error());
1764 if ( mysql_num_rows($r) ) {
1765 $row = mysql_fetch_array($r); $val = $row['cl_dbc_tarief'];
1766 } else {
1767 $val = 0;
1770 return $val;
1773 //-----------------------------------------------------------------------------
1775 * RETURN PATIENTS WITH OPENED ZTN BUT NO OPENED DBC
1778 * @param
1779 * @return
1781 function df_opztn_nodbc() {
1782 $q = sprintf("SELECT id FROM patient_data WHERE 1");
1783 $r = mysql_query($q) or die(mysql_error());
1785 while ( $row = mysql_fetch_array($r) ) {
1786 $pid = $row['id'];
1788 $result = has_ztndbc($pid);
1789 if ( $result['code'] ) {
1790 $allztn = all_ztn($pid);
1791 $lastztn = end($allztn);
1793 $alldbc = all_dbc($lastztn['cn_ztn']);
1794 $lastdbc = end($alldbc);
1796 if ( !$lastdbc['ax_open'] ) {
1797 $pidres[$pid]['result'] = $result;
1798 $pidres[$pid]['dbc'] = $lastdbc;
1799 $pidres[$pid]['ztn'] = $lastztn;
1800 } // if
1802 } // while
1804 return $pidres;
1807 //-----------------------------------------------------------------------------