3 # Parts copyright 2014 ByWater Solutions
5 # This file is part of Koha.
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.
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.
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>.
22 use Getopt::Long qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
26 use Koha::Patrons::Import;
27 use C4::Log qw( cronlogaction );
28 my $Import = Koha::Patrons::Import->new();
32 my $overwrite_cardnumber;
33 my $overwrite_passwords;
41 my $update_dateexpiry;
42 my $update_dateexpiry_from_today;
44 my $command_line_options = join( " ", @ARGV );
47 'c|confirm' => \$confirm,
48 'f|file=s' => \$csv_file,
49 'm|matchpoint=s' => \$matchpoint,
50 'd|default=s' => \%defaults,
51 'o|overwrite' => \$overwrite_cardnumber,
52 'op|overwrite_passwords' => \$overwrite_passwords,
53 'ue|update-expiration' => \$update_dateexpiry,
54 'et|expiration-from-today' => \$update_dateexpiry_from_today,
55 'en|email-new' => \$welcome_new,
56 'p|preserve-extended-attributes' => \$ext_preserve,
57 'pf|preserve-field=s' => \@preserve_fields,
58 'v|verbose+' => \$verbose,
62 pod2usage(1) if $help;
63 pod2usage(q|--file is required|) unless $csv_file;
64 pod2usage(q|--matchpoint is required|) unless $matchpoint;
67 cronlogaction( { action => 'Run', info => $command_line_options } );
69 warn "Running in dry-run mode, provide --confirm to apply the changes\n";
73 open( $handle, "<", $csv_file ) or die $!;
75 my $return = $Import->import_patrons(
78 defaults => \%defaults,
79 matchpoint => $matchpoint,
80 overwrite_cardnumber => $overwrite_cardnumber,
81 overwrite_passwords => $overwrite_passwords,
82 preserve_extended_attributes => $ext_preserve,
83 preserve_fields => \@preserve_fields,
84 update_dateexpiry => $update_dateexpiry,
85 update_dateexpiry_from_today => $update_dateexpiry_from_today,
86 send_welcome => $welcome_new,
91 my $feedback = $return->{feedback};
92 my $errors = $return->{errors};
93 my $imported = $return->{imported};
94 my $overwritten = $return->{overwritten};
95 my $alreadyindb = $return->{already_in_db};
96 my $invalid = $return->{invalid};
97 my $total = $imported + $alreadyindb + $invalid + $overwritten;
101 say "Import complete:";
102 say "Imported: $imported";
103 say "Overwritten: $overwritten";
104 say "Skipped: $alreadyindb";
105 say "Invalid: $invalid";
112 say Data::Dumper::Dumper( $errors );
117 say Data::Dumper::Dumper( $feedback );
134 cronlogaction( { action => 'End', info => $info } );
139 import_patrons.pl - CLI script to import patrons data into Koha
143 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]
151 Prints a brief help message and exits
153 =item B<-c|--confirm>
155 Confirms you really want to import these patrons, otherwise prints this help
159 Path to the CSV file of patrons to import
161 =item B<-m|--matchpoint>
163 Field on which to match incoming patrons to existing patrons
165 =item B<-d|--default>
167 Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
169 =item B<-k|--preserve-field>
171 Prevent specified patron fields for existing patrons from being overwritten
173 =item B<-o|--overwrite>
175 Overwrite existing patrons with new data if a match is found
177 =item B<-p|--preserve-extended-attributes>
179 Retain extended patron attributes for existing patrons being overwritten
181 =item B<-en|--email-new>
183 Send the WELCOME notice email to new users
185 =item B<-ue|--update-expiration>
187 If a matching patron is found, extend the expiration date of their account using the patron's enrollment date as the base
189 =item B<-et|--expiration-from-today>
191 If a matching patron is found, extend the expiration date of their account using today's date as the base
193 =item B<-v|--verbose>
197 Multiple -v options increase the verbosity
199 2 repetitions or above will report lines in error
201 3 repetitions or above will report feedback