Bug 10853: All existing routing to get a CSV should return a MARC csv
[koha.git] / C4 / Calendar.pm
1 package C4::Calendar;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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
17
18 use strict;
19 use warnings;
20 use vars qw($VERSION @EXPORT);
21
22 use Carp;
23 use Date::Calc qw( Date_to_Days Today);
24
25 use C4::Context;
26
27 use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
28 =head1 NAME
29
30 C4::Calendar::Calendar - Koha module dealing with holidays.
31
32 =head1 SYNOPSIS
33
34     use C4::Calendar::Calendar;
35
36 =head1 DESCRIPTION
37
38 This package is used to deal with holidays. Through this package, you can set 
39 all kind of holidays for the library.
40
41 =head1 FUNCTIONS
42
43 =head2 new
44
45   $calendar = C4::Calendar->new(branchcode => $branchcode);
46
47 Each library branch has its own Calendar.  
48 C<$branchcode> specifies which Calendar you want.
49
50 =cut
51
52 sub new {
53     my $classname = shift @_;
54     my %options = @_;
55     my $self = bless({}, $classname);
56     foreach my $optionName (keys %options) {
57         $self->{lc($optionName)} = $options{$optionName};
58     }
59     defined($self->{branchcode}) or croak "No branchcode argument to new.  Should be C4::Calendar->new(branchcode => \$branchcode)";
60     $self->_init($self->{branchcode});
61     return $self;
62 }
63
64 sub _init {
65     my $self = shift @_;
66     my $branch = shift;
67     defined($branch) or die "No branchcode sent to _init";  # must test for defined here and above to allow ""
68     my $dbh = C4::Context->dbh();
69     my $repeatable = $dbh->prepare( 'SELECT *
70                                        FROM repeatable_holidays
71                                       WHERE ( branchcode = ? )
72                                         AND (ISNULL(weekday) = ?)' );
73     $repeatable->execute($branch,0);
74     my %week_days_holidays;
75     while (my $row = $repeatable->fetchrow_hashref) {
76         my $key = $row->{weekday};
77         $week_days_holidays{$key}{title}       = $row->{title};
78         $week_days_holidays{$key}{description} = $row->{description};
79     }
80     $self->{'week_days_holidays'} = \%week_days_holidays;
81
82     $repeatable->execute($branch,1);
83     my %day_month_holidays;
84     while (my $row = $repeatable->fetchrow_hashref) {
85         my $key = $row->{month} . "/" . $row->{day};
86         $day_month_holidays{$key}{title}       = $row->{title};
87         $day_month_holidays{$key}{description} = $row->{description};
88         $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
89         $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
90     }
91     $self->{'day_month_holidays'} = \%day_month_holidays;
92
93     my $special = $dbh->prepare( 'SELECT day, month, year, title, description
94                                     FROM special_holidays
95                                    WHERE ( branchcode = ? )
96                                      AND (isexception = ?)' );
97     $special->execute($branch,1);
98     my %exception_holidays;
99     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
100         $exception_holidays{"$year/$month/$day"}{title} = $title;
101         $exception_holidays{"$year/$month/$day"}{description} = $description;
102         $exception_holidays{"$year/$month/$day"}{date} = 
103                 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
104     }
105     $self->{'exception_holidays'} = \%exception_holidays;
106
107     $special->execute($branch,0);
108     my %single_holidays;
109     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
110         $single_holidays{"$year/$month/$day"}{title} = $title;
111         $single_holidays{"$year/$month/$day"}{description} = $description;
112         $single_holidays{"$year/$month/$day"}{date} = 
113                 sprintf(ISO_DATE_FORMAT, $year, $month, $day);
114     }
115     $self->{'single_holidays'} = \%single_holidays;
116     return $self;
117 }
118
119 =head2 get_week_days_holidays
120
121    $week_days_holidays = $calendar->get_week_days_holidays();
122
123 Returns a hash reference to week days holidays.
124
125 =cut
126
127 sub get_week_days_holidays {
128     my $self = shift @_;
129     my $week_days_holidays = $self->{'week_days_holidays'};
130     return $week_days_holidays;
131 }
132
133 =head2 get_day_month_holidays
134
135    $day_month_holidays = $calendar->get_day_month_holidays();
136
137 Returns a hash reference to day month holidays.
138
139 =cut
140
141 sub get_day_month_holidays {
142     my $self = shift @_;
143     my $day_month_holidays = $self->{'day_month_holidays'};
144     return $day_month_holidays;
145 }
146
147 =head2 get_exception_holidays
148
149     $exception_holidays = $calendar->exception_holidays();
150
151 Returns a hash reference to exception holidays. This kind of days are those
152 which stands for a holiday, but you wanted to make an exception for this particular
153 date.
154
155 =cut
156
157 sub get_exception_holidays {
158     my $self = shift @_;
159     my $exception_holidays = $self->{'exception_holidays'};
160     return $exception_holidays;
161 }
162
163 =head2 get_single_holidays
164
165     $single_holidays = $calendar->get_single_holidays();
166
167 Returns a hash reference to single holidays. This kind of holidays are those which
168 happend just one time.
169
170 =cut
171
172 sub get_single_holidays {
173     my $self = shift @_;
174     my $single_holidays = $self->{'single_holidays'};
175     return $single_holidays;
176 }
177
178 =head2 insert_week_day_holiday
179
180     insert_week_day_holiday(weekday => $weekday,
181                             title => $title,
182                             description => $description);
183
184 Inserts a new week day for $self->{branchcode}.
185
186 C<$day> Is the week day to make holiday.
187
188 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
189
190 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
191
192 =cut
193
194 sub insert_week_day_holiday {
195     my $self = shift @_;
196     my %options = @_;
197
198     my $weekday = $options{weekday};
199     croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/;
200
201     my $dbh = C4::Context->dbh();
202     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); 
203         $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description});
204     $self->{'week_days_holidays'}->{$weekday}{title} = $options{title};
205     $self->{'week_days_holidays'}->{$weekday}{description} = $options{description};
206     return $self;
207 }
208
209 =head2 insert_day_month_holiday
210
211     insert_day_month_holiday(day => $day,
212                              month => $month,
213                              title => $title,
214                              description => $description);
215
216 Inserts a new day month holiday for $self->{branchcode}.
217
218 C<$day> Is the day month to make the date to insert.
219
220 C<$month> Is month to make the date to insert.
221
222 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
223
224 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
225
226 =cut
227
228 sub insert_day_month_holiday {
229     my $self = shift @_;
230     my %options = @_;
231
232     my $dbh = C4::Context->dbh();
233     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
234         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
235     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
236     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
237     return $self;
238 }
239
240 =head2 insert_single_holiday
241
242     insert_single_holiday(day => $day,
243                           month => $month,
244                           year => $year,
245                           title => $title,
246                           description => $description);
247
248 Inserts a new single holiday for $self->{branchcode}.
249
250 C<$day> Is the day month to make the date to insert.
251
252 C<$month> Is month to make the date to insert.
253
254 C<$year> Is year to make the date to insert.
255
256 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
257
258 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
259
260 =cut
261
262 sub insert_single_holiday {
263     my $self = shift @_;
264     my %options = @_;
265     
266     @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
267       if $options{date} && !$options{day};
268
269         my $dbh = C4::Context->dbh();
270     my $isexception = 0;
271     my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
272         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
273     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
274     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
275     return $self;
276 }
277
278 =head2 insert_exception_holiday
279
280     insert_exception_holiday(day => $day,
281                              month => $month,
282                              year => $year,
283                              title => $title,
284                              description => $description);
285
286 Inserts a new exception holiday for $self->{branchcode}.
287
288 C<$day> Is the day month to make the date to insert.
289
290 C<$month> Is month to make the date to insert.
291
292 C<$year> Is year to make the date to insert.
293
294 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
295
296 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
297
298 =cut
299
300 sub insert_exception_holiday {
301     my $self = shift @_;
302     my %options = @_;
303
304     @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o )
305       if $options{date} && !$options{day};
306
307     my $dbh = C4::Context->dbh();
308     my $isexception = 1;
309     my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
310         $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
311     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
312     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
313     return $self;
314 }
315
316 =head2 ModWeekdayholiday
317
318     ModWeekdayholiday(weekday =>$weekday,
319                       title => $title,
320                       description => $description)
321
322 Modifies the title and description of a weekday for $self->{branchcode}.
323
324 C<$weekday> Is the title to update for the holiday.
325
326 C<$description> Is the description to update for the holiday.
327
328 =cut
329
330 sub ModWeekdayholiday {
331     my $self = shift @_;
332     my %options = @_;
333
334     my $dbh = C4::Context->dbh();
335     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
336     $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); 
337     $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
338     $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
339     return $self;
340 }
341
342 =head2 ModDaymonthholiday
343
344     ModDaymonthholiday(day => $day,
345                        month => $month,
346                        title => $title,
347                        description => $description);
348
349 Modifies the title and description for a day/month holiday for $self->{branchcode}.
350
351 C<$day> The day of the month for the update.
352
353 C<$month> The month to be used for the update.
354
355 C<$title> The title to be updated for the holiday.
356
357 C<$description> The description to be update for the holiday.
358
359 =cut
360
361 sub ModDaymonthholiday {
362     my $self = shift @_;
363     my %options = @_;
364
365     my $dbh = C4::Context->dbh();
366     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
367        $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); 
368     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
369     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
370     return $self;
371 }
372
373 =head2 ModSingleholiday
374
375     ModSingleholiday(day => $day,
376                      month => $month,
377                      year => $year,
378                      title => $title,
379                      description => $description);
380
381 Modifies the title and description for a single holiday for $self->{branchcode}.
382
383 C<$day> Is the day of the month to make the update.
384
385 C<$month> Is the month to make the update.
386
387 C<$year> Is the year to make the update.
388
389 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
390
391 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
392
393 =cut
394
395 sub ModSingleholiday {
396     my $self = shift @_;
397     my %options = @_;
398
399     my $dbh = C4::Context->dbh();
400     my $isexception = 0;
401     my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
402       $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);    
403     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
404     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
405     return $self;
406 }
407
408 =head2 ModExceptionholiday
409
410     ModExceptionholiday(day => $day,
411                         month => $month,
412                         year => $year,
413                         title => $title,
414                         description => $description);
415
416 Modifies the title and description for an exception holiday for $self->{branchcode}.
417
418 C<$day> Is the day of the month for the holiday.
419
420 C<$month> Is the month for the holiday.
421
422 C<$year> Is the year for the holiday.
423
424 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
425
426 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
427
428 =cut
429
430 sub ModExceptionholiday {
431     my $self = shift @_;
432     my %options = @_;
433
434     my $dbh = C4::Context->dbh();
435     my $isexception = 1;
436     my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
437     $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);    
438     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
439     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
440     return $self;
441 }
442
443 =head2 delete_holiday
444
445     delete_holiday(weekday => $weekday
446                    day => $day,
447                    month => $month,
448                    year => $year);
449
450 Delete a holiday for $self->{branchcode}.
451
452 C<$weekday> Is the week day to delete.
453
454 C<$day> Is the day month to make the date to delete.
455
456 C<$month> Is month to make the date to delete.
457
458 C<$year> Is year to make the date to delete.
459
460 =cut
461
462 sub delete_holiday {
463     my $self = shift @_;
464     my %options = @_;
465
466     # Verify what kind of holiday that day is. For example, if it is
467     # a repeatable holiday, this should check if there are some exception
468         # for that holiday rule. Otherwise, if it is a regular holiday, it´s 
469     # ok just deleting it.
470
471     my $dbh = C4::Context->dbh();
472     my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
473     $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
474     if ($isSingleHoliday->rows) {
475         my $id = $isSingleHoliday->fetchrow;
476         $isSingleHoliday->finish; # Close the last query
477
478         my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
479         $deleteHoliday->execute($id);
480         delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
481     } else {
482         $isSingleHoliday->finish; # Close the last query
483
484         my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
485         $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
486         if ($isWeekdayHoliday->rows) {
487             my $id = $isWeekdayHoliday->fetchrow;
488             $isWeekdayHoliday->finish; # Close the last query
489
490             my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
491             $updateExceptions->execute($options{weekday}, $self->{branchcode});
492             $updateExceptions->finish; # Close the last query
493
494             my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
495             $deleteHoliday->execute($id);
496             delete($self->{'week_days_holidays'}->{$options{weekday}});
497         } else {
498             $isWeekdayHoliday->finish; # Close the last query
499
500             my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
501             $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
502             if ($isDayMonthHoliday->rows) {
503                 my $id = $isDayMonthHoliday->fetchrow;
504                 $isDayMonthHoliday->finish;
505                 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
506                 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
507                 $updateExceptions->finish; # Close the last query
508
509                 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
510                 $deleteHoliday->execute($id);
511                 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
512             }
513         }
514     }
515     return $self;
516 }
517 =head2 delete_holiday_range
518
519     delete_holiday_range(day => $day,
520                    month => $month,
521                    year => $year);
522
523 Delete a holiday range of dates for $self->{branchcode}.
524
525 C<$day> Is the day month to make the date to delete.
526
527 C<$month> Is month to make the date to delete.
528
529 C<$year> Is year to make the date to delete.
530
531 =cut
532
533 sub delete_holiday_range {
534     my $self = shift;
535     my %options = @_;
536
537     my $dbh = C4::Context->dbh();
538     my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
539     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
540 }
541
542 =head2 delete_holiday_range_repeatable
543
544     delete_holiday_range_repeatable(day => $day,
545                    month => $month);
546
547 Delete a holiday for $self->{branchcode}.
548
549 C<$day> Is the day month to make the date to delete.
550
551 C<$month> Is month to make the date to delete.
552
553 =cut
554
555 sub delete_holiday_range_repeatable {
556     my $self = shift;
557     my %options = @_;
558
559     my $dbh = C4::Context->dbh();
560     my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
561     $sth->execute($self->{branchcode}, $options{day}, $options{month});
562 }
563
564 =head2 delete_exception_holiday_range
565
566     delete_exception_holiday_range(weekday => $weekday
567                    day => $day,
568                    month => $month,
569                    year => $year);
570
571 Delete a holiday for $self->{branchcode}.
572
573 C<$day> Is the day month to make the date to delete.
574
575 C<$month> Is month to make the date to delete.
576
577 C<$year> Is year to make the date to delete.
578
579 =cut
580
581 sub delete_exception_holiday_range {
582     my $self = shift;
583     my %options = @_;
584
585     my $dbh = C4::Context->dbh();
586     my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)");
587     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
588 }
589
590 =head2 isHoliday
591
592     $isHoliday = isHoliday($day, $month $year);
593
594 C<$day> Is the day to check whether if is a holiday or not.
595
596 C<$month> Is the month to check whether if is a holiday or not.
597
598 C<$year> Is the year to check whether if is a holiday or not.
599
600 =cut
601
602 sub isHoliday {
603     my ($self, $day, $month, $year) = @_;
604         # FIXME - date strings are stored in non-padded metric format. should change to iso.
605         # FIXME - should change arguments to accept C4::Dates object
606         $month=$month+0;
607         $year=$year+0;
608         $day=$day+0;
609     my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; 
610     my $weekDays   = $self->get_week_days_holidays();
611     my $dayMonths  = $self->get_day_month_holidays();
612     my $exceptions = $self->get_exception_holidays();
613     my $singles    = $self->get_single_holidays();
614     if (defined($exceptions->{"$year/$month/$day"})) {
615         return 0;
616     } else {
617         if ((exists($weekDays->{$weekday})) ||
618             (exists($dayMonths->{"$month/$day"})) ||
619             (exists($singles->{"$year/$month/$day"}))) {
620                         return 1;
621         } else {
622             return 0;
623         }
624     }
625
626 }
627
628 =head2 copy_to_branch
629
630     $calendar->copy_to_branch($target_branch)
631
632 =cut
633
634 sub copy_to_branch {
635     my ($self, $target_branch) = @_;
636
637     croak "No target_branch" unless $target_branch;
638
639     my $target_calendar = C4::Calendar->new(branchcode => $target_branch);
640
641     my ($y, $m, $d) = Today();
642     my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d;
643
644     my $wdh = $self->get_week_days_holidays;
645     $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } )
646       foreach keys %$wdh;
647     $target_calendar->insert_day_month_holiday(%$_)
648       foreach values %{ $self->get_day_month_holidays };
649     $target_calendar->insert_exception_holiday(%$_)
650       foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays };
651     $target_calendar->insert_single_holiday(%$_)
652       foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays };
653
654     return 1;
655 }
656
657 =head2 addDate
658
659     my ($day, $month, $year) = $calendar->addDate($date, $offset)
660
661 C<$date> is a C4::Dates object representing the starting date of the interval.
662
663 C<$offset> Is the number of days that this function has to count from $date.
664
665 =cut
666
667 sub addDate {
668     my ($self, $startdate, $offset) = @_;
669     my ($year,$month,$day) = split("-",$startdate->output('iso'));
670         my $daystep = 1;
671         if ($offset < 0) { # In case $offset is negative
672        # $offset = $offset*(-1);
673                 $daystep = -1;
674     }
675         my $daysMode = C4::Context->preference('useDaysMode');
676     if ($daysMode eq 'Datedue') {
677         ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
678                 while ($self->isHoliday($day, $month, $year)) {
679             ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
680         }
681     } elsif($daysMode eq 'Calendar') {
682         while ($offset !=  0) {
683             ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
684             if (!($self->isHoliday($day, $month, $year))) {
685                 $offset = $offset - $daystep;
686                         }
687         }
688         } else { ## ($daysMode eq 'Days') 
689         ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
690     }
691     return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso'));
692 }
693
694 =head2 daysBetween
695
696     my $daysBetween = $calendar->daysBetween($startdate, $enddate)
697
698 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
699
700 Returns the number of non-holiday days in the interval.
701 useDaysMode syspref has no effect here.
702 =cut
703
704 sub daysBetween {
705     my $self      = shift or return;
706     my $startdate = shift or return;
707     my $enddate   = shift or return;
708     my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
709     my ($yearTo,  $monthTo,  $dayTo  ) = split("-",  $enddate->output('iso'));
710     if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
711         return 0;
712         # we don't go backwards  ( FIXME - handle this error better )
713     }
714     my $count = 0;
715     while (1) {
716         ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
717         unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
718             $count++;
719         }
720         ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
721     }
722     return($count);
723 }
724
725 1;
726
727 __END__
728
729 =head1 AUTHOR
730
731 Koha Physics Library UNLP <matias_veleda@hotmail.com>
732
733 =cut