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 3739-3749 sub CalcDateDue { Link Here
3739
        }
3740
        }
3740
    );
3741
    );
3741
3742
3743
    my $considerlibraryhours = C4::Context->preference('ConsiderLibraryHoursWhenIssuing');
3744
    # starter vars so don't do calculations directly to $datedue
3745
    my $potential_datedue = $datedue->clone;
3746
    my $library_close = $datedue->clone;
3747
    my $dayofweek = $datedue->local_day_of_week - 1;
3748
    my $todayhours = Koha::Library::Hours->find({ branchcode => $branch, day => $dayofweek });
3749
    my @close = undef;
3750
    my $tomorrowhours = Koha::Library::Hours->find({ branchcode => $branch, day => $dayofweek+1 }); # get open hours of next day
3751
    my @open = undef;
3752
    if ( $todayhours->close_time and $tomorrowhours->open_time ) {
3753
        @close = split( ":", $todayhours->close_time );
3754
        $library_close = $library_close->set( hour => $close[0], minute => $close[1] );
3755
        $potential_datedue = $potential_datedue->add( hours => $loanlength->{$length_key} ); # datedue without consideration for open hours
3756
        @open = split( ":", $tomorrowhours->open_time );
3757
    }
3758
3742
    # calculate the datedue as normal
3759
    # calculate the datedue as normal
3743
    if ( $daysmode eq 'Days' )
3760
    if ( $daysmode eq 'Days' )
3744
    {    # ignoring calendar
3761
    {    # ignoring calendar
3745
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3762
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3746
            $datedue->add( hours => $loanlength->{$length_key} );
3763
            if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) {
3764
                if ( $considerlibraryhours eq 'close' ) {
3765
                    # datedue will be after the library closes on that day
3766
                    # shorten loan period to end when library closes
3767
                    $datedue->set( hour => $close[0], minute => $close[1] );
3768
                } elsif ( $considerlibraryhours eq 'open' ) {
3769
                    # datedue will be after the library closes on that day
3770
                    # extend loan period to when library opens following day
3771
                    $datedue->add( days => 1 )->set( hour => $open[0], minute => $open[1] );
3772
                } else {
3773
                    # ignore library open hours
3774
                    $datedue->add( hours => $loanlength->{$length_key} );
3775
                }
3776
            } else {
3777
                # due time doesn't conflict with library open hours, don't need to check
3778
                $datedue->add( hours => $loanlength->{$length_key} );
3779
            }
3747
        } else {    # days
3780
        } else {    # days
3748
            $datedue->add( days => $loanlength->{$length_key} );
3781
            $datedue->add( days => $loanlength->{$length_key} );
3749
            $datedue->set_hour(23);
3782
            $datedue->set_hour(23);
Lines 3751-3767 sub CalcDateDue { Link Here
3751
        }
3784
        }
3752
    } else {
3785
    } else {
3753
        my $dur;
3786
        my $dur;
3787
        my $sethours;
3754
        if ($loanlength->{lengthunit} eq 'hours') {
3788
        if ($loanlength->{lengthunit} eq 'hours') {
3755
            $dur = DateTime::Duration->new( hours => $loanlength->{$length_key});
3789
            if ( $potential_datedue > $library_close and $todayhours->close_time and $tomorrowhours->open_time ) {
3790
                if ( $considerlibraryhours eq 'close' ) {
3791
                    # datedue will be after the library closes on that day
3792
                    # shorten loan period to end when library closes
3793
                    $dur = $potential_datedue->delta_ms( $library_close );
3794
                    $sethours = $considerlibraryhours;
3795
                } elsif ( $considerlibraryhours eq 'open' ) {
3796
                    # datedue will be after the library closes on that day
3797
                    # extend loan period to when library opens following day
3798
                    my $library_open = $datedue->clone->set( hour => $open[0], minute => $open[1] );
3799
                    $dur = $potential_datedue->delta_ms( $library_open )->add( days => 1 );
3800
                    $sethours = $considerlibraryhours;
3801
                } else {
3802
                    # ignore library open hours
3803
                    $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} );
3804
                }
3805
            } else {
3806
                # due time doesn't conflict with library open hours, don't need to check
3807
                $dur = DateTime::Duration->new( hours => $loanlength->{$length_key} );
3808
            }
3756
        }
3809
        }
3757
        else { # days
3810
        else { # days
3758
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key});
3811
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key} );
3759
        }
3812
        }
3760
        my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );
3813
        my $calendar = Koha::Calendar->new( branchcode => $branch, days_mode => $daysmode );
3761
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3814
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3762
        if ($loanlength->{lengthunit} eq 'days') {
3815
        if ($loanlength->{lengthunit} eq 'days') {
3763
            $datedue->set_hour(23);
3816
            $datedue->set_hour(23);
3764
            $datedue->set_minute(59);
3817
            $datedue->set_minute(59);
3818
        } else {
3819
            if ( $sethours and $sethours eq 'close' ) {
3820
                $datedue->set( hour => $close[0], minute => $close[1] );
3821
            } elsif ( $sethours and $sethours eq 'open' ) {
3822
                $datedue->set( hour => $open[0], minute => $open[1] );
3823
            }
3765
        }
3824
        }
3766
    }
3825
    }
3767
3826
(-)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 => 17;
5
use Test::More tests => 21;
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 303-307 $calendar->delete_holiday( Link Here
303
    weekday => 6
304
    weekday => 6
304
);
305
);
305
306
307
# Testing hourly loans consider library open hours
308
309
my $library1 = $builder->build( { source => 'Branch' } );
310
Koha::CirculationRules->set_rules(
311
    {
312
        categorycode => $categorycode,
313
        itemtype     => $itemtype,
314
        branchcode   => $library1->{branchcode},
315
        rules        => {
316
            issuelength   => 3, # loan period is 3 hours
317
            lengthunit    => 'hours',
318
        }
319
    }
320
);
321
322
my $open = DateTime->now->subtract( hours => 4 )->hms;
323
my $close = DateTime->now->add( hours => 2 )->hms;
324
my $now = DateTime->now;
325
326
foreach (0..6) {
327
    # library opened 4 hours ago and closes in 2 hours.
328
    Koha::Library::Hour->new({ day => $_, branchcode => $library1->{branchcode}, open_time => $open, close_time => $close })->store;
329
}
330
331
# ignore calendar
332
t::lib::Mocks::mock_preference('useDaysMode', 'Days');
333
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'close');
334
# shorten loan period
335
336
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
337
my $expected_duetime = DateTime->now->add( hours => 2 );
338
is( $date, $expected_duetime, "Loan period was shortened because ConsiderLibraryHoursWhenIssuing is set to close time" );
339
340
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'open');
341
# extend loan period
342
343
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
344
$expected_duetime = DateTime->now->add( days => 1 )->subtract( hours => 4 );
345
is( $date, $expected_duetime, "Loan period was extended because ConsiderLibraryHoursWhenIssuing is set to open time" );
346
347
my $holiday_tomorrow = DateTime->now->add( days => 1 );
348
349
# consider calendar
350
my $library1_calendar = C4::Calendar->new( branchcode => $library1->{branchcode} );
351
$library1_calendar->insert_single_holiday(
352
    day             => $holiday_tomorrow->day,
353
    month           => $holiday_tomorrow->month,
354
    year            => $holiday_tomorrow->year,
355
    title           => 'testholiday',
356
    description     => 'testholiday'
357
);
358
Koha::CirculationRules->set_rules(
359
    {
360
        categorycode => $categorycode,
361
        itemtype     => $itemtype,
362
        branchcode   => $library1->{branchcode},
363
        rules        => {
364
            issuelength   => 13, # loan period must cross over into tomorrow
365
            lengthunit    => 'hours',
366
        }
367
    }
368
);
369
370
t::lib::Mocks::mock_preference('useDaysMode', 'Calendar');
371
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'close');
372
# shorten loan period
373
374
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
375
$expected_duetime = DateTime->now->add( days => 2, hours => 2 );
376
is( $date, $expected_duetime, "Loan period was shortened (but considers the holiday) because ConsiderLibraryHoursWhenIssuing is set to close time" );
377
378
t::lib::Mocks::mock_preference('ConsiderLibraryHoursWhenIssuing', 'open');
379
# extend loan period
380
381
$date = C4::Circulation::CalcDateDue( $now, $itemtype, $library1->{branchcode}, $borrower );
382
$expected_duetime = DateTime->now->add( days => 2 )->subtract( hours => 4 );
383
is( $date, $expected_duetime, "Loan period was extended (but considers the holiday) because ConsiderLibraryHoursWhenIssuing is set to open time" );
384
306
$cache->clear_from_cache($key);
385
$cache->clear_from_cache($key);
307
$schema->storage->txn_rollback;
386
$schema->storage->txn_rollback;
308
- 

Return to bug 6796