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};
108 $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
109 $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
111 $self->{'day_month_holidays'} = \%day_month_holidays;
113 my $special = $dbh->prepare( 'SELECT day, month, year, title, description
114 FROM special_holidays
115 WHERE ( branchcode = ? )
116 AND (isexception = ?)' );
117 $special->execute($branch,1);
118 my %exception_holidays;
119 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
120 $exception_holidays{"$year/$month/$day"}{title} = $title;
121 $exception_holidays{"$year/$month/$day"}{description} = $description;
122 $exception_holidays{"$year/$month/$day"}{date} =
123 sprintf("%04d-%02d-%02d", $year, $month, $day);
125 $self->{'exception_holidays'} = \%exception_holidays;
127 $special->execute($branch,0);
129 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
130 $single_holidays{"$year/$month/$day"}{title} = $title;
131 $single_holidays{"$year/$month/$day"}{description} = $description;
132 $single_holidays{"$year/$month/$day"}{date} =
133 sprintf("%04d-%02d-%02d", $year, $month, $day);
135 $self->{'single_holidays'} = \%single_holidays;
139 =item get_week_days_holidays
141 $week_days_holidays = $calendar->get_week_days_holidays();
143 Returns a hash reference to week days holidays.
147 sub get_week_days_holidays {
149 my $week_days_holidays = $self->{'week_days_holidays'};
150 return $week_days_holidays;
153 =item get_day_month_holidays
155 $day_month_holidays = $calendar->get_day_month_holidays();
157 Returns a hash reference to day month holidays.
161 sub get_day_month_holidays {
163 my $day_month_holidays = $self->{'day_month_holidays'};
164 return $day_month_holidays;
167 =item get_exception_holidays
169 $exception_holidays = $calendar->exception_holidays();
171 Returns a hash reference to exception holidays. This kind of days are those
172 which stands for a holiday, but you wanted to make an exception for this particular
177 sub get_exception_holidays {
179 my $exception_holidays = $self->{'exception_holidays'};
180 return $exception_holidays;
183 =item get_single_holidays
185 $single_holidays = $calendar->get_single_holidays();
187 Returns a hash reference to single holidays. This kind of holidays are those which
188 happend just one time.
192 sub get_single_holidays {
194 my $single_holidays = $self->{'single_holidays'};
195 return $single_holidays;
198 =item insert_week_day_holiday
200 insert_week_day_holiday(weekday => $weekday,
202 description => $description);
204 Inserts a new week day for $self->{branchcode}.
206 C<$day> Is the week day to make holiday.
208 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
210 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
214 sub insert_week_day_holiday {
218 my $dbh = C4::Context->dbh();
219 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
220 $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description});
221 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
222 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
226 =item insert_day_month_holiday
228 insert_day_month_holiday(day => $day,
231 description => $description);
233 Inserts a new day month holiday for $self->{branchcode}.
235 C<$day> Is the day month to make the date to insert.
237 C<$month> Is month to make the date to insert.
239 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
241 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
245 sub insert_day_month_holiday {
249 my $dbh = C4::Context->dbh();
250 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
251 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
252 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
253 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
257 =item insert_single_holiday
259 insert_single_holiday(day => $day,
263 description => $description);
265 Inserts a new single holiday for $self->{branchcode}.
267 C<$day> Is the day month to make the date to insert.
269 C<$month> Is month to make the date to insert.
271 C<$year> Is year to make the date to insert.
273 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
275 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
279 sub insert_single_holiday {
283 my $dbh = C4::Context->dbh();
285 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
286 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
287 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
288 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
292 =item insert_exception_holiday
294 insert_exception_holiday(day => $day,
298 description => $description);
300 Inserts a new exception holiday for $self->{branchcode}.
302 C<$day> Is the day month to make the date to insert.
304 C<$month> Is month to make the date to insert.
306 C<$year> Is year to make the date to insert.
308 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
310 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
314 sub insert_exception_holiday {
318 my $dbh = C4::Context->dbh();
320 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
321 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
322 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
323 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
329 delete_holiday(weekday => $weekday
334 Delete a holiday for $self->{branchcode}.
336 C<$weekday> Is the week day to delete.
338 C<$day> Is the day month to make the date to delete.
340 C<$month> Is month to make the date to delete.
342 C<$year> Is year to make the date to delete.
350 # Verify what kind of holiday that day is. For example, if it is
351 # a repeatable holiday, this should check if there are some exception
352 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
353 # ok just deleting it.
355 my $dbh = C4::Context->dbh();
356 my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
357 $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
358 if ($isSingleHoliday->rows) {
359 my $id = $isSingleHoliday->fetchrow;
360 $isSingleHoliday->finish; # Close the last query
362 my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
363 $deleteHoliday->execute($id);
364 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
366 $isSingleHoliday->finish; # Close the last query
368 my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
369 $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
370 if ($isWeekdayHoliday->rows) {
371 my $id = $isWeekdayHoliday->fetchrow;
372 $isWeekdayHoliday->finish; # Close the last query
374 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
375 $updateExceptions->execute($options{weekday}, $self->{branchcode});
376 $updateExceptions->finish; # Close the last query
378 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
379 $deleteHoliday->execute($id);
380 delete($self->{'week_days_holidays'}->{$options{weekday}});
382 $isWeekdayHoliday->finish; # Close the last query
384 my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
385 $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
386 if ($isDayMonthHoliday->rows) {
387 my $id = $isDayMonthHoliday->fetchrow;
388 $isDayMonthHoliday->finish;
389 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
390 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
391 $updateExceptions->finish; # Close the last query
393 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
394 $deleteHoliday->execute($id);
395 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
404 $isHoliday = isHoliday($day, $month $year);
407 C<$day> Is the day to check whether if is a holiday or not.
409 C<$month> Is the month to check whether if is a holiday or not.
411 C<$year> Is the year to check whether if is a holiday or not.
416 my ($self, $day, $month, $year) = @_;
417 # FIXME - date strings are stored in non-padded metric format. should change to iso.
418 # FIXME - should change arguments to accept C4::Dates object
422 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
423 my $weekDays = $self->get_week_days_holidays();
424 my $dayMonths = $self->get_day_month_holidays();
425 my $exceptions = $self->get_exception_holidays();
426 my $singles = $self->get_single_holidays();
427 if (defined($exceptions->{"$year/$month/$day"})) {
430 if ((exists($weekDays->{$weekday})) ||
431 (exists($dayMonths->{"$month/$day"})) ||
432 (exists($singles->{"$year/$month/$day"}))) {
443 my ($day, $month, $year) = $calendar->addDate($date, $offset)
445 C<$date> is a C4::Dates object representing the starting date of the interval.
447 C<$offset> Is the number of days that this function has to count from $date.
452 my ($self, $startdate, $offset) = @_;
453 my ($year,$month,$day) = split("-",$startdate->output('iso'));
455 if ($offset < 0) { # In case $offset is negative
456 # $offset = $offset*(-1);
459 my $daysMode = C4::Context->preference('useDaysMode');
460 if ($daysMode eq 'Datedue') {
461 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
462 while ($self->isHoliday($day, $month, $year)) {
463 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
465 } elsif($daysMode eq 'Calendar') {
466 while ($offset != 0) {
467 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
468 if (!($self->isHoliday($day, $month, $year))) {
469 $offset = $offset - $daystep;
472 } else { ## ($daysMode eq 'Days')
473 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
475 return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
480 my $daysBetween = $calendar->daysBetween($startdate, $enddate)
482 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
484 Returns the number of non-holiday days in the interval.
485 useDaysMode syspref has no effect here.
488 sub daysBetween ($$$) {
489 my $self = shift or return undef;
490 my $startdate = shift or return undef;
491 my $enddate = shift or return undef;
492 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
493 my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso'));
494 if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
496 # we don't go backwards ( FIXME - handle this error better )
500 ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
501 unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
504 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
517 Koha Physics Library UNLP <matias_veleda@hotmail.com>