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

(-)a/Koha/Calendar.pm (-26 / +32 lines)
Lines 4-10 use warnings; Link Here
4
use 5.010;
4
use 5.010;
5
5
6
use DateTime;
6
use DateTime;
7
use DateTime::Set;
8
use DateTime::Duration;
7
use DateTime::Duration;
9
use C4::Context;
8
use C4::Context;
10
use Koha::Caches;
9
use Koha::Caches;
Lines 53-84 sub _init { Link Here
53
}
52
}
54
53
55
sub exception_holidays {
54
sub exception_holidays {
56
    my ( $self ) = @_;
55
    my ( $self, $date ) = @_;
56
57
    my $cache              = Koha::Caches->get_instance();
58
    my $exception_holidays = $cache->get_from_cache('exception_holidays');
57
59
58
    my $cache  = Koha::Caches->get_instance();
60
    # Populate the cache is necessary
59
    my $cached = $cache->get_from_cache('exception_holidays');
61
    unless ($exception_holidays) {
60
    return $cached if $cached;
62
        my $dbh = C4::Context->dbh;
63
        $exception_holidays = {};
64
65
        # Push holidays for each branch
66
        my $exception_holidays_sth = $dbh->prepare(
67
'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 1'
68
        );
69
        $exception_holidays_sth->execute();
70
        my $dates = [];
71
        while ( my ( $day, $month, $year, $branch ) =
72
            $exception_holidays_sth->fetchrow )
73
        {
74
            push @{ $exception_holidays->{$branch} },
75
                sprintf( "%04d", $year )
76
              . sprintf( "%02d", $month )
77
              . sprintf( "%02d", $day );
78
        }
79
        $cache->set_in_cache( 'exception_holidays', $exception_holidays,
80
            { expiry => 76800 } );
81
    }
61
82
62
    my $dbh = C4::Context->dbh;
83
    # Return if we match
63
    my $branch = $self->{branchcode};
84
    my $branch = $self->{branchcode};
64
    my $exception_holidays_sth = $dbh->prepare(
85
    for my $hols ( @{ $exception_holidays->{$branch} } ) {
65
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
86
        return 1 if ( $date == $hols );
66
    );
67
    $exception_holidays_sth->execute( $branch );
68
    my $dates = [];
69
    while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
70
        push @{$dates},
71
          DateTime->new(
72
            day       => $day,
73
            month     => $month,
74
            year      => $year,
75
            time_zone => "floating",
76
          )->truncate( to => 'day' );
77
    }
87
    }
78
    $self->{exception_holidays} =
88
    return 0;
79
      DateTime::Set->from_datetimes( dates => $dates );
80
    $cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} );
81
    return $self->{exception_holidays};
82
}
89
}
83
90
84
sub single_holidays {
91
sub single_holidays {
Lines 250-262 sub is_holiday { Link Here
250
    my $localdt = $dt->clone();
257
    my $localdt = $dt->clone();
251
    my $day   = $localdt->day;
258
    my $day   = $localdt->day;
252
    my $month = $localdt->month;
259
    my $month = $localdt->month;
260
    my $ymd   = $localdt->ymd('')  ;
253
261
254
    #Change timezone to "floating" before doing any calculations or comparisons
262
    #Change timezone to "floating" before doing any calculations or comparisons
255
    $localdt->set_time_zone("floating");
263
    $localdt->set_time_zone("floating");
256
    $localdt->truncate( to => 'day' );
264
    $localdt->truncate( to => 'day' );
257
265
258
266
259
    if ( $self->exception_holidays->contains($localdt) ) {
267
    if ( $self->exception_holidays( $ymd ) == 1 ) {
260
        # exceptions are not holidays
268
        # exceptions are not holidays
261
        return 0;
269
        return 0;
262
    }
270
    }
Lines 277-283 sub is_holiday { Link Here
277
        return 1;
285
        return 1;
278
    }
286
    }
279
287
280
    my $ymd   = $localdt->ymd('')  ;
281
    if ($self->single_holidays(  $ymd  ) == 1 ) {
288
    if ($self->single_holidays(  $ymd  ) == 1 ) {
282
        return 1;
289
        return 1;
283
    }
290
    }
284
- 

Return to bug 25723