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 <david@davidnind.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Kyle Hall 2022-08-03 07:13:49 -04:00 committed by Tomas Cohen Arazi
parent 883b57b0dd
commit e4e453d5df
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
5 changed files with 42 additions and 5 deletions

View File

@ -79,6 +79,8 @@ sub import_patrons {
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 $schema = Koha::Database->new->schema;
$schema->storage->txn_begin if $dry_run;
@ -171,7 +173,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 ) if $update_dateexpiry;
my $borrowernumber;
my ( $member, $patron );

View File

@ -323,7 +323,15 @@
<li>
<input class="overwrite_passwords" type="checkbox" id="overwrite_passwords" name="overwrite_passwords" disabled/>
<label class="overwrite_passwords" for="overwrite_passwords">Replace patron passwords with those in the file (blank passwords will be ignored)</label>
</li
</li>
<li>
<input class="update_dateexpiry" type="checkbox" id="update_dateexpiry" name="update_dateexpiry" disabled/>
<label class="update_dateexpiry" for="update_dateexpiry">Update existing patron expiration dates.</label>
</li>
<li>
<input class="update_dateexpiry_from_today" type="checkbox" id="update_dateexpiry_from_today" name="update_dateexpiry_from_today" disabled/>
<label class="update_dateexpiry_from_today" for="update_dateexpiry_from_today">Update patron's expiration date based on the current date instead of the patron's enrollment date</label>
</li>
</ul>
</li>
</ol>
@ -443,9 +451,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);
});
</script>
[% END %]

View File

@ -37,6 +37,8 @@ my $confirm;
my $verbose = 0;
my $help;
my @preserve_fields;
my $update_dateexpiry;
my $update_dateexpiry_from_today;
GetOptions(
'c|confirm' => \$confirm,
@ -45,6 +47,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,
'en|email-new' => \$welcome_new,
'p|preserve-extended-attributes' => \$ext_preserve,
'pf|preserve-field=s' => \@preserve_fields,
@ -70,6 +74,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
send_welcome => $welcome_new,
dry_run => !$confirm,
}
@ -110,7 +116,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 <column>] [--preserve-extended-attributes] [--verbose]
import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve_field <column>] [--preserve-extended-attributes] [--update-expiration] [--verbose]
=head1 OPTIONS
@ -152,6 +158,14 @@ Retain extended patron attributes for existing patrons being overwritten
Send the WELCOME notice email to new users
=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

View File

@ -18,7 +18,7 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 174;
use Test::More tests => 176;
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" );

View File

@ -114,6 +114,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,
@ -123,6 +125,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,
send_welcome => $welcome_new,
}
);