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