1 package C4::Calendar;
\r
3 # This file is part of Koha.
\r
5 # Koha is free software; you can redistribute it and/or modify it under the
\r
6 # terms of the GNU General Public License as published by the Free Software
\r
7 # Foundation; either version 2 of the License, or (at your option) any later
\r
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
\r
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
\r
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
\r
14 # You should have received a copy of the GNU General Public License along with
\r
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
\r
16 # Suite 330, Boston, MA 02111-1307 USA
\r
20 use vars qw($VERSION @EXPORT);
\r
26 # set the version for version checking
\r
31 C4::Calendar::Calendar - Koha module dealing with holidays.
\r
35 use C4::Calendar::Calendar;
\r
39 This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library.
\r
49 &get_week_days_holidays
\r
50 &get_day_month_holidays
\r
51 &get_exception_holidays
\r
52 &get_single_holidays
\r
53 &insert_week_day_holiday
\r
54 &insert_day_month_holiday
\r
55 &insert_single_holiday
\r
56 &insert_exception_holiday
\r
64 $calendar = C4::Calendar::Calendar->new(branchcode => $branchcode);
\r
66 C<$branchcode> Is the branch code wich you want to use calendar.
\r
71 my $classname = shift @_;
\r
75 my $self = bless(\%hash, $classname);
\r
77 foreach my $optionName (keys %options) {
\r
78 $self->{lc($optionName)} = $options{$optionName};
\r
87 my $self = shift @_;
\r
89 my $dbh = C4::Context->dbh();
\r
90 my $week_days_sql = $dbh->prepare("select weekday, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and (NOT(ISNULL(weekday)))");
\r
91 $week_days_sql->execute;
\r
92 my %week_days_holidays;
\r
93 while (my ($weekday, $title, $description) = $week_days_sql->fetchrow) {
\r
94 $week_days_holidays{$weekday}{title} = $title;
\r
95 $week_days_holidays{$weekday}{description} = $description;
\r
97 $week_days_sql->finish;
\r
98 $self->{'week_days_holidays'} = \%week_days_holidays;
\r
100 my $day_month_sql = $dbh->prepare("select day, month, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and ISNULL(weekday)");
\r
101 $day_month_sql->execute;
\r
102 my %day_month_holidays;
\r
103 while (my ($day, $month, $title, $description) = $day_month_sql->fetchrow) {
\r
104 $day_month_holidays{"$month/$day"}{title} = $title;
\r
105 $day_month_holidays{"$month/$day"}{description} = $description;
\r
107 $day_month_sql->finish;
\r
108 $self->{'day_month_holidays'} = \%day_month_holidays;
\r
110 my $exception_holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 1)");
\r
111 $exception_holidays_sql->execute;
\r
112 my %exception_holidays;
\r
113 while (my ($day, $month, $year, $title, $description) = $exception_holidays_sql->fetchrow) {
\r
114 $exception_holidays{"$year/$month/$day"}{title} = $title;
\r
115 $exception_holidays{"$year/$month/$day"}{description} = $description;
\r
117 $exception_holidays_sql->finish;
\r
118 $self->{'exception_holidays'} = \%exception_holidays;
\r
120 my $holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 0)");
\r
121 $holidays_sql->execute;
\r
122 my %single_holidays;
\r
123 while (my ($day, $month, $year, $title, $description) = $holidays_sql->fetchrow) {
\r
124 $single_holidays{"$year/$month/$day"}{title} = $title;
\r
125 $single_holidays{"$year/$month/$day"}{description} = $description;
\r
127 $holidays_sql->finish;
\r
128 $self->{'single_holidays'} = \%single_holidays;
\r
131 =item change_branchcode
\r
133 $calendar->change_branchcode(branchcode => $branchcode)
\r
135 Change the calendar branch code. This means to change the holidays structure.
\r
137 C<$branchcode> Is the branch code wich you want to use calendar.
\r
141 sub change_branchcode {
\r
142 my ($self, $branchcode) = @_;
\r
145 foreach my $optionName (keys %options) {
\r
146 $self->{lc($optionName)} = $options{$optionName};
\r
153 =item get_week_days_holidays
\r
155 $week_days_holidays = $calendar->get_week_days_holidays();
\r
157 Returns a hash reference to week days holidays.
\r
161 sub get_week_days_holidays {
\r
162 my $self = shift @_;
\r
163 my $week_days_holidays = $self->{'week_days_holidays'};
\r
164 return $week_days_holidays;
\r
167 =item get_day_month_holidays
\r
169 $day_month_holidays = $calendar->get_day_month_holidays();
\r
171 Returns a hash reference to day month holidays.
\r
175 sub get_day_month_holidays {
\r
176 my $self = shift @_;
\r
177 my $day_month_holidays = $self->{'day_month_holidays'};
\r
178 return $day_month_holidays;
\r
181 =item get_exception_holidays
\r
183 $exception_holidays = $calendar->exception_holidays();
\r
185 Returns a hash reference to exception holidays. This kind of days are those
\r
186 which stands for a holiday, but you wanted to make an exception for this particular
\r
191 sub get_exception_holidays {
\r
192 my $self = shift @_;
\r
193 my $exception_holidays = $self->{'exception_holidays'};
\r
194 return $exception_holidays;
\r
197 =item get_single_holidays
\r
199 $single_holidays = $calendar->get_single_holidays();
\r
201 Returns a hash reference to single holidays. This kind of holidays are those which
\r
202 happend just one time.
\r
206 sub get_single_holidays {
\r
207 my $self = shift @_;
\r
208 my $single_holidays = $self->{'single_holidays'};
\r
209 return $single_holidays;
\r
212 =item insert_week_day_holiday
\r
214 insert_week_day_holiday(weekday => $weekday,
\r
216 description => $description);
\r
218 Inserts a new week day for $self->{branchcode}.
\r
220 C<$day> Is the week day to make holiday.
\r
222 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
\r
224 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
\r
228 sub insert_week_day_holiday {
\r
229 my $self = shift @_;
\r
232 my $dbh = C4::Context->dbh();
\r
233 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', '$self->{branchcode}', $options{weekday}, NULL, NULL, '$options{title}', '$options{description}')");
\r
234 $insertHoliday->execute;
\r
235 $insertHoliday->finish;
\r
237 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
\r
238 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
\r
242 =item insert_day_month_holiday
\r
244 insert_day_month_holiday(day => $day,
\r
247 description => $description);
\r
249 Inserts a new day month holiday for $self->{branchcode}.
\r
251 C<$day> Is the day month to make the date to insert.
\r
253 C<$month> Is month to make the date to insert.
\r
255 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
\r
257 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
\r
261 sub insert_day_month_holiday {
\r
262 my $self = shift @_;
\r
265 my $dbh = C4::Context->dbh();
\r
266 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', '$self->{branchcode}', NULL, $options{day}, $options{month}, '$options{title}', '$options{description}')");
\r
267 $insertHoliday->execute;
\r
268 $insertHoliday->finish;
\r
270 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
\r
271 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
\r
275 =item insert_single_holiday
\r
277 insert_single_holiday(day => $day,
\r
281 description => $description);
\r
283 Inserts a new single holiday for $self->{branchcode}.
\r
285 C<$day> Is the day month to make the date to insert.
\r
287 C<$month> Is month to make the date to insert.
\r
289 C<$year> Is year to make the date to insert.
\r
291 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
\r
293 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
\r
297 sub insert_single_holiday {
\r
298 my $self = shift @_;
\r
301 my $dbh = C4::Context->dbh();
\r
302 my $isexception = 0;
\r
303 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', '$self->{branchcode}', $options{day}, $options{month}, $options{year}, $isexception, '$options{title}', '$options{description}')");
\r
304 $insertHoliday->execute;
\r
305 $insertHoliday->finish;
\r
307 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
\r
308 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
\r
312 =item insert_exception_holiday
\r
314 insert_exception_holiday(day => $day,
\r
318 description => $description);
\r
320 Inserts a new exception holiday for $self->{branchcode}.
\r
322 C<$day> Is the day month to make the date to insert.
\r
324 C<$month> Is month to make the date to insert.
\r
326 C<$year> Is year to make the date to insert.
\r
328 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
\r
330 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
\r
334 sub insert_exception_holiday {
\r
335 my $self = shift @_;
\r
338 my $dbh = C4::Context->dbh();
\r
339 my $isexception = 1;
\r
340 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', '$self->{branchcode}', $options{day}, $options{month}, $options{year}, $isexception, '$options{title}', '$options{description}')");
\r
341 $insertException->execute;
\r
342 $insertException->finish;
\r
344 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
\r
345 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
\r
349 =item delete_holiday
\r
351 delete_holiday(weekday => $weekday
\r
356 Delete a holiday for $self->{branchcode}.
\r
358 C<$weekday> Is the week day to delete.
\r
360 C<$day> Is the day month to make the date to delete.
\r
362 C<$month> Is month to make the date to delete.
\r
364 C<$year> Is year to make the date to delete.
\r
368 sub delete_holiday {
\r
369 my $self = shift @_;
\r
372 # Verify what kind of holiday that day is. For example, if it is
\r
373 # a repeatable holiday, this should check if there are some exception
\r
374 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
\r
375 # ok just deleting it.
\r
377 my $dbh = C4::Context->dbh();
\r
378 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})");
\r
379 $isSingleHoliday->execute;
\r
380 if ($isSingleHoliday->rows) {
\r
381 my $id = $isSingleHoliday->fetchrow;
\r
382 $isSingleHoliday->finish; # Close the last query
\r
384 my $deleteHoliday = $dbh->prepare("delete from special_holidays where (id = $id)");
\r
385 $deleteHoliday->execute;
\r
386 $deleteHoliday->finish; # Close the last query
\r
387 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
\r
389 $isSingleHoliday->finish; # Close the last query
\r
391 my $isWeekdayHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (weekday = $options{weekday})");
\r
392 $isWeekdayHoliday->execute;
\r
393 if ($isWeekdayHoliday->rows) {
\r
394 my $id = $isWeekdayHoliday->fetchrow;
\r
395 $isWeekdayHoliday->finish; # Close the last query
\r
397 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}')");
\r
398 $updateExceptions->execute;
\r
399 $updateExceptions->finish; # Close the last query
\r
401 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
\r
402 $deleteHoliday->execute;
\r
403 $deleteHoliday->finish;
\r
404 delete($self->{'week_days_holidays'}->{$options{weekday}});
\r
406 $isWeekdayHoliday->finish; # Close the last query
\r
408 my $isDayMonthHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') (day = $options{day}) and (month = $options{month})");
\r
409 $isDayMonthHoliday->execute;
\r
410 if ($isDayMonthHoliday->rows) {
\r
411 my $id = $isDayMonthHoliday->fetchrow;
\r
412 $isDayMonthHoliday->finish;
\r
413 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})");
\r
414 $updateExceptions->execute;
\r
415 $updateExceptions->finish; # Close the last query
\r
417 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
\r
418 $deleteHoliday->execute;
\r
419 $deleteHoliday->finish; # Close the last query
\r
420 $isDayMonthHoliday->finish; # Close the last query
\r
421 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
\r
430 $isHoliday = isHoliday($day, $month $year);
\r
433 C<$day> Is the day to check wether if is a holiday or not.
\r
435 C<$month> Is the month to check wether if is a holiday or not.
\r
437 C<$year> Is the year to check wether if is a holiday or not.
\r
442 my ($self, $day, $month, $year) = @_;
\r
444 my $weekday = Date_DayOfWeek($month, $day, $year) % 7;
\r
446 my $weekDays = $self->get_week_days_holidays();
\r
447 my $dayMonths = $self->get_day_month_holidays();
\r
448 my $exceptions = $self->get_exception_holidays();
\r
449 my $singles = $self->get_single_holidays();
\r
451 if (defined($exceptions->{"$year/$month/$day"})) {
\r
454 if ((exists($weekDays->{$weekday})) ||
\r
455 (exists($dayMonths->{"$month/$day"})) ||
\r
456 (exists($singles->{"$year/$month/$day"}))) {
\r
467 my ($day, $month, $year) = $calendar->addDate($day, $month, $year, $offset)
\r
469 C<$day> Is the starting day of the interval.
\r
471 C<$month> Is the starting month of the interval.
\r
473 C<$year> Is the starting year of the interval.
\r
475 C<$offset> Is the number of days that this function has to count from $date.
\r
480 my ($self, $day, $month, $year, $offset) = @_;
\r
482 if ($offset < 0) { # In case $offset is negative
\r
483 $offset = $offset*(-1);
\r
486 my $daysMode = C4::Context->preference('useDaysMode');
\r
487 if ($daysMode eq 'normal') {
\r
488 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, ($offset - 1));
\r
490 while ($offset > 0) {
\r
491 if (!($self->isHoliday($day, $month, $year))) {
\r
492 $offset = $offset - 1;
\r
495 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, 1);
\r
500 return($day, $month, $year);
\r
505 my $daysBetween = $calendar->daysBetween($dayFrom, $monthFrom, $yearFrom,
\r
506 $dayTo, $monthTo, $yearTo)
\r
508 C<$dayFrom> Is the starting day of the interval.
\r
510 C<$monthFrom> Is the starting month of the interval.
\r
512 C<$yearFrom> Is the starting year of the interval.
\r
514 C<$dayTo> Is the ending day of the interval.
\r
516 C<$monthTo> Is the ending month of the interval.
\r
518 C<$yearTo> Is the ending year of the interval.
\r
523 my ($self, $dayFrom, $monthFrom, $yearFrom, $dayTo, $monthTo, $yearTo) = @_;
\r
525 my $daysMode = C4::Context->preference('useDaysMode');
\r
528 if ($daysMode eq 'normal') {
\r
529 while ($continue) {
\r
530 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
\r
531 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
\r
538 while ($continue) {
\r
539 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
\r
540 if (!($self->isHoliday($dayFrom, $monthFrom, $yearFrom))) {
\r
543 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
\r
560 Koha Physics Library UNLP <matias_veleda@hotmail.com>
\r