3 # This file is part of Koha.
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.
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.
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>.
20 use vars qw($VERSION @EXPORT);
23 use Date::Calc qw( Date_to_Days Today);
27 use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
30 C4::Calendar::Calendar - Koha module dealing with holidays.
34 use C4::Calendar::Calendar;
38 This package is used to deal with holidays. Through this package, you can set
39 all kind of holidays for the library.
45 $calendar = C4::Calendar->new(branchcode => $branchcode);
47 Each library branch has its own Calendar.
48 C<$branchcode> specifies which Calendar you want.
53 my $classname = shift @_;
55 my $self = bless({}, $classname);
56 foreach my $optionName (keys %options) {
57 $self->{lc($optionName)} = $options{$optionName};
59 defined($self->{branchcode}) or croak "No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)";
60 $self->_init($self->{branchcode});
67 defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow ""
68 my $dbh = C4::Context->dbh();
69 my $repeatable = $dbh->prepare( 'SELECT *
70 FROM repeatable_holidays
71 WHERE ( branchcode = ? )
72 AND (ISNULL(weekday) = ?)' );
73 $repeatable->execute($branch,0);
74 my %week_days_holidays;
75 while (my $row = $repeatable->fetchrow_hashref) {
76 my $key = $row->{weekday};
77 $week_days_holidays{$key}{title} = $row->{title};
78 $week_days_holidays{$key}{description} = $row->{description};
80 $self->{'week_days_holidays'} = \%week_days_holidays;
82 $repeatable->execute($branch,1);
83 my %day_month_holidays;
84 while (my $row = $repeatable->fetchrow_hashref) {
85 my $key = $row->{month} . "/" . $row->{day};
86 $day_month_holidays{$key}{title} = $row->{title};
87 $day_month_holidays{$key}{description} = $row->{description};
88 $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
89 $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
91 $self->{'day_month_holidays'} = \%day_month_holidays;
93 my $special = $dbh->prepare( 'SELECT day, month, year, title, description
95 WHERE ( branchcode = ? )
96 AND (isexception = ?)' );
97 $special->execute($branch,1);
98 my %exception_holidays;
99 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
100 $exception_holidays{"$year/$month/$day"}{title} = $title;
101 $exception_holidays{"$year/$month/$day"}{description} = $description;
102 $exception_holidays{"$year/$month/$day"}{date} =
103 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
105 $self->{'exception_holidays'} = \%exception_holidays;
107 $special->execute($branch,0);
109 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
110 $single_holidays{"$year/$month/$day"}{title} = $title;
111 $single_holidays{"$year/$month/$day"}{description} = $description;
112 $single_holidays{"$year/$month/$day"}{date} =
113 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
115 $self->{'single_holidays'} = \%single_holidays;
119 =head2 get_week_days_holidays
121 $week_days_holidays = $calendar->get_week_days_holidays();
123 Returns a hash reference to week days holidays.
127 sub get_week_days_holidays {
129 my $week_days_holidays = $self->{'week_days_holidays'};
130 return $week_days_holidays;
133 =head2 get_day_month_holidays
135 $day_month_holidays = $calendar->get_day_month_holidays();
137 Returns a hash reference to day month holidays.
141 sub get_day_month_holidays {
143 my $day_month_holidays = $self->{'day_month_holidays'};
144 return $day_month_holidays;
147 =head2 get_exception_holidays
149 $exception_holidays = $calendar->exception_holidays();
151 Returns a hash reference to exception holidays. This kind of days are those
152 which stands for a holiday, but you wanted to make an exception for this particular
157 sub get_exception_holidays {
159 my $exception_holidays = $self->{'exception_holidays'};
160 return $exception_holidays;
163 =head2 get_single_holidays
165 $single_holidays = $calendar->get_single_holidays();
167 Returns a hash reference to single holidays. This kind of holidays are those which
168 happend just one time.
172 sub get_single_holidays {
174 my $single_holidays = $self->{'single_holidays'};
175 return $single_holidays;
178 =head2 insert_week_day_holiday
180 insert_week_day_holiday(weekday => $weekday,
182 description => $description);
184 Inserts a new week day for $self->{branchcode}.
186 C<$day> Is the week day to make holiday.
188 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
190 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
194 sub insert_week_day_holiday {
198 my $weekday = $options{weekday};
199 croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/;
201 my $dbh = C4::Context->dbh();
202 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
203 $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description});
204 $self->{'week_days_holidays'}->{$weekday}{title} = $options{title};
205 $self->{'week_days_holidays'}->{$weekday}{description} = $options{description};
209 =head2 insert_day_month_holiday
211 insert_day_month_holiday(day => $day,
214 description => $description);
216 Inserts a new day month holiday for $self->{branchcode}.
218 C<$day> Is the day month to make the date to insert.
220 C<$month> Is month to make the date to insert.
222 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
224 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
228 sub insert_day_month_holiday {
232 my $dbh = C4::Context->dbh();
233 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
234 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
235 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
236 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
240 =head2 insert_single_holiday
242 insert_single_holiday(day => $day,
246 description => $description);
248 Inserts a new single holiday for $self->{branchcode}.
250 C<$day> Is the day month to make the date to insert.
252 C<$month> Is month to make the date to insert.
254 C<$year> Is year to make the date to insert.
256 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
258 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
262 sub insert_single_holiday {
266 @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
267 if $options{date} && !$options{day};
269 my $dbh = C4::Context->dbh();
271 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
272 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
273 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
274 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
278 =head2 insert_exception_holiday
280 insert_exception_holiday(day => $day,
284 description => $description);
286 Inserts a new exception holiday for $self->{branchcode}.
288 C<$day> Is the day month to make the date to insert.
290 C<$month> Is month to make the date to insert.
292 C<$year> Is year to make the date to insert.
294 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
296 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
300 sub insert_exception_holiday {
304 @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
305 if $options{date} && !$options{day};
307 my $dbh = C4::Context->dbh();
309 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
310 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
311 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
312 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
316 =head2 ModWeekdayholiday
318 ModWeekdayholiday(weekday =>$weekday,
320 description => $description)
322 Modifies the title and description of a weekday for $self->{branchcode}.
324 C<$weekday> Is the title to update for the holiday.
326 C<$description> Is the description to update for the holiday.
330 sub ModWeekdayholiday {
334 my $dbh = C4::Context->dbh();
335 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
336 $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday});
337 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
338 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
342 =head2 ModDaymonthholiday
344 ModDaymonthholiday(day => $day,
347 description => $description);
349 Modifies the title and description for a day/month holiday for $self->{branchcode}.
351 C<$day> The day of the month for the update.
353 C<$month> The month to be used for the update.
355 C<$title> The title to be updated for the holiday.
357 C<$description> The description to be update for the holiday.
361 sub ModDaymonthholiday {
365 my $dbh = C4::Context->dbh();
366 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
367 $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode});
368 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
369 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
373 =head2 ModSingleholiday
375 ModSingleholiday(day => $day,
379 description => $description);
381 Modifies the title and description for a single holiday for $self->{branchcode}.
383 C<$day> Is the day of the month to make the update.
385 C<$month> Is the month to make the update.
387 C<$year> Is the year to make the update.
389 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
391 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
395 sub ModSingleholiday {
399 my $dbh = C4::Context->dbh();
401 my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
402 $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);
403 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
404 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
408 =head2 ModExceptionholiday
410 ModExceptionholiday(day => $day,
414 description => $description);
416 Modifies the title and description for an exception holiday for $self->{branchcode}.
418 C<$day> Is the day of the month for the holiday.
420 C<$month> Is the month for the holiday.
422 C<$year> Is the year for the holiday.
424 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
426 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
430 sub ModExceptionholiday {
434 my $dbh = C4::Context->dbh();
436 my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
437 $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);
438 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
439 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
443 =head2 delete_holiday
445 delete_holiday(weekday => $weekday
450 Delete a holiday for $self->{branchcode}.
452 C<$weekday> Is the week day to delete.
454 C<$day> Is the day month to make the date to delete.
456 C<$month> Is month to make the date to delete.
458 C<$year> Is year to make the date to delete.
466 # Verify what kind of holiday that day is. For example, if it is
467 # a repeatable holiday, this should check if there are some exception
468 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
469 # ok just deleting it.
471 my $dbh = C4::Context->dbh();
472 my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
473 $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
474 if ($isSingleHoliday->rows) {
475 my $id = $isSingleHoliday->fetchrow;
476 $isSingleHoliday->finish; # Close the last query
478 my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
479 $deleteHoliday->execute($id);
480 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
482 $isSingleHoliday->finish; # Close the last query
484 my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
485 $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
486 if ($isWeekdayHoliday->rows) {
487 my $id = $isWeekdayHoliday->fetchrow;
488 $isWeekdayHoliday->finish; # Close the last query
490 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
491 $updateExceptions->execute($options{weekday}, $self->{branchcode});
492 $updateExceptions->finish; # Close the last query
494 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
495 $deleteHoliday->execute($id);
496 delete($self->{'week_days_holidays'}->{$options{weekday}});
498 $isWeekdayHoliday->finish; # Close the last query
500 my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
501 $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
502 if ($isDayMonthHoliday->rows) {
503 my $id = $isDayMonthHoliday->fetchrow;
504 $isDayMonthHoliday->finish;
505 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
506 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
507 $updateExceptions->finish; # Close the last query
509 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
510 $deleteHoliday->execute($id);
511 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
517 =head2 delete_holiday_range
519 delete_holiday_range(day => $day,
523 Delete a holiday range of dates for $self->{branchcode}.
525 C<$day> Is the day month to make the date to delete.
527 C<$month> Is month to make the date to delete.
529 C<$year> Is year to make the date to delete.
533 sub delete_holiday_range {
537 my $dbh = C4::Context->dbh();
538 my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
539 $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
542 =head2 delete_holiday_range_repeatable
544 delete_holiday_range_repeatable(day => $day,
547 Delete a holiday for $self->{branchcode}.
549 C<$day> Is the day month to make the date to delete.
551 C<$month> Is month to make the date to delete.
555 sub delete_holiday_range_repeatable {
559 my $dbh = C4::Context->dbh();
560 my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
561 $sth->execute($self->{branchcode}, $options{day}, $options{month});
564 =head2 delete_exception_holiday_range
566 delete_exception_holiday_range(weekday => $weekday
571 Delete a holiday for $self->{branchcode}.
573 C<$day> Is the day month to make the date to delete.
575 C<$month> Is month to make the date to delete.
577 C<$year> Is year to make the date to delete.
581 sub delete_exception_holiday_range {
585 my $dbh = C4::Context->dbh();
586 my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)");
587 $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
592 $isHoliday = isHoliday($day, $month $year);
594 C<$day> Is the day to check whether if is a holiday or not.
596 C<$month> Is the month to check whether if is a holiday or not.
598 C<$year> Is the year to check whether if is a holiday or not.
603 my ($self, $day, $month, $year) = @_;
604 # FIXME - date strings are stored in non-padded metric format. should change to iso.
605 # FIXME - should change arguments to accept C4::Dates object
609 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
610 my $weekDays = $self->get_week_days_holidays();
611 my $dayMonths = $self->get_day_month_holidays();
612 my $exceptions = $self->get_exception_holidays();
613 my $singles = $self->get_single_holidays();
614 if (defined($exceptions->{"$year/$month/$day"})) {
617 if ((exists($weekDays->{$weekday})) ||
618 (exists($dayMonths->{"$month/$day"})) ||
619 (exists($singles->{"$year/$month/$day"}))) {
628 =head2 copy_to_branch
630 $calendar->copy_to_branch($target_branch)
635 my ($self, $target_branch) = @_;
637 croak "No target_branch" unless $target_branch;
639 my $target_calendar = C4::Calendar->new(branchcode => $target_branch);
641 my ($y, $m, $d) = Today();
642 my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d;
644 my $wdh = $self->get_week_days_holidays;
645 $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } )
647 $target_calendar->insert_day_month_holiday(%$_)
648 foreach values %{ $self->get_day_month_holidays };
649 $target_calendar->insert_exception_holiday(%$_)
650 foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays };
651 $target_calendar->insert_single_holiday(%$_)
652 foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays };
659 my ($day, $month, $year) = $calendar->addDate($date, $offset)
661 C<$date> is a C4::Dates object representing the starting date of the interval.
663 C<$offset> Is the number of days that this function has to count from $date.
668 my ($self, $startdate, $offset) = @_;
669 my ($year,$month,$day) = split("-",$startdate->output('iso'));
671 if ($offset < 0) { # In case $offset is negative
672 # $offset = $offset*(-1);
675 my $daysMode = C4::Context->preference('useDaysMode');
676 if ($daysMode eq 'Datedue') {
677 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
678 while ($self->isHoliday($day, $month, $year)) {
679 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
681 } elsif($daysMode eq 'Calendar') {
682 while ($offset != 0) {
683 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
684 if (!($self->isHoliday($day, $month, $year))) {
685 $offset = $offset - $daystep;
688 } else { ## ($daysMode eq 'Days')
689 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
691 return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso'));
696 my $daysBetween = $calendar->daysBetween($startdate, $enddate)
698 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
700 Returns the number of non-holiday days in the interval.
701 useDaysMode syspref has no effect here.
705 my $self = shift or return;
706 my $startdate = shift or return;
707 my $enddate = shift or return;
708 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
709 my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso'));
710 if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
712 # we don't go backwards ( FIXME - handle this error better )
716 ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
717 unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
720 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
731 Koha Physics Library UNLP <matias_veleda@hotmail.com>