Bug 10190: Update has_restricting_overdues to refer to circulation rules

This patch updates the Koha::Patron->has_restriction_overdues method to
refer to the migrated overdue rules.

We also correct an oversight in the method prior to this patch that
meant that not all overdues were being correctly checked for a
restriction.. we were incorrectly filtering to only overdues at the
current checkin branch.

Finally, we also take account of the itemtype level addition here too.

NOTE: There weren't any unit tests for this Koha::Patron method.. we
should add some here!

Sponsored-by: Glasgow Colleges Library Group <https://library.cityofglasgowcollege.ac.uk>
Signed-off-by: George Harkins <George.Harkins@cityofglasgowcollege.ac.uk>
Signed-off-by: Michaela <michaela.sieber@kit.edu>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Martin Renvoize 2024-08-08 14:52:31 +01:00 committed by Pedro Amorim
parent d2c575a93d
commit f056486560
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
2 changed files with 51 additions and 62 deletions

View File

@ -3047,7 +3047,7 @@ sub MarkIssueReturned {
if ( C4::Context->preference('AutoRemoveOverduesRestrictions') ne 'no' && $patron->is_debarred ) {
my $remove_restrictions =
C4::Context->preference('AutoRemoveOverduesRestrictions') eq 'when_no_overdue_causing_debarment'
? !$patron->has_restricting_overdues( { issue_branchcode => $issue_branchcode } )
? !$patron->has_restricting_overdues()
: !$patron->has_overdues;
if ( $remove_restrictions && $overdue_restrictions->count ) {
DelUniqueDebarment( { borrowernumber => $borrowernumber, type => 'OVERDUES' } );

View File

@ -55,7 +55,6 @@ use Koha::HoldGroups;
use Koha::ILL::Requests;
use Koha::ItemTypes;
use Koha::Old::Checkouts;
use Koha::OverdueRules;
use Koha::Patron::Attributes;
use Koha::Patron::Categories;
use Koha::Patron::Consents;
@ -1405,23 +1404,23 @@ sub has_overdues {
=head3 has_restricting_overdues
my $has_restricting_overdues = $patron->has_restricting_overdues({ issue_branchcode => $branchcode });
my $has_restricting_overdues = $patron->has_restricting_overdues();
Returns true if patron has overdues that would result in debarment.
=cut
sub has_restricting_overdues {
my ( $self, $params ) = @_;
$params //= {};
my $date = dt_from_string()->truncate( to => 'day' );
my ($self) = @_;
# 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.
#TODO: bail out/throw exception if $params->{issue_branchcode} not set?
my $debarred_delay = _get_overdue_debarred_delay( $params->{issue_branchcode}, $self->categorycode() );
return 0 unless defined $debarred_delay;
# Fetch overdues
my $overdues = $self->overdues;
# Short circuit if no overdues
return 0 unless $overdues->count;
my $categorycode = $self->categorycode();
my $calendar;
# Emulate the conditions in overdue_notices.pl.
# The overdue_notices-script effectively truncates both issues.date_due and current date
@ -1433,65 +1432,55 @@ sub has_restricting_overdues {
# the current date or later. We can emulate this query by instead of truncating both to days in the SQL-query,
# using the condition that date_due must be less then the current date truncated to days (time set to 00:00:00)
# offset by one day in the future.
my $date = dt_from_string()->truncate( to => 'day' )->add( days => 1 );
$date->add( days => 1 );
# Work from oldest overdue to newest
$overdues = $overdues->search( {}, { order_by => { '-desc' => 'me.date_due' } } );
my $now = dt_from_string();
my $calendar;
if ( C4::Context->preference('OverdueNoticeCalendar') ) {
$calendar = Koha::Calendar->new( branchcode => $params->{issue_branchcode} );
}
my ( $itemtype, $branchcode ) = ( "", "" );
while ( my $overdue = $overdues->next ) {
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
my $issues = $self->_result->issues->search( { date_due => { '<' => $dtf->format_datetime($date) } } );
my $now = dt_from_string();
# Short circuit if we're looking at the same branch and itemtype combination as last time as we've
# checked the oldest for this combination already
next if ( ( $overdue->branchcode eq $branchcode ) && ( $overdue->item->itemtype eq $itemtype ) );
while ( my $issue = $issues->next ) {
my $days_between =
C4::Context->preference('OverdueNoticeCalendar')
? $calendar->days_between( dt_from_string( $issue->date_due ), $now )->in_units('days')
: $now->delta_days( dt_from_string( $issue->date_due ) )->in_units('days');
if ( $days_between >= $debarred_delay ) {
return 1;
# Capture the current branchcode and itemtype
$branchcode = $overdue->branchcode;
$itemtype = $overdue->item->itemtype;
my $i = 0;
DELAY: while (1) {
$i++;
my $overdue_rules = Koha::CirculationRules->get_effective_rules(
{
rules => [ "overdue_$i" . '_delay', "overdue_$i" . '_restrict' ],
categorycode => $categorycode,
branchcode => $branchcode,
itemtype => $itemtype,
}
);
last DELAY if ( !defined( $overdue_rules->{ "overdue_$i" . '_delay' } ) );
next DELAY unless $overdue_rules->{ "overdue_$i" . '_restrict' };
if ( C4::Context->preference('OverdueNoticeCalendar') ) {
$calendar = Koha::Calendar->new( branchcode => $branchcode );
}
my $days_between =
C4::Context->preference('OverdueNoticeCalendar')
? $calendar->days_between( dt_from_string( $overdue->date_due ), $now )->in_units('days')
: $now->delta_days( dt_from_string( $overdue->date_due ) )->in_units('days');
if ( $days_between >= $overdue_rules->{ "overdue_$i" . '_delay' } ) {
return 1;
}
}
}
return 0;
}
=head3 _get_overdue_debarred_delay
my $delay = _get_overdue_debarred_delay( $branchcode, $categorycode );
Returns the first overdue delay value (delay1/delay2/delay3) from the
overduerules for the given branch and category where the corresponding
debarred flag is set. Falls back to default branch rules when no
branch-specific rule exists. Returns 0 if no debarring rule is found.
=cut
sub _get_overdue_debarred_delay {
my ( $branchcode, $categorycode ) = @_;
# We get default rules if there is no rule for this branch
my $rule = Koha::OverdueRules->find(
{
branchcode => $branchcode,
categorycode => $categorycode
}
)
|| Koha::OverdueRules->find(
{
branchcode => q{},
categorycode => $categorycode
}
);
if ($rule) {
return $rule->delay1 if $rule->debarred1;
return $rule->delay2 if $rule->debarred2;
return $rule->delay3 if $rule->debarred3;
}
}
=head3 update_lastseen
$patron->update_lastseen('activity');