From c533a690a725634bb6a9abc59bf5390c5fae685c Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 10 Mar 2021 13:43:32 -0500 Subject: [PATCH] Bug 27920: Add ability to update patron expiration dates when importing patrons Some libraries need to recalculate a patron's expiration date any time they are updated via a patron import from file. Test Plan: 1) Apply this patch 2) prove t/db_dependent/Koha/Patrons/Import.t --- Koha/Patrons/Import.pm | 3 +++ .../prog/en/modules/tools/import_borrowers.tt | 8 +++++++- misc/import_patrons.pl | 9 ++++++++- t/db_dependent/Koha/Patrons/Import.t | 9 +++++++-- tools/import_borrowers.pl | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Koha/Patrons/Import.pm b/Koha/Patrons/Import.pm index df9d42cda1..a80861021c 100644 --- a/Koha/Patrons/Import.pm +++ b/Koha/Patrons/Import.pm @@ -71,6 +71,7 @@ sub import_patrons { my $matchpoint = $params->{matchpoint}; my $defaults = $params->{defaults}; my $preserve_fields = $params->{preserve_fields}; + my $update_dateexpiry = $params->{update_dateexpiry}; my $ext_preserve = $params->{preserve_extended_attributes}; my $overwrite_cardnumber = $params->{overwrite_cardnumber}; my $overwrite_passwords = $params->{overwrite_passwords}; @@ -264,6 +265,8 @@ sub import_patrons { } } + $borrower{dateexpiry} = Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $borrower{dateenrolled} ) if $update_dateexpiry; + for my $col ( keys %borrower ) { # use values from extant patron unless our csv file includes this column or we provided a default. diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt index 7ee6d6885c..7f432411d7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt @@ -295,7 +295,11 @@
  • -
  • +
  • + + +
  • @@ -399,9 +403,11 @@ you can supply dates in ISO format (e.g., '2010-10-28'). $("#overwrite_cardnumberno").click(function(){ $("#overwrite_passwords").prop('checked',false).prop('disabled',true); + $("#update_dateexpiry").prop('checked',false).prop('disabled',true); }); $("#overwrite_cardnumberyes").click(function(){ $("#overwrite_passwords").prop('disabled',false); + $("#update_dateexpiry").prop('disabled',false); }); [% END %] diff --git a/misc/import_patrons.pl b/misc/import_patrons.pl index ee0d8a92c9..5057db6a0b 100755 --- a/misc/import_patrons.pl +++ b/misc/import_patrons.pl @@ -36,6 +36,7 @@ my $confirm; my $verbose = 0; my $help; my @preserve_fields; +my $update_dateexpiry; GetOptions( 'c|confirm' => \$confirm, @@ -44,6 +45,7 @@ GetOptions( 'd|default=s' => \%defaults, 'o|overwrite' => \$overwrite_cardnumber, 'op|overwrite_passwords' => \$overwrite_passwords, + 'ue|update_expiration' => \$update_dateexpiry, 'p|preserve-extended-attributes' => \$ext_preserve, 'pf|preserve-field=s' => \@preserve_fields, 'v|verbose+' => \$verbose, @@ -68,6 +70,7 @@ my $return = $Import->import_patrons( overwrite_passwords => $overwrite_passwords, preserve_extended_attributes => $ext_preserve, preserve_fields => \@preserve_fields, + update_dateexpiry => $update_dateexpiry dry_run => !$confirm, } ); @@ -107,7 +110,7 @@ import_patrons.pl - CLI script to import patrons data into Koha =head1 SYNOPSIS -import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve_field ] [--preserve-extended-attributes] [--verbose] +import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve_field ] [--preserve-extended-attributes] [--update-expiration] [--verbose] =head1 OPTIONS @@ -145,6 +148,10 @@ Overwrite existing patrons with new data if a match is found Retain extended patron attributes for existing patrons being overwritten +=item B<-p|--update-expiration> + +If a matching patron is found, extend the expiration date of their account using the patron's enrollment date as the base + =item B<-v|--verbose> Be verbose diff --git a/t/db_dependent/Koha/Patrons/Import.t b/t/db_dependent/Koha/Patrons/Import.t index 4627133412..1e41486235 100755 --- a/t/db_dependent/Koha/Patrons/Import.t +++ b/t/db_dependent/Koha/Patrons/Import.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 172; +use Test::More tests => 174; use Test::Warn; use Encode qw( encode_utf8 ); use utf8; @@ -188,7 +188,10 @@ is($result_3a->{overwritten}, 1, 'Got the expected 1 overwritten result from imp # overwrite but firstname is not my $filename_3c = make_csv($temp_dir, $csv_headers, $csv_one_line_b); open(my $handle_3c, "<", $filename_3c) or die "cannot open < $filename_3: $!"; -my $params_3c = { file => $handle_3c, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ] }; +my $params_3c = { file => $handle_3c, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ], update_dateexpiry => 1 }; + +my $patron_3 = Koha::Patrons->find({ cardnumber => '1000' }); +is( $patron_3->dateexpiry, '2015-07-01', "Expiration date is correct with update_dateexpiry = false" ); # When ... my $result_3c; @@ -210,6 +213,8 @@ is($result_3c->{invalid}, 0, 'Got the expected 0 invalid result from import_patr is($result_3c->{overwritten}, 1, 'Got the expected 1 overwritten result from import_patrons that matched'); my $patron_3c = Koha::Patrons->find({ cardnumber => '1000' }); +is( $patron_3c->dateexpiry, '2023-03-28', "Expiration date is correct with update_dateexpiry = true" ); + is( $patron_3c->surname, "Nancy2", "Surname field is preserved from original" ); is( $patron_3c->firstname, "Jenkins", "Firstname field is overwritten" ); diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index d22104f27d..28eb9a9866 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -120,6 +120,7 @@ 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 = defined $input->param('update_dateexpiry') ? 1 : 0; my $return = $Import->import_patrons( { file => $handle, @@ -129,6 +130,7 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { overwrite_passwords => $overwrite_passwords, preserve_extended_attributes => scalar $input->param( 'ext_preserve' ) || 0, preserve_fields => \@preserve_fields, + update_dateexpiry => $update_dateexpiry, } ); -- 2.24.3 (Apple Git-128)