Bug 24545: Fix license statements
[srvgit] / Koha / Patrons / Import.pm
index dbf945c..91ab8fb 100644 (file)
@@ -2,18 +2,18 @@ package Koha::Patrons::Import;
 
 # 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;
@@ -21,6 +21,7 @@ use namespace::clean;
 
 use Carp;
 use Text::CSV;
+use Encode qw( decode_utf8 );
 
 use C4::Members;
 use C4::Members::Attributes qw(:all);
@@ -101,7 +102,7 @@ sub import_patrons {
         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;
@@ -125,7 +126,7 @@ sub import_patrons {
                 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} = '';
@@ -133,6 +134,8 @@ sub import_patrons {
             }
         }
 
+        $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);
 
@@ -176,6 +179,7 @@ sub import_patrons {
                     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;
                     }
                 }
@@ -206,13 +210,18 @@ sub import_patrons {
             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
@@ -241,12 +250,15 @@ sub import_patrons {
                 }
             }
 
-            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
                     }
@@ -295,39 +307,42 @@ sub import_patrons {
             );
         }
         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,
                     }
                 );
             }
@@ -338,11 +353,22 @@ sub import_patrons {
                     @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 {
@@ -378,6 +404,7 @@ sub prepare_columns {
     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++;
     }
 
@@ -458,14 +485,14 @@ sub check_branch_code {
 
     # 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, });
     }
 }
@@ -483,14 +510,14 @@ sub check_borrower_category {
 
     # 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, });
     }
 }
@@ -507,7 +534,7 @@ be formatted to the chosen date format. Populates the correctly formatted date o
 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' } ); };
 
@@ -515,7 +542,7 @@ sub format_dates {
             $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 });
         }
     }
 }