Fixed various bugs and added date field support with popup calendar.
[openemr.git] / contrib / util / load_icd_desc.plx
blob71d429e9cd9c6ac37c586507b61a4c515bf70fc0
1 #!/usr/bin/perl
2 use strict;
4 use DBI;
6 #######################################################################
7 # Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This loads descriptions of ICD9 codes into the "codes" table of
15 # OpenEMR.
17 # Run it like this:
19 # ./load_icd_desc.plx < V22ICD9_FILE1.TXT
21 # To get this input file, download and extract v22_icd9.zip from
22 # http://www.cms.hhs.gov/providers/pufdownload/. After that, you might
23 # want to also load the "friendlier" descriptions from icd9_long.txt at
24 # http://www.aafp.org/.
25 #######################################################################
27 #######################################################################
28 # Parameters that you may customize #
29 #######################################################################
31 my $DBNAME = "openemr"; # database name
33 # You can hard-code the database user name and password (see below),
34 # or else put them into the environment with bash commands like these
35 # before running this script:
37 # export DBI_USER=username
38 # export DBI_PASS=password
40 my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME") or die $DBI::errstr;
42 # my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME", "username", "password")
43 # or die $DBI::errstr;
45 # temporary!
47 # $dbh->do("delete from codes where code_type = 2") or die "oops";
49 #######################################################################
50 # Startup #
51 #######################################################################
53 $| = 1; # Turn on autoflushing of stdout.
55 my $countup = 0;
56 my $countnew = 0;
58 #######################################################################
59 # Main Loop #
60 #######################################################################
62 while (my $line = <STDIN>) {
63 $line =~ s/^> //;
65 next unless ($line =~ /^([0-9A-Z]\d\d\S*)\s+(\S.*)$/);
67 my $code = $1;
68 my $desc = $2;
69 $code =~ s/\.//; # remove periods from the icd9 codes
70 $desc =~ s/\s*$//g; # remove all trailing whitespace
71 $desc =~ s/'/''/g; # just in case there are any quotes
73 my $usth = $dbh->prepare("SELECT id FROM codes " .
74 "WHERE code_type = 2 AND code = '$code'")
75 or die $dbh->errstr;
76 $usth->execute() or die $usth->errstr;
77 my @urow = $usth->fetchrow_array();
79 my $query;
80 if (! @urow) {
81 $query = "INSERT INTO codes " .
82 "( code_type, code, modifier, code_text ) VALUES " .
83 "( 2, '$code', '', '$desc' )";
84 ++$countnew;
86 else {
87 $query = "UPDATE codes SET code_text = '$desc' " .
88 "WHERE code_type = 2 AND code = '$code'";
89 ++$countup;
92 # Comment this out if you do not want to update the database here.
93 # You can save stdout to a file if you want to inspect it and then
94 # run it through the mysql utility.
96 $dbh->do($query) or die $query;
98 print $query . ";\n";
101 #######################################################################
102 # Shutdown #
103 #######################################################################
105 print "\nInserted $countnew rows, updated $countup codes.\n";
107 $dbh->disconnect;