Bug 12598: New misc/import_borrowers.pl command line tool
[koha.git] / misc / import_patrons.pl
1 #!/usr/bin/perl
2
3 # Parts copyright 2014 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Getopt::Long;
23
24 use Koha::Patrons::Import;
25 my $Import = Koha::Patrons::Import->new();
26
27 my $csv_file;
28 my $matchpoint;
29 my $overwrite_cardnumber;
30 my %defaults;
31 my $ext_preserve = 0;
32 my $confirm;
33 my $verbose      = 0;
34 my $help;
35
36 GetOptions(
37     'c|confirm'                     => \$confirm,
38     'f|file=s'                      => \$csv_file,
39     'm|matchpoint=s'                => \$matchpoint,
40     'd|default=s'                   => \%defaults,
41     'o|overwrite'                   => \$overwrite_cardnumber,
42     'p|preserve-extended-atributes' => \$ext_preserve,
43     'v|verbose+'                    => \$verbose,
44     'h|help|?'                      => \$help,
45 );
46
47 print_help() if ( $help || !$csv_file || !$matchpoint || !$confirm );
48
49 my $handle;
50 open( $handle, "<", $csv_file ) or die $!;
51
52 my $return = $Import->import_patrons(
53     {
54         file                         => $handle,
55         defaults                     => \%defaults,
56         matchpoint                   => $matchpoint,
57         overwrite_cardnumber         => $overwrite_cardnumber,
58         preserve_extended_attributes => $ext_preserve,
59     }
60 );
61
62 my $feedback    = $return->{feedback};
63 my $errors      = $return->{errors};
64 my $imported    = $return->{imported};
65 my $overwritten = $return->{overwritten};
66 my $alreadyindb = $return->{already_in_db};
67 my $invalid     = $return->{invalid};
68
69 if ($verbose) {
70     my $total = $imported + $alreadyindb + $invalid + $overwritten;
71     say q{};
72     say "Import complete:";
73     say "Imported:    $imported";
74     say "Overwritten: $overwritten";
75     say "Skipped:     $alreadyindb";
76     say "Invalid:     $invalid";
77     say "Total:       $total";
78     say q{};
79 }
80
81 if ($verbose > 1 ) {
82     say "Errors:";
83     say Data::Dumper::Dumper( $errors );
84 }
85
86 if ($verbose > 2 ) {
87     say "Feedback:";
88     say Data::Dumper::Dumper( $feedback );
89 }
90
91 sub print_help {
92     print <<_USAGE_;
93 import_patrons.pl -c /path/to/patrons.csv -m cardnumber
94     -c --confirm                        Confirms you really want to import these patrons, otherwise prints this help
95     -f --file                           Path to the CSV file of patrons to import
96     -m --matchpoint                     Field on which to match incoming patrons to existing patrons
97     -d --default                        Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
98     -p --preserve-extended-atributes    Retain extended patron attributes for existing patrons being overwritten
99     -o --overwrite                      Overwrite existing patrons with new data if a match is found
100     -v --verbose                        Be verbose
101 _USAGE_
102     exit;
103 }