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>.
20 use C4::Circulation qw( AddIssue AddReturn GetBranchBorrowerCircRule GetBranchItemRule );
21 use C4::Items qw( ModItemTransfer );
22 use C4::Biblio qw( AddBiblio );
24 use Koha::CirculationRules;
28 use Test::More tests => 18;
30 use t::lib::TestBuilder;
33 use_ok('C4::Circulation', qw( AddIssue AddReturn GetBranchBorrowerCircRule GetBranchItemRule ));
36 can_ok( 'C4::Circulation', qw(
39 GetBranchBorrowerCircRule
44 my $schema = Koha::Database->schema;
45 $schema->storage->txn_begin;
46 my $dbh = C4::Context->dbh;
49 $dbh->do(q|DELETE FROM issues|);
50 $dbh->do(q|DELETE FROM items|);
51 $dbh->do(q|DELETE FROM borrowers|);
52 $dbh->do(q|DELETE FROM clubs|);
53 $dbh->do(q|DELETE FROM branches|);
54 $dbh->do(q|DELETE FROM categories|);
55 $dbh->do(q|DELETE FROM accountlines|);
56 $dbh->do(q|DELETE FROM itemtypes|);
57 $dbh->do(q|DELETE FROM circulation_rules|);
59 my $builder = t::lib::TestBuilder->new();
62 my $samplebranch1 = $builder->build({ source => 'Branch' });
63 my $samplebranch2 = $builder->build({ source => 'Branch' });
65 my $no_circ_itemtype = $builder->build({
72 my $sampleitemtype1 = $builder->build({
75 rentalcharge => '10.0',
79 my $sampleitemtype2 = $builder->build({
82 rentalcharge => '5.0',
87 my $samplecat = $builder->build({
95 my $record = MARC::Record->new();
96 $record->append_fields(
97 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
98 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
100 # item 1 has home branch and holding branch samplebranch1
101 my $item_id1 = Koha::Item->new(
103 biblionumber => $biblionumber,
104 barcode => 'barcode_1',
105 itemcallnumber => 'callnumber1',
106 homebranch => $samplebranch1->{branchcode},
107 holdingbranch => $samplebranch1->{branchcode},
108 itype => $no_circ_itemtype->{ itemtype }
110 )->store->itemnumber;
112 # item 2 has holding branch samplebranch2
113 my $item_id2 = Koha::Item->new(
115 biblionumber => $biblionumber,
116 barcode => 'barcode_2',
117 itemcallnumber => 'callnumber2',
118 homebranch => $samplebranch2->{branchcode},
119 holdingbranch => $samplebranch1->{branchcode},
120 itype => $no_circ_itemtype->{ itemtype }
122 )->store->itemnumber;
124 # item 3 has item type sampleitemtype2 with noreturn policy
125 my $item_id3 = Koha::Item->new(
127 biblionumber => $biblionumber,
128 barcode => 'barcode_3',
129 itemcallnumber => 'callnumber3',
130 homebranch => $samplebranch2->{branchcode},
131 holdingbranch => $samplebranch2->{branchcode},
132 itype => $sampleitemtype2->{itemtype}
134 )->store->itemnumber;
137 my $borrower_id1 = Koha::Patron->new({
138 firstname => 'firstname1',
139 surname => 'surname1 ',
140 categorycode => $samplecat->{categorycode},
141 branchcode => $samplebranch1->{branchcode},
142 })->store->borrowernumber;
145 GetBranchBorrowerCircRule(),
146 { patron_maxissueqty => undef, patron_maxonsiteissueqty => undef },
147 "Without parameter, GetBranchBorrower returns undef (unilimited) for patron_maxissueqty and patron_maxonsiteissueqty if no rules defined"
150 Koha::CirculationRules->set_rules(
152 branchcode => $samplebranch1->{branchcode},
153 categorycode => $samplecat->{categorycode},
155 patron_maxissueqty => 5,
156 patron_maxonsiteissueqty => 6,
162 Koha::CirculationRules->set_rules(
164 branchcode => $samplebranch2->{branchcode},
165 categorycode => undef,
167 patron_maxissueqty => 3,
168 patron_maxonsiteissueqty => 2,
172 Koha::CirculationRules->set_rules(
174 branchcode => $samplebranch2->{branchcode},
177 holdallowed => 'from_home_library',
178 returnbranch => 'holdingbranch',
183 Koha::CirculationRules->set_rules(
186 categorycode => undef,
188 patron_maxissueqty => 4,
189 patron_maxonsiteissueqty => 5,
193 Koha::CirculationRules->set_rules(
198 holdallowed => 'from_local_hold_group',
199 returnbranch => 'homebranch',
204 Koha::CirculationRules->set_rules(
206 branchcode => $samplebranch1->{branchcode},
207 itemtype => $sampleitemtype1->{itemtype},
209 holdallowed => 'invalid_value',
210 returnbranch => 'homebranch',
214 Koha::CirculationRules->set_rules(
216 branchcode => $samplebranch2->{branchcode},
217 itemtype => $sampleitemtype1->{itemtype},
219 holdallowed => 'invalid_value',
220 returnbranch => 'holdingbranch',
224 Koha::CirculationRules->set_rules(
226 branchcode => $samplebranch2->{branchcode},
227 itemtype => $sampleitemtype2->{itemtype},
229 holdallowed => 'invalid_value',
230 returnbranch => 'noreturn',
235 #Test GetBranchBorrowerCircRule
237 GetBranchBorrowerCircRule(),
238 { patron_maxissueqty => 4, patron_maxonsiteissueqty => 5 },
239 "Without parameter, GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty of default_circ_rules"
242 GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
243 { patron_maxissueqty => 3, patron_maxonsiteissueqty => 2 },
244 "Without only the branchcode specified, GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty corresponding"
247 GetBranchBorrowerCircRule(
248 $samplebranch1->{branchcode},
249 $samplecat->{categorycode}
251 { patron_maxissueqty => 5, patron_maxonsiteissueqty => 6 },
252 "GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty of the branch1 and the category1"
255 GetBranchBorrowerCircRule( -1, -1 ),
256 { patron_maxissueqty => 4, patron_maxonsiteissueqty => 5 },
257 "GetBranchBorrower with wrong parameters returns the patron_maxissueqty and patron_maxonsiteissueqty of default_circ_rules"
260 #Test GetBranchItemRule
261 my @lazy_any = ( 'hold_fulfillment_policy' => 'any' );
264 $samplebranch1->{branchcode},
265 $sampleitemtype1->{itemtype},
267 { holdallowed => 'invalid_value', @lazy_any },
268 "GetBranchitem returns holdallowed and return branch"
272 { holdallowed => 'from_local_hold_group', @lazy_any },
273 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
276 GetBranchItemRule( $samplebranch2->{branchcode} ),
277 { holdallowed => 'from_home_library', @lazy_any },
278 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
281 GetBranchItemRule( -1, -1 ),
282 { holdallowed => 'from_local_hold_group', @lazy_any },
283 "With only one parametern GetBranchItemRule returns default values"
286 # Test return policies
287 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
289 # item1 returned at branch2 should trigger transfer to homebranch
291 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
292 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
294 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
295 my $log_count_before = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
296 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
297 $samplebranch2->{branchcode});
298 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
299 my $log_count_after = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
300 is($log_count_before, $log_count_after, "Update to holdingbranch is not logged");
302 # item2 returned at branch2 should trigger transfer to holding branch
304 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
305 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
306 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
307 $samplebranch2->{branchcode});
308 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
310 # Generate the transfer from above
311 ModItemTransfer($item_id2, $samplebranch2->{branchcode}, $samplebranch1->{branchcode}, "ReturnToHolding");
313 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',$samplebranch1->{branchcode});
314 is( $messages->{NeedsTransfer}, undef, "AddReturn does not generate a new transfer for return policy when resolving an existing non-Reserve transfer" );
316 # Generate a hold caused transfer which doesn't have a hold i.e. is the hold is cancelled
317 ModItemTransfer($item_id2, $samplebranch2->{branchcode}, $samplebranch1->{branchcode}, "Reserve");
319 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',$samplebranch1->{branchcode});
320 is( $messages->{NeedsTransfer}, $samplebranch2->{branchcode}, "AddReturn generates a new transfer for hold transfer if the hold was cancelled" );
322 # item3 should not trigger transfer - floating collection
324 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
325 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
326 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
327 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
328 $samplebranch1->{branchcode});
329 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
330 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
332 $schema->storage->txn_rollback;
334 subtest "GetBranchItemRule() tests" => sub {
337 $schema->storage->txn_begin;
339 $dbh->do('DELETE FROM circulation_rules');
341 my $homebranch = $builder->build( { source => 'Branch' } )->{branchcode};
342 my $holdingbranch = $builder->build( { source => 'Branch' } )->{branchcode};
343 my $checkinbranch = $builder->build( { source => 'Branch' } )->{branchcode};
345 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
346 t::lib::Mocks::mock_userenv(
349 branchcode => $checkinbranch,
353 my $biblio = $builder->build_sample_biblio;
354 my $item = Koha::Item->new(
356 biblionumber => $biblio->id,
357 homebranch => $homebranch,
358 holdingbranch => $holdingbranch,
359 itype => $sampleitemtype1->{itemtype}
363 Koha::CirculationRules->set_rule(
365 branchcode => $homebranch,
367 rule_name => 'returnbranch',
368 rule_value => 'homebranch',
372 Koha::CirculationRules->set_rule(
374 branchcode => $holdingbranch,
376 rule_name => 'returnbranch',
377 rule_value => 'holdingbranch',
381 Koha::CirculationRules->set_rule(
383 branchcode => $checkinbranch,
385 rule_name => 'returnbranch',
386 rule_value => 'noreturn',
390 t::lib::Mocks::mock_preference( 'CircControlReturnsBranch', 'ItemHomeLibrary' );
391 is( Koha::CirculationRules->get_return_branch_policy($item), 'homebranch' );
393 t::lib::Mocks::mock_preference( 'CircControlReturnsBranch', 'ItemHoldingLibrary' );
394 is( Koha::CirculationRules->get_return_branch_policy($item), 'holdingbranch' );
396 t::lib::Mocks::mock_preference( 'CircControlReturnsBranch', 'CheckInLibrary' );
397 is( Koha::CirculationRules->get_return_branch_policy($item), 'noreturn' );
399 $schema->storage->txn_rollback;