Bug 24183: Add before_send_messages hook
[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 use Pod::Usage;
24
25 use Koha::Script;
26 use Koha::Patrons::Import;
27 my $Import = Koha::Patrons::Import->new();
28
29 my $csv_file;
30 my $matchpoint;
31 my $overwrite_cardnumber;
32 my $overwrite_passwords;
33 my %defaults;
34 my $ext_preserve = 0;
35 my $confirm;
36 my $verbose      = 0;
37 my $help;
38
39 GetOptions(
40     'c|confirm'                     => \$confirm,
41     'f|file=s'                      => \$csv_file,
42     'm|matchpoint=s'                => \$matchpoint,
43     'd|default=s'                   => \%defaults,
44     'o|overwrite'                   => \$overwrite_cardnumber,
45     'op|overwrite_passwords'        => \$overwrite_passwords,
46     'p|preserve-extended-attributes' => \$ext_preserve,
47     'v|verbose+'                    => \$verbose,
48     'h|help|?'                      => \$help,
49 ) or pod2usage(2);
50
51 pod2usage(1) if $help;
52 pod2usage(q|--file is required|) unless $csv_file;
53 pod2usage(q|--matchpoint is required|) unless $matchpoint;
54
55 warn "Running in dry-run mode, provide --confirm to apply the changes\n" unless $confirm;
56
57 my $handle;
58 open( $handle, "<", $csv_file ) or die $!;
59
60 my $return = $Import->import_patrons(
61     {
62         file                         => $handle,
63         defaults                     => \%defaults,
64         matchpoint                   => $matchpoint,
65         overwrite_cardnumber         => $overwrite_cardnumber,
66         overwrite_passwords          => $overwrite_passwords,
67         preserve_extended_attributes => $ext_preserve,
68     }
69 );
70
71 my $feedback    = $return->{feedback};
72 my $errors      = $return->{errors};
73 my $imported    = $return->{imported};
74 my $overwritten = $return->{overwritten};
75 my $alreadyindb = $return->{already_in_db};
76 my $invalid     = $return->{invalid};
77
78 if ($verbose) {
79     my $total = $imported + $alreadyindb + $invalid + $overwritten;
80     say q{};
81     say "Import complete:";
82     say "Imported:    $imported";
83     say "Overwritten: $overwritten";
84     say "Skipped:     $alreadyindb";
85     say "Invalid:     $invalid";
86     say "Total:       $total";
87     say q{};
88 }
89
90 if ($verbose > 1 ) {
91     say "Errors:";
92     say Data::Dumper::Dumper( $errors );
93 }
94
95 if ($verbose > 2 ) {
96     say "Feedback:";
97     say Data::Dumper::Dumper( $feedback );
98 }
99
100 =head1 NAME
101
102 import_patrons.pl - CLI script to import patrons data into Koha
103
104 =head1 SYNOPSIS
105
106 import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve-extended-attributes] [--verbose]
107
108 =head1 OPTIONS
109
110 =over 8
111
112 =item B<-h|--help>
113
114 Prints a brief help message and exits
115
116 =item B<-c|--confirm>
117
118 Confirms you really want to import these patrons, otherwise prints this help
119
120 =item B<-f|--file>
121
122 Path to the CSV file of patrons to import
123
124 =item B<-c|--matchpoint>
125
126 Field on which to match incoming patrons to existing patrons
127
128 =item B<-d|--default>
129
130 Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
131
132 =item B<-o|--overwrite>
133
134 Overwrite existing patrons with new data if a match is found
135
136 =item B<-p|--preserve-extended-attributes>
137
138 Retain extended patron attributes for existing patrons being overwritten
139
140 =item B<-v|--verbose>
141
142 Be verbose
143
144 Multiple -v options increase the verbosity
145
146 2 repetitions or above will report lines in error
147
148 3 repetitions or above will report feedback
149
150 =back
151
152 =cut