DBC Dutch System
[openemr.git] / library / DBC_functions.php
blob1371be79495fd6363fd7bf50ac2a08ffbe022338
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 require_once(dirname(__FILE__) . '/../interface/globals.php');
15 require_once(dirname(__FILE__) . '/sql.inc');
16 require_once('DBC_cfunctions.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']);
49 $r = mysql_query($q) or die(mysql_error());
50 if ( mysql_num_rows($r) ) {
51 while ( $row = mysql_fetch_array($r) ) {
52 $a .="<option value='{$row['code']}'>{$row['elem']}</option>";
54 } else {
55 $a .= '<option value="0"></option>';
58 echo $a;
59 //echo dirname(__FILE__).'/../interface/globals.php';
64 //-----------------------------------------------------------------------------
65 /**
66 * VERIFY SELECTEERBAAR
68 * this field must be equal to 1 for saving into table
69 * @param int $vcode - element code
71 function verify_code($vcode, $where = 1) {
72 if ( $where == 1) {
73 $q = sprintf("SELECT cl_activiteit_selecteerbaar AS slbar, cl_activiteit_sysid AS sysid
74 FROM cl_activiteit WHERE cl_activiteit_code = '%s'", $vcode);
75 } elseif ( $where == 2) {
76 $q = sprintf("SELECT cl_diagnose_selecteerbaar AS slbar, cl_diagnose_sysid AS sysid
77 FROM cl_diagnose WHERE cl_diagnose_code = '%s'", $vcode);
80 $r = mysql_query($q) or die(mysql_error());
81 if ( mysql_num_rows($r) ) {
82 $row = mysql_fetch_array($r);
83 return ( $row['slbar'] == 0 ) ? FALSE : TRUE;
84 } else {
85 return FALSE;
89 //-----------------------------------------------------------------------------
90 /**
91 * WHAT SYSID/NAME
93 * find the sysid for a code in activiteit table
94 * OR the name based on a known sysid
95 * sysid must be unique in this table
97 * @param string $code - activity's code
98 * @param string $sysid - activity's sysid
99 * @return int|string|bool(false)
101 function what_sysid($code = 0, $sysid = 0) {
102 if (!$code && !$sysid) return FALSE;
104 if ( isset($_SESSION['event_date']) && $_SESSION['event_date']) {
105 $today = $_SESSION['event_date'];
106 } else {
107 $today = date('Y-m-d');
110 if ( $code ) {
111 $q = sprintf("SELECT cl_activiteit_sysid AS sysid
112 FROM cl_activiteit WHERE cl_activiteit_code = '%s' AND
113 cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'", $code, $today, $today);
114 } elseif ( $sysid ) {
115 $q = sprintf("SELECT cl_activiteit_beschrijving AS besc
116 FROM cl_activiteit WHERE cl_activiteit_sysid = %d AND
117 cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'", $sysid, $today, $today);
120 $r = mysql_query($q) or die(mysql_error());
121 if ( mysql_num_rows($r) ) {
122 $row = mysql_fetch_array($r);
123 if ( $code ) return $row['sysid'];
124 else if ($sysid) return $row['besc'];
125 } else {
126 return false;
132 //-----------------------------------------------------------------------------
134 * FULL NAME FOR A SYSID
136 * @param int $sysid (cl_activiteit_sysid)
137 * @return
139 function what_full_sysid($sysid) {
140 if ( !$sysid ) return FALSE;
141 $full_str = '';
143 $parent = what_groepcode_activiteit($sysid);
145 if ( $parent['parent'] ) {
146 $full_str .= what_sysid(0, $sysid) .' - '; // first sysid (not included in reccursive)
147 rec_parent_activiteit($parent['parent']); // produce the rest of the string
148 $full_str .= $GLOBALS['full_name_activity']; // concatenate the above 2 parts
149 } else {
150 // NO PARENT; THIS IS THE FIRST IN LINE
151 $full_str = what_sysid(0, $parent['sysid']);
154 return $full_str;
158 //-----------------------------------------------------------------------------
160 * WHAT CODE FOR AN ACTIVITY
162 * find the code for an activity
164 * @param string $sysid - activity's code
165 * @return string
167 function what_code_activity($sysid = 0) {
168 if ( !$sysid ) return FALSE;
170 $q = sprintf("SELECT * FROM cl_activiteit WHERE cl_activiteit_sysid = %d", $sysid);
171 $r = mysql_query($q) or die(mysql_error());
173 if ( mysql_num_rows($r) ) {
174 $row = mysql_fetch_array($r);
175 return $row['cl_activiteit_code'];
176 } else {
177 return '';
182 //-----------------------------------------------------------------------------
184 * THE PARENT RECORD CODE FOR A SYSID
186 * Look for the record with code / sysid
188 * @param int $sysid (cl_activiteit_sysid)
189 * @param string $code (cl_activiteit_code)
190 * @return array - contains the parent code and its sysid
192 function what_groepcode_activiteit($sysid = 0, $code = '') {
193 if ( !$sysid && !$code ) return FALSE;
194 $today = date('Y-m-d');
195 $rez = array('parent' => '', 'sysid' => '');
197 if ( $sysid ) {
198 $q = sprintf("SELECT cl_activiteit_groepcode AS clag
199 FROM cl_activiteit
200 WHERE cl_activiteit_sysid = %d AND cl_activiteit_begindatum < '%s' AND cl_activiteit_einddatum > '%s'", $sysid, $today, $today);
201 } else if ( $code ) {
202 $q = sprintf("SELECT cl_activiteit_groepcode AS clac, cl_activiteit_sysid AS clsy
203 FROM cl_activiteit
204 WHERE cl_activiteit_code = '%s' AND cl_activiteit_begindatum < '%s' AND cl_activiteit_einddatum > '%s' ", $code, $today, $today);
207 $r = mysql_query($q) or die(mysql_error());
208 if ( mysql_num_rows($r) ) {
209 $row = mysql_fetch_array($r);
210 if ( $sysid) {
211 $rez['parent'] = trim($row['clag']); $rez['sysid'] = (int)$sysid;
212 } else {
213 // array who contains 1 - the parent code 2 - the record sysid (used to find its name)
214 $rez['parent'] = trim($row['clac']); $rez['sysid'] = $row['clsy'];
217 return $rez;
218 } else {
219 return false;
225 //-----------------------------------------------------------------------------
227 * RECURSIVE FUNCTION
229 * @param string $parent - groepcode
230 * @return void
232 function rec_parent_activiteit($parent, $string = '') {
233 $next = what_groepcode_activiteit(0, $parent);
234 $string .= what_sysid(0, $next['sysid']) .' - ';
236 if ( $next['parent'] ) {
237 rec_parent_activiteit($next['parent'], $string);
238 } else {
239 // write the result in a SESSION VARIABLE
240 $GLOBALS['full_name_activity'] = $string; return 1;
245 //-----------------------------------------------------------------------------
247 * RETURNS THE RECORDS FOR LEVEL 1 (main activities)
249 * @param string $what ev - addeditevent, as - axes selection
250 * @param integer $gaf - used only for AS5
251 * @return array $result - contains arrays
253 function records_level1 ($what = 'ev', $gaf = 0) {
254 $result = array();
255 $today = date('Y-m-d');
256 mysql_query("SET NAMES utf8");
259 switch ( $what ) {
260 case 'ev' : // ADD EDIT EVENT
261 $q = sprintf("SELECT cl_activiteit_beschrijving, cl_activiteit_sysid, cl_activiteit_code, cl_activiteit_element
262 FROM cl_activiteit
263 WHERE cl_activiteit_hierarchieniveau = 1
264 AND cl_activiteit_einddatum > '%s' AND cl_activiteit_begindatum < '%s'",
265 $_SESSION['event_date'], $_SESSION['event_date']);
266 break;
268 case 'as1' : // AXES CASE
269 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
270 FROM cl_diagnose
271 WHERE cl_diagnose_as = 1 AND cl_diagnose_hierarchieniveau = 2
272 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
273 ORDER BY cl_diagnose_element",
274 $today, $today);
275 break;
277 case 'as2' : // AXES CASE
278 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
279 FROM cl_diagnose
280 WHERE cl_diagnose_as = 2 AND cl_diagnose_hierarchieniveau = 2
281 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
282 ORDER BY cl_diagnose_element",
283 $today, $today);
284 break;
286 case 'as3' : // AXES CASE
287 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
288 FROM cl_diagnose
289 WHERE cl_diagnose_as = 3
290 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'",
291 $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
292 break;
294 case 'as4' : // AXES CASE
295 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
296 FROM cl_diagnose
297 WHERE cl_diagnose_as = 4
298 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'
299 ORDER BY cl_diagnose_element",
300 $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
301 break;
303 case 'as5' : // AXES CASE
304 $groepcode = 'as5_0' . (int)$gaf;
305 $q = sprintf("SELECT cl_diagnose_beschrijving, cl_diagnose_sysid, cl_diagnose_code, cl_diagnose_element
306 FROM cl_diagnose
307 WHERE cl_diagnose_as = 5 AND cl_diagnose_groepcode = '%s'
308 AND cl_diagnose_einddatum > '%s' AND cl_diagnose_begindatum < '%s'",
309 $groepcode, $today, $today); // cl_dia..._hierarchieniveau is 1 in this case, not 2
310 break;
313 $result[] = '';
314 $r = mysql_query($q) or die( mysql_error() );
315 while ( $row = mysql_fetch_array($r) ) {
316 $result[] = $row;
319 //echo "<pre>" . print_r($result, true) . "</pre>"; // debug
320 return $result;
323 //-----------------------------------------------------------------------------
325 * FIND SAVED ACTIVITY FOR AN EVENT
327 * @param $eid - event id
328 * @return string|bool(false) - sysid for that event|false
330 function what_activity($eid) {
331 $q = sprintf("SELECT activity_sysid FROM cl_event_activiteit WHERE event_id= %d ", $eid);
332 $r = mysql_query($q) or die(mysql_error());
333 if ( $r ) {
334 $row = mysql_fetch_array($r);
335 return $row['activity_sysid'];
336 } else {
337 return FALSE;
341 //-----------------------------------------------------------------------------
343 * RETURN THE NAME OF A DIAGNOSE
345 * @param string $ascode
346 * @return string
348 function what_as($ascode) {
349 mysql_query("SET NAMES utf8");
350 $q = sprintf("SELECT cl_diagnose_beschrijving FROM cl_diagnose WHERE cl_diagnose_code = '%s'", $ascode);
351 $r = mysql_query($q) or die( mysql_error() );
352 $row = mysql_fetch_array($r);
353 return $row['cl_diagnose_beschrijving'];
357 //-----------------------------------------------------------------------------
359 * GENERATE AN UNIQUE PATIENT NUMBER
361 * number user in DBC is generated at a new client
363 * @param int $pid - patient id
364 * @return none
366 function generate_id1250($pid) {
367 if ($pid) {
368 mysql_query("START TRANSACTION");
369 $date = date('Ymd');
370 // retrieve last allocated number for this day
371 // let's check if for today there is record
372 $qd = sprintf("SELECT aux_varc FROM cl_aux WHERE aux_id='dn_id1250'");
373 $rd = mysql_query($qd) or die(mysql_error());
374 $rez = mysql_fetch_array($rd);
376 // if strings are NOT equal, then we must update the date field with today value
377 // and reset the counter (aux_varn) to 0
378 if ( $date !== trim($rez['aux_varc']) ) {
379 $qc = sprintf("UPDATE cl_aux SET aux_varc = '%s', aux_varn = 0 WHERE aux_id='dn_id1250'", $date);
380 mysql_query($qc) or die(mysql_error());
383 $nq = mysql_query("SELECT aux_varn FROM cl_aux WHERE aux_id='dn_id1250'") or die(mysql_error());
384 $nrow = mysql_fetch_array($nq);
385 $current_number = $nrow['aux_varn'] + 1;
386 $current_number = strval($current_number);
388 // prepare new ID1250 number (date + 6 digits)
389 $np = str_pad($current_number, 6, '0', STR_PAD_LEFT);
390 $ns = $date . $np;
392 // as an extra caution measure, we check for openemrid and newly generated number
393 // in table; these must be unique
394 $check = sprintf("SELECT * FROM cl_patient_number WHERE pn_oemrid = %d OR pn_id1250 = '%s'",
395 $pid, $ns);
396 $rezcheck = mysql_query($check) or die(mysql_error());
397 if ( !mysql_num_rows($rezcheck) ) {
398 // insert into db
399 $q = sprintf("INSERT INTO cl_patient_number VALUES ('%d', '%s')", $pid, $ns);
400 mysql_query($q) or die(mysql_error());
402 // update auxiliary table
403 $qu = sprintf("UPDATE cl_aux SET aux_varn = aux_varn + 1 WHERE aux_id='dn_id1250'");
404 mysql_query($qu) or die(mysql_error());
405 } else {
406 echo '<script>alert("The generated number or patient id already exists!")</script>';
408 mysql_query("COMMIT");
409 }//if pid
413 //-----------------------------------------------------------------------------
415 * GET AN ID FOR A PATIENT
417 * @param string $whatid
418 * @param int $pid
420 function get_id($whatid, $pid) {
421 $q = FALSE;
422 switch ($whatid) {
423 case 'id1250':
424 $q = sprintf("SELECT pn_id1250 FROM cl_patient_number WHERE pn_oemrid = %d", $pid);
425 break;
428 if ( $q ) {
429 $res = mysql_query($q) or die(mysql_error());
430 $row = mysql_fetch_row($res);
431 return $row[0];
432 } else {
433 return FALSE;
437 //-----------------------------------------------------------------------------
439 * GENERATE A CARE ROUTE NUMBER - ID 1007
440 * (ZORGTRAJECTNUMMER)
442 * this function actually checks for an opened CRN (ZTN - zorgtrajectnummer),
443 * and returns it; only if it doesn't exist generate a new one
445 * @param int $pid
446 * @param string $eventdate
447 * @return string - CRN(ZTN) if exists; new CRN(ZTN)
449 function generate_id1007($pid, $eventdate) {
450 if ($pid) {
451 mysql_query("START TRANSACTION");
452 // check for previous opened CRN
453 $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $pid);
454 $rez = mysql_query($qc) or die(mysql_error());
455 if ( mysql_num_rows($rez) ) {
456 $row = mysql_fetch_array($rez);
457 return $row['cn_ztn'];
458 } else {
459 // generate a new one
460 // get last value from auxiliary table
461 $raux = mysql_query("SELECT aux_varn FROM cl_aux WHERE aux_id='dn_id1007'") or die(mysql_error());
462 $rez = mysql_fetch_array($raux);
463 $current_val = strval($rez['aux_varn'] + 1);
464 $ztn = str_pad($current_val, 7, '0', STR_PAD_LEFT);
466 // insert new values into cl_careroute_numbers table and set it to opened
467 $qi = sprintf("INSERT INTO cl_careroute_numbers(cn_ztn, cn_pid, cn_dopen, cn_open)
468 VALUES('%s', %d, '%s', %d)", $ztn, $pid, $eventdate, 1);
469 mysql_query($qi);
470 $newid = mysql_insert_id();
472 // update value from aux table
473 $qu = sprintf("UPDATE cl_aux SET aux_varn = aux_varn + 1 WHERE aux_id='dn_id1007'");
474 mysql_query($qu) or die(mysql_error());
476 return $newid;
478 mysql_query("COMMIT");
479 } // if pid
482 //-----------------------------------------------------------------------------
484 * SAVE DBC RECORDS
486 * @param none
487 * @return void
489 function save_dbc() {
490 if ( !vl_validate_diagnoses() ) {
491 echo '<script>alert("One or more diagnoses are NOT valid or the diagnoses are NOT UNIQUE!")</script>';
492 return FALSE;
495 if ( $_SESSION['save'] ) {
497 mysql_query("START TRANSACTION");
499 // check for an opened ztn
500 $opened_ztn = verify_ztn();
502 if ( !$opened_ztn ) $opened_ztn = generate_id1007($_SESSION['pid'], date('Y-m-d')); // if it's close, create a new one
504 // insert a new DBC Route
505 $today = date('Y-m-d');
506 if ( $opened_ztn ) {
507 // as a caution we check for posas. must be NOT null
508 if ( !$_SESSION['posas'] ) $_SESSION['posas'] = 1; // default value
510 // at as1 and as2 we must add radio postion for Main Diagnose
511 $as1arr['content'] = $_SESSION['as1'];
512 $as1arr['mainpos'] = (int)$_SESSION['posas'];
513 $as2arr['content'] = $_SESSION['as2'];
514 $as2arr['mainpos'] = (int)$_SESSION['posas'];
515 $circode = (int)$_SESSION['circuitcode'];
517 if ( $_SESSION['show_axid'] ) {
518 // ----------------------
519 // edit case
520 $q = sprintf("UPDATE cl_axes
521 SET ax_as1 = '%s', ax_as2 = '%s', ax_as3 = '%s', ax_as4 = '%s', ax_as5 = '%s' WHERE ax_id = %d",
522 serialize($as1arr), serialize($as2arr), serialize($_SESSION['as3']), serialize($_SESSION['as4']),
523 serialize($_SESSION['as5']), $_SESSION['show_axid'] );
524 mysql_query($q) or die(mysql_error());
526 // edit circuit-dbc connection
527 $icircode = ( vl_validate_circuit($circode) ) ? $circode : '1' ; // validate the circuit
528 $qcir = sprintf("INSERT INTO cl_circuit_dbc(ccd_dbcid, ccd_circuitcode) VALUES(%d, %d) ON DUPLICATE KEY
529 UPDATE ccd_circuitcode = %d", $_SESSION['show_axid'], $icircode, $icircode);
530 mysql_query($qcir) or die(mysql_error());
532 } else {
533 // ----------------------
534 // new case
536 // if it's the first DBC, open date is given by the event with @ flag (pc_apptstatus) after ZTN was opened)
537 if ( first_dbc($_SESSION['show_axid']) ) {
538 // open date for ztn
539 $qztn = sprintf("SELECT cn_dopen FROM cl_careroute_numbers WHERE cn_ztn = '%s' ", $opened_ztn);
540 $rztn = mysql_query($qztn) or die (mysql_error());
541 $ztarr = mysql_fetch_array($rztn); $ztnodate = $ztarr['cn_dopen'];
543 // find first event who is visit (with @ flag after $ztnodate)
544 $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']);
545 $rv = mysql_query($qv) or die (mysql_error());
547 $earr = mysql_fetch_array($rv);
548 $opendate = $earr['mindate'];
550 // no encounter found so we cannot insert a DBC
551 if ( !$opendate ) {
552 echo "<script>alert('No Encounter found! Please generate first at least one encounter.')</script>";
553 exit();
556 } else {
557 $opendate = $today;
560 $q = sprintf("INSERT INTO cl_axes (ax_ztn, ax_open, ax_as1, ax_as2, ax_as3, ax_as4, ax_as5, ax_odate)
561 VALUES('%s', %d, '%s', '%s', '%s', '%s', '%s', '%s')",
562 $opened_ztn, 1, serialize($as1arr), serialize ($as2arr), serialize ($_SESSION['as3']), serialize ($_SESSION['as4']), serialize ($_SESSION['as5']), $opendate);
563 mysql_query($q) or die(mysql_error()); // leave it here that next line to work
565 // new circuit-dbc connection
566 $rlast = mysql_insert_id();
567 $icircode = ( vl_validate_circuit($circode) ) ? $circode : '1' ; // validate the circuit
568 $qcir = sprintf("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES(%d, %d)", $icircode, $rlast);
569 mysql_query($qcir) or die(mysql_error());
571 } // new | edit case
573 } else {
574 echo '<script>alert("No ZTN selected!")</script>'; return FALSE;
577 // reset implied session variables
578 $_SESSION['save'] = TRUE;
579 for ($i = 1; $i <= 5; $i++) {
580 $_SESSION["as$i"] = '';
583 mysql_query("COMMIT");
585 return TRUE;
587 } //if
590 //-----------------------------------------------------------------------------
592 * VERIFY ZTN
594 * verify for a patient if a ZTN is already opened
595 * return the number of ztn if true, false otherwise
597 * @param $pid - optionally; if provided, we'll ignore the session var
598 * @return string $opened_ztn | bool false
600 function verify_ztn($pid = 0){
602 // patient id could be taken from a param or from a session var
603 $cn_pid = ( $pid ) ? $pid : $_SESSION['pid'];
605 // search for an opened ZTN (id1007)
606 $qc = sprintf("SELECT cn_ztn FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $cn_pid);
607 // echo $qc; // debug
608 $rez = mysql_query($qc) or die(mysql_error()); $opened_ztn = '';
609 if ( mysql_num_rows($rez) ) {
610 $row = mysql_fetch_array($rez);
611 $opened_ztn = $row['cn_ztn'];
612 return $opened_ztn;
613 } else {
614 return false;
619 //-----------------------------------------------------------------------------
621 * LIST ZTN's
623 * lists ZTN's from a patient
625 * @param int $mode 0-all 1-only opened 2-only closed
627 function lists_ztn($mode = 0){
628 switch ($mode) {
629 case 0: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $_SESSION['pid']); break;
630 case 1: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1' ORDER BY cn_dopen", $_SESSION['pid']); break;
631 case 2: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '0' ORDER BY cn_dopen", $_SESSION['pid']); break;
632 default: $qc = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $_SESSION['pid']); break;
635 $artn = array();
636 $rez = mysql_query($qc) or die(mysql_error());
637 if ( mysql_num_rows($rez) ) {
638 while ( $row = mysql_fetch_array($rez)) {
639 $artn[] = $row;
641 return $artn;
642 } else {
643 return false;
647 //-----------------------------------------------------------------------------
649 * LIST DIAGNOSES
651 * lists diagnoses for a ZTN
652 * if called for last, this means the open dbc
654 * @param string ax_odate DESC default, 'last' for the last one (ordered by date)
655 * @return array | bool(false)
657 function lists_diagnoses($order = 'ax_odate DESC'){
659 // obtain opened ztn
660 $ztn = verify_ztn();
661 if ( $order == 'last') {
662 $qc = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 1 ORDER BY ax_odate DESC ", $ztn);
663 } else {
664 $qc = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' ORDER BY %s ", $ztn, $order);
667 $artn = array();
668 $rez = mysql_query($qc) or die(mysql_error());
670 if ( mysql_num_rows($rez) ) {
671 while ( $row = mysql_fetch_array($rez)) {
672 $artn[] = $row;
674 return $artn;
675 } else {
676 return false;
680 //-----------------------------------------------------------------------------
682 * LAST DIAGNOSE
684 * specific function to return the last entered diagnose
685 * a.k.a. the open DBC
687 function last_diagnose() {
688 $arr = lists_diagnoses('last');
689 return $arr[0];
692 //-----------------------------------------------------------------------------
694 * ZORGTYPE CODES
696 * @param none
697 * @return array
699 function zorgtype_codes() {
700 mysql_query("SET NAMES utf8");
701 $check = first_dbc($_SESSION['show_axid']);
702 if ( $check ) { //first dbc
703 $qc = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_groepcode = 100 AND cl_zorgtype_selecteerbaar = 1");
704 } else {
705 $qc = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_groepcode = 200 AND cl_zorgtype_selecteerbaar = 1");
707 $rez = mysql_query($qc) or die(mysql_error());
709 $ra = array();
710 while ($r = mysql_fetch_assoc($rez)) {
711 $ra[] = $r;
714 return $ra;
717 //-----------------------------------------------------------------------------
719 * FIRST DBC?
721 * find if a dbc is the first , a 'follow-up' or there is no DBC yet.
722 * (the same function is in DBC_files)
724 * @param none
725 * @return bool - true if it's the first, 0 - otherwise
727 function first_dbc($ax_id) {
728 // to be the first means there is only one DBC per open ZTN
729 $openztn = verify_ztn();
731 // to be the first means there is only one DBC per open ZTN
732 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s' AND ax_id < %d", $openztn, $ax_id);
733 $rez = mysql_query($qz) or die(mysql_error());
735 return ( !mysql_num_rows($rez) );
739 //-----------------------------------------------------------------------------
741 * STATUS FOR A ZTN
743 * find if a dbc is the first , a 'follow-up' or there is no DBC yet.
744 * retcode 0 - no ZTN open
745 * 1 - ZTN open, no DBC
746 * 2 - ZTN open, (just) one opened DBC
747 * 3 - ZTN open, (just) one closed DBC
748 * 4 - ZTN open, more DBC's
750 * @param none
751 * @return int - for $retcode see above
753 function ztn_status() {
755 // return the open ztn if any
756 $openztn = verify_ztn();
758 if ( $openztn ) {
759 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s'", $openztn);
760 $rez = mysql_query($qz) or die(mysql_error());
761 $rows = mysql_num_rows($rez);
763 switch ($rows) {
764 case 0: $retcode = 1; break; // no rows, means no DBC in ZTN
765 case 1:
766 $ro = mysql_fetch_array($rez);
767 $retcode = ( $ro['ax_open'] ) ? 2 : 3 ;
768 break; // just one row, means one DBC (initial) in ZTN
769 default: $retcode = 4; // any other numbers means more DBC's, so we have initial + follow's up
771 } else {
772 $retcode = 0;
775 return $retcode;
779 //-----------------------------------------------------------------------------
781 * REDENSLUITEN CODES
783 * @param none
784 * @return array - an array with all redensluiten codes
786 function reden_codes() {
787 if ( $_SESSION['eind'] ) {
788 mysql_query("SET NAMES utf8");
789 $eind = mysql_real_escape_string($_SESSION['eind']);
790 $qc = sprintf("SELECT * FROM cl_redensluiten clr WHERE clr.cl_redensluiten_begindatum < '%s' AND clr.cl_redensluiten_einddatum >'%s' ", $eind, $eind);
791 $rez = mysql_query($qc) or die(mysql_error());
793 $ra = array();
794 while ($r = mysql_fetch_assoc($rez)) {
795 $ra[] = $r;
798 return $ra;
799 }//if
802 //-----------------------------------------------------------------------------
804 * CHECK FOR 'SENT TO INSURER' CASE
806 * check if the open DBC was already sent to insurer or not
808 * @param none
809 * @return bool
811 function sent_to_insurer() {
812 $ldbc = last_diagnose(1); // return open DBC
813 return ( $ldbc['ax_sti'] ) ? TRUE : FALSE;
817 //-----------------------------------------------------------------------------
819 * LOAD OPEN DBC
821 * load the values from the open dbc into session variables
822 * to edit form
824 function load_dbc() {
825 $ldbc = last_diagnose(1);
826 $as1 = unserialize($ldbc['ax_as1']);
827 $_SESSION['as1'] = $as1['content']; $_SESSION['posas'] = (int)$as1['mainpos'];
829 $as2 = unserialize($ldbc['ax_as2']);
830 $_SESSION['as2'] = $as2['content'];
832 $_SESSION['as3'] = unserialize($ldbc['ax_as3']);
833 $_SESSION['as4'] = unserialize($ldbc['ax_as4']);
834 $_SESSION['as5'] = unserialize($ldbc['ax_as5']);
837 //-----------------------------------------------------------------------------
839 * CLOSE AN OPEN DBC
841 * @param int $follow - specify if a DBC is followed by another one
842 * created with the same content, or close + ZTN close
843 * @param int $stoornis - $_POST['stoornis']
844 * @param int $ztc - $_POST['ztc']
845 * @param int $rtc - $_POST['rtc']
846 * @param array $gaf - array with 2 elements (hoogste/eind)
847 * @param int $dbcid - dbcid if called from a script
848 * @param string $eind - closing date if called from a script
849 * @param int $patid - patient id if called from a script
850 * @return void
852 function close_dbc($follow = 0, $stoornis = 0, $ztc, $rtc, $gaf, $dbcid = 0, $eind = '', $patid = 0) {
853 mysql_query('START TRANSACTION');
855 $einddate = ( $dbcid ) ? $eind : $_SESSION['eind'];
856 $dbc_id = ( $dbcid ) ? $dbcid : $_SESSION['show_axid'];
857 $pid = ( $dbcid ) ? $patid : $_SESSION['pid'];
859 // close the open dbc; also mark for vektis
860 // $q = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s', ax_vkstatus = 1
861 $q = sprintf("UPDATE cl_axes SET ax_open = 99, ax_cdate = '%s', ax_vkstatus = 1
862 WHERE ax_id = %d", $einddate, $dbc_id);
863 mysql_query($q) or die(mysql_error());
865 // update for the current
866 // NOTE: we must run this here because the following statement
867 // retrieves the content for DBC; otherwise the duplicated dbc will not contain
868 // the new $gaf array
869 update_gaf($dbc_id, $gaf);
871 // this MUST be run after update_gaf
872 $ldbc = content_diagnose($dbc_id);
874 // write the closing parameters from dbc_sluiten form
875 write_stoornis($stoornis, $dbc_id);
876 write_zorg($ztc, $dbc_id);
877 write_reden($rtc, $dbc_id);
879 // generate prestatie code
880 dt_prestatiecode($ztc, $stoornis, $dbc_id);
882 // open a new one if it is the case
883 if ( $follow ) {
884 $newid = duplicate_dbc($ldbc);
885 if ( !$dbcid ) $_SESSION['show_axid'] = $newid;
886 } else {
887 $qztn = sprintf("UPDATE cl_careroute_numbers SET cn_open = 0, cn_dclosed = '%s'
888 WHERE cn_pid = %d", $einddate, $pid);
889 mysql_query($qztn) or die(mysql_error());
891 if ( !$dbcid ) $_SESSION['show_axid'] = FALSE;
894 mysql_query('COMMIT');
897 //-----------------------------------------------------------------------------
899 * TOTAL TIME SPENT
901 * adds all direct, indirect + travel time from the first day of this DBC to the last day
902 * only for encounters (events with @ pc_apptstatus)
904 * @param int $dbcid - if used instead of $_SESSION
905 * @return array - contains the three times (indirect, travel, total)
907 function total_time_spent($dbcid = 0, $btime = '', $etime = '') {
908 // our big results
909 $total_time = 0 ; $indirect_time = 0 ; $travel_time = 0;
911 // DBC ID
912 // we have a session var or a given value?
913 $dbc = ( $dbcid ) ? $_SESSION['show_axid'] : $dbcid;
915 // begin date for DBC
916 if ( $btime ) {
917 $bd_dbc = $btime;
918 } else {
919 $cd = content_diagnose($dbc);
920 $bd_dbc = $cd['ax_odate'];
923 // end date of DBC
924 if ( $etime ) {
925 $ed_dbc = $etime;
926 } else {
927 $ed_dbc = ( $_SESSION['eind'] ) ? $_SESSION['eind'] : date('Y-m-d');
930 // if we have a $dbcid we must find a $pid
931 if ( $dbcid ) {
932 $pid = what_patient($dbcid);
933 } else {
934 $pid = $_SESSION['pid'];
937 // also, we don't check for first_dbc if we have $btime
938 if ( $btime == '2008-01-01' || $etime == '2007-12-31' ) {
939 $check_first = FALSE;
940 } else {
941 $check_first = TRUE;
944 // find all events between DBC's dates and sum up total times
945 $q = sprintf("SELECT pc_eid, pc_duration FROM openemr_postcalendar_events
946 WHERE pc_pid = '%s' AND pc_eventDate >= '%s' AND pc_eventDate <= '%s' AND pc_apptstatus = '@' ",
947 $pid, $bd_dbc, $ed_dbc);
949 $r = mysql_query($q) or die(mysql_error());
951 while ($row = mysql_fetch_array($r)) {
952 $total_time += $row['pc_duration'];
954 // get indirect+travel time
955 $q1 = sprintf("SELECT * FROM cl_time_activiteit WHERE event_id = %d", $row['pc_eid']);
956 $r1 = mysql_query($q1) or die(mysql_error());
957 if ( mysql_num_rows($r1) ) {
958 $row1 = mysql_fetch_array($r1);
959 $indirect_time += $row1['indirect_time'];
960 $travel_time += $row1['travel_time'];
962 } // while
964 // if it is the first DBC we look for previous events
965 // which weren't included in DBC and add timing too
966 if ( $check_first && first_dbc($dbc) ) {
967 // begin date of ZTN
968 $r = lists_ztn(1);
969 $bdztn = $r[0]['cn_dopen'];
971 // find all events between DBC's dates and sum up total times
972 $q = sprintf("SELECT pc_eid, pc_duration FROM openemr_postcalendar_events
973 WHERE pc_pid = '%s' AND pc_eventDate > '%s' AND pc_eventDate < '%s'",
974 $pid, $bdztn, $bd_dbc);
975 $r = mysql_query($q) or die(mysql_error());
976 while ($row = mysql_fetch_array($r)) {
977 $total_time += $row['pc_duration'];
978 // get indirect+travel time
979 $q2 = sprintf("SELECT * FROM cl_time_activiteit WHERE event_id = %d", $row['pc_eid']);
980 $r2 = mysql_query($q2) or die(mysql_error());
981 if ( mysql_num_rows($r2) ) {
982 $row2 = mysql_fetch_array($r2);
983 $indirect_time += $row2['indirect_time'];
984 $travel_time += $row2['travel_time'];
986 } // while
987 } // if
989 $total_time /= 60 ; //transform it to minutes from seconds
990 $total_time += $indirect_time + $travel_time;
992 $time = array ('total_time' => $total_time, 'indirect_time' => $indirect_time, 'travel_time' => $travel_time);
993 return $time;
996 //-----------------------------------------------------------------------------
998 * GENERATE STOORNIS(DISEASE) DROPDOWN
1000 * NOTE maybe not used anymore; there is an automatic procedure (DBC_decisiontree)
1002 * generates a dropdown for stoornis section in
1003 * close dbc module
1005 * @param string $odate - opening date for DBC (which is gonna be closed)
1006 * @return string
1008 function stoornis_dropdown($odate) {
1009 $s = '<select name="stoornis" id="stoornis">';
1011 mysql_query("SET NAMES utf8");
1012 $qc = sprintf("SELECT * FROM cl_productgroep clp
1013 WHERE clp.cl_productgroep_code_verblijf = 0
1014 AND clp.cl_productgroep_selecteerbaar = 1
1015 AND clp.cl_productgroep_begindatum < '%s' AND clp.cl_productgroep_einddatum > '%s' ", $odate, $odate);
1016 $rez = mysql_query($qc) or die(mysql_error());
1018 while ( $row = mysql_fetch_array($rez) ) {
1019 $s .= '<option value="' .$row['cl_productgroep_sysid']. '">' .$row['cl_productgroep_beschrijving']. '</option>';
1022 $s .= '</select>';
1023 return $s;
1026 //-----------------------------------------------------------------------------
1028 * GENERATE VERBLIJF(HOSPITAL STAY) DROPDOWN
1030 * generates a dropdown for verblijf section in close dbc module
1032 * @param return
1034 function verblijf_dropdown() {
1035 $s = '<select name="verblijf" id="verblijf">';
1036 $s .= '<option value="0">Geen Verblijf</option>';
1037 $qc = sprintf("SELECT * FROM cl_productgroep clp WHERE clp.cl_productgroep_code < 999
1038 AND clp.cl_productgroep_selecteerbaar = 1");
1039 $rez = mysql_query($qc) or die(mysql_error());
1041 while ( $row = mysql_fetch_array($rez) ) {
1042 $s .= '<option value="' .$row['cl_productgroep_sysid']. '">'.$row['cl_productgroep_beschrijving']. '</option>';
1045 $s .= '</select>';
1046 return $s;
1049 //-----------------------------------------------------------------------------
1051 * GENERATE CIRCUITCODE DROPDOWN
1053 * @param int - $sel the selected item
1054 * @return string
1056 function circuit_dropdown($sel = 0) {
1057 $today = date('Y-m-d');
1058 $s = '<select id="circuit" name="circuit">';
1060 mysql_query("SET NAMES utf8");
1061 $query = sprintf ("SELECT * FROM cl_circuit WHERE cl_circuit_begindatum < '%s' AND cl_circuit_einddatum > '%s'", $today, $today);
1062 $qr = mysql_query($query) or die (mysql_error());
1063 while ( $row = mysql_fetch_array($qr) ) {
1064 $selected = ( $row['cl_circuit_code'] == $sel ) ? 'selected="selected"' : '';
1065 $s .= '<option value="' .$row['cl_circuit_code']. '" ' .$selected. '>' .$row['cl_circuit_beschrijving']. '</option>';
1068 $s .= '</select>';
1069 return $s;
1072 //-----------------------------------------------------------------------------
1074 * HAS CIRCUIT
1076 * check if a dbc has a link with a circuit code in cl_circuit_dbc table
1078 * @param int $axid
1079 * @return int|bool - the circuit code or false
1081 function has_circuit($axid = 0) {
1082 if ( !$axid ) return 0;
1084 $qh = sprintf("SELECT * FROM cl_circuit_dbc WHERE ccd_dbcid = %d", $axid);
1085 $rh = mysql_query($qh) or die(mysql_error());
1087 $result = '';
1088 if ( mysql_num_rows($rh) ) {
1089 $row = mysql_fetch_array($rh);
1090 $result = $row['ccd_circuitcode'];
1091 } else {
1092 $result = 0;
1095 return $result;
1098 //-----------------------------------------------------------------------------
1100 * GET CIRCUIT CODE
1102 * return circuit code for a dbc
1104 * @param int dbc id
1105 * @param bool|int
1107 function get_circuitcode($dbcid) {
1108 if ( !$dbcid) return FALSE;
1110 mysql_query("SET NAMES utf8");
1111 $qc = sprintf("SELECT cl_circuit_beschrijving FROM cl_circuit clc
1112 JOIN cl_circuit_dbc clcd ON clcd.ccd_circuitcode = clc.cl_circuit_code
1113 WHERE clcd.ccd_dbcid = %d", $dbcid);
1114 $r = mysql_query($qc) or die(mysql_error());
1116 if ( mysql_num_rows($r) ) {
1117 $row = mysql_fetch_array($r);
1118 return $row['cl_circuit_beschrijving'];
1119 } else return FALSE;
1124 //-----------------------------------------------------------------------------
1126 * WRITE ZORG
1128 * write zorg sysid for a closing dbc
1130 * @param int - zorg sysid
1131 * @return none
1133 function write_zorg($zsysid) {
1134 // validate the code
1135 $izsysid = ( vl_validate_zorg($zsysid) ) ? $zsysid : 1 ;
1137 $qz = sprintf("INSERT INTO cl_zorg_dbc VALUES(%d, %d)", $_SESSION['show_axid'], $izsysid);
1138 $r = mysql_query($qz) or die(mysql_error());
1141 //-----------------------------------------------------------------------------
1143 * WRITE STOORNIS
1145 * save the value from stoornis dropdown
1147 * @param int - cl_productgroep_sysid
1148 * @return none
1150 function write_stoornis($zsysid) {
1151 $qz = sprintf("INSERT INTO cl_productgroep_dbc VALUES(%d, %d)", $zsysid, $_SESSION['show_axid']);
1152 $r = mysql_query($qz) or die(mysql_error());
1155 //-----------------------------------------------------------------------------
1157 * WRITE REDEN
1159 * @param int cl_redensluiten_code
1160 * @return void
1162 function write_reden($rcode) {
1163 // validate the code
1164 $ircode = ( vl_validate_redencode($rcode) ) ? $rcode : 1 ;
1166 $qz = sprintf("INSERT INTO cl_redensluiten_dbc VALUES(%d, %d)", $ircode, $_SESSION['show_axid']);
1167 $r = mysql_query($qz) or die(mysql_error());
1171 //-----------------------------------------------------------------------------
1173 * UPDATE GAF
1175 * When closing a DBC we still need two values from GAF dropdowns (middle and end).
1176 * The first one was on DBC opening.
1178 * @param int $dbcid
1179 * @param array $gaf
1180 * @return bool
1182 function update_gaf($dbcid, $gaf) {
1183 if ( !$dbcid ) return FALSE;
1185 $qz = sprintf("SELECT ax_as5 FROM cl_axes WHERE ax_id = %d", $dbcid);
1186 $rz = mysql_query($qz) or die(mysql_error());
1187 $rowrz = mysql_fetch_array($rz);
1189 // we update the missing values
1190 $unser = unserialize($rowrz['ax_as5']);
1191 $un['gaf1'] = $unser['gaf1'];
1192 $un['gaf2'] = mysql_real_escape_string($gaf['gaf2']);
1193 $un['gaf3'] = mysql_real_escape_string($gaf['gaf3']);
1194 $ser = serialize($un);
1195 // update the new values
1196 $qu = sprintf("UPDATE cl_axes SET ax_as5 = '%s' WHERE ax_id = %d", $ser, $dbcid);
1197 $ru = mysql_query($qu) or die(mysql_error());
1199 return TRUE;
1202 //-----------------------------------------------------------------------------
1204 * GET PRIMARY CARE PROVIDER INFOS
1206 * used at demographics_full
1207 * @param int patient_id
1208 * @return array
1210 function get_provider_DBC($pid = 0) {
1211 if ( !$pid ) return FALSE;
1212 $qz = sprintf("SELECT * FROM cl_providers WHERE pro_pid = %d", $pid);
1213 $r = mysql_query($qz) or die(mysql_error());
1214 return mysql_fetch_assoc($r);
1217 //-----------------------------------------------------------------------------
1219 * GET REFERER INFOS
1221 * used at demographics_full
1222 * @param int patient_id
1223 * @return array
1225 function get_referer_DBC($pid = 0) {
1226 if ( !$pid ) return FALSE;
1227 $qz = sprintf("SELECT * FROM cl_referers WHERE ref_pid = %d", $pid);
1228 $r = mysql_query($qz) or die(mysql_error());
1229 return mysql_fetch_assoc($r);
1233 //-----------------------------------------------------------------------------
1235 * RETURN JOB DESCRIPTION (BEROEP)
1237 * for a specified user
1239 * @param int user id
1240 * @param int $code - look for code or name field
1241 * @return string
1243 function what_beroep($pid = 0, $code = 0) {
1244 if ( !$pid ) return FALSE;
1246 $qz = sprintf("SELECT cl_beroep_element as cbe, cl_beroep_code as cbc
1247 FROM cl_beroep JOIN cl_user_beroep ON cl_beroep.cl_beroep_sysid = cl_user_beroep.cl_beroep_sysid
1248 WHERE cl_user_beroep.cl_beroep_userid = %d", $pid);
1249 $rz = mysql_query($qz) or die(mysql_error());
1250 $rez = mysql_fetch_assoc($rz);
1252 return ( $code ) ? $rez['cbc'] : $rez['cbe'];
1255 //-----------------------------------------------------------------------------
1257 * RETURN FULL DUTCH NAME
1259 * for a specified patient
1261 * @param int $pid - patient id
1262 * @return string $full_name
1264 function dutch_name($pid = 0) {
1265 if ( !$pid ) return FALSE;
1267 mysql_query("SET NAMES utf8");
1269 $qn = sprintf("SELECT fname, lname FROM patient_data WHERE id = %d", $pid);
1270 $rn = mysql_query($qn) or die(mysql_error());
1271 $rez = mysql_fetch_assoc($rn);
1273 $full_name = '';
1274 $first_name = $rez['fname'];
1275 $last_name = $rez['lname'];
1277 // then we look into patient_data_NL because it's not mandatory that there would be a record for this $pid
1278 // (so, don't use JOIN between these tables)
1279 $qn2 = sprintf("SELECT pdn_pxlast, pdn_pxlastpar, pdn_lastpar FROM patient_data_NL WHERE pdn_id = %d", $pid);
1280 $rn2 = mysql_query($qn2) or die(mysql_error());
1282 if ( mysql_num_rows($rn2) ) {
1283 $reznl = mysql_fetch_assoc($rn2);
1284 // partner is prefix + last name
1285 $partner = ( $reznl['pdn_lastpar'] ) ? $reznl['pdn_pxlastpar'] .' '. $reznl['pdn_lastpar'] .' - ' : '' ;
1286 $prefix = $reznl['pdn_pxlast'];
1287 // we make the full name
1288 $full_name = $first_name .' '. $partner . $prefix .' '. $last_name;
1289 } else {
1290 $full_name = $first_name .' '. $last_name;
1293 return $full_name;
1298 //-----------------------------------------------------------------------------
1300 * PREPARE STRINGS FOR UTF8
1302 * check if a string is UTF8 encoded; if not, convert it
1303 * (the string must be ISO-8859-1 because we use utf8_encode function)
1305 * @param string $string (utf8 or latin1)
1306 * @return string - converted utf8 string
1309 function sutf8($string)
1311 $rez = preg_match('%(?:
1312 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1313 |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1314 |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1315 |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1316 |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1317 |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1318 |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1319 )+%xs', $string);
1321 $newst = ( $rez ) ? $string : utf8_encode($string);
1322 return $newst;
1325 //-----------------------------------------------------------------------------
1327 * RETURN ALL THE ZTN's FOR A PATIENT
1329 * @param int $pid
1330 * @return array - contains all ztn's (closed, open) ; empty array if not ztn available
1332 function all_ztn($pid = 0) {
1333 if ( !$pid ) return FALSE;
1335 $result = array();
1336 $q = sprintf("SELECT * FROM cl_careroute_numbers WHERE cn_pid = %d ORDER BY cn_dopen", $pid);
1337 $r = mysql_query($q) or die( mysql_error() );
1338 if ( mysql_num_rows($r) ) {
1339 while ( $row = mysql_fetch_array($r) ) {
1340 $result[] = $row;
1344 return $result;
1347 //-----------------------------------------------------------------------------
1349 * RETURN ALL THE DBC's FOR A PATIENT
1351 * using ztn id
1353 * @param int $ztn
1354 * @return array - contains all dbc's (closed, open, sent to insurer) ; empty array if not ztn available
1356 function all_dbc($ztnid = 0) {
1357 if ( !$ztnid ) return FALSE;
1359 $result = array();
1360 $q = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' ORDER BY ax_odate", $ztnid);
1361 $r = mysql_query($q) or die( mysql_error() );
1362 if ( mysql_num_rows($r) ) {
1363 while ( $row = mysql_fetch_array($r) ) {
1364 $result[] = $row;
1368 return $result;
1371 //-----------------------------------------------------------------------------
1373 * OPEN ZTN/DBC?
1375 * verify for a patient if a ZTN is already opened
1376 * if it is, verify further if there is any open DBC
1378 * DBC opened - DBC open - 2
1379 * DBC closed - Geen DBC - 1
1380 * ZTN closed - Dossier gesloten - 0
1382 * @param $pid patient id
1383 * @return array - a string + a value
1385 function has_ztndbc($pid = 0){
1386 if ( !$pid ) return FALSE;
1387 $result = '';
1389 // search for an opened ZTN (id1007)
1390 $qc = sprintf("SELECT cn_ztn FROM cl_careroute_numbers WHERE cn_pid = %d AND cn_open = '1'", $pid);
1391 $rez = mysql_query($qc) or die(mysql_error());
1392 if ( mysql_num_rows($rez) ) {
1393 $row = mysql_fetch_array($rez);
1394 $opened_ztn = $row['cn_ztn'];
1396 $qb = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = '1' ", $opened_ztn);
1397 $rezb = mysql_query($qb) or die(mysql_error());
1399 $result['str'] = ( mysql_num_rows($rezb) ) ? 'DBC open.' : 'Geen DBC!';
1400 $result['code'] = ( mysql_num_rows($rezb) ) ? 2 : 1;
1402 } else {
1403 $result['str'] = 'Dossier gesloten!'; $result['code'] = 0;
1406 return $result;
1410 //-----------------------------------------------------------------------------
1412 * BEROEP DROPDOWN
1414 * build the dropdown for providers jobs
1415 * name: beroep id: beroep
1417 * @param int $selected
1418 * @return void - just echo the string - html encoded
1420 function beroep_dropdown($selected = 0){
1422 $string = '<select name="beroep" id="beroep">';
1423 $today = date('Y-m-d');
1425 $q = sprintf(" SELECT cl_beroep_element AS cbe, cl_beroep_sysid AS cbs
1426 FROM cl_beroep
1427 WHERE cl_beroep_selecteerbaar = 1
1428 AND cl_beroep_einddatum > '%s' AND cl_beroep_begindatum < '%s'
1429 ORDER BY cbe", $today, $today);
1430 $r = mysql_query($q) or die(mysql_error());
1431 while ( $row = mysql_fetch_array($r) ) {
1432 $sufix = ( $selected == $row['cbs']) ? 'selected="selected"' : '';
1433 $string .= "<option value='{$row['cbs']}' $sufix>{$row['cbe']}</option>";
1436 $string .= '</select>';
1437 echo $string;
1441 //-----------------------------------------------------------------------------
1443 * ZORGTYPECODES DROPDOWN
1445 * build the dropdown for zorg types codes
1446 * in dbc closing section (dbc_close.php)
1448 * name="ztc" id="ztc"
1450 * @param none
1451 * @return void - just echo the string - html encoded
1453 function zorgtype_dropdown(){
1455 $string = '<select name="ztc" id="ztc">';
1456 $today = date('Y-m-d');
1458 $ztc = zorgtype_codes();
1460 $display = 1;
1461 foreach($ztc as $z) {
1462 // zorgtype validation ; we try to exclude 180104 if the activities validation failed
1463 if ( $z['cl_zorgtype_sysid'] == 180104 && !vl_zorgtype_104() ) {
1464 $display = 0;
1466 // zorgtype validation ; we try to exclude 180104 if the direct time > 180
1467 if ( $z['cl_zorgtype_sysid'] == 180104 && !vl_zorgtype_880() ) {
1468 $display = 0;
1470 // zorgtype validation ; we try to exclude 180106 if the activities total time validation failed
1471 if ( $z['cl_zorgtype_sysid'] == 180106 && !vl_zorgtype_106() ) {
1472 $display = 0;
1474 // zorgtype validation ; we try to exclude 180111 if the providers job test failed
1475 if ( $z['cl_zorgtype_sysid'] == 180111 && !vl_zorgtype_111 () ) {
1476 $display = 0;
1479 if ( $display )
1480 $string .= '<option value="' .$z['cl_zorgtype_sysid']. '">' .$z['cl_zorgtype_beschrijving']. '</option>';
1482 $display = 1;
1484 $string .= '</select>';
1486 echo $string;
1490 //-----------------------------------------------------------------------------
1492 * PATIENT AGE
1494 * return the patient age
1495 * (!) uses a function from OpenEMR
1497 * @param int $pid - patient ID
1498 * @return int - age in years
1500 function patient_age($pid = 0){
1501 if ( !$pid ) return FALSE;
1503 // retrieve DOB for the patient
1504 $q = sprintf("SELECT DOB FROM patient_data WHERE id = %d ", $pid);
1505 $r = mysql_query($q) or die(mysql_error());
1506 $row = mysql_fetch_array($r);
1508 $dob = $row['DOB'];
1509 if ( 0 == $dob ) {
1510 vl_log("Patient with ID: $pid doesn't have DOB!"); return FALSE;
1513 $dobn = str_replace('-','', $dob);
1514 $age = getPatientAge($dobn); // function from library/patient.inc
1516 // $age can contain strings like 6 month, 8 month for age < 2 years old
1517 if ( is_string($age) ) $age = 2;
1519 return $age;
1522 //-----------------------------------------------------------------------------
1524 * HAS BEGIN GAF
1526 * for some patients (with age < 4) we don't fill a begin GAF - AS5
1527 * so, if we close the DBC, we don't ask for the other 2 GAF if we don't have the first one.
1529 * @param int $axid - DBC id
1530 * @return bool - true if there is a begin GAF
1532 function has_beginGAF($axid = 0){
1533 if ( !$axid ) return FALSE;
1535 $dbc = content_diagnose($axid);
1536 $ax5 = unserialize($dbc['ax_as5']);
1538 return ( !empty($ax5) );
1542 //-----------------------------------------------------------------------------
1544 * DISPLAY LINKS
1546 * display links as Add/Edit DSM-IV, Close DSM-IV, etc... in coding.php (patient_file)
1548 * @param none
1549 * @return void
1551 function display_links(){
1552 $retcode = ztn_status(); // find the ZTN situation
1554 switch ( $retcode ) {
1555 case 0: $msg = 'No ZTN opened!'; break;
1556 case 1:
1557 case 3: $msg = '<dd><a class="text" href="javascript:selas()">Add DSM IV</a></dd>
1558 <dd><a class="text" href="#" id="closeztn">Close ZTN</a></dd>'; break;
1559 case 2: $msg = '<dd><a class="text" href="javascript:selas()">Edit DSM IV</a></dd>
1560 <dd><a class="text" href="javascript:selcl()">DBC Sluiten</a></dd>'; break;
1561 case 4: $msg = '<dd><a class="text" href="javascript:selfl()">Follow up display</a></dd>
1562 <dd><a class="text" href="javascript:selcl()">DBC Sluiten</a></dd>'; break;
1565 echo $msg;
1568 //-----------------------------------------------------------------------------
1570 * RETURN THE DBC'S W/OUT FUTURE EVENTS
1572 * return opened dbc's without future events
1574 * @param none
1575 * @return array
1577 function df_future_events(){
1578 $resarr = array(); // dummy array for result
1579 $date = date('Y-m-d');
1580 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1582 if ( mysql_num_rows($q) ) {
1583 while ( $row = mysql_fetch_array($q) ) {
1584 $count = 0;
1585 $pid = what_patient($row['ax_id']);
1586 $fe = mysql_query("SELECT COUNT(*) AS a FROM openemr_postcalendar_events WHERE pc_pid = $pid
1587 AND pc_eventDate > '$date' ") or die(mysql_error());
1588 $rfe = mysql_fetch_array($fe);
1589 $count = $rfe['a']; // how many future encounters
1591 if ( !$count ) { $row['pid'] = $pid; $resarr[] = $row; }
1593 } // while
1596 return $resarr;
1601 //-----------------------------------------------------------------------------
1603 * RETURN OPENED DBC'S WITH TOTAL TIME
1605 * the times are separated per years 2007/2008
1607 * @param none
1608 * @return array
1610 function df_allopendbc_wtimes(){
1611 $resarr = array(); // dummy array for result
1612 $today = date('Y-m-d');
1613 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1615 if ( mysql_num_rows($q) ) {
1616 $count = 1;
1617 while ( $row = mysql_fetch_array($q) ) {
1618 $odate = $row['ax_odate'];
1619 $resarr[$count]['dbcid'] = $row['ax_id'];
1620 $resarr[$count]['odate'] = $odate;
1622 if ( $odate <= '2007-12-31' ) {
1623 $times2007 = total_time_spent($row['ax_id'], $odate, '2007-12-31');
1624 $times2008 = total_time_spent($row['ax_id'], '2008-01-01', $today);
1625 $resarr[$count]['2007'] = $times2007['total_time'];
1626 $resarr[$count]['2008'] = $times2008['total_time'];
1627 } else {
1628 $times = total_time_spent($row['ax_id'], '2008-01-01', $today);
1629 $resarr[$count]['2007'] = 0;
1630 $resarr[$count]['2008'] = $times['total_time'];
1633 $pid = what_patient($row['ax_id']);
1634 $resarr[$count]['pid'] = $pid; // using $times as a returning array
1635 $count++;
1636 } // while
1638 //echo '<pre>' . print_r($resarr, TRUE) . '</pre>'; // debug
1639 return $resarr;
1643 //-----------------------------------------------------------------------------
1645 * RETURNS SELECTED ACTIVITY FROM THE FORM
1647 * it's about add_edit_event.php form
1648 * @param none
1649 * @return string
1651 function selected_ac() {
1652 // same logic as in javascript validation
1653 if ( $_POST['box5'] ) $ac = $_POST['box5'];
1654 elseif ( $_POST['box4']) $ac = $_POST['box4'];
1655 elseif ( $_POST['box3']) $ac = $_POST['box3'];
1656 elseif ( $_POST['box2']) $ac = $_POST['box2'];
1657 elseif ( $_POST['box1']) $ac = $_POST['box1'];
1659 return $ac;
1663 //-----------------------------------------------------------------------------
1665 * RETURN OPENED DBC'S WITH MONEY VALUES FOR EACH
1667 * it simulates a closing for opened DBC's
1669 * @param none
1670 * @return array
1672 function df_dbcvalues(){
1673 $dbcdata = array(); // dummy array for result
1674 $ztn = 180101 ; // default zorgtraject at closing (Reguliere zorg)
1675 $today = date('Y-m-d');
1676 $q = mysql_query("SELECT * FROM cl_axes ca WHERE ca.ax_open = 1 ORDER BY ca.ax_odate") or die(mysql_error());
1678 if ( mysql_num_rows($q) ) {
1679 $count = 1;
1680 global $rfsum;
1681 while ( $row = mysql_fetch_array($q) ) {
1682 $dbcid = $row['ax_id'];
1684 $rfsum = 0; // reset the rfsum!
1685 dt_main(1, $dbcid, $today);
1686 $z = dt_whatproductgroep($rfsum, $today);
1687 $pcode = $z['id'];
1689 $prestatie = dt_prestatiecode($ztn, $pcode, $dbcid, 1);
1691 $declaratie = df_declaratie($prestatie, $today);
1692 $tariff = df_tariff($declaratie, $today);
1694 $dedamo = ( vk_is_overloop_dbc($dbcid) ) ? vk_deduction($dbcid) : 0;
1696 $tariff_per = round (((int)$tariff * C417) / 10000);
1697 $tariff_final = $tariff_per - $dedamo;
1700 $dbcdata[$count]['pid'] = what_patient($dbcid);
1701 $dbcdata[$count]['dbcid'] = $dbcid;
1702 $dbcdata[$count]['rfsum'] = $rfsum;
1703 $dbcdata[$count]['pcode'] = $pcode;
1704 $dbcdata[$count]['prestatie'] = $prestatie;
1705 $dbcdata[$count]['declaratie'] = $declaratie;
1706 $dbcdata[$count]['tariff'] = $tariff_final;
1707 $dbcdata[$count]['odate'] = $row['ax_odate'];
1709 $count++;
1710 } // while
1713 return $dbcdata;
1718 //-----------------------------------------------------------------------------
1720 * DECLARATIE CODE
1722 * used by df_dbcvalues()
1724 * @param
1725 * @return
1727 function df_declaratie($pcode, $cdate) {
1728 $q = sprintf("SELECT cl_declaratiecode AS cd FROM cl_prestatiecode WHERE cl_dbc_prestatiecode = '%s'
1729 AND cl_prestatiecode_begindatum <= '%s' AND cl_prestatiecode_einddatum >= '%s' ", $pcode, $cdate, $cdate);
1730 $r = mysql_query($q) or die(mysql_error());
1732 if ( $row = mysql_fetch_array($r) ) {
1733 $retval = $row['cd'];
1734 } else {
1735 $retval = 0;
1738 return $retval;
1742 function df_tariff($decode, $date) {
1743 $q = sprintf("SELECT cl_dbc_tarief FROM cl_dbc_tarief WHERE cl_declaratiecode = '%s'
1744 AND cl_dbc_tarief_begindatum <= '%s' AND cl_dbc_tarief_einddatum >= '%s' ", $decode, $date, $date);
1745 $r = mysql_query($q) or die(mysql_error());
1747 if ( mysql_num_rows($r) ) {
1748 $row = mysql_fetch_array($r); $val = $row['cl_dbc_tarief'];
1749 } else {
1750 $val = 0;
1753 return $val;
1757 //-----------------------------------------------------------------------------