}
if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
- my @waitingdate = split /-/, $values->{'waitingdate'};
-
- $values->{'expirationdate'} = '';
- if ( C4::Context->preference('ReservesMaxPickUpDelay') ) {
- my $dt = dt_from_string();
- $dt->add( days => C4::Context->preference('ReservesMaxPickUpDelay') );
- $values->{'expirationdate'} = output_pref( { dt => $dt, dateonly => 1 } );
- }
-
$values->{'waitingdate'} = output_pref({ dt => dt_from_string( $values->{'waitingdate'} ), dateonly => 1 });
-
}
if ($letter->{content} && $letter->{content} =~ /<<today>>/) {
my $dbh = C4::Context->dbh;
my $query = "
- SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
+ SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate, expirationdate
FROM reserves
WHERE priority='0'
AND found='W'
=cut
sub CancelExpiredReserves {
+ return unless C4::Context->preference("ExpireReservesMaxPickUpDelay");
+
my $today = dt_from_string();
my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
my $start_dt = shift;
my $num_days = shift;
+ return $start_dt unless $num_days > 0;
+
my $base_dt = $start_dt->clone();
while ($num_days--) {
$requested_expiration = dt_from_string($self->expirationdate);
}
- if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
- my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
- my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
- my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
-
- my $expirationdate = $today->clone;
- $expirationdate->add(days => $max_pickup_delay);
-
- if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
- $expirationdate = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
- }
-
- # If patron's requested expiration date is prior to the
- # calculated one, we keep the patron's one.
- my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
- $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
+ my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
+ my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+ my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
+
+ my $expirationdate = $today->clone;
+ $expirationdate->add(days => $max_pickup_delay);
+
+ if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
+ $expirationdate = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
}
+ # If patron's requested expiration date is prior to the
+ # calculated one, we keep the patron's one.
+ my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
+ $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
+
$self->set($values)->store();
return $self;
my $today = Date_to_Days(&Today);
my $max_pickup_delay = C4::Context->preference('ReservesMaxPickUpDelay');
-$max_pickup_delay-- if C4::Context->preference('ExpireReservesMaxPickUpDelay');
foreach my $num (@getreserves) {
next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
my $getborrower = GetMember(borrowernumber => $num->{'borrowernumber'});
my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} ); # using the fixed up itype/itemtype
$getreserv{'waitingdate'} = $num->{'waitingdate'};
- my ( $waiting_year, $waiting_month, $waiting_day ) = split (/-/, $num->{'waitingdate'});
-
- ( $waiting_year, $waiting_month, $waiting_day ) =
- Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
- $max_pickup_delay);
- my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
+ my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $num->{'expirationdate'});
+ my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
$getreserv{'itemtype'} = $itemtypeinfo->{'description'};
$getreserv{'title'} = $gettitle->{'title'};
--- /dev/null
+use C4::Context;
+
+use Koha::Holds;
+use Koha::DateUtils;
+use Koha::Calendar;
+
+my $waiting_holds = Koha::Holds->search({ found => 'W', priority => 0 });
+while ( my $hold = $waiting_holds->next ) {
+
+ my $requested_expiration;
+ if ($hold->expirationdate) {
+ $requested_expiration = dt_from_string($hold->expirationdate);
+ }
+
+ if ( my $waitingdate = dt_from_string($hold->waitingdate) ) {
+ my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
+ my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+ my $calendar = Koha::Calendar->new( branchcode => $hold->branchcode );
+
+ my $expirationdate = $waitingdate->clone;
+ $expirationdate->add(days => $max_pickup_delay);
+
+ if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
+ $expirationdate = $calendar->days_forward( dt_from_string($hold->waitingdate), $max_pickup_delay );
+ }
+
+ my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
+ $hold->expirationdate($cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd)->store;
+ }
+}
\ No newline at end of file
--- /dev/null
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 5;
+use t::lib::TestBuilder;
+
+use DateTime;
+use Koha::Caches;
+use Koha::DateUtils;
+
+use_ok('Koha::Calendar');
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+my $today = dt_from_string();
+my $holiday_dt = $today->clone;
+$holiday_dt->add(days => 15);
+
+Koha::Caches->get_instance()->flush_all();
+
+my $builder = t::lib::TestBuilder->new();
+my $holiday = $builder->build({
+ source => 'SpecialHoliday',
+ value => {
+ branchcode => 'LIB1',
+ day => $holiday_dt->day,
+ month => $holiday_dt->month,
+ year => $holiday_dt->year,
+ title => 'My holiday',
+ isexception => 0
+ },
+});
+
+my $calendar = Koha::Calendar->new( branchcode => 'LIB1');
+my $forwarded_dt = $calendar->days_forward($today, 10);
+
+my $expected = $today->clone;
+$expected->add(days => 10);
+is($forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 10 days');
+
+$forwarded_dt = $calendar->days_forward($today, 20);
+
+$expected->add(days => 11);
+is($forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 20 days + 1 day for holiday');
+
+$forwarded_dt = $calendar->days_forward($today, 0);
+is($forwarded_dt->ymd, $today->ymd, '0 day should return start dt');
+
+$forwarded_dt = $calendar->days_forward($today, -2);
+is($forwarded_dt->ymd, $today->ymd, 'negative day should return start dt');
+
+$schema->storage->txn_rollback();
use t::lib::Mocks;
use t::lib::TestBuilder;
-use Test::More tests => 5;
+use Test::More tests => 6;
use_ok('C4::Reserves');
CancelExpiredReserves();
my $r2 = Koha::Holds->find($reserve2->{reserve_id});
-is($r2, undef,'Reserve 2 should be canceled.');
+ok($r2, 'Without ExpireReservesMaxPickUpDelay, reserve 2 should not be canceled.');
+
+t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
+CancelExpiredReserves();
+$r2 = Koha::Holds->find($reserve2->{reserve_id});
+is($r2, undef,'With ExpireReservesMaxPickUpDelay, reserve 2 should be canceled.');
# Reserve expired on holiday
my $reserve3 = $builder->build({