From f2d2a7839ed8727ce603ed2b435b7fcae56f7f49 Mon Sep 17 00:00:00 2001 From: Stefan Berndtsson Date: Thu, 8 Feb 2018 15:54:44 +0100 Subject: [PATCH] Bug 29145: use overdues restrict delays when removing overdues restriction upon return How to test: 1) Run tests in t/db_dependent/Circulation/MarkIssueReturned.t Sponsored by: Gothenburg University Library Signed-off-by: Michaela Signed-off-by: Tomas Cohen Arazi --- C4/Circulation.pm | 12 +++++- Koha/Patron.pm | 42 ++++++++++++++++++- ...ue-debarment-removal-allow-unrestricted.pl | 14 +++++++ .../admin/preferences/circulation.pref | 6 +++ misc/cronjobs/overdue_notices.pl | 4 +- 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/odue-debarment-removal-allow-unrestricted.pl diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 4c8b51e3c7..9a5f9edff9 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2623,12 +2623,22 @@ sub MarkIssueReturned { $item->last_returned_by( $patron->borrowernumber )->store; } + # The reason this is here, and not in Koha::Patron->has_overdues() is + # to make sure it will not cause any side effects elsewhere, since this + # is only relevant for removal of debarments. + my $has_overdue_ignore_unrestricted = 0; + if(C4::Context->preference('ODueDebarmentRemovalAllowUnrestricted')) { + $has_overdue_ignore_unrestricted = 1; + } + # Remove any OVERDUES related debarment if the borrower has no overdues my $overdue_restrictions = $patron->restrictions->search({ type => 'OVERDUES' }); if ( C4::Context->preference('AutoRemoveOverduesRestrictions') && $patron->debarred - && !$patron->has_overdues && $overdue_restrictions->count + && !$patron->has_overdues({ + ignore_unrestricted => $has_overdue_ignore_unrestricted, + issue_branch => $issue->{'branchcode'} }) ) { DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' }); } diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 18e9d234e0..e39670e6f8 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1008,9 +1008,47 @@ Returns the number of patron's overdues =cut sub has_overdues { - my ($self) = @_; + my ($self, $params) = @_; + my $date = dt_from_string(); + + # If ignoring unrestricted overdues, calculate which delay value for + # overdue messages is set with restrictions. Then only include overdue + # issues older than that date when counting. + if($params->{ignore_unrestricted}) { + my $branchcode = $params->{issue_branchcode}; + my $date_offset = _get_overdue_restrict_delay($params->{issue_branchcode}, $self->categorycode()); + $date->subtract(days => $date_offset); + } + my $dtf = Koha::Database->new->schema->storage->datetime_parser; - return $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( dt_from_string() ) } })->count; + return $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( $date )} })->count; +} + +# Fetch first delayX value from overduerules where debarredX is set, or 0 for no delay +sub _get_overdue_restrict_delay { + my ($branchcode, $categorycode) = @_; + my $dbh = C4::Context->dbh(); + + my $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = ? AND categorycode = ?"; + + my $rqoverduerules = $dbh->prepare($query); + $rqoverduerules->execute($branchcode, $categorycode); + + # We get default rules if there is no rule for this branch + if($rqoverduerules->rows == 0){ + $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = '' AND categorycode = ?"; + + $rqoverduerules = $dbh->prepare($query); + $rqoverduerules->execute($categorycode); + } + + while ( my $overdue_rules = $rqoverduerules->fetchrow_hashref ) { + return $overdue_rules->{"delay1"} if($overdue_rules->{"debarred1"}); + return $overdue_rules->{"delay2"} if($overdue_rules->{"debarred2"}); + return $overdue_rules->{"delay3"} if($overdue_rules->{"debarred3"}); + } + + return 0; } =head3 track_login diff --git a/installer/data/mysql/atomicupdate/odue-debarment-removal-allow-unrestricted.pl b/installer/data/mysql/atomicupdate/odue-debarment-removal-allow-unrestricted.pl new file mode 100644 index 0000000000..3a019999d1 --- /dev/null +++ b/installer/data/mysql/atomicupdate/odue-debarment-removal-allow-unrestricted.pl @@ -0,0 +1,14 @@ +use Modern::Perl; + +return { + bug_number => "", + description => "Add system preference", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + # Do you stuffs here + $dbh->do(q{INSERT IGNORE INTO systempreferences (`variable`, `value`, `options`, `explanation`, `type`) VALUES ('ODueDebarmentRemovalAllowUnrestricted', '0', null, 'Allow removal of OVERDUES debarment when overdues still exist, but has not reached restricting delay', 'YesNo')}); + # Print useful stuff here + say $out "System preference added"; + }, +} diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref index ba28859e03..c7b2caf57b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -658,6 +658,12 @@ Circulation: 1: Store 0: "Don't store" - 'the last patron to return an item. This setting is independent of the opacreadinghistory and AnonymousPatron system preferences.' + - + - pref: ODueDebarmentRemovalAllowUnrestricted + choices: + yes: Allow + no: Do not allow + - removal of Overdue debarments when patron has overdue items but none are old enough to have reached restricting delay. Used in combination with AutoRemoveOverduesRestrictions. Holds policy: - - In the staff interface, split the holds queue into separate tables by diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl index 19e6aef27b..b772cfc9d3 100755 --- a/misc/cronjobs/overdue_notices.pl +++ b/misc/cronjobs/overdue_notices.pl @@ -489,8 +489,8 @@ END_SQL my $rqoverduerules = $dbh->prepare($query); $rqoverduerules->execute($branchcode, @myborcat, @myborcatout); - - # We get default rules is there is no rule for this branch + + # We get default rules if there is no rule for this branch if($rqoverduerules->rows == 0){ $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = '' "; $query .= " AND categorycode IN (".join( ',' , ('?') x @myborcat ).") " if (@myborcat); -- 2.39.5