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>.
29 use Koha
::CirculationRules
;
30 use t
::lib
::TestBuilder
;
33 use Test
::More tests
=> 21;
35 my $dbh = C4
::Context
->dbh;
36 my $schema = Koha
::Database
->new()->schema();
37 my $builder = t
::lib
::TestBuilder
->new;
39 $schema->storage->txn_begin();
41 my $now_value = dt_from_string
();
42 my $mocked_datetime = Test
::MockModule
->new('DateTime');
43 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
45 my $library = $builder->build( { source
=> 'Branch' } );
46 my $category = $builder->build( { source
=> 'Category' } );
47 my $itemtype = $builder->build( { source
=> 'Itemtype' } )->{itemtype
};
49 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library->{branchcode
} });
50 is
( C4
::Context
->userenv->{branch
}, $library->{branchcode
}, 'userenv set' );
52 my $patron_category = $builder->build({
61 for my $i ( 1 .. 20 ) {
62 my $patron = Koha
::Patron
->new({
65 categorycode
=> $category->{categorycode
},
66 branchcode
=> $library->{branchcode
},
67 categorycode
=> $patron_category->{categorycode
},
69 push( @patrons, $patron );
72 my $biblio = $builder->build_sample_biblio();
74 # The biblio gets 10 items
76 for my $i ( 1 .. 10 ) {
77 my $item = $builder->build_sample_item(
79 biblionumber
=> $biblio->id(),
83 push( @items, $item );
86 # Place 6 holds, patrons 0,1,2,3,4,5
87 for my $i ( 0 .. 5 ) {
88 my $patron = $patrons[$i];
89 my $hold = Koha
::Hold
->new(
91 borrowernumber
=> $patron->id,
92 biblionumber
=> $biblio->id,
93 branchcode
=> $library->{branchcode
},
98 my $item = shift(@items);
99 my $patron = shift(@patrons);
100 my $patron_hold = Koha
::Holds
->find({ borrowernumber
=> $patron->borrowernumber, biblionumber
=> $item->biblionumber });
102 Koha
::CirculationRules
->set_rules(
105 categorycode
=> undef,
106 itemtype
=> $item->itype,
109 lengthunit
=> 'days',
110 reservesallowed
=> '99',
111 holds_per_record
=> '99',
112 decreaseloanholds
=> 0,
117 my $orig_due = C4
::Circulation
::CalcDateDue
(
119 $item->effective_itemtype,
124 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHolds', 1 );
125 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsDuration', 1 );
126 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 1 );
127 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'static' );
128 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
130 my $item_hr = { itemnumber
=> $item->id, biblionumber
=> $biblio->id, homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, barcode
=> $item->barcode };
131 my $patron_hr = { borrowernumber
=> $patron->id, branchcode
=> $library->{branchcode
} };
133 my $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
134 is
( $data->{exceeded
}, 1, "Static mode should exceed threshold" );
135 is
( $data->{outstanding
}, 6, "Should have 6 outstanding holds" );
136 is
( $data->{duration
}, 0, "Should have duration of 0 because of specific circulation rules" );
137 is
( ref( $data->{due_date
} ), 'DateTime', "due_date should be a DateTime object" );
139 Koha
::CirculationRules
->set_rules(
142 categorycode
=> undef,
143 itemtype
=> $item->itype,
146 lengthunit
=> 'days',
147 reservesallowed
=> '99',
148 holds_per_record
=> '99',
149 decreaseloanholds
=> undef,
154 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
155 is
( $data->{duration
}, 1, "Should have a duration of 1 because no specific circulation rules so defaults to system preference" );
157 my $duedate = $data->{due_date
};
158 is
($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
159 is
($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
160 is
($duedate->sec, 0, 'New due date second is zero.');
162 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'dynamic' );
163 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
164 is
( $data->{exceeded
}, 0, "Should not exceed threshold" );
167 # Place 6 more holds - patrons 5,6,7,8,9,10
168 for my $i ( 5 .. 10 ) {
169 my $patron = $patrons[$i];
170 my $hold = Koha
::Hold
->new(
172 borrowernumber
=> $patron->id,
173 biblionumber
=> $biblio->id,
174 branchcode
=> $library->{branchcode
},
179 # 12 holds, threshold is 1 over 10 holdable items = 11
180 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
181 is
( $data->{exceeded
}, 1, "Should exceed threshold of 1" );
182 is
( $data->{outstanding
}, 12, "Should exceed threshold of 1" );
184 # 12 holds, threshold is 2 over 10 holdable items = 12 (equal is okay)
185 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 2 );
186 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
187 is
( $data->{exceeded
}, 0, "Should not exceed threshold of 2" );
189 my $unholdable = pop(@items);
190 $unholdable->damaged(-1);
191 $unholdable->store();
193 # 12 holds, threshold is 2 over 9 holdable items = 11
194 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
195 is
( $data->{exceeded
}, 1, "Should exceed threshold with one damaged item" );
197 $unholdable->damaged(0);
198 $unholdable->itemlost(-1);
199 $unholdable->store();
201 # 12 holds, threshold is 2 over 9 holdable items = 11
202 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
203 is
( $data->{exceeded
}, 1, "Should exceed threshold with one lost item" );
205 $unholdable->itemlost(0);
206 $unholdable->notforloan(-1);
207 $unholdable->store();
209 # 12 holds, threshold is 2 over 9 holdable items = 11
210 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
211 is
( $data->{exceeded
}, 1, "Should exceed threshold with one notforloan item" );
213 $unholdable->notforloan(0);
214 $unholdable->withdrawn(-1);
215 $unholdable->store();
217 # 12 holds, threshold is 2 over 9 holdable items = 11
218 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
219 is
( $data->{exceeded
}, 1, "Should exceed threshold with one withdrawn item" );
221 $patron_hold->found('F')->store;
222 # 11 holds, threshold is 2 over 9 holdable items = 11
223 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
224 is
( $data->{exceeded
}, 1, "Should exceed threshold with one withdrawn item" );
225 $patron_hold->found(undef)->store;
227 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
229 my $patron_object = Koha
::Patrons
->find( $patron_hr->{borrowernumber
} );
230 my ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_object, $item->barcode );
231 ok
( $needsconfirmation->{HIGHHOLDS
}, "High holds checkout needs confirmation" );
233 ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds
=> 1 } );
234 ok
( !$needsconfirmation->{HIGHHOLDS
}, "High holds checkout does not need confirmation" );
236 Koha
::CirculationRules
->set_rule(
239 categorycode
=> undef,
240 itemtype
=> $item->itype,
241 rule_name
=> 'decreaseloanholds',
246 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
247 is
( $data->{duration
}, 2, "Circulation rules override system preferences" );
249 $schema->storage->txn_rollback();