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

(-)a/C4/Circulation.pm (-1 / +1 lines)
Lines 2673-2679 sub MarkIssueReturned { Link Here
2673
        if ( C4::Context->preference('AutoRemoveOverduesRestrictions') ne 'no' && $patron->is_debarred ) {
2673
        if ( C4::Context->preference('AutoRemoveOverduesRestrictions') ne 'no' && $patron->is_debarred ) {
2674
            my $remove_restrictions =
2674
            my $remove_restrictions =
2675
                C4::Context->preference('AutoRemoveOverduesRestrictions') eq 'when_no_overdue_causing_debarment'
2675
                C4::Context->preference('AutoRemoveOverduesRestrictions') eq 'when_no_overdue_causing_debarment'
2676
                ? !$patron->has_restricting_overdues( { issue_branchcode => $issue_branchcode } )
2676
                ? !$patron->has_restricting_overdues()
2677
                : !$patron->has_overdues;
2677
                : !$patron->has_overdues;
2678
            if ( $remove_restrictions && $overdue_restrictions->count ) {
2678
            if ( $remove_restrictions && $overdue_restrictions->count ) {
2679
                DelUniqueDebarment( { borrowernumber => $borrowernumber, type => 'OVERDUES' } );
2679
                DelUniqueDebarment( { borrowernumber => $borrowernumber, type => 'OVERDUES' } );
(-)a/Koha/Patron.pm (-52 / +49 lines)
Lines 46-52 use Koha::Exceptions; Link Here
46
use Koha::Exceptions::Password;
46
use Koha::Exceptions::Password;
47
use Koha::Holds;
47
use Koha::Holds;
48
use Koha::Old::Checkouts;
48
use Koha::Old::Checkouts;
49
use Koha::OverdueRules;
50
use Koha::Patron::Attributes;
49
use Koha::Patron::Attributes;
51
use Koha::Patron::Categories;
50
use Koha::Patron::Categories;
52
use Koha::Patron::Consents;
51
use Koha::Patron::Consents;
Lines 1120-1142 sub has_overdues { Link Here
1120
1119
1121
=head3 has_restricting_overdues
1120
=head3 has_restricting_overdues
1122
1121
1123
my $has_restricting_overdues = $patron->has_restricting_overdues({ issue_branchcode => $branchcode });
1122
  my $has_restricting_overdues = $patron->has_restricting_overdues();
1124
1123
1125
Returns true if patron has overdues that would result in debarment.
1124
Returns true if patron has overdues that would result in debarment.
1126
1125
1127
=cut
1126
=cut
1128
1127
1129
sub has_restricting_overdues {
1128
sub has_restricting_overdues {
1130
    my ( $self, $params ) = @_;
1129
    my ($self) = @_;
1131
    $params //= {};
1132
    my $date = dt_from_string()->truncate( to => 'day' );
1133
1130
1134
    # If ignoring unrestricted overdues, calculate which delay value for
1131
    # Fetch overdues
1135
    # overdue messages is set with restrictions. Then only include overdue
1132
    my $overdues = $self->overdues;
1136
    # issues older than that date when counting.
1133
1137
    #TODO: bail out/throw exception if $params->{issue_branchcode} not set?
1134
    # Short circuit if no overdues
1138
    my $debarred_delay = _get_overdue_debarred_delay( $params->{issue_branchcode}, $self->categorycode() );
1135
    return 0 unless $overdues->count;
1139
    return 0 unless defined $debarred_delay;
1136
1137
    my $categorycode = $self->categorycode();
1138
    my $calendar;
1140
1139
1141
    # Emulate the conditions in overdue_notices.pl.
1140
    # Emulate the conditions in overdue_notices.pl.
1142
    # The overdue_notices-script effectively truncates both issues.date_due and current date
1141
    # The overdue_notices-script effectively truncates both issues.date_due and current date
Lines 1148-1201 sub has_restricting_overdues { Link Here
1148
    # the current date or later. We can emulate this query by instead of truncating both to days in the SQL-query,
1147
    # the current date or later. We can emulate this query by instead of truncating both to days in the SQL-query,
1149
    # using the condition that date_due must be less then the current date truncated to days (time set to 00:00:00)
1148
    # using the condition that date_due must be less then the current date truncated to days (time set to 00:00:00)
1150
    # offset by one day in the future.
1149
    # offset by one day in the future.
1150
    my $date = dt_from_string()->truncate( to => 'day' )->add( days => 1 );
1151
1151
1152
    $date->add( days => 1 );
1152
    # Work from oldest overdue to newest
1153
    $overdues = $overdues->search( {}, { order_by => { '-desc' => 'me.date_due' } } );
1154
    my $now = dt_from_string();
1153
1155
1154
    my $calendar;
1156
    my ( $itemtype, $branchcode ) = ( "", "" );
1155
    if ( C4::Context->preference('OverdueNoticeCalendar') ) {
1157
    while ( my $overdue = $overdues->next ) {
1156
        $calendar = Koha::Calendar->new( branchcode => $params->{issue_branchcode} );
1157
    }
1158
1158
1159
    my $dtf    = Koha::Database->new->schema->storage->datetime_parser;
1159
        # Short circuit if we're looking at the same branch and itemtype combination as last time as we've
1160
    my $issues = $self->_result->issues->search( { date_due => { '<' => $dtf->format_datetime($date) } } );
1160
        # checked the oldest for this combination already
1161
    my $now    = dt_from_string();
1161
        next if ( ( $overdue->branchcode eq $branchcode ) && ( $overdue->item->itemtype eq $itemtype ) );
1162
1162
1163
    while ( my $issue = $issues->next ) {
1163
        # Capture the current branchcode and itemtype
1164
        my $days_between =
1164
        $branchcode = $overdue->branchcode;
1165
            C4::Context->preference('OverdueNoticeCalendar')
1165
        $itemtype   = $overdue->item->itemtype;
1166
            ? $calendar->days_between( dt_from_string( $issue->date_due ), $now )->in_units('days')
1167
            : $now->delta_days( dt_from_string( $issue->date_due ) )->in_units('days');
1168
        if ( $days_between >= $debarred_delay ) {
1169
            return 1;
1170
        }
1171
    }
1172
    return 0;
1173
}
1174
1166
1175
# Fetch first delayX value from overduerules where debarredX is set, or 0 for no delay
1167
        my $i = 0;
1176
sub _get_overdue_debarred_delay {
1168
    DELAY: while (1) {
1177
    my ( $branchcode, $categorycode ) = @_;
1169
            $i++;
1178
    my $dbh = C4::Context->dbh();
1170
            my $overdue_rules = Koha::CirculationRules->get_effective_rules(
1171
                {
1172
                    rules        => [ "overdue_$i" . '_delay', "overdue_$i" . '_restrict' ],
1173
                    categorycode => $categorycode,
1174
                    branchcode   => $branchcode,
1175
                    itemtype     => $itemtype,
1176
                }
1177
            );
1179
1178
1180
    # We get default rules if there is no rule for this branch
1179
            last DELAY if ( !defined( $overdue_rules->{ "overdue_$i" . '_delay' } ) );
1181
    my $rule = Koha::OverdueRules->find(
1182
        {
1183
            branchcode   => $branchcode,
1184
            categorycode => $categorycode
1185
        }
1186
        )
1187
        || Koha::OverdueRules->find(
1188
        {
1189
            branchcode   => q{},
1190
            categorycode => $categorycode
1191
        }
1192
        );
1193
1180
1194
    if ($rule) {
1181
            next DELAY unless $overdue_rules->{ "overdue_$i" . '_restrict' };
1195
        return $rule->delay1 if $rule->debarred1;
1182
1196
        return $rule->delay2 if $rule->debarred2;
1183
            if ( C4::Context->preference('OverdueNoticeCalendar') ) {
1197
        return $rule->delay3 if $rule->debarred3;
1184
                $calendar = Koha::Calendar->new( branchcode => $branchcode );
1185
            }
1186
1187
            my $days_between =
1188
                C4::Context->preference('OverdueNoticeCalendar')
1189
                ? $calendar->days_between( dt_from_string( $overdue->date_due ), $now )->in_units('days')
1190
                : $now->delta_days( dt_from_string( $overdue->date_due ) )->in_units('days');
1191
            if ( $days_between >= $overdue_rules->{ "overdue_$i" . '_delay' } ) {
1192
                return 1;
1193
            }
1194
        }
1198
    }
1195
    }
1196
    return 0;
1199
}
1197
}
1200
1198
1201
=head3 update_lastseen
1199
=head3 update_lastseen
1202
- 

Return to bug 10190