Prepping ubuntu package for 4.1.2 release
[openemr.git] / contrib / util / load_cpt_desc.plx
blob5e7680320acef2a4871a8c4fe336084830aec785
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 CPT codes into the "codes" table of
15 # OpenEMR. The codes must already be in the table
16 # (see load_doc_fees.plx).
18 # Run it like this:
20 # ./load_cpt_desc.plx < MEDU.txt
22 # See https://catalog.ama-assn.org/Catalog/ for purchasing and
23 # downloading CPT codes and descriptions. What you want is the
24 # "CPT ASCII Data Files Complete Set".
25 #######################################################################
27 #######################################################################
28 # Parameters that you may customize #
29 #######################################################################
31 my $DBNAME = "openemr"; # database name
33 # To load the short descriptions (SHORTU.txt, not currently used by
34 # OpenEMR but probably should), change this to "code_text_short":
36 my $TEXT_COL = "code_text";
38 # You can hard-code the database user name and password (see below),
39 # or else put them into the environment with bash commands like these
40 # before running this script:
42 # export DBI_USER=username
43 # export DBI_PASS=password
45 my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME") or die $DBI::errstr;
47 # my $dbh = DBI->connect("dbi:mysql:dbname=$DBNAME", "username", "password")
48 # or die $DBI::errstr;
50 #######################################################################
51 # Startup #
52 #######################################################################
54 $| = 1; # Turn on autoflushing of stdout.
56 my $countup = 0;
57 my $countnew = 0;
59 #######################################################################
60 # Main Loop #
61 #######################################################################
63 while (my $line = <STDIN>) {
64 next unless ($line =~ /^\d/);
66 my $code = substr($line, 0, 5);
67 my $desc = substr($line, 6);
68 $desc =~ s/\s*$//g; # remove all trailing whitespace
69 $desc =~ s/'/''/g; # just in case there are any quotes
71 my $usth = $dbh->prepare("SELECT id FROM codes " .
72 "WHERE code_type = 1 AND code = '$code'")
73 or die $dbh->errstr;
74 $usth->execute() or die $usth->errstr;
75 my @urow = $usth->fetchrow_array();
77 my $query;
78 if (! @urow) {
79 $query = "INSERT INTO codes " .
80 "( $TEXT_COL, code, code_type, modifier ) VALUES " .
81 "( '$desc', '$code', 1, '' )";
82 ++$countnew;
84 else {
85 $query = "UPDATE codes SET $TEXT_COL = '$desc' " .
86 "WHERE code_type = 1 AND code = '$code'";
87 ++$countup;
90 # Comment this out if you do not want to update the database here.
91 # You can save stdout to a file if you want to inspect it and then
92 # run it through the mysql utility.
94 $dbh->do($query) or die $query;
96 print $query . ";\n";
99 #######################################################################
100 # Shutdown #
101 #######################################################################
103 print "\nInserted $countnew rows, updated $countup codes.\n";
105 $dbh->disconnect;