@@ -, +, @@ an expiry date DateTime->new( year => 9999, month => 12, day => 31, time_zone => C4::Context->tz ); DateTime->compare($today, $expiry_dt) --- C4/Circulation.pm | 21 ++++++--------------- t/db_dependent/Circulation.t | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 16 deletions(-) --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -767,24 +767,15 @@ sub CanBookBeIssued { if ( !defined $borrower->{dateexpiry} || $borrower->{'dateexpiry'} eq '0000-00-00') { $issuingimpossible{EXPIRED} = 1; } else { - my ($y, $m, $d) = split /-/,$borrower->{'dateexpiry'}; - if ($y && $m && $d) { # are we really writing oinvalid dates to borrs - my $expiry_dt = DateTime->new( - year => $y, - month => $m, - day => $d, - time_zone => C4::Context->tz, - ); - $expiry_dt->truncate( to => 'day'); - my $today = $now->clone()->truncate(to => 'day'); - if (DateTime->compare($today, $expiry_dt) == 1) { - $issuingimpossible{EXPIRED} = 1; - } - } else { - carp("Invalid expity date in borr"); + my $expiry_dt = dt_from_string( $borrower->{dateexpiry}, 'sql' ); + $expiry_dt->truncate( to => 'day'); + my $today = $now->clone()->truncate(to => 'day'); + + if ($expiry_dt->year < 9999 && DateTime->compare($today, $expiry_dt) == 1) { $issuingimpossible{EXPIRED} = 1; } } + # # BORROWER STATUS # --- a/t/db_dependent/Circulation.t +++ a/t/db_dependent/Circulation.t @@ -18,6 +18,7 @@ use Modern::Perl; use DateTime; +use Time::HiRes qw( gettimeofday ); use C4::Biblio; use C4::Branch; use C4::Items; @@ -26,8 +27,9 @@ use C4::Reserves; use C4::Overdues qw(UpdateFine); use Koha::DateUtils; use Koha::Database; +use t::lib::TestBuilder; -use Test::More tests => 61; +use Test::More tests => 65; BEGIN { use_ok('C4::Circulation'); @@ -594,6 +596,43 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" ); } +{ + my $builder = t::lib::TestBuilder->new(); + my $item = $builder->build( { source => 'Item' } ); + my $patron = $builder->build( + { source => 'Borrower', + value => { dateexpiry => '9999-12-31' } + } + ); + $patron->{flags} = C4::Members::patronflags($borrower); + my $duration = gettimeofday(); + my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} ); + $duration = gettimeofday() - $duration; + cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired"; + is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' ); + + $item = $builder->build( { source => 'Item' } ); + $patron = $builder->build( + { source => 'Borrower', + value => { dateexpiry => '0000-00-00' } + } + ); + $patron->{flags} = C4::Members::patronflags($borrower); + ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} ); + is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' ); + + my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) ); + $item = $builder->build( { source => 'Item' } ); + $patron = $builder->build( + { source => 'Borrower', + value => { dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ) }, + } + ); + $patron->{flags} = C4::Members::patronflags($borrower); + ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} ); + is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' ); +} + $dbh->rollback; 1; --