Bug 11592: (QA followup) Add missing framework code to ViewPolicy filter calls
[koha.git] / C4 / Calendar.pm
1 package C4::Calendar;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use strict;
19 use warnings;
20 use vars qw(@EXPORT);
21
22 use Carp;
23 use Date::Calc qw( Date_to_Days Today);
24
25 use C4::Context;
26 use Koha::Caches;
27
28 use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
29
30 =head1 NAME
31
32 C4::Calendar::Calendar - Koha module dealing with holidays.
33
34 =head1 SYNOPSIS
35
36     use C4::Calendar::Calendar;
37
38 =head1 DESCRIPTION
39
40 This package is used to deal with holidays. Through this package, you can set 
41 all kind of holidays for the library.
42
43 =head1 FUNCTIONS
44
45 =head2 new
46
47   $calendar = C4::Calendar->new(branchcode => $branchcode);
48
49 Each library branch has its own Calendar.  
50 C<$branchcode> specifies which Calendar you want.
51
52 =cut
53
54 sub new {
55     my $classname = shift @_;
56     my %options = @_;
57     my $self = bless({}, $classname);
58     foreach my $optionName (keys %options) {
59         $self->{lc($optionName)} = $options{$optionName};
60     }
61     defined($self->{branchcode}) or croak "No branchcode argument to new.  Should be C4::Calendar->new(branchcode => \$branchcode)";
62     $self->_init($self->{branchcode});
63     return $self;
64 }
65
66 sub _init {
67     my $self = shift @_;
68     my $branch = shift;
69     defined($branch) or die "No branchcode sent to _init";  # must test for defined here and above to allow ""
70     my $dbh = C4::Context->dbh();
71     my $repeatable = $dbh->prepare( 'SELECT *
72                                        FROM repeatable_holidays
73                                       WHERE ( branchcode = ? )
74                                         AND (ISNULL(weekday) = ?)' );
75     $repeatable->execute($branch,0);
76     my %week_days_holidays;
77     while (my $row = $repeatable->fetchrow_hashref) {
78         my $key = $row->{weekday};
79         $week_days_holidays{$key}{title}       = $row->{title};
80         $week_days_holidays{$key}{description} = $row->{description};
81     }
82     $self->{'week_days_holidays'} = \%week_days_holidays;
83
84     $repeatable->execute($branch,1);
85     my %day_month_holidays;
86     while (my $row = $repeatable->fetchrow_hashref) {
87         my $key = $row->{month} . "/" . $row->{day};
88         $day_month_holidays{$key}{title}       = $row->{title};
89         $day_month_holidays{$key}{description} = $row->{description};
90         $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
91         $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
92     }
93     $self->{'day_month_holidays'} = \%day_month_holidays;
94
95     my $special = $dbh->prepare( 'SELECT day, month, year, title, description
96                                     FROM special_holidays
97                                    WHERE ( branchcode = ? )
98                                      AND (isexception = ?)' );
99     $special->execute($branch,1);
100     my %exception_holidays;
101     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
102         $exception_holidays{"$year/$month/$day"}{title} = $title;
103         $exception_holidays{"$year/$month/$day"}{description} = $description;
104         $exception_holidays{"$year/$month/$day"}{date} = 
105                 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
106     }
107     $self->{'exception_holidays'} = \%exception_holidays;
108
109     $special->execute($branch,0);
110     my %single_holidays;
111     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
112         $single_holidays{"$year/$month/$day"}{title} = $title;
113         $single_holidays{"$year/$month/$day"}{description} = $description;
114         $single_holidays{"$year/$month/$day"}{date} = 
115                 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
116     }
117     $self->{'single_holidays'} = \%single_holidays;
118     return $self;
119 }
120
121 =head2 get_week_days_holidays
122
123    $week_days_holidays = $calendar->get_week_days_holidays();
124
125 Returns a hash reference to week days holidays.
126
127 =cut
128
129 sub get_week_days_holidays {
130     my $self = shift @_;
131     my $week_days_holidays = $self->{'week_days_holidays'};
132     return $week_days_holidays;
133 }
134
135 =head2 get_day_month_holidays
136
137    $day_month_holidays = $calendar->get_day_month_holidays();
138
139 Returns a hash reference to day month holidays.
140
141 =cut
142
143 sub get_day_month_holidays {
144     my $self = shift @_;
145     my $day_month_holidays = $self->{'day_month_holidays'};
146     return $day_month_holidays;
147 }
148
149 =head2 get_exception_holidays
150
151     $exception_holidays = $calendar->exception_holidays();
152
153 Returns a hash reference to exception holidays. This kind of days are those
154 which stands for a holiday, but you wanted to make an exception for this particular
155 date.
156
157 =cut
158
159 sub get_exception_holidays {
160     my $self = shift @_;
161     my $exception_holidays = $self->{'exception_holidays'};
162     return $exception_holidays;
163 }
164
165 =head2 get_single_holidays
166
167     $single_holidays = $calendar->get_single_holidays();
168
169 Returns a hash reference to single holidays. This kind of holidays are those which
170 happened just one time.
171
172 =cut
173
174 sub get_single_holidays {
175     my $self = shift @_;
176     my $single_holidays = $self->{'single_holidays'};
177     return $single_holidays;
178 }
179
180 =head2 insert_week_day_holiday
181
182     insert_week_day_holiday(weekday => $weekday,
183                             title => $title,
184                             description => $description);
185
186 Inserts a new week day for $self->{branchcode}.
187
188 C<$day> Is the week day to make holiday.
189
190 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
191
192 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
193
194 =cut
195
196 sub insert_week_day_holiday {
197     my $self = shift @_;
198     my %options = @_;
199
200     my $weekday = $options{weekday};
201     croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/;
202
203     my $dbh = C4::Context->dbh();
204     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); 
205         $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description});
206     $self->{'week_days_holidays'}->{$weekday}{title} = $options{title};
207     $self->{'week_days_holidays'}->{$weekday}{description} = $options{description};
208     return $self;
209 }
210
211 =head2 insert_day_month_holiday
212
213     insert_day_month_holiday(day => $day,
214                              month => $month,
215                              title => $title,
216                              description => $description);
217
218 Inserts a new day month holiday for $self->{branchcode}.
219
220 C<$day> Is the day month to make the date to insert.
221
222 C<$month> Is month to make the date to insert.
223
224 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
225
226 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
227
228 =cut
229
230 sub insert_day_month_holiday {
231     my $self = shift @_;
232     my %options = @_;
233
234     my $dbh = C4::Context->dbh();
235     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
236         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
237     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
238     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
239     return $self;
240 }
241
242 =head2 insert_single_holiday
243
244     insert_single_holiday(day => $day,
245                           month => $month,
246                           year => $year,
247                           title => $title,
248                           description => $description);
249
250 Inserts a new single holiday for $self->{branchcode}.
251
252 C<$day> Is the day month to make the date to insert.
253
254 C<$month> Is month to make the date to insert.
255
256 C<$year> Is year to make the date to insert.
257
258 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
259
260 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
261
262 =cut
263
264 sub insert_single_holiday {
265     my $self = shift @_;
266     my %options = @_;
267     @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
268       if $options{date} && !$options{day};
269
270         my $dbh = C4::Context->dbh();
271     my $isexception = 0;
272     my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
273         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
274     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
275     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
276
277
278     # changed the 'single_holidays' table, lets force/reset its cache
279     my $cache = Koha::Caches->get_instance();
280     $cache->clear_from_cache( 'single_holidays') ;
281     $cache->clear_from_cache( 'exception_holidays') ;
282
283     return $self;
284
285 }
286
287 =head2 insert_exception_holiday
288
289     insert_exception_holiday(day => $day,
290                              month => $month,
291                              year => $year,
292                              title => $title,
293                              description => $description);
294
295 Inserts a new exception holiday for $self->{branchcode}.
296
297 C<$day> Is the day month to make the date to insert.
298
299 C<$month> Is month to make the date to insert.
300
301 C<$year> Is year to make the date to insert.
302
303 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
304
305 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
306
307 =cut
308
309 sub insert_exception_holiday {
310     my $self = shift @_;
311     my %options = @_;
312
313     @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
314       if $options{date} && !$options{day};
315
316     my $dbh = C4::Context->dbh();
317     my $isexception = 1;
318     my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
319         $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
320     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
321     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
322
323     # changed the 'single_holidays' table, lets force/reset its cache
324     my $cache = Koha::Caches->get_instance();
325     $cache->clear_from_cache( 'single_holidays') ;
326     $cache->clear_from_cache( 'exception_holidays') ;
327
328     return $self;
329 }
330
331 =head2 ModWeekdayholiday
332
333     ModWeekdayholiday(weekday =>$weekday,
334                       title => $title,
335                       description => $description)
336
337 Modifies the title and description of a weekday for $self->{branchcode}.
338
339 C<$weekday> Is the title to update for the holiday.
340
341 C<$description> Is the description to update for the holiday.
342
343 =cut
344
345 sub ModWeekdayholiday {
346     my $self = shift @_;
347     my %options = @_;
348
349     my $dbh = C4::Context->dbh();
350     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
351     $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); 
352     $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
353     $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
354     return $self;
355 }
356
357 =head2 ModDaymonthholiday
358
359     ModDaymonthholiday(day => $day,
360                        month => $month,
361                        title => $title,
362                        description => $description);
363
364 Modifies the title and description for a day/month holiday for $self->{branchcode}.
365
366 C<$day> The day of the month for the update.
367
368 C<$month> The month to be used for the update.
369
370 C<$title> The title to be updated for the holiday.
371
372 C<$description> The description to be update for the holiday.
373
374 =cut
375
376 sub ModDaymonthholiday {
377     my $self = shift @_;
378     my %options = @_;
379
380     my $dbh = C4::Context->dbh();
381     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
382        $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); 
383     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
384     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
385     return $self;
386 }
387
388 =head2 ModSingleholiday
389
390     ModSingleholiday(day => $day,
391                      month => $month,
392                      year => $year,
393                      title => $title,
394                      description => $description);
395
396 Modifies the title and description for a single holiday for $self->{branchcode}.
397
398 C<$day> Is the day of the month to make the update.
399
400 C<$month> Is the month to make the update.
401
402 C<$year> Is the year to make the update.
403
404 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
405
406 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
407
408 =cut
409
410 sub ModSingleholiday {
411     my $self = shift @_;
412     my %options = @_;
413
414     my $dbh = C4::Context->dbh();
415     my $isexception = 0;
416
417     my $updateHoliday = $dbh->prepare("
418 UPDATE special_holidays SET title = ?, description = ?
419     WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
420       $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);    
421     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
422     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
423
424     # changed the 'single_holidays' table, lets force/reset its cache
425     my $cache = Koha::Caches->get_instance();
426     $cache->clear_from_cache( 'single_holidays') ;
427     $cache->clear_from_cache( 'exception_holidays') ;
428
429     return $self;
430 }
431
432 =head2 ModExceptionholiday
433
434     ModExceptionholiday(day => $day,
435                         month => $month,
436                         year => $year,
437                         title => $title,
438                         description => $description);
439
440 Modifies the title and description for an exception holiday for $self->{branchcode}.
441
442 C<$day> Is the day of the month for the holiday.
443
444 C<$month> Is the month for the holiday.
445
446 C<$year> Is the year for the holiday.
447
448 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
449
450 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
451
452 =cut
453
454 sub ModExceptionholiday {
455     my $self = shift @_;
456     my %options = @_;
457
458     my $dbh = C4::Context->dbh();
459     my $isexception = 1;
460     my $updateHoliday = $dbh->prepare("
461 UPDATE special_holidays SET title = ?, description = ?
462     WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
463     $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);
464     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
465     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
466
467     # changed the 'single_holidays' table, lets force/reset its cache
468     my $cache = Koha::Caches->get_instance();
469     $cache->clear_from_cache( 'single_holidays') ;
470     $cache->clear_from_cache( 'exception_holidays') ;
471
472     return $self;
473 }
474
475 =head2 delete_holiday
476
477     delete_holiday(weekday => $weekday
478                    day => $day,
479                    month => $month,
480                    year => $year);
481
482 Delete a holiday for $self->{branchcode}.
483
484 C<$weekday> Is the week day to delete.
485
486 C<$day> Is the day month to make the date to delete.
487
488 C<$month> Is month to make the date to delete.
489
490 C<$year> Is year to make the date to delete.
491
492 =cut
493
494 sub delete_holiday {
495     my $self = shift @_;
496     my %options = @_;
497
498     # Verify what kind of holiday that day is. For example, if it is
499     # a repeatable holiday, this should check if there are some exception
500     # for that holiday rule. Otherwise, if it is a regular holiday, it´s
501     # ok just deleting it.
502
503     my $dbh = C4::Context->dbh();
504     my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
505     $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
506     if ($isSingleHoliday->rows) {
507         my $id = $isSingleHoliday->fetchrow;
508         $isSingleHoliday->finish; # Close the last query
509
510         my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
511         $deleteHoliday->execute($id);
512         delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
513     } else {
514         $isSingleHoliday->finish; # Close the last query
515
516         my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
517         $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
518         if ($isWeekdayHoliday->rows) {
519             my $id = $isWeekdayHoliday->fetchrow;
520             $isWeekdayHoliday->finish; # Close the last query
521
522             my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
523             $updateExceptions->execute($options{weekday}, $self->{branchcode});
524             $updateExceptions->finish; # Close the last query
525
526             my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
527             $deleteHoliday->execute($id);
528             delete($self->{'week_days_holidays'}->{$options{weekday}});
529         } else {
530             $isWeekdayHoliday->finish; # Close the last query
531
532             my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
533             $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
534             if ($isDayMonthHoliday->rows) {
535                 my $id = $isDayMonthHoliday->fetchrow;
536                 $isDayMonthHoliday->finish;
537                 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
538                 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
539                 $updateExceptions->finish; # Close the last query
540
541                 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
542                 $deleteHoliday->execute($id);
543                 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
544             }
545         }
546     }
547
548     # changed the 'single_holidays' table, lets force/reset its cache
549     my $cache = Koha::Caches->get_instance();
550     $cache->clear_from_cache( 'single_holidays') ;
551     $cache->clear_from_cache( 'exception_holidays') ;
552
553     return $self;
554 }
555 =head2 delete_holiday_range
556
557     delete_holiday_range(day => $day,
558                    month => $month,
559                    year => $year);
560
561 Delete a holiday range of dates for $self->{branchcode}.
562
563 C<$day> Is the day month to make the date to delete.
564
565 C<$month> Is month to make the date to delete.
566
567 C<$year> Is year to make the date to delete.
568
569 =cut
570
571 sub delete_holiday_range {
572     my $self = shift;
573     my %options = @_;
574
575     my $dbh = C4::Context->dbh();
576     my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
577     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
578
579     # changed the 'single_holidays' table, lets force/reset its cache
580     my $cache = Koha::Caches->get_instance();
581     $cache->clear_from_cache( 'single_holidays') ;
582     $cache->clear_from_cache( 'exception_holidays') ;
583
584 }
585
586 =head2 delete_holiday_range_repeatable
587
588     delete_holiday_range_repeatable(day => $day,
589                    month => $month);
590
591 Delete a holiday for $self->{branchcode}.
592
593 C<$day> Is the day month to make the date to delete.
594
595 C<$month> Is month to make the date to delete.
596
597 =cut
598
599 sub delete_holiday_range_repeatable {
600     my $self = shift;
601     my %options = @_;
602
603     my $dbh = C4::Context->dbh();
604     my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
605     $sth->execute($self->{branchcode}, $options{day}, $options{month});
606 }
607
608 =head2 delete_exception_holiday_range
609
610     delete_exception_holiday_range(weekday => $weekday
611                    day => $day,
612                    month => $month,
613                    year => $year);
614
615 Delete a holiday for $self->{branchcode}.
616
617 C<$day> Is the day month to make the date to delete.
618
619 C<$month> Is month to make the date to delete.
620
621 C<$year> Is year to make the date to delete.
622
623 =cut
624
625 sub delete_exception_holiday_range {
626     my $self = shift;
627     my %options = @_;
628
629     my $dbh = C4::Context->dbh();
630     my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)");
631     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
632
633     # changed the 'single_holidays' table, lets force/reset its cache
634     my $cache = Koha::Caches->get_instance();
635     $cache->clear_from_cache( 'single_holidays') ;
636     $cache->clear_from_cache( 'exception_holidays') ;
637 }
638
639 =head2 isHoliday
640
641     $isHoliday = isHoliday($day, $month $year);
642
643 C<$day> Is the day to check whether if is a holiday or not.
644
645 C<$month> Is the month to check whether if is a holiday or not.
646
647 C<$year> Is the year to check whether if is a holiday or not.
648
649 =cut
650
651 sub isHoliday {
652     my ($self, $day, $month, $year) = @_;
653         # FIXME - date strings are stored in non-padded metric format. should change to iso.
654         $month=$month+0;
655         $year=$year+0;
656         $day=$day+0;
657     my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
658     my $weekDays   = $self->get_week_days_holidays();
659     my $dayMonths  = $self->get_day_month_holidays();
660     my $exceptions = $self->get_exception_holidays();
661     my $singles    = $self->get_single_holidays();
662     if (defined($exceptions->{"$year/$month/$day"})) {
663         return 0;
664     } else {
665         if ((exists($weekDays->{$weekday})) ||
666             (exists($dayMonths->{"$month/$day"})) ||
667             (exists($singles->{"$year/$month/$day"}))) {
668             return 1;
669         } else {
670             return 0;
671         }
672     }
673
674 }
675
676 =head2 copy_to_branch
677
678     $calendar->copy_to_branch($target_branch)
679
680 =cut
681
682 sub copy_to_branch {
683     my ($self, $target_branch) = @_;
684
685     croak "No target_branch" unless $target_branch;
686
687     my $target_calendar = C4::Calendar->new(branchcode => $target_branch);
688
689     my ($y, $m, $d) = Today();
690     my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d;
691
692     my $wdh = $self->get_week_days_holidays;
693     $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } )
694       foreach keys %$wdh;
695     $target_calendar->insert_day_month_holiday(%$_)
696       foreach values %{ $self->get_day_month_holidays };
697     $target_calendar->insert_exception_holiday(%$_)
698       foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays };
699     $target_calendar->insert_single_holiday(%$_)
700       foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays };
701
702     return 1;
703 }
704
705 1;
706
707 __END__
708
709 =head1 AUTHOR
710
711 Koha Physics Library UNLP <matias_veleda@hotmail.com>
712
713 =cut