Bug 13590: (RM followup) DBIx update
[koha.git] / C4 / Calendar.pm
blobd8705aff007794e45ab410ab6360eb8c9b9dc6ec
1 package C4::Calendar;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use strict;
19 use warnings;
20 use vars qw($VERSION @EXPORT);
22 use Carp;
23 use Date::Calc qw( Date_to_Days Today);
25 use C4::Context;
26 use Koha::Cache;
28 use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
30 =head1 NAME
32 C4::Calendar::Calendar - Koha module dealing with holidays.
34 =head1 SYNOPSIS
36 use C4::Calendar::Calendar;
38 =head1 DESCRIPTION
40 This package is used to deal with holidays. Through this package, you can set
41 all kind of holidays for the library.
43 =head1 FUNCTIONS
45 =head2 new
47 $calendar = C4::Calendar->new(branchcode => $branchcode);
49 Each library branch has its own Calendar.
50 C<$branchcode> specifies which Calendar you want.
52 =cut
54 sub new {
55 my $classname = shift @_;
56 my %options = @_;
57 my $self = bless({}, $classname);
58 foreach my $optionName (keys %options) {
59 $self->{lc($optionName)} = $options{$optionName};
61 defined($self->{branchcode}) or croak "No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)";
62 $self->_init($self->{branchcode});
63 return $self;
66 sub _init {
67 my $self = shift @_;
68 my $branch = shift;
69 defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow ""
70 my $dbh = C4::Context->dbh();
71 my $repeatable = $dbh->prepare( 'SELECT *
72 FROM repeatable_holidays
73 WHERE ( branchcode = ? )
74 AND (ISNULL(weekday) = ?)' );
75 $repeatable->execute($branch,0);
76 my %week_days_holidays;
77 while (my $row = $repeatable->fetchrow_hashref) {
78 my $key = $row->{weekday};
79 $week_days_holidays{$key}{title} = $row->{title};
80 $week_days_holidays{$key}{description} = $row->{description};
82 $self->{'week_days_holidays'} = \%week_days_holidays;
84 $repeatable->execute($branch,1);
85 my %day_month_holidays;
86 while (my $row = $repeatable->fetchrow_hashref) {
87 my $key = $row->{month} . "/" . $row->{day};
88 $day_month_holidays{$key}{title} = $row->{title};
89 $day_month_holidays{$key}{description} = $row->{description};
90 $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
91 $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
93 $self->{'day_month_holidays'} = \%day_month_holidays;
95 my $special = $dbh->prepare( 'SELECT day, month, year, title, description
96 FROM special_holidays
97 WHERE ( branchcode = ? )
98 AND (isexception = ?)' );
99 $special->execute($branch,1);
100 my %exception_holidays;
101 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
102 $exception_holidays{"$year/$month/$day"}{title} = $title;
103 $exception_holidays{"$year/$month/$day"}{description} = $description;
104 $exception_holidays{"$year/$month/$day"}{date} =
105 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
107 $self->{'exception_holidays'} = \%exception_holidays;
109 $special->execute($branch,0);
110 my %single_holidays;
111 while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
112 $single_holidays{"$year/$month/$day"}{title} = $title;
113 $single_holidays{"$year/$month/$day"}{description} = $description;
114 $single_holidays{"$year/$month/$day"}{date} =
115 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
117 $self->{'single_holidays'} = \%single_holidays;
118 return $self;
121 =head2 get_week_days_holidays
123 $week_days_holidays = $calendar->get_week_days_holidays();
125 Returns a hash reference to week days holidays.
127 =cut
129 sub get_week_days_holidays {
130 my $self = shift @_;
131 my $week_days_holidays = $self->{'week_days_holidays'};
132 return $week_days_holidays;
135 =head2 get_day_month_holidays
137 $day_month_holidays = $calendar->get_day_month_holidays();
139 Returns a hash reference to day month holidays.
141 =cut
143 sub get_day_month_holidays {
144 my $self = shift @_;
145 my $day_month_holidays = $self->{'day_month_holidays'};
146 return $day_month_holidays;
149 =head2 get_exception_holidays
151 $exception_holidays = $calendar->exception_holidays();
153 Returns a hash reference to exception holidays. This kind of days are those
154 which stands for a holiday, but you wanted to make an exception for this particular
155 date.
157 =cut
159 sub get_exception_holidays {
160 my $self = shift @_;
161 my $exception_holidays = $self->{'exception_holidays'};
162 return $exception_holidays;
165 =head2 get_single_holidays
167 $single_holidays = $calendar->get_single_holidays();
169 Returns a hash reference to single holidays. This kind of holidays are those which
170 happened just one time.
172 =cut
174 sub get_single_holidays {
175 my $self = shift @_;
176 my $single_holidays = $self->{'single_holidays'};
177 return $single_holidays;
180 =head2 insert_week_day_holiday
182 insert_week_day_holiday(weekday => $weekday,
183 title => $title,
184 description => $description);
186 Inserts a new week day for $self->{branchcode}.
188 C<$day> Is the week day to make holiday.
190 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
192 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
194 =cut
196 sub insert_week_day_holiday {
197 my $self = shift @_;
198 my %options = @_;
200 my $weekday = $options{weekday};
201 croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/;
203 my $dbh = C4::Context->dbh();
204 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )");
205 $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description});
206 $self->{'week_days_holidays'}->{$weekday}{title} = $options{title};
207 $self->{'week_days_holidays'}->{$weekday}{description} = $options{description};
208 return $self;
211 =head2 insert_day_month_holiday
213 insert_day_month_holiday(day => $day,
214 month => $month,
215 title => $title,
216 description => $description);
218 Inserts a new day month holiday for $self->{branchcode}.
220 C<$day> Is the day month to make the date to insert.
222 C<$month> Is month to make the date to insert.
224 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
226 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
228 =cut
230 sub insert_day_month_holiday {
231 my $self = shift @_;
232 my %options = @_;
234 my $dbh = C4::Context->dbh();
235 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
236 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
237 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
238 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
239 return $self;
242 =head2 insert_single_holiday
244 insert_single_holiday(day => $day,
245 month => $month,
246 year => $year,
247 title => $title,
248 description => $description);
250 Inserts a new single holiday for $self->{branchcode}.
252 C<$day> Is the day month to make the date to insert.
254 C<$month> Is month to make the date to insert.
256 C<$year> Is year to make the date to insert.
258 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
260 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
262 =cut
264 sub insert_single_holiday {
265 my $self = shift @_;
266 my %options = @_;
267 @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
268 if $options{date} && !$options{day};
270 my $dbh = C4::Context->dbh();
271 my $isexception = 0;
272 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
273 $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
274 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
275 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
278 # changed the 'single_holidays' table, lets force/reset its cache
279 my $cache = Koha::Cache->get_instance();
280 $cache->clear_from_cache( 'single_holidays') ;
282 return $self;
286 =head2 insert_exception_holiday
288 insert_exception_holiday(day => $day,
289 month => $month,
290 year => $year,
291 title => $title,
292 description => $description);
294 Inserts a new exception holiday for $self->{branchcode}.
296 C<$day> Is the day month to make the date to insert.
298 C<$month> Is month to make the date to insert.
300 C<$year> Is year to make the date to insert.
302 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
304 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
306 =cut
308 sub insert_exception_holiday {
309 my $self = shift @_;
310 my %options = @_;
312 @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
313 if $options{date} && !$options{day};
315 my $dbh = C4::Context->dbh();
316 my $isexception = 1;
317 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
318 $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
319 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
320 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
322 # changed the 'single_holidays' table, lets force/reset its cache
323 my $cache = Koha::Cache->get_instance();
324 $cache->clear_from_cache( 'single_holidays') ;
326 return $self;
329 =head2 ModWeekdayholiday
331 ModWeekdayholiday(weekday =>$weekday,
332 title => $title,
333 description => $description)
335 Modifies the title and description of a weekday for $self->{branchcode}.
337 C<$weekday> Is the title to update for the holiday.
339 C<$description> Is the description to update for the holiday.
341 =cut
343 sub ModWeekdayholiday {
344 my $self = shift @_;
345 my %options = @_;
347 my $dbh = C4::Context->dbh();
348 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
349 $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday});
350 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
351 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
352 return $self;
355 =head2 ModDaymonthholiday
357 ModDaymonthholiday(day => $day,
358 month => $month,
359 title => $title,
360 description => $description);
362 Modifies the title and description for a day/month holiday for $self->{branchcode}.
364 C<$day> The day of the month for the update.
366 C<$month> The month to be used for the update.
368 C<$title> The title to be updated for the holiday.
370 C<$description> The description to be update for the holiday.
372 =cut
374 sub ModDaymonthholiday {
375 my $self = shift @_;
376 my %options = @_;
378 my $dbh = C4::Context->dbh();
379 my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
380 $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode});
381 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
382 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
383 return $self;
386 =head2 ModSingleholiday
388 ModSingleholiday(day => $day,
389 month => $month,
390 year => $year,
391 title => $title,
392 description => $description);
394 Modifies the title and description for a single holiday for $self->{branchcode}.
396 C<$day> Is the day of the month to make the update.
398 C<$month> Is the month to make the update.
400 C<$year> Is the year to make the update.
402 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
404 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
406 =cut
408 sub ModSingleholiday {
409 my $self = shift @_;
410 my %options = @_;
412 my $dbh = C4::Context->dbh();
413 my $isexception = 0;
415 my $updateHoliday = $dbh->prepare("
416 UPDATE special_holidays SET title = ?, description = ?
417 WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
418 $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);
419 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
420 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
422 # changed the 'single_holidays' table, lets force/reset its cache
423 my $cache = Koha::Cache->get_instance();
424 $cache->clear_from_cache( 'single_holidays') ;
426 return $self;
429 =head2 ModExceptionholiday
431 ModExceptionholiday(day => $day,
432 month => $month,
433 year => $year,
434 title => $title,
435 description => $description);
437 Modifies the title and description for an exception holiday for $self->{branchcode}.
439 C<$day> Is the day of the month for the holiday.
441 C<$month> Is the month for the holiday.
443 C<$year> Is the year for the holiday.
445 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
447 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
449 =cut
451 sub ModExceptionholiday {
452 my $self = shift @_;
453 my %options = @_;
455 my $dbh = C4::Context->dbh();
456 my $isexception = 1;
457 my $updateHoliday = $dbh->prepare("
458 UPDATE special_holidays SET title = ?, description = ?
459 WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
460 $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);
461 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
462 $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
464 # changed the 'single_holidays' table, lets force/reset its cache
465 my $cache = Koha::Cache->get_instance();
466 $cache->clear_from_cache( 'single_holidays') ;
468 return $self;
471 =head2 delete_holiday
473 delete_holiday(weekday => $weekday
474 day => $day,
475 month => $month,
476 year => $year);
478 Delete a holiday for $self->{branchcode}.
480 C<$weekday> Is the week day to delete.
482 C<$day> Is the day month to make the date to delete.
484 C<$month> Is month to make the date to delete.
486 C<$year> Is year to make the date to delete.
488 =cut
490 sub delete_holiday {
491 my $self = shift @_;
492 my %options = @_;
494 # Verify what kind of holiday that day is. For example, if it is
495 # a repeatable holiday, this should check if there are some exception
496 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
497 # ok just deleting it.
499 my $dbh = C4::Context->dbh();
500 my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
501 $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
502 if ($isSingleHoliday->rows) {
503 my $id = $isSingleHoliday->fetchrow;
504 $isSingleHoliday->finish; # Close the last query
506 my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
507 $deleteHoliday->execute($id);
508 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
509 } else {
510 $isSingleHoliday->finish; # Close the last query
512 my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
513 $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
514 if ($isWeekdayHoliday->rows) {
515 my $id = $isWeekdayHoliday->fetchrow;
516 $isWeekdayHoliday->finish; # Close the last query
518 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
519 $updateExceptions->execute($options{weekday}, $self->{branchcode});
520 $updateExceptions->finish; # Close the last query
522 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
523 $deleteHoliday->execute($id);
524 delete($self->{'week_days_holidays'}->{$options{weekday}});
525 } else {
526 $isWeekdayHoliday->finish; # Close the last query
528 my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
529 $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
530 if ($isDayMonthHoliday->rows) {
531 my $id = $isDayMonthHoliday->fetchrow;
532 $isDayMonthHoliday->finish;
533 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
534 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
535 $updateExceptions->finish; # Close the last query
537 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
538 $deleteHoliday->execute($id);
539 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
544 # changed the 'single_holidays' table, lets force/reset its cache
545 my $cache = Koha::Cache->get_instance();
546 $cache->clear_from_cache( 'single_holidays') ;
548 return $self;
550 =head2 delete_holiday_range
552 delete_holiday_range(day => $day,
553 month => $month,
554 year => $year);
556 Delete a holiday range of dates for $self->{branchcode}.
558 C<$day> Is the day month to make the date to delete.
560 C<$month> Is month to make the date to delete.
562 C<$year> Is year to make the date to delete.
564 =cut
566 sub delete_holiday_range {
567 my $self = shift;
568 my %options = @_;
570 my $dbh = C4::Context->dbh();
571 my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
572 $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
574 # changed the 'single_holidays' table, lets force/reset its cache
575 my $cache = Koha::Cache->get_instance();
576 $cache->clear_from_cache( 'single_holidays') ;
580 =head2 delete_holiday_range_repeatable
582 delete_holiday_range_repeatable(day => $day,
583 month => $month);
585 Delete a holiday for $self->{branchcode}.
587 C<$day> Is the day month to make the date to delete.
589 C<$month> Is month to make the date to delete.
591 =cut
593 sub delete_holiday_range_repeatable {
594 my $self = shift;
595 my %options = @_;
597 my $dbh = C4::Context->dbh();
598 my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
599 $sth->execute($self->{branchcode}, $options{day}, $options{month});
602 =head2 delete_exception_holiday_range
604 delete_exception_holiday_range(weekday => $weekday
605 day => $day,
606 month => $month,
607 year => $year);
609 Delete a holiday for $self->{branchcode}.
611 C<$day> Is the day month to make the date to delete.
613 C<$month> Is month to make the date to delete.
615 C<$year> Is year to make the date to delete.
617 =cut
619 sub delete_exception_holiday_range {
620 my $self = shift;
621 my %options = @_;
623 my $dbh = C4::Context->dbh();
624 my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)");
625 $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
627 # changed the 'single_holidays' table, lets force/reset its cache
628 my $cache = Koha::Cache->get_instance();
629 $cache->clear_from_cache( 'single_holidays') ;
632 =head2 isHoliday
634 $isHoliday = isHoliday($day, $month $year);
636 C<$day> Is the day to check whether if is a holiday or not.
638 C<$month> Is the month to check whether if is a holiday or not.
640 C<$year> Is the year to check whether if is a holiday or not.
642 =cut
644 sub isHoliday {
645 my ($self, $day, $month, $year) = @_;
646 # FIXME - date strings are stored in non-padded metric format. should change to iso.
647 # FIXME - should change arguments to accept C4::Dates object
648 $month=$month+0;
649 $year=$year+0;
650 $day=$day+0;
651 my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7;
652 my $weekDays = $self->get_week_days_holidays();
653 my $dayMonths = $self->get_day_month_holidays();
654 my $exceptions = $self->get_exception_holidays();
655 my $singles = $self->get_single_holidays();
656 if (defined($exceptions->{"$year/$month/$day"})) {
657 return 0;
658 } else {
659 if ((exists($weekDays->{$weekday})) ||
660 (exists($dayMonths->{"$month/$day"})) ||
661 (exists($singles->{"$year/$month/$day"}))) {
662 return 1;
663 } else {
664 return 0;
670 =head2 copy_to_branch
672 $calendar->copy_to_branch($target_branch)
674 =cut
676 sub copy_to_branch {
677 my ($self, $target_branch) = @_;
679 croak "No target_branch" unless $target_branch;
681 my $target_calendar = C4::Calendar->new(branchcode => $target_branch);
683 my ($y, $m, $d) = Today();
684 my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d;
686 my $wdh = $self->get_week_days_holidays;
687 $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } )
688 foreach keys %$wdh;
689 $target_calendar->insert_day_month_holiday(%$_)
690 foreach values %{ $self->get_day_month_holidays };
691 $target_calendar->insert_exception_holiday(%$_)
692 foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays };
693 $target_calendar->insert_single_holiday(%$_)
694 foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays };
696 return 1;
699 =head2 addDate
701 my ($day, $month, $year) = $calendar->addDate($date, $offset)
703 C<$date> is a C4::Dates object representing the starting date of the interval.
705 C<$offset> Is the number of days that this function has to count from $date.
707 =cut
709 sub addDate {
710 my ($self, $startdate, $offset) = @_;
711 my ($year,$month,$day) = split("-",$startdate->output('iso'));
712 my $daystep = 1;
713 if ($offset < 0) { # In case $offset is negative
714 # $offset = $offset*(-1);
715 $daystep = -1;
717 my $daysMode = C4::Context->preference('useDaysMode');
718 if ($daysMode eq 'Datedue') {
719 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
720 while ($self->isHoliday($day, $month, $year)) {
721 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
723 } elsif($daysMode eq 'Calendar') {
724 while ($offset != 0) {
725 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
726 if (!($self->isHoliday($day, $month, $year))) {
727 $offset = $offset - $daystep;
730 } else { ## ($daysMode eq 'Days')
731 ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
733 return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso'));
736 =head2 daysBetween
738 my $daysBetween = $calendar->daysBetween($startdate, $enddate)
740 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
742 Returns the number of non-holiday days in the interval.
743 useDaysMode syspref has no effect here.
744 =cut
746 sub daysBetween {
747 my $self = shift or return;
748 my $startdate = shift or return;
749 my $enddate = shift or return;
750 my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
751 my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso'));
752 if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
753 return 0;
754 # we don't go backwards ( FIXME - handle this error better )
756 my $count = 0;
757 while (1) {
758 ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
759 unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
760 $count++;
762 ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
764 return($count);
769 __END__
771 =head1 AUTHOR
773 Koha Physics Library UNLP <matias_veleda@hotmail.com>
775 =cut