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

(-)a/C4/Calendar.pm (-31 / +77 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 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
(-)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