View | Details | Raw Unified | Return to bug 10190
Collapse All | Expand All

(-)a/C4/Circulation.pm (-1 / +1 lines)
Lines 2868-2874 sub MarkIssueReturned { Link Here
2868
            if ( C4::Context->preference('AutoRemoveOverduesRestrictions') ne 'no' && $patron->is_debarred ) {
2868
            if ( C4::Context->preference('AutoRemoveOverduesRestrictions') ne 'no' && $patron->is_debarred ) {
2869
                my $remove_restrictions =
2869
                my $remove_restrictions =
2870
                    C4::Context->preference('AutoRemoveOverduesRestrictions') eq 'when_no_overdue_causing_debarment'
2870
                    C4::Context->preference('AutoRemoveOverduesRestrictions') eq 'when_no_overdue_causing_debarment'
2871
                    ? !$patron->has_restricting_overdues( { issue_branchcode => $issue_branchcode } )
2871
                    ? !$patron->has_restricting_overdues()
2872
                    : !$patron->has_overdues;
2872
                    : !$patron->has_overdues;
2873
                if ( $remove_restrictions && $overdue_restrictions->count ) {
2873
                if ( $remove_restrictions && $overdue_restrictions->count ) {
2874
                    DelUniqueDebarment( { borrowernumber => $borrowernumber, type => 'OVERDUES' } );
2874
                    DelUniqueDebarment( { borrowernumber => $borrowernumber, type => 'OVERDUES' } );
(-)a/Koha/Patron.pm (-52 / +50 lines)
Lines 51-57 use Koha::HoldGroups; Link Here
51
use Koha::ILL::Requests;
51
use Koha::ILL::Requests;
52
use Koha::ItemTypes;
52
use Koha::ItemTypes;
53
use Koha::Old::Checkouts;
53
use Koha::Old::Checkouts;
54
use Koha::OverdueRules;
55
use Koha::Patron::Attributes;
54
use Koha::Patron::Attributes;
56
use Koha::Patron::Categories;
55
use Koha::Patron::Categories;
57
use Koha::Patron::Consents;
56
use Koha::Patron::Consents;
Lines 1221-1243 sub has_overdues { Link Here
1221
1220
1222
=head3 has_restricting_overdues
1221
=head3 has_restricting_overdues
1223
1222
1224
my $has_restricting_overdues = $patron->has_restricting_overdues({ issue_branchcode => $branchcode });
1223
  my $has_restricting_overdues = $patron->has_restricting_overdues();
1225
1224
1226
Returns true if patron has overdues that would result in debarment.
1225
Returns true if patron has overdues that would result in debarment.
1227
1226
1228
=cut
1227
=cut
1229
1228
1230
sub has_restricting_overdues {
1229
sub has_restricting_overdues {
1231
    my ( $self, $params ) = @_;
1230
    my ($self) = @_;
1232
    $params //= {};
1231
1233
    my $date = dt_from_string()->truncate( to => 'day' );
1232
    # Fetch overdues
1233
    my $overdues = $self->overdues;
1234
1235
    # Short circuit if no overdues
1236
    return 0 unless $overdues->count;
1234
1237
1235
    # If ignoring unrestricted overdues, calculate which delay value for
1238
    my $categorycode = $self->categorycode();
1236
    # overdue messages is set with restrictions. Then only include overdue
1239
    my $calendar;
1237
    # issues older than that date when counting.
1238
    #TODO: bail out/throw exception if $params->{issue_branchcode} not set?
1239
    my $debarred_delay = _get_overdue_debarred_delay( $params->{issue_branchcode}, $self->categorycode() );
1240
    return 0 unless defined $debarred_delay;
1241
1240
1242
    # Emulate the conditions in overdue_notices.pl.
1241
    # Emulate the conditions in overdue_notices.pl.
1243
    # The overdue_notices-script effectively truncates both issues.date_due and current date
1242
    # The overdue_notices-script effectively truncates both issues.date_due and current date
Lines 1249-1301 sub has_restricting_overdues { Link Here
1249
    # the current date or later. We can emulate this query by instead of truncating both to days in the SQL-query,
1248
    # the current date or later. We can emulate this query by instead of truncating both to days in the SQL-query,
1250
    # using the condition that date_due must be less then the current date truncated to days (time set to 00:00:00)
1249
    # using the condition that date_due must be less then the current date truncated to days (time set to 00:00:00)
1251
    # offset by one day in the future.
1250
    # offset by one day in the future.
1251
    my $date = dt_from_string()->truncate( to => 'day' )->add( days => 1 );
1252
1252
1253
    $date->add( days => 1 );
1253
    # Work from oldest overdue to newest
1254
    $overdues = $overdues->search( {}, { order_by => { '-desc' => 'me.date_due' } } );
1255
    my $now = dt_from_string();
1254
1256
1255
    my $calendar;
1257
    my ( $itemtype, $branchcode ) = ( "", "" );
1256
    if ( C4::Context->preference('OverdueNoticeCalendar') ) {
1258
    while ( my $overdue = $overdues->next ) {
1257
        $calendar = Koha::Calendar->new( branchcode => $params->{issue_branchcode} );
1258
    }
1259
1259
1260
    my $dtf    = Koha::Database->new->schema->storage->datetime_parser;
1260
        # Short circuit if we're looking at the same branch and itemtype combination as last time as we've
1261
    my $issues = $self->_result->issues->search( { date_due => { '<' => $dtf->format_datetime($date) } } );
1261
        # checked the oldest for this combination already
1262
    my $now    = dt_from_string();
1262
        next if ( ( $overdue->branchcode eq $branchcode ) && ( $overdue->item->itemtype eq $itemtype ) );
1263
1264
    while ( my $issue = $issues->next ) {
1265
        my $days_between =
1266
            C4::Context->preference('OverdueNoticeCalendar')
1267
            ? $calendar->days_between( dt_from_string( $issue->date_due ), $now )->in_units('days')
1268
            : $now->delta_days( dt_from_string( $issue->date_due ) )->in_units('days');
1269
        if ( $days_between >= $debarred_delay ) {
1270
            return 1;
1271
        }
1272
    }
1273
    return 0;
1274
}
1275
1263
1276
# Fetch first delayX value from overduerules where debarredX is set, or 0 for no delay
1264
        # Capture the current branchcode and itemtype
1277
sub _get_overdue_debarred_delay {
1265
        $branchcode = $overdue->branchcode;
1278
    my ( $branchcode, $categorycode ) = @_;
1266
        $itemtype   = $overdue->item->itemtype;
1279
1267
1280
    # We get default rules if there is no rule for this branch
1268
        my $i = 0;
1281
    my $rule = Koha::OverdueRules->find(
1269
    DELAY: while (1) {
1282
        {
1270
            $i++;
1283
            branchcode   => $branchcode,
1271
            my $overdue_rules = Koha::CirculationRules->get_effective_rules(
1284
            categorycode => $categorycode
1272
                {
1285
        }
1273
                    rules        => [ "overdue_$i" . '_delay', "overdue_$i" . '_restrict' ],
1286
        )
1274
                    categorycode => $categorycode,
1287
        || Koha::OverdueRules->find(
1275
                    branchcode   => $branchcode,
1288
        {
1276
                    itemtype     => $itemtype,
1289
            branchcode   => q{},
1277
                }
1290
            categorycode => $categorycode
1278
            );
1291
        }
1292
        );
1293
1279
1294
    if ($rule) {
1280
            last DELAY if ( !defined( $overdue_rules->{ "overdue_$i" . '_delay' } ) );
1295
        return $rule->delay1 if $rule->debarred1;
1281
1296
        return $rule->delay2 if $rule->debarred2;
1282
            next DELAY unless $overdue_rules->{ "overdue_$i" . '_restrict' };
1297
        return $rule->delay3 if $rule->debarred3;
1283
1284
            if ( C4::Context->preference('OverdueNoticeCalendar') ) {
1285
                $calendar = Koha::Calendar->new( branchcode => $branchcode );
1286
            }
1287
1288
            my $days_between =
1289
                C4::Context->preference('OverdueNoticeCalendar')
1290
                ? $calendar->days_between( dt_from_string( $overdue->date_due ), $now )->in_units('days')
1291
                : $now->delta_days( dt_from_string( $overdue->date_due ) )->in_units('days');
1292
            if ( $days_between >= $overdue_rules->{ "overdue_$i" . '_delay' } ) {
1293
                return 1;
1294
            }
1295
        }
1298
    }
1296
    }
1297
    return 0;
1299
}
1298
}
1300
1299
1301
=head3 update_lastseen
1300
=head3 update_lastseen
1302
- 

Return to bug 10190