X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2Fimport_borrowers.pl;h=15053ccd7335ebc200697531f14406afd2f9fee0;hb=49f2837b2e75511becd09059db99d209917647a7;hp=78353c9ed3414a9fee44233a007c9facff42eb43;hpb=ab59c080359170b156eceb101b7cf029e96d4e09;p=koha.git diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index 78353c9ed3..15053ccd73 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -5,18 +5,18 @@ # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . # Script to take some borrowers data in a known format and load it into Koha # @@ -41,35 +41,38 @@ use C4::Auth; use C4::Output; use C4::Dates qw(format_date_in_iso); use C4::Context; -use C4::Branch qw(GetBranchName); +use C4::Branch qw/GetBranchesLoop GetBranchName/; use C4::Members; use C4::Members::Attributes qw(:all); use C4::Members::AttributeTypes; use C4::Members::Messaging; +use C4::Reports::Guided; +use C4::Templates; +use Koha::Borrower::Debarments; use Text::CSV; # Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals: # ė # č -use CGI; +use CGI qw ( -utf8 ); # use encoding 'utf8'; # don't do this my (@errors, @feedback); my $extended = C4::Context->preference('ExtendedPatronAttributes'); my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); -my @columnkeys = C4::Members->columns; +my @columnkeys = C4::Members::columns(); +@columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } @columnkeys; if ($extended) { push @columnkeys, 'patron_attributes'; } -my $columnkeystpl = [ map { {'key' => $_} } grep {$_ ne 'borrowernumber' } @columnkeys ]; # ref. to array of hashrefs. my $input = CGI->new(); our $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode #push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; #XXX my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ - template_name => "tools/import_borrowers.tmpl", + template_name => "tools/import_borrowers.tt", query => $input, type => "intranet", authnotrequired => 0, @@ -77,7 +80,15 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ debug => 1, }); -$template->param(columnkeys => $columnkeystpl); +# get the branches and pass them to the template +my $branches = GetBranchesLoop(); +$template->param( branches => $branches ) if ( $branches ); +# get the patron categories and pass them to the template +my $categories = GetBorrowercategoryList(); +$template->param( categories => $categories ) if ( $categories ); +my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers}; +$columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ]; +$template->param( borrower_fields => $columns ); if ($input->param('sample')) { print $input->header( @@ -97,8 +108,6 @@ my $overwrite_cardnumber = $input->param('overwrite_cardnumber'); $template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} ); -($extended) and $template->param(ExtendedPatronAttributes => 1); - if ( $uploadborrowers && length($uploadborrowers) > 0 ) { push @feedback, {feedback=>1, name=>'filename', value=>$uploadborrowers, filename=>$uploadborrowers}; my $handle = $input->upload('uploadborrowers'); @@ -220,6 +229,11 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { if ($member) { $borrowernumber = $member->{'borrowernumber'}; } + } elsif ( ($matchpoint eq 'userid') && ($borrower{'userid'}) ) { + $member = GetMember( 'userid' => $borrower{'userid'} ); + if ($member) { + $borrowernumber = $member->{'borrowernumber'}; + } } elsif ($extended) { if (defined($matchpoint_attr_type)) { foreach my $attr (@$patron_attributes) { @@ -231,7 +245,18 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { } } } - + + if ( C4::Members::checkcardnumber( $borrower{cardnumber}, $borrowernumber ) ) { + push @errors, { + invalid_cardnumber => 1, + borrowernumber => $borrowernumber, + cardnumber => $borrower{cardnumber} + }; + $invalid++; + next; + } + + if ($borrowernumber) { # borrower exists unless ($overwrite_cardnumber) { @@ -253,11 +278,31 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { } unless (ModMember(%borrower)) { $invalid++; - # untill we have better error trapping, we have no way of knowing why ModMember errored out... + # until we have better error trapping, we have no way of knowing why ModMember errored out... push @errors, {unknown_error => 1}; $template->param('lastinvalid'=>$borrower{'surname'}.' / '.$borrowernumber); next LINE; } + if ( $borrower{debarred} ) { + # Check to see if this debarment already exists + my $debarrments = GetDebarments( + { + borrowernumber => $borrowernumber, + expiration => $borrower{debarred}, + comment => $borrower{debarredcomment} + } + ); + # If it doesn't, then add it! + unless (@$debarrments) { + AddDebarment( + { + borrowernumber => $borrowernumber, + expiration => $borrower{debarred}, + comment => $borrower{debarredcomment} + } + ); + } + } if ($extended) { if ($ext_preserve) { my $old_attributes = GetBorrowerAttributes($borrowernumber); @@ -274,13 +319,26 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { $borrower{'cardnumber'} = fixup_cardnumber(undef); } if ($borrowernumber = AddMember(%borrower)) { + + if ( $borrower{debarred} ) { + AddDebarment( + { + borrowernumber => $borrowernumber, + expiration => $borrower{debarred}, + comment => $borrower{debarredcomment} + } + ); + } + if ($extended) { SetBorrowerAttributes($borrowernumber, $patron_attributes); } + if ($set_messaging_prefs) { C4::Members::Messaging::SetMessagingPreferencesFromDefaults({ borrowernumber => $borrowernumber, categorycode => $borrower{categorycode} }); } + $imported++; $template->param('lastimported'=>$borrower{'surname'}.' / '.$borrowernumber); } else {