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