forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emr_scan_load.plx
executable file
·485 lines (429 loc) · 15.8 KB
/
emr_scan_load.plx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/perl
use strict;
use Time::Local;
use DBI;
#######################################################################
# Copyright (C) 2006 Rod Roark <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#######################################################################
# This program is to be run frequently via the system crontab. On
# each run it will move scanned-in documents from a shared directory
# into matching locations in the openemr/documents directory, and also
# update the database accordingly.
# Each scanned-in file must be placed into a directory corresponding
# to its category, and its name must begin with the patient's pubpid
# followed by any non-alphanumeric character. For example:
# <shared-directory>/Categories/XRay/1234-knee-xray-20060131.jpg
#######################################################################
# NOTE: This is contributed as-is for the possible benefit of those who
# may wish to build on it. As of this writing it is not known how well
# it works, if at all, with current OpenEMR.
#######################################################################
#######################################################################
# Parameters that you must customize #
#######################################################################
# Parameters for MySQL database connections:
#
my $DBNAME = "openemr"; # database name
my $DBUSER = "openemr"; # database user name
my $DBPASS = "secret"; # database user's password
# Log file location:
#
my $INSLOG = "/mnt/drive2/emr_scan_load.log";
# Shared directory base where the scanners deposit files:
#
my $INPATH = "/mnt/drive2/scan_docs";
# Base directory for OpenEMR documents:
#
# my $OUTPATH = "/usr/local/apache2/htdocs/openemr/documents";
my $OUTPATH = "/mnt/drive2/documents";
# This should specify the user and group that the web server runs as:
#
my $CHOWN_COMMAND = "chown nobody:nogroup";
# This is the user from whom patient notes are addressed:
#
my $SCANNER_OPERATOR = 'ksears';
# This person gets patient notes if the doctor cannot be determined or is
# out of the office:
#
my $DEFAULT_PRACTITIONER = 'candroney';
# We need a SQL condition to identify encounter forms that will only be
# entered by practitioners. Yeah there's probably a better way.
#
my $PRACTITIONER_FORM =
"formdir = 'soap' OR formdir = 'reviewofs' OR formdir = 'ros'";
# For each day of the week (Sun-Sat), if the office is open:
#
my @open_days = (0, 1, 1, 1, 1, 1, 0);
# Office closing time:
#
my $closing_time = '17:00:00';
# Set this to 0 for production use:
#
my $DEBUG = 0;
#######################################################################
# Initialization #
#######################################################################
my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME", $DBUSER, $DBPASS)
or die $DBI::errstr;
$| = 1; # Turn on autoflushing of stdout.
#######################################################################
# Functions #
#######################################################################
# Write a log message.
#
sub tolog($$) {
my ($msg, $error) = @_;
my @tm = localtime; $tm[5] += 1900; $tm[4] += 1;
my $ts = sprintf "%04u-%02u-%02u %02u:%02u:%02u",
$tm[5], $tm[4], $tm[3], $tm[2], $tm[1], $tm[0];
if ($error) {
$msg = '***ERROR: ' . $msg;
}
if ($DEBUG) {
$msg = '*DEBUGGING* ' . $msg;
}
open LOG, ">> $INSLOG" or die "Cannot open $INSLOG: $!";
print LOG "$ts $msg\n";
close LOG;
}
# Determine if the designated doc is in the office at the specified time,
# or will be later that day.
#
sub is_doc_available($$) {
my ($practitioner, $now) = @_;
$now = time() if (! $now);
my @tm = localtime $now; $tm[5] += 1900; $tm[4] += 1;
my $current_date = sprintf "%04u-%02u-%02u", $tm[5], $tm[4], $tm[3];
my $current_time = sprintf "%02u:%02u:%02u", $tm[2], $tm[1], $tm[0];
my $daynow = int($now / (24 * 60 * 60));
my $docid = $dbh->selectrow_array("SELECT id FROM users WHERE " .
"username = '$practitioner'");
my $query = "SELECT " .
"pc_catid, pc_eventDate, pc_endDate, pc_recurrtype, pc_recurrspec, " .
"pc_startTime, pc_endTime, pc_alldayevent " .
"FROM openemr_postcalendar_events " .
"WHERE pc_aid = '$docid' AND " .
"( pc_catid = 2 OR pc_catid = 3 OR pc_duration >= 21600 ) AND " .
"pc_eventDate <= '$current_date' AND pc_endDate >= '$current_date' " .
"ORDER BY pc_startTime";
my $esth = $dbh->prepare($query)
or die $dbh->errstr;
$esth->execute() or die $esth->errstr;
# &tolog($query, 0); # debugging
my $vacation = 0;
my $in_active = 0;
my $in_until = '';
# Look at each event selected.
#
while (my @erow = $esth->fetchrow_array()) {
my ($pc_catid, $pc_eventDate, $pc_endDate, $pc_recurrtype, $pc_recurrspec,
$pc_startTime, $pc_endTime, $pc_alldayevent) = @erow;
my $repeattype = '0';
my $repeatfreq = '0';
if ($pc_recurrspec =~ /"event_repeat_freq_type";s:1:"(\d)"/) {
# 0 = day, 1 = week, 2 = month, 3 = year, 4 = workday
$repeattype = $1;
}
if ($pc_recurrspec =~ /"event_repeat_freq";s:1:"(\d)"/) {
# 1 = every, 2 = every other, etc.
$repeatfreq = $1;
}
# If this is a repeating event, determine if it applies to today.
#
if ($pc_recurrtype) {
$pc_eventDate =~ /^(\d+)\D(\d+)\D(\d+)/;
my $time0 = timelocal(1, 0, 0, $3, $2 - 1, $1 - 1900);
my $day0 = int($time0 / (24 * 60 * 60));
my $elapsed_days = $daynow - $day0;
my @tm0 = localtime $time0; $tm0[5] += 1900; $tm0[4] += 1;
if ($repeattype == 0) { # day
if ($repeatfreq > 1) {
my $quotient = sprintf('%.4f', $elapsed_days / $repeatfreq);
next if ($quotient != int($quotient));
}
}
elsif ($repeattype == 1) { # week
my $repdays = $repeatfreq * 7;
if ($repdays > 0) {
my $quotient = sprintf('%.4f', $elapsed_days / $repdays);
next if ($quotient != int($quotient));
}
}
elsif ($repeattype == 2) { # month
next if ($tm[3] != $tm0[3]); # if not same day of month
if ($repeatfreq > 1) {
my $elapsed_months = ($tm[5] - $tm0[5]) * 12 + $tm[4] - $tm0[4];
my $quotient = sprintf('%.4f', $elapsed_months / $repeatfreq);
next if ($quotient != int($quotient));
}
}
elsif ($repeattype == 3) { # year
next if ($tm[3] != $tm0[3] || $tm[4] != $tm0[4]);
if ($repeatfreq > 1) {
my $elapsed_years = $tm[5] - $tm0[5];
my $quotient = sprintf('%.4f', $elapsed_years / $repeatfreq);
next if ($quotient != int($quotient));
}
}
elsif ($repeattype == 4) { # work day (M-F)
next if ($tm0[6] == 0 || $tm0[6] == 6); # if today is not a work day
if ($repeatfreq > 1) {
my $dowdiff = $tm[6] - $tm0[6];
my $elapsed_workdays = ($elapsed_days - $dowdiff) * 5 / 7 + $dowdiff;
my $quotient = sprintf('%.4f', $elapsed_workdays / $repeatfreq);
next if ($quotient != int($quotient));
}
}
}
# Phew. Now we know that this event is applicable to this day.
if ($pc_catid == 2) { # In Office
$in_active = 1;
$in_until = '23:59:59';
}
elsif ($pc_catid == 3) { # Out of Office
if ($in_active) {
$in_until = $pc_startTime;
$in_active = 0;
}
}
else { # Vacation or equivalent
$vacation = 1;
}
}
return 1 if ($in_until && ! $vacation && $in_until gt $current_time);
return 0;
}
# Generate a patient note if appropriate for this top-level category.
#
sub generate_note($$$) {
# my ($pid, $path, $topcategory, $docid) = @_;
my ($pid, $path, $docid) = @_;
# if ($NO_NOTE_CATEGORIES{$topcategory}) {
# return;
# }
# Get the login name of the user who entered the last clinical form for
# this patient. That's who we'll send the note to.
#
my $fsth = $dbh->prepare("SELECT user, groupname FROM forms WHERE " .
"pid = '$pid' AND ( $PRACTITIONER_FORM ) ORDER BY date DESC LIMIT 1")
or die $dbh->errstr;
$fsth->execute() or die $fsth->errstr;
my @frow = $fsth->fetchrow_array();
#
my $assigned_to = $DEFAULT_PRACTITIONER;
my $groupname = '';
if (@frow) {
$assigned_to = $frow[0];
$groupname = $frow[1];
# Check the schedule to see if this doc is in today (or will be in the
# next working day if it's after hours now); if not, assign the default.
#
my $now = time();
my @tm = localtime $now;
my $current_time = sprintf "%02u:%02u:%02u", $tm[2], $tm[1], $tm[0];
while ($open_days[$tm[6]] == 0 || $current_time gt $closing_time) {
$current_time = '00:00:00';
$now = timelocal(0, 0, 0, $tm[3], $tm[4], $tm[5]) + (24 * 60 * 60);
@tm = localtime $now;
}
if (! &is_doc_available($assigned_to, $now)) {
&tolog("$assigned_to not available, using default practitioner", 0);
$assigned_to = $DEFAULT_PRACTITIONER;
}
} else {
&tolog("Patient $pid has no clinical forms, using default practitioner", 0);
}
# Build the text of the note including timestamp and addressing.
# The document ID is also included, so that OpenEMR can easily
# look up and display the document when the note is viewed.
#
my @tm = localtime; $tm[5] += 1900; $tm[4] += 1;
my $body = sprintf "%04u-%02u-%02u %02u:%02u",
$tm[5], $tm[4], $tm[3], $tm[2], $tm[1];
$body .= " ($SCANNER_OPERATOR to $assigned_to) ";
$body .= "New scanned document $docid: $path";
# Write it to the database.
#
my $query = "INSERT INTO pnotes ( date, body, pid, user, groupname, " .
"authorized, activity, title, assigned_to ) VALUES ( " .
"NOW(), '$body', '$pid', '$SCANNER_OPERATOR', '$groupname', '1', '1', " .
"'New Document', '$assigned_to')";
if (! $DEBUG) {
$dbh->do($query) or die $query;
}
&tolog("Patient note assigned to $assigned_to", 0);
}
# Process a document file.
#
sub process_file($$) {
my ($path, $notify) = @_;
# Extract the ending filename from the path. Clean it up a bit for
# use as an output filename. Return if it's a leftover problem.
#
my $dname = '';
my $fname = $path;
if ($path =~ m'^(.*)/([^/]+)$') {
$dname = $1;
$fname = $2;
}
return if ($fname =~ /^ERR/);
$fname =~ s/[^a-zA-Z0-9_.]/_/g;
while ($fname =~ s/__/_/g) {}
# Get out if the source file is open by any other process. This
# normally means that it's still being written via smbd.
#
if (my $pr = `lsof -t '$INPATH/$path'`) {
&tolog("Temporarily skipping '$path' which is open by process $pr", 0);
return;
}
# Get the chart number and look up the patient's pid.
#
my $pubpid = '';
if ($fname =~ /^([A-Za-z0-9]+)/) {
$pubpid = $1;
}
#
my $psth = $dbh->prepare("SELECT pid FROM patient_data " .
"WHERE pubpid = '$pubpid' LIMIT 1")
or die $dbh->errstr;
$psth->execute() or die $psth->errstr;
my @prow = $psth->fetchrow_array();
#
if (! @prow) {
&tolog("$path: there is no patient with chart id '$pubpid'", 1);
rename "$INPATH/$path", "$INPATH/$dname/ERR-$fname" if (! $DEBUG);
return;
}
if ($psth->fetchrow_array()) {
&tolog("$path: there are multiple patients with chart id '$pubpid'", 1);
rename "$INPATH/$path", "$INPATH/$dname/ERR-$fname" if (! $DEBUG);
return;
}
my $pid = $prow[0];
# Look up the document category and get its ID.
#
my @catpath = split /\//, $dname;
my $catid = 0;
my $catname = '';
for (my $i = 0; $i < scalar @catpath; ++$i) {
$catname = $catpath[$i];
$catid = $dbh->selectrow_array("SELECT id FROM categories WHERE " .
"name = '$catname' AND parent = $catid");
}
if (! $catid) {
&tolog("Category '$dname' does not exist", 1);
rename "$INPATH/$path", "$INPATH/$dname/ERR-$fname";
return;
}
# Get the source file size; if zero, skip it. It appears this
# can be a case where the scanner software has created the
# directory entry but has not yet written the data, so we do
# not want to delete the file.
#
my $fsize = (stat("$INPATH/$path"))[7];
if (! $fsize) {
# &tolog("Deleting and skipping empty file '$path'", 1);
# unlink "$INPATH/$path";
&tolog("Skipping empty file '$path'", 1);
return;
}
# Make sure the target directory exists.
#
system "mkdir -p '$OUTPATH/$pid'; $CHOWN_COMMAND '$OUTPATH/$pid'" if (! $DEBUG);
# If the target filename exists, modify it until it doesn't.
#
my $count = 0;
while (-e "$OUTPATH/$pid/$fname") {
my $oldfname = $fname;
my $fsuff = '';
if ($fname =~ /^(.*)(\..+)$/) {
$fname = $1;
$fsuff = $2;
}
if ($count++) {
$fname =~ s/_\d+$//;
}
$fname .= '_' . $count . $fsuff;
&tolog("File '$pid/$oldfname' already exists; trying '$pid/$fname' ...", 0);
}
my $target = "$OUTPATH/$pid/$fname";
# Move the file to its destination and set its owner and group.
#
my $movecmd = "mv '$INPATH/$path' '$target'";
if (! $DEBUG) {
my $rc = system $movecmd;
if ($rc != 0) {
&tolog("Command '$movecmd' failed with return code $rc", 1);
return;
}
system "$CHOWN_COMMAND '$target'";
}
# Compute assorted values for the documents table.
#
$dbh->do("update sequences set id = id + 1") if (! $DEBUG);
my $newid = $dbh->selectrow_array("SELECT id FROM sequences");
my @tm = localtime; $tm[5] += 1900; $tm[4] += 1;
my $ts1 = sprintf "%04u-%02u-%02u %02u:%02u:%02u", $tm[5], $tm[4], $tm[3], $tm[2], $tm[1], $tm[0];
my $ts2 = $ts1;
$ts2 =~ s/\D//g;
my $mimetype = $DEBUG ? '' : `file -i $target`;
$mimetype =~ s/;.*$//; # remove trailing "; charset=..." if present
$mimetype =~ s/^.*:\s*//; # remove everything preceding the mime type
$mimetype =~ s/\s*$//; # remove any trailing line feed or other whitespace
if (! $mimetype) {
&tolog("Unable to determine MIME type using 'file -i $target'; proceeding with empty type", 1);
}
if (! $DEBUG) {
# Update the database.
#
my $query = "INSERT INTO documents ( " .
"id, type, size, date, url, mimetype, revision, foreign_id" .
" ) VALUES ( " .
"'$newid', 'file_url', '$fsize', '$ts1', 'file:https://$target', '$mimetype', '$ts2', $pid " .
")";
$dbh->do($query) or die $query;
#
my $query = "INSERT INTO categories_to_documents ( " .
"category_id, document_id" .
" ) VALUES ( " .
"'$catid', '$newid' " .
")";
$dbh->do($query) or die $query;
}
&tolog("Loaded '$path' as $mimetype", 0);
# Generate the patient note if appropriate for this category.
#
# &generate_note($pid, "$dname/$fname", $catpath[1], $newid);
&generate_note($pid, "$dname/$fname", $newid) if $notify;
}
# Scan the source directory recursively to invoke processing of each
# document file.
#
sub scan_dir($) {
my $path = shift;
my $notify = -f "$INPATH/$path/.notify";
opendir my $dh, "$INPATH/$path";
while (my $dirent = readdir $dh) {
next if ($dirent =~ /^\./);
my $thispath = $path ? "$path/$dirent" : $dirent;
if (-d "$INPATH/$thispath") {
&scan_dir($thispath);
} else {
&process_file($thispath, $notify);
}
}
closedir $dh;
}
#######################################################################
# Processing #
#######################################################################
&scan_dir('Categories'); # This makes everything happen.
#######################################################################
# Shutdown #
#######################################################################
$dbh->disconnect;