X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=Koha%2FCalendar.pm;h=75c5c9ebb15c6516692a323f483134c30a4f2bf7;hb=72f91afd9c7871b706d553cfbadf23d4f6db7e02;hp=b44bdd6dbb0faf11fba2b5424dce5253e3d8b713;hpb=40b907bc0bc6db7500dde805ee188c0cf0ac6ae5;p=koha_gimpoz diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index b44bdd6dbb..75c5c9ebb1 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -18,7 +18,7 @@ sub new { my $o = lc $o_name; $self->{$o} = $options{$o_name}; } - if (exists $options{TEST_MODE}) { + if ( exists $options{TEST_MODE} ) { $self->_mockinit(); return $self; } @@ -37,15 +37,16 @@ sub _init { 'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?' ); $repeat_sth->execute( $branch, 0 ); - $self->{weekly_closed_days} = [0,0,0,0,0,0,0]; + $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; Readonly::Scalar my $sunday => 7; while ( my $tuple = $repeat_sth->fetchrow_hashref ) { - $self->{weekly_closed_days}->[$tuple->{weekday }] = 1; + $self->{weekly_closed_days}->[ $tuple->{weekday} ] = 1; } $repeat_sth->execute( $branch, 1 ); - $self->{day_month_closed_days} = []; + $self->{day_month_closed_days} = {}; while ( my $tuple = $repeat_sth->fetchrow_hashref ) { - $self->{day_month_closed_days}->{ $tuple->{day}}->{$tuple->{month} } = 1; + $self->{day_month_closed_days}->{ $tuple->{day} }->{ $tuple->{month} } = + 1; } my $special = $dbh->prepare( 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)' @@ -77,12 +78,18 @@ sub _init { )->truncate( to => 'day' ); } $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates ); + $self->{days_mode} = C4::Context->preference('useDaysMode'); return; } sub addDate { - my ( $self, $base_date, $add_duration, $unit ) = @_; - my $days_mode = C4::Context->preference('useDaysMode'); + my ( $self, $startdate, $add_duration, $unit ) = @_; + my $base_date = $startdate->clone(); + if ( ref $add_duration ne 'DateTime::Duration' ) { + $add_duration = DateTime::Duration->new( days => $add_duration ); + } + $unit ||= q{}; # default days ? + my $days_mode = $self->{days_mode}; Readonly::Scalar my $return_by_hour => 10; my $day_dur = DateTime::Duration->new( days => 1 ); if ( $add_duration->is_negative() ) { @@ -109,7 +116,7 @@ sub addDate { } } else { - my $days = $add_duration->in_units('days'); + my $days = abs $add_duration->in_units('days'); while ($days) { $base_date->add_duration($day_dur); if ( $self->is_holiday($base_date) ) { @@ -134,16 +141,16 @@ sub addDate { sub is_holiday { my ( $self, $dt ) = @_; my $dow = $dt->day_of_week; - if ($dow == 7) { + if ( $dow == 7 ) { $dow = 0; } - if ($self->{weekly_closed_days}->[$dow] == 1) { + if ( $self->{weekly_closed_days}->[$dow] == 1 ) { return 1; } $dt->truncate( to => 'days' ); my $day = $dt->day; my $month = $dt->month; - if ( $self->{day_month_closed_days}->{$month}->{$day} == 1 ) { + if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) { return 1; } if ( $self->{exception_holidays}->contains($dt) ) { @@ -161,11 +168,9 @@ sub days_between { my $self = shift; my $start_dt = shift; my $end_dt = shift; - $start_dt->truncate( to => 'hours' ); - $end_dt->truncate( to => 'hours' ); # start and end should not be closed days - my $duration = $end_dt - $start_dt; + my $duration = $end_dt->delta_days($start_dt); $start_dt->truncate( to => 'days' ); $end_dt->truncate( to => 'days' ); while ( DateTime->compare( $start_dt, $end_dt ) == -1 ) { @@ -178,24 +183,41 @@ sub days_between { } -sub _mockinit { - my $self = shift; - $self->{weekly_closed_days} = [1,0,0,0,0,0,0]; # Sunday only - $self->{day_month_closed_days} = { - 6 => { - 16 => 1, +sub hours_between { + my ($self, $start_dt, $end_dt) = @_; + my $duration = $end_dt->delta_ms($start_dt); + $start_dt->truncate( to => 'days' ); + $end_dt->truncate( to => 'days' ); + # NB this is a kludge in that it assumes all days are 24 hours + # However for hourly loans the logic should be expanded to + # take into account open/close times then it would be a duration + # of library open hours + while ( DateTime->compare( $start_dt, $end_dt ) == -1 ) { + $start_dt->add( days => 1 ); + if ( $self->is_holiday($start_dt) ) { + $duration->subtract( hours => 24 ); } - }; + } + return $duration; + +} + +sub _mockinit { + my $self = shift; + $self->{weekly_closed_days} = [ 1, 0, 0, 0, 0, 0, 0 ]; # Sunday only + $self->{day_month_closed_days} = { 6 => { 16 => 1, } }; my $dates = []; - $self->{exception_holidays} = DateTime::Set->from_datetimes( dates => $dates ); + $self->{exception_holidays} = + DateTime::Set->from_datetimes( dates => $dates ); my $special = DateTime->new( - year => 2011, - month => 6, - day => 1, + year => 2011, + month => 6, + day => 1, time_zone => 'Europe/London', ); push @{$dates}, $special; $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates ); + $self->{days_mode} = 'Calendar'; return; }