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 283-288
sub insert_single_holiday {
Link Here
|
283 |
my $self = shift @_; |
260 |
my $self = shift @_; |
284 |
my %options = @_; |
261 |
my %options = @_; |
285 |
|
262 |
|
|
|
263 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
264 |
if $options{date} && !$options{day}; |
265 |
|
286 |
my $dbh = C4::Context->dbh(); |
266 |
my $dbh = C4::Context->dbh(); |
287 |
my $isexception = 0; |
267 |
my $isexception = 0; |
288 |
my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
268 |
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 @_; |
298 |
my $self = shift @_; |
319 |
my %options = @_; |
299 |
my %options = @_; |
320 |
|
300 |
|
|
|
301 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
302 |
if $options{date} && !$options{day}; |
303 |
|
321 |
my $dbh = C4::Context->dbh(); |
304 |
my $dbh = C4::Context->dbh(); |
322 |
my $isexception = 1; |
305 |
my $isexception = 1; |
323 |
my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
306 |
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 |
|
550 |
|
568 |
} |
551 |
} |
569 |
|
552 |
|
|
|
553 |
=head2 copy_to_branch |
554 |
|
555 |
$calendar->copy_to_branch($target_branch) |
556 |
|
557 |
=cut |
558 |
|
559 |
sub copy_to_branch { |
560 |
my ($self, $target_branch) = @_; |
561 |
|
562 |
croak "No target_branch" unless $target_branch; |
563 |
|
564 |
my $target_calendar = C4::Calendar->new(branchcode => $target_branch); |
565 |
|
566 |
my ($y, $m, $d) = Today(); |
567 |
my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; |
568 |
|
569 |
$target_calendar->insert_week_day_holiday(%$_) |
570 |
foreach values %{ $self->get_week_days_holidays }; |
571 |
$target_calendar->insert_day_month_holiday(%$_) |
572 |
foreach values %{ $self->get_day_month_holidays }; |
573 |
$target_calendar->insert_exception_holiday(%$_) |
574 |
foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; |
575 |
$target_calendar->insert_single_holiday(%$_) |
576 |
foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; |
577 |
|
578 |
return 1; |
579 |
} |
580 |
|
581 |
=head2 daysBetween |
582 |
|
583 |
my $daysBetween = $calendar->daysBetween($startdate, $enddate) |
584 |
|
585 |
C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. |
586 |
|
587 |
Returns the number of non-holiday days in the interval. |
588 |
useDaysMode syspref has no effect here. |
589 |
=cut |
590 |
|
591 |
sub daysBetween ($$$) { |
592 |
my $self = shift or return undef; |
593 |
my $startdate = shift or return undef; |
594 |
my $enddate = shift or return undef; |
595 |
my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); |
596 |
my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); |
597 |
if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { |
598 |
return 0; |
599 |
# we don't go backwards ( FIXME - handle this error better ) |
600 |
} |
601 |
my $count = 0; |
602 |
while (1) { |
603 |
($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day |
604 |
unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) { |
605 |
$count++; |
606 |
} |
607 |
($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); |
608 |
} |
609 |
return($count); |
610 |
} |
611 |
|
570 |
=head2 addDate |
612 |
=head2 addDate |
571 |
|
613 |
|
572 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
614 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
Lines 601-607
sub addDate {
Link Here
|
601 |
} else { ## ($daysMode eq 'Days') |
643 |
} else { ## ($daysMode eq 'Days') |
602 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
644 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
603 |
} |
645 |
} |
604 |
return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso')); |
646 |
return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); |
605 |
} |
647 |
} |
606 |
|
648 |
|
607 |
=head2 daysBetween |
649 |
=head2 daysBetween |