warn on attempts to add duplicate item barcodes during batch import
[koha.git] / misc / commit_biblios_file.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 use C4::Context;
6 use C4::ImportBatch;
7 use Getopt::Long;
8
9 $| = 1;
10
11 # command-line parameters
12 my $batch_number = "";
13 my $list_batches = 0;
14 my $want_help = 0;
15
16 my $result = GetOptions(
17     'batch-number:s' => \$batch_number,
18     'list-batches'   => \$list_batches,
19     'h|help'         => \$want_help
20 );
21
22 if ($want_help or (not $batch_number and not $list_batches)) {
23     print_usage();
24     exit 0;
25 }
26
27 if ($list_batches) {
28     list_batches();
29     exit 0;
30 }
31
32 # FIXME dummy user so that logging won't fail
33 # in future, probably should tie to a real user account
34 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
35
36 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
37     my $batch = GetImportBatch($batch_number);
38     die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
39     die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
40         unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
41     process_batch($batch_number);
42 } else {
43     die "$0: please specify a numeric batch ID\n";
44 }
45
46 exit 0;
47
48 sub list_batches {
49     my $results = GetAllImportBatches();
50     print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
51     print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
52     foreach my $batch (@{ $results}) {
53         if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
54             print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
55                           $batch->{'import_batch_id'},
56                           $batch->{'file_name'},
57                           $batch->{'comments'},
58                           $batch->{'import_status'});
59         }
60     }
61 }
62
63 sub process_batch {
64     my ($import_batch_id) = @_;
65
66     print "... importing MARC records -- please wait\n";
67     my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = 
68         BatchCommitBibRecords($import_batch_id, 100, \&print_progress);
69     print "... finished importing MARC records\n";
70
71     print <<_SUMMARY_;
72
73 MARC record import report
74 ----------------------------------------
75 Batch number:                    $import_batch_id
76 Number of new bibs added:        $num_added
77 Number of bibs replaced:         $num_updated
78 Number of bibs ignored:          $num_ignored
79 Number of items added:           $num_items_added
80 Number of items ignored:         $num_items_errored
81
82 Note: an item is ignored if its barcode is a 
83 duplicate of one already in the database.
84 _SUMMARY_
85 }
86
87 sub print_progress {
88     my $recs = shift;
89     print "... processed $recs records\n";
90 }
91
92 sub print_usage {
93     print <<_USAGE_;
94 $0: import a batch of staged MARC records into database.
95
96 Use this batch job to complete the import of a batch of
97 MARC records that was staged either by the batch job
98 stage_biblios_file.pl or by the Koha Tools option
99 "Stage MARC Records for Import".
100
101 Parameters:
102     --batch-number <#>   number of the record batch
103                          to import
104     --list-batches       print a list of record batches
105                          available to commit
106     --help or -h            show this message.
107 _USAGE_
108 }