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

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