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 => 48;
21 use DateTime::Duration;
24 use t::lib::TestBuilder;
38 use Koha::CirculationRules;
42 require_ok('C4::Circulation');
60 my $schema = Koha::Database->schema;
61 $schema->storage->txn_begin;
62 my $dbh = C4::Context->dbh;
64 my $builder = t::lib::TestBuilder->new();
66 $dbh->do(q|DELETE FROM issues|);
67 $dbh->do(q|DELETE FROM items|);
68 $dbh->do(q|DELETE FROM borrowers|);
69 $dbh->do(q|DELETE FROM categories|);
70 $dbh->do(q|DELETE FROM accountlines|);
71 $dbh->do(q|DELETE FROM circulation_rules|);
72 $dbh->do(q|DELETE FROM reserves|);
73 $dbh->do(q|DELETE FROM old_reserves|);
74 $dbh->do(q|DELETE FROM statistics|);
76 # Generate sample datas
77 my $itemtype = $builder->build(
78 { source => 'Itemtype',
79 value => { notforloan => undef, rentalcharge => 0 }
82 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
84 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
85 my $categorycode = $builder->build({
87 value => { enrolmentfee => undef }
90 # A default issuingrule should always be present
91 Koha::CirculationRules->set_rules(
106 my $dt_today = dt_from_string;
107 my $today = output_pref(
110 timeformat => '24hr',
115 my $dt_today2 = dt_from_string;
116 my $dur10 = DateTime::Duration->new( days => -10 );
117 $dt_today2->add_duration($dur10);
118 my $daysago10 = output_pref(
121 timeformat => '24hr',
126 # Add biblio and item
127 my $record = MARC::Record->new();
128 $record->append_fields(
129 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
131 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
133 my $barcode_1 = 'barcode_1';
134 my $barcode_2 = 'barcode_2';
135 my $item_id1 = Koha::Item->new(
137 biblionumber => $biblionumber,
138 barcode => $barcode_1,
139 itemcallnumber => 'callnumber1',
140 homebranch => $branchcode_1,
141 holdingbranch => $branchcode_1,
144 )->store->itemnumber;
145 my $item_id2 = Koha::Item->new(
147 biblionumber => $biblionumber,
148 barcode => $barcode_2,
149 itemcallnumber => 'callnumber2',
150 homebranch => $branchcode_2,
151 holdingbranch => $branchcode_2,
155 )->store->itemnumber;
158 my $borrower_id1 = Koha::Patron->new({
159 firstname => 'firstname1',
160 surname => 'surname1 ',
161 categorycode => $categorycode,
162 branchcode => $branchcode_1
163 })->store->borrowernumber;
164 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
165 my $borrower_1 = $patron_1->unblessed;
166 my $borrower_id2 = Koha::Patron->new({
167 firstname => 'firstname2',
168 surname => 'surname2 ',
169 categorycode => $categorycode,
170 branchcode => $branchcode_2,
171 })->store->borrowernumber;
172 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
173 my $borrower_2 = $patron_2->unblessed;
175 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
180 my $query = " SELECT count(*) FROM issues";
181 my $sth = $dbh->prepare($query);
183 my $countissue = $sth -> fetchrow_array;
184 is ($countissue ,0, "there is no issue");
185 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
186 is( ref $issue1, 'Koha::Checkout',
187 'AddIssue returns a Koha::Checkout object' );
188 my $datedue1 = dt_from_string( $issue1->date_due() );
191 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
192 "Koha::Checkout->date_due() returns a date"
194 my $issue_id1 = $issue1->issue_id;
196 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
197 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
200 $countissue = $sth -> fetchrow_array;
201 is ($countissue,1,"1 issues have been added");
203 #Test AddIssuingCharge
204 $query = " SELECT count(*) FROM accountlines";
205 $sth = $dbh->prepare($query);
207 my $countaccount = $sth->fetchrow_array;
208 is ($countaccount,0,"0 accountline exists");
209 my $checkout = Koha::Checkouts->find( $issue_id1 );
210 my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10, 'RENT' );
211 is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
212 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
213 my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
214 is( $offset->credit_id, undef, 'Offset was created');
216 $countaccount = $sth->fetchrow_array;
217 is ($countaccount,1,"1 accountline has been added");
221 my $se = Test::MockModule->new( 'C4::Context' );
222 $se->mock( 'interface', sub {return 'intranet'});
224 # Let's renew this one at a different library for statistical purposes to test Bug 17781
225 # Mocking userenv with a different branchcode
226 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
228 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
230 # Restoring the userenv with the original branchcode
231 t::lib::Mocks::mock_userenv({ patron => $patron_1});
235 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
236 "AddRenewal returns a date"
239 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
240 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
242 subtest 'Show that AddRenewal respects OpacRenewalBranch and interface' => sub {
245 my $item_library = $builder->build_object( { class => 'Koha::Libraries' } );
246 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
247 my $logged_in_user = $builder->build_object( { class => 'Koha::Patrons' } );
248 t::lib::Mocks::mock_userenv( { patron => $logged_in_user } );
250 my $OpacRenewalBranch = {
251 opacrenew => "OPACRenew",
252 checkoutbranch => $logged_in_user->branchcode,
253 patronhomebranch => $patron->branchcode,
254 itemhomebranch => $item_library->branchcode,
258 while ( my ( $syspref, $expected_branchcode ) = each %$OpacRenewalBranch ) {
260 t::lib::Mocks::mock_preference( 'OpacRenewalBranch', $syspref );
263 $se->mock( 'interface', sub { return 'opac' } );
265 my $item = $builder->build_sample_item(
266 { library => $item_library->branchcode, itype => $itemtype } );
267 my $opac_renew_issue =
268 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
270 AddRenewal( $patron->borrowernumber, $item->itemnumber,
271 "Stavromula", $datedue1, $daysago10 );
273 my $stat = Koha::Statistics->search(
274 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
275 is( $stat->branch, $expected_branchcode,
276 "->renewal_branchcode is respected for OpacRenewalBranch = $syspref"
281 $se->mock( 'interface', sub { return 'intranet' } );
283 my $item = $builder->build_sample_item(
284 { library => $item_library->branchcode, itype => $itemtype } );
285 my $opac_renew_issue =
286 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
288 AddRenewal( $patron->borrowernumber, $item->itemnumber,
289 "Stavromula", $datedue1, $daysago10 );
291 my $stat = Koha::Statistics->search(
292 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
293 is( $stat->branch, $logged_in_user->branchcode,
294 "->renewal_branchcode is always logged in branch for intranet"
300 #Test GetBiblioIssues
301 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
304 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
305 is( GetOpenIssue(-1), undef,
306 "With wrong parameter GetOpenIssue returns undef" );
307 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
311 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
312 #Without anything in DB
313 @renewcount = C4::Circulation::GetRenewCount();
316 [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
317 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
319 @renewcount = C4::Circulation::GetRenewCount(-1);
322 [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed
323 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
325 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
328 [ 2, 0, 0, 0, 0, 0 ],
329 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
332 #With something in DB
333 @renewcount = C4::Circulation::GetRenewCount();
336 [ 0, 0, 0, 0, 0, 0 ],
337 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
339 @renewcount = C4::Circulation::GetRenewCount(-1);
342 [ 0, 0, 0, 0, 0, 0 ],
343 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
345 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
348 [ 2, 0, 0, 0, 0, 0 ],
349 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
352 # Add a default rule: renewal is allowed
353 Koha::CirculationRules->set_rules(
355 categorycode => undef,
359 renewalsallowed => 3,
363 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
366 [ 2, 3, 1, 0, 0, 0 ],
367 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
370 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
371 $datedue3, $daysago10 );
372 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
375 [ 3, 3, 0, 0, 0, 0 ],
376 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
379 $dbh->do("DELETE FROM old_issues");
380 AddReturn($barcode_1);
381 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
382 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
384 $dbh->do("DELETE FROM old_issues");
385 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
386 AddReturn($barcode_1, undef, undef, dt_from_string('2014-04-01 23:42'));
387 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
388 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" );
390 my $itemnumber = Koha::Item->new(
392 biblionumber => $biblionumber,
393 barcode => 'barcode_3',
394 itemcallnumber => 'callnumber3',
395 homebranch => $branchcode_1,
396 holdingbranch => $branchcode_1,
400 )->store->itemnumber;
402 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
403 AddReturn( 'barcode_3', $branchcode_1 );
404 my $item = Koha::Items->find( $itemnumber );
405 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
407 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
408 AddReturn( 'barcode_3', $branchcode_1 );
409 $item = Koha::Items->find( $itemnumber );
410 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
412 AddReturn( 'barcode_3', $branchcode_1 );
413 $item = Koha::Items->find( $itemnumber );
414 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
416 my $itemnumber2 = Koha::Item->new(
418 biblionumber => $biblionumber,
419 barcode => 'barcode_4',
420 itemcallnumber => 'callnumber4',
421 homebranch => $branchcode_1,
422 holdingbranch => $branchcode_1,
426 )->store->itemnumber;
428 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
429 AddReturn( 'barcode_4', $branchcode_1 );
430 my $item2 = Koha::Items->find( $itemnumber2 );
431 ok( $item2->location eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
433 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
434 AddReturn( 'barcode_4', $branchcode_1 );
435 $item2 = Koha::Items->find( $itemnumber2 );
436 is( $item2->location, 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
437 is( $item2->permanent_location, 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
438 AddReturn( 'barcode_4', $branchcode_1 );
439 $item2 = Koha::Items->find( $itemnumber2 );
440 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
442 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
443 AddReturn( 'barcode_4', $branchcode_1 );
444 $item2 = Koha::Items->find( $itemnumber2 );
445 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
446 Koha::Item::Transfer->new({
447 itemnumber => $itemnumber2,
448 frombranch => $branchcode_2,
449 tobranch => $branchcode_1,
450 datesent => '2020-01-01'
452 AddReturn( 'barcode_4', $branchcode_1 );
453 $item2 = Koha::Items->find( $itemnumber2 );
454 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART" when transfer filled} );
456 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
457 AddIssue( $borrower_1, 'barcode_4', $daysago10,0, $today, '' );
458 $item2 = Koha::Items->find( $itemnumber2 );
459 ok( $item2->location eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
461 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
462 AddReturn( 'barcode_4', $branchcode_1 );
463 $item2 = Koha::Items->find( $itemnumber2 );
464 ok( $item2->location eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
465 AddReturn( 'barcode_4', $branchcode_1 );
466 $item2 = Koha::Items->find( $itemnumber2 );
467 ok( $item2->location eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
468 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
469 AddReturn( 'barcode_4', $branchcode_1 );
470 $item2 = Koha::Items->find( $itemnumber2 );
471 ok( $item2->location eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
472 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
477 # Bug 14640 - Cancel the hold on checking out if asked
478 my $reserve_id = AddReserve(
480 branchcode => $branchcode_1,
481 borrowernumber => $borrower_id1,
482 biblionumber => $biblionumber,
488 ok( $reserve_id, 'The reserve should have been inserted' );
489 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
490 my $hold = Koha::Holds->find( $reserve_id );
491 is( $hold, undef, 'The reserve should have been correctly cancelled' );
494 t::lib::Mocks::mock_preference('UnseenRenewals', 1);
495 # Add a default circ rule: 3 unseen renewals allowed
496 Koha::CirculationRules->set_rules(
498 categorycode => undef,
502 renewalsallowed => 10,
503 unseen_renewals_allowed => 3
508 my $unseen_library = $builder->build_object( { class => 'Koha::Libraries' } );
509 my $unseen_patron = $builder->build_object( { class => 'Koha::Patrons' } );
510 my $unseen_item = $builder->build_sample_item(
511 { library => $unseen_library->branchcode, itype => $itemtype } );
512 my $unseen_issue = C4::Circulation::AddIssue( $unseen_patron->unblessed, $unseen_item->barcode );
514 # Does an unseen renewal increment the issue's count
515 my ( $unseen_before ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
516 AddRenewal( $unseen_patron->borrowernumber, $unseen_item->itemnumber, $branchcode_1, undef, undef, undef, 0 );
517 my ( $unseen_after ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
518 is( $unseen_after, $unseen_before + 1, 'unseen_renewals increments' );
520 # Does a seen renewal reset the unseen count
521 AddRenewal( $unseen_patron->borrowernumber, $unseen_item->itemnumber, $branchcode_1, undef, undef, undef, 1 );
522 my ( $unseen_reset ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
523 is( $unseen_reset, 0, 'seen renewal resets the unseen count' );
526 $schema->storage->txn_rollback;