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

(-)a/Koha/Calendar.pm (+67 lines)
Lines 7-12 use DateTime; Link Here
7
use DateTime::Set;
7
use DateTime::Set;
8
use DateTime::Duration;
8
use DateTime::Duration;
9
use C4::Context;
9
use C4::Context;
10
use C4::Calendar;
10
use Koha::Caches;
11
use Koha::Caches;
11
use Carp;
12
use Carp;
12
13
Lines 368-373 sub clear_weekly_closed_days { Link Here
368
    return;
369
    return;
369
}
370
}
370
371
372
373
sub add_holiday {
374
    my ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list) = @_;  
375
    my $calendar = C4::Calendar->new(branchcode => $branchcode);
376
377
    if ($newoperation eq 'weekday') {
378
        unless ( $weekday && ($weekday ne '') ) { 
379
            # was dow calculated by javascript?  original code implies it was supposed to be.
380
            # if not, we need it.
381
            $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
382
        }
383
        unless($calendar->isHoliday($day, $month, $year)) {
384
        $calendar->insert_week_day_holiday(weekday => $weekday,
385
                                           title => $title,
386
                                           description => $description);
387
        }
388
    }elsif ($newoperation eq 'repeatable') {
389
        unless($calendar->isHoliday($day, $month, $year)) {
390
            $calendar->insert_day_month_holiday(day => $day,
391
                                                month => $month, 
392
                                                title => $title,
393
                                                description => $description);
394
        }
395
    } elsif ($newoperation eq 'holiday') {
396
        unless($calendar->isHoliday($day, $month, $year)) {
397
            $calendar->insert_single_holiday(
398
                day => $day,
399
                month => $month,
400
                year => $year,
401
                title => $title,
402
                description => $description);
403
        }
404
405
    } elsif ( $newoperation eq 'holidayrange' ) {
406
        if (@holiday_list){
407
            foreach my $date (@holiday_list){
408
                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
409
                    $calendar->insert_single_holiday(
410
                            day         => $date->{local_c}->{day},
411
                            month       => $date->{local_c}->{month},
412
                            year        => $date->{local_c}->{year},
413
                            title       => $title,
414
                            description => $description
415
                    );
416
                 }
417
            }
418
        }
419
    } elsif ( $newoperation eq 'holidayrangerepeat' ) {
420
        if (@holiday_list){
421
            foreach my $date (@holiday_list){
422
                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
423
                    $calendar->insert_day_month_holiday(
424
                        day         => $date->{local_c}->{day},
425
                        month       => $date->{local_c}->{month},
426
                        title       => $title,
427
                        description => $description
428
                    );
429
                }
430
            }
431
        }
432
    }
433
    # we updated the single_holidays table, so wipe its cache
434
    my $cache = Koha::Caches->get_instance();
435
    $cache->clear_from_cache( 'single_holidays') ;
436
}
437
371
1;
438
1;
372
__END__
439
__END__
373
440
(-)a/t/db_dependent/Circulation/dateexpiry.t (-1 / +25 lines)
Lines 23-28 use Test::More tests => 2; Link Here
23
use C4::Members;
23
use C4::Members;
24
use Koha::DateUtils;
24
use Koha::DateUtils;
25
use Koha::Database;
25
use Koha::Database;
26
use Koha::Calendar;
26
27
27
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
28
use t::lib::Mocks qw( mock_preference );
29
use t::lib::Mocks qw( mock_preference );
Lines 39-45 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub { Link Here
39
    can_book_be_issued();
40
    can_book_be_issued();
40
};
41
};
41
subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
42
subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
42
    plan tests => 4;
43
    plan tests => 5;
43
    calc_date_due();
44
    calc_date_due();
44
};
45
};
45
46
Lines 82-87 sub can_book_be_issued { Link Here
82
83
83
sub calc_date_due {
84
sub calc_date_due {
84
    t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
85
    t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
86
    t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' );
85
87
86
    # this triggers the compare between expiry and due date
88
    # this triggers the compare between expiry and due date
87
89
Lines 116-121 sub calc_date_due { Link Here
116
    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
118
    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
117
    my $t2 = time;
119
    my $t2 = time;
118
    is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
120
    is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
121
     
122
    # fifth test takes account of closed days
123
    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
124
    t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' );
125
    my $calendar = C4::Calendar->new(branchcode => $branch->{branchcode});
126
    Koha::Calendar::add_holiday('holiday', 
127
                            $branch->{branchcode}, 
128
                            $d->day_of_week(),
129
                            $d->day(),
130
                            $d->month(),
131
                            $d->year(),
132
                            'holidayTest',
133
                            'holidayDesc'
134
    );
135
    $calendar->delete_holiday(weekday => $d->day_of_week() - 1, day => $d->day()-1, month =>$d->month(), year=>$d->year() );
136
    $d2 = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
137
    $d2->add(days => 1);
138
    $d->truncate( to => 'day' );
139
    $d2->truncate( to => 'day' );
140
    warn Data::Dumper::Dumper($d->datetime());
141
    warn Data::Dumper::Dumper($d2->datetime());
142
    is ( DateTime->compare( $d, $d2) == 0, 1, "no problem with closed days");
119
}
143
}
120
144
121
$schema->storage->txn_rollback;
145
$schema->storage->txn_rollback;
(-)a/tools/newHolidays.pl (-77 / +12 lines)
Lines 15-34 use Koha::Caches; Link Here
15
use C4::Calendar;
15
use C4::Calendar;
16
use DateTime;
16
use DateTime;
17
use Koha::DateUtils;
17
use Koha::DateUtils;
18
use Koha::Calendar;
18
19
19
my $input               = new CGI;
20
my $input               = new CGI;
20
my $dbh                 = C4::Context->dbh();
21
my $dbh                 = C4::Context->dbh();
21
22
22
our $branchcode          = $input->param('newBranchName');
23
my $branchcode          = $input->param('newBranchName');
23
my $originalbranchcode  = $branchcode;
24
my $originalbranchcode  = $branchcode;
24
our $weekday             = $input->param('newWeekday');
25
my $weekday             = $input->param('newWeekday');
25
our $day                 = $input->param('newDay');
26
my $day                 = $input->param('newDay');
26
our $month               = $input->param('newMonth');
27
my $month               = $input->param('newMonth');
27
our $year                = $input->param('newYear');
28
my $year                = $input->param('newYear');
28
my $dateofrange         = $input->param('dateofrange');
29
my $dateofrange         = $input->param('dateofrange');
29
our $title               = $input->param('newTitle');
30
my $title               = $input->param('newTitle');
30
our $description         = $input->param('newDescription');
31
my $description         = $input->param('newDescription');
31
our $newoperation        = $input->param('newOperation');
32
my $newoperation        = $input->param('newOperation');
32
my $allbranches         = $input->param('allBranches');
33
my $allbranches         = $input->param('allBranches');
33
34
34
35
Lines 46-52 if ($description) { Link Here
46
}
47
}
47
48
48
# We make an array with holiday's days
49
# We make an array with holiday's days
49
our @holiday_list;
50
my @holiday_list;
50
if ($end_dt){
51
if ($end_dt){
51
    for (my $dt = $first_dt->clone();
52
    for (my $dt = $first_dt->clone();
52
    $dt <= $end_dt;
53
    $dt <= $end_dt;
Lines 59-133 if ($end_dt){ Link Here
59
if($allbranches) {
60
if($allbranches) {
60
    my $libraries = Koha::Libraries->search;
61
    my $libraries = Koha::Libraries->search;
61
    while ( my $library = $libraries->next ) {
62
    while ( my $library = $libraries->next ) {
62
        add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description);
63
        Koha::Calendar::add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list);
63
    }
64
    }
64
} else {
65
} else {
65
    add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
66
    Koha::Calendar::add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list);
66
}
67
}
67
68
68
print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
69
print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
69
70
#FIXME: move add_holiday() to a better place
71
sub add_holiday {
72
	($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
73
	my $calendar = C4::Calendar->new(branchcode => $branchcode);
74
75
	if ($newoperation eq 'weekday') {
76
		unless ( $weekday && ($weekday ne '') ) { 
77
			# was dow calculated by javascript?  original code implies it was supposed to be.
78
			# if not, we need it.
79
			$weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
80
		}
81
		unless($calendar->isHoliday($day, $month, $year)) {
82
			$calendar->insert_week_day_holiday(weekday => $weekday,
83
							           title => $title,
84
							           description => $description);
85
		}
86
	} elsif ($newoperation eq 'repeatable') {
87
		unless($calendar->isHoliday($day, $month, $year)) {
88
			$calendar->insert_day_month_holiday(day => $day,
89
	                                    month => $month,
90
							            title => $title,
91
							            description => $description);
92
		}
93
	} elsif ($newoperation eq 'holiday') {
94
		unless($calendar->isHoliday($day, $month, $year)) {
95
			$calendar->insert_single_holiday(day => $day,
96
	                                 month => $month,
97
						             year => $year,
98
						             title => $title,
99
						             description => $description);
100
		}
101
102
	} elsif ( $newoperation eq 'holidayrange' ) {
103
        if (@holiday_list){
104
            foreach my $date (@holiday_list){
105
                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
106
                    $calendar->insert_single_holiday(
107
                        day         => $date->{local_c}->{day},
108
                        month       => $date->{local_c}->{month},
109
                        year        => $date->{local_c}->{year},
110
                        title       => $title,
111
                        description => $description
112
                    );
113
                }
114
            }
115
        }
116
    } elsif ( $newoperation eq 'holidayrangerepeat' ) {
117
        if (@holiday_list){
118
            foreach my $date (@holiday_list){
119
                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
120
                    $calendar->insert_day_month_holiday(
121
                        day         => $date->{local_c}->{day},
122
                        month       => $date->{local_c}->{month},
123
                        title       => $title,
124
                        description => $description
125
                    );
126
                }
127
            }
128
        }
129
    }
130
    # we updated the single_holidays table, so wipe its cache
131
    my $cache = Koha::Caches->get_instance();
132
    $cache->clear_from_cache( 'single_holidays') ;
133
}
134
- 

Return to bug 11580