Bug 4877 [ENH] - Update man page for koha-create
[koha.git] / C4 / Calendar.pm
blobdc9037b9c888de9eac45850bac2eee8b86207e6d
1 package C4::Calendar;
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
8 # version.
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
18 use strict;
19 use warnings;
20 use vars qw($VERSION @EXPORT);
22 use Carp;
23 use Date::Calc qw( Date_to_Days );
25 use C4::Context;
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.01;
30 require Exporter;
31 @EXPORT = qw(
32 &get_week_days_holidays
33 &get_day_month_holidays
34 &get_exception_holidays
35 &get_single_holidays
36 &insert_week_day_holiday
37 &insert_day_month_holiday
38 &insert_single_holiday
39 &insert_exception_holiday
40 &ModWeekdayholiday
41 &ModDaymonthholiday
42 &ModSingleholiday
43 &ModExceptionholiday
44 &delete_holiday
45 &isHoliday
46 &addDate
47 &daysBetween
51 =head1 NAME
53 C4::Calendar::Calendar - Koha module dealing with holidays.
55 =head1 SYNOPSIS
57 use C4::Calendar::Calendar;
59 =head1 DESCRIPTION
61 This package is used to deal with holidays. Through this package, you can set
62 all kind of holidays for the library.
64 =head1 FUNCTIONS
66 =head2 new
68 $calendar = C4::Calendar->new(branchcode => $branchcode);
70 Each library branch has its own Calendar.
71 C<$branchcode> specifies which Calendar you want.
73 =cut
75 sub new {
76 my $classname = shift @_;
77 my %options = @_;
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});
84 return $self;
87 sub _init {
88 my $self = shift @_;
89 my $branch = shift;
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);
131 my %single_holidays;
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;
139 return $self;
142 =head2 get_week_days_holidays
144 $week_days_holidays = $calendar->get_week_days_holidays();
146 Returns a hash reference to week days holidays.
148 =cut
150 sub get_week_days_holidays {
151 my $self = shift @_;
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.
162 =cut
164 sub get_day_month_holidays {
165 my $self = shift @_;
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
176 date.
178 =cut
180 sub get_exception_holidays {
181 my $self = shift @_;
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.
193 =cut
195 sub get_single_holidays {
196 my $self = shift @_;
197 my $single_holidays = $self->{'single_holidays'};
198 return $single_holidays;
201 =head2 insert_week_day_holiday
203 insert_week_day_holiday(weekday => $weekday,
204 title => $title,
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.
215 =cut
217 sub insert_week_day_holiday {
218 my $self = shift @_;
219 my %options = @_;
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};
226 return $self;
229 =head2 insert_day_month_holiday
231 insert_day_month_holiday(day => $day,
232 month => $month,
233 title => $title,
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.
246 =cut
248 sub insert_day_month_holiday {
249 my $self = shift @_;
250 my %options = @_;
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};
257 return $self;
260 =head2 insert_single_holiday
262 insert_single_holiday(day => $day,
263 month => $month,
264 year => $year,
265 title => $title,
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.
280 =cut
282 sub insert_single_holiday {
283 my $self = shift @_;
284 my %options = @_;
286 my $dbh = C4::Context->dbh();
287 my $isexception = 0;
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};
292 return $self;
295 =head2 insert_exception_holiday
297 insert_exception_holiday(day => $day,
298 month => $month,
299 year => $year,
300 title => $title,
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.
315 =cut
317 sub insert_exception_holiday {
318 my $self = shift @_;
319 my %options = @_;
321 my $dbh = C4::Context->dbh();
322 my $isexception = 1;
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};
327 return $self;
330 =head2 ModWeekdayholiday
332 ModWeekdayholiday(weekday =>$weekday,
333 title => $title,
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.
342 =cut
344 sub ModWeekdayholiday {
345 my $self = shift @_;
346 my %options = @_;
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};
353 return $self;
356 =head2 ModDaymonthholiday
358 ModDaymonthholiday(day => $day,
359 month => $month,
360 title => $title,
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.
373 =cut
375 sub ModDaymonthholiday {
376 my $self = shift @_;
377 my %options = @_;
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};
384 return $self;
387 =head2 ModSingleholiday
389 ModSingleholiday(day => $day,
390 month => $month,
391 year => $year,
392 title => $title,
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.
407 =cut
409 sub ModSingleholiday {
410 my $self = shift @_;
411 my %options = @_;
413 my $dbh = C4::Context->dbh();
414 my $isexception = 0;
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};
419 return $self;
422 =head2 ModExceptionholiday
424 ModExceptionholiday(day => $day,
425 month => $month,
426 year => $year,
427 title => $title,
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.
442 =cut
444 sub ModExceptionholiday {
445 my $self = shift @_;
446 my %options = @_;
448 my $dbh = C4::Context->dbh();
449 my $isexception = 1;
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};
454 return $self;
457 =head2 delete_holiday
459 delete_holiday(weekday => $weekday
460 day => $day,
461 month => $month,
462 year => $year);
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.
474 =cut
476 sub delete_holiday {
477 my $self = shift @_;
478 my %options = @_;
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}"});
495 } else {
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}});
511 } else {
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}"});
529 return $self;
532 =head2 isHoliday
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.
542 =cut
544 sub isHoliday {
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
548 $month=$month+0;
549 $year=$year+0;
550 $day=$day+0;
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"})) {
557 return 0;
558 } else {
559 if ((exists($weekDays->{$weekday})) ||
560 (exists($dayMonths->{"$month/$day"})) ||
561 (exists($singles->{"$year/$month/$day"}))) {
562 return 1;
563 } else {
564 return 0;
570 =head2 addDate
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.
578 =cut
580 sub addDate {
581 my ($self, $startdate, $offset) = @_;
582 my ($year,$month,$day) = split("-",$startdate->output('iso'));
583 my $daystep = 1;
584 if ($offset < 0) { # In case $offset is negative
585 # $offset = $offset*(-1);
586 $daystep = -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'));
607 =head2 daysBetween
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.
615 =cut
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)) {
624 return 0;
625 # we don't go backwards ( FIXME - handle this error better )
627 my $count = 0;
628 while (1) {
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)) {
631 $count++;
633 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
635 return($count);
640 __END__
642 =head1 AUTHOR
644 Koha Physics Library UNLP <matias_veleda@hotmail.com>
646 =cut