Bug 32170: Add page-section to csv-profiles
[koha-ffzg.git] / tools / import_borrowers.pl
index 4da2cd0..a02f14d 100755 (executable)
 
 use Modern::Perl;
 
-use C4::Auth;
-use C4::Output;
-use C4::Templates;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_and_exit output_html_with_http_headers );
+use Koha::Database::Columns;
 use Koha::Patrons;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Token;
 use Koha::Libraries;
 use Koha::Patron::Categories;
-use Koha::List::Patron;
+use Koha::Patron::Attribute::Types;
+use Koha::List::Patron qw( AddPatronList AddPatronsToList );
 
 use Koha::Patrons::Import;
 my $Import = Koha::Patrons::Import->new();
 
 use Text::CSV;
 
-# Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals:
-# ė
-# č
-
 use CGI qw ( -utf8 );
 
-my ( @errors, @feedback );
 my $extended = C4::Context->preference('ExtendedPatronAttributes');
 
 my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } Koha::Patrons->columns();
 push( @columnkeys, 'patron_attributes' ) if $extended;
-push( @columnkeys, qw( relationship guarantor_id  guarantor_firstname guarantor_surname ) );
+push( @columnkeys, qw( guarantor_relationship guarantor_id ) );
 
 my $input = CGI->new();
 
-#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.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { tools => 'import_patrons' },
-        debug           => 1,
     }
 );
 
 # get the patron categories and pass them to the template
-my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
 $template->param( categories => \@patron_categories );
-my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers};
-$columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
-$template->param( borrower_fields => $columns );
+$template->param( borrower_fields => Koha::Database::Columns->columns->{borrowers} );
 
 if ( $input->param('sample') ) {
     our $csv = Text::CSV->new( { binary => 1 } );    # binary needed for non-ASCII Unicode
@@ -97,8 +87,11 @@ if ( $input->param('sample') ) {
     exit 0;
 }
 
+my @preserve_fields = $input->multi_param('preserve_existing');
+
 my $uploadborrowers = $input->param('uploadborrowers');
 my $matchpoint      = $input->param('matchpoint');
+my $welcome_new     = $input->param('welcome_new');
 if ($matchpoint) {
     $matchpoint =~ s/^patron_attribute_//;
 }
@@ -120,14 +113,20 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
 
     my $handle   = $input->upload('uploadborrowers');
     my %defaults = $input->Vars;
-
+    my $overwrite_passwords = defined $input->param('overwrite_passwords') ? 1 : 0;
+    my $update_dateexpiry = $input->param('update_dateexpiry');
     my $return = $Import->import_patrons(
         {
             file                         => $handle,
             defaults                     => \%defaults,
             matchpoint                   => $matchpoint,
-            overwrite_cardnumber         => $input->param('overwrite_cardnumber'),
-            preserve_extended_attributes => $input->param('ext_preserve') || 0,
+            overwrite_cardnumber         => scalar $input->param( 'overwrite_cardnumber' ),
+            overwrite_passwords          => $overwrite_passwords,
+            preserve_extended_attributes => scalar $input->param( 'ext_preserve' ) || 0,
+            preserve_fields              => \@preserve_fields,
+            update_dateexpiry            => $update_dateexpiry ? 1 : 0,
+            update_dateexpiry_from_today => $update_dateexpiry eq "now" ? 1 : 0,
+            send_welcome                 => $welcome_new,
         }
     );
 
@@ -167,9 +166,9 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
 else {
     if ($extended) {
         my @matchpoints = ();
-        my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( undef, 1 );
-        foreach my $type (@attr_types) {
-            my $attr_type = C4::Members::AttributeTypes->fetch( $type->{code} );
+        my $attribute_types = Koha::Patron::Attribute::Types->search;
+
+        while ( my $attr_type = $attribute_types->next ) {
             if ( $attr_type->unique_id() ) {
                 push @matchpoints,
                   { code => "patron_attribute_" . $attr_type->code(), description => $attr_type->description() };