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

(-)a/C4/Circulation.pm (-3 / +62 lines)
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
(-)a/t/db_dependent/Circulation/CalcDateDue.t (-3 / +81 lines)
Lines 2-15 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::More tests => 19;
5
use Test::More tests => 23;
6
use Test::MockModule;
6
use Test::MockModule;
7
use DBI;
7
use DBI;
8
use DateTime;
8
use DateTime;
9
use t::lib::Mocks;
9
use t::lib::Mocks;
10
use t::lib::TestBuilder;
10
use t::lib::TestBuilder;
11
use C4::Calendar qw( new insert_single_holiday delete_holiday insert_week_day_holiday );
11
use C4::Calendar qw( new insert_single_holiday delete_holiday insert_week_day_holiday );
12
12
use Koha::DateUtils qw( dt_from_string );
13
use Koha::Library::Hour;
13
use Koha::CirculationRules;
14
use Koha::CirculationRules;
14
15
15
use_ok('C4::Circulation', qw( CalcDateDue ));
16
use_ok('C4::Circulation', qw( CalcDateDue ));
Lines 340-344 my $renewed_date = $start_date->clone->add( days => 7 ); Link Here
340
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
341
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
341
is( $date->ymd, $renewed_date->ymd, 'Renewal period of "" should trigger fallover to issuelength for renewal' );
342
is( $date->ymd, $renewed_date->ymd, 'Renewal period of "" should trigger fallover to issuelength for renewal' );
342
343
344
# Testing hourly loans consider library open hours
345
346
my $library1 = $builder->build( { source => 'Branch' } );
347
Koha::CirculationRules->set_rules(
348
    {
349
        categorycode => $categorycode,
350
        itemtype     => $itemtype,
351
        branchcode   => $library1->{branchcode},
352
        rules        => {
353
            issuelength   => 3, # loan period is 3 hours
354
            lengthunit    => 'hours',
355
        }
356
    }
357
);
358
359
my $open = DateTime->now->subtract( hours => 4 )->hms;
360
my $close = DateTime->now->add( hours => 2 )->hms;
361
my $now = DateTime->now;
362
363
foreach (0..6) {
364
    # library opened 4 hours ago and closes in 2 hours.
365
    Koha::Library::Hour->new({ day => $_, branchcode => $library1->{branchcode}, open_time => $open, close_time => $close })->store;
366
}
367
368
# ignore calendar
369
t::lib::Mocks::mock_preference('useDaysMode', 'Days');
370
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'close');
371
# shorten loan period
372
373
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
374
my $expected_duetime = DateTime->now->add( hours => 2 );
375
is( $date, $expected_duetime, "Loan period was shortened because ConsiderLibraryHoursWhenIssuing is set to close time" );
376
377
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'open');
378
# extend loan period
379
380
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
381
$expected_duetime = DateTime->now->add( days => 1 )->subtract( hours => 4 );
382
is( $date, $expected_duetime, "Loan period was extended because ConsiderLibraryHoursWhenIssuing is set to open time" );
383
384
my $holiday_tomorrow = DateTime->now->add( days => 1 );
385
386
# consider calendar
387
my $library1_calendar = C4::Calendar->new( branchcode => $library1->{branchcode} );
388
$library1_calendar->insert_single_holiday(
389
    day             => $holiday_tomorrow->day,
390
    month           => $holiday_tomorrow->month,
391
    year            => $holiday_tomorrow->year,
392
    title           => 'testholiday',
393
    description     => 'testholiday'
394
);
395
Koha::CirculationRules->set_rules(
396
    {
397
        categorycode => $categorycode,
398
        itemtype     => $itemtype,
399
        branchcode   => $library1->{branchcode},
400
        rules        => {
401
            issuelength   => 13, # loan period must cross over into tomorrow
402
            lengthunit    => 'hours',
403
        }
404
    }
405
);
406
407
t::lib::Mocks::mock_preference('useDaysMode', 'Calendar');
408
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'close');
409
# shorten loan period
410
411
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
412
$expected_duetime = DateTime->now->add( days => 2, hours => 2 );
413
is( $date, $expected_duetime, "Loan period was shortened (but considers the holiday) because ConsiderLibraryHoursWhenIssuing is set to close time" );
414
415
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'open');
416
# extend loan period
417
418
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
419
$expected_duetime = DateTime->now->add( days => 2 )->subtract( hours => 4 );
420
is( $date, $expected_duetime, "Loan period was extended (but considers the holiday) because ConsiderLibraryHoursWhenIssuing is set to open time" );
421
343
$cache->clear_from_cache($key);
422
$cache->clear_from_cache($key);
344
$schema->storage->txn_rollback;
423
$schema->storage->txn_rollback;
345
- 

Return to bug 6796