| 
      
            Lines 63-68
          use Koha::SearchEngine::Indexer;
      
      
        Link Here
      
     | 
  
        
          | 63 | 
          use Koha::Exceptions::Checkout;  | 
          63 | 
          use Koha::Exceptions::Checkout;  | 
        
        
          | 64 | 
          use Koha::Plugins;  | 
          64 | 
          use Koha::Plugins;  | 
        
        
          | 65 | 
          use Koha::Recalls;  | 
          65 | 
          use Koha::Recalls;  | 
        
            
               | 
               | 
              66 | 
              use Koha::Library::Hours;  | 
            
        
          | 66 | 
          use Carp qw( carp );  | 
          67 | 
          use Carp qw( carp );  | 
        
        
          | 67 | 
          use List::MoreUtils qw( any );  | 
          68 | 
          use List::MoreUtils qw( any );  | 
        
        
          | 68 | 
          use Scalar::Util qw( looks_like_number );  | 
          69 | 
          use Scalar::Util qw( looks_like_number );  | 
        
  
    | 
      
            Lines 3720-3730
          sub CalcDateDue {
      
      
        Link Here
      
     | 
  
        
          | 3720 | 
                  }  | 
          3721 | 
                  }  | 
        
        
          | 3721 | 
              );  | 
          3722 | 
              );  | 
        
        
          | 3722 | 
           | 
          3723 | 
           | 
        
            
               | 
               | 
              3724 | 
                  my $considerlibraryhours = C4::Context->preference('ConsiderLibraryHoursWhenIssuing'); | 
            
            
              | 3725 | 
                  # starter vars so don't do calculations directly to $datedue  | 
            
            
              | 3726 | 
                  my $potential_datedue = $datedue->clone;  | 
            
            
              | 3727 | 
                  my $library_close = $datedue->clone;  | 
            
            
              | 3728 | 
                  my $dayofweek = $datedue->local_day_of_week - 1;  | 
            
            
              | 3729 | 
                  my $todayhours = Koha::Library::Hours->find({ branchcode => $branch, day => $dayofweek }); | 
            
            
              | 3730 | 
                  my @close = undef;  | 
            
            
              | 3731 | 
                  my $tomorrowhours = Koha::Library::Hours->find({ branchcode => $branch, day => $dayofweek+1 }); # get open hours of next day | 
            
            
              | 3732 | 
                  my @open = undef;  | 
            
            
              | 3733 | 
                  if ( $todayhours->close_time and $tomorrowhours->open_time ) { | 
            
            
              | 3734 | 
                      @close = split( ":", $todayhours->close_time );  | 
            
            
              | 3735 | 
                      $library_close = $library_close->set( hour => $close[0], minute => $close[1] );  | 
            
            
              | 3736 | 
                      $potential_datedue = $potential_datedue->add( hours => $loanlength->{$length_key} ); # datedue without consideration for open hours | 
            
            
              | 3737 | 
                      @open = split( ":", $tomorrowhours->open_time );  | 
            
            
              | 3738 | 
                  }  | 
            
            
              | 3739 | 
               | 
            
        
          | 3723 | 
              # calculate the datedue as normal  | 
          3740 | 
              # calculate the datedue as normal  | 
        
        
          | 3724 | 
              if ( $daysmode eq 'Days' )  | 
          3741 | 
              if ( $daysmode eq 'Days' )  | 
        
        
          | 3725 | 
              {    # ignoring calendar | 
          3742 | 
              {    # ignoring calendar | 
        
        
          | 3726 | 
                  if ( $loanlength->{lengthunit} eq 'hours' ) { | 
          3743 | 
                  if ( $loanlength->{lengthunit} eq 'hours' ) { | 
        
          
            
              | 3727 | 
                          $datedue->add( hours => $loanlength->{$length_key} ); | 
              3744 | 
                          if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) { | 
            
            
               | 
               | 
              3745 | 
                              if ( $considerlibraryhours eq 'close' ) { | 
            
            
              | 3746 | 
                                  # datedue will be after the library closes on that day  | 
            
            
              | 3747 | 
                                  # shorten loan period to end when library closes  | 
            
            
              | 3748 | 
                                  $datedue->set( hour => $close[0], minute => $close[1] );  | 
            
            
              | 3749 | 
                              } elsif ( $considerlibraryhours eq 'open' ) { | 
            
            
              | 3750 | 
                                  # datedue will be after the library closes on that day  | 
            
            
              | 3751 | 
                                  # extend loan period to when library opens following day  | 
            
            
              | 3752 | 
                                  $datedue->add( days => 1 )->set( hour => $open[0], minute => $open[1] );  | 
            
            
              | 3753 | 
                              } else { | 
            
            
              | 3754 | 
                                  # ignore library open hours  | 
            
            
              | 3755 | 
                                  $datedue->add( hours => $loanlength->{$length_key} ); | 
            
            
              | 3756 | 
                              }  | 
            
            
              | 3757 | 
                          } else { | 
            
            
              | 3758 | 
                              # due time doesn't conflict with library open hours, don't need to check  | 
            
            
              | 3759 | 
                              $datedue->add( hours => $loanlength->{$length_key} ); | 
            
            
              | 3760 | 
                          }  | 
            
        
          | 3728 | 
                  } else {    # days | 
          3761 | 
                  } else {    # days | 
        
        
          | 3729 | 
                      $datedue->add( days => $loanlength->{$length_key} ); | 
          3762 | 
                      $datedue->add( days => $loanlength->{$length_key} ); | 
        
        
          | 3730 | 
                      $datedue->set_hour(23);  | 
          3763 | 
                      $datedue->set_hour(23);  | 
        
  
    | 
      
            Lines 3732-3748
          sub CalcDateDue {
      
      
        Link Here
      
     | 
  
        
          | 3732 | 
                  }  | 
          3765 | 
                  }  | 
        
        
          | 3733 | 
              } else { | 
          3766 | 
              } else { | 
        
        
          | 3734 | 
                  my $dur;  | 
          3767 | 
                  my $dur;  | 
        
            
               | 
               | 
              3768 | 
                      my $sethours;  | 
            
        
          | 3735 | 
                  if ($loanlength->{lengthunit} eq 'hours') { | 
          3769 | 
                  if ($loanlength->{lengthunit} eq 'hours') { | 
        
          
            
              | 3736 | 
                          $dur = DateTime::Duration->new( hours => $loanlength->{$length_key}); | 
              3770 | 
                          if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) { | 
            
            
               | 
               | 
              3771 | 
                              if ( $considerlibraryhours eq 'close' ) { | 
            
            
              | 3772 | 
                                  # datedue will be after the library closes on that day  | 
            
            
              | 3773 | 
                                  # shorten loan period to end when library closes  | 
            
            
              | 3774 | 
                                  $dur = $potential_datedue->delta_ms( $library_close );  | 
            
            
              | 3775 | 
                                  $sethours = $considerlibraryhours;  | 
            
            
              | 3776 | 
                              } elsif ( $considerlibraryhours eq 'open' ) { | 
            
            
              | 3777 | 
                                  # datedue will be after the library closes on that day  | 
            
            
              | 3778 | 
                                  # extend loan period to when library opens following day  | 
            
            
              | 3779 | 
                                  my $library_open = $datedue->clone->set( hour => $open[0], minute => $open[1] );  | 
            
            
              | 3780 | 
                                  $dur = $potential_datedue->delta_ms( $library_open )->add( days => 1 );  | 
            
            
              | 3781 | 
                                  $sethours = $considerlibraryhours;  | 
            
            
              | 3782 | 
                              } else { | 
            
            
              | 3783 | 
                                  # ignore library open hours  | 
            
            
              | 3784 | 
                                  $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} ); | 
            
            
              | 3785 | 
                              }  | 
            
            
              | 3786 | 
                          } else { | 
            
            
              | 3787 | 
                              # due time doesn't conflict with library open hours, don't need to check  | 
            
            
              | 3788 | 
                              $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} ); | 
            
            
              | 3789 | 
                          }  | 
            
        
          | 3737 | 
                  }  | 
          3790 | 
                  }  | 
        
        
          | 3738 | 
                  else { # days | 
          3791 | 
                  else { # days | 
        
          
            
              | 3739 | 
                          $dur = DateTime::Duration->new( days => $loanlength->{$length_key}); | 
              3792 | 
                          $dur = DateTime::Duration->new( days => $loanlength->{$length_key} ); | 
            
        
          | 3740 | 
                  }  | 
          3793 | 
                  }  | 
        
        
          | 3741 | 
                  my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );  | 
          3794 | 
                  my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );  | 
        
        
          | 3742 | 
                  $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} ); | 
          3795 | 
                  $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} ); | 
        
        
          | 3743 | 
                  if ($loanlength->{lengthunit} eq 'days') { | 
          3796 | 
                  if ($loanlength->{lengthunit} eq 'days') { | 
        
        
          | 3744 | 
                      $datedue->set_hour(23);  | 
          3797 | 
                      $datedue->set_hour(23);  | 
        
        
          | 3745 | 
                      $datedue->set_minute(59);  | 
          3798 | 
                      $datedue->set_minute(59);  | 
        
            
               | 
               | 
              3799 | 
                      } else { | 
            
            
              | 3800 | 
                          if ( $sethours and $sethours eq 'close' ) { | 
            
            
              | 3801 | 
                              $datedue->set( hour => $close[0], minute => $close[1] );  | 
            
            
              | 3802 | 
                          } elsif ( $sethours and $sethours eq 'open' ) { | 
            
            
              | 3803 | 
                              $datedue->set( hour => $open[0], minute => $open[1] );  | 
            
            
              | 3804 | 
                          }  | 
            
        
          | 3746 | 
                  }  | 
          3805 | 
                  }  | 
        
        
          | 3747 | 
              }  | 
          3806 | 
              }  | 
        
        
          | 3748 | 
           | 
          3807 | 
           |