#
# 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 <http://www.gnu.org/licenses>.
# Script to take some borrowers data in a known format and load it into Koha
#
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' && $_ ne 'cardnumber'} @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};
+#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,
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(
$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');
} elsif (@columns == @columnkeys) {
@borrower{@columnkeys} = @columns;
# MJR: try to fill blanks gracefully by using default values
- foreach my $key (@criticals) {
+ foreach my $key (@columnkeys) {
if ($borrower{$key} !~ /\S/) {
$borrower{$key} = $defaults{$key};
}
}
if ($extended) {
my $attr_str = $borrower{patron_attributes};
+ $attr_str =~ s/\xe2\x80\x9c/"/g; # fixup double quotes in case we are passed smart quotes
+ $attr_str =~ s/\xe2\x80\x9d/"/g;
+ push @feedback, {feedback=>1, name=>'attribute string', value=>$attr_str, filename=>$uploadborrowers};
delete $borrower{patron_attributes}; # not really a field in borrowers, so we don't want to pass it to ModMember.
$patron_attributes = extended_attributes_code_value_arrayref($attr_str);
}
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) {
}
}
}
-
+
+ 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) {
for my $col (keys %borrower) {
# use values from extant patron unless our csv file includes this column or we provided a default.
# FIXME : You cannot update a field with a perl-evaluated false value using the defaults.
+
+ # The password is always encrypted, skip it!
+ next if $col eq 'password';
+
unless(exists($csvkeycol{$col}) || $defaults{$col}) {
$borrower{$col} = $member->{$col} if($member->{$col}) ;
}
}
unless (ModMember(%borrower)) {
$invalid++;
+ # 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);
$patron_attributes = extended_attributes_merge($old_attributes, $patron_attributes); #TODO: expose repeatable options in template
}
- SetBorrowerAttributes($borrower{'borrowernumber'}, $patron_attributes);
+ push @errors, {unknown_error => 1} unless SetBorrowerAttributes($borrower{'borrowernumber'}, $patron_attributes);
}
$overwritten++;
$template->param('lastoverwritten'=>$borrower{'surname'}.' / '.$borrowernumber);
$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 {
$invalid++;
+ push @errors, {unknown_error => 1};
$template->param('lastinvalid'=>$borrower{'surname'}.' / AddMember');
}
}
} else {
if ($extended) {
my @matchpoints = ();
- my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
+ my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes(undef, 1);
foreach my $type (@attr_types) {
my $attr_type = C4::Members::AttributeTypes->fetch($type->{code});
if ($attr_type->unique_id()) {