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