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 => 90;
23 require_ok('C4::Circulation');
29 use t::lib::TestBuilder;
36 use C4::Overdues qw(UpdateFine CalcFine);
40 my $schema = Koha::Database->schema;
41 $schema->storage->txn_begin;
42 my $builder = t::lib::TestBuilder->new;
43 my $dbh = C4::Context->dbh;
46 $dbh->{RaiseError} = 1;
48 # Start with a clean slate
49 $dbh->do('DELETE FROM issues');
51 my $library = $builder->build({
54 my $library2 = $builder->build({
58 my $CircControl = C4::Context->preference('CircControl');
59 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
62 homebranch => $library2->{branchcode},
63 holdingbranch => $library2->{branchcode}
67 branchcode => $library2->{branchcode}
70 # No userenv, PickupLibrary
71 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
73 C4::Context->preference('CircControl'),
75 'CircControl changed to PickupLibrary'
78 C4::Circulation::_GetCircControlBranch($item, $borrower),
79 $item->{$HomeOrHoldingBranch},
80 '_GetCircControlBranch returned item branch (no userenv defined)'
83 # No userenv, PatronLibrary
84 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
86 C4::Context->preference('CircControl'),
88 'CircControl changed to PatronLibrary'
91 C4::Circulation::_GetCircControlBranch($item, $borrower),
92 $borrower->{branchcode},
93 '_GetCircControlBranch returned borrower branch'
96 # No userenv, ItemHomeLibrary
97 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
99 C4::Context->preference('CircControl'),
101 'CircControl changed to ItemHomeLibrary'
104 $item->{$HomeOrHoldingBranch},
105 C4::Circulation::_GetCircControlBranch($item, $borrower),
106 '_GetCircControlBranch returned item branch'
110 C4::Context->_new_userenv('xxx');
111 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
112 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
114 # Userenv set, PickupLibrary
115 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
117 C4::Context->preference('CircControl'),
119 'CircControl changed to PickupLibrary'
122 C4::Circulation::_GetCircControlBranch($item, $borrower),
123 $library2->{branchcode},
124 '_GetCircControlBranch returned current branch'
127 # Userenv set, PatronLibrary
128 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
130 C4::Context->preference('CircControl'),
132 'CircControl changed to PatronLibrary'
135 C4::Circulation::_GetCircControlBranch($item, $borrower),
136 $borrower->{branchcode},
137 '_GetCircControlBranch returned borrower branch'
140 # Userenv set, ItemHomeLibrary
141 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
143 C4::Context->preference('CircControl'),
145 'CircControl changed to ItemHomeLibrary'
148 C4::Circulation::_GetCircControlBranch($item, $borrower),
149 $item->{$HomeOrHoldingBranch},
150 '_GetCircControlBranch returned item branch'
153 # Reset initial configuration
154 t::lib::Mocks::mock_preference('CircControl', $CircControl);
156 C4::Context->preference('CircControl'),
158 'CircControl reset to its initial value'
161 # Set a simple circ policy
162 $dbh->do('DELETE FROM issuingrules');
164 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
165 maxissueqty, issuelength, lengthunit,
166 renewalsallowed, renewalperiod,
167 norenewalbefore, auto_renew,
184 # Test C4::Circulation::ProcessOfflinePayment
185 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
187 my ( $original_count ) = $sth->fetchrow_array();
189 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', ? )", undef, $library2->{branchcode} );
191 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
194 my ( $new_count ) = $sth->fetchrow_array();
196 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
198 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
199 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
200 C4::Context->dbh->do("DELETE FROM accountlines");
202 # CanBookBeRenewed tests
204 # Generate test biblio
205 my $biblio = MARC::Record->new();
206 my $title = 'Silence in the library';
207 $biblio->append_fields(
208 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
209 MARC::Field->new('245', ' ', ' ', a => $title),
212 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
214 my $barcode = 'R00000342';
215 my $branch = $library2->{branchcode};
217 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
219 homebranch => $branch,
220 holdingbranch => $branch,
222 replacementprice => 12.00
227 my $barcode2 = 'R00000343';
228 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
230 homebranch => $branch,
231 holdingbranch => $branch,
232 barcode => $barcode2,
233 replacementprice => 23.00
238 my $barcode3 = 'R00000346';
239 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
241 homebranch => $branch,
242 holdingbranch => $branch,
243 barcode => $barcode3,
244 replacementprice => 23.00
253 my %renewing_borrower_data = (
255 surname => 'Renewal',
257 branchcode => $branch,
260 my %reserving_borrower_data = (
261 firstname => 'Katrin',
262 surname => 'Reservation',
264 branchcode => $branch,
267 my %hold_waiting_borrower_data = (
269 surname => 'Reservation',
271 branchcode => $branch,
274 my %restricted_borrower_data = (
275 firstname => 'Alice',
276 surname => 'Reservation',
278 debarred => '3228-01-01',
279 branchcode => $branch,
282 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
283 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
284 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
285 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
287 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
288 my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
295 my $checkitem = undef;
298 my $issue = AddIssue( $renewing_borrower, $barcode);
299 my $datedue = dt_from_string( $issue->date_due() );
300 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
302 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
303 $datedue = dt_from_string( $issue->date_due() );
304 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
307 my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
308 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
310 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
311 is( $renewokay, 1, 'Can renew, no holds for this title or item');
314 # Biblio-level hold, renewal test
316 $branch, $reserving_borrowernumber, $biblionumber,
317 $bibitems, $priority, $resdate, $expdate, $notes,
318 $title, $checkitem, $found
321 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
322 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
323 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
324 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
325 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
326 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
327 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
329 # Now let's add an item level hold, we should no longer be able to renew the item
330 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
332 borrowernumber => $hold_waiting_borrowernumber,
333 biblionumber => $biblionumber,
334 itemnumber => $itemnumber,
335 branchcode => $branch,
339 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
340 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
343 # 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
344 # be able to renew these items
345 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
347 borrowernumber => $hold_waiting_borrowernumber,
348 biblionumber => $biblionumber,
349 itemnumber => $itemnumber3,
350 branchcode => $branch,
355 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
356 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
357 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
358 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
359 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
361 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
362 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
363 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
365 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
366 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
367 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
369 my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
370 my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
371 AddIssue($reserving_borrower, $barcode3);
372 my $reserve = $dbh->selectrow_hashref(
373 'SELECT * FROM old_reserves WHERE reserve_id = ?',
377 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
379 # Item-level hold, renewal test
381 $branch, $reserving_borrowernumber, $biblionumber,
382 $bibitems, $priority, $resdate, $expdate, $notes,
383 $title, $itemnumber, $found
386 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
387 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
388 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
390 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
391 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
393 # Items can't fill hold for reasons
394 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
395 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
396 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
397 ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
399 # FIXME: Add more for itemtype not for loan etc.
401 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
402 my $barcode5 = 'R00000347';
403 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
405 homebranch => $branch,
406 holdingbranch => $branch,
407 barcode => $barcode5,
408 replacementprice => 23.00
412 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
413 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
415 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
416 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
417 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
418 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
419 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
421 # Users cannot renew an overdue item
422 my $barcode6 = 'R00000348';
423 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
425 homebranch => $branch,
426 holdingbranch => $branch,
427 barcode => $barcode6,
428 replacementprice => 23.00
433 my $barcode7 = 'R00000349';
434 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
436 homebranch => $branch,
437 holdingbranch => $branch,
438 barcode => $barcode7,
439 replacementprice => 23.00
443 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
444 is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
446 my $now = dt_from_string();
447 my $five_weeks = DateTime::Duration->new(weeks => 5);
448 my $five_weeks_ago = $now - $five_weeks;
450 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
451 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
453 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
454 C4::Overdues::UpdateFine(
456 issue_id => $passeddatedue1->id(),
457 itemnumber => $itemnumber7,
458 borrowernumber => $renewing_borrower->{borrowernumber},
461 due => Koha::DateUtils::output_pref($five_weeks_ago)
464 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
465 $fine = $schema->resultset('Accountline')->single( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
466 is( $fine->accounttype, 'F', 'Fine on renewed item is closed out properly' );
469 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
470 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
471 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
472 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
473 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
476 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
477 CancelReserve({ reserve_id => $reserveid });
480 # Test automatic renewal before value for "norenewalbefore" in policy is set
481 # In this case automatic renewal is not permitted prior to due date
482 my $barcode4 = '11235813';
483 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
485 homebranch => $branch,
486 holdingbranch => $branch,
487 barcode => $barcode4,
488 replacementprice => 16.00
493 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
494 ( $renewokay, $error ) =
495 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
496 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
497 is( $error, 'auto_too_soon',
498 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
501 # Test premature manual renewal
502 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
504 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
505 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
506 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
509 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
510 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
512 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
513 $datedue->clone->add( days => -7 ),
514 'Bug 14395: Renewals permitted 7 days before due date, as expected'
518 # Test 'date' setting for syspref NoRenewalBeforePrecision
519 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
521 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
522 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
523 'Bug 14395: Renewals permitted 7 days before due date, as expected'
527 # Test premature automatic renewal
528 ( $renewokay, $error ) =
529 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
530 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
531 is( $error, 'auto_too_soon',
532 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
535 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
536 # and test automatic renewal again
537 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
538 ( $renewokay, $error ) =
539 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
540 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
541 is( $error, 'auto_too_soon',
542 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
545 # Change policy so that loans can be renewed 99 days prior to the due date
546 # and test automatic renewal again
547 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
548 ( $renewokay, $error ) =
549 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
550 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
551 is( $error, 'auto_renew',
552 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
555 subtest "too_late_renewal / no_auto_renewal_after" => sub {
557 my $item_to_auto_renew = $builder->build(
560 biblionumber => $biblionumber,
561 homebranch => $branch,
562 holdingbranch => $branch,
567 my $ten_days_before = dt_from_string->add( days => -10 );
568 my $ten_days_ahead = dt_from_string->add( days => 10 );
569 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
571 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
572 ( $renewokay, $error ) =
573 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
574 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
575 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
577 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
578 ( $renewokay, $error ) =
579 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
580 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
581 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
583 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
584 ( $renewokay, $error ) =
585 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
586 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
587 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
589 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
590 ( $renewokay, $error ) =
591 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
592 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
593 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
598 # set policy to forbid renewals
599 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
601 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
602 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
603 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
605 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
606 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
607 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
609 C4::Overdues::UpdateFine(
611 issue_id => $issue->id(),
612 itemnumber => $itemnumber,
613 borrowernumber => $renewing_borrower->{borrowernumber},
616 due => Koha::DateUtils::output_pref($datedue)
620 LostItem( $itemnumber, 1 );
622 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
623 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
625 my $total_due = $dbh->selectrow_array(
626 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
627 undef, $renewing_borrower->{borrowernumber}
630 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
632 C4::Context->dbh->do("DELETE FROM accountlines");
634 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
635 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
637 C4::Overdues::UpdateFine(
639 issue_id => $issue2->id(),
640 itemnumber => $itemnumber2,
641 borrowernumber => $renewing_borrower->{borrowernumber},
644 due => Koha::DateUtils::output_pref($datedue)
648 LostItem( $itemnumber2, 0 );
650 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
651 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
653 $total_due = $dbh->selectrow_array(
654 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
655 undef, $renewing_borrower->{borrowernumber}
658 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
660 my $future = dt_from_string();
661 $future->add( days => 7 );
662 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
663 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
665 # Users cannot renew any item if there is an overdue item
666 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
667 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
668 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
669 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
670 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
675 # GetUpcomingDueIssues tests
676 my $barcode = 'R00000342';
677 my $barcode2 = 'R00000343';
678 my $barcode3 = 'R00000344';
679 my $branch = $library2->{branchcode};
681 #Create another record
682 my $biblio2 = MARC::Record->new();
683 my $title2 = 'Something is worng here';
684 $biblio2->append_fields(
685 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
686 MARC::Field->new('245', ' ', ' ', a => $title2),
688 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
693 homebranch => $branch,
694 holdingbranch => $branch,
701 my %a_borrower_data = (
702 firstname => 'Fridolyn',
705 branchcode => $branch,
708 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
709 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
711 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
712 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
713 my $today = DateTime->today(time_zone => C4::Context->tz());
715 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
716 my $datedue = dt_from_string( $issue->date_due() );
717 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
718 my $datedue2 = dt_from_string( $issue->date_due() );
722 # GetUpcomingDueIssues tests
724 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
725 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
728 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
729 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
730 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
733 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
734 is ( scalar( @$upcoming_dues ), 1,
735 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
738 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
740 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
742 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
743 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
745 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
746 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
748 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
749 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
751 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
752 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
754 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
755 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
757 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
758 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
763 my $barcode = '1234567890';
764 my $branch = $library2->{branchcode};
766 my $biblio = MARC::Record->new();
767 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
770 my ( undef, undef, $itemnumber ) = AddItem(
772 homebranch => $branch,
773 holdingbranch => $branch,
780 my %a_borrower_data = (
784 branchcode => $branch,
787 my $borrowernumber = AddMember(%a_borrower_data);
789 my $issue = AddIssue( GetMember( borrowernumber => $borrowernumber ), $barcode );
792 issue_id => $issue->id(),
793 itemnumber => $itemnumber,
794 borrowernumber => $borrowernumber,
799 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
800 my $count = $hr->{count};
802 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
806 $dbh->do('DELETE FROM issues');
807 $dbh->do('DELETE FROM items');
808 $dbh->do('DELETE FROM issuingrules');
811 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
812 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
821 my $biblio = MARC::Record->new();
822 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
824 my $barcode1 = '1234';
825 my ( undef, undef, $itemnumber1 ) = AddItem(
827 homebranch => $library2->{branchcode},
828 holdingbranch => $library2->{branchcode},
829 barcode => $barcode1,
833 my $barcode2 = '4321';
834 my ( undef, undef, $itemnumber2 ) = AddItem(
836 homebranch => $library2->{branchcode},
837 holdingbranch => $library2->{branchcode},
838 barcode => $barcode2,
843 my $borrowernumber1 = AddMember(
847 branchcode => $library2->{branchcode},
849 my $borrowernumber2 = AddMember(
850 firstname => 'Chelsea',
853 branchcode => $library2->{branchcode},
856 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
857 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
859 my $issue = AddIssue( $borrower1, $barcode1 );
861 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
862 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
865 $library2->{branchcode}, $borrowernumber2, $biblionumber,
866 '', 1, undef, undef, '',
870 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
871 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
872 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
873 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
875 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
876 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
877 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
878 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
880 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
881 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
882 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
883 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
885 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
886 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
887 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
888 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
890 # Setting item not checked out to be not for loan but holdable
891 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
893 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
894 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' );
898 # Don't allow renewing onsite checkout
899 my $barcode = 'R00000XXX';
900 my $branch = $library->{branchcode};
902 #Create another record
903 my $biblio = MARC::Record->new();
904 $biblio->append_fields(
905 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
906 MARC::Field->new('245', ' ', ' ', a => 'A title'),
908 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
910 my (undef, undef, $itemnumber) = AddItem(
912 homebranch => $branch,
913 holdingbranch => $branch,
919 my $borrowernumber = AddMember(
923 branchcode => $branch,
926 my $borrower = GetMember( borrowernumber => $borrowernumber );
927 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
928 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
929 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
930 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
934 my $library = $builder->build({ source => 'Branch' });
936 my $biblio = MARC::Record->new();
937 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
939 my $barcode = 'just a barcode';
940 my ( undef, undef, $itemnumber ) = AddItem(
942 homebranch => $library->{branchcode},
943 holdingbranch => $library->{branchcode},
949 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
951 my $issue = AddIssue( GetMember( borrowernumber => $patron->{borrowernumber} ), $barcode );
954 issue_id => $issue->id(),
955 itemnumber => $itemnumber,
956 borrowernumber => $patron->{borrowernumber},
962 issue_id => $issue->id(),
963 itemnumber => $itemnumber,
964 borrowernumber => $patron->{borrowernumber},
968 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
971 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
974 my $homebranch = $builder->build( { source => 'Branch' } );
975 my $holdingbranch = $builder->build( { source => 'Branch' } );
976 my $otherbranch = $builder->build( { source => 'Branch' } );
977 my $patron_1 = $builder->build( { source => 'Borrower' } );
978 my $patron_2 = $builder->build( { source => 'Borrower' } );
980 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
981 my $item = $builder->build(
984 homebranch => $homebranch->{branchcode},
985 holdingbranch => $holdingbranch->{branchcode},
989 biblionumber => $biblioitem->{biblionumber}
994 set_userenv($holdingbranch);
996 my $issue = AddIssue( $patron_1, $item->{barcode} );
997 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Issue
999 my ( $error, $question, $alerts );
1001 # AllowReturnToBranch == anywhere
1002 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1003 ## Can be issued from homebranch
1004 set_userenv($homebranch);
1005 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1006 is( keys(%$error) + keys(%$alerts), 0 );
1007 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1008 ## Can be issued from holdingbranch
1009 set_userenv($holdingbranch);
1010 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1011 is( keys(%$error) + keys(%$alerts), 0 );
1012 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1013 ## Can be issued from another branch
1014 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1015 is( keys(%$error) + keys(%$alerts), 0 );
1016 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1018 # AllowReturnToBranch == holdingbranch
1019 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1020 ## Cannot be issued from homebranch
1021 set_userenv($homebranch);
1022 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1023 is( keys(%$question) + keys(%$alerts), 0 );
1024 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1025 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1026 ## Can be issued from holdinbranch
1027 set_userenv($holdingbranch);
1028 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1029 is( keys(%$error) + keys(%$alerts), 0 );
1030 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1031 ## Cannot be issued from another branch
1032 set_userenv($otherbranch);
1033 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1034 is( keys(%$question) + keys(%$alerts), 0 );
1035 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1036 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1038 # AllowReturnToBranch == homebranch
1039 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1040 ## Can be issued from holdinbranch
1041 set_userenv($homebranch);
1042 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1043 is( keys(%$error) + keys(%$alerts), 0 );
1044 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1045 ## Cannot be issued from holdinbranch
1046 set_userenv($holdingbranch);
1047 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1048 is( keys(%$question) + keys(%$alerts), 0 );
1049 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1050 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1051 ## Cannot be issued from holdinbranch
1052 set_userenv($otherbranch);
1053 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1054 is( keys(%$question) + keys(%$alerts), 0 );
1055 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1056 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1058 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1061 subtest 'AddIssue & AllowReturnToBranch' => sub {
1064 my $homebranch = $builder->build( { source => 'Branch' } );
1065 my $holdingbranch = $builder->build( { source => 'Branch' } );
1066 my $otherbranch = $builder->build( { source => 'Branch' } );
1067 my $patron_1 = $builder->build( { source => 'Borrower' } );
1068 my $patron_2 = $builder->build( { source => 'Borrower' } );
1070 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1071 my $item = $builder->build(
1074 homebranch => $homebranch->{branchcode},
1075 holdingbranch => $holdingbranch->{branchcode},
1079 biblionumber => $biblioitem->{biblionumber}
1084 set_userenv($holdingbranch);
1086 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Issue
1087 my $issue = AddIssue( $patron_1, $item->{barcode} );
1089 my ( $error, $question, $alerts );
1091 # AllowReturnToBranch == homebranch
1092 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1093 ## Can be issued from homebranch
1094 set_userenv($homebranch);
1095 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1096 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1097 ## Can be issued from holdinbranch
1098 set_userenv($holdingbranch);
1099 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1100 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1101 ## Can be issued from another branch
1102 set_userenv($otherbranch);
1103 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1104 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1106 # AllowReturnToBranch == holdinbranch
1107 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1108 ## Cannot be issued from homebranch
1109 set_userenv($homebranch);
1110 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1111 ## Can be issued from holdingbranch
1112 set_userenv($holdingbranch);
1113 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1114 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1115 ## Cannot be issued from another branch
1116 set_userenv($otherbranch);
1117 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1119 # AllowReturnToBranch == homebranch
1120 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1121 ## Can be issued from homebranch
1122 set_userenv($homebranch);
1123 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1124 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1125 ## Cannot be issued from holdinbranch
1126 set_userenv($holdingbranch);
1127 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1128 ## Cannot be issued from another branch
1129 set_userenv($otherbranch);
1130 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1131 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1134 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1137 my $library = $builder->build( { source => 'Branch' } );
1138 my $patron = $builder->build( { source => 'Borrower' } );
1140 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1141 my $item_1 = $builder->build(
1144 homebranch => $library->{branchcode},
1145 holdingbranch => $library->{branchcode},
1149 biblionumber => $biblioitem_1->{biblionumber}
1153 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1154 my $item_2 = $builder->build(
1157 homebranch => $library->{branchcode},
1158 holdingbranch => $library->{branchcode},
1162 biblionumber => $biblioitem_2->{biblionumber}
1167 my ( $error, $question, $alerts );
1169 # Patron cannot issue item_1, he has overdues
1170 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1171 my $issue = AddIssue( $patron, $item_1->{barcode}, $yesterday ); # Add an overdue
1173 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1174 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1175 is( keys(%$error) + keys(%$alerts), 0 );
1176 is( $question->{USERBLOCKEDOVERDUE}, 1 );
1178 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1179 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1180 is( keys(%$question) + keys(%$alerts), 0 );
1181 is( $error->{USERBLOCKEDOVERDUE}, 1 );
1183 # Patron cannot issue item_1, he is debarred
1184 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1185 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber}, expiration => $tomorrow } );
1186 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1187 is( keys(%$question) + keys(%$alerts), 0 );
1188 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ) );
1190 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber} } );
1191 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1192 is( keys(%$question) + keys(%$alerts), 0 );
1193 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31' );
1197 my ( $library ) = @_;
1198 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');