remove directory checking, no longer required
[openemr.git] / library / DBC_files.php
blobfd3bcf7a85b74a66d9d27238000cbc3eb2ea091a
1 <?php
2 /**
3 * DBC DUTCH SYSTEM - DBC_FILES
4 * several functions used to generate txt files for reporting
5 *
6 *
7 * Cristian NAVALICI (lemonsoftware [..] gmail [.] com)
8 * @version 1.0
9 */
11 //-----------------------------------------------------------------------------
12 // some constants in our newly generated files
15 require_once('sql.inc');
17 define('DECLINST', '73730925'); // declaring institution code
18 define('LOCATION', '0'); // location code for declaring institution
19 define('TEPR', 'TEST'); // TEST or PRODuction ?
20 define('DBC_WORKINGDIR', $webserver_root . '/temp/dbc'); // working directory for our archive - server path w/out trailing slash
21 define('HOST', $host ); // mysql host
22 define('USER', $login ); // user for database quering
23 define('PASS', $pass ); // password for user
24 define('DATABASE', $dbase ); // database name
26 $pk_patients = 0;
27 $pk_careroutes = 0;
28 $pk_dbcs = 0;
29 $pk_diagnoses = 0;
30 $pk_tijdschrijven = 0;
31 $dbcid_arr = array();
33 //-----------------------------------------------------------------------------
34 /**
35 * MAIN FUNCTION
37 * @param string $file
38 * @param int $simulate - is simulation or real (the difference is that in case of
39 * real, the db gets updated)
40 * @return string - generated archive name
42 function dbc_generatefile($file = 'all', $simulate = 1) {
43 //db_connect();
45 switch($file) {
46 case 'dbctraject': gf_dbctraject(); break;
47 case 'zorgtraject': gf_zorgtraject(); break;
48 case 'diagnose': gf_diagnose(); break;
49 case 'patient': gf_patient(); break;
50 case 'empty': gf_empty(); break;
51 case 'tijdschrijven': gf_tijdschrijven(); break;
52 case 'pakbon': gf_pakbon(); break;
53 case 'all': gf_dbctraject(); gf_zorgtraject(); gf_diagnose(); gf_patient(); gf_tijdschrijven(); gf_empty(); break;
57 // final step; count the votes!
58 gf_pakbon();
60 // function to update some records (eg sti field from cl_axes AFTER generating
61 // all the required files
62 // THIS IS THE LAST STEP AFTER *ALL* ERRORS WERE CORRECTED!
63 if ( !$simulate ) update_db();
64 //if ( !$simulate ) update_db_2007();
66 // create zip archive
67 // if the directory it's not there, we'll create it
68 $dbc_workingdir = TMPDIR_DBC . '/dbc';
70 if ( !file_exists($dbc_workingdir) ) {
71 mkdir($dbc_workingdir);
74 // also in pakbon.txt
75 $archive_name = 'DIS_GGZ_DBC_' .TEPR. '_030_' .DECLINST. '_' .LOCATION. '_' .date('Ym'). '01.zip';
76 $st = 'cd ' . $dbc_workingdir .'; zip ' .$archive_name. ' *';
77 exec($st);
79 return (DBC_WORKINGDIR .'/'. $archive_name);
83 //-----------------------------------------------------------------------------
84 /**
85 * DBC_TRAJECT
87 * generate dbc_traject.txt file
89 * @param none
90 * @return void
92 function gf_dbctraject() {
93 dbc_message('DBCTRAJECT GENERATING');
94 $file = DBC_WORKINGDIR . '/dbc_traject.txt';
95 if ( !$h = fopen($file, 'wb') ) {
96 echo "Cannot create file ($filename)";
97 exit;
100 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
101 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
102 $r = mysql_query($q) or die(mysql_error());
104 global $dbcid_arr;
106 if ( mysql_num_rows($r) ) {
107 while ( $row = mysql_fetch_array($r) ) {
108 // we write the dbc id in an array, used for REAL mode (update the values)
109 $dbcid_arr[] = $row['ax_id'];
111 // prepare data128
112 $_128 = DECLINST;
113 $_129 = LOCATION;
114 $_1008 = str_pad($row['ax_id'], 20, ' ');
115 $_222 = ' ';
116 $_997 = str_pad($row['ax_ztn'], 20, ' ');
117 $_1002 = str_replace('-','', $row['ax_odate']);
118 $_1003 = str_replace('-','', $row['ax_cdate']);
119 $_133 = ' '; // 4 spaces
121 $_1004 = str_pad(what_zorg(0, $row['ax_id']), 20, ' ', STR_PAD_RIGHT);
122 fl_mb_beroep($_1004, $row['ax_odate'], $row['ax_cdate'], $row['ax_ztn']);
124 $_890 = str_pad(what_circuit_new($row['ax_id']), 20, ' ', STR_PAD_RIGHT);
125 $_891 = str_pad(what_reden($row['ax_id']), 20, ' ', STR_PAD_RIGHT);
126 $_979 = str_pad('0',11, ' ', STR_PAD_LEFT);
127 $_1064 = 'J';
128 $_1254 = str_pad(what_stoornis(0, $row['ax_id']), 6, '0', STR_PAD_LEFT);
130 $display = vl_validdbc_combinations($row['ax_id']);
132 if ( $display ) {
133 $content = $_128 . $_129 . $_1008 . $_222 . $_997 . $_1002 . $_1003 . $_133 . $_1004 . $_890 . $_891 . $_979 . $_1064 . $_1254 ."\r\n";
134 //$content = $_128 .'|'. $_129 .'|'. $_1008 .'|'. $_222 .'|'. $_997 .'|'. $_1002 .'|'. $_1003 .'|'. $_133 .'|'. $_1004 .'|'. $_890 .'|'. $_891 .'|'. $_979 .'|'. $_1064 .'|'. $_1254 ."\r\n";
135 $GLOBALS['pk_dbcs']++;
136 } else {
137 vl_log("DBC {$row['ax_id']} is not valid! Activities combinations validation failed.");
140 // WRITE DATA TO FILE
141 if ( fwrite($h, $content) === FALSE ) {
142 echo "Cannot write to file ($filename)";
143 exit;
148 fclose($h);
152 //-----------------------------------------------------------------------------
154 ZORGTRAJECT
156 generate zorgtraject.txt file
158 @param
159 @return
161 function gf_zorgtraject() {
162 dbc_message('ZORGTRAJECT GENERATING');
164 $file = DBC_WORKINGDIR . '/zorgtraject.txt';
165 if ( !$h = fopen($file, 'wb') ) {
166 echo "Cannot create file ($filename)";
167 exit;
170 // get all ZTNs associated with processed DBCs (not sent to insurer and closed - because a ZTN can contain multiple
171 // closed DBC's and just ONE opened DBC
172 //$q = sprintf("SELECT * FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn
173 //WHERE cl_axes.ax_sti = 0 AND cl_axes.ax_open = 99");
174 $q = sprintf("SELECT * FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn
175 WHERE cl_axes.ax_sti = 0 AND cl_axes.ax_open = 0");
176 $r = mysql_query($q) or die(mysql_error());
178 if ( mysql_num_rows($r) ) {
179 while ( $row = mysql_fetch_array($r) ) {
180 // prepare data
181 $_1000 = DECLINST;
182 $_1001 = LOCATION;
183 $_1007 = str_pad($row['cn_ztn'], 20, ' ');
184 $_1255 = ' '; //status flag J-for removed
185 $_998 = str_replace('-','', $row['cn_dopen']);
186 $_999 = ( $row['cn_dclosed'] == '9999-12-31' ) ? ' ' : str_replace('-','', $row['cn_dclosed']);
187 $_1252 = str_pad(what_joining_number($row['cn_pid']), 15, ' ');
188 $_1056 = ' '; // 8 spaces - health care send that sent patient; not used for us
190 // $qs = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 99", $row['cn_ztn']);
191 $qs = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 0", $row['cn_ztn']);
192 $rs = mysql_query($qs) or die(mysql_error());
193 $dbc = mysql_fetch_assoc($rs);
195 $as1 = unserialize($dbc['ax_as1']);
196 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
197 $as2brut = unserialize($dbc['ax_as2']); $as2 = $as2brut['content'];
199 // first we look in referer table
200 $qr = sprintf("SELECT * FROM cl_referers WHERE ref_pid = %d", $row['cn_pid']);
201 $rr = mysql_query($qr) or die(mysql_error());
203 // referer type (optional)
204 $_1057 = '';
205 if ( mysql_num_rows($rr) ) {
206 $ref_res = mysql_fetch_assoc($rr);
207 $_1057 = str_pad($ref_res['ref_code'], 4, '0', STR_PAD_LEFT);
208 } else {
209 // then look in provider table
210 $qpr = sprintf("SELECT * FROM cl_providers WHERE pro_pid = %d", $row['cn_pid']);
212 $rpr = mysql_query($qpr) or die(mysql_error());
213 if ( mysql_num_rows($rpr) ) {
214 $_1057 = '0100'; //always huisarts
215 } else {
216 $_1057 = ' '; // optional but must be filled with spaces
219 $_1057 = substr($_1057, 0, 4); // assure there's only 4 chars here
221 $_948 = '';
222 if ( count($as1['content']) >= $mainpos) {
223 $_1058 = ' ';
224 $_948 = $as1['content'][$mainpos - 1];
225 } else {
226 $nr = $mainpos - count($as1['content']);
227 $record = $as2[$nr-1];
228 $_1058 = ( $record['trekken']) ? 'J' : ' ';
229 $_948 = $record['code'];
232 $_948 = str_pad($_948, 20, ' ');
233 $_949 = str_replace('-','', $dbc['ax_odate']);
235 $content = $_1000 . $_1001 . $_1007 . $_1255 . $_998 . $_999 . $_1252 . $_1056 . $_1057 . $_948 . $_1058 . $_949 ."\r\n";
236 //$content = $_1000 .'|'. $_1001 .'|'. $_1007 .'|'. $_1255 .'|'. $_998 .'|'. $_999 .'|'. $_1252 .'|'. $_1056 .'|'. $_1057 .'|'. $_948 .'|'. $_1058 .'|'. $_949 ."\r\n";
238 $GLOBALS['pk_careroutes']++;
240 // WRITE DATA TO FILE
241 if ( fwrite($h, $content) === FALSE ) {
242 echo "Cannot write to file ($filename)";
243 exit;
248 fclose($h);
252 //-----------------------------------------------------------------------------
254 PATIENT
256 generate patient.txt file
258 @param
259 @return
261 function gf_patient() {
262 dbc_message('PATIENT GENERATING');
264 $file = DBC_WORKINGDIR . '/patient.txt';
265 if ( !$h = fopen($file, 'wb') ) {
266 echo "Cannot create file ($filename)";
267 exit;
270 // select all patients for ready-to-be-sent DBCs
271 //$q = sprintf("SELECT DISTINCT cn_pid FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn WHERE cl_axes.ax_sti = 0 and cl_axes.ax_open = 99");
272 $q = sprintf("SELECT DISTINCT cn_pid FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn WHERE cl_axes.ax_sti = 0 and cl_axes.ax_open = 0");
273 $r = mysql_query($q) or die(mysql_error());
275 if ( mysql_num_rows($r) ) {
276 while ( $row = mysql_fetch_array($r) ) {
277 // prepare data
278 $infopatient = info_patient($row['cn_pid']);
279 switch ( $infopatient['sex'] ) {
280 case 'Male': $sex = 1; break; // male
281 case 'Female': $sex = 2; break; // female
282 //case : $sex = 9; break; // not specified - what openemr correspondent?
283 default: $sex = 0;
286 $names = names($row['cn_pid']);
288 $_7 = DECLINST;
289 $_8 = LOCATION;
290 $_1250 = str_pad(what_joining_number($row['cn_pid']), 15, ' ');
291 $_10 = str_pad('', 25, 'X'); // for now, we just anonimise; otherwise use $names array
292 $_11 = str_pad('', 10, 'X');
293 $_12 = $names['code_fpn'];
294 $_13 = str_pad('', 25, 'X'); // for now, we just anonimise; otherwise use $names array
295 $_14 = str_pad('', 10, 'X');
296 $_15 = $names['code_lpn'];
297 $_16 = str_pad('', 6, 'X');
298 $_17 = substr($infopatient['postal_code'], 0, 4) . 'AA'; // anonimise postalcode (first 4 char + 2 A's)
299 //$_18 = str_pad($infopatient['pdn_number'], 5, ' ', STR_PAD_LEFT); // house number
300 $_18 = str_pad('1', 5, ' ', STR_PAD_LEFT);
301 $_19 = str_pad('', 4, 'X');
302 $_20 = substr(vk_countrycode($infopatient['country_code']), 0, 2); // assure there's only 2 chars here
303 if ( !$_20 ) $_20 = 'XX'; // default value in case of blank values
305 $_21 = str_replace('-','', $infopatient['DOB']);
306 if ( 0 == $_21 ) fl_log("Patient with ID: {$infopatient['id']} doesn't have DOB!");
308 $_22 = $sex;
309 $_804 = str_pad($infopatient['ss'], 9, ' ');
311 $content = $_7 . $_8 . $_1250 . $_10 . $_11 . $_12 . $_13 . $_14 . $_15 . $_16 . $_17 . $_18 . $_19 . $_20 . $_21 . $_22 . $_804 . "\r\n";
312 //$content = $_7 .'|'. $_8 .'|'. $_1250 .'|'. $_10 .'|'. $_11 .'|'. $_12 .'|'. $_13 .'|'. $_14 .'|'. $_15 .'|'. $_16 .'|'. $_17 .'|'. $_18 .'|'. $_19 .'|'. $_20 .'|'. $_21 .'|'. $_22 .'|'. $_804 . "\r\n";
313 $GLOBALS['pk_patients']++; // count for pakbon
315 // WRITE DATA TO FILE
316 if ( fwrite($h, $content) === FALSE ) {
317 echo "Cannot write to file ($filename)";
318 exit;
323 fclose($h);
328 //-----------------------------------------------------------------------------
330 DIAGNOSE
332 @param
333 @return
335 function gf_diagnose() {
336 dbc_message('DIAGNOSE GENERATING');
338 $file = DBC_WORKINGDIR . '/diagnose.txt';
339 if ( !$h = fopen($file, 'wb') ) {
340 echo "Cannot create file ($filename)";
341 exit;
344 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
345 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
346 $r = mysql_query($q) or die(mysql_error());
348 $GLOBALS['pk_diagnoses'] = 0;
350 if ( mysql_num_rows($r) ) {
351 while ( $row = mysql_fetch_array($r) ) {
352 // prepare data
353 $_950 = DECLINST;
354 $_951 = LOCATION;
356 $as1 = unserialize($row['ax_as1']);
357 $as2 = unserialize($row['ax_as2']);
358 $as3 = unserialize($row['ax_as3']);
359 $as4 = unserialize($row['ax_as4']);
360 $as5 = unserialize($row['ax_as5']);
361 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
362 $as2c = $as2['content'];
364 $_882 = str_pad($row['ax_id'], 20, ' ');
365 $_887 = str_replace('-','', $row['ax_odate']);;
367 // we must avoid MAIN DIAGNOSE (mainpos); this is written in zorgtraject
369 $partial_content = ''; // represents all the lines associated with a single DBC
370 $counter = 1;
372 foreach ( $as1c as $a) {
373 if ( $counter != $mainpos ) {
374 $_883 = str_pad($a, 20, ' ');
375 $_885 = ' ';
376 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
377 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
378 $GLOBALS['pk_diagnoses']++;
380 $counter++;
383 if ( $as2c ) {
384 foreach ( $as2c as $a) {
385 if ( $counter != $mainpos ) {
386 $_883 = str_pad($a['code'], 20, ' ');
387 $_885 = ( $a['trekken'] ) ? 'J' : '';
388 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
389 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
390 $GLOBALS['pk_diagnoses']++;
392 $counter++;
394 } // if $as2c
397 $_883 = str_pad($as3, 20, ' '); $_885 = ' ';
398 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
399 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
400 $GLOBALS['pk_diagnoses']++;
402 $_883 = str_pad($as4, 20, ' '); $_885 = ' ';
403 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
404 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
405 $GLOBALS['pk_diagnoses']++;
408 // if we don't have the second and last GAF, we fill them with gaf1 value
409 // for official validation purposes
410 if ( empty($as5['gaf2']) ) $as5['gaf2'] = $as5['gaf1'];
411 if ( empty($as5['gaf3']) ) $as5['gaf3'] = $as5['gaf1'];
412 foreach ( $as5 as $a) {
413 $_883 = str_pad($a, 20, ' '); $_885 = ' ';
414 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
415 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
416 $GLOBALS['pk_diagnoses']++;
419 // WRITE DATA TO FILE
420 if ( fwrite($h, $partial_content) === FALSE ) {
421 echo "Cannot write to file ($filename)";
422 exit;
425 } // while
426 } // if
428 fclose($h);
433 //-----------------------------------------------------------------------------
435 GELEVERD ZORGPROFIEL TIJDSCHRIJVEN
437 @param
438 @return
440 function gf_tijdschrijven() {
441 dbc_message('TIJDSCHRIJVEN GENERATING');
444 $file = DBC_WORKINGDIR . '/geleverd_zorgprofiel_tijdschrijven.txt';
445 if ( !$h = fopen($file, 'wb') ) {
446 echo "Cannot create file ($filename)";
447 exit;
450 $content = '';
452 // for every DBC we find events associated
453 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0");
454 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
455 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
456 $r = mysql_query($q) or die(mysql_error());
458 if ( mysql_num_rows($r) ) {
459 while ( $row = mysql_fetch_array($r) ) {
460 // prepare data
461 $_919 = DECLINST;
462 $_920 = LOCATION;
463 $_921 = str_pad($row['ax_id'], 20, ' ');
465 // set begin and end date(if exists - for closed but not sent DBC)
466 // begin date is ax_odate only for the first DBC in ZTN
467 // the followers take the beginning date from the previous DBC - closing date
468 $sign = '>';
469 if ( first_dbc_2($row['ax_id'], $row['ax_ztn']) ) {
470 $bd_dbc = $row['ax_odate']; $sign = '>=';
471 } else {
472 $bd_dbc = previous_dbc($row['ax_id'], $row['ax_ztn']);
475 $ed_dbc = ( ($row['ax_cdate'] !== '0000-00-00') && (!empty($row['ax_cdate'])) ) ? $row['ax_cdate'] : date('Y-m-d');
476 $pid = what_patient($row['ax_id']);
478 // find all events between DBC's dates and sum up total times
479 $qevent = sprintf("SELECT * FROM openemr_postcalendar_events
480 WHERE pc_pid = '%s' AND pc_eventDate $sign '%s' AND pc_eventDate <= '%s' AND pc_apptstatus = '@' ",
481 $pid, $bd_dbc, $ed_dbc);
482 $revent = mysql_query($qevent) or die(mysql_error());
485 // we are doing this because in the case of enabled multiple providers option,
486 // there are some events duplicated but with the same content (except for providers field)
487 $m_arr = array(); // array with distinct values for pc_multiple
488 $revent_good = array();
489 while ( $rowe = mysql_fetch_array($revent) ) {
490 // MULTIPLE PROVIDERS CASE
491 if ( $rowe['pc_multiple'] ) {
492 if ( !in_array($rowe['pc_multiple'], $m_arr) ) {
493 $revent_good[] = $rowe;
494 $m_arr[] = $rowe['pc_multiple'];
496 // SINGLE PROVIDERS CASE
497 } else {
498 $revent_good[] = $rowe;
503 // we build for every event a $content
504 foreach ( $revent_good as $rg ) {
505 $_922 = str_pad($rg['pc_eid'], 20, ' ');
506 $_873 = str_pad(what_activity_event($rg['pc_eid'], $bd_dbc), 20, ' ');
508 if ( empty($rg['pc_eventDate']) )
509 fl_log("Event eid = {$rg['eid']} has an empty date for pc_eventDate!");
510 $_874 = str_replace('-','', $rg['pc_eventDate']);
512 $_877 = str_pad(what_profession_provider($rg['pc_aid'], $bd_dbc), 20, ' ');
514 if ( empty($rg['pc_duration']) ) $rg['pc_duration'] = 0;
515 $_880 = str_pad($rg['pc_duration']/60, 6, ' ');
517 // get indirect+travel time
518 $time = what_time_event($rg['pc_eid']);
519 if ( empty($time['indirect_time']) ) $time['indirect_time'] = 0;
520 if ( empty($time['travel_time']) ) $time['travel_time'] = 0;
521 $_954 = str_pad($time['indirect_time'], 6, ' ');
522 $_955 = str_pad($time['travel_time'], 6, ' ');
524 // time validation
525 if ( fl_sumup_time(trim($_880), $time['indirect_time'], $time['travel_time'], $_921, $_922) ) {
526 $content .= $_919 . $_920 . $_921 . $_922 . $_873 . $_874 . $_877 . $_880 . $_954 . $_955 ."\r\n";
527 //$content .= $_919 .'|'. $_920 .'|'. $_921 .'|'. $_922 .'|'. $_873 .'|'. $_874 .'|'. $_877 .'|'. $_880 .'|'. $_954 .'|'. $_955 ."\r\n";
528 $GLOBALS['pk_tijdschrijven']++;
530 } // for each
531 } // while
532 } // if
536 // WRITE DATA TO FILE
537 if ( fwrite($h, $content) === FALSE ) {
538 echo "Cannot write to file ($filename)";
539 exit;
542 fclose($h);
546 //-----------------------------------------------------------------------------
548 PAKBON
550 generate pakbon.txt file
552 @param
553 @return
555 function gf_pakbon() {
556 dbc_message('PAKBON GENERATING');
558 $file = DBC_WORKINGDIR . '/pakbon.txt';
559 if ( !$h = fopen($file, 'wb') ) {
560 echo "Cannot create file ($filename)";
561 exit;
564 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0");
565 //$r = mysql_query($q) or die(mysql_error());
567 // prepare data128
568 $_996 = DECLINST;
569 $_997 = LOCATION;
570 $_995 = '03.0';
571 $_980 = date('Ymd');
572 $_981 = 'DIS_GGZ_DBC_' .TEPR. '_030_' .DECLINST. '_' .LOCATION. '_' .date('Ym'). '01.zip';
574 // TESTS | PROD
575 $_1233 = str_pad('OpenEMR-DBC01', 15, ' ');
576 $_982 = str_pad($GLOBALS['pk_patients'], 7, ' ', STR_PAD_LEFT);
577 $_1013 = str_pad($GLOBALS['pk_careroutes'], 7, ' ', STR_PAD_LEFT);
578 $_987 = str_pad($GLOBALS['pk_dbcs'], 7, ' ', STR_PAD_LEFT);
579 $_988 = str_pad($GLOBALS['pk_diagnoses'], 7, ' ', STR_PAD_LEFT);
580 $_990 = str_pad($GLOBALS['pk_tijdschrijven'], 7, ' ', STR_PAD_LEFT);
581 $_991 = str_pad(0, 7, ' ', STR_PAD_LEFT);
582 $_992 = str_pad(0, 7, ' ', STR_PAD_LEFT);
583 $_1234 = str_pad(0, 7, ' ', STR_PAD_LEFT);
584 $_994 = str_pad(0, 7, ' ', STR_PAD_LEFT);
585 $content = $_996 . $_997 . $_995 . $_980 . $_981 . $_1233 . $_982 . $_1013 . $_987 . $_988 . $_990 .
586 $_991 . $_992 . $_1234 . $_994 ."\r\n";
587 //$content = $_996 .'|'. $_997 .'|'. $_995 .'|'. $_980 .'|'. $_981 .'|'. $_1233 .'|'. $_982 .'|'. $_1013 .'|'. $_987 .'|'. $_988 .'|'. $_990 .'|'. $_991 .'|'. $_992 .'|'. $_1234 .'|'. $_994 ."\r\n";
589 // WRITE DATA TO FILE
590 if ( fwrite($h, $content) === FALSE ) {
591 echo "Cannot write to file ($filename)";
592 exit;
594 fclose($h);
598 //-----------------------------------------------------------------------------
600 EMPTY
602 generate empties files
604 @param
605 @return
607 function gf_empty() {
608 // array with empties files
609 $empties = array('overige_verrichting.txt', 'geleverd_zorgprofiel_verblijfsdagen.txt', 'geleverd_zorgprofiel_verrichtingen.txt',
610 'geleverd_zorgprofiel_dagbesteding.txt');
612 foreach ( $empties as $e ) {
613 $file = DBC_WORKINGDIR . '/' . $e;
614 if ( !$h = fopen($file, 'wb') ) {
615 echo "Cannot create file ($filename)";
616 exit;
618 fclose($h);
624 //-----------------------------------------------------------------------------
625 //-----------------------------------------------------------------------------
626 //-----------------------------------------------------------------------------
628 CONNECT TO DATABASE
630 make the connections to openemr database
632 @param none
633 @return void
635 function db_connect() {
636 $link = mysql_connect(HOST, USER, PASS);
637 if (!$link) {
638 die('Could not connect: ' . mysql_error());
641 mysql_query("USE " . DATABASE);
645 //-----------------------------------------------------------------------------
647 RETURN THE CODE FOR A ZORG IDENTIFIED BY SYSID
649 @param int sysid - zorg sysid
650 @param int dbc sysid - return the code for ASSOCIATED zorg with this dbc
651 @return string
653 function what_zorg($id = 0, $dbc = 0) {
654 if ( !$id && !$dbc) return FALSE;
656 if ( $dbc ) {
657 $q = sprintf("SELECT * FROM cl_zorg_dbc WHERE zd_dbc = %d ", $dbc);
658 $r = mysql_query($q) or die( mysql_error() );
659 $row = mysql_fetch_array($r);
660 $id = (int)$row['zd_zorg']; if ( !$id ) return FALSE;
663 $q = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_sysid = %d ", $id);
664 $r = mysql_query($q) or die( mysql_error() );
666 if ( mysql_num_rows($r) ) {
667 $row = mysql_fetch_array($r);
668 return $row['cl_zorgtype_code'];
669 } else {
670 fl_log("DBC with ID: $id doesn't have a zorg code in cl_zorg_dbc!");
671 return 0;
676 //-----------------------------------------------------------------------------
678 RETURN THE CODE FOR REDENSLUITEN
680 @param int $dbcid
681 @return int
683 function what_reden($dbcid) {
684 if ( !$dbcid ) return FALSE;
686 $q = sprintf("SELECT * FROM cl_redensluiten_dbc WHERE rd_dbc = %d ", $dbcid);
687 $r = mysql_query($q) or die( mysql_error() );
689 if ( mysql_num_rows($r) ) {
690 $row = mysql_fetch_array($r);
691 return $row['rd_redencode'];
692 } else {
693 fl_log("DBC with ID: $dbcid doesn't have a redensluiten code in cl_redensluiten_dbc!");
694 return 0;
699 //-----------------------------------------------------------------------------
701 RETURN THE CODE FOR CIRCUIT
703 NEW VERSION !!!!
705 @param int $ztnid - we based our search on ztn id
706 @return int
708 function what_circuit_new($dbcid) {
709 if ( !$dbcid ) return FALSE;
711 // find patient id
712 $qs = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d", $dbcid);
713 $rs = mysql_query($qs) or die(mysql_error());
714 $row = mysql_fetch_array($rs);
715 $pid = $row['ccd_circuitcode'];
717 return $pid;
721 //-----------------------------------------------------------------------------
723 RETURN THE CODE FOR PRODUCTGROEP
725 @param int sysid - productgroep sysid
726 @param int dbc sysid - return the code for ASSOCIATED zorg with this dbc
727 @return string
729 function what_stoornis($id = 0, $dbcid = 0) {
730 if ( !$id && !$dbcid) return FALSE;
732 if ( $dbcid ) {
733 $q = sprintf("SELECT * FROM cl_productgroep_dbc WHERE pc_dbc = %d ", $dbcid);
734 $r = mysql_query($q) or die( mysql_error() );
735 $row = mysql_fetch_array($r);
736 $id = (int)$row['pc_productcode']; if ( !$id ) return FALSE;
739 $q = sprintf("SELECT * FROM cl_productgroep WHERE cl_productgroep_sysid = %d ", $id);
740 $r = mysql_query($q) or die( mysql_error() );
741 $row = mysql_fetch_array($r);
742 return $row['cl_productgroep_code'];
746 //-----------------------------------------------------------------------------
748 RETURN THE UNIQUE GENERATED CODE FOR A PATIENT
750 return the id1250 for patient.txt file
752 @param int $patientid - patient id as in OpenEMR tables
753 @return string
755 function what_joining_number($patientid = 0) {
756 if ( !$patientid ) return FALSE;
758 $patq = sprintf("SELECT * FROM cl_patient_number WHERE pn_oemrid = %d", $patientid);
759 $r = mysql_query($patq) or die( mysql_error() );
761 if ( mysql_num_rows($r) ) {
762 $row = mysql_fetch_array($r);
763 return $row['pn_id1250'];
764 } else {
765 fl_log("Patient with ID: $patientid doesn't have a unique number in cl_patient_number!");
766 return 0;
770 //-----------------------------------------------------------------------------
772 RETURN ALL INFOS ABOUT A PATIENT
774 query patient_data original database
776 @param int $patientid - patient id as in OpenEMR tables
777 @return array
779 function info_patient($patientid = 0) {
780 if ( !$patientid ) return FALSE;
782 $patq = sprintf("SELECT * FROM patient_data pd JOIN patient_data_NL pdn ON
783 pdn.pdn_id = pd.id WHERE pd.id = %d", $patientid);
784 $r = mysql_query($patq) or die( mysql_error() );
785 $row = mysql_fetch_array($r);
787 return $row;
790 //-----------------------------------------------------------------------------
792 RETURN ACTIVITY CODE
794 query cl_event_activiteit for activity id
796 @param int $eid - eventid
797 @param string $date
798 @return string
800 function what_activity_event($eid = 0, $date = '') {
801 if ( !$eid || !$date) return FALSE;
803 $eidq = sprintf("SELECT cl_activiteit_code FROM cl_activiteit ca JOIN cl_event_activiteit cea
804 ON ca.cl_activiteit_sysid = cea.activity_sysid
805 WHERE cea.event_id = %d AND cl_activiteit_begindatum <= '%s' AND cl_activiteit_einddatum >= '%s'", $eid, $date, $date);
807 $r = mysql_query($eidq) or die( mysql_error() );
808 if ( mysql_num_rows($r) ) {
809 $row = mysql_fetch_array($r);
810 return $row['cl_activiteit_code'];
811 } else {
812 $qa = mysql_query("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid") or die( mysql_error());
813 $ra = mysql_fetch_array($qa);
815 $date_aux = $ra['pc_eventDate'];
816 $pid = $ra['pc_pid'];
818 fl_log("Event with eid = $eid (Date: $date_aux / PID: $pid) don't have an associated activity code in cl_event_activiteit");
819 return 0;
823 //-----------------------------------------------------------------------------
825 RETURN PROFESSION CODE
827 query cl_user_beroep for profession id
829 @param int $pid - provider id
830 @return int
832 function what_profession_provider($pid = 0, $date = '') {
833 if ( !$pid ) return FALSE;
835 //$pidq = sprintf("SELECT * FROM cl_user_beroep WHERE cl_beroep_userid = %d", $pid);
836 $pidq = sprintf("SELECT cl_beroep_code FROM cl_beroep cb JOIN cl_user_beroep cub
837 ON cb.cl_beroep_sysid = cub.cl_beroep_sysid WHERE cub.cl_beroep_userid = %d
838 AND cb.cl_beroep_begindatum <= '%s' AND cb.cl_beroep_einddatum >= '%s'", $pid, $date, $date) ;
840 $r = mysql_query($pidq) or die( mysql_error() );
841 if ( mysql_num_rows($r) ) {
842 $row = mysql_fetch_array($r);
843 return $row['cl_beroep_code'];
844 //return $row['cl_beroep_sysid'];
845 } else {
846 fl_log("Provider with $pid don't have an associated beroep (job) code in cl_user_beroep");
847 return 0;
851 //-----------------------------------------------------------------------------
853 RETURN TIMES FOR AN EVENT
855 query cl_time_activiteit for an event
857 @param int $eid - activity id
858 @return array
860 function what_time_event($eid = 0) {
861 if ( !$eid ) return FALSE;
863 // check if this id is part of a multiple providers record
864 // what is multiple key around this $eid?
865 $rq = sprintf("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = %d", $eid);
866 $rezq = mysql_query($rq) or die( mysql_error() );
867 $rowmulti = mysql_fetch_array($rezq);
869 if ( $rowmulti['pc_multiple'] ) {
870 // --------------- MULTIPLE PROVIDERS CASE ----------------
871 // what are all pc_eid's grouped by multiple key
872 $eventsrow = array();
873 $rezev = mysql_query("SELECT pc_eid FROM openemr_postcalendar_events WHERE pc_multiple = {$rowmulti['pc_multiple']}");
874 while ( $row = mysql_fetch_array($rezev) ) {
875 $eventsrow[] = $row['pc_eid'];
878 // we look in cl_time_activiteit for a matching record
879 $timerow = '';
880 foreach ( $eventsrow as $ev) {
881 $time = mysql_query("SELECT * FROM cl_time_activiteit WHERE event_id = $ev");
882 if ( mysql_num_rows($time) ) {
883 $timeres = mysql_fetch_array($time);
884 $timerow = ( $timeres ) ? $timeres : '';
885 } else {
886 $timerow = array('indirect_time' => 0, 'travel_time' => 0); // as a last solution, return an empty array
889 // --------------- EOS MULTIPLE PROVIDERS CASE ----------------
890 } else {
891 // --------------- SINGLE PROVIDERS CASE ----------------
892 $time = mysql_query("SELECT * FROM cl_time_activiteit WHERE event_id = $eid");
893 if ( mysql_num_rows($time) ) {
894 $timerow = mysql_fetch_array($time);
895 } else {
896 $timerow = array('indirect_time' => 0, 'travel_time' => 0); // as a last solution, return an empty array
898 // --------------- EOS SINGLE PROVIDERS CASE ----------------
901 return $timerow;
905 //-----------------------------------------------------------------------------
907 UPDATE DATABASE
909 @param none
910 @return void
912 function update_db() {
913 // for every open DBC generate a duplicate one; the older one
914 // will be marked as sent_to_insurer and closed
916 $date = date('Y-m-d');
917 mysql_query('START TRANSACTION');
919 // look for openened dbc
920 $q = sprintf("SELECT * FROM cl_axes WHERE ax_open = 1 AND ax_sti = 0");
921 $r = mysql_query($q) or die(mysql_error());
923 if ( !mysql_num_rows($r) ) return;
925 while ( $row = mysql_fetch_array($r) ) {
926 transform_dbc($row['ax_id']);
927 } // while
929 mysql_query('COMMIT');
934 //-----------------------------------------------------------------------------
936 UPDATE DATABASE
938 FUNCTION ONLY FOR DIRTY TRICKS: CLOSING PER MONTHS IN 2007!!!!
939 DON'T USE IT IN A REGULAR PRODUCTION ENV
941 @param none
942 @return void
944 function update_db_2007() {
945 global $dbcid_arr;
946 foreach ( $dbcid_arr as $dbc ) {
947 $q = sprintf("UPDATE cl_axes SET ax_sti = 1, ax_open = 0 WHERE ax_id = %d", $dbc);
948 mysql_query($q) or die(mysql_error());
949 echo $q . '<br />';
953 //-----------------------------------------------------------------------------
955 TRANSFORM DBC
957 duplicate a DBC and then mark the older one with sent to insurer flag
959 @param int $dbc id
960 @return void
962 function transform_dbc($dbcid = 0) {
964 if ( !$dbcid ) return FALSE;
966 mysql_query("START TRANSACTION");
968 $date = date('Y-m-d');
970 // read the current dbc
971 $q = sprintf("SELECT * FROM cl_axes WHERE ax_id = %d", $dbcid);
972 $r = mysql_query($q) or die(mysql_error());
973 $dbc = mysql_fetch_array($r);
975 // duplicate it (sent to insurer flag = 0 for the new one, 1 for the older one)
976 $qi = sprintf("INSERT INTO cl_axes (ax_ztn, ax_open, ax_as1, ax_as2, ax_as3, ax_as4, ax_as5, ax_odate, ax_cdate, ax_sti)
977 VALUES ('%s', %d,'%s','%s','%s','%s','%s','%s','%s','%s')", $dbc['ax_ztn'], 1, $dbc['ax_as1'], $dbc['ax_as2'],
978 $dbc['ax_as3'],$dbc['ax_as4'],$dbc['ax_as5'], $date, 0, 0);
979 mysql_query($qi) or die (mysql_error());
981 // close the old one
982 $qu = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s', ax_sti = 1 WHERE ax_id = %d", $date, $dbcid);
983 mysql_query($qu) or die(mysql_error());
985 // update the related tables (cl_circuit_dbc)
986 $qc = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d ", $dbcid);
987 $rc = mysql_query($qc) or die(mysql_error());
988 $circuit = mysql_fetch_array($rc);
990 mysql_query("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES (%d, %d)", $circuit['ccd_circuitcode'], $dbcid);
992 mysql_query("COMMIT");
995 //-----------------------------------------------------------------------------
997 NAAMCODE
999 1 then naam_1 is the name of the married partner; 2 - name of the patient
1001 @param int $pid
1002 @return int (1 or 2)
1004 function naamcode_1($pid = 0) {
1005 if (!$pid) return 0;
1007 $qp = sprintf("SELECT fname FROM patient_data WHERE id = %d ", $pid);
1008 $rp = mysql_query($qp) or die(mysql_error());
1009 $row = mysql_fetch_array($rp);
1011 return ( $row['fname'] ) ? 2 : 1;
1015 //-----------------------------------------------------------------------------
1017 NAMES
1019 return the names for patient
1020 (we use patient names or if we don't have, partner name)
1022 @param int $pid - patient id
1023 @return array
1025 function names($pid = 0) {
1026 if (!$pid) return 0;
1028 $qp1 = sprintf("SELECT fname, lname FROM patient_data WHERE id = %d ", $pid);
1029 $rp1 = mysql_query($qp1) or die(mysql_error());
1030 $row1 = mysql_fetch_array($rp1);
1032 $qp2 = sprintf("SELECT pdn_pxlast, pdn_pxlastpar,pdn_lastpar FROM patient_data_NL WHERE pdn_id = %d ", $pid);
1033 $rp2 = mysql_query($qp2) or die(mysql_error());
1034 $row2 = mysql_fetch_array($rp2);
1036 $ret = array();
1038 // first part of the name (if we don't have it, the pad just 25X)
1039 // first part = firstname + lastname patient
1040 // second part = lastname partner (if any)
1041 $firstpart = $row1['fname'] .' '. $row1['lname'];
1042 $ret['firstpname'] = str_pad($firstpart, 25, ' ', STR_PAD_RIGHT);
1043 $ret['prefix_fpn'] = ( $row2['pdn_pxlast'] ) ? str_pad($row2['pdn_pxlast'], 10, ' ') : str_pad('', 10, 'X');
1044 $ret['code_fpn'] = '2'; // name of the patient
1046 // last part of the name (partner ones)
1047 if ( $row2['pdn_lastpar'] ) {
1048 $ret['lastpname'] = str_pad($row2['pdn_lastpar'], 25, ' ', STR_PAD_RIGHT);
1049 $ret['prefix_lpn'] = str_pad($row2['pdn_pxlastpar'], 10, ' ', STR_PAD_RIGHT);
1050 $ret['code_lpn'] = '1'; // name of the patient
1051 } else {
1052 // empty values
1053 $ret['lastnpame'] = str_pad('', 25, ' ', STR_PAD_RIGHT);
1054 $ret['prefix_lpn'] = str_pad('', 10, ' ', STR_PAD_RIGHT);
1055 $ret['code_lpn'] = ' ';
1058 return $ret;
1061 //-----------------------------------------------------------------------------
1063 FIRST DBC?
1065 find if a dbc is the first or a 'follow-up'
1066 (the same function is in DBC_functions with a small modification here --> ztn arg)
1068 @param int - ax_id - the DBC id
1069 @param int - ztn id
1070 @return bool | int
1072 function first_dbc_2($ax_id, $ztn_id) {
1073 // look for all dbcs in a careroute
1074 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s' ORDER BY ax_id", $ztn_id);
1075 $rez = mysql_query($qz) or die(mysql_error());
1077 while ( $row = mysql_fetch_array($rez) ) {
1078 $arrdbc[] = $row['ax_id'];
1081 // and now, the analysis:
1082 // - first means the ax_id is the first in array
1083 // - followup means the ax_id is NOT the first in array (because after the first, all dbcs
1084 // are followups)
1085 return ( $arrdbc[0] == $ax_id );
1088 //-----------------------------------------------------------------------------
1090 PREVIOUS DBC
1092 find the previous dbc for a given one and return its closing date
1094 @param int - ax id
1095 @param int $ztn_id - ztn id
1096 @return string
1098 function previous_dbc($ax_id, $ztn_id) {
1099 $qp = sprintf("SELECT ax_cdate FROM cl_axes WHERE ax_ztn='%s' AND ax_id < %d ORDER BY ax_id DESC", $ztn_id, $ax_id);
1100 $rez = mysql_query($qp) or die(mysql_error());
1101 $r = mysql_fetch_array($rez);
1103 return $r['ax_cdate'];
1107 // ----------------------------------------------------------------------------
1109 SIMILAR LOG FUNCTION WITH THE ONE FROM DBC_VALIDATIONS
1111 simple function to log different events
1113 @param string $string
1114 @return
1116 function fl_log($string) {
1117 $file = TMPDIR_DBC . '/DBC_problems.log';
1118 if ( !$h = fopen($file, 'ab') ) {
1119 echo "Cannot create file ($file)";
1120 exit;
1123 $content = date('d-m-Y') . " $string \r\n";
1125 // WRITE DATA TO FILE
1126 if ( fwrite($h, $content) === FALSE ) {
1127 echo "Cannot write to file ($file)";
1128 exit;
1131 fclose($h);
1135 // ----------------------------------------------------------------------------
1137 VERIFY IF THE TOTAL TIME IF GREATER THAN 0
1138 AND ALSO IF INDIRECT TIME>0 AND DIRECT TIME = 0
1140 @param int $duration - encounter duration
1141 @param int $indirect - indirect time
1142 @param int $travel - travel time
1143 @param int $axid - id for DBC
1144 @param int $eid - event id
1145 @return bool
1147 function fl_sumup_time($duration, $indirect = 0, $travel = 0, $axid = 0, $eid = 0) {
1148 $sum = (int)$duration + (int)$indirect + (int)$travel;
1150 if ( !$sum ) {
1151 fl_log("DBC id:$axid EVENT: $eid has total time = 0 (E:$duration/I:$indirect/T:$travel)");
1152 return FALSE;
1153 } else if ( ($duration == 0 || $indirect == 0 ) && ($travel > 0) ) {
1154 fl_log("DBC id:$axid has travel time = $indirect but no direct OR indirect.");
1155 return FALSE;
1158 return TRUE;
1163 // ----------------------------------------------------------------------------
1165 VERIFY IF THE PATIENT HAS SOME REQUIRED PROVIDERS WITH THE RIGHT JOBS
1167 @param string $zorg
1168 @param string $odate - opening date for dbc
1169 @param string $cdate - closing date for dbc
1170 @param string $ztn - ztn id
1173 function fl_mb_beroep($zorg, $odate, $cdate, $ztn) {
1174 // the zorg values who'll be checked for beroep
1175 $checklist = array(110, 206, 116, 211);
1177 $zo = (int)trim($zorg);
1178 if ( !in_array($zo, $checklist) ) {
1179 return TRUE;
1180 } else {
1181 // find the id for the user
1182 $qu = sprintf("SELECT cn_pid FROM cl_careroute_numbers WHERE cn_ztn = '%s'", trim($ztn));
1183 $ru = mysql_query($qu) or die(mysql_error());
1184 $rowu = mysql_fetch_array($ru);
1185 $user = $rowu['cn_pid'];
1187 // find all events between the opening and closing of the DBC
1188 // and look for every provider's job
1189 $qe = sprintf("SELECT * FROM openemr_postcalendar_events WHERE pc_pid = '%s' AND pc_apptstatus = '@'
1190 AND pc_eventDate >= '%s' AND pc_eventDate <= '%s' ", $user, $odate, $cdate);
1191 $re = mysql_query($qe) or die(mysql_error());
1193 $has = FALSE;
1194 if ( mysql_num_rows($re) ) {
1195 while ( $row = mysql_fetch_array($re) ) {
1196 $job = what_beroep($row['pc_aid'], 1);
1197 if ( preg_match('/^MB\./', $job) ) $has |= TRUE;
1199 } // if num_rows
1201 if ( !$has ) fl_log("USER/ZTN: $user/$ztn doesn't have at least one provider with job (MB.%)");
1203 } // if else
1208 // ----------------------------------------------------------------------------
1210 * DISPLAY A MESSAGE
1212 * used to display messages in DBC generation phase
1213 * must be called only with hardcoded (and safe) strings!
1215 * @param string $msg
1217 function dbc_message($msg) {
1218 $str = "$msg <br>";
1219 echo $str;
1223 // ----------------------------------------------------------------------------