# 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 3 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>.
use Modern::Perl;
use Moo;
use Carp;
use Text::CSV;
+use Encode qw( decode_utf8 );
use C4::Members;
use C4::Members::Attributes qw(:all);
my $status = $self->text_csv->parse($borrowerline);
my @columns = $self->text_csv->fields();
if ( !$status ) {
- push @missing_criticals, { badparse => 1, line => $line_number, lineraw => $borrowerline };
+ push @missing_criticals, { badparse => 1, line => $line_number, lineraw => decode_utf8($borrowerline) };
}
elsif ( @columns == @columnkeys ) {
@borrower{@columnkeys} = @columns;
elsif ( scalar grep { $key eq $_ } @criticals ) {
# a critical field is undefined
- push @missing_criticals, { key => $key, line => $., lineraw => $borrowerline };
+ push @missing_criticals, { key => $key, line => $., lineraw => decode_utf8($borrowerline) };
}
else {
$borrower{$key} = '';
}
}
+ $borrower{cardnumber} = undef if $borrower{cardnumber} eq "";
+
# Check if borrower category code exists and if it matches to a known category. Pushing error to missing_criticals otherwise.
$self->check_borrower_category($borrower{categorycode}, $borrowerline, $line_number, \@missing_criticals);
if ( $attr->{code} eq $matchpoint and $attr->{value} ne '' ) {
my @borrowernumbers = $matchpoint_attr_type->get_patrons( $attr->{value} );
$borrowernumber = $borrowernumbers[0] if scalar(@borrowernumbers) == 1;
+ $patron = Koha::Patrons->find( $borrowernumber );
last;
}
}
and $matchpoint ne 'userid'
and exists $borrower{userid}
and $borrower{userid}
- and not Koha::Patron->new( { userid => $borrower{userid} } )->has_valid_userid
+ and not ( $borrowernumber ? $patron->userid( $borrower{userid} )->has_valid_userid : Koha::Patron->new( { userid => $borrower{userid} } )->has_valid_userid )
) {
push @errors, { duplicate_userid => 1, userid => $borrower{userid} };
$invalid++;
next LINE;
}
+ my $relationship = $borrower{relationship};
+ my $guarantor_id = $borrower{guarantor_id};
+ delete $borrower{relationship};
+ delete $borrower{guarantor_id};
+
if ($borrowernumber) {
# borrower exists
}
}
- unless ( ModMember(%borrower) ) {
+ my $patron = Koha::Patrons->find( $borrowernumber );
+ eval { $patron->set(\%borrower)->store };
+ if ( $@ ) {
$invalid++;
push(
@errors,
{
+ # TODO We can raise a better error
name => 'lastinvalid',
value => $borrower{'surname'} . ' / ' . $borrowernumber
}
);
}
else {
- if ( $borrowernumber = AddMember(%borrower) ) {
+ my $patron = eval {
+ Koha::Patron->new(\%borrower)->store;
+ };
+ unless ( $@ ) {
- if ( $borrower{debarred} ) {
+ if ( $patron->is_debarred ) {
AddDebarment(
{
- borrowernumber => $borrowernumber,
- expiration => $borrower{debarred},
- comment => $borrower{debarredcomment}
+ borrowernumber => $patron->borrowernumber,
+ expiration => $patron->debarred,
+ comment => $patron->debarredcomment,
}
);
}
if ($extended) {
- SetBorrowerAttributes( $borrowernumber, $patron_attributes );
+ SetBorrowerAttributes( $patron->borrowernumber, $patron_attributes );
}
if ($set_messaging_prefs) {
C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
{
- borrowernumber => $borrowernumber,
- categorycode => $borrower{categorycode}
+ borrowernumber => $patron->borrowernumber,
+ categorycode => $patron->categorycode,
}
);
}
$imported++;
- push @imported_borrowers, $borrowernumber; #for patronlist
+ push @imported_borrowers, $patron->borrowernumber; #for patronlist
push(
@feedback,
{
feedback => 1,
name => 'lastimported',
- value => $borrower{'surname'} . ' / ' . $borrowernumber
+ value => $patron->surname . ' / ' . $patron->borrowernumber,
}
);
}
@errors,
{
name => 'lastinvalid',
- value => $borrower{'surname'} . ' / AddMember',
+ value => $borrower{'surname'} . ' / Create patron',
}
);
}
}
+
+ # Add a guarantor if we are given a relationship
+ if ( $guarantor_id ) {
+ Koha::Patron::Relationship->new(
+ {
+ guarantee_id => $borrowernumber,
+ relationship => $relationship,
+ guarantor_id => $guarantor_id,
+ }
+ )->store();
+ }
}
return {
foreach my $keycol (@csvcolumns) {
# columnkeys don't contain whitespace, but some stupid tools add it
$keycol =~ s/ +//g;
+ $keycol =~ s/^\N{BOM}//; # Strip BOM if exists, otherwise it will be part of first column key
$params->{keycol}->{$keycol} = $col++;
}
# No branch code
unless( $branchcode ) {
- push (@$missing_criticals, { key => 'branchcode', line => $line_number, lineraw => $borrowerline, });
+ push (@$missing_criticals, { key => 'branchcode', line => $line_number, lineraw => decode_utf8($borrowerline), });
return;
}
# look for branch code
my $library = Koha::Libraries->find( $branchcode );
unless( $library ) {
- push (@$missing_criticals, { key => 'branchcode', line => $line_number, lineraw => $borrowerline,
+ push (@$missing_criticals, { key => 'branchcode', line => $line_number, lineraw => decode_utf8($borrowerline),
value => $branchcode, branch_map => 1, });
}
}
# No branch code
unless( $categorycode ) {
- push (@$missing_criticals, { key => 'categorycode', line => $line_number, lineraw => $borrowerline, });
+ push (@$missing_criticals, { key => 'categorycode', line => $line_number, lineraw => decode_utf8($borrowerline), });
return;
}
# Looking for borrower category
my $category = Koha::Patron::Categories->find($categorycode);
unless( $category ) {
- push (@$missing_criticals, { key => 'categorycode', line => $line_number, lineraw => $borrowerline,
+ push (@$missing_criticals, { key => 'categorycode', line => $line_number, lineraw => decode_utf8($borrowerline),
value => $categorycode, category_map => 1, });
}
}
sub format_dates {
my ($self, $params) = @_;
- foreach my $date_type (qw(dateofbirth dateenrolled dateexpiry)) {
+ foreach my $date_type (qw(dateofbirth dateenrolled dateexpiry date_renewed)) {
my $tempdate = $params->{borrower}->{$date_type} or next();
my $formatted_date = eval { output_pref( { dt => dt_from_string( $tempdate ), dateonly => 1, dateformat => 'iso' } ); };
$params->{borrower}->{$date_type} = $formatted_date;
} else {
$params->{borrower}->{$date_type} = '';
- push (@{$params->{missing_criticals}}, { key => $date_type, line => $params->{line}, lineraw => $params->{lineraw}, bad_date => 1 });
+ push (@{$params->{missing_criticals}}, { key => $date_type, line => $params->{line}, lineraw => decode_utf8($params->{lineraw}), bad_date => 1 });
}
}
}