Lines 20-53
use warnings;
Link Here
|
20 |
use vars qw($VERSION @EXPORT); |
20 |
use vars qw($VERSION @EXPORT); |
21 |
|
21 |
|
22 |
use Carp; |
22 |
use Carp; |
23 |
use Date::Calc qw( Date_to_Days ); |
23 |
use Date::Calc qw( Date_to_Days Today); |
24 |
|
24 |
|
25 |
use C4::Context; |
25 |
use C4::Context; |
26 |
|
26 |
|
27 |
BEGIN { |
27 |
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; |
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 |
48 |
); |
49 |
} |
50 |
|
51 |
=head1 NAME |
28 |
=head1 NAME |
52 |
|
29 |
|
53 |
C4::Calendar::Calendar - Koha module dealing with holidays. |
30 |
C4::Calendar::Calendar - Koha module dealing with holidays. |
Lines 123-129
sub _init {
Link Here
|
123 |
$exception_holidays{"$year/$month/$day"}{title} = $title; |
100 |
$exception_holidays{"$year/$month/$day"}{title} = $title; |
124 |
$exception_holidays{"$year/$month/$day"}{description} = $description; |
101 |
$exception_holidays{"$year/$month/$day"}{description} = $description; |
125 |
$exception_holidays{"$year/$month/$day"}{date} = |
102 |
$exception_holidays{"$year/$month/$day"}{date} = |
126 |
sprintf("%04d-%02d-%02d", $year, $month, $day); |
103 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
127 |
} |
104 |
} |
128 |
$self->{'exception_holidays'} = \%exception_holidays; |
105 |
$self->{'exception_holidays'} = \%exception_holidays; |
129 |
|
106 |
|
Lines 133-139
sub _init {
Link Here
|
133 |
$single_holidays{"$year/$month/$day"}{title} = $title; |
110 |
$single_holidays{"$year/$month/$day"}{title} = $title; |
134 |
$single_holidays{"$year/$month/$day"}{description} = $description; |
111 |
$single_holidays{"$year/$month/$day"}{description} = $description; |
135 |
$single_holidays{"$year/$month/$day"}{date} = |
112 |
$single_holidays{"$year/$month/$day"}{date} = |
136 |
sprintf("%04d-%02d-%02d", $year, $month, $day); |
113 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
137 |
} |
114 |
} |
138 |
$self->{'single_holidays'} = \%single_holidays; |
115 |
$self->{'single_holidays'} = \%single_holidays; |
139 |
return $self; |
116 |
return $self; |
Lines 218-228
sub insert_week_day_holiday {
Link Here
|
218 |
my $self = shift @_; |
195 |
my $self = shift @_; |
219 |
my %options = @_; |
196 |
my %options = @_; |
220 |
|
197 |
|
|
|
198 |
my $weekday = $options{weekday}; |
199 |
croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/; |
200 |
|
221 |
my $dbh = C4::Context->dbh(); |
201 |
my $dbh = C4::Context->dbh(); |
222 |
my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); |
202 |
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}); |
203 |
$insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description}); |
224 |
$self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; |
204 |
$self->{'week_days_holidays'}->{$weekday}{title} = $options{title}; |
225 |
$self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; |
205 |
$self->{'week_days_holidays'}->{$weekday}{description} = $options{description}; |
226 |
return $self; |
206 |
return $self; |
227 |
} |
207 |
} |
228 |
|
208 |
|
Lines 283-288
sub insert_single_holiday {
Link Here
|
283 |
my $self = shift @_; |
263 |
my $self = shift @_; |
284 |
my %options = @_; |
264 |
my %options = @_; |
285 |
|
265 |
|
|
|
266 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
267 |
if $options{date} && !$options{day}; |
268 |
|
286 |
my $dbh = C4::Context->dbh(); |
269 |
my $dbh = C4::Context->dbh(); |
287 |
my $isexception = 0; |
270 |
my $isexception = 0; |
288 |
my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
271 |
my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
Lines 318-323
sub insert_exception_holiday {
Link Here
|
318 |
my $self = shift @_; |
301 |
my $self = shift @_; |
319 |
my %options = @_; |
302 |
my %options = @_; |
320 |
|
303 |
|
|
|
304 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
305 |
if $options{date} && !$options{day}; |
306 |
|
321 |
my $dbh = C4::Context->dbh(); |
307 |
my $dbh = C4::Context->dbh(); |
322 |
my $isexception = 1; |
308 |
my $isexception = 1; |
323 |
my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
309 |
my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
Lines 567-572
sub isHoliday {
Link Here
|
567 |
|
553 |
|
568 |
} |
554 |
} |
569 |
|
555 |
|
|
|
556 |
=head2 copy_to_branch |
557 |
|
558 |
$calendar->copy_to_branch($target_branch) |
559 |
|
560 |
=cut |
561 |
|
562 |
sub copy_to_branch { |
563 |
my ($self, $target_branch) = @_; |
564 |
|
565 |
croak "No target_branch" unless $target_branch; |
566 |
|
567 |
my $target_calendar = C4::Calendar->new(branchcode => $target_branch); |
568 |
|
569 |
my ($y, $m, $d) = Today(); |
570 |
my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; |
571 |
|
572 |
my $wdh = $self->get_week_days_holidays; |
573 |
$target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } ) |
574 |
foreach keys %$wdh; |
575 |
$target_calendar->insert_day_month_holiday(%$_) |
576 |
foreach values %{ $self->get_day_month_holidays }; |
577 |
$target_calendar->insert_exception_holiday(%$_) |
578 |
foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; |
579 |
$target_calendar->insert_single_holiday(%$_) |
580 |
foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; |
581 |
|
582 |
return 1; |
583 |
} |
584 |
|
585 |
=head2 daysBetween |
586 |
|
587 |
my $daysBetween = $calendar->daysBetween($startdate, $enddate) |
588 |
|
589 |
C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. |
590 |
|
591 |
Returns the number of non-holiday days in the interval. |
592 |
useDaysMode syspref has no effect here. |
593 |
=cut |
594 |
|
595 |
sub daysBetween ($$$) { |
596 |
my $self = shift or return undef; |
597 |
my $startdate = shift or return undef; |
598 |
my $enddate = shift or return undef; |
599 |
my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); |
600 |
my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); |
601 |
if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { |
602 |
return 0; |
603 |
# we don't go backwards ( FIXME - handle this error better ) |
604 |
} |
605 |
my $count = 0; |
606 |
while (1) { |
607 |
($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day |
608 |
unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) { |
609 |
$count++; |
610 |
} |
611 |
($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); |
612 |
} |
613 |
return($count); |
614 |
} |
615 |
|
570 |
=head2 addDate |
616 |
=head2 addDate |
571 |
|
617 |
|
572 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
618 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
Lines 601-607
sub addDate {
Link Here
|
601 |
} else { ## ($daysMode eq 'Days') |
647 |
} else { ## ($daysMode eq 'Days') |
602 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
648 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
603 |
} |
649 |
} |
604 |
return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso')); |
650 |
return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); |
605 |
} |
651 |
} |
606 |
|
652 |
|
607 |
=head2 daysBetween |
653 |
=head2 daysBetween |