Links to rx_print changed to make use of GET variable as now required by rx_print...
[openemr.git] / library / DBC_files.php
blob6351a9a1663b5bef59e953d23f71cbddf2b8f4a9
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
16 define('DECLINST', '73730925'); // declaring institution code
17 define('LOCATION', '0'); // location code for declaring institution
18 define('TEPR', 'TEST'); // TEST or PRODuction ?
19 define('DBC_WORKINGDIR', '/var/www/html/openemr/temp/dbc'); // working directory for our archive - server path w/out trailing slash
20 define('HOST', 'localhost'); // mysql host
21 define('USER', 'openemr'); // user for database quering
22 define('PASS', 'tt-allbusy.gsm'); // password for user
23 define('DATABASE', 'openemr'); // database name
25 $pk_patients = 0;
26 $pk_careroutes = 0;
27 $pk_dbcs = 0;
28 $pk_diagnoses = 0;
29 $pk_tijdschrijven = 0;
31 //-----------------------------------------------------------------------------
32 /**
33 * MAIN FUNCTION
35 * @param string $file
36 * @param int $simulate - is simulation or real (the difference is that in case of
37 * real, the db gets updated)
38 * @return string - generated archive name
40 function dbc_generatefile($file = 'all', $simulate = 1) {
41 //db_connect();
43 switch($file) {
44 case 'dbctraject': gf_dbctraject(); break;
45 case 'zorgtraject': gf_zorgtraject(); break;
46 case 'diagnose': gf_diagnose(); break;
47 case 'patient': gf_patient(); break;
48 case 'empty': gf_empty(); break;
49 case 'tijdschrijven': gf_tijdschrijven(); break;
50 case 'pakbon': gf_pakbon(); break;
51 case 'all': gf_dbctraject(); gf_zorgtraject(); gf_diagnose(); gf_patient(); gf_tijdschrijven(); gf_empty(); break;
54 // final step; count the votes!
55 gf_pakbon();
57 // function to update some records (eg sti field from cl_axes AFTER generating
58 // all the required files
59 // THIS IS THE LAST STEP AFTER *ALL* ERRORS WERE CORRECTED!
60 //if ( !$simulate ) update_db();
61 if ( !$simulate ) update_db_2007();
63 // create zip archive
64 // if the directory it's not there, we'll create it
65 $dbc_workingdir = TMPDIR_DBC . '/dbc';
67 if ( !file_exists($dbc_workingdir) ) {
68 mkdir($dbc_workingdir);
71 // also in pakbon.txt
72 $archive_name = 'DIS_GGZ_DBC_' .TEPR. '_030_' .DECLINST. '_' .LOCATION. '_' .date('Ym'). '01.zip';
73 $st = 'cd ' . $dbc_workingdir .'; zip ' .$archive_name. ' *';
74 exec($st);
76 return (DBC_WORKINGDIR .'/'. $archive_name);
80 //-----------------------------------------------------------------------------
81 /**
82 * DBC_TRAJECT
84 * generate dbc_traject.txt file
86 * @param none
87 * @return void
89 function gf_dbctraject() {
90 dbc_message('DBCTRAJECT GENERATING');
91 $file = DBC_WORKINGDIR . '/dbc_traject.txt';
92 if ( !$h = fopen($file, 'wb') ) {
93 echo "Cannot create file ($filename)";
94 exit;
97 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
98 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
99 $r = mysql_query($q) or die(mysql_error());
101 global $dbcid_arr;
103 if ( mysql_num_rows($r) ) {
104 while ( $row = mysql_fetch_array($r) ) {
105 $dbcid_arr[] = $row['ax_id'];
107 // prepare data128
108 $_128 = DECLINST;
109 $_129 = LOCATION;
110 $_1008 = str_pad($row['ax_id'], 20, ' ');
111 $_222 = ' ';
112 $_997 = str_pad($row['ax_ztn'], 20, ' ');
113 $_1002 = str_replace('-','', $row['ax_odate']);
114 $_1003 = str_replace('-','', $row['ax_cdate']);
115 $_133 = ' '; // 4 spaces
117 $_1004 = str_pad(what_zorg(0, $row['ax_id']), 20, ' ', STR_PAD_RIGHT);
118 fl_mb_beroep($_1004, $row['ax_odate'], $row['ax_cdate'], $row['ax_ztn']);
120 $_890 = str_pad(what_circuit_new($row['ax_id']), 20, ' ', STR_PAD_RIGHT);
121 $_891 = str_pad(what_reden($row['ax_id']), 20, ' ', STR_PAD_RIGHT);
122 $_979 = str_pad('0',11, ' ', STR_PAD_LEFT);
123 $_1064 = 'J';
124 $_1254 = str_pad(what_stoornis(0, $row['ax_id']), 6, '0', STR_PAD_LEFT);
126 $display = vl_validdbc_combinations($row['ax_id']);
128 if ( $display ) {
129 $content = $_128 . $_129 . $_1008 . $_222 . $_997 . $_1002 . $_1003 . $_133 . $_1004 . $_890 . $_891 . $_979 . $_1064 . $_1254 ."\r\n";
130 //$content = $_128 .'|'. $_129 .'|'. $_1008 .'|'. $_222 .'|'. $_997 .'|'. $_1002 .'|'. $_1003 .'|'. $_133 .'|'. $_1004 .'|'. $_890 .'|'. $_891 .'|'. $_979 .'|'. $_1064 .'|'. $_1254 ."\r\n";
131 $GLOBALS['pk_dbcs']++;
132 } else {
133 vl_log("DBC {$row['ax_id']} is not valid! Activities combinations validation failed.");
136 // WRITE DATA TO FILE
137 if ( fwrite($h, $content) === FALSE ) {
138 echo "Cannot write to file ($filename)";
139 exit;
144 fclose($h);
148 //-----------------------------------------------------------------------------
150 ZORGTRAJECT
152 generate zorgtraject.txt file
154 @param
155 @return
157 function gf_zorgtraject() {
158 dbc_message('ZORGTRAJECT GENERATING');
160 $file = DBC_WORKINGDIR . '/zorgtraject.txt';
161 if ( !$h = fopen($file, 'wb') ) {
162 echo "Cannot create file ($filename)";
163 exit;
166 // get all ZTNs associated with processed DBCs (not sent to insurer and closed - because a ZTN can contain multiple
167 // closed DBC's and just ONE opened DBC
168 //$q = sprintf("SELECT * FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn
169 //WHERE cl_axes.ax_sti = 0 AND cl_axes.ax_open = 0");
170 $q = sprintf("SELECT * FROM cl_careroute_numbers JOIN cl_axes ON cl_axes.ax_ztn = cl_careroute_numbers.cn_ztn
171 WHERE cl_axes.ax_sti = 0 AND cl_axes.ax_open = 99");
172 $r = mysql_query($q) or die(mysql_error());
174 if ( mysql_num_rows($r) ) {
175 while ( $row = mysql_fetch_array($r) ) {
176 // prepare data
177 $_1000 = DECLINST;
178 $_1001 = LOCATION;
179 $_1007 = str_pad($row['cn_ztn'], 20, ' ');
180 $_1255 = ' '; //status flag J-for removed
181 $_998 = str_replace('-','', $row['cn_dopen']);
182 $_999 = ( $row['cn_dclosed'] == '9999-12-31' ) ? ' ' : str_replace('-','', $row['cn_dclosed']);
183 $_1252 = str_pad(what_joining_number($row['cn_pid']), 15, ' ');
184 $_1056 = ' '; // 8 spaces - health care send that sent patient; not used for us
186 // $qs = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 0", $row['cn_ztn']);
187 $qs = sprintf("SELECT * FROM cl_axes WHERE ax_ztn = '%s' AND ax_open = 99", $row['cn_ztn']);
188 $rs = mysql_query($qs) or die(mysql_error());
189 $dbc = mysql_fetch_assoc($rs);
191 $as1 = unserialize($dbc['ax_as1']);
192 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
193 $as2brut = unserialize($dbc['ax_as2']); $as2 = $as2brut['content'];
195 // first we look in referer table
196 $qr = sprintf("SELECT * FROM cl_referers WHERE ref_pid = %d", $row['cn_pid']);
197 $rr = mysql_query($qr) or die(mysql_error());
199 // referer type (optional)
200 $_1057 = '';
201 if ( mysql_num_rows($rr) ) {
202 $ref_res = mysql_fetch_assoc($rr);
203 $_1057 = str_pad($ref_res['ref_code'], 4, '0', STR_PAD_LEFT);
204 } else {
205 // then look in provider table
206 $qpr = sprintf("SELECT * FROM cl_providers WHERE pro_pid = %d", $row['cn_pid']);
207 $rpr = mysql_query($qpr) or die(mysql_error());
208 if ( mysql_num_rows($rpr) ) {
209 $_1057 = '0100'; //always huisarts
210 } else {
211 $_1057 = ' '; // optional but must be filled with spaces
214 $_1057 = substr($_1057, 0, 4); // assure there's only 4 chars here
216 $_948 = '';
217 if ( count($as1['content']) >= $mainpos) {
218 $_1058 = ' ';
219 $_948 = $as1['content'][$mainpos - 1];
220 } else {
221 $nr = $mainpos - count($as1['content']);
222 $record = $as2[$nr-1];
223 $_1058 = ( $record['trekken']) ? 'J' : ' ';
224 $_948 = $record['code'];
227 $_948 = str_pad($_948, 20, ' ');
228 $_949 = str_replace('-','', $dbc['ax_odate']);
230 $content = $_1000 . $_1001 . $_1007 . $_1255 . $_998 . $_999 . $_1252 . $_1056 . $_1057 . $_948 . $_1058 . $_949 ."\r\n";
231 //$content = $_1000 .'|'. $_1001 .'|'. $_1007 .'|'. $_1255 .'|'. $_998 .'|'. $_999 .'|'. $_1252 .'|'. $_1056 .'|'. $_1057 .'|'. $_948 .'|'. $_1058 .'|'. $_949 ."\r\n";
233 $GLOBALS['pk_careroutes']++;
235 // WRITE DATA TO FILE
236 if ( fwrite($h, $content) === FALSE ) {
237 echo "Cannot write to file ($filename)";
238 exit;
243 fclose($h);
247 //-----------------------------------------------------------------------------
249 PATIENT
251 generate patient.txt file
253 @param
254 @return
256 function gf_patient() {
257 dbc_message('PATIENT GENERATING');
259 $file = DBC_WORKINGDIR . '/patient.txt';
260 if ( !$h = fopen($file, 'wb') ) {
261 echo "Cannot create file ($filename)";
262 exit;
265 // select all patients for ready-to-be-sent DBCs
266 //$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");
267 $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");
268 $r = mysql_query($q) or die(mysql_error());
270 if ( mysql_num_rows($r) ) {
271 while ( $row = mysql_fetch_array($r) ) {
272 // prepare data
273 $infopatient = info_patient($row['cn_pid']);
274 switch ((int)$infopatient['sex']) {
275 case 0: $sex = 0; break; // unknown
276 case 1: $sex = 1; break; // male
277 case 2: $sex = 2; break; // female
278 case 9: $sex = 9; break; // not specified
279 default: $sex = 9;
282 $names = names($row['cn_pid']);
284 $_7 = DECLINST;
285 $_8 = LOCATION;
286 $_1250 = str_pad(what_joining_number($row['cn_pid']), 15, ' ');
287 $_10 = str_pad('', 25, 'X'); // for now, we just anonimise; otherwise use $names array
288 $_11 = str_pad('', 10, 'X');
289 $_12 = $names['code_fpn'];
290 $_13 = str_pad('', 25, 'X'); // for now, we just anonimise; otherwise use $names array
291 $_14 = str_pad('', 10, 'X');
292 $_15 = $names['code_lpn'];
293 $_16 = str_pad('', 6, 'X');
294 $_17 = substr($infopatient['postal_code'], 0, 4) . 'AA'; // anonimise postalcode (first 4 char + 2 A's)
295 //$_18 = str_pad($infopatient['pdn_number'], 5, ' ', STR_PAD_LEFT); // house number
296 $_18 = str_pad('1', 5, ' ', STR_PAD_LEFT);
297 $_19 = str_pad('', 4, 'X');
298 $_20 = substr(vk_countrycode($infopatient['country_code']), 0, 2); // assure there's only 2 chars here
299 if ( !$_20 ) $_20 = 'XX'; // default value in case of blank values
301 $_21 = str_replace('-','', $infopatient['DOB']);
302 if ( 0 == $_21 ) fl_log("Patient with ID: {$infopatient['id']} doesn't have DOB!");
304 $_22 = $sex;
305 $_804 = str_pad($infopatient['ss'], 9, ' ');
307 $content = $_7 . $_8 . $_1250 . $_10 . $_11 . $_12 . $_13 . $_14 . $_15 . $_16 . $_17 . $_18 . $_19 . $_20 . $_21 . $_22 . $_804 . "\r\n";
308 //$content = $_7 .'|'. $_8 .'|'. $_1250 .'|'. $_10 .'|'. $_11 .'|'. $_12 .'|'. $_13 .'|'. $_14 .'|'. $_15 .'|'. $_16 .'|'. $_17 .'|'. $_18 .'|'. $_19 .'|'. $_20 .'|'. $_21 .'|'. $_22 .'|'. $_804 . "\r\n";
309 $GLOBALS['pk_patients']++; // count for pakbon
311 // WRITE DATA TO FILE
312 if ( fwrite($h, $content) === FALSE ) {
313 echo "Cannot write to file ($filename)";
314 exit;
319 fclose($h);
324 //-----------------------------------------------------------------------------
326 DIAGNOSE
328 @param
329 @return
331 function gf_diagnose() {
332 dbc_message('DIAGNOSE GENERATING');
334 $file = DBC_WORKINGDIR . '/diagnose.txt';
335 if ( !$h = fopen($file, 'wb') ) {
336 echo "Cannot create file ($filename)";
337 exit;
340 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
341 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
342 $r = mysql_query($q) or die(mysql_error());
344 if ( mysql_num_rows($r) ) {
345 while ( $row = mysql_fetch_array($r) ) {
346 // prepare data
347 $_950 = DECLINST;
348 $_951 = LOCATION;
350 $as1 = unserialize($row['ax_as1']);
351 $as2 = unserialize($row['ax_as2']);
352 $as3 = unserialize($row['ax_as3']);
353 $as4 = unserialize($row['ax_as4']);
354 $as5 = unserialize($row['ax_as5']);
355 $as1c = $as1['content']; $mainpos = (int)$as1['mainpos']; // mainpos is written in both places
356 $as2c = $as2['content'];
358 $_882 = str_pad($row['ax_id'], 20, ' ');
359 $_887 = str_replace('-','', $row['ax_odate']);;
361 // we must avoid MAIN DIAGNOSE (mainpos); this is written in zorgtraject
363 $partial_content = ''; // represents all the lines associated with a single DBC
364 $counter = 1;
365 $GLOBALS['pk_diagnoses'] = 0;
367 foreach ( $as1c as $a) {
368 if ( $counter != $mainpos ) {
369 $_883 = str_pad($a, 20, ' ');
370 $_885 = ' ';
371 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
372 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
373 $GLOBALS['pk_diagnoses']++;
375 $counter++;
378 if ( $as2c ) {
379 foreach ( $as2c as $a) {
380 if ( $counter != $mainpos ) {
381 $_883 = str_pad($a['code'], 20, ' ');
382 $_885 = ( $a['trekken'] ) ? 'J' : '';
383 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
384 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
385 $GLOBALS['pk_diagnoses']++;
387 $counter++;
389 } // if $as2c
392 $_883 = str_pad($as3, 20, ' '); $_885 = ' ';
393 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
394 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
395 $GLOBALS['pk_diagnoses']++;
397 $_883 = str_pad($as4, 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']++;
403 // if we don't have the second and last GAF, we fill them with gaf1 value
404 // for official validation purposes
405 if ( empty($as5['gaf2']) ) $as5['gaf2'] = $as5['gaf1'];
406 if ( empty($as5['gaf3']) ) $as5['gaf3'] = $as5['gaf1'];
407 foreach ( $as5 as $a) {
408 $_883 = str_pad($a, 20, ' '); $_885 = ' ';
409 $partial_content .= $_950 . $_951 . $_882 . $_887 . $_883 . $_885 ."\r\n";
410 //$partial_content .= $_950 .'|'. $_951 .'|'. $_882 .'|'. $_887 .'|'. $_883 .'|'. $_885 ."\r\n";
411 $GLOBALS['pk_diagnoses']++;
414 // WRITE DATA TO FILE
415 if ( fwrite($h, $partial_content) === FALSE ) {
416 echo "Cannot write to file ($filename)";
417 exit;
420 } // while
421 } // if
423 fclose($h);
428 //-----------------------------------------------------------------------------
430 GELEVERD ZORGPROFIEL TIJDSCHRIJVEN
432 @param
433 @return
435 function gf_tijdschrijven() {
436 dbc_message('TIJDSCHRIJVEN GENERATING');
439 $file = DBC_WORKINGDIR . '/geleverd_zorgprofiel_tijdschrijven.txt';
440 if ( !$h = fopen($file, 'wb') ) {
441 echo "Cannot create file ($filename)";
442 exit;
445 $content = '';
447 // for every DBC we find events associated
448 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0");
449 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 0");
450 $q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0 and ax_open = 99");
451 $r = mysql_query($q) or die(mysql_error());
453 if ( mysql_num_rows($r) ) {
454 while ( $row = mysql_fetch_array($r) ) {
455 // prepare data
456 $_919 = DECLINST;
457 $_920 = LOCATION;
458 $_921 = str_pad($row['ax_id'], 20, ' ');
460 // set begin and end date(if exists - for closed but not sent DBC)
461 // begin date is ax_odate only for the first DBC in ZTN
462 // the followers take the beginning date from the previous DBC - closing date
463 $sign = '>';
464 if ( first_dbc_2($row['ax_id'], $row['ax_ztn']) ) {
465 $bd_dbc = $row['ax_odate']; $sign = '>=';
466 } else {
467 $bd_dbc = previous_dbc($row['ax_id'], $row['ax_ztn']);
470 $ed_dbc = ( ($row['ax_cdate'] !== '0000-00-00') && (!empty($row['ax_cdate'])) ) ? $row['ax_cdate'] : date('Y-m-d');
471 $pid = what_patient($row['ax_id']);
473 // find all events between DBC's dates and sum up total times
474 $qevent = sprintf("SELECT * FROM openemr_postcalendar_events
475 WHERE pc_pid = '%s' AND pc_eventDate $sign '%s' AND pc_eventDate <= '%s' AND pc_apptstatus = '@'",
476 $pid, $bd_dbc, $ed_dbc);
477 $revent = mysql_query($qevent) or die(mysql_error());
480 // we are doing this because in the case of enabled multiple providers option,
481 // there are some events duplicated but with the same content (except for providers field)
482 $m_arr = array(); // array with distinct values for pc_multiple
483 $revent_good = array();
484 while ( $rowe = mysql_fetch_array($revent) ) {
485 // MULTIPLE PROVIDERS CASE
486 if ( $rowe['pc_multiple'] ) {
487 if ( !in_array($rowe['pc_multiple'], $m_arr) ) {
488 $revent_good[] = $rowe;
489 $m_arr[] = $rowe['pc_multiple'];
491 // SINGLE PROVIDERS CASE
492 } else {
493 $revent_good[] = $rowe;
498 // we build for every event a $content
499 foreach ( $revent_good as $rg ) {
500 $_922 = str_pad($rg['pc_eid'], 20, ' ');
501 $_873 = str_pad(what_activity_event($rg['pc_eid']), 20, ' ');
503 if ( empty($rg['pc_eventDate']) )
504 fl_log("Event eid = {$rg['eid']} has an empty date for pc_eventDate!");
505 $_874 = str_replace('-','', $rg['pc_eventDate']);
507 $_877 = str_pad(what_profession_provider($rg['pc_aid']), 20, ' ');
509 if ( empty($rg['pc_duration']) ) $rg['pc_duration'] = 0;
510 $_880 = str_pad($rg['pc_duration']/60, 6, ' ');
512 // get indirect+travel time
513 $time = what_time_event($rg['pc_eid']);
514 if ( empty($time['indirect_time']) ) $time['indirect_time'] = 0;
515 if ( empty($time['travel_time']) ) $time['travel_time'] = 0;
516 $_954 = str_pad($time['indirect_time'], 6, ' ');
517 $_955 = str_pad($time['travel_time'], 6, ' ');
519 // time validation
520 if ( fl_sumup_time(trim($_880), $time['indirect_time'], $time['travel_time'], $_921, $_922) ) {
521 $content .= $_919 . $_920 . $_921 . $_922 . $_873 . $_874 . $_877 . $_880 . $_954 . $_955 ."\r\n";
522 //$content .= $_919 .'|'. $_920 .'|'. $_921 .'|'. $_922 .'|'. $_873 .'|'. $_874 .'|'. $_877 .'|'. $_880 .'|'. $_954 .'|'. $_955 ."\r\n";
523 $GLOBALS['pk_tijdschrijven']++;
525 } // for each
526 } // while
527 } // if
531 // WRITE DATA TO FILE
532 if ( fwrite($h, $content) === FALSE ) {
533 echo "Cannot write to file ($filename)";
534 exit;
537 fclose($h);
541 //-----------------------------------------------------------------------------
543 PAKBON
545 generate pakbon.txt file
547 @param
548 @return
550 function gf_pakbon() {
551 dbc_message('PAKBON GENERATING');
553 $file = DBC_WORKINGDIR . '/pakbon.txt';
554 if ( !$h = fopen($file, 'wb') ) {
555 echo "Cannot create file ($filename)";
556 exit;
559 //$q = sprintf("SELECT * FROM cl_axes WHERE ax_sti = 0");
560 //$r = mysql_query($q) or die(mysql_error());
562 // prepare data128
563 $_996 = DECLINST;
564 $_997 = LOCATION;
565 $_995 = '03.0';
566 $_980 = date('Ymd');
567 $_981 = 'DIS_GGZ_DBC_' .TEPR. '_030_' .DECLINST. '_' .LOCATION. '_' .date('Ym'). '01.zip';
569 // TESTS | PROD
570 $_1233 = str_pad('OpenEMR-DBC01', 15, ' ');
571 $_982 = str_pad($GLOBALS['pk_patients'], 7, ' ', STR_PAD_LEFT);
572 $_1013 = str_pad($GLOBALS['pk_careroutes'], 7, ' ', STR_PAD_LEFT);
573 $_987 = str_pad($GLOBALS['pk_dbcs'], 7, ' ', STR_PAD_LEFT);
574 $_988 = str_pad($GLOBALS['pk_diagnoses'], 7, ' ', STR_PAD_LEFT);
575 $_990 = str_pad($GLOBALS['pk_tijdschrijven'], 7, ' ', STR_PAD_LEFT);
576 $_991 = str_pad(0, 7, ' ', STR_PAD_LEFT);
577 $_992 = str_pad(0, 7, ' ', STR_PAD_LEFT);
578 $_1234 = str_pad(0, 7, ' ', STR_PAD_LEFT);
579 $_994 = str_pad(0, 7, ' ', STR_PAD_LEFT);
580 $content = $_996 . $_997 . $_995 . $_980 . $_981 . $_1233 . $_982 . $_1013 . $_987 . $_988 . $_990 .
581 $_991 . $_992 . $_1234 . $_994 ."\r\n";
582 //$content = $_996 .'|'. $_997 .'|'. $_995 .'|'. $_980 .'|'. $_981 .'|'. $_1233 .'|'. $_982 .'|'. $_1013 .'|'. $_987 .'|'. $_988 .'|'. $_990 .'|'. $_991 .'|'. $_992 .'|'. $_1234 .'|'. $_994 ."\r\n";
584 // WRITE DATA TO FILE
585 if ( fwrite($h, $content) === FALSE ) {
586 echo "Cannot write to file ($filename)";
587 exit;
589 fclose($h);
593 //-----------------------------------------------------------------------------
595 EMPTY
597 generate empties files
599 @param
600 @return
602 function gf_empty() {
603 // array with empties files
604 $empties = array('overige_verrichting.txt', 'geleverd_zorgprofiel_verblijfsdagen.txt', 'geleverd_zorgprofiel_verrichtingen.txt',
605 'geleverd_zorgprofiel_dagbesteding.txt');
607 foreach ( $empties as $e ) {
608 $file = DBC_WORKINGDIR . '/' . $e;
609 if ( !$h = fopen($file, 'wb') ) {
610 echo "Cannot create file ($filename)";
611 exit;
613 fclose($h);
619 //-----------------------------------------------------------------------------
620 //-----------------------------------------------------------------------------
621 //-----------------------------------------------------------------------------
623 CONNECT TO DATABASE
625 make the connections to openemr database
627 @param none
628 @return void
630 function db_connect() {
631 $link = mysql_connect(HOST, USER, PASS);
632 if (!$link) {
633 die('Could not connect: ' . mysql_error());
636 mysql_query("USE " . DATABASE);
640 //-----------------------------------------------------------------------------
642 RETURN THE CODE FOR A ZORG IDENTIFIED BY SYSID
644 @param int sysid - zorg sysid
645 @param int dbc sysid - return the code for ASSOCIATED zorg with this dbc
646 @return string
648 function what_zorg($id = 0, $dbc = 0) {
649 if ( !$id && !$dbc) return FALSE;
651 if ( $dbc ) {
652 $q = sprintf("SELECT * FROM cl_zorg_dbc WHERE zd_dbc = %d ", $dbc);
653 $r = mysql_query($q) or die( mysql_error() );
654 $row = mysql_fetch_array($r);
655 $id = (int)$row['zd_zorg']; if ( !$id ) return FALSE;
658 $q = sprintf("SELECT * FROM cl_zorg WHERE cl_zorgtype_sysid = %d ", $id);
659 $r = mysql_query($q) or die( mysql_error() );
661 if ( mysql_num_rows($r) ) {
662 $row = mysql_fetch_array($r);
663 return $row['cl_zorgtype_code'];
664 } else {
665 fl_log("DBC with ID: $id doesn't have a zorg code in cl_zorg_dbc!");
666 return 0;
671 //-----------------------------------------------------------------------------
673 RETURN THE CODE FOR REDENSLUITEN
675 @param int $dbcid
676 @return int
678 function what_reden($dbcid) {
679 if ( !$dbcid ) return FALSE;
681 $q = sprintf("SELECT * FROM cl_redensluiten_dbc WHERE rd_dbc = %d ", $dbcid);
682 $r = mysql_query($q) or die( mysql_error() );
684 if ( mysql_num_rows($r) ) {
685 $row = mysql_fetch_array($r);
686 return $row['rd_redencode'];
687 } else {
688 fl_log("DBC with ID: $dbcid doesn't have a redensluiten code in cl_redensluiten_dbc!");
689 return 0;
694 //-----------------------------------------------------------------------------
696 RETURN THE CODE FOR CIRCUIT
698 NEW VERSION !!!!
700 @param int $ztnid - we based our search on ztn id
701 @return int
703 function what_circuit_new($dbcid) {
704 if ( !$dbcid ) return FALSE;
706 // find patient id
707 $qs = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d", $dbcid);
708 $rs = mysql_query($qs) or die(mysql_error());
709 $row = mysql_fetch_array($rs);
710 $pid = $row['ccd_circuitcode'];
712 return $pid;
716 //-----------------------------------------------------------------------------
718 RETURN THE CODE FOR PRODUCTGROEP
720 @param int sysid - productgroep sysid
721 @param int dbc sysid - return the code for ASSOCIATED zorg with this dbc
722 @return string
724 function what_stoornis($id = 0, $dbcid = 0) {
725 if ( !$id && !$dbcid) return FALSE;
727 if ( $dbcid ) {
728 $q = sprintf("SELECT * FROM cl_productgroep_dbc WHERE pc_dbc = %d ", $dbcid);
729 $r = mysql_query($q) or die( mysql_error() );
730 $row = mysql_fetch_array($r);
731 $id = (int)$row['pc_productcode']; if ( !$id ) return FALSE;
734 $q = sprintf("SELECT * FROM cl_productgroep WHERE cl_productgroep_sysid = %d ", $id);
735 $r = mysql_query($q) or die( mysql_error() );
736 $row = mysql_fetch_array($r);
737 return $row['cl_productgroep_code'];
741 //-----------------------------------------------------------------------------
743 RETURN THE UNIQUE GENERATED CODE FOR A PATIENT
745 return the id1250 for patient.txt file
747 @param int $patientid - patient id as in OpenEMR tables
748 @return string
750 function what_joining_number($patientid = 0) {
751 if ( !$patientid ) return FALSE;
753 $patq = sprintf("SELECT * FROM cl_patient_number WHERE pn_oemrid = %d", $patientid);
754 $r = mysql_query($patq) or die( mysql_error() );
756 if ( mysql_num_rows($r) ) {
757 $row = mysql_fetch_array($r);
758 return $row['pn_id1250'];
759 } else {
760 fl_log("Patient with ID: $patientid doesn't have a unique number in cl_patient_number!");
761 return 0;
765 //-----------------------------------------------------------------------------
767 RETURN ALL INFOS ABOUT A PATIENT
769 query patient_data original database
771 @param int $patientid - patient id as in OpenEMR tables
772 @return array
774 function info_patient($patientid = 0) {
775 if ( !$patientid ) return FALSE;
777 $patq = sprintf("SELECT * FROM patient_data pd, patient_data_NL pdn JOIN patient_data_NL ON
778 pdn.pdn_id = pd.id WHERE pd.id = %d", $patientid);
779 $r = mysql_query($patq) or die( mysql_error() );
780 $row = mysql_fetch_array($r);
782 return $row;
785 //-----------------------------------------------------------------------------
787 RETURN ACTIVITY CODE
789 query cl_event_activiteit for activity id
791 @param int $eid - eventid
792 @return string
794 function what_activity_event($eid = 0) {
795 if ( !$eid ) return FALSE;
797 $eidq = sprintf("SELECT cl_activiteit_code FROM cl_activiteit ca JOIN cl_event_activiteit cea
798 ON ca.cl_activiteit_sysid = cea.activity_sysid WHERE cea.event_id = %d", $eid);
799 //$eidq = sprintf("SELECT activity_sysid FROM cl_event_activiteit WHERE event_id = %d", $eid);
800 $r = mysql_query($eidq) or die( mysql_error() );
801 if ( mysql_num_rows($r) ) {
802 $row = mysql_fetch_array($r);
803 //return (int)$row['activity_sysid'];
804 return $row['cl_activiteit_code'];
805 } else {
806 fl_log("Event with eid = $eid don't have an associated activity code in cl_event_activiteit");
807 return 0;
811 //-----------------------------------------------------------------------------
813 RETURN PROFESSION CODE
815 query cl_user_beroep for profession id
817 @param int $pid - provider id
818 @return int
820 function what_profession_provider($pid = 0) {
821 if ( !$pid ) return FALSE;
823 //$pidq = sprintf("SELECT * FROM cl_user_beroep WHERE cl_beroep_userid = %d", $pid);
824 $pidq = sprintf("SELECT cl_beroep_code FROM cl_beroep cb JOIN cl_user_beroep cub
825 ON cb.cl_beroep_sysid = cub.cl_beroep_sysid WHERE cub.cl_beroep_userid = %d", $pid);
826 $r = mysql_query($pidq) or die( mysql_error() );
827 if ( mysql_num_rows($r) ) {
828 $row = mysql_fetch_array($r);
829 return $row['cl_beroep_code'];
830 //return $row['cl_beroep_sysid'];
831 } else {
832 fl_log("Provider with $pid don't have an associated beroep (job) code in cl_user_beroep");
833 return 0;
837 //-----------------------------------------------------------------------------
839 RETURN TIMES FOR AN EVENT
841 query cl_time_activiteit for an event
843 @param int $eid - activity id
844 @return array
846 function what_time_event($eid = 0) {
847 if ( !$eid ) return FALSE;
849 // check if this id is part of a multiple providers record
850 // what is multiple key around this $eid?
851 $rq = sprintf("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = %d", $eid);
852 $rezq = mysql_query($rq) or die( mysql_error() );
853 $rowmulti = mysql_fetch_array($rezq);
855 if ( $rowmulti['pc_multiple'] ) {
856 // --------------- MULTIPLE PROVIDERS CASE ----------------
857 // what are all pc_eid's grouped by multiple key
858 $eventsrow = array();
859 $rezev = mysql_query("SELECT pc_eid FROM openemr_postcalendar_events WHERE pc_multiple = {$rowmulti['pc_multiple']}");
860 while ( $row = mysql_fetch_array($rezev) ) {
861 $eventsrow[] = $row['pc_eid'];
864 // we look in cl_time_activiteit for a matching record
865 $timerow = '';
866 foreach ( $eventsrow as $ev) {
867 $time = mysql_query("SELECT * FROM cl_time_activiteit WHERE event_id = $ev");
868 if ( mysql_num_rows($time) ) {
869 $timeres = mysql_fetch_array($time);
870 $timerow = ( $timeres ) ? $timeres : '';
871 } else {
872 $timerow = array('indirect_time' => 0, 'travel_time' => 0); // as a last solution, return an empty array
875 // --------------- EOS MULTIPLE PROVIDERS CASE ----------------
876 } else {
877 // --------------- SINGLE PROVIDERS CASE ----------------
878 $time = mysql_query("SELECT * FROM cl_time_activiteit WHERE event_id = $eid");
879 if ( mysql_num_rows($time) ) {
880 $timerow = mysql_fetch_array($time);
881 } else {
882 $timerow = array('indirect_time' => 0, 'travel_time' => 0); // as a last solution, return an empty array
884 // --------------- EOS SINGLE PROVIDERS CASE ----------------
887 return $timerow;
891 //-----------------------------------------------------------------------------
893 UPDATE DATABASE
896 @param none
897 @return void
899 function update_db() {
900 // for every open DBC generate a duplicate one; the older one
901 // will be marked as sent_to_insurer and closed
903 $date = date('Y-m-d');
904 mysql_query('START TRANSACTION');
906 // look for openened dbc
907 $q = sprintf("SELECT * FROM cl_axes WHERE ax_open = 1 AND ax_sti = 0");
908 $r = mysql_query($q) or die(mysql_error());
910 if ( !mysql_num_rows($r) ) return;
912 while ( $row = mysql_fetch_array($r) ) {
913 transform_dbc($row['ax_id']);
914 } // while
916 mysql_query('COMMIT');
920 //-----------------------------------------------------------------------------
922 UPDATE DATABASE
924 FUNCTION ONLY FOR DIRTY TRICKS: CLOSING PER MONTHS IN 2007!!!!
925 DON'T USE IT IN A REGULAR PRODUCTION ENV
927 @param none
928 @return void
930 function update_db_2007() {
931 global $dbcid_arr;
933 foreach ( $dbcid_arr as $dbc ) {
934 $q = sprintf("UPDATE cl_axes SET ax_sti = 1, ax_open = 0 WHERE ax_id = %d", $dbc);
935 echo $q . '<br />';
936 mysql_query($q) or die(mysql_error());
940 //-----------------------------------------------------------------------------
942 TRANSFORM DBC
944 duplicate a DBC and then mark the older one with sent to insurer flag
946 @param int $dbc id
947 @return void
949 function transform_dbc($dbcid = 0) {
951 if ( !$dbcid ) return FALSE;
953 mysql_query("START TRANSACTION");
955 $date = date('Y-m-d');
957 // read the current dbc
958 $q = sprintf("SELECT * FROM cl_axes WHERE ax_id = %d", $dbcid);
959 $r = mysql_query($q) or die(mysql_error());
960 $dbc = mysql_fetch_array($r);
962 // duplicate it (sent to insurer flag = 0 for the new one, 1 for the older one)
963 $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)
964 VALUES ('%s', %d,'%s','%s','%s','%s','%s','%s','%s','%s')", $dbc['ax_ztn'], 1, $dbc['ax_as1'], $dbc['ax_as2'],
965 $dbc['ax_as3'],$dbc['ax_as4'],$dbc['ax_as5'], $date, 0, 0);
966 mysql_query($qi) or die (mysql_error());
968 // close the old one
969 $qu = sprintf("UPDATE cl_axes SET ax_open = 0, ax_cdate = '%s', ax_sti = 1 WHERE ax_id = %d", $date, $dbcid);
970 mysql_query($qu) or die(mysql_error());
972 // update the related tables (cl_circuit_dbc)
973 $qc = sprintf("SELECT ccd_circuitcode FROM cl_circuit_dbc WHERE ccd_dbcid = %d ", $dbcid);
974 $rc = mysql_query($qc) or die(mysql_error());
975 $circuit = mysql_fetch_array($rc);
977 mysql_query("INSERT INTO cl_circuit_dbc(ccd_circuitcode, ccd_dbcid) VALUES (%d, %d)", $circuit['ccd_circuitcode'], $dbcid);
979 mysql_query("COMMIT");
982 //-----------------------------------------------------------------------------
984 NAAMCODE
986 1 then naam_1 is the name of the married partner; 2 - name of the patient
988 @param int $pid
989 @return int (1 or 2)
991 function naamcode_1($pid = 0) {
992 if (!$pid) return 0;
994 $qp = sprintf("SELECT fname FROM patient_data WHERE id = %d ", $pid);
995 $rp = mysql_query($qp) or die(mysql_error());
996 $row = mysql_fetch_array($rp);
998 return ( $row['fname'] ) ? 2 : 1;
1002 //-----------------------------------------------------------------------------
1004 NAMES
1006 return the names for patient
1007 (we use patient names or if we don't have, partner name)
1009 @param int $pid - patient id
1010 @return array
1012 function names($pid = 0) {
1013 if (!$pid) return 0;
1015 $qp1 = sprintf("SELECT fname, lname FROM patient_data WHERE id = %d ", $pid);
1016 $rp1 = mysql_query($qp1) or die(mysql_error());
1017 $row1 = mysql_fetch_array($rp1);
1019 $qp2 = sprintf("SELECT pdn_pxlast, pdn_pxlastpar,pdn_lastpar FROM patient_data_NL WHERE pdn_id = %d ", $pid);
1020 $rp2 = mysql_query($qp2) or die(mysql_error());
1021 $row2 = mysql_fetch_array($rp2);
1023 $ret = array();
1025 // first part of the name (if we don't have it, the pad just 25X)
1026 // first part = firstname + lastname patient
1027 // second part = lastname partner (if any)
1028 $firstpart = $row1['fname'] .' '. $row1['lname'];
1029 $ret['firstpname'] = str_pad($firstpart, 25, ' ', STR_PAD_RIGHT);
1030 $ret['prefix_fpn'] = ( $row2['pdn_pxlast'] ) ? str_pad($row2['pdn_pxlast'], 10, ' ') : str_pad('', 10, 'X');
1031 $ret['code_fpn'] = '2'; // name of the patient
1033 // last part of the name (partner ones)
1034 if ( $row2['pdn_lastpar'] ) {
1035 $ret['lastpname'] = str_pad($row2['pdn_lastpar'], 25, ' ', STR_PAD_RIGHT);
1036 $ret['prefix_lpn'] = str_pad($row2['pdn_pxlastpar'], 10, ' ', STR_PAD_RIGHT);
1037 $ret['code_lpn'] = '1'; // name of the patient
1038 } else {
1039 // empty values
1040 $ret['lastnpame'] = str_pad('', 25, ' ', STR_PAD_RIGHT);
1041 $ret['prefix_lpn'] = str_pad('', 10, ' ', STR_PAD_RIGHT);
1042 $ret['code_lpn'] = ' ';
1045 return $ret;
1048 //-----------------------------------------------------------------------------
1050 FIRST DBC?
1052 find if a dbc is the first or a 'follow-up' or there is no DBC yet.
1053 (the same function is in DBC_functions with a small modification here --> ztn arg)
1055 @param int - ax_id - the DBC id
1056 @param int - ztn id
1057 @return bool | int
1059 function first_dbc_2($ax_id, $ztn_id) {
1060 // to be the first means there is only one DBC per open ZTN
1061 $qz = sprintf("SELECT * FROM cl_axes WHERE ax_ztn='%s' AND ax_id < %d", $ztn_id, $ax_id);
1062 $rez = mysql_query($qz) or die(mysql_error());
1064 return ( !mysql_num_rows($rez) );
1067 //-----------------------------------------------------------------------------
1069 PREVIOUS DBC
1071 find the previous dbc for a given one and return its closing date
1073 @param int - ax id
1074 @param int $ztn_id - ztn id
1075 @return string
1077 function previous_dbc($ax_id, $ztn_id) {
1078 $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);
1079 $rez = mysql_query($qp) or die(mysql_error());
1080 $r = mysql_fetch_array($rez);
1082 return $r['ax_cdate'];
1086 // ----------------------------------------------------------------------------
1088 SIMILAR LOG FUNCTION WITH THE ONE FROM DBC_VALIDATIONS
1090 simple function to log different events
1092 @param string $string
1093 @return
1095 function fl_log($string) {
1096 $file = TMPDIR_DBC . '/DBC_problems.log';
1097 if ( !$h = fopen($file, 'ab') ) {
1098 echo "Cannot create file ($file)";
1099 exit;
1102 $content = date('d-m-Y') . " $string \r\n";
1104 // WRITE DATA TO FILE
1105 if ( fwrite($h, $content) === FALSE ) {
1106 echo "Cannot write to file ($file)";
1107 exit;
1110 fclose($h);
1114 // ----------------------------------------------------------------------------
1116 VERIFY IF THE TOTAL TIME IF GREATER THAN 0
1117 AND ALSO IF INDIRECT TIME>0 AND DIRECT TIME = 0
1119 @param int $duration - encounter duration
1120 @param int $indirect - indirect time
1121 @param int $travel - travel time
1122 @param int $axid - id for DBC
1123 @param int $eid - event id
1124 @return bool
1126 function fl_sumup_time($duration, $indirect = 0, $travel = 0, $axid = 0, $eid = 0) {
1127 $sum = (int)$duration + (int)$indirect + (int)$travel;
1129 if ( !$sum ) {
1130 fl_log("DBC id:$axid EVENT: $eid has total time = 0 (E:$duration/I:$indirect/T:$travel)");
1131 return FALSE;
1132 } else if ( $indirect > 0 && ($duration == 0 && $travel == 0) ) {
1133 fl_log("DBC id:$axid has indirect time = $indirect but direct and travel time = 0.");
1134 return FALSE;
1137 return TRUE;
1142 // ----------------------------------------------------------------------------
1144 VERIFY IF THE PATIENT HAS SOME REQUIRED PROVIDERS WITH THE RIGHT JOBS
1146 @param string $zorg
1147 @param string $odate - opening date for dbc
1148 @param string $cdate - closing date for dbc
1149 @param string $ztn - ztn id
1152 function fl_mb_beroep($zorg, $odate, $cdate, $ztn) {
1153 // the zorg values who'll be checked for beroep
1154 $checklist = array(110, 206, 116, 211);
1156 $zo = (int)trim($zorg);
1157 if ( !in_array($zo, $checklist) ) {
1158 return TRUE;
1159 } else {
1160 // find the id for the user
1161 $qu = sprintf("SELECT cn_pid FROM cl_careroute_numbers WHERE cn_ztn = '%s'", trim($ztn));
1162 $ru = mysql_query($qu) or die(mysql_error());
1163 $rowu = mysql_fetch_array($ru);
1164 $user = $rowu['cn_pid'];
1166 // find all events between the opening and closing of the DBC
1167 // and look for every provider's job
1168 $qe = sprintf("SELECT * FROM openemr_postcalendar_events WHERE pc_pid = '%s' AND pc_apptstatus = '@'
1169 AND pc_eventDate >= '%s' AND pc_eventDate <= '%s' ", $user, $odate, $cdate);
1170 $re = mysql_query($qe) or die(mysql_error());
1172 $has = FALSE;
1173 if ( mysql_num_rows($re) ) {
1174 while ( $row = mysql_fetch_array($re) ) {
1175 $job = what_beroep($row['pc_aid'], 1);
1176 if ( preg_match('/^MB\./', $job) ) $has |= TRUE;
1178 } // if num_rows
1180 if ( !$has ) fl_log("USER/ZTN: $user/$ztn doesn't have at least one provider with job (MB.%)");
1182 } // if else
1187 // ----------------------------------------------------------------------------
1189 * DISPLAY A MESSAGE
1191 * used to display messages in DBC generation phase
1192 * must be called only with hardcoded (and safe) strings!
1194 * @param string $msg
1196 function dbc_message($msg) {
1197 $str = "$msg <br>";
1198 echo $str;
1202 // ----------------------------------------------------------------------------