3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
20 use vars qw($VERSION @EXPORT);
23 use Date::Calc qw( Date_to_Days );
28 # set the version for version checking
32 &get_week_days_holidays
33 &get_day_month_holidays
34 &get_exception_holidays
36 &insert_week_day_holiday
37 &insert_day_month_holiday
38 &insert_single_holiday
39 &insert_exception_holiday
49 C4::Calendar::Calendar - Koha module dealing with holidays.
53 use C4::Calendar::Calendar;
57 This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library.
65 $calendar = C4::Calendar->new(branchcode => $branchcode);
67 Each library branch has its own Calendar.
68 C<$branchcode> specifies which Calendar you want.
73 my $classname = shift @_;
75 my $self = bless({}, $classname);
76 foreach my $optionName (keys %options) {
77 $self->{lc($optionName)} = $options{$optionName};
79 defined($self->{branchcode}) or croak "No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)";
80 $self->_init($self->{branchcode});
87 defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow ""
88 my $dbh = C4::Context->dbh();
89 my $repeatable = $dbh->prepare( 'SELECT *
90 FROM repeatable_holidays
91 WHERE ( branchcode = ? )
92 AND (ISNULL(weekday) = ?)' );
93 $repeatable->execute($branch,0);
94 my %week_days_holidays;
95 while (my $row = $repeatable->fetchrow_hashref) {
96 my $key = $row->{weekday};
97 $week_days_holidays{$key}{title} = $row->{title};
98 $week_days_holidays{$key}{description} = $row->{description};
100 $self->{'week_days_holidays'} = \%week_days_holidays;
102 $repeatable->execute($branch,1);
103 my %day_month_holidays;
104 while (my $row = $repeatable->fetchrow_hashref) {
105 my $key = $row->{month} . "/" . $row->{day};
106 $day_month_holidays{$key}{title} = $row->{title};
107 $day_month_holidays{$key}{description} = $row->{description}
109 $self->{'day_month_holidays'} = \%day_month_holidays;
111 my $special = $dbh->prepare( 'SELECT day, month, year, title, description
112 FROM special_holidays
113 WHERE ( branchcode = ? )
114 AND (isexception = ?)' );
115 $special->execute($branch,1);
116 my %exception_holidays;
117 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
118 $exception_holidays{"$year/$month/$day"}{title} = $title;
119 $exception_holidays{"$year/$month/$day"}{description} = $description;
121 $self->{'exception_holidays'} = \%exception_holidays;
123 $special->execute($branch,0);
125 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
126 $single_holidays{"$year/$month/$day"}{title} = $title;
127 $single_holidays{"$year/$month/$day"}{description} = $description;
129 $self->{'single_holidays'} = \%single_holidays;
133 =item get_week_days_holidays
135 $week_days_holidays = $calendar->get_week_days_holidays();
137 Returns a hash reference to week days holidays.
141 sub get_week_days_holidays {
143 my $week_days_holidays = $self->{'week_days_holidays'};
144 return $week_days_holidays;
147 =item get_day_month_holidays
149 $day_month_holidays = $calendar->get_day_month_holidays();
151 Returns a hash reference to day month holidays.
155 sub get_day_month_holidays {
157 my $day_month_holidays = $self->{'day_month_holidays'};
158 return $day_month_holidays;
161 =item get_exception_holidays
163 $exception_holidays = $calendar->exception_holidays();
165 Returns a hash reference to exception holidays. This kind of days are those
166 which stands for a holiday, but you wanted to make an exception for this particular
171 sub get_exception_holidays {
173 my $exception_holidays = $self->{'exception_holidays'};
174 return $exception_holidays;
177 =item get_single_holidays
179 $single_holidays = $calendar->get_single_holidays();
181 Returns a hash reference to single holidays. This kind of holidays are those which
182 happend just one time.
186 sub get_single_holidays {
188 my $single_holidays = $self->{'single_holidays'};
189 return $single_holidays;
192 =item insert_week_day_holiday
194 insert_week_day_holiday(weekday => $weekday,
196 description => $description);
198 Inserts a new week day for $self->{branchcode}.
200 C<$day> Is the week day to make holiday.
202 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
204 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
208 sub insert_week_day_holiday {
212 my $dbh = C4::Context->dbh();
213 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
214 $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description});
215 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
216 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
220 =item insert_day_month_holiday
222 insert_day_month_holiday(day => $day,
225 description => $description);
227 Inserts a new day month holiday for $self->{branchcode}.
229 C<$day> Is the day month to make the date to insert.
231 C<$month> Is month to make the date to insert.
233 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
235 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
239 sub insert_day_month_holiday {
243 my $dbh = C4::Context->dbh();
244 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
245 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
246 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
247 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
251 =item insert_single_holiday
253 insert_single_holiday(day => $day,
257 description => $description);
259 Inserts a new single holiday for $self->{branchcode}.
261 C<$day> Is the day month to make the date to insert.
263 C<$month> Is month to make the date to insert.
265 C<$year> Is year to make the date to insert.
267 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
269 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
273 sub insert_single_holiday {
277 my $dbh = C4::Context->dbh();
279 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
280 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
281 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
282 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
286 =item insert_exception_holiday
288 insert_exception_holiday(day => $day,
292 description => $description);
294 Inserts a new exception holiday for $self->{branchcode}.
296 C<$day> Is the day month to make the date to insert.
298 C<$month> Is month to make the date to insert.
300 C<$year> Is year to make the date to insert.
302 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
304 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
308 sub insert_exception_holiday {
312 my $dbh = C4::Context->dbh();
314 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
315 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
316 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
317 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
323 delete_holiday(weekday => $weekday
328 Delete a holiday for $self->{branchcode}.
330 C<$weekday> Is the week day to delete.
332 C<$day> Is the day month to make the date to delete.
334 C<$month> Is month to make the date to delete.
336 C<$year> Is year to make the date to delete.
344 # Verify what kind of holiday that day is. For example, if it is
345 # a repeatable holiday, this should check if there are some exception
346 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
347 # ok just deleting it.
349 my $dbh = C4::Context->dbh();
350 my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
351 $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
352 if ($isSingleHoliday->rows) {
353 my $id = $isSingleHoliday->fetchrow;
354 $isSingleHoliday->finish; # Close the last query
356 my $deleteHoliday = $dbh->prepare("SELECT FROM special_holidays WHERE id = ?");
357 $deleteHoliday->execute($id);
358 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
360 $isSingleHoliday->finish; # Close the last query
362 my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
363 $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
364 if ($isWeekdayHoliday->rows) {
365 my $id = $isWeekdayHoliday->fetchrow;
366 $isWeekdayHoliday->finish; # Close the last query
368 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
369 $updateExceptions->execute($options{weekday}, $self->{branchcode});
370 $updateExceptions->finish; # Close the last query
372 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
373 $deleteHoliday->execute($id);
374 delete($self->{'week_days_holidays'}->{$options{weekday}});
376 $isWeekdayHoliday->finish; # Close the last query
378 my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
379 $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
380 if ($isDayMonthHoliday->rows) {
381 my $id = $isDayMonthHoliday->fetchrow;
382 $isDayMonthHoliday->finish;
383 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
384 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
385 $updateExceptions->finish; # Close the last query
387 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
388 $deleteHoliday->execute($id);
389 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
398 $isHoliday = isHoliday($day, $month $year);
401 C<$day> Is the day to check whether if is a holiday or not.
403 C<$month> Is the month to check whether if is a holiday or not.
405 C<$year> Is the year to check whether if is a holiday or not.
410 my ($self, $day, $month, $year) = @_;
411 # FIXME - date strings are stored in non-padded metric format. should change to iso.
412 # FIXME - should change arguments to accept C4::Dates object
416 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
417 my $weekDays = $self->get_week_days_holidays();
418 my $dayMonths = $self->get_day_month_holidays();
419 my $exceptions = $self->get_exception_holidays();
420 my $singles = $self->get_single_holidays();
421 if (defined($exceptions->{"$year/$month/$day"})) {
424 if ((exists($weekDays->{$weekday})) ||
425 (exists($dayMonths->{"$month/$day"})) ||
426 (exists($singles->{"$year/$month/$day"}))) {
437 my ($day, $month, $year) = $calendar->addDate($date, $offset)
439 C<$date> is a C4::Dates object representing the starting date of the interval.
441 C<$offset> Is the number of days that this function has to count from $date.
446 my ($self, $startdate, $offset) = @_;
447 my ($year,$month,$day) = split("-",$startdate->output('iso'));
449 if ($offset < 0) { # In case $offset is negative
450 # $offset = $offset*(-1);
453 my $daysMode = C4::Context->preference('useDaysMode');
454 if ($daysMode eq 'Datedue') {
455 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
456 while ($self->isHoliday($day, $month, $year)) {
457 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
459 } elsif($daysMode eq 'Calendar') {
460 while ($offset != 0) {
461 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
462 if (!($self->isHoliday($day, $month, $year))) {
463 $offset = $offset - $daystep;
466 } else { ## ($daysMode eq 'Days')
467 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
469 return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
474 my $daysBetween = $calendar->daysBetween($startdate, $enddate)
476 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
478 Returns the number of non-holiday days in the interval.
479 useDaysMode syspref has no effect here.
482 sub daysBetween ($$$) {
483 my $self = shift or return undef;
484 my $startdate = shift or return undef;
485 my $enddate = shift or return undef;
486 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
487 my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso'));
488 if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
490 # we don't go backwards ( FIXME - handle this error better )
494 ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
495 unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
498 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
511 Koha Physics Library UNLP <matias_veleda@hotmail.com>