X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;ds=sidebyside;f=C4%2FCalendar.pm;h=257119c8e32f01669bb69eed94001ce41be0b6ad;hb=9d6d641d1f8b77271800f43bc027b651f9aea52b;hp=dc9037b9c888de9eac45850bac2eee8b86207e6d;hpb=ef087b000620b5c5d113a4cabe5b8c22c984b3f8;p=srvgit diff --git a/C4/Calendar.pm b/C4/Calendar.pm index dc9037b9c8..257119c8e3 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -2,51 +2,30 @@ package C4::Calendar; # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; use warnings; -use vars qw($VERSION @EXPORT); +use vars qw(@EXPORT); -use Carp; -use Date::Calc qw( Date_to_Days ); +use Carp qw( croak ); +use Date::Calc qw( Today ); use C4::Context; +use Koha::Caches; -BEGIN { - # set the version for version checking - $VERSION = 3.01; - require Exporter; - @EXPORT = qw( - &get_week_days_holidays - &get_day_month_holidays - &get_exception_holidays - &get_single_holidays - &insert_week_day_holiday - &insert_day_month_holiday - &insert_single_holiday - &insert_exception_holiday - &ModWeekdayholiday - &ModDaymonthholiday - &ModSingleholiday - &ModExceptionholiday - &delete_holiday - &isHoliday - &addDate - &daysBetween - ); -} +use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; =head1 NAME @@ -123,7 +102,7 @@ sub _init { $exception_holidays{"$year/$month/$day"}{title} = $title; $exception_holidays{"$year/$month/$day"}{description} = $description; $exception_holidays{"$year/$month/$day"}{date} = - sprintf("%04d-%02d-%02d", $year, $month, $day); + sprintf(ISO_DATE_FORMAT, $year, $month, $day); } $self->{'exception_holidays'} = \%exception_holidays; @@ -133,7 +112,7 @@ sub _init { $single_holidays{"$year/$month/$day"}{title} = $title; $single_holidays{"$year/$month/$day"}{description} = $description; $single_holidays{"$year/$month/$day"}{date} = - sprintf("%04d-%02d-%02d", $year, $month, $day); + sprintf(ISO_DATE_FORMAT, $year, $month, $day); } $self->{'single_holidays'} = \%single_holidays; return $self; @@ -188,7 +167,7 @@ sub get_exception_holidays { $single_holidays = $calendar->get_single_holidays(); Returns a hash reference to single holidays. This kind of holidays are those which -happend just one time. +happened just one time. =cut @@ -218,11 +197,14 @@ sub insert_week_day_holiday { my $self = shift @_; my %options = @_; + my $weekday = $options{weekday}; + croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/; + my $dbh = C4::Context->dbh(); - my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); - $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description}); - $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; - $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; + my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (branchcode,weekday,day,month,title,description) values ( ?,?,NULL,NULL,?,? )"); + $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description}); + $self->{'week_days_holidays'}->{$weekday}{title} = $options{title}; + $self->{'week_days_holidays'}->{$weekday}{description} = $options{description}; return $self; } @@ -250,7 +232,7 @@ sub insert_day_month_holiday { my %options = @_; my $dbh = C4::Context->dbh(); - my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )"); + my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (branchcode,weekday,day,month,title,description) values (?, NULL, ?, ?, ?,? )"); $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description}); $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; @@ -282,14 +264,24 @@ C<$description> Is the description to store for the holiday formed by $year/$mon sub insert_single_holiday { my $self = shift @_; my %options = @_; - + @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) + if $options{date} && !$options{day}; + my $dbh = C4::Context->dbh(); my $isexception = 0; - my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); + my $insertHoliday = $dbh->prepare("insert into special_holidays (branchcode,day,month,year,isexception,title,description) values (?,?,?,?,?,?,?)"); $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + return $self; + } =head2 insert_exception_holiday @@ -318,12 +310,21 @@ sub insert_exception_holiday { my $self = shift @_; my %options = @_; + @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) + if $options{date} && !$options{day}; + my $dbh = C4::Context->dbh(); my $isexception = 1; - my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); + my $insertException = $dbh->prepare("insert into special_holidays (branchcode,day,month,year,isexception,title,description) values (?,?,?,?,?,?,?)"); $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + return $self; } @@ -412,10 +413,19 @@ sub ModSingleholiday { my $dbh = C4::Context->dbh(); my $isexception = 0; - my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); + + my $updateHoliday = $dbh->prepare(" +UPDATE special_holidays SET title = ?, description = ? + WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + return $self; } @@ -447,10 +457,18 @@ sub ModExceptionholiday { my $dbh = C4::Context->dbh(); my $isexception = 1; - my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); - $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); + my $updateHoliday = $dbh->prepare(" +UPDATE special_holidays SET title = ?, description = ? + WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); + $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + return $self; } @@ -479,7 +497,7 @@ sub delete_holiday { # Verify what kind of holiday that day is. For example, if it is # a repeatable holiday, this should check if there are some exception - # for that holiday rule. Otherwise, if it is a regular holiday, it´s + # for that holiday rule. Otherwise, if it is a regular holiday, it´s # ok just deleting it. my $dbh = C4::Context->dbh(); @@ -526,8 +544,97 @@ sub delete_holiday { } } } + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + return $self; } +=head2 delete_holiday_range + + delete_holiday_range(day => $day, + month => $month, + year => $year); + +Delete a holiday range of dates for $self->{branchcode}. + +C<$day> Is the day month to make the date to delete. + +C<$month> Is month to make the date to delete. + +C<$year> Is year to make the date to delete. + +=cut + +sub delete_holiday_range { + my $self = shift; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)"); + $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); + +} + +=head2 delete_holiday_range_repeatable + + delete_holiday_range_repeatable(day => $day, + month => $month); + +Delete a holiday for $self->{branchcode}. + +C<$day> Is the day month to make the date to delete. + +C<$month> Is month to make the date to delete. + +=cut + +sub delete_holiday_range_repeatable { + my $self = shift; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)"); + $sth->execute($self->{branchcode}, $options{day}, $options{month}); +} + +=head2 delete_exception_holiday_range + + delete_exception_holiday_range(weekday => $weekday + day => $day, + month => $month, + year => $year); + +Delete a holiday for $self->{branchcode}. + +C<$day> Is the day month to make the date to delete. + +C<$month> Is month to make the date to delete. + +C<$year> Is year to make the date to delete. + +=cut + +sub delete_exception_holiday_range { + my $self = shift; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)"); + $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); + + # changed the 'single_holidays' table, lets force/reset its cache + my $cache = Koha::Caches->get_instance(); + my $key = $self->{branchcode} . "_holidays"; + $cache->clear_from_cache($key); +} =head2 isHoliday @@ -544,11 +651,10 @@ C<$year> Is the year to check whether if is a holiday or not. sub isHoliday { my ($self, $day, $month, $year) = @_; # FIXME - date strings are stored in non-padded metric format. should change to iso. - # FIXME - should change arguments to accept C4::Dates object $month=$month+0; $year=$year+0; $day=$day+0; - my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; + my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; my $weekDays = $self->get_week_days_holidays(); my $dayMonths = $self->get_day_month_holidays(); my $exceptions = $self->get_exception_holidays(); @@ -559,7 +665,7 @@ sub isHoliday { if ((exists($weekDays->{$weekday})) || (exists($dayMonths->{"$month/$day"})) || (exists($singles->{"$year/$month/$day"}))) { - return 1; + return 1; } else { return 0; } @@ -567,72 +673,55 @@ sub isHoliday { } -=head2 addDate +=head2 copy_to_branch - my ($day, $month, $year) = $calendar->addDate($date, $offset) + $calendar->copy_to_branch($target_branch) -C<$date> is a C4::Dates object representing the starting date of the interval. +=cut -C<$offset> Is the number of days that this function has to count from $date. +sub copy_to_branch { + my ($self, $target_branch) = @_; -=cut + croak "No target_branch" unless $target_branch; -sub addDate { - my ($self, $startdate, $offset) = @_; - my ($year,$month,$day) = split("-",$startdate->output('iso')); - my $daystep = 1; - if ($offset < 0) { # In case $offset is negative - # $offset = $offset*(-1); - $daystep = -1; - } - my $daysMode = C4::Context->preference('useDaysMode'); - if ($daysMode eq 'Datedue') { - ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); - while ($self->isHoliday($day, $month, $year)) { - ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); - } - } elsif($daysMode eq 'Calendar') { - while ($offset != 0) { - ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); - if (!($self->isHoliday($day, $month, $year))) { - $offset = $offset - $daystep; - } - } - } else { ## ($daysMode eq 'Days') - ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); - } - return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso')); -} + my $target_calendar = C4::Calendar->new(branchcode => $target_branch); -=head2 daysBetween + my ($y, $m, $d) = Today(); + my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; - my $daysBetween = $calendar->daysBetween($startdate, $enddate) + my $wdh = $self->get_week_days_holidays; + my $target_wdh = $target_calendar->get_week_days_holidays; + foreach my $key (keys %$wdh) { + unless (grep { $_ eq $key } keys %$target_wdh) { + $target_calendar->insert_week_day_holiday( weekday => $key, %{ $wdh->{$key} } ) + } + } -C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. + my $dmh = $self->get_day_month_holidays; + my $target_dmh = $target_calendar->get_day_month_holidays; + foreach my $values (values %$dmh) { + unless (grep { $_->{day} eq $values->{day} && $_->{month} eq $values->{month} } values %$target_dmh) { + $target_calendar->insert_day_month_holiday(%{ $values }); + } + } -Returns the number of non-holiday days in the interval. -useDaysMode syspref has no effect here. -=cut + my $exception_holidays = $self->get_exception_holidays; + my $target_exceptions = $target_calendar->get_exception_holidays; + foreach my $values ( grep {$_->{date} gt $today} values %{ $exception_holidays }) { + unless ( grep { $_->{date} eq $values->{date} } values %$target_exceptions) { + $target_calendar->insert_exception_holiday(%{ $values }); + } + } -sub daysBetween ($$$) { - my $self = shift or return undef; - my $startdate = shift or return undef; - my $enddate = shift or return undef; - my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); - my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); - if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { - return 0; - # we don't go backwards ( FIXME - handle this error better ) - } - my $count = 0; - while (1) { - ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day - unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) { - $count++; + my $single_holidays = $self->get_single_holidays; + my $target_singles = $target_calendar->get_single_holidays; + foreach my $values ( grep {$_->{date} gt $today} values %{ $single_holidays }) { + unless ( grep { $_->{date} eq $values->{date} } values %$target_singles){ + $target_calendar->insert_single_holiday(%{ $values }); } - ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); } - return($count); + + return 1; } 1; @@ -644,3 +733,4 @@ __END__ Koha Physics Library UNLP =cut +