Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3698 lines
153 KiB

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use utf8;
use Test::More tests => 46;
use Test::MockModule;
use Test::Deep qw( cmp_deeply );
use Data::Dumper;
use DateTime;
use Time::Fake;
use POSIX qw( floor );
use t::lib::Mocks;
use t::lib::TestBuilder;
use C4::Accounts;
use C4::Calendar;
use C4::Circulation;
use C4::Biblio;
use C4::Items;
use C4::Log;
use C4::Reserves;
use C4::Overdues qw(UpdateFine CalcFine);
use Koha::DateUtils;
use Koha::Database;
use Koha::Items;
use Koha::Checkouts;
use Koha::Patrons;
use Koha::CirculationRules;
use Koha::Subscriptions;
use Koha::Account::Lines;
use Koha::Account::Offsets;
use Koha::ActionLogs;
sub set_userenv {
my ( $library ) = @_;
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
}
sub str {
my ( $error, $question, $alert ) = @_;
my $s;
$s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
$s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
$s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
return $s;
}
sub test_debarment_on_checkout {
my ($params) = @_;
my $item = $params->{item};
my $library = $params->{library};
my $patron = $params->{patron};
my $due_date = $params->{due_date} || dt_from_string;
my $return_date = $params->{return_date} || dt_from_string;
my $expected_expiration_date = $params->{expiration_date};
$expected_expiration_date = output_pref(
{
dt => $expected_expiration_date,
dateformat => 'sql',
dateonly => 1,
}
);
my @caller = caller;
my $line_number = $caller[2];
AddIssue( $patron, $item->{barcode}, $due_date );
my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
or diag('AddReturn returned message ' . Dumper $message );
my $debarments = Koha::Patron::Debarments::GetDebarments(
{ borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
is( $debarments->[0]->{expiration},
$expected_expiration_date, 'Test at line ' . $line_number );
Koha::Patron::Debarments::DelUniqueDebarment(
{ borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
};
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
# Prevent random failures by mocking ->now
my $now_value = dt_from_string;
my $mocked_datetime = Test::MockModule->new('DateTime');
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
# Start transaction
$dbh->{RaiseError} = 1;
my $cache = Koha::Caches->get_instance();
$dbh->do(q|DELETE FROM special_holidays);
$dbh->do(q|DELETE FROM repeatable_holidays);
$cache->clear_from_cache('single_holidays');
# Start with a clean slate
$dbh->do('DELETE FROM issues');
$dbh->do('DELETE FROM borrowers');
my $library = $builder->build({
source => 'Branch',
});
my $library2 = $builder->build({
source => 'Branch',
});
my $itemtype = $builder->build(
{
source => 'Itemtype',
value => {
notforloan => undef,
rentalcharge => 0,
rentalcharge_daily => 0,
defaultreplacecost => undef,
processfee => undef
}
}
)->{itemtype};
my $patron_category = $builder->build(
{
source => 'Category',
value => {
category_type => 'P',
enrolmentfee => 0,
BlockExpiredPatronOpacActions => -1, # Pick the pref value
}
}
);
my $CircControl = C4::Context->preference('CircControl');
my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
my $item = {
homebranch => $library2->{branchcode},
holdingbranch => $library2->{branchcode}
};
my $borrower = {
branchcode => $library2->{branchcode}
};
t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
# No userenv, PickupLibrary
t::lib::Mocks::mock_preference('IndependentBranches', '0');
t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
is(
C4::Context->preference('CircControl'),
'PickupLibrary',
'CircControl changed to PickupLibrary'
);
is(
C4::Circulation::_GetCircControlBranch($item, $borrower),
$item->{$HomeOrHoldingBranch},
'_GetCircControlBranch returned item branch (no userenv defined)'
);
# No userenv, PatronLibrary
t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
is(
C4::Context->preference('CircControl'),
'PatronLibrary',
'CircControl changed to PatronLibrary'
);
is(
C4::Circulation::_GetCircControlBranch($item, $borrower),
$borrower->{branchcode},
'_GetCircControlBranch returned borrower branch'
);
# No userenv, ItemHomeLibrary
t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
is(
C4::Context->preference('CircControl'),
'ItemHomeLibrary',
'CircControl changed to ItemHomeLibrary'
);
is(
$item->{$HomeOrHoldingBranch},
C4::Circulation::_GetCircControlBranch($item, $borrower),
'_GetCircControlBranch returned item branch'
);
# Now, set a userenv
t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
# Userenv set, PickupLibrary
t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
is(
C4::Context->preference('CircControl'),
'PickupLibrary',
'CircControl changed to PickupLibrary'
);
is(
C4::Circulation::_GetCircControlBranch($item, $borrower),
$library2->{branchcode},
'_GetCircControlBranch returned current branch'
);
# Userenv set, PatronLibrary
t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
is(
C4::Context->preference('CircControl'),
'PatronLibrary',
'CircControl changed to PatronLibrary'
);
is(
C4::Circulation::_GetCircControlBranch($item, $borrower),
$borrower->{branchcode},
'_GetCircControlBranch returned borrower branch'
);
# Userenv set, ItemHomeLibrary
t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
is(
C4::Context->preference('CircControl'),
'ItemHomeLibrary',
'CircControl changed to ItemHomeLibrary'
);
is(
C4::Circulation::_GetCircControlBranch($item, $borrower),
$item->{$HomeOrHoldingBranch},
'_GetCircControlBranch returned item branch'
);
# Reset initial configuration
t::lib::Mocks::mock_preference('CircControl', $CircControl);
is(
C4::Context->preference('CircControl'),
$CircControl,
'CircControl reset to its initial value'
);
# Set a simple circ policy
$dbh->do('DELETE FROM circulation_rules');
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
reservesallowed => 25,
issuelength => 14,
lengthunit => 'days',
renewalsallowed => 1,
renewalperiod => 7,
norenewalbefore => undef,
auto_renew => 0,
fine => .10,
chargeperiod => 1,
}
}
);
my ( $reused_itemnumber_1, $reused_itemnumber_2 );
subtest "CanBookBeRenewed tests" => sub {
plan tests => 71;
C4::Context->set_preference('ItemsDeniedRenewal','');
# Generate test biblio
my $biblio = $builder->build_sample_biblio();
my $branch = $library2->{branchcode};
my $item_1 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 12.00,
itype => $itemtype
}
);
$reused_itemnumber_1 = $item_1->itemnumber;
my $item_2 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 23.00,
itype => $itemtype
}
);
$reused_itemnumber_2 = $item_2->itemnumber;
my $item_3 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 23.00,
itype => $itemtype
}
);
# Create borrowers
my %renewing_borrower_data = (
firstname => 'John',
surname => 'Renewal',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
);
my %reserving_borrower_data = (
firstname => 'Katrin',
surname => 'Reservation',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
);
my %hold_waiting_borrower_data = (
firstname => 'Kyle',
surname => 'Reservation',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
);
my %restricted_borrower_data = (
firstname => 'Alice',
surname => 'Reservation',
categorycode => $patron_category->{categorycode},
debarred => '3228-01-01',
branchcode => $branch,
);
my %expired_borrower_data = (
firstname => 'Ça',
surname => 'Glisse',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
dateexpiry => dt_from_string->subtract( months => 1 ),
);
my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
my $bibitems = '';
my $priority = '1';
my $resdate = undef;
my $expdate = undef;
my $notes = '';
my $checkitem = undef;
my $found = undef;
my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
my $datedue = dt_from_string( $issue->date_due() );
is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
my $issue2 = AddIssue( $renewing_borrower, $item_2->barcode);
$datedue = dt_from_string( $issue->date_due() );
is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $item_1->itemnumber } )->borrowernumber;
is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
is( $renewokay, 1, 'Can renew, no holds for this title or item');
# Biblio-level hold, renewal test
AddReserve(
{
branchcode => $branch,
borrowernumber => $reserving_borrowernumber,
biblionumber => $biblio->biblionumber,
priority => $priority,
reservation_date => $resdate,
expiration_date => $expdate,
notes => $notes,
itemnumber => $checkitem,
found => $found,
}
);
# Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
Koha::CirculationRules->set_rule(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rule_name => 'onshelfholds',
rule_value => '1',
}
);
t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
# Now let's add an item level hold, we should no longer be able to renew the item
my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
{
borrowernumber => $hold_waiting_borrowernumber,
biblionumber => $biblio->biblionumber,
itemnumber => $item_1->itemnumber,
branchcode => $branch,
priority => 3,
}
);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
$hold->delete();
# Now let's add a waiting hold on the 3rd item, it's no longer available tp check out by just anyone, so we should no longer
# be able to renew these items
$hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
{
borrowernumber => $hold_waiting_borrowernumber,
biblionumber => $biblio->biblionumber,
itemnumber => $item_3->itemnumber,
branchcode => $branch,
priority => 0,
found => 'W'
}
);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
AddIssue($reserving_borrower, $item_3->barcode);
my $reserve = $dbh->selectrow_hashref(
'SELECT * FROM old_reserves WHERE reserve_id = ?',
{ Slice => {} },
$reserveid
);
is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
# Item-level hold, renewal test
AddReserve(
{
branchcode => $branch,
borrowernumber => $reserving_borrowernumber,
biblionumber => $biblio->biblionumber,
priority => $priority,
reservation_date => $resdate,
expiration_date => $expdate,
notes => $notes,
itemnumber => $item_1->itemnumber,
found => $found,
}
);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber, 1);
is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
# Items can't fill hold for reasons
ModItem({ notforloan => 1 }, $biblio->biblionumber, $item_1->itemnumber);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
ModItem({ notforloan => 0, itype => $itemtype }, $biblio->biblionumber, $item_1->itemnumber);
# FIXME: Add more for itemtype not for loan etc.
# Restricted users cannot renew when RestrictionBlockRenewing is enabled
my $item_5 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 23.00,
itype => $itemtype,
}
);
my $datedue5 = AddIssue($restricted_borrower, $item_5->barcode);
is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $item_5->itemnumber);
is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
# Users cannot renew an overdue item
my $item_6 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 23.00,
itype => $itemtype,
}
);
my $item_7 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 23.00,
itype => $itemtype,
}
);
my $datedue6 = AddIssue( $renewing_borrower, $item_6->barcode);
is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
my $now = dt_from_string();
my $five_weeks = DateTime::Duration->new(weeks => 5);
my $five_weeks_ago = $now - $five_weeks;
t::lib::Mocks::mock_preference('finesMode', 'production');
my $passeddatedue1 = AddIssue($renewing_borrower, $item_7->barcode, $five_weeks_ago);
is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
my ( $fine ) = CalcFine( $item_7->unblessed, $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
C4::Overdues::UpdateFine(
{
issue_id => $passeddatedue1->id(),
itemnumber => $item_7->itemnumber,
borrowernumber => $renewing_borrower->{borrowernumber},
amount => $fine,
due => Koha::DateUtils::output_pref($five_weeks_ago)
}
);
t::lib::Mocks::mock_preference('RenewalLog', 0);
my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
my %params_renewal = (
timestamp => { -like => $date . "%" },
module => "CIRCULATION",
action => "RENEWAL",
);
my %params_issue = (
timestamp => { -like => $date . "%" },
module => "CIRCULATION",
action => "ISSUE"
);
my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
my $dt = dt_from_string();
Time::Fake->offset( $dt->epoch );
my $datedue1 = AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
Time::Fake->reset;
t::lib::Mocks::mock_preference('RenewalLog', 1);
$date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
$old_log_size = Koha::ActionLogs->count( \%params_renewal );
AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
$new_log_size = Koha::ActionLogs->count( \%params_renewal );
is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
is( $fines->count, 2, 'AddRenewal left both fines' );
isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
$fines->delete();
my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
AddIssue( $renewing_borrower,$item_7->barcode,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
$new_log_size = Koha::ActionLogs->count( \%params_renewal );
is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
$new_log_size = Koha::ActionLogs->count( \%params_issue );
is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
$fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
$fines->delete();
t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
$hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next;
$hold->cancel;
# Bug 14101
# Test automatic renewal before value for "norenewalbefore" in policy is set
# In this case automatic renewal is not permitted prior to due date
my $item_4 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
replacementprice => 16.00,
itype => $itemtype,
}
);
$issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
is( $error, 'auto_too_soon',
'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
# Bug 7413
# Test premature manual renewal
Koha::CirculationRules->set_rule(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rule_name => 'norenewalbefore',
rule_value => '7',
}
);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
# Bug 14395
# Test 'exact time' setting for syspref NoRenewalBeforePrecision
t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
is(
GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
$datedue->clone->add( days => -7 ),
'Bug 14395: Renewals permitted 7 days before due date, as expected'
);
# Bug 14395
# Test 'date' setting for syspref NoRenewalBeforePrecision
t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
is(
GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
$datedue->clone->add( days => -7 )->truncate( to => 'day' ),
'Bug 14395: Renewals permitted 7 days before due date, as expected'
);
# Bug 14101
# Test premature automatic renewal
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
is( $error, 'auto_too_soon',
'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
);
# Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
# and test automatic renewal again
$dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
is( $error, 'auto_too_soon',
'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
);
# Change policy so that loans can be renewed 99 days prior to the due date
# and test automatic renewal again
$dbh->do(q{UPDATE circulation_rules SET rule_value = '99' WHERE rule_name = 'norenewalbefore'});
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
is( $error, 'auto_renew',
'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
);
subtest "too_late_renewal / no_auto_renewal_after" => sub {
plan tests => 14;
my $item_to_auto_renew = $builder->build(
{ source => 'Item',
value => {
biblionumber => $biblio->biblionumber,
homebranch => $branch,
holdingbranch => $branch,
}
}
);
my $ten_days_before = dt_from_string->add( days => -10 );
my $ten_days_ahead = dt_from_string->add( days => 10 );
AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '7',
no_auto_renewal_after => '9',
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '7',
no_auto_renewal_after => '10',
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '7',
no_auto_renewal_after => '11',
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '11',
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => undef,
no_auto_renewal_after_hard_limit => dt_from_string->add( days => -1 ),
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '7',
no_auto_renewal_after => '15',
no_auto_renewal_after_hard_limit => dt_from_string->add( days => -1 ),
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => undef,
no_auto_renewal_after_hard_limit => dt_from_string->add( days => 1 ),
}
}
);
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
};
subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew & OPACFineNoRenewalsIncludeCredit" => sub {
plan tests => 10;
my $item_to_auto_renew = $builder->build({
source => 'Item',
value => {
biblionumber => $biblio->biblionumber,
homebranch => $branch,
holdingbranch => $branch,
}
});
my $ten_days_before = dt_from_string->add( days => -10 );
my $ten_days_ahead = dt_from_string->add( days => 10 );
AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '11',
}
}
);
C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
C4::Context->set_preference('OPACFineNoRenewals','10');
C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','1');
my $fines_amount = 5;
my $account = Koha::Account->new({patron_id => $renewing_borrowernumber});
$account->add_debit(
{
amount => $fines_amount,
interface => 'test',
type => 'OVERDUE',
item_id => $item_to_auto_renew->{itemnumber},
description => "Some fines"
}
)->status('RETURNED')->store;
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
$account->add_debit(
{
amount => $fines_amount,
interface => 'test',
type => 'OVERDUE',
item_id => $item_to_auto_renew->{itemnumber},
description => "Some fines"
}
)->status('RETURNED')->store;
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
$account->add_debit(
{
amount => $fines_amount,
interface => 'test',
type => 'OVERDUE',
item_id => $item_to_auto_renew->{itemnumber},
description => "Some fines"
}
)->status('RETURNED')->store;
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
$account->add_credit(
{
amount => $fines_amount,
interface => 'test',
type => 'PAYMENT',
description => "Some payment"
}
)->store;
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','0');
( $renewokay, $error ) =
CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
$dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','1');
};
subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
plan tests => 6;
my $item_to_auto_renew = $builder->build({
source => 'Item',
value => {
biblionumber => $biblio->biblionumber,
homebranch => $branch,
holdingbranch => $branch,
}
});
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => 10,
no_auto_renewal_after => 11,
}
}
);
my $ten_days_before = dt_from_string->add( days => -10 );
my $ten_days_ahead = dt_from_string->add( days => 10 );
# Patron is expired and BlockExpiredPatronOpacActions=0
# => auto renew is allowed
t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
my $patron = $expired_borrower;
my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
( $renewokay, $error ) =
CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
Koha::Checkouts->find( $checkout->issue_id )->delete;
# Patron is expired and BlockExpiredPatronOpacActions=1
# => auto renew is not allowed
t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
$patron = $expired_borrower;
$checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
( $renewokay, $error ) =
CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
Koha::Checkouts->find( $checkout->issue_id )->delete;
# Patron is not expired and BlockExpiredPatronOpacActions=1
# => auto renew is allowed
t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
$patron = $renewing_borrower;
$checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
( $renewokay, $error ) =
CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
is( $renewokay, 0, 'Do not renew, renewal is automatic' );
is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
Koha::Checkouts->find( $checkout->issue_id )->delete;
};
subtest "GetLatestAutoRenewDate" => sub {
plan tests => 5;
my $item_to_auto_renew = $builder->build(
{ source => 'Item',
value => {
biblionumber => $biblio->biblionumber,
homebranch => $branch,
holdingbranch => $branch,
}
}
);
my $ten_days_before = dt_from_string->add( days => -10 );
my $ten_days_ahead = dt_from_string->add( days => 10 );
AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '7',
no_auto_renewal_after => '',
no_auto_renewal_after_hard_limit => undef,
}
}
);
my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date, undef, 'GetLatestAutoRenewDate should return undef if no_auto_renewal_after or no_auto_renewal_after_hard_limit are not defined' );
my $five_days_before = dt_from_string->add( days => -5 );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '5',
no_auto_renewal_after_hard_limit => undef,
}
}
);
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'minute' ),
$five_days_before->truncate( to => 'minute' ),
'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
);
my $five_days_ahead = dt_from_string->add( days => 5 );
$dbh->do(q{UPDATE circulation_rules SET rule_value = '10' WHERE rule_name = 'norenewalbefore'});
$dbh->do(q{UPDATE circulation_rules SET rule_value = '15' WHERE rule_name = 'no_auto_renewal_after'});
$dbh->do(q{UPDATE circulation_rules SET rule_value = NULL WHERE rule_name = 'no_auto_renewal_after_hard_limit'});
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '15',
no_auto_renewal_after_hard_limit => undef,
}
}
);
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'minute' ),
$five_days_ahead->truncate( to => 'minute' ),
'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
);
my $two_days_ahead = dt_from_string->add( days => 2 );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '',
no_auto_renewal_after_hard_limit => dt_from_string->add( days => 2 ),
}
}
);
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'day' ),
$two_days_ahead->truncate( to => 'day' ),
'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
);
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => '10',
no_auto_renewal_after => '15',
no_auto_renewal_after_hard_limit => dt_from_string->add( days => 2 ),
}
}
);
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'day' ),
$two_days_ahead->truncate( to => 'day' ),
'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
);
};
# Too many renewals
# set policy to forbid renewals
Koha::CirculationRules->set_rules(
{
categorycode => undef,
branchcode => undef,
itemtype => undef,
rules => {
norenewalbefore => undef,
renewalsallowed => 0,
}
}
);
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
# Test WhenLostForgiveFine and WhenLostChargeReplacementFee
t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
C4::Overdues::UpdateFine(
{
issue_id => $issue->id(),
itemnumber => $item_1->itemnumber,
borrowernumber => $renewing_borrower->{borrowernumber},
amount => 15.00,
type => q{},
due => Koha::DateUtils::output_pref($datedue)
}
);
my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
is( $line->debit_type_code, 'OVERDUE', 'Account line type is OVERDUE' );
is( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
is( $line->amountoutstanding+0, 15, 'Account line amount outstanding is 15.00' );
is( $line->amount+0, 15, 'Account line amount is 15.00' );
is( $line->issue_id, $issue->id, 'Account line issue id matches' );
my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
is( $offset->amount+0, 15, 'Account offset amount is 15.00' );
t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
LostItem( $item_1->itemnumber, 'test', 1 );
$line = Koha::Account::Lines->find($line->id);
is( $line->debit_type_code, 'OVERDUE', 'Account type remains as OVERDUE' );
isnt( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
my $item = Koha::Items->find($item_1->itemnumber);
ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
my $checkout = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber });
is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
my $total_due = $dbh->selectrow_array(
'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
undef, $renewing_borrower->{borrowernumber}
);
is( $total_due+0, 15, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
C4::Context->dbh->do("DELETE FROM accountlines");
C4::Overdues::UpdateFine(
{
issue_id => $issue2->id(),
itemnumber => $item_2->itemnumber,
borrowernumber => $renewing_borrower->{borrowernumber},
amount => 15.00,
type => q{},
due => Koha::DateUtils::output_pref($datedue)
}
);
LostItem( $item_2->itemnumber, 'test', 0 );
my $item2 = Koha::Items->find($item_2->itemnumber);
ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
ok( Koha::Checkouts->find({ itemnumber => $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
$total_due = $dbh->selectrow_array(
'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
undef, $renewing_borrower->{borrowernumber}
);
ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
my $future = dt_from_string();
$future->add( days => 7 );
my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
# Users cannot renew any item if there is an overdue item
t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
my $manager = $builder->build_object({ class => "Koha::Patrons" });
t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
$checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
LostItem( $item_3->itemnumber, 'test', 0 );
my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
is(
$accountline->description,
sprintf( "%s %s %s",
$item_3->biblio->title || '',
$item_3->barcode || '',
$item_3->itemcallnumber || '' ),
"Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
);
};
subtest "GetUpcomingDueIssues" => sub {
plan tests => 12;
my $branch = $library2->{branchcode};
#Create another record
my $biblio2 = $builder->build_sample_biblio();
#Create third item
my $item_1 = Koha::Items->find($reused_itemnumber_1);
my $item_2 = Koha::Items->find($reused_itemnumber_2);
my $item_3 = $builder->build_sample_item(
{
biblionumber => $biblio2->biblionumber,
library => $branch,
itype => $itemtype,
}
);
# Create a borrower
my %a_borrower_data = (
firstname => 'Fridolyn',
surname => 'SOMERS',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
);
my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
my $today = DateTime->today(time_zone => C4::Context->tz());
my $issue = AddIssue( $a_borrower, $item_1->barcode, $yesterday );
my $datedue = dt_from_string( $issue->date_due() );
my $issue2 = AddIssue( $a_borrower, $item_2->barcode, $two_days_ahead );
my $datedue2 = dt_from_string( $issue->date_due() );
my $upcoming_dues;
# GetUpcomingDueIssues tests
for my $i(0..1) {
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
}
#days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
for my $i(3..5) {
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
is ( scalar( @$upcoming_dues ), 1,
"Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
}
# Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
my $issue3 = AddIssue( $a_borrower, $item_3->barcode, $today );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
$upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
};
subtest "Bug 13841 - Do not create new 0 amount fines" => sub {
my $branch = $library2->{branchcode};
my $biblio = $builder->build_sample_biblio();
#Create third item
my $item = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
itype => $itemtype,
}
);
# Create a borrower
my %a_borrower_data = (
firstname => 'Kyle',
surname => 'Hall',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
);
my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
my $issue = AddIssue( $borrower, $item->barcode );
UpdateFine(
{
issue_id => $issue->id(),
itemnumber => $item->itemnumber,
borrowernumber => $borrowernumber,
amount => 0,
type => q{}
}
);
my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
my $count = $hr->{count};
is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
};
subtest "AllowRenewalIfOtherItemsAvailable tests" => sub {
$dbh->do('DELETE FROM issues');
$dbh->do('DELETE FROM items');
$dbh->do('DELETE FROM circulation_rules');
Koha::CirculationRules->set_rules(
{
categorycode => undef,
itemtype => undef,
branchcode => undef,
rules => {
reservesallowed => 25,
issuelength => 14,
lengthunit => 'days',
renewalsallowed => 1,
renewalperiod => 7,
norenewalbefore => undef,
auto_renew => 0,
fine => .10,
chargeperiod => 1,
maxissueqty => 20
}
}
);
my $biblio = $builder->build_sample_biblio();
my $item_1 = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $library2->{branchcode},
itype => $itemtype,
}
);
my $item_2= $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $library2->{branchcode},
itype => $itemtype,
}
);
my $borrowernumber1 = Koha::Patron->new({
firstname => 'Kyle',
surname => 'Hall',
categorycode => $patron_category->{categorycode},
branchcode => $library2->{branchcode},
})->store->borrowernumber;
my $borrowernumber2 = Koha::Patron->new({
firstname => 'Chelsea',
surname => 'Hall',
categorycode => $patron_category->{categorycode},
branchcode => $library2->{branchcode},
})->store->borrowernumber;
my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
my $issue = AddIssue( $borrower1, $item_1->barcode );
my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
AddReserve(
{
branchcode => $library2->{branchcode},
borrowernumber => $borrowernumber2,
biblionumber => $biblio->biblionumber,
priority => 1,
}
);
Koha::CirculationRules->set_rules(
{
categorycode => undef,
itemtype => undef,
branchcode => undef,
rules => {
onshelfholds => 0,
}
}
);
t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
itemtype => undef,
branchcode => undef,
rules => {
onshelfholds => 0,
}
}
);
t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
itemtype => undef,
branchcode => undef,
rules => {
onshelfholds => 1,
}
}
);
t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
Koha::CirculationRules->set_rules(
{
categorycode => undef,
itemtype => undef,
branchcode => undef,
rules => {
onshelfholds => 1,
}
}
);
t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
# Setting item not checked out to be not for loan but holdable
ModItem({ notforloan => -1 }, $biblio->biblionumber, $item_2->itemnumber);
( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
is( $renewokay, 0, 'Bug 14337 - Verify the borrower can not renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled but the only available item is notforloan' );
};
{
# Don't allow renewing onsite checkout
my $branch = $library->{branchcode};
#Create another record
my $biblio = $builder->build_sample_biblio();
my $item = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $branch,
itype => $itemtype,
}
);
my $borrowernumber = Koha::Patron->new({
firstname => 'fn',
surname => 'dn',
categorycode => $patron_category->{categorycode},
branchcode => $branch,
})->store->borrowernumber;
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $item->itemnumber );
is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
}
{
my $library = $builder->build({ source => 'Branch' });
my $biblio = $builder->build_sample_biblio();
my $item = $builder->build_sample_item(
{
biblionumber => $biblio->biblionumber,
library => $library->{branchcode},
itype => $itemtype,
}
);
my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
my $issue = AddIssue( $patron, $item->barcode );
UpdateFine(
{
issue_id => $issue->id(),
itemnumber => $item->itemnumber,
borrowernumber => $patron->{borrowernumber},
amount => 1,
type => q{}
}
);
UpdateFine(
{
issue_id => $issue->id(),
itemnumber => $item->itemnumber,
borrowernumber => $patron->{borrowernumber},
amount => 2,
type => q{}
}
);
is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
}
subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
plan tests => 24;
my $homebranch = $builder->build( { source => 'Branch' } );
my $holdingbranch = $builder->build( { source => 'Branch' } );
my $otherbranch = $builder->build( { source => 'Branch' } );
my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
my $item = $builder->build_sample_item(
{
homebranch => $homebranch->{branchcode},
holdingbranch => $holdingbranch->{branchcode},
}
)->unblessed;
set_userenv($holdingbranch);
my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
is( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
my ( $error, $question, $alerts );
# AllowReturnToBranch == anywhere
t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
## Test that unknown barcodes don't generate internal server errors
set_userenv($homebranch);
( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
## Can be issued from homebranch
set_userenv($homebranch);
( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
## Can be issued from holdingbranch
set_userenv($holdingbranch);
( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
## Can be issued from another branch
( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors