Translation for 16.05.02
[koha.git] / tools / import_borrowers.pl
blob8c03274fcfd27618f3559d0b0e4aa06cfdc499a7
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 # Script to take some borrowers data in a known format and load it into Koha
23 # File format
25 # cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,
26 # address line , address line 2, city, zipcode, contry, email, phone, mobile, fax, work email, work phone,
27 # alternate streetnumber, alternate streettype, alternate address line 1, alternate city,
28 # alternate zipcode, alternate country, alternate email, alternate phone, date of birth, branchcode,
29 # categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname,
30 # contact firstname, contact title, borrower notes, contact relationship
31 # gender, username, opac note, contact note, password, sort one, sort two
33 # any fields except cardnumber can be blank but the number of fields must match
34 # dates should be in the format you have set up Koha to expect
35 # branchcode and categorycode need to be valid
37 use strict;
38 use warnings;
40 use C4::Auth;
41 use C4::Output;
42 use C4::Context;
43 use C4::Branch qw/GetBranchesLoop GetBranchName/;
44 use C4::Members;
45 use C4::Members::Attributes qw(:all);
46 use C4::Members::AttributeTypes;
47 use C4::Members::Messaging;
48 use C4::Reports::Guided;
49 use C4::Templates;
50 use Koha::Patron::Debarments;
51 use Koha::DateUtils;
53 use Text::CSV;
54 # Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals:
55 # ė
56 # č
58 use CGI qw ( -utf8 );
59 # use encoding 'utf8'; # don't do this
61 my (@errors, @feedback);
62 my $extended = C4::Context->preference('ExtendedPatronAttributes');
63 my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences');
64 my @columnkeys = C4::Members::columns();
65 @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } @columnkeys;
66 if ($extended) {
67 push @columnkeys, 'patron_attributes';
70 my $input = CGI->new();
71 our $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode
72 #push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; #XXX
74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
75 template_name => "tools/import_borrowers.tt",
76 query => $input,
77 type => "intranet",
78 authnotrequired => 0,
79 flagsrequired => { tools => 'import_patrons' },
80 debug => 1,
81 });
83 # get the branches and pass them to the template
84 my $branches = GetBranchesLoop();
85 $template->param( branches => $branches ) if ( $branches );
86 # get the patron categories and pass them to the template
87 my $categories = GetBorrowercategoryList();
88 $template->param( categories => $categories ) if ( $categories );
89 my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers};
90 $columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
91 $template->param( borrower_fields => $columns );
93 if ($input->param('sample')) {
94 print $input->header(
95 -type => 'application/vnd.sun.xml.calc', # 'application/vnd.ms-excel' ?
96 -attachment => 'patron_import.csv',
98 $csv->combine(@columnkeys);
99 print $csv->string, "\n";
100 exit 0;
102 my $uploadborrowers = $input->param('uploadborrowers');
103 my $matchpoint = $input->param('matchpoint');
104 if ($matchpoint) {
105 $matchpoint =~ s/^patron_attribute_//;
107 my $overwrite_cardnumber = $input->param('overwrite_cardnumber');
109 $template->param( SCRIPT_NAME => '/cgi-bin/koha/tools/import_borrowers.pl' );
111 if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
112 push @feedback, {feedback=>1, name=>'filename', value=>$uploadborrowers, filename=>$uploadborrowers};
113 my $handle = $input->upload('uploadborrowers');
114 my $uploadinfo = $input->uploadInfo($uploadborrowers);
115 foreach (keys %$uploadinfo) {
116 push @feedback, {feedback=>1, name=>$_, value=>$uploadinfo->{$_}, $_=>$uploadinfo->{$_}};
118 my $imported = 0;
119 my $alreadyindb = 0;
120 my $overwritten = 0;
121 my $invalid = 0;
122 my $matchpoint_attr_type;
123 my %defaults = $input->Vars;
125 # use header line to construct key to column map
126 my $borrowerline = <$handle>;
127 my $status = $csv->parse($borrowerline);
128 ($status) or push @errors, {badheader=>1,line=>$., lineraw=>$borrowerline};
129 my @csvcolumns = $csv->fields();
130 my %csvkeycol;
131 my $col = 0;
132 foreach my $keycol (@csvcolumns) {
133 # columnkeys don't contain whitespace, but some stupid tools add it
134 $keycol =~ s/ +//g;
135 $csvkeycol{$keycol} = $col++;
137 #warn($borrowerline);
138 my $ext_preserve = $input->param('ext_preserve') || 0;
139 if ($extended) {
140 $matchpoint_attr_type = C4::Members::AttributeTypes->fetch($matchpoint);
143 push @feedback, {feedback=>1, name=>'headerrow', value=>join(', ', @csvcolumns)};
144 my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
145 my @criticals = qw(surname branchcode categorycode); # there probably should be others
146 my @bad_dates; # I've had a few.
147 LINE: while ( my $borrowerline = <$handle> ) {
148 my %borrower;
149 my @missing_criticals;
150 my $patron_attributes;
151 my $status = $csv->parse($borrowerline);
152 my @columns = $csv->fields();
153 if (! $status) {
154 push @missing_criticals, {badparse=>1, line=>$., lineraw=>$borrowerline};
155 } elsif (@columns == @columnkeys) {
156 @borrower{@columnkeys} = @columns;
157 # MJR: try to fill blanks gracefully by using default values
158 foreach my $key (@columnkeys) {
159 if ($borrower{$key} !~ /\S/) {
160 $borrower{$key} = $defaults{$key};
163 } else {
164 # MJR: try to recover gracefully by using default values
165 foreach my $key (@columnkeys) {
166 if (defined($csvkeycol{$key}) and $columns[$csvkeycol{$key}] =~ /\S/) {
167 $borrower{$key} = $columns[$csvkeycol{$key}];
168 } elsif ( $defaults{$key} ) {
169 $borrower{$key} = $defaults{$key};
170 } elsif ( scalar grep {$key eq $_} @criticals ) {
171 # a critical field is undefined
172 push @missing_criticals, {key=>$key, line=>$., lineraw=>$borrowerline};
173 } else {
174 $borrower{$key} = '';
178 #warn join(':',%borrower);
179 if ($borrower{categorycode}) {
180 push @missing_criticals, {key=>'categorycode', line=>$. , lineraw=>$borrowerline, value=>$borrower{categorycode}, category_map=>1}
181 unless GetBorrowercategory($borrower{categorycode});
182 } else {
183 push @missing_criticals, {key=>'categorycode', line=>$. , lineraw=>$borrowerline};
185 if ($borrower{branchcode}) {
186 push @missing_criticals, {key=>'branchcode', line=>$. , lineraw=>$borrowerline, value=>$borrower{branchcode}, branch_map=>1}
187 unless GetBranchName($borrower{branchcode});
188 } else {
189 push @missing_criticals, {key=>'branchcode', line=>$. , lineraw=>$borrowerline};
191 if (@missing_criticals) {
192 foreach (@missing_criticals) {
193 $_->{borrowernumber} = $borrower{borrowernumber} || 'UNDEF';
194 $_->{surname} = $borrower{surname} || 'UNDEF';
196 $invalid++;
197 (25 > scalar @errors) and push @errors, {missing_criticals=>\@missing_criticals};
198 # The first 25 errors are enough. Keeping track of 30,000+ would destroy performance.
199 next LINE;
201 if ($extended) {
202 my $attr_str = $borrower{patron_attributes};
203 $attr_str =~ s/\xe2\x80\x9c/"/g; # fixup double quotes in case we are passed smart quotes
204 $attr_str =~ s/\xe2\x80\x9d/"/g;
205 push @feedback, {feedback=>1, name=>'attribute string', value=>$attr_str, filename=>$uploadborrowers};
206 delete $borrower{patron_attributes}; # not really a field in borrowers, so we don't want to pass it to ModMember.
207 $patron_attributes = extended_attributes_code_value_arrayref($attr_str);
209 # Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it.
210 foreach (qw(dateofbirth dateenrolled dateexpiry)) {
211 my $tempdate = $borrower{$_} or next;
212 $tempdate = eval { output_pref( { dt => dt_from_string( $tempdate ), dateonly => 1, dateformat => 'iso' } ); };
213 if ($tempdate) {
214 $borrower{$_} = $tempdate;
215 } else {
216 $borrower{$_} = '';
217 push @missing_criticals, {key=>$_, line=>$. , lineraw=>$borrowerline, bad_date=>1};
220 $borrower{dateenrolled} = $today_iso unless $borrower{dateenrolled};
221 $borrower{dateexpiry} = GetExpiryDate($borrower{categorycode},$borrower{dateenrolled}) unless $borrower{dateexpiry};
222 my $borrowernumber;
223 my $member;
224 if ( ($matchpoint eq 'cardnumber') && ($borrower{'cardnumber'}) ) {
225 $member = GetMember( 'cardnumber' => $borrower{'cardnumber'} );
226 if ($member) {
227 $borrowernumber = $member->{'borrowernumber'};
229 } elsif ( ($matchpoint eq 'userid') && ($borrower{'userid'}) ) {
230 $member = GetMember( 'userid' => $borrower{'userid'} );
231 if ($member) {
232 $borrowernumber = $member->{'borrowernumber'};
234 } elsif ($extended) {
235 if (defined($matchpoint_attr_type)) {
236 foreach my $attr (@$patron_attributes) {
237 if ($attr->{code} eq $matchpoint and $attr->{value} ne '') {
238 my @borrowernumbers = $matchpoint_attr_type->get_patrons($attr->{value});
239 $borrowernumber = $borrowernumbers[0] if scalar(@borrowernumbers) == 1;
240 last;
246 if ( C4::Members::checkcardnumber( $borrower{cardnumber}, $borrowernumber ) ) {
247 push @errors, {
248 invalid_cardnumber => 1,
249 borrowernumber => $borrowernumber,
250 cardnumber => $borrower{cardnumber}
252 $invalid++;
253 next;
256 if ($borrowernumber) {
257 # borrower exists
258 unless ($overwrite_cardnumber) {
259 $alreadyindb++;
260 $template->param('lastalreadyindb'=>$borrower{'surname'}.' / '.$borrowernumber);
261 next LINE;
263 $borrower{'borrowernumber'} = $borrowernumber;
264 for my $col (keys %borrower) {
265 # use values from extant patron unless our csv file includes this column or we provided a default.
266 # FIXME : You cannot update a field with a perl-evaluated false value using the defaults.
268 # The password is always encrypted, skip it!
269 next if $col eq 'password';
271 unless(exists($csvkeycol{$col}) || $defaults{$col}) {
272 $borrower{$col} = $member->{$col} if($member->{$col}) ;
276 # Check if the userid provided does not exist yet
277 if ( exists $borrower{userid}
278 and $borrower{userid}
279 and not Check_Userid( $borrower{userid}, $borrower{borrowernumber} ) ) {
280 push @errors, { duplicate_userid => 1, userid => $borrower{userid} };
281 $invalid++;
282 next LINE;
285 unless (ModMember(%borrower)) {
286 $invalid++;
287 # until we have better error trapping, we have no way of knowing why ModMember errored out...
288 push @errors, {unknown_error => 1};
289 $template->param('lastinvalid'=>$borrower{'surname'}.' / '.$borrowernumber);
290 next LINE;
293 # Don't add a new restriction if the existing 'combined' restriction matches this one
294 if ( $borrower{debarred} && ( ( $borrower{debarred} ne $member->{debarred} ) || ( $borrower{debarredcomment} ne $member->{debarredcomment} ) ) ) {
295 # Check to see if this debarment already exists
296 my $debarrments = GetDebarments(
298 borrowernumber => $borrowernumber,
299 expiration => $borrower{debarred},
300 comment => $borrower{debarredcomment}
303 # If it doesn't, then add it!
304 unless (@$debarrments) {
305 AddDebarment(
307 borrowernumber => $borrowernumber,
308 expiration => $borrower{debarred},
309 comment => $borrower{debarredcomment}
315 if ($extended) {
316 if ($ext_preserve) {
317 my $old_attributes = GetBorrowerAttributes($borrowernumber);
318 $patron_attributes = extended_attributes_merge($old_attributes, $patron_attributes); #TODO: expose repeatable options in template
320 push @errors, {unknown_error => 1} unless SetBorrowerAttributes($borrower{'borrowernumber'}, $patron_attributes, 'no_branch_limit' );
322 $overwritten++;
323 $template->param('lastoverwritten'=>$borrower{'surname'}.' / '.$borrowernumber);
324 } else {
325 # FIXME: fixup_cardnumber says to lock table, but the web interface doesn't so this doesn't either.
326 # At least this is closer to AddMember than in members/memberentry.pl
327 if (!$borrower{'cardnumber'}) {
328 $borrower{'cardnumber'} = fixup_cardnumber(undef);
330 if ($borrowernumber = AddMember(%borrower)) {
332 if ( $borrower{debarred} ) {
333 AddDebarment(
335 borrowernumber => $borrowernumber,
336 expiration => $borrower{debarred},
337 comment => $borrower{debarredcomment}
342 if ($extended) {
343 SetBorrowerAttributes($borrowernumber, $patron_attributes);
346 if ($set_messaging_prefs) {
347 C4::Members::Messaging::SetMessagingPreferencesFromDefaults({ borrowernumber => $borrowernumber,
348 categorycode => $borrower{categorycode} });
351 $imported++;
352 $template->param('lastimported'=>$borrower{'surname'}.' / '.$borrowernumber);
353 } else {
354 $invalid++;
355 push @errors, {unknown_error => 1};
356 $template->param('lastinvalid'=>$borrower{'surname'}.' / AddMember');
360 (@errors ) and $template->param( ERRORS=>\@errors );
361 (@feedback) and $template->param(FEEDBACK=>\@feedback);
362 $template->param(
363 'uploadborrowers' => 1,
364 'imported' => $imported,
365 'overwritten' => $overwritten,
366 'alreadyindb' => $alreadyindb,
367 'invalid' => $invalid,
368 'total' => $imported + $alreadyindb + $invalid + $overwritten,
371 } else {
372 if ($extended) {
373 my @matchpoints = ();
374 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes(undef, 1);
375 foreach my $type (@attr_types) {
376 my $attr_type = C4::Members::AttributeTypes->fetch($type->{code});
377 if ($attr_type->unique_id()) {
378 push @matchpoints, { code => "patron_attribute_" . $attr_type->code(), description => $attr_type->description() };
381 $template->param(matchpoints => \@matchpoints);
385 output_html_with_http_headers $input, $cookie, $template->output;