Bug 36018: Prevent random failure from api/v1/acquisitions_orders.t
[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 qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
24
25 use Koha::Script;
26 use Koha::Patrons::Import;
27 use C4::Log qw( cronlogaction );
28 my $Import = Koha::Patrons::Import->new();
29
30 my $csv_file;
31 my $matchpoint;
32 my $overwrite_cardnumber;
33 my $overwrite_passwords;
34 my $welcome_new = 0;
35 my %defaults;
36 my $ext_preserve = 0;
37 my $confirm;
38 my $verbose = 0;
39 my $help;
40 my @preserve_fields;
41 my $update_dateexpiry;
42 my $update_dateexpiry_from_today;
43 my $update_dateexpiry_from_existing;
44
45 my $command_line_options = join( " ", @ARGV );
46
47 GetOptions(
48     'c|confirm'                      => \$confirm,
49     'f|file=s'                       => \$csv_file,
50     'm|matchpoint=s'                 => \$matchpoint,
51     'd|default=s'                    => \%defaults,
52     'o|overwrite'                    => \$overwrite_cardnumber,
53     'op|overwrite_passwords'         => \$overwrite_passwords,
54     'ue|update-expiration'           => \$update_dateexpiry,
55     'et|expiration-from-today'       => \$update_dateexpiry_from_today,
56     'ee|expiration-from-existing'    => \$update_dateexpiry_from_existing,
57     'en|email-new'                   => \$welcome_new,
58     'p|preserve-extended-attributes' => \$ext_preserve,
59     'pf|preserve-field=s'            => \@preserve_fields,
60     'v|verbose+'                     => \$verbose,
61     'h|help|?'                       => \$help,
62 ) or pod2usage(2);
63
64 pod2usage(1)                                       if $help;
65 pod2usage(q|--ee and --et are mutually exclusive|) if $update_dateexpiry_from_today && $update_dateexpiry_from_existing;
66 pod2usage(q|--file is required|)       unless $csv_file;
67 pod2usage(q|--matchpoint is required|) unless $matchpoint;
68
69 if ($confirm) {
70     cronlogaction( { action => 'Run', info => $command_line_options } );
71 } else {
72     warn "Running in dry-run mode, provide --confirm to apply the changes\n";
73 }
74
75 my $handle;
76 open( $handle, "<", $csv_file ) or die $!;
77
78 my $return = $Import->import_patrons(
79     {
80         file                            => $handle,
81         defaults                        => \%defaults,
82         matchpoint                      => $matchpoint,
83         overwrite_cardnumber            => $overwrite_cardnumber,
84         overwrite_passwords             => $overwrite_passwords,
85         preserve_extended_attributes    => $ext_preserve,
86         preserve_fields                 => \@preserve_fields,
87         update_dateexpiry               => $update_dateexpiry,
88         update_dateexpiry_from_today    => $update_dateexpiry_from_today,
89         update_dateexpiry_from_existing => $update_dateexpiry_from_existing,
90         send_welcome                    => $welcome_new,
91         dry_run                         => !$confirm,
92     }
93 );
94
95 my $feedback    = $return->{feedback};
96 my $errors      = $return->{errors};
97 my $imported    = $return->{imported};
98 my $overwritten = $return->{overwritten};
99 my $alreadyindb = $return->{already_in_db};
100 my $invalid     = $return->{invalid};
101 my $total       = $imported + $alreadyindb + $invalid + $overwritten;
102
103 if ($verbose) {
104     say q{};
105     say "Import complete:";
106     say "Imported:    $imported";
107     say "Overwritten: $overwritten";
108     say "Skipped:     $alreadyindb";
109     say "Invalid:     $invalid";
110     say "Total:       $total";
111     say q{};
112 }
113
114 if ( $verbose > 1 ) {
115     say "Errors:";
116     say Data::Dumper::Dumper($errors);
117 }
118
119 if ( $verbose > 2 ) {
120     say "Feedback:";
121     say Data::Dumper::Dumper($feedback);
122 }
123
124 my $info =
125       "Import complete. "
126     . "Imported: "
127     . $imported
128     . " Overwritten: "
129     . $overwritten
130     . " Skipped: "
131     . $alreadyindb
132     . " Invalid: "
133     . $invalid
134     . " Total: "
135     . $total;
136
137 if ($confirm) {
138     cronlogaction( { action => 'End', info => $info } );
139 }
140
141 =head1 NAME
142
143 import_patrons.pl - CLI script to import patrons data into Koha
144
145 =head1 SYNOPSIS
146
147 import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve-field <column>] [--preserve-extended-attributes] [--update-expiration] [--expiration-from-today] [--verbose]
148
149 =head1 OPTIONS
150
151 =over 8
152
153 =item B<-h|--help>
154
155 Prints a brief help message and exits
156
157 =item B<-c|--confirm>
158
159 Confirms you really want to import these patrons, otherwise prints this help
160
161 =item B<-f|--file>
162
163 Path to the CSV file of patrons to import
164
165 =item B<-m|--matchpoint>
166
167 Field on which to match incoming patrons to existing patrons
168
169 =item B<-d|--default>
170
171 Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
172
173 =item B<-k|--preserve-field>
174
175 Prevent specified patron fields for existing patrons from being overwritten
176
177 =item B<-o|--overwrite>
178
179 Overwrite existing patrons with new data if a match is found
180
181 =item B<-p|--preserve-extended-attributes>
182
183 Retain extended patron attributes for existing patrons being overwritten
184
185 =item B<-en|--email-new>
186
187 Send the WELCOME notice email to new users
188
189 =item B<-ue|--update-expiration>
190
191 If a matching patron is found, extend the expiration date of their account using the patron's enrollment date as the base
192
193 =item B<-et|--expiration-from-today>
194
195 If a matching patron is found, extend the expiration date of their account using today's date as the base
196 Cannot by used in conjunction with --expiration-from-existing
197
198 =item B<-ee|--expiration-from-existing>
199
200 If a matching patron is found, extend the expiration date of their account using the patron's current expiration date as the base
201 Cannot by used in conjunction with --expiration-from-today
202
203 =item B<-v|--verbose>
204
205 Be verbose
206
207 Multiple -v options increase the verbosity
208
209 2 repetitions or above will report lines in error
210
211 3 repetitions or above will report feedback
212
213 =back
214
215 =cut