From b9991954e0c28426049a0b1e018725f2073c80a8 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 Signed-off-by: David Nind --- Koha/Patrons/Import.pm | 23 ++++++++++++------- .../prog/en/modules/tools/import_borrowers.tt | 12 +++++++++- misc/import_patrons.pl | 16 ++++++++++++- t/db_dependent/Koha/Patrons/Import.t | 10 ++++++-- tools/import_borrowers.pl | 4 ++++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Koha/Patrons/Import.pm b/Koha/Patrons/Import.pm index 4904284d76..8c74c37cca 100644 --- a/Koha/Patrons/Import.pm +++ b/Koha/Patrons/Import.pm @@ -68,13 +68,15 @@ sub import_patrons { my $handle = $params->{file}; unless( $handle ) { carp('No file handle passed in!'); return; } - my $matchpoint = $params->{matchpoint}; - my $defaults = $params->{defaults}; - my $preserve_fields = $params->{preserve_fields}; - my $ext_preserve = $params->{preserve_extended_attributes}; - my $overwrite_cardnumber = $params->{overwrite_cardnumber}; - my $overwrite_passwords = $params->{overwrite_passwords}; - my $dry_run = $params->{dry_run}; + my $matchpoint = $params->{matchpoint}; + my $defaults = $params->{defaults}; + my $preserve_fields = $params->{preserve_fields}; + my $update_dateexpiry = $params->{update_dateexpiry}; + my $update_dateexpiry_from_today = $params->{update_dateexpiry_from_today}; + my $ext_preserve = $params->{preserve_extended_attributes}; + my $overwrite_cardnumber = $params->{overwrite_cardnumber}; + my $overwrite_passwords = $params->{overwrite_passwords}; + my $dry_run = $params->{dry_run}; my $extended = C4::Context->preference('ExtendedPatronAttributes'); my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); @@ -168,7 +170,8 @@ sub import_patrons { # Default date enrolled and date expiry if not already set. $borrower{dateenrolled} = $self->today_iso() unless $borrower{dateenrolled}; - $borrower{dateexpiry} = Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $borrower{dateenrolled} ) unless $borrower{dateexpiry}; + my $expiration_start_date = $update_dateexpiry_from_today ? dt_from_string : $borrower{dateenrolled}; + $borrower{dateexpiry} = Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $expiration_start_date ) unless $borrower{dateexpiry}; my $borrowernumber; my ( $member, $patron ); @@ -265,6 +268,10 @@ sub import_patrons { } } + my $expiration_start_date = $update_dateexpiry_from_today ? dt_from_string : $borrower{dateenrolled}; + $borrower{dateexpiry} = Koha::Patron::Categories->find( $borrower{categorycode} ) + ->get_expiry_date( $expiration_start_date ) 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 153ffd3a09..d96a25d253 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 @@ -322,7 +322,15 @@
  • -
  • +
  • + + +
  • +
  • + + +
  • @@ -426,9 +434,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..ac0dcb721b 100755 --- a/misc/import_patrons.pl +++ b/misc/import_patrons.pl @@ -36,6 +36,8 @@ my $confirm; my $verbose = 0; my $help; my @preserve_fields; +my $update_dateexpiry; +my $update_dateexpiry_from_today; GetOptions( 'c|confirm' => \$confirm, @@ -44,6 +46,8 @@ GetOptions( 'd|default=s' => \%defaults, 'o|overwrite' => \$overwrite_cardnumber, 'op|overwrite_passwords' => \$overwrite_passwords, + 'ue|update-expiration' => \$update_dateexpiry, + 'et|expiration-from-today' => \$update_dateexpiry_from_today, 'p|preserve-extended-attributes' => \$ext_preserve, 'pf|preserve-field=s' => \@preserve_fields, 'v|verbose+' => \$verbose, @@ -68,6 +72,8 @@ my $return = $Import->import_patrons( overwrite_passwords => $overwrite_passwords, preserve_extended_attributes => $ext_preserve, preserve_fields => \@preserve_fields, + update_dateexpiry => $update_dateexpiry + update_dateexpiry_from_today => $update_dateexpiry_from_today dry_run => !$confirm, } ); @@ -107,7 +113,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 +151,14 @@ Overwrite existing patrons with new data if a match is found Retain extended patron attributes for existing patrons being overwritten +=item B<-ue|--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<-et|--expiration-from-today> + +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 0d1eb8ff1e..46ac7701bc 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 => 173; +use Test::More tests => 175; use Test::Warn; use Test::Exception; use Encode qw( encode_utf8 ); @@ -28,6 +28,7 @@ use utf8; use Test::MockModule; use Koha::Database; use Koha::Patron::Relationships; +use Koha::DateUtils qw(dt_from_string); use File::Temp qw(tempfile tempdir); my $temp_dir = tempdir('Koha_patrons_import_test_XXXX', CLEANUP => 1, TMPDIR => 1); @@ -189,7 +190,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, update_dateexpiry_from_today => 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; @@ -211,6 +215,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, dt_from_string->add( months => 99, end_of_month => 'limit' )->ymd, "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..5207a6af91 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -120,6 +120,8 @@ 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 $update_dateexpiry_from_today = defined $input->param('update_dateexpiry_from_today') ? 1 : 0; my $return = $Import->import_patrons( { file => $handle, @@ -129,6 +131,8 @@ 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, + update_dateexpiry_from_today => $update_dateexpiry_from_today, } ); -- 2.30.1 (Apple Git-130)