3 use UNIVERSAL::require;
7 use Time::HiRes qw/time/;
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 #-------------------------------------------------------------------------------
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');
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');
48 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
49 ('Pg','localhost','evergreen','postgres','postgres');
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,
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,
79 if ($marctype eq 'XML') {
86 MARC::Charset->ignore_errors(1);
87 MARC::Charset->assume_encoding($enc);
90 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
91 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
93 my $t = 'actor_org_unit';
94 if ($db_driver eq 'Pg') {
95 $t = 'actor.org_unit';
97 my $sth = $dbh->prepare("SELECT $lib_map_field,id FROM $t");
101 while (my $lib = $sth->fetchrow_arrayref) {
102 $$lib_map{$$lib[0]} = $$lib[1];
105 my $tcn_sth = $dbh->prepare("SELECT id FROM biblio.record_entry WHERE tcn_value = ?");
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";
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;
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;
123 my $batch = MARC::Batch->new( $marctype => $marc_file );
124 $batch->strict_off();
125 $batch->warnings_off();
130 while ( try { $record = $batch->next } otherwise { $record = -1 } ) {
131 next if ($record == -1);
133 next if ($count <= $skip);
135 $rec_id = $record->subfield( $id_field => $id_subfield );
137 next unless ($rec_id);
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 ) || '';
147 my $status = $status_map{$available} || 0;
149 next unless $barcode;
150 next unless $owning_lib;
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) {
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";
167 # id,editor,creator,barcode,call_number,copy_number,available,loan_duration,fine_level,circulate,deposit,deposit_amount,price,ref,opac_visible
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',
180 print CN "SELECT setval('asset.call_number_id_seq'::TEXT, $cn_id);\n";
182 print CP "SELECT setval('asset.copy_id_seq'::TEXT, $cp_id);\n";