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 Test::More tests => 47;
21 use DateTime::Duration;
24 use t::lib::TestBuilder;
38 use Koha::CirculationRules;
42 require_ok('C4::Circulation');
59 my $schema = Koha::Database->schema;
60 $schema->storage->txn_begin;
61 my $dbh = C4::Context->dbh;
63 my $builder = t::lib::TestBuilder->new();
65 $dbh->do(q|DELETE FROM issues|);
66 $dbh->do(q|DELETE FROM items|);
67 $dbh->do(q|DELETE FROM borrowers|);
68 $dbh->do(q|DELETE FROM categories|);
69 $dbh->do(q|DELETE FROM accountlines|);
70 $dbh->do(q|DELETE FROM circulation_rules|);
71 $dbh->do(q|DELETE FROM reserves|);
72 $dbh->do(q|DELETE FROM old_reserves|);
73 $dbh->do(q|DELETE FROM statistics|);
75 # Generate sample datas
76 my $itemtype = $builder->build(
77 { source => 'Itemtype',
78 value => { notforloan => undef, rentalcharge => 0 }
81 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
82 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
84 my $categorycode = $builder->build({
86 value => { enrolmentfee => undef }
89 # A default issuingrule should always be present
90 Koha::CirculationRules->set_rules(
105 my $dt_today = dt_from_string;
106 my $today = output_pref(
109 timeformat => '24hr',
114 my $dt_today2 = dt_from_string;
115 my $dur10 = DateTime::Duration->new( days => -10 );
116 $dt_today2->add_duration($dur10);
117 my $daysago10 = output_pref(
120 timeformat => '24hr',
125 # Add biblio and item
126 my $record = MARC::Record->new();
127 $record->append_fields(
128 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
130 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
132 my $barcode_1 = 'barcode_1';
133 my $barcode_2 = 'barcode_2';
134 my $item_id1 = Koha::Item->new(
136 biblionumber => $biblionumber,
137 barcode => $barcode_1,
138 itemcallnumber => 'callnumber1',
139 homebranch => $branchcode_1,
140 holdingbranch => $branchcode_1,
143 )->store->itemnumber;
144 my $item_id2 = Koha::Item->new(
146 biblionumber => $biblionumber,
147 barcode => $barcode_2,
148 itemcallnumber => 'callnumber2',
149 homebranch => $branchcode_2,
150 holdingbranch => $branchcode_2,
154 )->store->itemnumber;
157 my $borrower_id1 = Koha::Patron->new({
158 firstname => 'firstname1',
159 surname => 'surname1 ',
160 categorycode => $categorycode,
161 branchcode => $branchcode_1
162 })->store->borrowernumber;
163 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
164 my $borrower_1 = $patron_1->unblessed;
165 my $borrower_id2 = Koha::Patron->new({
166 firstname => 'firstname2',
167 surname => 'surname2 ',
168 categorycode => $categorycode,
169 branchcode => $branchcode_2,
170 })->store->borrowernumber;
171 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
172 my $borrower_2 = $patron_2->unblessed;
174 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
179 my $query = " SELECT count(*) FROM issues";
180 my $sth = $dbh->prepare($query);
182 my $countissue = $sth -> fetchrow_array;
183 is ($countissue ,0, "there is no issue");
184 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
185 is( ref $issue1, 'Koha::Checkout',
186 'AddIssue returns a Koha::Checkout object' );
187 my $datedue1 = dt_from_string( $issue1->date_due() );
190 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
191 "Koha::Checkout->date_due() returns a date"
193 my $issue_id1 = $issue1->issue_id;
195 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
196 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
199 $countissue = $sth -> fetchrow_array;
200 is ($countissue,1,"1 issues have been added");
202 #Test AddIssuingCharge
203 $query = " SELECT count(*) FROM accountlines";
204 $sth = $dbh->prepare($query);
206 my $countaccount = $sth->fetchrow_array;
207 is ($countaccount,0,"0 accountline exists");
208 my $checkout = Koha::Checkouts->find( $issue_id1 );
209 my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10, 'RENT' );
210 is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
211 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
212 my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
213 is( $offset->credit_id, undef, 'Offset was created');
215 $countaccount = $sth->fetchrow_array;
216 is ($countaccount,1,"1 accountline has been added");
220 my $se = Test::MockModule->new( 'C4::Context' );
221 $se->mock( 'interface', sub {return 'intranet'});
223 # Let's renew this one at a different library for statistical purposes to test Bug 17781
224 # Mocking userenv with a different branchcode
225 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
227 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
229 # Restoring the userenv with the original branchcode
230 t::lib::Mocks::mock_userenv({ patron => $patron_1});
234 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
235 "AddRenewal returns a date"
238 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
239 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
241 subtest 'Show that AddRenewal respects OpacRenewalBranch and interface' => sub {
244 my $item_library = $builder->build_object( { class => 'Koha::Libraries' } );
245 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
246 my $logged_in_user = $builder->build_object( { class => 'Koha::Patrons' } );
247 t::lib::Mocks::mock_userenv( { patron => $logged_in_user } );
249 my $OpacRenewalBranch = {
250 opacrenew => "OPACRenew",
251 checkoutbranch => $logged_in_user->branchcode,
252 patronhomebranch => $patron->branchcode,
253 itemhomebranch => $item_library->branchcode,
257 while ( my ( $syspref, $expected_branchcode ) = each %$OpacRenewalBranch ) {
259 t::lib::Mocks::mock_preference( 'OpacRenewalBranch', $syspref );
262 $se->mock( 'interface', sub { return 'opac' } );
264 my $item = $builder->build_sample_item(
265 { library => $item_library->branchcode, itype => $itemtype } );
266 my $opac_renew_issue =
267 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
269 AddRenewal( $patron->borrowernumber, $item->itemnumber,
270 "Stavromula", $datedue1, $daysago10 );
272 my $stat = Koha::Statistics->search(
273 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
274 is( $stat->branch, $expected_branchcode,
275 "->renewal_branchcode is respected for OpacRenewalBranch = $syspref"
280 $se->mock( 'interface', sub { return 'intranet' } );
282 my $item = $builder->build_sample_item(
283 { library => $item_library->branchcode, itype => $itemtype } );
284 my $opac_renew_issue =
285 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
287 AddRenewal( $patron->borrowernumber, $item->itemnumber,
288 "Stavromula", $datedue1, $daysago10 );
290 my $stat = Koha::Statistics->search(
291 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
292 is( $stat->branch, $logged_in_user->branchcode,
293 "->renewal_branchcode is always logged in branch for intranet"
300 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
301 is( GetOpenIssue(-1), undef,
302 "With wrong parameter GetOpenIssue returns undef" );
303 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
307 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
308 #Without anything in DB
309 @renewcount = C4::Circulation::GetRenewCount();
312 [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
313 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
315 @renewcount = C4::Circulation::GetRenewCount(-1);
318 [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed
319 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
321 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
324 [ 2, 0, 0, 0, 0, 0 ],
325 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
328 #With something in DB
329 @renewcount = C4::Circulation::GetRenewCount();
332 [ 0, 0, 0, 0, 0, 0 ],
333 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
335 @renewcount = C4::Circulation::GetRenewCount(-1);
338 [ 0, 0, 0, 0, 0, 0 ],
339 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
341 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
344 [ 2, 0, 0, 0, 0, 0 ],
345 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
348 # Add a default rule: renewal is allowed
349 Koha::CirculationRules->set_rules(
351 categorycode => undef,
355 renewalsallowed => 3,
359 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
362 [ 2, 3, 1, 0, 0, 0 ],
363 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
366 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
367 $datedue3, $daysago10 );
368 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
371 [ 3, 3, 0, 0, 0, 0 ],
372 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
375 $dbh->do("DELETE FROM old_issues");
376 AddReturn($barcode_1);
377 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
378 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
380 $dbh->do("DELETE FROM old_issues");
381 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
382 AddReturn($barcode_1, undef, undef, dt_from_string('2014-04-01 23:42'));
383 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
384 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
386 my $itemnumber = Koha::Item->new(
388 biblionumber => $biblionumber,
389 barcode => 'barcode_3',
390 itemcallnumber => 'callnumber3',
391 homebranch => $branchcode_1,
392 holdingbranch => $branchcode_1,
396 )->store->itemnumber;
398 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
399 AddReturn( 'barcode_3', $branchcode_1 );
400 my $item = Koha::Items->find( $itemnumber );
401 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
403 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
404 AddReturn( 'barcode_3', $branchcode_1 );
405 $item = Koha::Items->find( $itemnumber );
406 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
408 AddReturn( 'barcode_3', $branchcode_1 );
409 $item = Koha::Items->find( $itemnumber );
410 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
412 my $itemnumber2 = Koha::Item->new(
414 biblionumber => $biblionumber,
415 barcode => 'barcode_4',
416 itemcallnumber => 'callnumber4',
417 homebranch => $branchcode_1,
418 holdingbranch => $branchcode_1,
422 )->store->itemnumber;
424 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
425 AddReturn( 'barcode_4', $branchcode_1 );
426 my $item2 = Koha::Items->find( $itemnumber2 );
427 ok( $item2->location eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
429 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
430 AddReturn( 'barcode_4', $branchcode_1 );
431 $item2 = Koha::Items->find( $itemnumber2 );
432 is( $item2->location, 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
433 is( $item2->permanent_location, 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
434 AddReturn( 'barcode_4', $branchcode_1 );
435 $item2 = Koha::Items->find( $itemnumber2 );
436 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
438 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
439 AddReturn( 'barcode_4', $branchcode_1 );
440 $item2 = Koha::Items->find( $itemnumber2 );
441 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
442 Koha::Item::Transfer->new({
443 itemnumber => $itemnumber2,
444 frombranch => $branchcode_2,
445 tobranch => $branchcode_1,
446 datesent => '2020-01-01'
448 AddReturn( 'barcode_4', $branchcode_1 );
449 $item2 = Koha::Items->find( $itemnumber2 );
450 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART" when transfer filled} );
452 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
453 AddIssue( $borrower_1, 'barcode_4', $daysago10,0, $today, '' );
454 $item2 = Koha::Items->find( $itemnumber2 );
455 ok( $item2->location eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
457 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
458 AddReturn( 'barcode_4', $branchcode_1 );
459 $item2 = Koha::Items->find( $itemnumber2 );
460 ok( $item2->location eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
461 AddReturn( 'barcode_4', $branchcode_1 );
462 $item2 = Koha::Items->find( $itemnumber2 );
463 ok( $item2->location eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
464 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
465 AddReturn( 'barcode_4', $branchcode_1 );
466 $item2 = Koha::Items->find( $itemnumber2 );
467 ok( $item2->location eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
468 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
473 # Bug 14640 - Cancel the hold on checking out if asked
474 my $reserve_id = AddReserve(
476 branchcode => $branchcode_1,
477 borrowernumber => $borrower_id1,
478 biblionumber => $biblionumber,
484 ok( $reserve_id, 'The reserve should have been inserted' );
485 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
486 my $hold = Koha::Holds->find( $reserve_id );
487 is( $hold, undef, 'The reserve should have been correctly cancelled' );
490 t::lib::Mocks::mock_preference('UnseenRenewals', 1);
491 # Add a default circ rule: 3 unseen renewals allowed
492 Koha::CirculationRules->set_rules(
494 categorycode => undef,
498 renewalsallowed => 10,
499 unseen_renewals_allowed => 3
504 my $unseen_library = $builder->build_object( { class => 'Koha::Libraries' } );
505 my $unseen_patron = $builder->build_object( { class => 'Koha::Patrons' } );
506 my $unseen_item = $builder->build_sample_item(
507 { library => $unseen_library->branchcode, itype => $itemtype } );
508 my $unseen_issue = C4::Circulation::AddIssue( $unseen_patron->unblessed, $unseen_item->barcode );
510 # Does an unseen renewal increment the issue's count
511 my ( $unseen_before ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
512 AddRenewal( $unseen_patron->borrowernumber, $unseen_item->itemnumber, $branchcode_1, undef, undef, undef, 0 );
513 my ( $unseen_after ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
514 is( $unseen_after, $unseen_before + 1, 'unseen_renewals increments' );
516 # Does a seen renewal reset the unseen count
517 AddRenewal( $unseen_patron->borrowernumber, $unseen_item->itemnumber, $branchcode_1, undef, undef, undef, 1 );
518 my ( $unseen_reset ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
519 is( $unseen_reset, 0, 'seen renewal resets the unseen count' );
522 $schema->storage->txn_rollback;