@@ -, +, @@ --- C4/Circulation.pm | 4 +-- Koha/DiscreteCalendar.pm | 2 +- t/db_dependent/Circulation.t | 82 ++++++++++++++++++++++++-------------------- 3 files changed, 48 insertions(+), 40 deletions(-) --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -2252,10 +2252,10 @@ sub _debar_user_on_return { my $new_debar_dt; # Use the calendar or not to calculate the debarment date if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) { - my $calendar = Koha::Calendar->new( + my $calendar = Koha::DiscreteCalendar->new( { branchcode => $branchcode, days_mode => 'Calendar' - ); + } ); $new_debar_dt = $calendar->addDate( $return_date, $suspension_days ); } else { --- a/Koha/DiscreteCalendar.pm +++ a/Koha/DiscreteCalendar.pm @@ -90,7 +90,7 @@ sub new { sub _init { my $self = shift; - $self->{days_mode} = C4::Context->preference('useDaysMode'); + $self->{days_mode} ||= C4::Context->preference('useDaysMode'); #If the branchcode doesn't exist we use the default calendar. my $schema = Koha::Database->new->schema; my $branchcode = $self->{branchcode}; --- a/t/db_dependent/Circulation.t +++ a/t/db_dependent/Circulation.t @@ -24,7 +24,6 @@ use POSIX qw( floor ); use t::lib::Mocks; use t::lib::TestBuilder; -use C4::Calendar; use C4::Circulation; use C4::Biblio; use C4::Items; @@ -33,6 +32,7 @@ use C4::Reserves; use C4::Overdues qw(UpdateFine CalcFine); use Koha::DateUtils; use Koha::Database; +use Koha::DiscreteCalendar; use Koha::IssuingRules; use Koha::Checkouts; use Koha::Patrons; @@ -1779,16 +1779,16 @@ subtest 'AddReturn + suspension_chargeperiod' => sub { ); $rule->store(); - my $five_days_ago = dt_from_string->subtract( days => 5 ); + my $in_five_days = dt_from_string->add( days => 5 ); # We want to charge 2 days every day, without grace # With 5 days of overdue: 5 * Z - my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 ); + my $expected_expiration = dt_from_string->add( days => 5 + ( 5 * 2 ) / 1 ); test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration, } ); @@ -1796,13 +1796,13 @@ subtest 'AddReturn + suspension_chargeperiod' => sub { # We want to charge 2 days every 2 days, without grace # With 5 days of overdue: (5 * 2) / 2 $rule->suspension_chargeperiod(2)->store; - $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 ); + $expected_expiration = dt_from_string->add( days => 5 + floor( 5 * 2 ) / 2 ); test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration, } ); @@ -1811,13 +1811,13 @@ subtest 'AddReturn + suspension_chargeperiod' => sub { # With 5 days of overdue: ((5-1) / 3 ) * 2 $rule->suspension_chargeperiod(3)->store; $rule->firstremind(1)->store; - $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) ); + $expected_expiration = dt_from_string->add( days => 5 + floor( ( ( 5 - 1 ) / 3 ) * 2 ) ); test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration, } ); @@ -1830,68 +1830,76 @@ subtest 'AddReturn + suspension_chargeperiod' => sub { t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed'); # Adding a holiday 2 days ago - my $calendar = C4::Calendar->new(branchcode => $library->{branchcode}); - my $two_days_ago = dt_from_string->subtract( days => 2 ); - $calendar->insert_single_holiday( - day => $two_days_ago->day, - month => $two_days_ago->month, - year => $two_days_ago->year, - title => 'holidayTest-2d', - description => 'holidayDesc 2 days ago' - ); + my $calendar = Koha::DiscreteCalendar->new( { branchcode => $library->{branchcode} } ); + my $in_two_days = dt_from_string->add( days => 2 ); + $calendar->edit_holiday( { + title => 'holidayTest+2d', + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},, + start_date => $in_two_days, + end_date => $in_two_days, + } ); # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday) - $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) ); + $expected_expiration = dt_from_string->add( days => 5 + floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) ); test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration, } ); # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped - my $two_days_ahead = dt_from_string->add( days => 2 ); - $calendar->insert_single_holiday( - day => $two_days_ahead->day, - month => $two_days_ahead->month, - year => $two_days_ahead->year, - title => 'holidayTest+2d', - description => 'holidayDesc 2 days ahead' - ); + my $in_seven_days = dt_from_string->add( days => 7 ); + $calendar->edit_holiday( { + title => 'holidayTest+7d', + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, + start_date => $in_seven_days, + end_date => $in_seven_days, + } ); # Same as above, but we should skip D+2 - $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 ); + $expected_expiration = dt_from_string->add( days => 5 + floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 ); test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration, } ); # Adding another holiday, day of expiration date my $expected_expiration_dt = dt_from_string($expected_expiration); - $calendar->insert_single_holiday( - day => $expected_expiration_dt->day, - month => $expected_expiration_dt->month, - year => $expected_expiration_dt->year, - title => 'holidayTest_exp', - description => 'holidayDesc on expiration date' - ); + $calendar->edit_holiday( { + title => 'holidayTest+expected_expiration', + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, + start_date => $expected_expiration_dt, + end_date => $expected_expiration_dt, + } ); + # Expiration date will be the day after test_debarment_on_checkout( { item => $item_1, library => $library, patron => $patron, - due_date => $five_days_ago, + return_date => $in_five_days, expiration_date => $expected_expiration_dt->clone->add( days => 1 ), } ); + + test_debarment_on_checkout( + { + item => $item_1, + library => $library, + patron => $patron, + return_date => dt_from_string->add(days => 5), + expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) + 1 ), # We add an extra +1 because of the holiday + } + ); }; subtest 'AddReturn | is_overdue' => sub { --