From b89d744e868cd93632d6b84f5f39116a475d342e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 25 Aug 2023 17:37:46 +0000 Subject: [PATCH] Bug 34621: implement Patron import option to 'Renew existing patrons' 'from the current membership expiry date' Test Plan: 1) Test importing a patron with the "from the current membership expiry date" option, note it does not work 2) Apply this patch 3) Restart all the things! 4) Re-test, note the expiration was renewed from the patron's current expiration date! Signed-off-by: David Nind Rebased-by: Victor Grousset/tuxayo Signed-off-by: Marcel de Rooy Signed-off-by: Katrin Fischer --- Koha/Patrons/Import.pm | 31 ++++++++++++++++----------- misc/import_patrons.pl | 32 ++++++++++++++++++---------- t/db_dependent/Koha/Patrons/Import.t | 14 +++++++++++- tools/import_borrowers.pl | 21 +++++++++--------- 4 files changed, 64 insertions(+), 34 deletions(-) diff --git a/Koha/Patrons/Import.pm b/Koha/Patrons/Import.pm index d6e0846cad..5c8dc6b876 100644 --- a/Koha/Patrons/Import.pm +++ b/Koha/Patrons/Import.pm @@ -69,18 +69,20 @@ 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 $send_welcome = $params->{send_welcome}; - my $extended = C4::Context->preference('ExtendedPatronAttributes'); - my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); - my $update_dateexpiry = $params->{update_dateexpiry}; - my $update_dateexpiry_from_today = $params->{update_dateexpiry_from_today}; + 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 $send_welcome = $params->{send_welcome}; + my $update_dateexpiry = $params->{update_dateexpiry}; + my $update_dateexpiry_from_today = $params->{update_dateexpiry_from_today}; + my $update_dateexpiry_from_existing = $params->{update_dateexpiry_from_existing}; + + my $extended = C4::Context->preference('ExtendedPatronAttributes'); + my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); my $schema = Koha::Database->new->schema; $schema->storage->txn_begin if $dry_run; @@ -205,6 +207,11 @@ sub import_patrons { } } + if ( $patron && $update_dateexpiry_from_existing ) { + $patron->dateexpiry( Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $patron->dateexpiry ) ); + delete $borrower{dateexpiry}; + } + my $is_new = 0; if ($patron) { $member = $patron->unblessed; diff --git a/misc/import_patrons.pl b/misc/import_patrons.pl index a4affb801c..964b2a1ce9 100755 --- a/misc/import_patrons.pl +++ b/misc/import_patrons.pl @@ -40,6 +40,7 @@ my $help; my @preserve_fields; my $update_dateexpiry; my $update_dateexpiry_from_today; +my $update_dateexpiry_from_existing; my $command_line_options = join( " ", @ARGV ); @@ -52,6 +53,7 @@ GetOptions( 'op|overwrite_passwords' => \$overwrite_passwords, 'ue|update-expiration' => \$update_dateexpiry, 'et|expiration-from-today' => \$update_dateexpiry_from_today, + 'ee|expiration-from-existing' => \$update_dateexpiry_from_existing, 'en|email-new' => \$welcome_new, 'p|preserve-extended-attributes' => \$ext_preserve, 'pf|preserve-field=s' => \@preserve_fields, @@ -60,6 +62,7 @@ GetOptions( ) or pod2usage(2); pod2usage(1) if $help; +pod2usage(q|--ee and --et are mutually exclusive|) if $update_dateexpiry_from_today && $update_dateexpiry_from_existing; pod2usage(q|--file is required|) unless $csv_file; pod2usage(q|--matchpoint is required|) unless $matchpoint; @@ -74,17 +77,18 @@ open( $handle, "<", $csv_file ) or die $!; my $return = $Import->import_patrons( { - file => $handle, - defaults => \%defaults, - matchpoint => $matchpoint, - overwrite_cardnumber => $overwrite_cardnumber, - 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, - send_welcome => $welcome_new, - dry_run => !$confirm, + file => $handle, + defaults => \%defaults, + matchpoint => $matchpoint, + overwrite_cardnumber => $overwrite_cardnumber, + 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, + update_dateexpiry_from_existing => $update_dateexpiry_from_existing, + send_welcome => $welcome_new, + dry_run => !$confirm, } ); @@ -189,6 +193,12 @@ If a matching patron is found, extend the expiration date of their account using =item B<-et|--expiration-from-today> If a matching patron is found, extend the expiration date of their account using today's date as the base +Cannot by used in conjunction with --expiration-from-existing + +=item B<-ee|--expiration-from-existing> + +If a matching patron is found, extend the expiration date of their account using the patron's current expiration date as the base +Cannot by used in conjunction with --expiration-from-today =item B<-v|--verbose> diff --git a/t/db_dependent/Koha/Patrons/Import.t b/t/db_dependent/Koha/Patrons/Import.t index dd19bbe16a..e9afc42b50 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 => 178; +use Test::More tests => 180; use Test::Warn; use Test::Exception; use Encode qw( encode_utf8 ); @@ -220,6 +220,18 @@ is( $patron_3c->dateexpiry, dt_from_string->add( months => 99, end_of_month => ' is( $patron_3c->surname, "Nancy2", "Surname field is preserved from original" ); is( $patron_3c->firstname, "Jenkins", "Firstname field is overwritten" ); +# test update_dateexpiry_from_current +my $filename_3d = make_csv($temp_dir, $csv_headers, $csv_one_line_b); +open(my $handle_3d, "<", $filename_3d) or die "cannot open < $filename_3: $!"; +$patron_3c->dateexpiry('1980-01-01')->store(); +my $params_3d = { file => $handle_3d, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ], update_dateexpiry => 1, update_dateexpiry_from_existing => 1 }; +warning_is { $patrons_import->import_patrons($params_3d) } + undef, + "No warning raised by import_patrons"; +$patron_3c = Koha::Patrons->find({ cardnumber => '1000' }); +is( $patron_3c->dateexpiry, '1988-04-01', "Expiration date is correct with update_dateexpiry_from_existing => 1" ); +# /test update_dateexpiry_from_current + # Given ... valid file handle, good matchpoint that does not match and conflicting userid. my $filename_3b = make_csv($temp_dir, $csv_headers, $csv_one_line_a); open(my $handle_3b, "<", $filename_3b) or die "cannot open < $filename_3: $!"; diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index 540a933b65..048d053d47 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -113,16 +113,17 @@ if ( $op eq 'cud-import' && $uploadborrowers && length($uploadborrowers) > 0 ) { my $update_dateexpiry = $input->param('update_dateexpiry'); my $return = $Import->import_patrons( { - file => $handle, - defaults => \%defaults, - matchpoint => $matchpoint, - 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, + file => $handle, + defaults => \%defaults, + matchpoint => $matchpoint, + 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, + update_dateexpiry_from_existing => $update_dateexpiry eq "dateexpiry" ? 1 : 0, + send_welcome => $welcome_new, } ); -- 2.39.5