Lines 34-54
Link Here
|
34 |
# dates should be in the format you have set up Koha to expect |
34 |
# dates should be in the format you have set up Koha to expect |
35 |
# branchcode and categorycode need to be valid |
35 |
# branchcode and categorycode need to be valid |
36 |
|
36 |
|
37 |
use strict; |
37 |
use Modern::Perl; |
38 |
use warnings; |
|
|
39 |
|
38 |
|
40 |
use C4::Auth; |
39 |
use C4::Auth; |
41 |
use C4::Output; |
40 |
use C4::Output; |
42 |
use C4::Dates qw(format_date_in_iso); |
|
|
43 |
use C4::Context; |
44 |
use C4::Branch qw/GetBranchesLoop GetBranchName/; |
45 |
use C4::Members; |
46 |
use C4::Members::Attributes qw(:all); |
47 |
use C4::Members::AttributeTypes; |
48 |
use C4::Members::Messaging; |
49 |
use C4::Reports::Guided; |
50 |
use C4::Templates; |
41 |
use C4::Templates; |
51 |
use Koha::Borrower::Debarments; |
42 |
|
|
|
43 |
use C4::Branch; |
44 |
use C4::Members; |
45 |
|
46 |
use Koha::Patrons::Import qw(import_patrons); |
52 |
|
47 |
|
53 |
use Text::CSV; |
48 |
use Text::CSV; |
54 |
|
49 |
|
Lines 61-76
use CGI qw ( -utf8 );
Link Here
|
61 |
# use encoding 'utf8'; # don't do this |
56 |
# use encoding 'utf8'; # don't do this |
62 |
|
57 |
|
63 |
my ( @errors, @feedback ); |
58 |
my ( @errors, @feedback ); |
64 |
my $extended = C4::Context->preference('ExtendedPatronAttributes'); |
59 |
my $extended = C4::Context->preference('ExtendedPatronAttributes'); |
65 |
my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); |
60 |
|
66 |
my @columnkeys = C4::Members::columns(); |
61 |
my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } C4::Members::columns(); |
67 |
@columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } @columnkeys; |
62 |
push( @columnkeys, 'patron_attributes' ) if $extended; |
68 |
if ($extended) { |
|
|
69 |
push @columnkeys, 'patron_attributes'; |
70 |
} |
71 |
|
63 |
|
72 |
my $input = CGI->new(); |
64 |
my $input = CGI->new(); |
73 |
our $csv = Text::CSV->new( { binary => 1 } ); # binary needed for non-ASCII Unicode |
|
|
74 |
|
65 |
|
75 |
#push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; #XXX |
66 |
#push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; #XXX |
76 |
|
67 |
|
Lines 97-102
$columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
Link Here
|
97 |
$template->param( borrower_fields => $columns ); |
88 |
$template->param( borrower_fields => $columns ); |
98 |
|
89 |
|
99 |
if ( $input->param('sample') ) { |
90 |
if ( $input->param('sample') ) { |
|
|
91 |
our $csv = Text::CSV->new( { binary => 1 } ); # binary needed for non-ASCII Unicode |
100 |
print $input->header( |
92 |
print $input->header( |
101 |
-type => 'application/vnd.sun.xml.calc', # 'application/vnd.ms-excel' ? |
93 |
-type => 'application/vnd.sun.xml.calc', # 'application/vnd.ms-excel' ? |
102 |
-attachment => 'patron_import.csv', |
94 |
-attachment => 'patron_import.csv', |
Lines 105-414
if ( $input->param('sample') ) {
Link Here
|
105 |
print $csv->string, "\n"; |
97 |
print $csv->string, "\n"; |
106 |
exit 1; |
98 |
exit 1; |
107 |
} |
99 |
} |
|
|
100 |
|
108 |
my $uploadborrowers = $input->param('uploadborrowers'); |
101 |
my $uploadborrowers = $input->param('uploadborrowers'); |
109 |
my $matchpoint = $input->param('matchpoint'); |
102 |
my $matchpoint = $input->param('matchpoint'); |
110 |
if ($matchpoint) { |
103 |
if ($matchpoint) { |
111 |
$matchpoint =~ s/^patron_attribute_//; |
104 |
$matchpoint =~ s/^patron_attribute_//; |
112 |
} |
105 |
} |
113 |
my $overwrite_cardnumber = $input->param('overwrite_cardnumber'); |
|
|
114 |
|
106 |
|
115 |
$template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} ); |
107 |
$template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} ); |
116 |
|
108 |
|
117 |
if ( $uploadborrowers && length($uploadborrowers) > 0 ) { |
109 |
if ( $uploadborrowers && length($uploadborrowers) > 0 ) { |
118 |
push @feedback, { feedback => 1, name => 'filename', value => $uploadborrowers, filename => $uploadborrowers }; |
110 |
my $handle = $input->upload('uploadborrowers'); |
119 |
my $handle = $input->upload('uploadborrowers'); |
|
|
120 |
my $uploadinfo = $input->uploadInfo($uploadborrowers); |
121 |
foreach ( keys %$uploadinfo ) { |
122 |
push @feedback, { feedback => 1, name => $_, value => $uploadinfo->{$_}, $_ => $uploadinfo->{$_} }; |
123 |
} |
124 |
my $imported = 0; |
125 |
my $alreadyindb = 0; |
126 |
my $overwritten = 0; |
127 |
my $invalid = 0; |
128 |
my $matchpoint_attr_type; |
129 |
my %defaults = $input->Vars; |
111 |
my %defaults = $input->Vars; |
130 |
|
112 |
|
131 |
# use header line to construct key to column map |
113 |
my $return = Koha::Patrons::Import::import_patrons( |
132 |
my $borrowerline = <$handle>; |
114 |
{ |
133 |
my $status = $csv->parse($borrowerline); |
115 |
file => $handle, |
134 |
($status) or push @errors, { badheader => 1, line => $., lineraw => $borrowerline }; |
116 |
defaults => \%defaults, |
135 |
my @csvcolumns = $csv->fields(); |
117 |
matchpoint => $matchpoint, |
136 |
my %csvkeycol; |
118 |
overwrite_cardnumber => $input->param('overwrite_cardnumber'), |
137 |
my $col = 0; |
119 |
preserve_extended_attributes => $input->param('ext_preserve') || 0, |
138 |
foreach my $keycol (@csvcolumns) { |
|
|
139 |
|
140 |
# columnkeys don't contain whitespace, but some stupid tools add it |
141 |
$keycol =~ s/ +//g; |
142 |
$csvkeycol{$keycol} = $col++; |
143 |
} |
144 |
|
145 |
#warn($borrowerline); |
146 |
my $ext_preserve = $input->param('ext_preserve') || 0; |
147 |
if ($extended) { |
148 |
$matchpoint_attr_type = C4::Members::AttributeTypes->fetch($matchpoint); |
149 |
} |
150 |
|
151 |
push @feedback, { feedback => 1, name => 'headerrow', value => join( ', ', @csvcolumns ) }; |
152 |
my $today_iso = C4::Dates->new()->output('iso'); |
153 |
my @criticals = qw(surname branchcode categorycode); # there probably should be others |
154 |
my @bad_dates; # I've had a few. |
155 |
my $date_re = C4::Dates->new->regexp('syspref'); |
156 |
my $iso_re = C4::Dates->new->regexp('iso'); |
157 |
LINE: while ( my $borrowerline = <$handle> ) { |
158 |
my %borrower; |
159 |
my @missing_criticals; |
160 |
my $patron_attributes; |
161 |
my $status = $csv->parse($borrowerline); |
162 |
my @columns = $csv->fields(); |
163 |
if ( !$status ) { |
164 |
push @missing_criticals, { badparse => 1, line => $., lineraw => $borrowerline }; |
165 |
} |
120 |
} |
166 |
elsif ( @columns == @columnkeys ) { |
121 |
); |
167 |
@borrower{@columnkeys} = @columns; |
|
|
168 |
|
169 |
# MJR: try to fill blanks gracefully by using default values |
170 |
foreach my $key (@columnkeys) { |
171 |
if ( $borrower{$key} !~ /\S/ ) { |
172 |
$borrower{$key} = $defaults{$key}; |
173 |
} |
174 |
} |
175 |
} |
176 |
else { |
177 |
# MJR: try to recover gracefully by using default values |
178 |
foreach my $key (@columnkeys) { |
179 |
if ( defined( $csvkeycol{$key} ) and $columns[ $csvkeycol{$key} ] =~ /\S/ ) { |
180 |
$borrower{$key} = $columns[ $csvkeycol{$key} ]; |
181 |
} |
182 |
elsif ( $defaults{$key} ) { |
183 |
$borrower{$key} = $defaults{$key}; |
184 |
} |
185 |
elsif ( scalar grep { $key eq $_ } @criticals ) { |
186 |
|
187 |
# a critical field is undefined |
188 |
push @missing_criticals, { key => $key, line => $., lineraw => $borrowerline }; |
189 |
} |
190 |
else { |
191 |
$borrower{$key} = ''; |
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
#warn join(':',%borrower); |
197 |
if ( $borrower{categorycode} ) { |
198 |
push @missing_criticals, |
199 |
{ |
200 |
key => 'categorycode', |
201 |
line => $., |
202 |
lineraw => $borrowerline, |
203 |
value => $borrower{categorycode}, |
204 |
category_map => 1 |
205 |
} |
206 |
unless GetBorrowercategory( $borrower{categorycode} ); |
207 |
} |
208 |
else { |
209 |
push @missing_criticals, { key => 'categorycode', line => $., lineraw => $borrowerline }; |
210 |
} |
211 |
if ( $borrower{branchcode} ) { |
212 |
push @missing_criticals, |
213 |
{ |
214 |
key => 'branchcode', |
215 |
line => $., |
216 |
lineraw => $borrowerline, |
217 |
value => $borrower{branchcode}, |
218 |
branch_map => 1 |
219 |
} |
220 |
unless GetBranchName( $borrower{branchcode} ); |
221 |
} |
222 |
else { |
223 |
push @missing_criticals, { key => 'branchcode', line => $., lineraw => $borrowerline }; |
224 |
} |
225 |
if (@missing_criticals) { |
226 |
foreach (@missing_criticals) { |
227 |
$_->{borrowernumber} = $borrower{borrowernumber} || 'UNDEF'; |
228 |
$_->{surname} = $borrower{surname} || 'UNDEF'; |
229 |
} |
230 |
$invalid++; |
231 |
( 25 > scalar @errors ) and push @errors, { missing_criticals => \@missing_criticals }; |
232 |
|
233 |
# The first 25 errors are enough. Keeping track of 30,000+ would destroy performance. |
234 |
next LINE; |
235 |
} |
236 |
if ($extended) { |
237 |
my $attr_str = $borrower{patron_attributes}; |
238 |
$attr_str =~ s/\xe2\x80\x9c/"/g; # fixup double quotes in case we are passed smart quotes |
239 |
$attr_str =~ s/\xe2\x80\x9d/"/g; |
240 |
push @feedback, |
241 |
{ feedback => 1, name => 'attribute string', value => $attr_str, filename => $uploadborrowers }; |
242 |
delete $borrower{patron_attributes} |
243 |
; # not really a field in borrowers, so we don't want to pass it to ModMember. |
244 |
$patron_attributes = extended_attributes_code_value_arrayref($attr_str); |
245 |
} |
246 |
|
247 |
# Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it. |
248 |
foreach (qw(dateofbirth dateenrolled dateexpiry)) { |
249 |
my $tempdate = $borrower{$_} or next; |
250 |
if ( $tempdate =~ /$date_re/ ) { |
251 |
$borrower{$_} = format_date_in_iso($tempdate); |
252 |
} |
253 |
elsif ( $tempdate =~ /$iso_re/ ) { |
254 |
$borrower{$_} = $tempdate; |
255 |
} |
256 |
else { |
257 |
$borrower{$_} = ''; |
258 |
push @missing_criticals, { key => $_, line => $., lineraw => $borrowerline, bad_date => 1 }; |
259 |
} |
260 |
} |
261 |
$borrower{dateenrolled} = $today_iso unless $borrower{dateenrolled}; |
262 |
$borrower{dateexpiry} = GetExpiryDate( $borrower{categorycode}, $borrower{dateenrolled} ) |
263 |
unless $borrower{dateexpiry}; |
264 |
my $borrowernumber; |
265 |
my $member; |
266 |
if ( ( $matchpoint eq 'cardnumber' ) && ( $borrower{'cardnumber'} ) ) { |
267 |
$member = GetMember( 'cardnumber' => $borrower{'cardnumber'} ); |
268 |
if ($member) { |
269 |
$borrowernumber = $member->{'borrowernumber'}; |
270 |
} |
271 |
} elsif ( ($matchpoint eq 'userid') && ($borrower{'userid'}) ) { |
272 |
$member = GetMember( 'userid' => $borrower{'userid'} ); |
273 |
if ($member) { |
274 |
$borrowernumber = $member->{'borrowernumber'}; |
275 |
} |
276 |
} elsif ($extended) { |
277 |
if (defined($matchpoint_attr_type)) { |
278 |
foreach my $attr (@$patron_attributes) { |
279 |
if ( $attr->{code} eq $matchpoint and $attr->{value} ne '' ) { |
280 |
my @borrowernumbers = $matchpoint_attr_type->get_patrons( $attr->{value} ); |
281 |
$borrowernumber = $borrowernumbers[0] if scalar(@borrowernumbers) == 1; |
282 |
last; |
283 |
} |
284 |
} |
285 |
} |
286 |
} |
287 |
|
288 |
if ( C4::Members::checkcardnumber( $borrower{cardnumber}, $borrowernumber ) ) { |
289 |
push @errors, |
290 |
{ |
291 |
invalid_cardnumber => 1, |
292 |
borrowernumber => $borrowernumber, |
293 |
cardnumber => $borrower{cardnumber} |
294 |
}; |
295 |
$invalid++; |
296 |
next; |
297 |
} |
298 |
|
299 |
if ($borrowernumber) { |
300 |
|
301 |
# borrower exists |
302 |
unless ($overwrite_cardnumber) { |
303 |
$alreadyindb++; |
304 |
$template->param( 'lastalreadyindb' => $borrower{'surname'} . ' / ' . $borrowernumber ); |
305 |
next LINE; |
306 |
} |
307 |
$borrower{'borrowernumber'} = $borrowernumber; |
308 |
for my $col ( keys %borrower ) { |
309 |
|
310 |
# use values from extant patron unless our csv file includes this column or we provided a default. |
311 |
# FIXME : You cannot update a field with a perl-evaluated false value using the defaults. |
312 |
|
313 |
# The password is always encrypted, skip it! |
314 |
next if $col eq 'password'; |
315 |
|
316 |
unless ( exists( $csvkeycol{$col} ) || $defaults{$col} ) { |
317 |
$borrower{$col} = $member->{$col} if ( $member->{$col} ); |
318 |
} |
319 |
} |
320 |
unless ( ModMember(%borrower) ) { |
321 |
$invalid++; |
322 |
|
323 |
# untill we have better error trapping, we have no way of knowing why ModMember errored out... |
324 |
push @errors, { unknown_error => 1 }; |
325 |
$template->param( 'lastinvalid' => $borrower{'surname'} . ' / ' . $borrowernumber ); |
326 |
next LINE; |
327 |
} |
328 |
if ( $borrower{debarred} ) { |
329 |
|
330 |
# Check to see if this debarment already exists |
331 |
my $debarrments = GetDebarments( |
332 |
{ |
333 |
borrowernumber => $borrowernumber, |
334 |
expiration => $borrower{debarred}, |
335 |
comment => $borrower{debarredcomment} |
336 |
} |
337 |
); |
338 |
|
339 |
# If it doesn't, then add it! |
340 |
unless (@$debarrments) { |
341 |
AddDebarment( |
342 |
{ |
343 |
borrowernumber => $borrowernumber, |
344 |
expiration => $borrower{debarred}, |
345 |
comment => $borrower{debarredcomment} |
346 |
} |
347 |
); |
348 |
} |
349 |
} |
350 |
if ($extended) { |
351 |
if ($ext_preserve) { |
352 |
my $old_attributes = GetBorrowerAttributes($borrowernumber); |
353 |
$patron_attributes = extended_attributes_merge( $old_attributes, $patron_attributes ) |
354 |
; #TODO: expose repeatable options in template |
355 |
} |
356 |
push @errors, { unknown_error => 1 } |
357 |
unless SetBorrowerAttributes( $borrower{'borrowernumber'}, $patron_attributes ); |
358 |
} |
359 |
$overwritten++; |
360 |
$template->param( 'lastoverwritten' => $borrower{'surname'} . ' / ' . $borrowernumber ); |
361 |
} |
362 |
else { |
363 |
# FIXME: fixup_cardnumber says to lock table, but the web interface doesn't so this doesn't either. |
364 |
# At least this is closer to AddMember than in members/memberentry.pl |
365 |
if ( !$borrower{'cardnumber'} ) { |
366 |
$borrower{'cardnumber'} = fixup_cardnumber(undef); |
367 |
} |
368 |
if ( $borrowernumber = AddMember(%borrower) ) { |
369 |
|
122 |
|
370 |
if ( $borrower{debarred} ) { |
123 |
my $feedback = $return->{feedback}; |
371 |
AddDebarment( |
124 |
my $errors = $return->{errors}; |
372 |
{ |
125 |
my $imported = $return->{imported}; |
373 |
borrowernumber => $borrowernumber, |
126 |
my $overwritten = $return->{overwritten}; |
374 |
expiration => $borrower{debarred}, |
127 |
my $alreadyindb = $return->{already_in_db}; |
375 |
comment => $borrower{debarredcomment} |
128 |
my $invalid = $return->{invalid}; |
376 |
} |
|
|
377 |
); |
378 |
} |
379 |
|
129 |
|
380 |
if ($extended) { |
130 |
my $uploadinfo = $input->uploadInfo($uploadborrowers); |
381 |
SetBorrowerAttributes( $borrowernumber, $patron_attributes ); |
131 |
foreach ( keys %$uploadinfo ) { |
382 |
} |
132 |
push @$feedback, { feedback => 1, name => $_, value => $uploadinfo->{$_}, $_ => $uploadinfo->{$_} }; |
|
|
133 |
} |
383 |
|
134 |
|
384 |
if ($set_messaging_prefs) { |
135 |
push @$feedback, { feedback => 1, name => 'filename', value => $uploadborrowers, filename => $uploadborrowers }; |
385 |
C4::Members::Messaging::SetMessagingPreferencesFromDefaults( |
|
|
386 |
{ |
387 |
borrowernumber => $borrowernumber, |
388 |
categorycode => $borrower{categorycode} |
389 |
} |
390 |
); |
391 |
} |
392 |
|
136 |
|
393 |
$imported++; |
|
|
394 |
$template->param( 'lastimported' => $borrower{'surname'} . ' / ' . $borrowernumber ); |
395 |
} |
396 |
else { |
397 |
$invalid++; |
398 |
push @errors, { unknown_error => 1 }; |
399 |
$template->param( 'lastinvalid' => $borrower{'surname'} . ' / AddMember' ); |
400 |
} |
401 |
} |
402 |
} |
403 |
(@errors) and $template->param( ERRORS => \@errors ); |
404 |
(@feedback) and $template->param( FEEDBACK => \@feedback ); |
405 |
$template->param( |
137 |
$template->param( |
406 |
'uploadborrowers' => 1, |
138 |
uploadborrowers => 1, |
407 |
'imported' => $imported, |
139 |
errors => $errors, |
408 |
'overwritten' => $overwritten, |
140 |
feedback => $feedback, |
409 |
'alreadyindb' => $alreadyindb, |
141 |
imported => $imported, |
410 |
'invalid' => $invalid, |
142 |
overwritten => $overwritten, |
411 |
'total' => $imported + $alreadyindb + $invalid + $overwritten, |
143 |
alreadyindb => $alreadyindb, |
|
|
144 |
invalid => $invalid, |
145 |
total => $imported + $alreadyindb + $invalid + $overwritten, |
412 |
); |
146 |
); |
413 |
|
147 |
|
414 |
} |
148 |
} |
415 |
- |
|
|