Bug fix : use delete_field/insert_grouped_field rather than replace_with
[koha.git] / C4 / Calendar.pm
bloba279e2fd8e65f22c5de34175dff5b2ace783f92d
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 require Exporter;
20 use vars qw($VERSION @EXPORT);
22 use Date::Calc qw( Date_to_Days );
24 # set the version for version checking
25 $VERSION = 3.00;
27 =head1 NAME
29 C4::Calendar::Calendar - Koha module dealing with holidays.
31 =head1 SYNOPSIS
33 use C4::Calendar::Calendar;
35 =head1 DESCRIPTION
37 This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library.
39 =head1 FUNCTIONS
41 =over 2
43 =cut
45 @EXPORT = qw(&new
46 &change_branchcode
47 &get_week_days_holidays
48 &get_day_month_holidays
49 &get_exception_holidays
50 &get_single_holidays
51 &insert_week_day_holiday
52 &insert_day_month_holiday
53 &insert_single_holiday
54 &insert_exception_holiday
55 &delete_holiday
56 &isHoliday
57 &addDate
58 &daysBetween);
60 =item new
62 $calendar = C4::Calendar->new(branchcode => $branchcode);
64 C<$branchcode> Is the branch code wich you want to use calendar.
66 =cut
68 sub new {
69 my $classname = shift @_;
70 my %options = @_;
72 my %hash;
73 my $self = bless(\%hash, $classname);
75 foreach my $optionName (keys %options) {
76 $self->{lc($optionName)} = $options{$optionName};
79 $self->_init;
81 return $self;
84 sub _init {
85 my $self = shift @_;
87 my $dbh = C4::Context->dbh();
88 my $week_days_sql = $dbh->prepare( 'SELECT weekday, title, description
89 FROM repeatable_holidays
90 WHERE ( branchcode = ? )
91 AND (NOT(ISNULL(weekday)))' );
92 $week_days_sql->execute( $self->{'branchcode'} );
93 my %week_days_holidays;
94 while (my ($weekday, $title, $description) = $week_days_sql->fetchrow) {
95 $week_days_holidays{$weekday}{title} = $title;
96 $week_days_holidays{$weekday}{description} = $description;
98 $week_days_sql->finish;
99 $self->{'week_days_holidays'} = \%week_days_holidays;
101 my $day_month_sql = $dbh->prepare( 'SELECT day, month, title, description
102 FROM repeatable_holidays
103 WHERE ( branchcode = ? )
104 AND ISNULL(weekday)' );
105 $day_month_sql->execute( $self->{'branchcode'} );
106 my %day_month_holidays;
107 while (my ($day, $month, $title, $description) = $day_month_sql->fetchrow) {
108 $day_month_holidays{"$month/$day"}{title} = $title;
109 $day_month_holidays{"$month/$day"}{description} = $description;
111 $day_month_sql->finish;
112 $self->{'day_month_holidays'} = \%day_month_holidays;
114 my $exception_holidays_sql = $dbh->prepare( 'SELECT day, month, year, title, description
115 FROM special_holidays
116 WHERE ( branchcode = ? )
117 AnD (isexception = 1)' );
118 $exception_holidays_sql->execute( $self->{'branchcode'} );
119 my %exception_holidays;
120 while (my ($day, $month, $year, $title, $description) = $exception_holidays_sql->fetchrow) {
121 $exception_holidays{"$year/$month/$day"}{title} = $title;
122 $exception_holidays{"$year/$month/$day"}{description} = $description;
124 $exception_holidays_sql->finish;
125 $self->{'exception_holidays'} = \%exception_holidays;
127 my $holidays_sql = $dbh->prepare( 'SELECT day, month, year, title, description
128 FROM special_holidays
129 WHERE ( branchcode = ? )
130 AND (isexception = 0)' );
131 $holidays_sql->execute( $self->{'branchcode'} );
132 my %single_holidays;
133 while (my ($day, $month, $year, $title, $description) = $holidays_sql->fetchrow) {
134 $single_holidays{"$year/$month/$day"}{title} = $title;
135 $single_holidays{"$year/$month/$day"}{description} = $description;
137 $holidays_sql->finish;
138 $self->{'single_holidays'} = \%single_holidays;
141 =item change_branchcode
143 $calendar->change_branchcode(branchcode => $branchcode)
145 Change the calendar branch code. This means to change the holidays structure.
147 C<$branchcode> Is the branch code wich you want to use calendar.
149 =cut
151 sub change_branchcode {
152 my ($self, $branchcode) = @_;
153 my %options = @_;
155 foreach my $optionName (keys %options) {
156 $self->{lc($optionName)} = $options{$optionName};
158 $self->_init;
160 return $self;
163 =item get_week_days_holidays
165 $week_days_holidays = $calendar->get_week_days_holidays();
167 Returns a hash reference to week days holidays.
169 =cut
171 sub get_week_days_holidays {
172 my $self = shift @_;
173 my $week_days_holidays = $self->{'week_days_holidays'};
174 return $week_days_holidays;
177 =item get_day_month_holidays
179 $day_month_holidays = $calendar->get_day_month_holidays();
181 Returns a hash reference to day month holidays.
183 =cut
185 sub get_day_month_holidays {
186 my $self = shift @_;
187 my $day_month_holidays = $self->{'day_month_holidays'};
188 return $day_month_holidays;
191 =item get_exception_holidays
193 $exception_holidays = $calendar->exception_holidays();
195 Returns a hash reference to exception holidays. This kind of days are those
196 which stands for a holiday, but you wanted to make an exception for this particular
197 date.
199 =cut
201 sub get_exception_holidays {
202 my $self = shift @_;
203 my $exception_holidays = $self->{'exception_holidays'};
204 return $exception_holidays;
207 =item get_single_holidays
209 $single_holidays = $calendar->get_single_holidays();
211 Returns a hash reference to single holidays. This kind of holidays are those which
212 happend just one time.
214 =cut
216 sub get_single_holidays {
217 my $self = shift @_;
218 my $single_holidays = $self->{'single_holidays'};
219 return $single_holidays;
222 =item insert_week_day_holiday
224 insert_week_day_holiday(weekday => $weekday,
225 title => $title,
226 description => $description);
228 Inserts a new week day for $self->{branchcode}.
230 C<$day> Is the week day to make holiday.
232 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
234 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
236 =cut
238 sub insert_week_day_holiday {
239 my $self = shift @_;
240 my %options = @_;
242 my $dbh = C4::Context->dbh();
243 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
244 $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description});
245 $insertHoliday->finish;
247 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
248 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
249 return $self;
252 =item insert_day_month_holiday
254 insert_day_month_holiday(day => $day,
255 month => $month,
256 title => $title,
257 description => $description);
259 Inserts a new day month holiday for $self->{branchcode}.
261 C<$day> Is the day month to make the date to insert.
263 C<$month> Is month to make the date to insert.
265 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
267 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
269 =cut
271 sub insert_day_month_holiday {
272 my $self = shift @_;
273 my %options = @_;
275 my $dbh = C4::Context->dbh();
276 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
277 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
278 $insertHoliday->finish;
280 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
281 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
282 return $self;
285 =item insert_single_holiday
287 insert_single_holiday(day => $day,
288 month => $month,
289 year => $year,
290 title => $title,
291 description => $description);
293 Inserts a new single holiday for $self->{branchcode}.
295 C<$day> Is the day month to make the date to insert.
297 C<$month> Is month to make the date to insert.
299 C<$year> Is year to make the date to insert.
301 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
303 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
305 =cut
307 sub insert_single_holiday {
308 my $self = shift @_;
309 my %options = @_;
311 my $dbh = C4::Context->dbh();
312 my $isexception = 0;
313 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
314 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
315 $insertHoliday->finish;
317 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
318 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
319 return $self;
322 =item insert_exception_holiday
324 insert_exception_holiday(day => $day,
325 month => $month,
326 year => $year,
327 title => $title,
328 description => $description);
330 Inserts a new exception holiday for $self->{branchcode}.
332 C<$day> Is the day month to make the date to insert.
334 C<$month> Is month to make the date to insert.
336 C<$year> Is year to make the date to insert.
338 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
340 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
342 =cut
344 sub insert_exception_holiday {
345 my $self = shift @_;
346 my %options = @_;
348 my $dbh = C4::Context->dbh();
349 my $isexception = 1;
350 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
351 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
352 $insertException->finish;
354 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
355 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
356 return $self;
359 =item delete_holiday
361 delete_holiday(weekday => $weekday
362 day => $day,
363 month => $month,
364 year => $year);
366 Delete a holiday for $self->{branchcode}.
368 C<$weekday> Is the week day to delete.
370 C<$day> Is the day month to make the date to delete.
372 C<$month> Is month to make the date to delete.
374 C<$year> Is year to make the date to delete.
376 =cut
378 sub delete_holiday {
379 my $self = shift @_;
380 my %options = @_;
382 # Verify what kind of holiday that day is. For example, if it is
383 # a repeatable holiday, this should check if there are some exception
384 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
385 # ok just deleting it.
387 my $dbh = C4::Context->dbh();
388 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})");
389 $isSingleHoliday->execute;
390 if ($isSingleHoliday->rows) {
391 my $id = $isSingleHoliday->fetchrow;
392 $isSingleHoliday->finish; # Close the last query
394 my $deleteHoliday = $dbh->prepare("delete from special_holidays where (id = $id)");
395 $deleteHoliday->execute;
396 $deleteHoliday->finish; # Close the last query
397 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
398 } else {
399 $isSingleHoliday->finish; # Close the last query
401 my $isWeekdayHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (weekday = $options{weekday})");
402 $isWeekdayHoliday->execute;
403 if ($isWeekdayHoliday->rows) {
404 my $id = $isWeekdayHoliday->fetchrow;
405 $isWeekdayHoliday->finish; # Close the last query
407 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}')");
408 $updateExceptions->execute;
409 $updateExceptions->finish; # Close the last query
411 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
412 $deleteHoliday->execute;
413 $deleteHoliday->finish;
414 delete($self->{'week_days_holidays'}->{$options{weekday}});
415 } else {
416 $isWeekdayHoliday->finish; # Close the last query
418 my $isDayMonthHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (day = '$options{day}') and (month = '$options{month}')");
419 $isDayMonthHoliday->execute;
420 if ($isDayMonthHoliday->rows) {
421 my $id = $isDayMonthHoliday->fetchrow;
422 $isDayMonthHoliday->finish;
423 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}')");
424 $updateExceptions->execute;
425 $updateExceptions->finish; # Close the last query
427 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = '$id')");
428 $deleteHoliday->execute;
429 $deleteHoliday->finish; # Close the last query
430 $isDayMonthHoliday->finish; # Close the last query
431 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
435 return $self;
438 =item isHoliday
440 $isHoliday = isHoliday($day, $month $year);
443 C<$day> Is the day to check whether if is a holiday or not.
445 C<$month> Is the month to check whether if is a holiday or not.
447 C<$year> Is the year to check whether if is a holiday or not.
449 =cut
451 sub isHoliday {
452 my ($self, $day, $month, $year) = @_;
453 # FIXME - date strings are stored in non-padded metric format. should change to iso.
454 # FIXME - should change arguments to accept C4::Dates object
455 $month=$month+0;
456 $year=$year+0;
457 $day=$day+0;
458 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
459 my $weekDays = $self->get_week_days_holidays();
460 my $dayMonths = $self->get_day_month_holidays();
461 my $exceptions = $self->get_exception_holidays();
462 my $singles = $self->get_single_holidays();
463 if (defined($exceptions->{"$year/$month/$day"})) {
464 return 0;
465 } else {
466 if ((exists($weekDays->{$weekday})) ||
467 (exists($dayMonths->{"$month/$day"})) ||
468 (exists($singles->{"$year/$month/$day"}))) {
469 return 1;
470 } else {
471 return 0;
477 =item addDate
479 my ($day, $month, $year) = $calendar->addDate($date, $offset)
481 C<$date> is a C4::Dates object representing the starting date of the interval.
483 C<$offset> Is the number of days that this function has to count from $date.
485 =cut
487 sub addDate {
488 my ($self, $startdate, $offset) = @_;
489 my ($year,$month,$day) = split("-",$startdate->output('iso'));
490 my $daystep = 1;
491 if ($offset < 0) { # In case $offset is negative
492 # $offset = $offset*(-1);
493 $daystep = -1;
495 my $daysMode = C4::Context->preference('useDaysMode');
496 if ($daysMode eq 'Datedue') {
497 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
498 while ($self->isHoliday($day, $month, $year)) {
499 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
501 } elsif($daysMode eq 'Calendar') {
502 while ($offset != 0) {
503 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
504 if (!($self->isHoliday($day, $month, $year))) {
505 $offset = $offset - $daystep;
508 } else { ## ($daysMode eq 'Days')
509 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
511 return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
514 =item daysBetween
516 my $daysBetween = $calendar->daysBetween($startdate, $enddate )
518 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
520 Returns the number of non-holiday days in the interval.
521 useDaysMode syspref has no effect here.
522 =cut
524 sub daysBetween {
525 my ( $self, $startdate, $enddate ) = @_ ;
526 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
527 my ($yearTo,$monthTo,$dayTo) = split("-",$enddate->output('iso'));
528 if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
529 return 0;
530 # we don't go backwards ( FIXME - handle this error better )
532 my $count = 0;
533 my $continue = 1;
534 while ($continue) {
535 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
536 if (!($self->isHoliday($dayFrom, $monthFrom, $yearFrom))) {
537 $count++;
539 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
540 } else {
541 $continue = 0;
544 return($count);
549 __END__
551 =back
553 =head1 AUTHOR
555 Koha Physics Library UNLP <matias_veleda@hotmail.com>
557 =cut