]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/import_holdings.pl
syncing cvs with the real world
[Evergreen.git] / Evergreen / src / extras / import / import_holdings.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use XML::LibXML;
4 use Time::HiRes qw/time/;
5 use Getopt::Long;
6 use Data::Dumper;
7 use Error qw/:try/;
8 use open qw/:utf8/;
9
10 $|=1;
11
12 my ($userid, $sourceid, $cn_id, $cp_id, $cp_file, $cn_file, $map_file, $lib_map_file) =
13         (1, 2, 1, 1, 'asset_copy.sql','asset_volume.sql','record_id_map.pl','lib-map.pl');
14
15 GetOptions (    
16         "sourceid"              => \$sourceid,
17         "copy_file=s"           => \$cp_file,
18         "volume_file=s"         => \$cn_file,
19         "tcn_map_file=s"        => \$map_file,
20         "lib_map_file=s"        => \$lib_map_file,
21         "userid=i"              => \$userid,
22         "first_volume=i"        => \$cn_id,
23         "first_copy=i"          => \$cp_id,
24 );
25
26 my $tcn_map;
27 my $lib_map;
28
29 eval `cat $map_file`;
30 eval `cat $lib_map_file`;
31
32 open CP, ">$cp_file" or die "Can't open $cp_file!  $!\n";
33 open CN, ">$cn_file" or die "Can't open $cn_file!  $!\n";
34
35 my %status_map = (
36         ''              => 0,
37         CHECKEDOUT      => 1,
38         BINDERY         => 2,
39         LOST            => 3,
40         MISSING         => 4,
41         INPROCESS       => 5,
42         INTRANSIT       => 6,
43         RESHELVING      => 7,
44         'ON HOLDS SHELF' => 8,
45         'ON-ORDER'      => 9,
46         ILL             => 10,
47         CATALOGING      => 11,
48         RESERVES        => 12,
49         DISCARD         => 13,
50 );
51
52
53 print CP <<SQL;
54 SET CLIENT_ENCODING TO 'UNICODE';
55 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;
56 SQL
57
58 print CN <<SQL;
59 SET CLIENT_ENCODING TO 'UNICODE';
60 COPY asset.call_number (id,editor,creator,record,label,owning_lib) FROM STDIN;
61 SQL
62
63 my $xact_id = time;
64
65 my $parser = XML::LibXML->new;
66
67 my $cn_map;
68
69 my $xml = '';
70 while ( $xml .= <STDIN> ) {
71         chomp $xml;
72         next unless $xml;
73
74         my $tcn;
75         my $doc;
76         my $success = 0;
77         try {
78                 $doc = $parser->parse_string($xml);;
79                 $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"][1]' );
80                 $success = 1;
81         } catch Error with {
82                 my $e = shift;
83                 warn $e;
84                 warn $xml;
85         };      
86         next unless $success;
87
88         $tcn =~ s/^.*?(\w+)\s*$/$1/go;
89         
90         unless ($tcn) {
91                 warn "\nNo TCN found in rec!!\n";
92                 $xml = '';
93                 next;
94         }
95
96         unless (exists($$tcn_map{$tcn})) {
97                 warn "\n !! TCN $tcn not in the map!\n";
98                 $xml = '';
99                 next;
100         }
101
102         my $rec_id = $$tcn_map{$tcn};
103
104         for my $node ($doc->documentElement->findnodes('/*/*[@tag="999"]')) {
105                 my $barcode = $node->findvalue( '*[@code="i"]' );
106                 my $label = $node->findvalue( '*[@code="a"]' );
107                 my $owning_lib = $$lib_map{ $node->findvalue( '*[@code="m"]' ) };
108                 my $price = $node->findvalue( '*[@code="p"]' );
109                 my $copy_number = $node->findvalue( '*[@code="c"]' );
110                 my $available = $node->findvalue( '*[@code="k"]' ) || '';
111
112                 my $status = $status_map{$available} || 0;
113
114                 next unless $barcode;
115                 next unless $owning_lib;
116                 next unless $label;
117
118                 $barcode =~ s/\\/\\\\/og;
119                 $label =~ s/\\/\\\\/og;
120                 $price =~ s/\$//og;
121                 if ($price !~ /^\s*\d{1,6}\.\d{2}\s*$/o) {
122                         $price = '0.00';
123                 }
124
125                 unless (exists($$cn_map{"$rec_id/$owning_lib/$label"})) {
126                         $$cn_map{"$rec_id/$owning_lib/$label"} = $cn_id;
127                         print CN join("\t",($cn_id,$userid,$userid,$rec_id,$label,$owning_lib))."\n";
128                         print 'v';
129                         $cn_id++;
130                 }
131
132 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
133
134                 print CP join("\t", (   $cp_id,$owning_lib,$userid,$userid,$barcode,
135                                         $$cn_map{"$rec_id/$owning_lib/$label"},
136                                         $copy_number,$status,2,2,1,0,'0.00',
137                                         $price,0,1 )
138                          )."\n";
139                 print 'c';
140                 $cp_id++;
141         }
142         $xml = '';
143 }
144
145 print CN "\\.\n";
146 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
147 print CP "\\.\n";
148 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";
149