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

(-)a/C4/Circulation.pm (-7 / +6 lines)
Lines 841-847 sub CanBookBeIssued { Link Here
841
        $issuingimpossible{DEBARRED} = 1;
841
        $issuingimpossible{DEBARRED} = 1;
842
    }
842
    }
843
843
844
    if ( $patron->is_expired ) {
844
    if ( $patron->is_expired({ cache => 1 }) ) {
845
        $issuingimpossible{EXPIRED} = 1;
845
        $issuingimpossible{EXPIRED} = 1;
846
    }
846
    }
847
847
Lines 927-933 sub CanBookBeIssued { Link Here
927
        else {
927
        else {
928
            $issuingimpossible{USERBLOCKEDWITHENDDATE} = $debarred_date;
928
            $issuingimpossible{USERBLOCKEDWITHENDDATE} = $debarred_date;
929
        }
929
        }
930
    } elsif ( my $num_overdues = $patron->has_overdues ) {
930
    } elsif ( my $num_overdues = $patron->has_overdues({ cache => 1 }) ) {
931
        ## patron has outstanding overdue loans
931
        ## patron has outstanding overdue loans
932
        if ( C4::Context->preference("OverduesBlockCirc") eq 'block'){
932
        if ( C4::Context->preference("OverduesBlockCirc") eq 'block'){
933
            $issuingimpossible{USERBLOCKEDOVERDUE} = $num_overdues;
933
            $issuingimpossible{USERBLOCKEDOVERDUE} = $num_overdues;
Lines 1737-1743 sub AddIssue { Link Here
1737
                if ( $accumulate_charge > 0 ) {
1737
                if ( $accumulate_charge > 0 ) {
1738
                    AddIssuingCharge( $issue, $accumulate_charge, 'RENT_DAILY' );
1738
                    AddIssuingCharge( $issue, $accumulate_charge, 'RENT_DAILY' );
1739
                    $charge += $accumulate_charge;
1739
                    $charge += $accumulate_charge;
1740
                    $item_unblessed->{charge} = $charge;
1741
                }
1740
                }
1742
            }
1741
            }
1743
1742
Lines 2542-2548 sub MarkIssueReturned { Link Here
2542
        # Remove any OVERDUES related debarment if the borrower has no overdues
2541
        # Remove any OVERDUES related debarment if the borrower has no overdues
2543
        if ( C4::Context->preference('AutoRemoveOverduesRestrictions')
2542
        if ( C4::Context->preference('AutoRemoveOverduesRestrictions')
2544
          && $patron->debarred
2543
          && $patron->debarred
2545
          && !$patron->has_overdues
2544
          && !$patron->has_overdues({ cache => 1 })
2546
          && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) }
2545
          && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) }
2547
        ) {
2546
        ) {
2548
            DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' });
2547
            DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' });
Lines 2898-2904 sub CanBookBeRenewed { Link Here
2898
        my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
2897
        my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
2899
        $patron         = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful?
2898
        $patron         = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful?
2900
        my $restricted  = $patron->is_debarred;
2899
        my $restricted  = $patron->is_debarred;
2901
        my $hasoverdues = $patron->has_overdues;
2900
        my $hasoverdues = $patron->has_overdues({ cache => 1 });
2902
2901
2903
        if ( $restricted and $restrictionblockrenewing ) {
2902
        if ( $restricted and $restrictionblockrenewing ) {
2904
            return ( 0, 'restriction');
2903
            return ( 0, 'restriction');
Lines 3173-3179 sub AddRenewal { Link Here
3173
        # Remove any OVERDUES related debarment if the borrower has no overdues
3172
        # Remove any OVERDUES related debarment if the borrower has no overdues
3174
        if ( $patron
3173
        if ( $patron
3175
          && $patron->is_debarred
3174
          && $patron->is_debarred
3176
          && ! $patron->has_overdues
3175
          && ! $patron->has_overdues({ cache => 1 })
3177
          && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) }
3176
          && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) }
3178
        ) {
3177
        ) {
3179
            DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' });
3178
            DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' });
Lines 4385-4391 sub _CanBookBeAutoRenewed { Link Here
4385
        }
4384
        }
4386
    );
4385
    );
4387
4386
4388
    if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) {
4387
    if ( $patron->is_expired({ cache => 1 }) && $patron->category->effective_BlockExpiredPatronOpacActions ) {
4389
        return 'auto_account_expired';
4388
        return 'auto_account_expired';
4390
    }
4389
    }
4391
4390
(-)a/Koha/Patron.pm (-7 / +60 lines)
Lines 762-767 sub is_debarred { Link Here
762
    return;
762
    return;
763
}
763
}
764
764
765
=head3 dateexpiry
766
767
Override Patron::dateexpiry to invalidate Patron::is_expired cache.
768
769
=cut
770
771
sub dateexpiry {
772
    my $self = shift @_;
773
    if (@_) {
774
        $self->{is_expired_cache} = undef;
775
    }
776
    return $self->SUPER::dateexpiry(@_);
777
}
778
779
=head3 set
780
781
Override Patron::set to invalidate Patron::is_expired cache if dateexpiry is set.
782
783
=cut
784
785
sub set {
786
    my $self = shift @_;
787
    my ($properties) = @_;
788
    if (exists $properties->{dateexpiry}) {
789
        $self->{is_expired_cache} = undef;
790
    }
791
    return $self->SUPER::set(@_);
792
}
793
765
=head3 is_expired
794
=head3 is_expired
766
795
767
my $is_expired = $patron->is_expired;
796
my $is_expired = $patron->is_expired;
Lines 771-781 Returns 1 if the patron is expired or 0; Link Here
771
=cut
800
=cut
772
801
773
sub is_expired {
802
sub is_expired {
774
    my ($self) = @_;
803
    my ($self, $params) = @_;
775
    return 0 unless $self->dateexpiry;
804
    $params //= {};
776
    return 0 if $self->dateexpiry =~ '^9999';
805
    if ($params->{cache} && defined $self->{is_expired_cache}) {
777
    return 1 if dt_from_string( $self->dateexpiry ) < dt_from_string->truncate( to => 'day' );
806
        return $self->{is_expired_cache};
778
    return 0;
807
    }
808
    my $is_expired =
809
        $self->dateexpiry &&
810
        $self->dateexpiry !~ '^9999' &&
811
        dt_from_string( $self->dateexpiry ) < dt_from_string->truncate( to => 'day' );
812
    $is_expired = $is_expired ? 1 : 0;
813
    if ($params->{cache}) {
814
        $self->{is_expired_cache} = $is_expired;
815
    }
816
    else {
817
        $self->{is_expired_cache} = undef;
818
    }
819
    return $is_expired;
779
}
820
}
780
821
781
=head3 password_expired
822
=head3 password_expired
Lines 979-987 Returns the number of patron's overdues Link Here
979
=cut
1020
=cut
980
1021
981
sub has_overdues {
1022
sub has_overdues {
982
    my ($self) = @_;
1023
    my ($self, $params) = @_;
1024
    $params //= {};
1025
1026
    if ($params->{cache} && defined $self->{has_overdues_cache}) {
1027
        return $self->{has_overdues_cache};
1028
    }
983
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
1029
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
984
    return $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( dt_from_string() ) } })->count;
1030
    my $has_overdues = $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( dt_from_string() ) } })->count;
1031
    if ($params->{cache}) {
1032
        $self->{has_overdues_cache} = $has_overdues;
1033
    }
1034
    else {
1035
        $self->{has_overdues_cache} = undef;
1036
    }
1037
    return $has_overdues;
985
}
1038
}
986
1039
987
=head3 track_login
1040
=head3 track_login
(-)a/t/db_dependent/Koha/Patrons.t (-7 / +35 lines)
Lines 264-270 subtest 'siblings' => sub { Link Here
264
};
264
};
265
265
266
subtest 'has_overdues' => sub {
266
subtest 'has_overdues' => sub {
267
    plan tests => 3;
267
    plan tests => 10;
268
268
269
    my $item_1 = $builder->build_sample_item;
269
    my $item_1 = $builder->build_sample_item;
270
    my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
270
    my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
Lines 273-298 subtest 'has_overdues' => sub { Link Here
273
    my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
273
    my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
274
    my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
274
    my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
275
    is( $retrieved_patron->has_overdues, 0, );
275
    is( $retrieved_patron->has_overdues, 0, );
276
    # Cache is set
277
    is( $retrieved_patron->has_overdues({ cache => 1}), 0, );
278
    is ( $retrieved_patron->{has_overdues_cache}, 0, 'has_overdues cache has been set');
279
    # Cache is used
280
    is( $retrieved_patron->has_overdues({ cache => 1}), 0, );
281
276
    $issue->delete();
282
    $issue->delete();
277
    my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
283
    my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
278
    $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
284
    $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
279
    $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
285
    $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
280
    is( $retrieved_patron->has_overdues, 1, );
286
    is( $retrieved_patron->has_overdues, 1, );
287
    ok( !defined $retrieved_patron->{has_overdues_cache}, 'has_overdues cache has been invalidated' );
288
    # Cache is set
289
    is( $retrieved_patron->has_overdues({ cache => 1}), 1, );
290
    is ( $retrieved_patron->{has_overdues_cache}, 1, 'has_overdues cache has been set' );
291
    # Cache is used
292
    is( $retrieved_patron->has_overdues({ cache => 1}), 1, );
293
281
    $issue->delete();
294
    $issue->delete();
282
};
295
};
283
296
284
subtest 'is_expired' => sub {
297
subtest 'is_expired' => sub {
285
    plan tests => 4;
298
    plan tests => 13;
286
    my $patron = $builder->build({ source => 'Borrower' });
299
    my $patron = $builder->build({ source => 'Borrower' });
287
    $patron = Koha::Patrons->find( $patron->{borrowernumber} );
300
    $patron = Koha::Patrons->find( $patron->{borrowernumber} );
288
    $patron->dateexpiry( undef )->store->discard_changes;
301
    $patron->dateexpiry( undef )->store->discard_changes;
289
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
302
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set' );
290
    $patron->dateexpiry( dt_from_string )->store->discard_changes;
303
    $patron->dateexpiry( dt_from_string )->store->discard_changes;
291
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
304
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today') ;
292
    $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
305
    $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
293
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
306
    is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow' );
307
    $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
308
    is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday' );
309
310
    # Test is_expired cache
311
    $patron->dateexpiry( undef )->store->discard_changes;
312
    # Set cache
313
    is( $patron->is_expired({ cache => 1 }), 0, 'Patron should not be considered expired if dateexpiry is not set, with caching enabled' );
314
    is( $patron->{is_expired_cache}, 0, 'is_expired cache has been set');
315
    is( $patron->is_expired({ cache => 1 }), 0, 'Patron should not be considered expired if dateexpiry is not set, with caching enabled and cache hit' );
294
    $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
316
    $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
295
    is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
317
    is( $patron->is_expired({ cache => 1 }), 1, 'Patron should be considered expired if dateexpiry is yesterday, cache has been invalidated' );
318
    is( $patron->{is_expired_cache}, 1, 'is_expired cache has been set');
319
    is( $patron->is_expired({ cache => 1 }), 1, 'Patron should be considered expired if dateexpiry is yesterday, on cache hit' );
320
    $patron->set({ dateexpiry => undef })->store->discard_changes;
321
    is( $patron->is_expired({ cache => 1 }), 0, 'Patron should not be considered expired if dateexpiry is not set, cache has been invalidated' );
322
    is( $patron->is_expired({ cache => 1 }), 0, 'Patron should not be considered expired if dateexpiry is not set, on cache hit' );
323
    $patron->is_expired;
324
    ok( !defined $patron->{is_expired_cache}, 'Cache has been invalidated after calling is_expired without caching enabled' );
296
325
297
    $patron->delete;
326
    $patron->delete;
298
};
327
};
299
- 

Return to bug 32476