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

(-)a/Koha/Calendar.pm (-75 / +32 lines)
Lines 51-144 sub _init { Link Here
51
    return;
51
    return;
52
}
52
}
53
53
54
sub exception_holidays {
54
sub _holidays {
55
    my ($self) = @_;
55
    my ($self) = @_;
56
56
57
    my $cache              = Koha::Caches->get_instance();
57
    my $key      = $self->{branchcode} . "_holidays";
58
    my $exception_holidays = $cache->get_from_cache('exception_holidays');
58
    my $cache    = Koha::Caches->get_instance();
59
    my $holidays = $cache->get_from_cache($key);
59
60
60
    # Populate the cache is necessary
61
    # $holidays looks like:
61
    unless ($exception_holidays) {
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
            my $datestring =
75
                sprintf( "%04d", $year )
76
              . sprintf( "%02d", $month )
77
              . sprintf( "%02d", $day );
78
79
            $exception_holidays->{$branch}->{$datestring} = 1;
80
        }
81
        $cache->set_in_cache( 'exception_holidays', $exception_holidays,
82
            { expiry => 76800 } );
83
    }
84
85
    return $exception_holidays->{ $self->{branchcode} } // {};
86
}
87
88
sub is_exception_holiday {
89
    my ( $self, $date ) = @_;
90
91
    return 1 if ( $self->exception_holidays->{$date} );
92
    return 0;
93
}
94
95
sub single_holidays {
96
    my ($self) = @_;
97
98
    my $cache           = Koha::Caches->get_instance();
99
    my $single_holidays = $cache->get_from_cache('single_holidays');
100
101
    # $single_holidays looks like:
102
    # {
62
    # {
103
    #   CPL =>  [
63
    #    20131122 => 1, # single_holiday
104
    #        [0] 20131122,
64
    #    20131123 => 0, # exception_holiday
105
    #         ...
65
    #    ...
106
    #    ],
107
    #   ...
108
    # }
66
    # }
109
67
110
    # Populate the cache if necessary
68
    # Populate the cache if necessary
111
    unless ($single_holidays) {
69
    unless ($holidays) {
112
        my $dbh = C4::Context->dbh;
70
        my $dbh = C4::Context->dbh;
113
        $single_holidays = {};
71
        $holidays = {};
114
72
115
        # Push holidays for each branch
73
        # Add holidays for each branch
116
        my $single_holidays_sth = $dbh->prepare(
74
        my $holidays_sth = $dbh->prepare(
117
'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 0'
75
'SELECT day, month, year, isexception FROM special_holidays WHERE branchcode = ?'
118
        );
76
        );
119
        $single_holidays_sth->execute();
77
        $holidays_sth->execute($self->{branchcode});
120
78
121
        while ( my ( $day, $month, $year, $branch ) =
79
        while ( my ( $day, $month, $year, $exception ) =
122
            $single_holidays_sth->fetchrow )
80
            $holidays_sth->fetchrow )
123
        {
81
        {
124
            my $datestring =
82
            my $datestring =
125
                sprintf( "%04d", $year )
83
                sprintf( "%04d", $year )
126
              . sprintf( "%02d", $month )
84
              . sprintf( "%02d", $month )
127
              . sprintf( "%02d", $day );
85
              . sprintf( "%02d", $day );
128
86
129
            $single_holidays->{$branch}->{$datestring} = 1;
87
            $holidays->{$datestring} = !$exception;
130
        }
88
        }
131
        $cache->set_in_cache( 'single_holidays', $single_holidays,
89
        $cache->set_in_cache( $key, $holidays, { expiry => 76800 } );
132
            { expiry => 76800 } );
133
    }
90
    }
134
91
135
    return $single_holidays->{ $self->{branchcode} } // {};
92
    return $holidays // {};
136
}
93
}
137
94
138
sub is_single_holiday {
95
sub is_single_holiday {
139
    my ( $self, $date ) = @_;
96
    my ( $self, $date ) = @_;
140
97
141
    return 1 if ( $self->single_holidays->{$date} );
98
    return 1 if ( $self->_holidays->{$date} );
99
    return 0;
100
}
101
102
sub is_exception_holiday {
103
    my ( $self, $date ) = @_;
104
105
    return 1
106
      if ( defined( $self->_holidays->{$date} ) && !$self->_holidays->{$date} );
142
    return 0;
107
    return 0;
143
}
108
}
144
109
Lines 258-264 sub is_holiday { Link Here
258
    my $localdt = $dt->clone();
223
    my $localdt = $dt->clone();
259
    my $day   = $localdt->day;
224
    my $day   = $localdt->day;
260
    my $month = $localdt->month;
225
    my $month = $localdt->month;
261
    my $ymd   = $localdt->ymd('')  ;
226
    my $ymd   = $localdt->ymd('');
262
227
263
    #Change timezone to "floating" before doing any calculations or comparisons
228
    #Change timezone to "floating" before doing any calculations or comparisons
264
    $localdt->set_time_zone("floating");
229
    $localdt->set_time_zone("floating");
Lines 485-502 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration Link Here
485
Currently unit is only used to invoke Staffs return Monday at 10 am rule this
450
Currently unit is only used to invoke Staffs return Monday at 10 am rule this
486
parameter will be removed when issuingrules properly cope with that
451
parameter will be removed when issuingrules properly cope with that
487
452
453
=head2 is_single_holiday
488
454
489
=head2 single_holidays
455
my $rc = $self->is_single_holiday( $ymd );
490
491
my $rc = $self->single_holidays(  $ymd  );
492
493
Passed a $date in Ymd (yyyymmdd) format -  returns 1 if date is a single_holiday, or 0 if not.
494
495
=head2 exception_holidays
496
497
my $exceptions = $self->exception_holidays;
498
456
499
Returns a hashref of exception holidays for the branch
457
Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is a single_holiday, or 0 if not.
500
458
501
=head2 is_exception_holiday
459
=head2 is_exception_holiday
502
460
503
- 

Return to bug 25723