]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/src/extras/import/import_holdings.pl
fixed "price" checking
[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
36 print CP <<SQL;
37 SET CLIENT_ENCODING TO 'UNICODE';
38 COPY asset.copy (id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible) FROM STDIN;
39 SQL
40
41 print CN <<SQL;
42 SET CLIENT_ENCODING TO 'UNICODE';
43 COPY asset.call_number (id,editor,creator,record,label,owning_lib) FROM STDIN;
44 SQL
45
46 my $xact_id = time;
47
48 my $parser = XML::LibXML->new;
49
50 my $cn_map;
51
52 my $xml = '';
53 while ( $xml .= <STDIN> ) {
54         chomp $xml;
55         next unless $xml;
56
57         my $tcn;
58         my $doc;
59         my $success = 0;
60         try {
61                 $doc = $parser->parse_string($xml);;
62                 $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"][1]' );
63                 $success = 1;
64         } catch Error with {
65                 my $e = shift;
66                 warn $e;
67                 warn $xml;
68         };      
69         next unless $success;
70
71         $tcn =~ s/^.*?(\w+)\s*$/$1/go;
72         
73         unless ($tcn) {
74                 warn "\nNo TCN found in rec!!\n";
75                 $xml = '';
76                 next;
77         }
78
79         unless (exists($$tcn_map{$tcn})) {
80                 warn "\n !! TCN $tcn not in the map!\n";
81                 $xml = '';
82                 next;
83         }
84
85         my $rec_id = $$tcn_map{$tcn};
86
87         for my $node ($doc->documentElement->findnodes('/*/*[@tag="999"]')) {
88                 my $barcode = $node->findvalue( '*[@code="i"]' );
89                 my $label = $node->findvalue( '*[@code="a"]' );
90                 my $owning_lib = $$lib_map{ $node->findvalue( '*[@code="m"]' ) };
91                 my $price = $node->findvalue( '*[@code="p"]' );
92                 my $copy_number = $node->findvalue( '*[@code="c"]' );
93                 my $available = $node->findvalue( '*[@code="k"]' ) ? 1 : 0;
94
95                 next unless $barcode;
96                 next unless $owning_lib;
97                 next unless $label;
98
99                 $barcode =~ s/\\/\\\\/og;
100                 $label =~ s/\\/\\\\/og;
101                 $price =~ s/\$//og;
102                 if ($price !~ /^\s*\d{1,6}\.\d{2}\s*$/o) {
103                         $price = '0.00';
104                 }
105
106                 unless (exists($$cn_map{"$rec_id/$owning_lib/$label"})) {
107                         $$cn_map{"$rec_id/$owning_lib/$label"} = $cn_id;
108                         print CN join("\t",($cn_id,$userid,$userid,$rec_id,$label,$owning_lib))."\n";
109                         print 'v';
110                         $cn_id++;
111                 }
112
113 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
114
115                 print CP join("\t", (   $cp_id,$userid,$userid,$barcode,
116                                         $$cn_map{"$rec_id/$owning_lib/$label"},
117                                         $copy_number,$available,2,2,1,0,'0.00',
118                                         $price,0,1 )
119                          )."\n";
120                 print 'c';
121                 $cp_id++;
122         }
123         $xml = '';
124 }
125
126 print CN "\\.\n";
127 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
128 print CP "\\.\n";
129 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";
130