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