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);
25 # set the version for version checking
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 all kind of holidays for the library.
48 &get_week_days_holidays
49 &get_day_month_holidays
50 &get_exception_holidays
52 &insert_week_day_holiday
53 &insert_day_month_holiday
54 &insert_single_holiday
55 &insert_exception_holiday
63 $calendar = C4::Calendar::Calendar->new(branchcode => $branchcode);
65 C<$branchcode> Is the branch code wich you want to use calendar.
70 my $classname = shift @_;
74 my $self = bless(\%hash, $classname);
76 foreach my $optionName (keys %options) {
77 $self->{lc($optionName)} = $options{$optionName};
88 my $dbh = C4::Context->dbh();
89 my $week_days_sql = $dbh->prepare("select weekday, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and (NOT(ISNULL(weekday)))");
90 $week_days_sql->execute;
91 my %week_days_holidays;
92 while (my ($weekday, $title, $description) = $week_days_sql->fetchrow) {
93 $week_days_holidays{$weekday}{title} = $title;
94 $week_days_holidays{$weekday}{description} = $description;
96 $week_days_sql->finish;
97 $self->{'week_days_holidays'} = \%week_days_holidays;
99 my $day_month_sql = $dbh->prepare("select day, month, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and ISNULL(weekday)");
100 $day_month_sql->execute;
101 my %day_month_holidays;
102 while (my ($day, $month, $title, $description) = $day_month_sql->fetchrow) {
103 $day_month_holidays{"$month/$day"}{title} = $title;
104 $day_month_holidays{"$month/$day"}{description} = $description;
106 $day_month_sql->finish;
107 $self->{'day_month_holidays'} = \%day_month_holidays;
109 my $exception_holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 1)");
110 $exception_holidays_sql->execute;
111 my %exception_holidays;
112 while (my ($day, $month, $year, $title, $description) = $exception_holidays_sql->fetchrow) {
113 $exception_holidays{"$year/$month/$day"}{title} = $title;
114 $exception_holidays{"$year/$month/$day"}{description} = $description;
116 $exception_holidays_sql->finish;
117 $self->{'exception_holidays'} = \%exception_holidays;
119 my $holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 0)");
120 $holidays_sql->execute;
122 while (my ($day, $month, $year, $title, $description) = $holidays_sql->fetchrow) {
123 $single_holidays{"$year/$month/$day"}{title} = $title;
124 $single_holidays{"$year/$month/$day"}{description} = $description;
126 $holidays_sql->finish;
127 $self->{'single_holidays'} = \%single_holidays;
130 =item change_branchcode
132 $calendar->change_branchcode(branchcode => $branchcode)
134 Change the calendar branch code. This means to change the holidays structure.
136 C<$branchcode> Is the branch code wich you want to use calendar.
140 sub change_branchcode {
141 my ($self, $branchcode) = @_;
144 foreach my $optionName (keys %options) {
145 $self->{lc($optionName)} = $options{$optionName};
152 =item get_week_days_holidays
154 $week_days_holidays = $calendar->get_week_days_holidays();
156 Returns a hash reference to week days holidays.
160 sub get_week_days_holidays {
162 my $week_days_holidays = $self->{'week_days_holidays'};
163 return $week_days_holidays;
166 =item get_day_month_holidays
168 $day_month_holidays = $calendar->get_day_month_holidays();
170 Returns a hash reference to day month holidays.
174 sub get_day_month_holidays {
176 my $day_month_holidays = $self->{'day_month_holidays'};
177 return $day_month_holidays;
180 =item get_exception_holidays
182 $exception_holidays = $calendar->exception_holidays();
184 Returns a hash reference to exception holidays. This kind of days are those
185 which stands for a holiday, but you wanted to make an exception for this particular
190 sub get_exception_holidays {
192 my $exception_holidays = $self->{'exception_holidays'};
193 return $exception_holidays;
196 =item get_single_holidays
198 $single_holidays = $calendar->get_single_holidays();
200 Returns a hash reference to single holidays. This kind of holidays are those which
201 happend just one time.
205 sub get_single_holidays {
207 my $single_holidays = $self->{'single_holidays'};
208 return $single_holidays;
211 =item insert_week_day_holiday
213 insert_week_day_holiday(weekday => $weekday,
215 description => $description);
217 Inserts a new week day for $self->{branchcode}.
219 C<$day> Is the week day to make holiday.
221 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
223 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
227 sub insert_week_day_holiday {
231 my $dbh = C4::Context->dbh();
232 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
233 $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description});
234 $insertHoliday->finish;
236 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
237 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
241 =item insert_day_month_holiday
243 insert_day_month_holiday(day => $day,
246 description => $description);
248 Inserts a new day month 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<$title> Is the title to store for the holiday formed by $year/$month/$day.
256 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
260 sub insert_day_month_holiday {
264 my $dbh = C4::Context->dbh();
265 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
266 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
267 $insertHoliday->finish;
269 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
270 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
274 =item insert_single_holiday
276 insert_single_holiday(day => $day,
280 description => $description);
282 Inserts a new single holiday for $self->{branchcode}.
284 C<$day> Is the day month to make the date to insert.
286 C<$month> Is month to make the date to insert.
288 C<$year> Is year to make the date to insert.
290 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
292 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
296 sub insert_single_holiday {
300 my $dbh = C4::Context->dbh();
302 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
303 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
304 $insertHoliday->finish;
306 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
307 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
311 =item insert_exception_holiday
313 insert_exception_holiday(day => $day,
317 description => $description);
319 Inserts a new exception holiday for $self->{branchcode}.
321 C<$day> Is the day month to make the date to insert.
323 C<$month> Is month to make the date to insert.
325 C<$year> Is year to make the date to insert.
327 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
329 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
333 sub insert_exception_holiday {
337 my $dbh = C4::Context->dbh();
339 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
340 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
341 $insertException->finish;
343 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
344 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
350 delete_holiday(weekday => $weekday
355 Delete a holiday for $self->{branchcode}.
357 C<$weekday> Is the week day to delete.
359 C<$day> Is the day month to make the date to delete.
361 C<$month> Is month to make the date to delete.
363 C<$year> Is year to make the date to delete.
371 # Verify what kind of holiday that day is. For example, if it is
372 # a repeatable holiday, this should check if there are some exception
373 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
374 # ok just deleting it.
376 my $dbh = C4::Context->dbh();
377 my $isSingleHoliday = $dbh->prepare("select id from special_holidays where (branchcode = '$self->{branchcode}') and (day = $options{day}) and (month = $options{month}) and (year = $options{year})");
378 $isSingleHoliday->execute;
379 if ($isSingleHoliday->rows) {
380 my $id = $isSingleHoliday->fetchrow;
381 $isSingleHoliday->finish; # Close the last query
383 my $deleteHoliday = $dbh->prepare("delete from special_holidays where (id = $id)");
384 $deleteHoliday->execute;
385 $deleteHoliday->finish; # Close the last query
386 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
388 $isSingleHoliday->finish; # Close the last query
390 my $isWeekdayHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (weekday = $options{weekday})");
391 $isWeekdayHoliday->execute;
392 if ($isWeekdayHoliday->rows) {
393 my $id = $isWeekdayHoliday->fetchrow;
394 $isWeekdayHoliday->finish; # Close the last query
396 my $updateExceptions = $dbh->prepare("update special_holidays set isexception = 0 where (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = $options{weekday}) and (branchcode = '$self->{branchcode}')");
397 $updateExceptions->execute;
398 $updateExceptions->finish; # Close the last query
400 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
401 $deleteHoliday->execute;
402 $deleteHoliday->finish;
403 delete($self->{'week_days_holidays'}->{$options{weekday}});
405 $isWeekdayHoliday->finish; # Close the last query
407 my $isDayMonthHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (day = '$options{day}') and (month = '$options{month}')");
408 $isDayMonthHoliday->execute;
409 if ($isDayMonthHoliday->rows) {
410 my $id = $isDayMonthHoliday->fetchrow;
411 $isDayMonthHoliday->finish;
412 my $updateExceptions = $dbh->prepare("update special_holidays set isexception = 0 where (special_holidays.branchcode = '$self->{branchcode}') and (special_holidays.day = '$options{day}') and (special_holidays.month = '$options{month}')");
413 $updateExceptions->execute;
414 $updateExceptions->finish; # Close the last query
416 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = '$id')");
417 $deleteHoliday->execute;
418 $deleteHoliday->finish; # Close the last query
419 $isDayMonthHoliday->finish; # Close the last query
420 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
429 $isHoliday = isHoliday($day, $month $year);
432 C<$day> Is the day to check whether if is a holiday or not.
434 C<$month> Is the month to check whether if is a holiday or not.
436 C<$year> Is the year to check whether if is a holiday or not.
441 my ($self, $day, $month, $year) = @_;
442 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
443 my $weekDays = $self->get_week_days_holidays();
444 my $dayMonths = $self->get_day_month_holidays();
445 my $exceptions = $self->get_exception_holidays();
446 my $singles = $self->get_single_holidays();
447 if (defined($exceptions->{"$year/$month/$day"})) {
450 if ((exists($weekDays->{$weekday})) ||
451 (exists($dayMonths->{"$month/$day"})) ||
452 (exists($singles->{"$year/$month/$day"}))) {
463 my ($day, $month, $year) = $calendar->addDate($date, $offset)
465 C<$date> is a C4::Dates object representing the starting date of the interval.
467 C<$offset> Is the number of days that this function has to count from $date.
472 my ($self, $startdate, $offset) = @_;
473 my ($year,$month,$day) = split("-",$startdate->output('iso'));
474 if ($offset < 0) { # In case $offset is negative
475 $offset = $offset*(-1);
477 my $daysMode = C4::Context->preference('useDaysMode');
478 if ($daysMode eq 'Datedue') {
479 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
480 while ($self->isHoliday($day, $month, $year)) {
481 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, 1);
483 } elsif($daysMode eq 'Calendar') {
484 while ($offset > 0) {
485 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, 1);
486 if (!($self->isHoliday($day, $month, $year))) {
487 $offset = $offset - 1;
490 } else { ## ($daysMode eq 'Days')
491 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
493 return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
498 my $daysBetween = $calendar->daysBetween($dayFrom, $monthFrom, $yearFrom,
499 $dayTo, $monthTo, $yearTo)
501 C<$dayFrom> Is the starting day of the interval.
503 C<$monthFrom> Is the starting month of the interval.
505 C<$yearFrom> Is the starting year of the interval.
507 C<$dayTo> Is the ending day of the interval.
509 C<$monthTo> Is the ending month of the interval.
511 C<$yearTo> Is the ending year of the interval.
516 my ($self, $dayFrom, $monthFrom, $yearFrom, $dayTo, $monthTo, $yearTo) = @_;
518 my $daysMode = C4::Context->preference('useDaysMode');
519 #FIXME : useDaysMode == 'Datedue' is not implemented here, but neither is this fcn used anywhere.
522 if ($daysMode eq 'Days') {
524 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
525 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
533 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
534 if (!($self->isHoliday($dayFrom, $monthFrom, $yearFrom))) {
537 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
554 Koha Physics Library UNLP <matias_veleda@hotmail.com>