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 => 95;
25 use t::lib::TestBuilder;
33 use C4::Overdues qw(UpdateFine CalcFine);
36 use Koha::IssuingRules;
37 use Koha::Subscriptions;
39 my $schema = Koha::Database->schema;
40 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
42 my $dbh = C4::Context->dbh;
45 $dbh->{RaiseError} = 1;
47 # Start with a clean slate
48 $dbh->do('DELETE FROM issues');
50 my $library = $builder->build({
53 my $library2 = $builder->build({
56 my $itemtype = $builder->build(
57 { source => 'Itemtype',
58 value => { notforloan => undef, rentalcharge => 0 }
61 my $patron_category = $builder->build({ source => 'Category', value => { enrolmentfee => 0 } });
63 my $CircControl = C4::Context->preference('CircControl');
64 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
67 homebranch => $library2->{branchcode},
68 holdingbranch => $library2->{branchcode}
72 branchcode => $library2->{branchcode}
75 # No userenv, PickupLibrary
76 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
78 C4::Context->preference('CircControl'),
80 'CircControl changed to PickupLibrary'
83 C4::Circulation::_GetCircControlBranch($item, $borrower),
84 $item->{$HomeOrHoldingBranch},
85 '_GetCircControlBranch returned item branch (no userenv defined)'
88 # No userenv, PatronLibrary
89 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
91 C4::Context->preference('CircControl'),
93 'CircControl changed to PatronLibrary'
96 C4::Circulation::_GetCircControlBranch($item, $borrower),
97 $borrower->{branchcode},
98 '_GetCircControlBranch returned borrower branch'
101 # No userenv, ItemHomeLibrary
102 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
104 C4::Context->preference('CircControl'),
106 'CircControl changed to ItemHomeLibrary'
109 $item->{$HomeOrHoldingBranch},
110 C4::Circulation::_GetCircControlBranch($item, $borrower),
111 '_GetCircControlBranch returned item branch'
115 C4::Context->_new_userenv('xxx');
116 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
117 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
119 # Userenv set, PickupLibrary
120 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
122 C4::Context->preference('CircControl'),
124 'CircControl changed to PickupLibrary'
127 C4::Circulation::_GetCircControlBranch($item, $borrower),
128 $library2->{branchcode},
129 '_GetCircControlBranch returned current branch'
132 # Userenv set, PatronLibrary
133 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
135 C4::Context->preference('CircControl'),
137 'CircControl changed to PatronLibrary'
140 C4::Circulation::_GetCircControlBranch($item, $borrower),
141 $borrower->{branchcode},
142 '_GetCircControlBranch returned borrower branch'
145 # Userenv set, ItemHomeLibrary
146 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
148 C4::Context->preference('CircControl'),
150 'CircControl changed to ItemHomeLibrary'
153 C4::Circulation::_GetCircControlBranch($item, $borrower),
154 $item->{$HomeOrHoldingBranch},
155 '_GetCircControlBranch returned item branch'
158 # Reset initial configuration
159 t::lib::Mocks::mock_preference('CircControl', $CircControl);
161 C4::Context->preference('CircControl'),
163 'CircControl reset to its initial value'
166 # Set a simple circ policy
167 $dbh->do('DELETE FROM issuingrules');
169 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
170 maxissueqty, issuelength, lengthunit,
171 renewalsallowed, renewalperiod,
172 norenewalbefore, auto_renew,
189 # Test C4::Circulation::ProcessOfflinePayment
190 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
192 my ( $original_count ) = $sth->fetchrow_array();
194 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', ?, ? )", undef, $patron_category->{categorycode}, $library2->{branchcode} );
196 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
199 my ( $new_count ) = $sth->fetchrow_array();
201 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
203 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
204 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
205 C4::Context->dbh->do("DELETE FROM accountlines");
207 # CanBookBeRenewed tests
209 # Generate test biblio
210 my $biblio = MARC::Record->new();
211 my $title = 'Silence in the library';
212 $biblio->append_fields(
213 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
214 MARC::Field->new('245', ' ', ' ', a => $title),
217 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
219 my $barcode = 'R00000342';
220 my $branch = $library2->{branchcode};
222 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
224 homebranch => $branch,
225 holdingbranch => $branch,
227 replacementprice => 12.00,
233 my $barcode2 = 'R00000343';
234 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
236 homebranch => $branch,
237 holdingbranch => $branch,
238 barcode => $barcode2,
239 replacementprice => 23.00,
245 my $barcode3 = 'R00000346';
246 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
248 homebranch => $branch,
249 holdingbranch => $branch,
250 barcode => $barcode3,
251 replacementprice => 23.00,
261 my %renewing_borrower_data = (
263 surname => 'Renewal',
264 categorycode => $patron_category->{categorycode},
265 branchcode => $branch,
268 my %reserving_borrower_data = (
269 firstname => 'Katrin',
270 surname => 'Reservation',
271 categorycode => $patron_category->{categorycode},
272 branchcode => $branch,
275 my %hold_waiting_borrower_data = (
277 surname => 'Reservation',
278 categorycode => $patron_category->{categorycode},
279 branchcode => $branch,
282 my %restricted_borrower_data = (
283 firstname => 'Alice',
284 surname => 'Reservation',
285 categorycode => $patron_category->{categorycode},
286 debarred => '3228-01-01',
287 branchcode => $branch,
290 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
291 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
292 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
293 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
295 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
296 my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
303 my $checkitem = undef;
306 my $issue = AddIssue( $renewing_borrower, $barcode);
307 my $datedue = dt_from_string( $issue->date_due() );
308 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
310 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
311 $datedue = dt_from_string( $issue->date_due() );
312 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
315 my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
316 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
318 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
319 is( $renewokay, 1, 'Can renew, no holds for this title or item');
322 # Biblio-level hold, renewal test
324 $branch, $reserving_borrowernumber, $biblionumber,
325 $bibitems, $priority, $resdate, $expdate, $notes,
326 $title, $checkitem, $found
329 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
330 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
331 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
332 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
333 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
334 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
335 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
337 # Now let's add an item level hold, we should no longer be able to renew the item
338 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
340 borrowernumber => $hold_waiting_borrowernumber,
341 biblionumber => $biblionumber,
342 itemnumber => $itemnumber,
343 branchcode => $branch,
347 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
348 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
351 # Now let's add a waiting hold on the 3rd item, it's no longer available tp check out by just anyone, so we should no longer
352 # be able to renew these items
353 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
355 borrowernumber => $hold_waiting_borrowernumber,
356 biblionumber => $biblionumber,
357 itemnumber => $itemnumber3,
358 branchcode => $branch,
363 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
364 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
365 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
366 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
367 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
369 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
370 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
371 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
373 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
374 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
375 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
377 my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
378 my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
379 AddIssue($reserving_borrower, $barcode3);
380 my $reserve = $dbh->selectrow_hashref(
381 'SELECT * FROM old_reserves WHERE reserve_id = ?',
385 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
387 # Item-level hold, renewal test
389 $branch, $reserving_borrowernumber, $biblionumber,
390 $bibitems, $priority, $resdate, $expdate, $notes,
391 $title, $itemnumber, $found
394 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
395 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
396 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
398 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
399 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
401 # Items can't fill hold for reasons
402 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
403 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
404 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
405 ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber,1);
407 # FIXME: Add more for itemtype not for loan etc.
409 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
410 my $barcode5 = 'R00000347';
411 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
413 homebranch => $branch,
414 holdingbranch => $branch,
415 barcode => $barcode5,
416 replacementprice => 23.00,
421 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
422 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
424 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
425 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
426 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
427 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
428 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
430 # Users cannot renew an overdue item
431 my $barcode6 = 'R00000348';
432 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
434 homebranch => $branch,
435 holdingbranch => $branch,
436 barcode => $barcode6,
437 replacementprice => 23.00,
443 my $barcode7 = 'R00000349';
444 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
446 homebranch => $branch,
447 holdingbranch => $branch,
448 barcode => $barcode7,
449 replacementprice => 23.00,
454 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
455 is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
457 my $now = dt_from_string();
458 my $five_weeks = DateTime::Duration->new(weeks => 5);
459 my $five_weeks_ago = $now - $five_weeks;
461 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
462 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
464 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
465 C4::Overdues::UpdateFine(
467 issue_id => $passeddatedue1->id(),
468 itemnumber => $itemnumber7,
469 borrowernumber => $renewing_borrower->{borrowernumber},
472 due => Koha::DateUtils::output_pref($five_weeks_ago)
475 t::lib::Mocks::mock_preference('RenewalLog', 0);
476 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
477 my $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
478 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
479 my $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
480 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
482 t::lib::Mocks::mock_preference('RenewalLog', 1);
483 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
484 $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
485 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
486 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
487 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
490 $fine = $schema->resultset('Accountline')->single( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
491 is( $fine->accounttype, 'F', 'Fine on renewed item is closed out properly' );
494 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
495 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
496 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
497 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
498 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
501 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
502 CancelReserve({ reserve_id => $reserveid });
505 # Test automatic renewal before value for "norenewalbefore" in policy is set
506 # In this case automatic renewal is not permitted prior to due date
507 my $barcode4 = '11235813';
508 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
510 homebranch => $branch,
511 holdingbranch => $branch,
512 barcode => $barcode4,
513 replacementprice => 16.00,
519 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
520 ( $renewokay, $error ) =
521 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
522 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
523 is( $error, 'auto_too_soon',
524 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
527 # Test premature manual renewal
528 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
530 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
531 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
532 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
535 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
536 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
538 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
539 $datedue->clone->add( days => -7 ),
540 'Bug 14395: Renewals permitted 7 days before due date, as expected'
544 # Test 'date' setting for syspref NoRenewalBeforePrecision
545 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
547 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
548 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
549 'Bug 14395: Renewals permitted 7 days before due date, as expected'
553 # Test premature automatic renewal
554 ( $renewokay, $error ) =
555 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
556 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
557 is( $error, 'auto_too_soon',
558 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
561 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
562 # and test automatic renewal again
563 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
564 ( $renewokay, $error ) =
565 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
566 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
567 is( $error, 'auto_too_soon',
568 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
571 # Change policy so that loans can be renewed 99 days prior to the due date
572 # and test automatic renewal again
573 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
574 ( $renewokay, $error ) =
575 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
576 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
577 is( $error, 'auto_renew',
578 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
581 subtest "too_late_renewal / no_auto_renewal_after" => sub {
583 my $item_to_auto_renew = $builder->build(
586 biblionumber => $biblionumber,
587 homebranch => $branch,
588 holdingbranch => $branch,
593 my $ten_days_before = dt_from_string->add( days => -10 );
594 my $ten_days_ahead = dt_from_string->add( days => 10 );
595 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
597 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
598 ( $renewokay, $error ) =
599 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
600 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
601 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
603 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
604 ( $renewokay, $error ) =
605 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
606 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
607 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
609 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
610 ( $renewokay, $error ) =
611 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
612 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
613 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
615 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
616 ( $renewokay, $error ) =
617 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
618 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
619 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
621 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => -1 ) );
622 ( $renewokay, $error ) =
623 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
624 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
625 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
627 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => -1 ) );
628 ( $renewokay, $error ) =
629 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
630 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
631 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
633 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 1 ) );
634 ( $renewokay, $error ) =
635 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
636 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
637 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
640 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
642 my $item_to_auto_renew = $builder->build({
645 biblionumber => $biblionumber,
646 homebranch => $branch,
647 holdingbranch => $branch,
651 my $ten_days_before = dt_from_string->add( days => -10 );
652 my $ten_days_ahead = dt_from_string->add( days => 10 );
653 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
655 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
656 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
657 C4::Context->set_preference('OPACFineNoRenewals','10');
658 my $fines_amount = 5;
659 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
660 ( $renewokay, $error ) =
661 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
662 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
663 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
665 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
666 ( $renewokay, $error ) =
667 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
668 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
669 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
671 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
672 ( $renewokay, $error ) =
673 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
674 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
675 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
677 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
680 subtest "GetLatestAutoRenewDate" => sub {
682 my $item_to_auto_renew = $builder->build(
685 biblionumber => $biblionumber,
686 homebranch => $branch,
687 holdingbranch => $branch,
692 my $ten_days_before = dt_from_string->add( days => -10 );
693 my $ten_days_ahead = dt_from_string->add( days => 10 );
694 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
695 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = "", no_auto_renewal_after_hard_limit = NULL');
696 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
697 is( $latest_auto_renew_date, undef, 'GetLatestAutoRenewDate should return undef if no_auto_renewal_after or no_auto_renewal_after_hard_limit are not defined' );
698 my $five_days_before = dt_from_string->add( days => -5 );
699 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
700 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
701 is( $latest_auto_renew_date->truncate( to => 'minute' ),
702 $five_days_before->truncate( to => 'minute' ),
703 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
705 my $five_days_ahead = dt_from_string->add( days => 5 );
706 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
707 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
708 is( $latest_auto_renew_date->truncate( to => 'minute' ),
709 $five_days_ahead->truncate( to => 'minute' ),
710 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
712 my $two_days_ahead = dt_from_string->add( days => 2 );
713 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = "", no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 2 ) );
714 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
715 is( $latest_auto_renew_date->truncate( to => 'day' ),
716 $two_days_ahead->truncate( to => 'day' ),
717 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
719 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 2 ) );
720 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
721 is( $latest_auto_renew_date->truncate( to => 'day' ),
722 $two_days_ahead->truncate( to => 'day' ),
723 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
730 # set policy to forbid renewals
731 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
733 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
734 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
735 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
737 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
738 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
739 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
741 C4::Overdues::UpdateFine(
743 issue_id => $issue->id(),
744 itemnumber => $itemnumber,
745 borrowernumber => $renewing_borrower->{borrowernumber},
748 due => Koha::DateUtils::output_pref($datedue)
752 LostItem( $itemnumber, 1 );
754 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
755 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
757 my $total_due = $dbh->selectrow_array(
758 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
759 undef, $renewing_borrower->{borrowernumber}
762 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
764 C4::Context->dbh->do("DELETE FROM accountlines");
766 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
767 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
769 C4::Overdues::UpdateFine(
771 issue_id => $issue2->id(),
772 itemnumber => $itemnumber2,
773 borrowernumber => $renewing_borrower->{borrowernumber},
776 due => Koha::DateUtils::output_pref($datedue)
780 LostItem( $itemnumber2, 0 );
782 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
783 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
785 $total_due = $dbh->selectrow_array(
786 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
787 undef, $renewing_borrower->{borrowernumber}
790 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
792 my $future = dt_from_string();
793 $future->add( days => 7 );
794 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
795 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
797 # Users cannot renew any item if there is an overdue item
798 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
799 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
800 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
801 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
802 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
807 # GetUpcomingDueIssues tests
808 my $barcode = 'R00000342';
809 my $barcode2 = 'R00000343';
810 my $barcode3 = 'R00000344';
811 my $branch = $library2->{branchcode};
813 #Create another record
814 my $biblio2 = MARC::Record->new();
815 my $title2 = 'Something is worng here';
816 $biblio2->append_fields(
817 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
818 MARC::Field->new('245', ' ', ' ', a => $title2),
820 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
825 homebranch => $branch,
826 holdingbranch => $branch,
827 barcode => $barcode3,
834 my %a_borrower_data = (
835 firstname => 'Fridolyn',
837 categorycode => $patron_category->{categorycode},
838 branchcode => $branch,
841 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
842 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
844 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
845 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
846 my $today = DateTime->today(time_zone => C4::Context->tz());
848 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
849 my $datedue = dt_from_string( $issue->date_due() );
850 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
851 my $datedue2 = dt_from_string( $issue->date_due() );
855 # GetUpcomingDueIssues tests
857 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
858 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
861 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
862 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
863 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
866 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
867 is ( scalar( @$upcoming_dues ), 1,
868 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
871 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
873 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
875 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
876 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
878 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
879 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
881 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
882 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
884 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
885 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
887 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
888 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
890 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
891 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
896 my $barcode = '1234567890';
897 my $branch = $library2->{branchcode};
899 my $biblio = MARC::Record->new();
900 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
903 my ( undef, undef, $itemnumber ) = AddItem(
905 homebranch => $branch,
906 holdingbranch => $branch,
914 my %a_borrower_data = (
917 categorycode => $patron_category->{categorycode},
918 branchcode => $branch,
921 my $borrowernumber = AddMember(%a_borrower_data);
923 my $issue = AddIssue( GetMember( borrowernumber => $borrowernumber ), $barcode );
926 issue_id => $issue->id(),
927 itemnumber => $itemnumber,
928 borrowernumber => $borrowernumber,
934 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
935 my $count = $hr->{count};
937 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
941 $dbh->do('DELETE FROM issues');
942 $dbh->do('DELETE FROM items');
943 $dbh->do('DELETE FROM issuingrules');
946 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
947 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
956 my $biblio = MARC::Record->new();
957 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
959 my $barcode1 = '1234';
960 my ( undef, undef, $itemnumber1 ) = AddItem(
962 homebranch => $library2->{branchcode},
963 holdingbranch => $library2->{branchcode},
964 barcode => $barcode1,
969 my $barcode2 = '4321';
970 my ( undef, undef, $itemnumber2 ) = AddItem(
972 homebranch => $library2->{branchcode},
973 holdingbranch => $library2->{branchcode},
974 barcode => $barcode2,
980 my $borrowernumber1 = AddMember(
983 categorycode => $patron_category->{categorycode},
984 branchcode => $library2->{branchcode},
986 my $borrowernumber2 = AddMember(
987 firstname => 'Chelsea',
989 categorycode => $patron_category->{categorycode},
990 branchcode => $library2->{branchcode},
993 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
994 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
996 my $issue = AddIssue( $borrower1, $barcode1 );
998 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
999 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1002 $library2->{branchcode}, $borrowernumber2, $biblionumber,
1003 '', 1, undef, undef, '',
1007 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1008 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1009 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1010 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1012 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1013 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1014 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1015 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1017 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1018 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1019 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1020 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1022 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1023 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1024 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1025 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1027 # Setting item not checked out to be not for loan but holdable
1028 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
1030 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1031 is( $renewokay, 0, 'Bug 14337 - Verify the borrower can not renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled but the only available item is notforloan' );
1035 # Don't allow renewing onsite checkout
1036 my $barcode = 'R00000XXX';
1037 my $branch = $library->{branchcode};
1039 #Create another record
1040 my $biblio = MARC::Record->new();
1041 $biblio->append_fields(
1042 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
1043 MARC::Field->new('245', ' ', ' ', a => 'A title'),
1045 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
1047 my (undef, undef, $itemnumber) = AddItem(
1049 homebranch => $branch,
1050 holdingbranch => $branch,
1051 barcode => $barcode,
1057 my $borrowernumber = AddMember(
1060 categorycode => $patron_category->{categorycode},
1061 branchcode => $branch,
1064 my $borrower = GetMember( borrowernumber => $borrowernumber );
1065 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1066 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
1067 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1068 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1072 my $library = $builder->build({ source => 'Branch' });
1074 my $biblio = MARC::Record->new();
1075 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
1077 my $barcode = 'just a barcode';
1078 my ( undef, undef, $itemnumber ) = AddItem(
1080 homebranch => $library->{branchcode},
1081 holdingbranch => $library->{branchcode},
1082 barcode => $barcode,
1088 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
1090 my $issue = AddIssue( GetMember( borrowernumber => $patron->{borrowernumber} ), $barcode );
1093 issue_id => $issue->id(),
1094 itemnumber => $itemnumber,
1095 borrowernumber => $patron->{borrowernumber},
1102 issue_id => $issue->id(),
1103 itemnumber => $itemnumber,
1104 borrowernumber => $patron->{borrowernumber},
1109 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1112 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1115 my $homebranch = $builder->build( { source => 'Branch' } );
1116 my $holdingbranch = $builder->build( { source => 'Branch' } );
1117 my $otherbranch = $builder->build( { source => 'Branch' } );
1118 my $patron_1 = $builder->build( { source => 'Borrower' } );
1119 my $patron_2 = $builder->build( { source => 'Borrower' } );
1121 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1122 my $item = $builder->build(
1125 homebranch => $homebranch->{branchcode},
1126 holdingbranch => $holdingbranch->{branchcode},
1131 biblionumber => $biblioitem->{biblionumber}
1136 set_userenv($holdingbranch);
1138 my $issue = AddIssue( $patron_1, $item->{barcode} );
1139 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Checkout
1141 my ( $error, $question, $alerts );
1143 # AllowReturnToBranch == anywhere
1144 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1145 ## Can be issued from homebranch
1146 set_userenv($homebranch);
1147 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1148 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
1149 is( keys(%$alerts), 0, 'There should not be any alerts' );
1150 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1151 ## Can be issued from holdingbranch
1152 set_userenv($holdingbranch);
1153 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1154 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
1155 is( keys(%$alerts), 0, 'There should not be any alerts' );
1156 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1157 ## Can be issued from another branch
1158 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1159 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
1160 is( keys(%$alerts), 0, 'There should not be any alerts' );
1161 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1163 # AllowReturnToBranch == holdingbranch
1164 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1165 ## Cannot be issued from homebranch
1166 set_userenv($homebranch);
1167 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1168 is( keys(%$question) + keys(%$alerts), 0 );
1169 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1170 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1171 ## Can be issued from holdinbranch
1172 set_userenv($holdingbranch);
1173 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1174 is( keys(%$error) + keys(%$alerts), 0 );
1175 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1176 ## Cannot be issued from another branch
1177 set_userenv($otherbranch);
1178 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1179 is( keys(%$question) + keys(%$alerts), 0 );
1180 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1181 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1183 # AllowReturnToBranch == homebranch
1184 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1185 ## Can be issued from holdinbranch
1186 set_userenv($homebranch);
1187 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1188 is( keys(%$error) + keys(%$alerts), 0 );
1189 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1190 ## Cannot be issued from holdinbranch
1191 set_userenv($holdingbranch);
1192 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1193 is( keys(%$question) + keys(%$alerts), 0 );
1194 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1195 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1196 ## Cannot be issued from holdinbranch
1197 set_userenv($otherbranch);
1198 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1199 is( keys(%$question) + keys(%$alerts), 0 );
1200 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1201 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1203 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1206 subtest 'AddIssue & AllowReturnToBranch' => sub {
1209 my $homebranch = $builder->build( { source => 'Branch' } );
1210 my $holdingbranch = $builder->build( { source => 'Branch' } );
1211 my $otherbranch = $builder->build( { source => 'Branch' } );
1212 my $patron_1 = $builder->build( { source => 'Borrower' } );
1213 my $patron_2 = $builder->build( { source => 'Borrower' } );
1215 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1216 my $item = $builder->build(
1219 homebranch => $homebranch->{branchcode},
1220 holdingbranch => $holdingbranch->{branchcode},
1224 biblionumber => $biblioitem->{biblionumber}
1229 set_userenv($holdingbranch);
1231 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Checkout
1232 my $issue = AddIssue( $patron_1, $item->{barcode} );
1234 my ( $error, $question, $alerts );
1236 # AllowReturnToBranch == homebranch
1237 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1238 ## Can be issued from homebranch
1239 set_userenv($homebranch);
1240 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1241 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1242 ## Can be issued from holdinbranch
1243 set_userenv($holdingbranch);
1244 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1245 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1246 ## Can be issued from another branch
1247 set_userenv($otherbranch);
1248 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1249 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1251 # AllowReturnToBranch == holdinbranch
1252 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1253 ## Cannot be issued from homebranch
1254 set_userenv($homebranch);
1255 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1256 ## Can be issued from holdingbranch
1257 set_userenv($holdingbranch);
1258 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1259 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1260 ## Cannot be issued from another branch
1261 set_userenv($otherbranch);
1262 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1264 # AllowReturnToBranch == homebranch
1265 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1266 ## Can be issued from homebranch
1267 set_userenv($homebranch);
1268 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1269 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1270 ## Cannot be issued from holdinbranch
1271 set_userenv($holdingbranch);
1272 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1273 ## Cannot be issued from another branch
1274 set_userenv($otherbranch);
1275 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1276 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1279 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1282 my $library = $builder->build( { source => 'Branch' } );
1283 my $patron = $builder->build( { source => 'Borrower' } );
1285 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1286 my $item_1 = $builder->build(
1289 homebranch => $library->{branchcode},
1290 holdingbranch => $library->{branchcode},
1294 biblionumber => $biblioitem_1->{biblionumber}
1298 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1299 my $item_2 = $builder->build(
1302 homebranch => $library->{branchcode},
1303 holdingbranch => $library->{branchcode},
1307 biblionumber => $biblioitem_2->{biblionumber}
1312 my ( $error, $question, $alerts );
1314 # Patron cannot issue item_1, they have overdues
1315 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1316 my $issue = AddIssue( $patron, $item_1->{barcode}, $yesterday ); # Add an overdue
1318 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1319 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1320 is( keys(%$error) + keys(%$alerts), 0 );
1321 is( $question->{USERBLOCKEDOVERDUE}, 1 );
1323 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1324 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1325 is( keys(%$question) + keys(%$alerts), 0 );
1326 is( $error->{USERBLOCKEDOVERDUE}, 1 );
1328 # Patron cannot issue item_1, they are debarred
1329 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1330 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber}, expiration => $tomorrow } );
1331 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1332 is( keys(%$question) + keys(%$alerts), 0 );
1333 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ) );
1335 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber} } );
1336 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1337 is( keys(%$question) + keys(%$alerts), 0 );
1338 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31' );
1341 subtest 'MultipleReserves' => sub {
1344 my $biblio = MARC::Record->new();
1345 my $title = 'Silence in the library';
1346 $biblio->append_fields(
1347 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
1348 MARC::Field->new('245', ' ', ' ', a => $title),
1351 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
1353 my $branch = $library2->{branchcode};
1355 my $barcode1 = 'R00110001';
1356 my ( $item_bibnum1, $item_bibitemnum1, $itemnumber1 ) = AddItem(
1358 homebranch => $branch,
1359 holdingbranch => $branch,
1360 barcode => $barcode1,
1361 replacementprice => 12.00,
1367 my $barcode2 = 'R00110002';
1368 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
1370 homebranch => $branch,
1371 holdingbranch => $branch,
1372 barcode => $barcode2,
1373 replacementprice => 12.00,
1381 my $resdate = undef;
1382 my $expdate = undef;
1384 my $checkitem = undef;
1387 my %renewing_borrower_data = (
1388 firstname => 'John',
1389 surname => 'Renewal',
1390 categorycode => $patron_category->{categorycode},
1391 branchcode => $branch,
1393 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
1394 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
1395 my $issue = AddIssue( $renewing_borrower, $barcode1);
1396 my $datedue = dt_from_string( $issue->date_due() );
1397 is (defined $issue->date_due(), 1, "item 1 checked out");
1398 my $borrowing_borrowernumber = GetItemIssue($itemnumber1)->{borrowernumber};
1400 my %reserving_borrower_data1 = (
1401 firstname => 'Katrin',
1402 surname => 'Reservation',
1403 categorycode => $patron_category->{categorycode},
1404 branchcode => $branch,
1406 my $reserving_borrowernumber1 = AddMember(%reserving_borrower_data1);
1408 $branch, $reserving_borrowernumber1, $biblionumber,
1409 $bibitems, $priority, $resdate, $expdate, $notes,
1410 $title, $checkitem, $found
1413 my %reserving_borrower_data2 = (
1414 firstname => 'Kirk',
1415 surname => 'Reservation',
1416 categorycode => $patron_category->{categorycode},
1417 branchcode => $branch,
1419 my $reserving_borrowernumber2 = AddMember(%reserving_borrower_data2);
1421 $branch, $reserving_borrowernumber2, $biblionumber,
1422 $bibitems, $priority, $resdate, $expdate, $notes,
1423 $title, $checkitem, $found
1427 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1428 is($renewokay, 0, 'Bug 17641 - should cover the case where 2 books are both reserved, so failing');
1431 my $barcode3 = 'R00110003';
1432 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
1434 homebranch => $branch,
1435 holdingbranch => $branch,
1436 barcode => $barcode3,
1437 replacementprice => 12.00,
1444 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1445 is($renewokay, 1, 'Bug 17641 - should cover the case where 2 books are reserved, but a third one is available');
1449 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1452 my $library = $builder->build( { source => 'Branch' } );
1453 my $patron = $builder->build( { source => 'Borrower' } );
1455 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1456 my $biblionumber = $biblioitem->{biblionumber};
1457 my $item_1 = $builder->build(
1460 homebranch => $library->{branchcode},
1461 holdingbranch => $library->{branchcode},
1465 biblionumber => $biblionumber,
1469 my $item_2 = $builder->build(
1472 homebranch => $library->{branchcode},
1473 holdingbranch => $library->{branchcode},
1477 biblionumber => $biblionumber,
1482 my ( $error, $question, $alerts );
1483 my $issue = AddIssue( $patron, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1485 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1486 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1487 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' );
1488 is( $question->{BIBLIO_ALREADY_ISSUED}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' );
1490 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1491 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1492 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' );
1494 # Add a subscription
1495 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1497 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1498 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1499 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' );
1501 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1502 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1503 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' );
1506 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1509 my $library = $builder->build( { source => 'Branch' } );
1510 my $patron = $builder->build( { source => 'Borrower' } );
1513 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1514 my $item_1 = $builder->build(
1518 homebranch => $library->{branchcode},
1519 holdingbranch => $library->{branchcode},
1523 biblionumber => $biblioitem_1->{biblionumber}
1527 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1528 my $item_2 = $builder->build(
1532 homebranch => $library->{branchcode},
1533 holdingbranch => $library->{branchcode},
1537 biblionumber => $biblioitem_2->{biblionumber}
1542 # And the issuing rule
1543 Koha::IssuingRules->search->delete;
1544 my $rule = Koha::IssuingRule->new(
1546 categorycode => '*',
1551 firstremind => 1, # 1 day of grace
1552 finedays => 2, # 2 days of fine per day of overdue
1553 lengthunit => 'days',
1558 # Patron cannot issue item_1, they have overdues
1559 my $five_days_ago = dt_from_string->subtract( days => 5 );
1560 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1561 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1562 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1563 ; # Add another overdue
1565 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1566 AddReturn( $item_1->{barcode}, $library->{branchcode},
1567 undef, undef, dt_from_string );
1568 my $debarments = Koha::Patron::Debarments::GetDebarments(
1569 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1570 is( scalar(@$debarments), 1 );
1572 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1573 # Same for the others
1574 my $expected_expiration = output_pref(
1576 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1577 dateformat => 'sql',
1581 is( $debarments->[0]->{expiration}, $expected_expiration );
1583 AddReturn( $item_2->{barcode}, $library->{branchcode},
1584 undef, undef, dt_from_string );
1585 $debarments = Koha::Patron::Debarments::GetDebarments(
1586 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1587 is( scalar(@$debarments), 1 );
1588 $expected_expiration = output_pref(
1590 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1591 dateformat => 'sql',
1595 is( $debarments->[0]->{expiration}, $expected_expiration );
1597 Koha::Patron::Debarments::DelUniqueDebarment(
1598 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1600 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1601 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1602 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1603 ; # Add another overdue
1604 AddReturn( $item_1->{barcode}, $library->{branchcode},
1605 undef, undef, dt_from_string );
1606 $debarments = Koha::Patron::Debarments::GetDebarments(
1607 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1608 is( scalar(@$debarments), 1 );
1609 $expected_expiration = output_pref(
1611 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1612 dateformat => 'sql',
1616 is( $debarments->[0]->{expiration}, $expected_expiration );
1618 AddReturn( $item_2->{barcode}, $library->{branchcode},
1619 undef, undef, dt_from_string );
1620 $debarments = Koha::Patron::Debarments::GetDebarments(
1621 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1622 is( scalar(@$debarments), 1 );
1623 $expected_expiration = output_pref(
1625 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1626 dateformat => 'sql',
1630 is( $debarments->[0]->{expiration}, $expected_expiration );
1634 my ( $library ) = @_;
1635 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');