From c80c96d298b2c34d1f4fbbbecdcecb46d77547d4 Mon Sep 17 00:00:00 2001 From: Srdjan Jankovic Date: Wed, 1 Feb 2012 12:45:15 +1300 Subject: [PATCH] bug_7477: Copy holidays from one branch to another --- C4/Calendar.pm | 98 ++++++++++++++------ .../prog/en/modules/tools/holidays.tt | 15 +++ tools/copy-holidays.pl | 40 ++++++++ 3 files changed, 125 insertions(+), 28 deletions(-) create mode 100755 tools/copy-holidays.pl diff --git a/C4/Calendar.pm b/C4/Calendar.pm index dc9037b..741e309 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -20,34 +20,11 @@ use warnings; use vars qw($VERSION @EXPORT); use Carp; -use Date::Calc qw( Date_to_Days ); +use Date::Calc qw( Date_to_Days Today); use C4::Context; -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 C4::Calendar::Calendar - Koha module dealing with holidays. @@ -123,7 +100,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 +110,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; @@ -283,6 +260,9 @@ 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 ('', ?,?,?,?,?,?,?)"); @@ -318,6 +298,9 @@ 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 ('', ?,?,?,?,?,?,?)"); @@ -567,6 +550,65 @@ sub isHoliday { } +=head2 copy_to_branch + + $calendar->copy_to_branch($target_branch) + +=cut + +sub copy_to_branch { + my ($self, $target_branch) = @_; + + croak "No target_branch" unless $target_branch; + + my $target_calendar = C4::Calendar->new(branchcode => $target_branch); + + my ($y, $m, $d) = Today(); + my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; + + $target_calendar->insert_week_day_holiday(%$_) + foreach values %{ $self->get_week_days_holidays }; + $target_calendar->insert_day_month_holiday(%$_) + foreach values %{ $self->get_day_month_holidays }; + $target_calendar->insert_exception_holiday(%$_) + foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; + $target_calendar->insert_single_holiday(%$_) + foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; + + return 1; +} + +=head2 daysBetween + + my $daysBetween = $calendar->daysBetween($startdate, $enddate) + +C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. + +Returns the number of non-holiday days in the interval. +useDaysMode syspref has no effect here. +=cut + +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++; + } + ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); + } + return($count); +} + =head2 addDate my ($day, $month, $year) = $calendar->addDate($date, $offset) @@ -601,7 +643,7 @@ sub addDate { } 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')); + return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); } =head2 daysBetween diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/holidays.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/holidays.tt index 686e3af..6ee87df 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/holidays.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/holidays.tt @@ -388,6 +388,21 @@ + +
+
+ + + + +
+
+
diff --git a/tools/copy-holidays.pl b/tools/copy-holidays.pl new file mode 100755 index 0000000..9de4b8c --- /dev/null +++ b/tools/copy-holidays.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +# Copyright 2012 Catalyst IT +# +# 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 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use warnings; + +use CGI; + +use C4::Auth; +use C4::Output; + + +use C4::Calendar; + +my $input = new CGI; +my $dbh = C4::Context->dbh(); + +my $branchcode = $input->param('branchcode'); +my $from_branchcode = $input->param('from_branchcode'); + +C4::Calendar->new(branchcode => $from_branchcode)->copy_to_branch($branchcode) if $from_branchcode && $branchcode; + +print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=".($branchcode || $from_branchcode)); + -- 1.6.5