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

(-)a/Koha/Calendar.pm (-84 / +33 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-274 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");
265
    $localdt->truncate( to => 'day' );
230
    $localdt->truncate( to => 'day' );
266
231
267
232
    return $self->_holidays->{$ymd} if defined($self->_holidays->{$ymd});
268
    if ( $self->is_exception_holiday( $ymd ) == 1 ) {
269
        # exceptions are not holidays
270
        return 0;
271
    }
272
233
273
    my $dow = $localdt->day_of_week;
234
    my $dow = $localdt->day_of_week;
274
    # Representation fix
235
    # Representation fix
Lines 286-295 sub is_holiday { Link Here
286
        return 1;
247
        return 1;
287
    }
248
    }
288
249
289
    if ($self->is_single_holiday(  $ymd  ) == 1 ) {
290
        return 1;
291
    }
292
293
    # damn have to go to work after all
250
    # damn have to go to work after all
294
    return 0;
251
    return 0;
295
}
252
}
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
442
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
443
parameter will be removed when issuingrules properly cope with that
487
444
445
=head2 is_single_holiday
488
446
489
=head2 single_holidays
447
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
448
499
Returns a hashref of exception holidays for the branch
449
Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is a single_holiday, or 0 if not.
500
450
501
=head2 is_exception_holiday
451
=head2 is_exception_holiday
502
452
503
- 

Return to bug 25723