From eb5e954eaee67eed131d22f5fc18a0c2efa6b414 Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Tue, 10 Oct 2017 11:15:59 -0400 Subject: [PATCH] Bug 17983 - Add Minutes math to DiscreteCalendar --- Koha/DiscreteCalendar.pm | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/Koha/DiscreteCalendar.pm b/Koha/DiscreteCalendar.pm index 8718137..5351847 100644 --- a/Koha/DiscreteCalendar.pm +++ b/Koha/DiscreteCalendar.pm @@ -1145,6 +1145,73 @@ sub open_hours_between { return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours')); } +=head2 open_minutes_between + + $minutes = $calendar->open_minutes_between($start_date, $end_date) + +Returns the number of minutes between C<$start_date> and C<$end_date>, taking into +account the opening hours of the library. + +=cut + +sub open_minutes_between { + my ($self, $start_date, $end_date) = @_; + my $branchcode = $self->{branchcode}; + my $schema = Koha::Database->new->schema; + my $dtf = $schema->storage->datetime_parser; + $start_date = $dtf->format_datetime($start_date); + $end_date = $dtf->format_datetime($end_date); + + my $working_minutes_between = $schema->resultset('DiscreteCalendar')->search( + { + branchcode => $branchcode, + isopened => 1, + }, + { + select => \['sum(time_to_sec(timediff(closehour, openhour)) / 60)'], + as => [qw /minutes_between/], + where => \['date BETWEEN DATE(?) AND DATE(?)', $start_date, $end_date] + } + ); + + my $loan_day = $schema->resultset('DiscreteCalendar')->search( + { + branchcode => $branchcode, + }, + { + where => \['date = DATE(?)', $start_date], + } + ); + + my $return_day = $schema->resultset('DiscreteCalendar')->search( + { + branchcode => $branchcode, + }, + { + where => \['date = DATE(?)', $end_date], + } + ); + + #Capture the time portion of the date + $start_date =~ /\s(.*)/; + my $loan_date_time = $1; + $end_date =~ /\s(.*)/; + my $return_date_time = $1; + + my $not_used_minutes = $schema->resultset('DiscreteCalendar')->search( + { + branchcode => $branchcode, + isopened => 1, + }, + { + select => \[ '(time_to_sec(timediff(?, ?)) + time_to_sec(timediff(?, ?)) ) / 60', $return_day->next()->closehour(), $return_date_time, $loan_date_time, $loan_day->next()->openhour()], + as => [qw /not_used_minutes/], + } + ); + + return DateTime::Duration->new( minutes => $working_minutes_between->next()->get_column('minutes_between') - $not_used_minutes->next()->get_column('not_used_minutes')); +} + =head2 addDate my $dt = $calendar->addDate($date, $dur, $unit) @@ -1171,6 +1238,8 @@ sub addDate { my $return_by_hour = 10; $dt = $self->addHours($startdate, $add_duration, $return_by_hour); + } elsif($unit eq 'minutes') { + $dt = $self->addMinutes($startdate, $add_duration); } else { # days $dt = $self->addDays($startdate, $add_duration); @@ -1213,6 +1282,31 @@ sub addHours { return $base_date; } +=head2 addMinutes + + $end = $calendar->addMinutes($date, $dur) + +Add C<$dur> minutes to C<$start> date. + +=cut + +sub addMinutes { + my ( $self, $startdate, $minutes_duration) = @_; + my $base_date = $startdate->clone(); + + $base_date->add_duration($minutes_duration); + + my $dateInfo = $self->get_date_info($base_date); + + my $close_datetime = dt_from_string($dateInfo->{date} ." ". $dateInfo->{closehour}, 'iso'); + + if(DateTime->compare($base_date, $close_datetime) > 0) { + return $close_datetime; + } + + return $base_date; +} + =head2 addDays $date = $calendar->addDays($start, $duration) -- 2.7.4