@@ -, +, @@ --- Koha/DiscreteCalendar.pm | 81 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) --- a/Koha/DiscreteCalendar.pm +++ a/Koha/DiscreteCalendar.pm @@ -875,6 +875,65 @@ sub open_hours_between { return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours')); } + +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')); +} + sub addDate { my ( $self, $startdate, $add_duration, $unit ) = @_; @@ -889,9 +948,10 @@ sub addDate { if ( $unit eq 'hours' ) { # Fixed for legacy support. Should be set as a branch parameter my $return_by_hour = 10; - $dt = $self->addHours($startdate, $add_duration, $return_by_hour); - } else { + } elsif($unit eq 'minutes'){ + $dt = $self->addMinutes($startdate, $add_duration); + }else { # days $dt = $self->addDays($startdate, $add_duration); } @@ -899,6 +959,23 @@ sub addDate { return $dt; } +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; +} + sub addHours { my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_; my $base_date = $startdate->clone(); --