]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/import_holdings.pl
adding subfield setting for idfield
[Evergreen.git] / Evergreen / src / extras / import / import_holdings.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use UNIVERSAL::require;
4 use MARC::Charset;
5 use MARC::Batch;
6 use MARC::File::XML;
7 use Time::HiRes qw/time/;
8 use Getopt::Long;
9 use Data::Dumper;
10 use Error qw/:try/;
11 use DBI;
12 use open qw/:utf8/;
13
14 #-------------------------------------------------------------------------------
15 #  The keys of this hash should be the string values stored in your legacy
16 #  system that map to the copy statuses in Open-ILS.  If you don't see a
17 #  legacy status here that you need to carry over to your new Open-ILS install
18 #  you can use the "Copy Statuses" bootstrapping CGI to create an entry for it.
19 #  Then simply a key for the legacy status that points to the SysID of the new
20 #  Open-ILS Copy Status.
21 #-------------------------------------------------------------------------------
22 my %status_map = (
23         ''              => 0,
24         CHECKEDOUT      => 1,
25         BINDERY         => 2,
26         LOST            => 3,
27         MISSING         => 4,
28         INPROCESS       => 5,
29         INTRANSIT       => 6,
30         RESHELVING      => 7,
31         'ON HOLDS SHELF'=> 8,
32         'ON-ORDER'      => 9,
33         ILL             => 10,
34         CATALOGING      => 11,
35         RESERVES        => 12,
36         DISCARD         => 13,
37 );
38
39
40 $|=1;
41
42 my ($userid,$cn_id,$cp_id,$cp_file,$cn_file,$lib_map_field,$id_tag,$id_field,$id_subfield, $marc_file) =
43         (1, 1, 1, 'asset_copy.sql','asset_volume.sql','shortname','001');
44
45 my ($skip,$enc,$marctype,$holding_tag,$bc,$lbl,$own,$pr,$cpn,$avail) =
46         (0,'utf-8','XML','999','i','a','m','p','c','k');
47
48 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
49         ('Pg','localhost','evergreen','postgres','postgres');
50
51 GetOptions (    
52         "encoding=s"            => \$enc,
53         "copy_file=s"           => \$cp_file,
54         "volume_file=s"         => \$cn_file,
55         "userid=i"              => \$userid,
56         "first_volume=i"        => \$cn_id,
57         "first_copy=i"          => \$cp_id,
58         "db_driver=s"           => \$db_driver,
59         "db_host=s"             => \$db_host,
60         "db_name=s"             => \$db_name,
61         "db_user=s"             => \$db_user,
62         "db_pw=s"               => \$db_pw,
63         "lib_map_field=s"       => \$lib_map_field,
64         "id_field=s"            => \$id_field,
65         "id_subfield=s"         => \$id_subfield,
66         "holding_field=s"       => \$holding_tag,
67         "item_barcode=s"        => \$bc,
68         "item_call_number=s"    => \$lbl,
69         "item_owning_lib=s"     => \$own,
70         "item_price=s"          => \$pr,
71         "item_copy_number=s"    => \$cpn,
72         "item_copy_status=s"    => \$avail,
73         "marc_file=s"           => \$marc_file,
74         "marctype=s"            => \$marctype,
75         "skip=i"                => \$skip,
76
77 );
78
79 if ($marctype eq 'XML') {
80         'open'->use(':utf8');
81 } else {
82         bytes->use();
83 }
84
85 if ($enc) {
86         MARC::Charset->ignore_errors(1);
87         MARC::Charset->assume_encoding($enc);
88 }
89
90 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
91 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
92
93 my $t = 'actor_org_unit';
94 if ($db_driver eq 'Pg') {
95         $t = 'actor.org_unit';
96 }
97 my $sth = $dbh->prepare("SELECT $lib_map_field,id FROM $t");
98 $sth->execute;
99
100 my $lib_map = {};
101 while (my $lib = $sth->fetchrow_arrayref) {
102         $$lib_map{$$lib[0]} = $$lib[1];
103 }
104
105 my $tcn_sth = $dbh->prepare("SELECT id FROM biblio.record_entry WHERE tcn_value = ?");
106 my $rec_id;
107
108 open CP, ">>$cp_file" or die "Can't open $cp_file!  $!\n";
109 open CN, ">>$cn_file" or die "Can't open $cn_file!  $!\n";
110
111 print CP <<SQL if (!$skip);
112 SET CLIENT_ENCODING TO 'UNICODE';
113 COPY asset.copy (id,circ_lib,editor,creator,barcode,call_number,copy_number,status,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible) FROM STDIN;
114 SQL
115
116 print CN <<SQL if (!$skip);
117 SET CLIENT_ENCODING TO 'UNICODE';
118 COPY asset.call_number (id,editor,creator,record,label,owning_lib) FROM STDIN;
119 SQL
120
121 my $xact_id = time;
122
123 my $batch = MARC::Batch->new( $marctype => $marc_file );
124 $batch->strict_off();
125 $batch->warnings_off();
126
127 my $cn_map;
128 my $count = 0;
129 my $record;
130 while ( try { $record = $batch->next } otherwise { $record = -1 } ) {
131         next if ($record == -1);
132         $count++;
133         next if ($count <= $skip);
134
135         $rec_id = $record->subfield( $id_field => $id_subfield );
136
137         next unless ($rec_id);
138
139         for my $field ($record->field($holding_tag)) {
140                 my $barcode = $field->subfield( $bc );
141                 my $label = $field->subfield( $lbl );
142                 my $owning_lib = $$lib_map{ $field->subfield( $own ) };
143                 my $price = $field->subfield( $pr );
144                 my $copy_number = $field->subfield( $cpn ) || '\N';
145                 my $available = $field->subfield( $avail ) || '';
146
147                 my $status = $status_map{$available} || 0;
148
149                 next unless $barcode;
150                 next unless $owning_lib;
151                 next unless $label;
152
153                 $barcode =~ s/\\/\\\\/og;
154                 $label =~ s/\\/\\\\/og;
155                 $price =~ s/\$//og if($price);
156                 if (!defined($price) || $price !~ /^\s*\d{1,6}\.\d{2}\s*$/o) {
157                         $price = '0.00';
158                 }
159
160                 unless (exists($$cn_map{"$rec_id/$owning_lib/$label"})) {
161                         $$cn_map{"$rec_id/$owning_lib/$label"} = $cn_id;
162                         print CN join("\t",($cn_id,$userid,$userid,$rec_id,$label,$owning_lib))."\n";
163                         print 'v';
164                         $cn_id++;
165                 }
166
167 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
168
169                 print CP join("\t", (   $cp_id,$owning_lib,$userid,$userid,$barcode,
170                                         $$cn_map{"$rec_id/$owning_lib/$label"},
171                                         $copy_number,$status,2,2,1,0,'0.00',
172                                         $price,0,1 )
173                          )."\n";
174                 print 'c';
175                 $cp_id++;
176         }
177 }
178
179 print CN "\\.\n";
180 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
181 print CP "\\.\n";
182 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";
183