Changed query that gets the date of the last encounter for date calculations. It...
[openemr.git] / contrib / util / load_hcpcs_desc.plx
blob2fbaefec359077e753aaaa79c577260da02151b9
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 HCPCS codes into the "codes" table of
15 # OpenEMR. Both the long and short descriptions are loaded from the
16 # same input file.
18 # For 2005, run it like this:
20 # ./load_hcpcs_desc.plx < 05anweb.txt
22 # To get this input file, download and extract anhcpc05.zip from
23 # http://www.cms.hhs.gov/providers/pufdownload/.
24 #######################################################################
26 #######################################################################
27 # Parameters that you may customize #
28 #######################################################################
30 my $DBNAME = "openemr"; # database name
32 # To load the short descriptions (SHORTU.txt, not currently used by
33 # OpenEMR but probably should), change this to "code_text_short":
35 my $TEXT_COL = "code_text";
37 # You can hard-code the database user name and password (see below),
38 # or else put them into the environment with bash commands like these
39 # before running this script:
41 # export DBI_USER=username
42 # export DBI_PASS=password
44 my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME") or die $DBI::errstr;
46 # my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME", "username", "password")
47 # or die $DBI::errstr;
49 #######################################################################
50 # Startup #
51 #######################################################################
53 my $currcode = "";
54 my $currshort = "";
55 my $currlong = "";
56 my $countup = 0;
57 my $countnew = 0;
59 $| = 1; # Turn on autoflushing of stdout.
61 #######################################################################
62 # Subroutines #
63 #######################################################################
65 sub writeCurrent() {
66 return unless $currcode;
68 $currlong =~ s/ / /g;
69 $currlong =~ s/'/''/g;
70 $currshort =~ s/'/''/g;
72 my $usth = $dbh->prepare("SELECT id FROM codes " .
73 "WHERE code_type = 3 AND code = '$currcode'")
74 or die $dbh->errstr;
75 $usth->execute() or die $usth->errstr;
76 my @urow = $usth->fetchrow_array();
78 my $query;
79 if (! @urow) {
80 $query = "INSERT INTO codes " .
81 "( code_type, code, modifier, code_text_short, code_text ) VALUES " .
82 "( 3, '$currcode', '', '$currshort', '$currshort' )";
83 ++$countnew;
85 else {
86 $query = "UPDATE codes SET code_text_short = '$currshort', code_text = '$currshort' " .
87 "WHERE code_type = 3 AND code = '$currcode'";
88 ++$countup;
91 # Comment this out if you do not want to update the database here.
92 # You can save stdout to a file if you want to inspect it and then
93 # run it through the mysql utility.
95 $dbh->do($query) or die $query;
97 print $query . ";\n";
100 #######################################################################
101 # Main Loop #
102 #######################################################################
104 while (my $line = <STDIN>) {
105 my $rectype = substr($line, 10, 1);
106 next unless ($rectype eq '3' or $rectype eq '4');
108 if ($rectype eq '3') {
109 &writeCurrent();
110 $currcode = substr($line, 0, 5);
111 $currlong = "";
112 $currshort = substr($line, 91, 28);
113 $currshort =~ s/\s*$//g; # remove all trailing whitespace
116 $currlong .= substr($line, 11, 80);
117 $currlong =~ s/\s*$//g;
120 &writeCurrent();
122 #######################################################################
123 # Shutdown #
124 #######################################################################
126 print "\nInserted $countnew rows, updated $countup codes.\n";
128 $dbh->disconnect;