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