View | Details | Raw Unified | Return to bug 7477
Collapse All | Expand All

(-)a/C4/Calendar.pm (-28 / +70 lines)
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
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/holidays.tt (+15 lines)
Lines 388-393 Link Here
388
388
389
389
390
</div>
390
</div>
391
392
<div style="margin-top: 2em;">
393
<form action="copy-holidays.pl" method="post">
394
	<input type="hidden" name="from_branchcode" value="[% branch %]" /> 
395
  <label for="branchcode">Copy holidays to:</label>
396
  <select id="branchcode" name="branchcode">
397
    <option value=""></option>
398
    [% FOREACH branchloo IN branchloop %]
399
    <option value="[% branchloo.value %]">[% branchloo.branchname %]</option>
400
    [% END %]
401
  </select>
402
	<input type="submit" value="Copy" />
403
</form>
404
</div>
405
	
391
</div>
406
</div>
392
<div class="yui-u">
407
<div class="yui-u">
393
<div class="help">
408
<div class="help">
(-)a/tools/copy-holidays.pl (-1 / +40 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2012 Catalyst IT
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use strict;
21
use warnings;
22
23
use CGI;
24
25
use C4::Auth;
26
use C4::Output;
27
28
29
use C4::Calendar;
30
31
my $input               = new CGI;
32
my $dbh                 = C4::Context->dbh();
33
34
my $branchcode          = $input->param('branchcode');
35
my $from_branchcode     = $input->param('from_branchcode');
36
37
C4::Calendar->new(branchcode => $from_branchcode)->copy_to_branch($branchcode) if $from_branchcode && $branchcode;
38
39
print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=".($branchcode || $from_branchcode));
40

Return to bug 7477