3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
30 use Test::More tests => 12;
32 my $dbh = C4::Context->dbh;
33 my $schema = Koha::Database->new()->schema();
36 $dbh->{RaiseError} = 1;
37 $schema->storage->txn_begin();
39 $dbh->do('DELETE FROM issues');
40 $dbh->do('DELETE FROM issuingrules');
41 $dbh->do('DELETE FROM borrowers');
42 $dbh->do('DELETE FROM items');
45 C4::Context->_new_userenv('xxx');
46 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', 'MPL', 'Midway Public Library', '', '', '' );
47 is( C4::Context->userenv->{branch}, 'MPL', 'userenv set' );
50 for my $i ( 1 .. 20 ) {
51 my $patron = Koha::Borrower->new(
52 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => 'S', branchcode => 'MPL' } )
54 push( @patrons, $patron );
57 my $biblio = Koha::Biblio->new()->store();
59 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
62 for my $i ( 1 .. 10 ) {
63 my $item = Koha::Item->new( { biblionumber => $biblio->id(), biblioitemnumber => $biblioitem->id(), } )->store();
64 push( @items, $item );
67 for my $i ( 0 .. 5 ) {
68 my $patron = $patrons[$i];
69 my $hold = Koha::Hold->new(
71 borrowernumber => $patron->id,
72 biblionumber => $biblio->id,
78 $schema->resultset('Issuingrule')
79 ->new( { branchcode => '*', categorycode => '*', itemtype => '*', issuelength => '14', lengthunit => 'days', reservesallowed => '99' } )
82 my $item = pop(@items);
83 my $patron = pop(@patrons);
85 C4::Context->set_preference( 'decreaseLoanHighHolds', 1 );
86 C4::Context->set_preference( 'decreaseLoanHighHoldsDuration', 1 );
87 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 1 );
88 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'static' );
89 C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
91 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => 'MPL', holdingbranch => 'MPL' };
92 my $patron_hr = { borrower => $patron->id, branchcode => 'MPL' };
94 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
95 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
96 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
97 is( $data->{duration}, 1, "Should have duration of 1" );
98 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
100 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
101 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
102 is( $data->{exceeded}, 0, "Should not exceed threshold" );
104 for my $i ( 5 .. 10 ) {
105 my $patron = $patrons[$i];
106 my $hold = Koha::Hold->new(
108 borrowernumber => $patron->id,
109 biblionumber => $biblio->id,
115 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
116 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
118 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
119 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
120 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
122 my $unholdable = pop(@items);
123 $unholdable->damaged(-1);
124 $unholdable->store();
126 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
127 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
129 $unholdable->damaged(0);
130 $unholdable->itemlost(-1);
131 $unholdable->store();
133 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
134 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
136 $unholdable->itemlost(0);
137 $unholdable->notforloan(-1);
138 $unholdable->store();
140 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
141 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
143 $unholdable->notforloan(0);
144 $unholdable->withdrawn(-1);
145 $unholdable->store();
147 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
148 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
150 $schema->storage->txn_rollback();