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

(-)a/Koha/Calendar.pm (-2 / +26 lines)
Lines 8-13 use DateTime::Duration; Link Here
8
use C4::Context;
8
use C4::Context;
9
use Koha::Caches;
9
use Koha::Caches;
10
use Koha::Exceptions;
10
use Koha::Exceptions;
11
use Koha::Exceptions::Calendar;
12
13
# This limit avoids an infinite loop when searching for an open day in an
14
# always closed library
15
# The value is arbitrary, but it should be large enough to be able to
16
# consider there is no open days if we haven't found any with that many
17
# iterations, and small enough to allow the loop to end quickly
18
# See next_open_days and prev_open_days
19
use constant OPEN_DAYS_SEARCH_MAX_ITERATIONS => 5000;
11
20
12
sub new {
21
sub new {
13
    my ( $classname, %options ) = @_;
22
    my ( $classname, %options ) = @_;
Lines 261-270 sub next_open_days { Link Here
261
    my $base_date = $dt->clone();
270
    my $base_date = $dt->clone();
262
271
263
    $base_date->add(days => $to_add);
272
    $base_date->add(days => $to_add);
264
    while ($self->is_holiday($base_date)) {
273
    my $i = 0;
274
    while ( $self->is_holiday($base_date) && $i < OPEN_DAYS_SEARCH_MAX_ITERATIONS ) {
265
        my $add_next = $self->get_push_amt($base_date);
275
        my $add_next = $self->get_push_amt($base_date);
266
        $base_date->add(days => $add_next);
276
        $base_date->add(days => $add_next);
277
        ++$i;
278
    }
279
280
    if ( $self->is_holiday($base_date) ) {
281
        Koha::Exceptions::Calendar::NoOpenDays->throw(
282
            sprintf( 'Unable to find an open day for library %s', $self->{branchcode} ) );
267
    }
283
    }
284
268
    return $base_date;
285
    return $base_date;
269
}
286
}
270
287
Lines 282-292 sub prev_open_days { Link Here
282
299
283
    $base_date->add(days => $to_sub);
300
    $base_date->add(days => $to_sub);
284
301
285
    while ($self->is_holiday($base_date)) {
302
    my $i = 0;
303
    while ( $self->is_holiday($base_date) && $i < OPEN_DAYS_SEARCH_MAX_ITERATIONS ) {
286
        my $sub_next = $self->get_push_amt($base_date);
304
        my $sub_next = $self->get_push_amt($base_date);
287
        # Ensure we're subtracting when we need to be
305
        # Ensure we're subtracting when we need to be
288
        $sub_next = $sub_next > 0 ? 0 - $sub_next : $sub_next;
306
        $sub_next = $sub_next > 0 ? 0 - $sub_next : $sub_next;
289
        $base_date->add(days => $sub_next);
307
        $base_date->add(days => $sub_next);
308
        ++$i;
309
    }
310
311
    if ( $self->is_holiday($base_date) ) {
312
        Koha::Exceptions::Calendar::NoOpenDays->throw(
313
            sprintf( 'Unable to find an open day for library %s', $self->{branchcode} ) );
290
    }
314
    }
291
315
292
    return $base_date;
316
    return $base_date;
(-)a/Koha/Exceptions/Calendar.pm (+17 lines)
Line 0 Link Here
1
package Koha::Exceptions::Calendar;
2
3
use Modern::Perl;
4
5
use Koha::Exception;
6
7
use Exception::Class (
8
    'Koha::Exceptions::Calendar' => {
9
        isa => 'Koha::Exception',
10
    },
11
    'Koha::Exceptions::Calendar::NoOpenDays' => {
12
        isa => 'Koha::Exceptions::Calendar',
13
        description => 'Library has no open days',
14
    },
15
);
16
17
1;
(-)a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js (+2 lines)
Lines 203-208 $(document).ready(function() { Link Here
203
                            content += __("Not allowed: patron restricted");
203
                            content += __("Not allowed: patron restricted");
204
                        } else if ( data.error == "overdue" ) {
204
                        } else if ( data.error == "overdue" ) {
205
                            content += __("Not allowed: overdue");
205
                            content += __("Not allowed: overdue");
206
                        } else if ( data.error == 'no_open_days' ) {
207
                            content += __('Unable to find an open day');
206
                        } else if ( data.error ) {
208
                        } else if ( data.error ) {
207
                            content += data.error;
209
                            content += data.error;
208
                        } else {
210
                        } else {
(-)a/svc/renew (+2 lines)
Lines 87-92 if ( $data->{renew_okay} || ( $seen && $data->{error} eq 'too_unseen') ) { Link Here
87
    } catch {
87
    } catch {
88
        if ( ref($_) eq 'Koha::Exceptions::Checkout::FailedRenewal' ) {
88
        if ( ref($_) eq 'Koha::Exceptions::Checkout::FailedRenewal' ) {
89
            $data->{error} = 'renewal_failed';
89
            $data->{error} = 'renewal_failed';
90
        } elsif ( ref($_) eq 'Koha::Exceptions::Calendar::NoOpenDays' ) {
91
            $data->{error} = 'no_open_days';
90
        } else {
92
        } else {
91
            $_->rethrow;
93
            $_->rethrow;
92
        }
94
        }
(-)a/t/db_dependent/Holidays.t (-2 / +60 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 12;
20
use Test::More tests => 14;
21
use Test::Exception;
21
22
22
use DateTime;
23
use DateTime;
23
use DateTime::TimeZone;
24
use DateTime::TimeZone;
Lines 277-281 subtest 'copy_to_branch' => sub { Link Here
277
278
278
};
279
};
279
280
281
subtest 'with a library that is never open' => sub {
282
    plan tests => 2;
283
284
    $schema->storage->txn_begin;
285
286
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
287
    my $calendar   = C4::Calendar->new( branchcode => $branchcode );
288
    foreach my $weekday ( 0 .. 6 ) {
289
        $calendar->insert_week_day_holiday( weekday => $weekday, title => '', description => '' );
290
    }
291
292
    my $now = DateTime->now;
293
294
    subtest 'next_open_days should throw an exception' => sub {
295
        my $kcalendar = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
296
        throws_ok { $kcalendar->next_open_days( $now, 1 ) } 'Koha::Exceptions::Calendar::NoOpenDays';
297
    };
298
299
    subtest 'prev_open_days should throw an exception' => sub {
300
        my $kcalendar = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
301
        throws_ok { $kcalendar->prev_open_days( $now, 1 ) } 'Koha::Exceptions::Calendar::NoOpenDays';
302
    };
303
304
    $schema->storage->txn_rollback;
305
};
306
307
subtest 'with a library that is *almost* never open' => sub {
308
    plan tests => 2;
309
310
    $schema->storage->txn_begin;
311
312
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
313
    my $calendar   = C4::Calendar->new( branchcode => $branchcode );
314
    foreach my $weekday ( 0 .. 6 ) {
315
        $calendar->insert_week_day_holiday( weekday => $weekday, title => '', description => '' );
316
    }
317
318
    my $now                    = DateTime->now;
319
    my $open_day_in_the_future = $now->clone()->add( years => 1 );
320
    my $open_day_in_the_past   = $now->clone()->subtract( years => 1 );
321
    $calendar->insert_exception_holiday( date => $open_day_in_the_future->ymd, title => '', description => '' );
322
    $calendar->insert_exception_holiday( date => $open_day_in_the_past->ymd,   title => '', description => '' );
323
324
    subtest 'next_open_days should find the open day' => sub {
325
        my $kcalendar     = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
326
        my $next_open_day = $kcalendar->next_open_days( $now, 1 );
327
        is( $next_open_day->ymd, $open_day_in_the_future->ymd );
328
    };
329
330
    subtest 'prev_open_days should find the open day' => sub {
331
        my $kcalendar     = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
332
        my $prev_open_day = $kcalendar->prev_open_days( $now, 1 );
333
        is( $prev_open_day->ymd, $open_day_in_the_past->ymd );
334
    };
335
336
    $schema->storage->txn_rollback;
337
};
338
280
# Clear cache
339
# Clear cache
281
Koha::Caches->get_instance->flush_all;
340
Koha::Caches->get_instance->flush_all;
282
- 

Return to bug 27249