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
53 C4::Calendar::Calendar - Koha module dealing with holidays.
57 use C4::Calendar::Calendar;
61 This package is used to deal with holidays. Through this package, you can set
62 all kind of holidays for the library.
68 $calendar = C4::Calendar->new(branchcode => $branchcode);
70 Each library branch has its own Calendar.
71 C<$branchcode> specifies which Calendar you want.
76 my $classname = shift @_;
78 my $self = bless({}, $classname);
79 foreach my $optionName (keys %options) {
80 $self->{lc($optionName)} = $options{$optionName};
82 defined($self->{branchcode
}) or croak
"No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)";
83 $self->_init($self->{branchcode
});
90 defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow ""
91 my $dbh = C4
::Context
->dbh();
92 my $repeatable = $dbh->prepare( 'SELECT *
93 FROM repeatable_holidays
94 WHERE ( branchcode = ? )
95 AND (ISNULL(weekday) = ?)' );
96 $repeatable->execute($branch,0);
97 my %week_days_holidays;
98 while (my $row = $repeatable->fetchrow_hashref) {
99 my $key = $row->{weekday
};
100 $week_days_holidays{$key}{title
} = $row->{title
};
101 $week_days_holidays{$key}{description
} = $row->{description
};
103 $self->{'week_days_holidays'} = \
%week_days_holidays;
105 $repeatable->execute($branch,1);
106 my %day_month_holidays;
107 while (my $row = $repeatable->fetchrow_hashref) {
108 my $key = $row->{month
} . "/" . $row->{day
};
109 $day_month_holidays{$key}{title
} = $row->{title
};
110 $day_month_holidays{$key}{description
} = $row->{description
};
111 $day_month_holidays{$key}{day
} = sprintf("%02d", $row->{day
});
112 $day_month_holidays{$key}{month
} = sprintf("%02d", $row->{month
});
114 $self->{'day_month_holidays'} = \
%day_month_holidays;
116 my $special = $dbh->prepare( 'SELECT day, month, year, title, description
117 FROM special_holidays
118 WHERE ( branchcode = ? )
119 AND (isexception = ?)' );
120 $special->execute($branch,1);
121 my %exception_holidays;
122 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
123 $exception_holidays{"$year/$month/$day"}{title
} = $title;
124 $exception_holidays{"$year/$month/$day"}{description
} = $description;
125 $exception_holidays{"$year/$month/$day"}{date
} =
126 sprintf("%04d-%02d-%02d", $year, $month, $day);
128 $self->{'exception_holidays'} = \
%exception_holidays;
130 $special->execute($branch,0);
132 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
133 $single_holidays{"$year/$month/$day"}{title
} = $title;
134 $single_holidays{"$year/$month/$day"}{description
} = $description;
135 $single_holidays{"$year/$month/$day"}{date
} =
136 sprintf("%04d-%02d-%02d", $year, $month, $day);
138 $self->{'single_holidays'} = \
%single_holidays;
142 =head2 get_week_days_holidays
144 $week_days_holidays = $calendar->get_week_days_holidays();
146 Returns a hash reference to week days holidays.
150 sub get_week_days_holidays
{
152 my $week_days_holidays = $self->{'week_days_holidays'};
153 return $week_days_holidays;
156 =head2 get_day_month_holidays
158 $day_month_holidays = $calendar->get_day_month_holidays();
160 Returns a hash reference to day month holidays.
164 sub get_day_month_holidays
{
166 my $day_month_holidays = $self->{'day_month_holidays'};
167 return $day_month_holidays;
170 =head2 get_exception_holidays
172 $exception_holidays = $calendar->exception_holidays();
174 Returns a hash reference to exception holidays. This kind of days are those
175 which stands for a holiday, but you wanted to make an exception for this particular
180 sub get_exception_holidays
{
182 my $exception_holidays = $self->{'exception_holidays'};
183 return $exception_holidays;
186 =head2 get_single_holidays
188 $single_holidays = $calendar->get_single_holidays();
190 Returns a hash reference to single holidays. This kind of holidays are those which
191 happend just one time.
195 sub get_single_holidays
{
197 my $single_holidays = $self->{'single_holidays'};
198 return $single_holidays;
201 =head2 insert_week_day_holiday
203 insert_week_day_holiday(weekday => $weekday,
205 description => $description);
207 Inserts a new week day for $self->{branchcode}.
209 C<$day> Is the week day to make holiday.
211 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
213 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
217 sub insert_week_day_holiday
{
221 my $dbh = C4
::Context
->dbh();
222 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
223 $insertHoliday->execute( $self->{branchcode
}, $options{weekday
},$options{title
}, $options{description
});
224 $self->{'week_days_holidays'}->{$options{weekday
}}{title
} = $options{title
};
225 $self->{'week_days_holidays'}->{$options{weekday
}}{description
} = $options{description
};
229 =head2 insert_day_month_holiday
231 insert_day_month_holiday(day => $day,
234 description => $description);
236 Inserts a new day month holiday for $self->{branchcode}.
238 C<$day> Is the day month to make the date to insert.
240 C<$month> Is month to make the date to insert.
242 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
244 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
248 sub insert_day_month_holiday
{
252 my $dbh = C4
::Context
->dbh();
253 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
254 $insertHoliday->execute( $self->{branchcode
}, $options{day
},$options{month
},$options{title
}, $options{description
});
255 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title
} = $options{title
};
256 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description
} = $options{description
};
260 =head2 insert_single_holiday
262 insert_single_holiday(day => $day,
266 description => $description);
268 Inserts a new single holiday for $self->{branchcode}.
270 C<$day> Is the day month to make the date to insert.
272 C<$month> Is month to make the date to insert.
274 C<$year> Is year to make the date to insert.
276 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
278 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
282 sub insert_single_holiday
{
286 my $dbh = C4
::Context
->dbh();
288 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
289 $insertHoliday->execute( $self->{branchcode
}, $options{day
},$options{month
},$options{year
}, $isexception, $options{title
}, $options{description
});
290 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title
} = $options{title
};
291 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description
} = $options{description
};
295 =head2 insert_exception_holiday
297 insert_exception_holiday(day => $day,
301 description => $description);
303 Inserts a new exception holiday for $self->{branchcode}.
305 C<$day> Is the day month to make the date to insert.
307 C<$month> Is month to make the date to insert.
309 C<$year> Is year to make the date to insert.
311 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
313 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
317 sub insert_exception_holiday
{
321 my $dbh = C4
::Context
->dbh();
323 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
324 $insertException->execute( $self->{branchcode
}, $options{day
},$options{month
},$options{year
}, $isexception, $options{title
}, $options{description
});
325 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title
} = $options{title
};
326 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description
} = $options{description
};
330 =head2 ModWeekdayholiday
332 ModWeekdayholiday(weekday =>$weekday,
334 description => $description)
336 Modifies the title and description of a weekday for $self->{branchcode}.
338 C<$weekday> Is the title to update for the holiday.
340 C<$description> Is the description to update for the holiday.
344 sub ModWeekdayholiday
{
348 my $dbh = C4
::Context
->dbh();
349 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
350 $updateHoliday->execute( $options{title
},$options{description
},$self->{branchcode
},$options{weekday
});
351 $self->{'week_days_holidays'}->{$options{weekday
}}{title
} = $options{title
};
352 $self->{'week_days_holidays'}->{$options{weekday
}}{description
} = $options{description
};
356 =head2 ModDaymonthholiday
358 ModDaymonthholiday(day => $day,
361 description => $description);
363 Modifies the title and description for a day/month holiday for $self->{branchcode}.
365 C<$day> The day of the month for the update.
367 C<$month> The month to be used for the update.
369 C<$title> The title to be updated for the holiday.
371 C<$description> The description to be update for the holiday.
375 sub ModDaymonthholiday
{
379 my $dbh = C4
::Context
->dbh();
380 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
381 $updateHoliday->execute( $options{title
},$options{description
},$options{month
},$options{day
},$self->{branchcode
});
382 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title
} = $options{title
};
383 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description
} = $options{description
};
387 =head2 ModSingleholiday
389 ModSingleholiday(day => $day,
393 description => $description);
395 Modifies the title and description for a single holiday for $self->{branchcode}.
397 C<$day> Is the day of the month to make the update.
399 C<$month> Is the month to make the update.
401 C<$year> Is the year to make the update.
403 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
405 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
409 sub ModSingleholiday
{
413 my $dbh = C4
::Context
->dbh();
415 my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
416 $updateHoliday->execute($options{title
},$options{description
},$options{day
},$options{month
},$options{year
},$self->{branchcode
},$isexception);
417 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title
} = $options{title
};
418 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description
} = $options{description
};
422 =head2 ModExceptionholiday
424 ModExceptionholiday(day => $day,
428 description => $description);
430 Modifies the title and description for an exception holiday for $self->{branchcode}.
432 C<$day> Is the day of the month for the holiday.
434 C<$month> Is the month for the holiday.
436 C<$year> Is the year for the holiday.
438 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
440 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
444 sub ModExceptionholiday
{
448 my $dbh = C4
::Context
->dbh();
450 my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
451 $updateHoliday->execute($options{title
},$options{description
},$options{day
},$options{month
},$options{year
},$self->{branchcode
},$isexception);
452 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title
} = $options{title
};
453 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description
} = $options{description
};
457 =head2 delete_holiday
459 delete_holiday(weekday => $weekday
464 Delete a holiday for $self->{branchcode}.
466 C<$weekday> Is the week day to delete.
468 C<$day> Is the day month to make the date to delete.
470 C<$month> Is month to make the date to delete.
472 C<$year> Is year to make the date to delete.
480 # Verify what kind of holiday that day is. For example, if it is
481 # a repeatable holiday, this should check if there are some exception
482 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
483 # ok just deleting it.
485 my $dbh = C4
::Context
->dbh();
486 my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
487 $isSingleHoliday->execute($self->{branchcode
}, $options{day
}, $options{month
}, $options{year
});
488 if ($isSingleHoliday->rows) {
489 my $id = $isSingleHoliday->fetchrow;
490 $isSingleHoliday->finish; # Close the last query
492 my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
493 $deleteHoliday->execute($id);
494 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
496 $isSingleHoliday->finish; # Close the last query
498 my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
499 $isWeekdayHoliday->execute($self->{branchcode
}, $options{weekday
});
500 if ($isWeekdayHoliday->rows) {
501 my $id = $isWeekdayHoliday->fetchrow;
502 $isWeekdayHoliday->finish; # Close the last query
504 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
505 $updateExceptions->execute($options{weekday
}, $self->{branchcode
});
506 $updateExceptions->finish; # Close the last query
508 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
509 $deleteHoliday->execute($id);
510 delete($self->{'week_days_holidays'}->{$options{weekday
}});
512 $isWeekdayHoliday->finish; # Close the last query
514 my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
515 $isDayMonthHoliday->execute($self->{branchcode
}, $options{day
}, $options{month
});
516 if ($isDayMonthHoliday->rows) {
517 my $id = $isDayMonthHoliday->fetchrow;
518 $isDayMonthHoliday->finish;
519 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
520 $updateExceptions->execute($self->{branchcode
}, $options{day
}, $options{month
});
521 $updateExceptions->finish; # Close the last query
523 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
524 $deleteHoliday->execute($id);
525 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
534 $isHoliday = isHoliday($day, $month $year);
536 C<$day> Is the day to check whether if is a holiday or not.
538 C<$month> Is the month to check whether if is a holiday or not.
540 C<$year> Is the year to check whether if is a holiday or not.
545 my ($self, $day, $month, $year) = @_;
546 # FIXME - date strings are stored in non-padded metric format. should change to iso.
547 # FIXME - should change arguments to accept C4::Dates object
551 my $weekday = &Date
::Calc
::Day_of_Week
($year, $month, $day) % 7;
552 my $weekDays = $self->get_week_days_holidays();
553 my $dayMonths = $self->get_day_month_holidays();
554 my $exceptions = $self->get_exception_holidays();
555 my $singles = $self->get_single_holidays();
556 if (defined($exceptions->{"$year/$month/$day"})) {
559 if ((exists($weekDays->{$weekday})) ||
560 (exists($dayMonths->{"$month/$day"})) ||
561 (exists($singles->{"$year/$month/$day"}))) {
572 my ($day, $month, $year) = $calendar->addDate($date, $offset)
574 C<$date> is a C4::Dates object representing the starting date of the interval.
576 C<$offset> Is the number of days that this function has to count from $date.
581 my ($self, $startdate, $offset) = @_;
582 my ($year,$month,$day) = split("-",$startdate->output('iso'));
584 if ($offset < 0) { # In case $offset is negative
585 # $offset = $offset*(-1);
588 my $daysMode = C4
::Context
->preference('useDaysMode');
589 if ($daysMode eq 'Datedue') {
590 ($year, $month, $day) = &Date
::Calc
::Add_Delta_Days
($year, $month, $day, $offset );
591 while ($self->isHoliday($day, $month, $year)) {
592 ($year, $month, $day) = &Date
::Calc
::Add_Delta_Days
($year, $month, $day, $daystep);
594 } elsif($daysMode eq 'Calendar') {
595 while ($offset != 0) {
596 ($year, $month, $day) = &Date
::Calc
::Add_Delta_Days
($year, $month, $day, $daystep);
597 if (!($self->isHoliday($day, $month, $year))) {
598 $offset = $offset - $daystep;
601 } else { ## ($daysMode eq 'Days')
602 ($year, $month, $day) = &Date
::Calc
::Add_Delta_Days
($year, $month, $day, $offset );
604 return(C4
::Dates
->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
609 my $daysBetween = $calendar->daysBetween($startdate, $enddate)
611 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
613 Returns the number of non-holiday days in the interval.
614 useDaysMode syspref has no effect here.
617 sub daysBetween
($$$) {
618 my $self = shift or return undef;
619 my $startdate = shift or return undef;
620 my $enddate = shift or return undef;
621 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
622 my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso'));
623 if (Date_to_Days
($yearFrom,$monthFrom,$dayFrom) > Date_to_Days
($yearTo,$monthTo,$dayTo)) {
625 # we don't go backwards ( FIXME - handle this error better )
629 ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
630 unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
633 ($yearFrom, $monthFrom, $dayFrom) = &Date
::Calc
::Add_Delta_Days
($yearFrom, $monthFrom, $dayFrom, 1);
644 Koha Physics Library UNLP <matias_veleda@hotmail.com>