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>.
27 use C4::Overdues qw(UpdateFine CalcFine);
31 use t::lib::TestBuilder;
33 use Test::More tests => 87;
36 use_ok('C4::Circulation');
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 }
62 my $CircControl = C4::Context->preference('CircControl');
63 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
66 homebranch => $library2->{branchcode},
67 holdingbranch => $library2->{branchcode}
71 branchcode => $library2->{branchcode}
74 # No userenv, PickupLibrary
75 t::lib::Mocks::mock_preference('IndependentBranches', '0');
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', 'S', ? )", undef, $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',
265 branchcode => $branch,
268 my %reserving_borrower_data = (
269 firstname => 'Katrin',
270 surname => 'Reservation',
272 branchcode => $branch,
275 my %hold_waiting_borrower_data = (
277 surname => 'Reservation',
279 branchcode => $branch,
282 my %restricted_borrower_data = (
283 firstname => 'Alice',
284 surname => 'Reservation',
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 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
476 $fine = $schema->resultset('Accountline')->single( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
477 is( $fine->accounttype, 'F', 'Fine on renewed item is closed out properly' );
480 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
481 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
482 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
483 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
484 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
487 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
488 CancelReserve({ reserve_id => $reserveid });
491 # Test automatic renewal before value for "norenewalbefore" in policy is set
492 # In this case automatic renewal is not permitted prior to due date
493 my $barcode4 = '11235813';
494 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
496 homebranch => $branch,
497 holdingbranch => $branch,
498 barcode => $barcode4,
499 replacementprice => 16.00,
505 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
506 ( $renewokay, $error ) =
507 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
508 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
509 is( $error, 'auto_too_soon',
510 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
513 # Test premature manual renewal
514 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
516 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
517 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
518 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
521 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
522 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
524 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
525 $datedue->clone->add( days => -7 ),
526 'Bug 14395: Renewals permitted 7 days before due date, as expected'
530 # Test 'date' setting for syspref NoRenewalBeforePrecision
531 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
533 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
534 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
535 'Bug 14395: Renewals permitted 7 days before due date, as expected'
539 # Test premature automatic renewal
540 ( $renewokay, $error ) =
541 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
542 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
543 is( $error, 'auto_too_soon',
544 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
547 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
548 # and test automatic renewal again
549 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
550 ( $renewokay, $error ) =
551 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
552 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
553 is( $error, 'auto_too_soon',
554 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
557 # Change policy so that loans can be renewed 99 days prior to the due date
558 # and test automatic renewal again
559 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
560 ( $renewokay, $error ) =
561 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
562 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
563 is( $error, 'auto_renew',
564 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
569 # set policy to forbid renewals
570 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
572 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
573 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
574 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
576 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
577 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
578 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
580 C4::Overdues::UpdateFine(
582 issue_id => $issue->id(),
583 itemnumber => $itemnumber,
584 borrowernumber => $renewing_borrower->{borrowernumber},
587 due => Koha::DateUtils::output_pref($datedue)
591 LostItem( $itemnumber, 1 );
593 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
594 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
596 my $total_due = $dbh->selectrow_array(
597 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
598 undef, $renewing_borrower->{borrowernumber}
601 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
603 C4::Context->dbh->do("DELETE FROM accountlines");
605 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
606 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
608 C4::Overdues::UpdateFine(
610 issue_id => $issue2->id(),
611 itemnumber => $itemnumber2,
612 borrowernumber => $renewing_borrower->{borrowernumber},
615 due => Koha::DateUtils::output_pref($datedue)
619 LostItem( $itemnumber2, 0 );
621 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
622 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
624 $total_due = $dbh->selectrow_array(
625 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
626 undef, $renewing_borrower->{borrowernumber}
629 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
631 my $future = dt_from_string();
632 $future->add( days => 7 );
633 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
634 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
636 # Users cannot renew any item if there is an overdue item
637 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
638 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
639 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
640 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
641 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
646 # GetUpcomingDueIssues tests
647 my $barcode = 'R00000342';
648 my $barcode2 = 'R00000343';
649 my $barcode3 = 'R00000344';
650 my $branch = $library2->{branchcode};
652 #Create another record
653 my $biblio2 = MARC::Record->new();
654 my $title2 = 'Something is worng here';
655 $biblio2->append_fields(
656 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
657 MARC::Field->new('245', ' ', ' ', a => $title2),
659 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
664 homebranch => $branch,
665 holdingbranch => $branch,
666 barcode => $barcode3,
673 my %a_borrower_data = (
674 firstname => 'Fridolyn',
677 branchcode => $branch,
680 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
681 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
683 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
684 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
685 my $today = DateTime->today(time_zone => C4::Context->tz());
687 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
688 my $datedue = dt_from_string( $issue->date_due() );
689 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
690 my $datedue2 = dt_from_string( $issue->date_due() );
694 # GetUpcomingDueIssues tests
696 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
697 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
700 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
701 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
702 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
705 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
706 is ( scalar( @$upcoming_dues ), 1,
707 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
710 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
712 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
714 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
715 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
717 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
718 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
720 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
721 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
723 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
724 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
726 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
727 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
729 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
730 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
735 my $barcode = '1234567890';
736 my $branch = $library2->{branchcode};
738 my $biblio = MARC::Record->new();
739 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
742 my ( undef, undef, $itemnumber ) = AddItem(
744 homebranch => $branch,
745 holdingbranch => $branch,
753 my %a_borrower_data = (
757 branchcode => $branch,
760 my $borrowernumber = AddMember(%a_borrower_data);
762 my $issue = AddIssue( GetMember( borrowernumber => $borrowernumber ), $barcode );
765 issue_id => $issue->id(),
766 itemnumber => $itemnumber,
767 borrowernumber => $borrowernumber,
773 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
774 my $count = $hr->{count};
776 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
780 $dbh->do('DELETE FROM issues');
781 $dbh->do('DELETE FROM items');
782 $dbh->do('DELETE FROM issuingrules');
785 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
786 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
795 my $biblio = MARC::Record->new();
796 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
798 my $barcode1 = '1234';
799 my ( undef, undef, $itemnumber1 ) = AddItem(
801 homebranch => $library2->{branchcode},
802 holdingbranch => $library2->{branchcode},
803 barcode => $barcode1,
808 my $barcode2 = '4321';
809 my ( undef, undef, $itemnumber2 ) = AddItem(
811 homebranch => $library2->{branchcode},
812 holdingbranch => $library2->{branchcode},
813 barcode => $barcode2,
819 my $borrowernumber1 = AddMember(
823 branchcode => $library2->{branchcode},
825 my $borrowernumber2 = AddMember(
826 firstname => 'Chelsea',
829 branchcode => $library2->{branchcode},
832 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
833 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
835 my $issue = AddIssue( $borrower1, $barcode1 );
837 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
838 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
841 $library2->{branchcode}, $borrowernumber2, $biblionumber,
842 '', 1, undef, undef, '',
846 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
847 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
848 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
849 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
851 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
852 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
853 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
854 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
856 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
857 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
858 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
859 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
861 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
862 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
863 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
864 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
866 # Setting item not checked out to be not for loan but holdable
867 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
869 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
870 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' );
874 # Don't allow renewing onsite checkout
875 my $barcode = 'R00000XXX';
876 my $branch = $library->{branchcode};
878 #Create another record
879 my $biblio = MARC::Record->new();
880 $biblio->append_fields(
881 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
882 MARC::Field->new('245', ' ', ' ', a => 'A title'),
884 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
886 my (undef, undef, $itemnumber) = AddItem(
888 homebranch => $branch,
889 holdingbranch => $branch,
896 my $borrowernumber = AddMember(
900 branchcode => $branch,
903 my $borrower = GetMember( borrowernumber => $borrowernumber );
904 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
905 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
906 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
907 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
911 my $library = $builder->build({ source => 'Branch' });
913 my $biblio = MARC::Record->new();
914 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
916 my $barcode = 'just a barcode';
917 my ( undef, undef, $itemnumber ) = AddItem(
919 homebranch => $library->{branchcode},
920 holdingbranch => $library->{branchcode},
927 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
929 my $issue = AddIssue( GetMember( borrowernumber => $patron->{borrowernumber} ), $barcode );
932 issue_id => $issue->id(),
933 itemnumber => $itemnumber,
934 borrowernumber => $patron->{borrowernumber},
941 issue_id => $issue->id(),
942 itemnumber => $itemnumber,
943 borrowernumber => $patron->{borrowernumber},
948 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
951 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
954 my $homebranch = $builder->build( { source => 'Branch' } );
955 my $holdingbranch = $builder->build( { source => 'Branch' } );
956 my $otherbranch = $builder->build( { source => 'Branch' } );
957 my $patron_1 = $builder->build( { source => 'Borrower' } );
958 my $patron_2 = $builder->build( { source => 'Borrower' } );
960 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
961 my $item = $builder->build(
964 homebranch => $homebranch->{branchcode},
965 holdingbranch => $holdingbranch->{branchcode},
969 biblionumber => $biblioitem->{biblionumber}
974 set_userenv($holdingbranch);
976 my $issue = AddIssue( $patron_1, $item->{barcode} );
977 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Issue
979 my ( $error, $question, $alerts );
981 # AllowReturnToBranch == anywhere
982 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
983 ## Can be issued from homebranch
984 set_userenv($homebranch);
985 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
986 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
987 is( keys(%$alerts), 0, 'There should not be any alerts' );
988 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
989 ## Can be issued from holdingbranch
990 set_userenv($holdingbranch);
991 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
992 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
993 is( keys(%$alerts), 0, 'There should not be any alerts' );
994 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
995 ## Can be issued from another branch
996 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
997 is( keys(%$error), 0, 'There should not be any errors (impossible)' );
998 is( keys(%$alerts), 0, 'There should not be any alerts' );
999 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1001 # AllowReturnToBranch == holdingbranch
1002 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1003 ## Cannot be issued from homebranch
1004 set_userenv($homebranch);
1005 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1006 is( keys(%$question) + keys(%$alerts), 0 );
1007 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1008 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1009 ## Can be issued from holdinbranch
1010 set_userenv($holdingbranch);
1011 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1012 is( keys(%$error) + keys(%$alerts), 0 );
1013 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1014 ## Cannot be issued from another branch
1015 set_userenv($otherbranch);
1016 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1017 is( keys(%$question) + keys(%$alerts), 0 );
1018 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1019 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1021 # AllowReturnToBranch == homebranch
1022 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1023 ## Can be issued from holdinbranch
1024 set_userenv($homebranch);
1025 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1026 is( keys(%$error) + keys(%$alerts), 0 );
1027 is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1028 ## Cannot be issued from holdinbranch
1029 set_userenv($holdingbranch);
1030 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1031 is( keys(%$question) + keys(%$alerts), 0 );
1032 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1033 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1034 ## Cannot be issued from holdinbranch
1035 set_userenv($otherbranch);
1036 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1037 is( keys(%$question) + keys(%$alerts), 0 );
1038 is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1039 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1041 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1044 subtest 'AddIssue & AllowReturnToBranch' => sub {
1047 my $homebranch = $builder->build( { source => 'Branch' } );
1048 my $holdingbranch = $builder->build( { source => 'Branch' } );
1049 my $otherbranch = $builder->build( { source => 'Branch' } );
1050 my $patron_1 = $builder->build( { source => 'Borrower' } );
1051 my $patron_2 = $builder->build( { source => 'Borrower' } );
1053 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1054 my $item = $builder->build(
1057 homebranch => $homebranch->{branchcode},
1058 holdingbranch => $holdingbranch->{branchcode},
1063 biblionumber => $biblioitem->{biblionumber}
1068 set_userenv($holdingbranch);
1070 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Issue
1071 my $issue = AddIssue( $patron_1, $item->{barcode} );
1073 my ( $error, $question, $alerts );
1075 # AllowReturnToBranch == homebranch
1076 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1077 ## Can be issued from homebranch
1078 set_userenv($homebranch);
1079 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1080 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1081 ## Can be issued from holdinbranch
1082 set_userenv($holdingbranch);
1083 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1084 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1085 ## Can be issued from another branch
1086 set_userenv($otherbranch);
1087 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1088 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1090 # AllowReturnToBranch == holdinbranch
1091 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1092 ## Cannot be issued from homebranch
1093 set_userenv($homebranch);
1094 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1095 ## Can be issued from holdingbranch
1096 set_userenv($holdingbranch);
1097 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1098 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1099 ## Cannot be issued from another branch
1100 set_userenv($otherbranch);
1101 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1103 # AllowReturnToBranch == homebranch
1104 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1105 ## Can be issued from homebranch
1106 set_userenv($homebranch);
1107 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1108 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1109 ## Cannot be issued from holdinbranch
1110 set_userenv($holdingbranch);
1111 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1112 ## Cannot be issued from another branch
1113 set_userenv($otherbranch);
1114 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1115 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1120 my ( $library ) = @_;
1121 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');