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

(-)a/C4/Circulation.pm (-3 / +63 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 blessed );
69
use Scalar::Util qw( looks_like_number blessed );
Lines 3891-3901 sub CalcDateDue { Link Here
3891
        }
3892
        }
3892
    );
3893
    );
3893
3894
3895
    my $considerlibraryhours = C4::Context->preference('ConsiderLibraryHoursInCirculation');
3896
3897
    # starter vars so don't do calculations directly to $datedue
3898
    my $potential_datedue = $datedue->clone;
3899
    my $library_close = $datedue->clone;
3900
    my $dayofweek = $datedue->local_day_of_week - 1;
3901
    my $todayhours = Koha::Library::Hours->find({ library_id => $branch, day => $dayofweek });
3902
    my @close = undef;
3903
    my $tomorrowhours = Koha::Library::Hours->find({ library_id => $branch, day => $dayofweek+1 }); # get open hours of next day
3904
    my @open = undef;
3905
    if ( $todayhours->close_time and $tomorrowhours->open_time ) {
3906
        @close = split( ":", $todayhours->close_time );
3907
        $library_close = $library_close->set( hour => $close[0], minute => $close[1] );
3908
        $potential_datedue = $potential_datedue->add( hours => $loanlength->{$length_key} ); # datedue without consideration for open hours
3909
        @open = split( ":", $tomorrowhours->open_time );
3910
    }
3911
3894
    # calculate the datedue as normal
3912
    # calculate the datedue as normal
3895
    if ( $daysmode eq 'Days' )
3913
    if ( $daysmode eq 'Days' )
3896
    {    # ignoring calendar
3914
    {    # ignoring calendar
3897
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3915
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3898
            $datedue->add( hours => $loanlength->{$length_key} );
3916
            if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) {
3917
                if ( $considerlibraryhours eq 'close' ) {
3918
                    # datedue will be after the library closes on that day
3919
                    # shorten loan period to end when library closes
3920
                    $datedue->set( hour => $close[0], minute => $close[1] );
3921
                } elsif ( $considerlibraryhours eq 'open' ) {
3922
                    # datedue will be after the library closes on that day
3923
                    # extend loan period to when library opens following day
3924
                    $datedue->add( days => 1 )->set( hour => $open[0], minute => $open[1] );
3925
                } else {
3926
                    # ignore library open hours
3927
                    $datedue->add( hours => $loanlength->{$length_key} );
3928
                }
3929
            } else {
3930
                # due time doesn't conflict with library open hours, don't need to check
3931
                $datedue->add( hours => $loanlength->{$length_key} );
3932
            }
3899
        } else {    # days
3933
        } else {    # days
3900
            $datedue->add( days => $loanlength->{$length_key} );
3934
            $datedue->add( days => $loanlength->{$length_key} );
3901
            $datedue->set_hour(23);
3935
            $datedue->set_hour(23);
Lines 3903-3919 sub CalcDateDue { Link Here
3903
        }
3937
        }
3904
    } else {
3938
    } else {
3905
        my $dur;
3939
        my $dur;
3940
        my $sethours;
3906
        if ($loanlength->{lengthunit} eq 'hours') {
3941
        if ($loanlength->{lengthunit} eq 'hours') {
3907
            $dur = DateTime::Duration->new( hours => $loanlength->{$length_key});
3942
            if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) {
3943
                if ( $considerlibraryhours eq 'close' ) {
3944
                    # datedue will be after the library closes on that day
3945
                    # shorten loan period to end when library closes
3946
                    $dur = $potential_datedue->delta_ms( $library_close );
3947
                    $sethours = $considerlibraryhours;
3948
                } elsif ( $considerlibraryhours eq 'open' ) {
3949
                    # datedue will be after the library closes on that day
3950
                    # extend loan period to when library opens following day
3951
                    my $library_open = $datedue->clone->set( hour => $open[0], minute => $open[1] );
3952
                    $dur = $potential_datedue->delta_ms( $library_open )->add( days => 1 );
3953
                    $sethours = $considerlibraryhours;
3954
                } else {
3955
                    # ignore library open hours
3956
                    $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} );
3957
                }
3958
            } else {
3959
                # due time doesn't conflict with library open hours, don't need to check
3960
                $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} );
3961
            }
3908
        }
3962
        }
3909
        else { # days
3963
        else { # days
3910
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key});
3964
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key} );
3911
        }
3965
        }
3912
        my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );
3966
        my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );
3913
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3967
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3914
        if ($loanlength->{lengthunit} eq 'days') {
3968
        if ($loanlength->{lengthunit} eq 'days') {
3915
            $datedue->set_hour(23);
3969
            $datedue->set_hour(23);
3916
            $datedue->set_minute(59);
3970
            $datedue->set_minute(59);
3971
        } else {
3972
            if ( $sethours and $sethours eq 'close' ) {
3973
                $datedue->set( hour => $close[0], minute => $close[1] );
3974
            } elsif ( $sethours and $sethours eq 'open' ) {
3975
                $datedue->set( hour => $open[0], minute => $open[1] );
3976
            }
3917
        }
3977
        }
3918
    }
3978
    }
3919
3979
(-)a/t/db_dependent/Circulation/CalcDateDue.t (-4 / +82 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::Hours;
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 18-24 my $schema = Koha::Database->new->schema; Link Here
18
$schema->storage->txn_begin;
19
$schema->storage->txn_begin;
19
my $builder = t::lib::TestBuilder->new;
20
my $builder = t::lib::TestBuilder->new;
20
21
21
22
t::lib::Mocks::mock_preference( 'ConsiderLibraryHoursInCirculation', 'ignore' );
22
my $library = $builder->build_object({ class => 'Koha::Libraries' })->store;
23
my $library = $builder->build_object({ class => 'Koha::Libraries' })->store;
23
my $dateexpiry = '2013-01-01';
24
my $dateexpiry = '2013-01-01';
24
my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { category_type => 'B' } })->store;
25
my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { category_type => 'B' } })->store;
Lines 356-360 my $renewed_date = $start_date->clone->add( days => 7 ); Link Here
356
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
357
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
357
is( $date->ymd, $renewed_date->ymd, 'Renewal period of "" should trigger fallover to issuelength for renewal' );
358
is( $date->ymd, $renewed_date->ymd, 'Renewal period of "" should trigger fallover to issuelength for renewal' );
358
359
360
# Testing hourly loans consider library open hours
361
362
my $library1 = $builder->build( { source => 'Branch' } );
363
Koha::CirculationRules->set_rules(
364
    {
365
        categorycode => $categorycode,
366
        itemtype     => $itemtype,
367
        branchcode   => $library1->{branchcode},
368
        rules        => {
369
            issuelength   => 3, # loan period is 3 hours
370
            lengthunit    => 'hours',
371
        }
372
    }
373
);
374
375
my $open = DateTime->now->subtract( hours => 4 )->hms;
376
my $close = DateTime->now->add( hours => 2 )->hms;
377
my $now = DateTime->now;
378
379
foreach (0..6) {
380
    # library opened 4 hours ago and closes in 2 hours.
381
    Koha::Library::Hour->new({ day => $_, library_id => $library1->{branchcode}, open_time => $open, close_time => $close })->store;
382
}
383
384
# ignore calendar
385
t::lib::Mocks::mock_preference('useDaysMode', 'Days');
386
t::lib::Mocks::mock_preference('ConsiderLibraryHoursInCirculation', 'close');
387
# shorten loan period
388
389
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
390
my $expected_duetime = DateTime->now->add( hours => 2 );
391
is( $date, $expected_duetime, "Loan period was shortened because ConsiderLibraryHoursInCirculation is set to close time" );
392
393
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'open');
394
# extend loan period
395
396
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
397
$expected_duetime = DateTime->now->add( days => 1 )->subtract( hours => 4 );
398
is( $date, $expected_duetime, "Loan period was extended because ConsiderLibraryHoursInCirculation is set to open time" );
399
400
my $holiday_tomorrow = DateTime->now->add( days => 1 );
401
402
# consider calendar
403
my $library1_calendar = C4::Calendar->new( branchcode => $library1->{branchcode} );
404
$library1_calendar->insert_single_holiday(
405
    day             => $holiday_tomorrow->day,
406
    month           => $holiday_tomorrow->month,
407
    year            => $holiday_tomorrow->year,
408
    title           => 'testholiday',
409
    description     => 'testholiday'
410
);
411
Koha::CirculationRules->set_rules(
412
    {
413
        categorycode => $categorycode,
414
        itemtype     => $itemtype,
415
        branchcode   => $library1->{branchcode},
416
        rules        => {
417
            issuelength   => 13, # loan period must cross over into tomorrow
418
            lengthunit    => 'hours',
419
        }
420
    }
421
);
422
423
t::lib::Mocks::mock_preference('useDaysMode', 'Calendar');
424
t::lib::Mocks::mock_preference('ConsiderLibraryHoursInCirculation', 'close');
425
# shorten loan period
426
427
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
428
$expected_duetime = DateTime->now->add( days => 2, hours => 2 );
429
is( $date, $expected_duetime, "Loan period was shortened (but considers the holiday) because ConsiderLibraryHoursInCirculation is set to close time" );
430
431
t::lib::Mocks::mock_preference( 'ConsiderLibraryHoursInCirculation', 'open' );
432
# extend loan period
433
434
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
435
$expected_duetime = DateTime->now->add( days => 2 )->subtract( hours => 4 );
436
is( $date, $expected_duetime, "Loan period was extended (but considers the holiday) because ConsiderLibraryHoursInCirculation is set to open time" );
437
359
$cache->clear_from_cache($key);
438
$cache->clear_from_cache($key);
360
$schema->storage->txn_rollback;
439
$schema->storage->txn_rollback;
361
- 

Return to bug 6796