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 => 120;
24 use POSIX qw( floor );
26 use t::lib::TestBuilder;
35 use C4::Overdues qw(UpdateFine CalcFine);
38 use Koha::IssuingRules;
41 use Koha::Subscriptions;
42 use Koha::Account::Lines;
43 use Koha::Account::Offsets;
45 my $schema = Koha::Database->schema;
46 $schema->storage->txn_begin;
47 my $builder = t::lib::TestBuilder->new;
48 my $dbh = C4::Context->dbh;
51 $dbh->{RaiseError} = 1;
53 my $cache = Koha::Caches->get_instance();
54 $dbh->do(q|DELETE FROM special_holidays|);
55 $dbh->do(q|DELETE FROM repeatable_holidays|);
56 $cache->clear_from_cache('single_holidays');
58 # Start with a clean slate
59 $dbh->do('DELETE FROM issues');
60 $dbh->do('DELETE FROM borrowers');
62 my $library = $builder->build({
65 my $library2 = $builder->build({
68 my $itemtype = $builder->build(
69 { source => 'Itemtype',
70 value => { notforloan => undef, rentalcharge => 0, defaultreplacecost => undef, processfee => undef }
73 my $patron_category = $builder->build(
79 BlockExpiredPatronOpacActions => -1, # Pick the pref value
84 my $CircControl = C4::Context->preference('CircControl');
85 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
88 homebranch => $library2->{branchcode},
89 holdingbranch => $library2->{branchcode}
93 branchcode => $library2->{branchcode}
96 # No userenv, PickupLibrary
97 t::lib::Mocks::mock_preference('IndependentBranches', '0');
98 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
100 C4::Context->preference('CircControl'),
102 'CircControl changed to PickupLibrary'
105 C4::Circulation::_GetCircControlBranch($item, $borrower),
106 $item->{$HomeOrHoldingBranch},
107 '_GetCircControlBranch returned item branch (no userenv defined)'
110 # No userenv, PatronLibrary
111 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
113 C4::Context->preference('CircControl'),
115 'CircControl changed to PatronLibrary'
118 C4::Circulation::_GetCircControlBranch($item, $borrower),
119 $borrower->{branchcode},
120 '_GetCircControlBranch returned borrower branch'
123 # No userenv, ItemHomeLibrary
124 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
126 C4::Context->preference('CircControl'),
128 'CircControl changed to ItemHomeLibrary'
131 $item->{$HomeOrHoldingBranch},
132 C4::Circulation::_GetCircControlBranch($item, $borrower),
133 '_GetCircControlBranch returned item branch'
137 C4::Context->_new_userenv('xxx');
138 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
139 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
141 # Userenv set, PickupLibrary
142 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
144 C4::Context->preference('CircControl'),
146 'CircControl changed to PickupLibrary'
149 C4::Circulation::_GetCircControlBranch($item, $borrower),
150 $library2->{branchcode},
151 '_GetCircControlBranch returned current branch'
154 # Userenv set, PatronLibrary
155 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
157 C4::Context->preference('CircControl'),
159 'CircControl changed to PatronLibrary'
162 C4::Circulation::_GetCircControlBranch($item, $borrower),
163 $borrower->{branchcode},
164 '_GetCircControlBranch returned borrower branch'
167 # Userenv set, ItemHomeLibrary
168 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
170 C4::Context->preference('CircControl'),
172 'CircControl changed to ItemHomeLibrary'
175 C4::Circulation::_GetCircControlBranch($item, $borrower),
176 $item->{$HomeOrHoldingBranch},
177 '_GetCircControlBranch returned item branch'
180 # Reset initial configuration
181 t::lib::Mocks::mock_preference('CircControl', $CircControl);
183 C4::Context->preference('CircControl'),
185 'CircControl reset to its initial value'
188 # Set a simple circ policy
189 $dbh->do('DELETE FROM issuingrules');
191 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
192 maxissueqty, issuelength, lengthunit,
193 renewalsallowed, renewalperiod,
194 norenewalbefore, auto_renew,
211 # Test C4::Circulation::ProcessOfflinePayment
212 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
214 my ( $original_count ) = $sth->fetchrow_array();
216 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', ?, ? )", undef, $patron_category->{categorycode}, $library2->{branchcode} );
218 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
221 my ( $new_count ) = $sth->fetchrow_array();
223 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
225 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
226 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
227 C4::Context->dbh->do("DELETE FROM accountlines");
229 # CanBookBeRenewed tests
231 # Generate test biblio
232 my $title = 'Silence in the library';
233 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
235 my $barcode = 'R00000342';
236 my $branch = $library2->{branchcode};
238 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
240 homebranch => $branch,
241 holdingbranch => $branch,
243 replacementprice => 12.00,
249 my $barcode2 = 'R00000343';
250 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
252 homebranch => $branch,
253 holdingbranch => $branch,
254 barcode => $barcode2,
255 replacementprice => 23.00,
261 my $barcode3 = 'R00000346';
262 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
264 homebranch => $branch,
265 holdingbranch => $branch,
266 barcode => $barcode3,
267 replacementprice => 23.00,
274 my %renewing_borrower_data = (
276 surname => 'Renewal',
277 categorycode => $patron_category->{categorycode},
278 branchcode => $branch,
281 my %reserving_borrower_data = (
282 firstname => 'Katrin',
283 surname => 'Reservation',
284 categorycode => $patron_category->{categorycode},
285 branchcode => $branch,
288 my %hold_waiting_borrower_data = (
290 surname => 'Reservation',
291 categorycode => $patron_category->{categorycode},
292 branchcode => $branch,
295 my %restricted_borrower_data = (
296 firstname => 'Alice',
297 surname => 'Reservation',
298 categorycode => $patron_category->{categorycode},
299 debarred => '3228-01-01',
300 branchcode => $branch,
303 my %expired_borrower_data = (
306 categorycode => $patron_category->{categorycode},
307 branchcode => $branch,
308 dateexpiry => dt_from_string->subtract( months => 1 ),
311 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
312 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
313 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
314 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
315 my $expired_borrowernumber = AddMember(%expired_borrower_data);
317 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
318 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
319 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
326 my $checkitem = undef;
329 my $issue = AddIssue( $renewing_borrower, $barcode);
330 my $datedue = dt_from_string( $issue->date_due() );
331 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
333 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
334 $datedue = dt_from_string( $issue->date_due() );
335 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
338 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $itemnumber } )->borrowernumber;
339 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
341 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
342 is( $renewokay, 1, 'Can renew, no holds for this title or item');
345 # Biblio-level hold, renewal test
347 $branch, $reserving_borrowernumber, $biblionumber,
348 $bibitems, $priority, $resdate, $expdate, $notes,
349 $title, $checkitem, $found
352 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
353 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
354 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
355 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
356 is( $renewokay, 1, '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, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
360 # Now let's add an item level hold, we should no longer be able to renew the item
361 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
363 borrowernumber => $hold_waiting_borrowernumber,
364 biblionumber => $biblionumber,
365 itemnumber => $itemnumber,
366 branchcode => $branch,
370 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
371 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
374 # 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
375 # be able to renew these items
376 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
378 borrowernumber => $hold_waiting_borrowernumber,
379 biblionumber => $biblionumber,
380 itemnumber => $itemnumber3,
381 branchcode => $branch,
386 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
387 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
388 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
389 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
390 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
392 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
393 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
394 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
396 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
397 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
398 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
400 my $reserveid = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
401 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
402 AddIssue($reserving_borrower, $barcode3);
403 my $reserve = $dbh->selectrow_hashref(
404 'SELECT * FROM old_reserves WHERE reserve_id = ?',
408 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
410 # Item-level hold, renewal test
412 $branch, $reserving_borrowernumber, $biblionumber,
413 $bibitems, $priority, $resdate, $expdate, $notes,
414 $title, $itemnumber, $found
417 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
418 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
419 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
421 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
422 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
424 # Items can't fill hold for reasons
425 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
426 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
427 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
428 ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber);
430 # FIXME: Add more for itemtype not for loan etc.
432 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
433 my $barcode5 = 'R00000347';
434 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
436 homebranch => $branch,
437 holdingbranch => $branch,
438 barcode => $barcode5,
439 replacementprice => 23.00,
444 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
445 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
447 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
448 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
449 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
450 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
451 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
453 # Users cannot renew an overdue item
454 my $barcode6 = 'R00000348';
455 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
457 homebranch => $branch,
458 holdingbranch => $branch,
459 barcode => $barcode6,
460 replacementprice => 23.00,
466 my $barcode7 = 'R00000349';
467 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
469 homebranch => $branch,
470 holdingbranch => $branch,
471 barcode => $barcode7,
472 replacementprice => 23.00,
477 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
478 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
480 my $now = dt_from_string();
481 my $five_weeks = DateTime::Duration->new(weeks => 5);
482 my $five_weeks_ago = $now - $five_weeks;
483 t::lib::Mocks::mock_preference('finesMode', 'production');
485 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
486 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
488 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
489 C4::Overdues::UpdateFine(
491 issue_id => $passeddatedue1->id(),
492 itemnumber => $itemnumber7,
493 borrowernumber => $renewing_borrower->{borrowernumber},
496 due => Koha::DateUtils::output_pref($five_weeks_ago)
500 t::lib::Mocks::mock_preference('RenewalLog', 0);
501 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
502 my $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
503 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
504 my $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
505 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
507 t::lib::Mocks::mock_preference('RenewalLog', 1);
508 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
509 $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
510 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
511 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
512 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
514 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
515 is( $fines->count, 2 );
516 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
517 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
521 my $old_issue_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
522 my $old_renew_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
523 AddIssue( $renewing_borrower,$barcode7,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
524 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
525 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
526 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
527 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
529 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
532 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
533 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
534 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
535 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
536 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
539 $hold = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next;
543 # Test automatic renewal before value for "norenewalbefore" in policy is set
544 # In this case automatic renewal is not permitted prior to due date
545 my $barcode4 = '11235813';
546 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
548 homebranch => $branch,
549 holdingbranch => $branch,
550 barcode => $barcode4,
551 replacementprice => 16.00,
557 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
558 ( $renewokay, $error ) =
559 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
560 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
561 is( $error, 'auto_too_soon',
562 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
565 # Test premature manual renewal
566 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
568 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
569 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
570 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
573 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
574 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
576 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
577 $datedue->clone->add( days => -7 ),
578 'Bug 14395: Renewals permitted 7 days before due date, as expected'
582 # Test 'date' setting for syspref NoRenewalBeforePrecision
583 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
585 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
586 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
587 'Bug 14395: Renewals permitted 7 days before due date, as expected'
591 # Test premature automatic renewal
592 ( $renewokay, $error ) =
593 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
594 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
595 is( $error, 'auto_too_soon',
596 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
599 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
600 # and test automatic renewal again
601 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
602 ( $renewokay, $error ) =
603 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
604 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
605 is( $error, 'auto_too_soon',
606 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
609 # Change policy so that loans can be renewed 99 days prior to the due date
610 # and test automatic renewal again
611 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
612 ( $renewokay, $error ) =
613 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
614 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
615 is( $error, 'auto_renew',
616 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
619 subtest "too_late_renewal / no_auto_renewal_after" => sub {
621 my $item_to_auto_renew = $builder->build(
624 biblionumber => $biblionumber,
625 homebranch => $branch,
626 holdingbranch => $branch,
631 my $ten_days_before = dt_from_string->add( days => -10 );
632 my $ten_days_ahead = dt_from_string->add( days => 10 );
633 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
635 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
636 ( $renewokay, $error ) =
637 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
638 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
639 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
641 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
642 ( $renewokay, $error ) =
643 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
644 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
645 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
647 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
648 ( $renewokay, $error ) =
649 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
650 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
651 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
653 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
654 ( $renewokay, $error ) =
655 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
656 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
657 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
659 $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 ) );
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_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
665 $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 ) );
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_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
671 $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 ) );
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_renew', 'Cannot renew, renew is automatic' );
678 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
680 my $item_to_auto_renew = $builder->build({
683 biblionumber => $biblionumber,
684 homebranch => $branch,
685 holdingbranch => $branch,
689 my $ten_days_before = dt_from_string->add( days => -10 );
690 my $ten_days_ahead = dt_from_string->add( days => 10 );
691 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
693 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
694 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
695 C4::Context->set_preference('OPACFineNoRenewals','10');
696 my $fines_amount = 5;
697 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
698 ( $renewokay, $error ) =
699 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
700 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
701 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
703 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
704 ( $renewokay, $error ) =
705 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
706 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
707 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
709 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
710 ( $renewokay, $error ) =
711 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
712 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
713 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
715 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
718 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
720 my $item_to_auto_renew = $builder->build({
723 biblionumber => $biblionumber,
724 homebranch => $branch,
725 holdingbranch => $branch,
729 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
731 my $ten_days_before = dt_from_string->add( days => -10 );
732 my $ten_days_ahead = dt_from_string->add( days => 10 );
734 # Patron is expired and BlockExpiredPatronOpacActions=0
735 # => auto renew is allowed
736 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
737 my $patron = $expired_borrower;
738 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
739 ( $renewokay, $error ) =
740 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
741 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
742 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
743 Koha::Checkouts->find( $checkout->issue_id )->delete;
746 # Patron is expired and BlockExpiredPatronOpacActions=1
747 # => auto renew is not allowed
748 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
749 $patron = $expired_borrower;
750 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
751 ( $renewokay, $error ) =
752 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
753 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
754 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
755 Koha::Checkouts->find( $checkout->issue_id )->delete;
758 # Patron is not expired and BlockExpiredPatronOpacActions=1
759 # => auto renew is allowed
760 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
761 $patron = $renewing_borrower;
762 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
763 ( $renewokay, $error ) =
764 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
765 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
766 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
767 Koha::Checkouts->find( $checkout->issue_id )->delete;
770 subtest "GetLatestAutoRenewDate" => sub {
772 my $item_to_auto_renew = $builder->build(
775 biblionumber => $biblionumber,
776 homebranch => $branch,
777 holdingbranch => $branch,
782 my $ten_days_before = dt_from_string->add( days => -10 );
783 my $ten_days_ahead = dt_from_string->add( days => 10 );
784 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
785 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
786 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
787 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' );
788 my $five_days_before = dt_from_string->add( days => -5 );
789 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
790 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
791 is( $latest_auto_renew_date->truncate( to => 'minute' ),
792 $five_days_before->truncate( to => 'minute' ),
793 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
795 my $five_days_ahead = dt_from_string->add( days => 5 );
796 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
797 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
798 is( $latest_auto_renew_date->truncate( to => 'minute' ),
799 $five_days_ahead->truncate( to => 'minute' ),
800 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
802 my $two_days_ahead = dt_from_string->add( days => 2 );
803 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 2 ) );
804 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
805 is( $latest_auto_renew_date->truncate( to => 'day' ),
806 $two_days_ahead->truncate( to => 'day' ),
807 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
809 $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 ) );
810 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
811 is( $latest_auto_renew_date->truncate( to => 'day' ),
812 $two_days_ahead->truncate( to => 'day' ),
813 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
820 # set policy to forbid renewals
821 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
823 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
824 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
825 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
827 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
828 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
829 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
831 C4::Overdues::UpdateFine(
833 issue_id => $issue->id(),
834 itemnumber => $itemnumber,
835 borrowernumber => $renewing_borrower->{borrowernumber},
838 due => Koha::DateUtils::output_pref($datedue)
842 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
843 is( $line->accounttype, 'FU', 'Account line type is FU' );
844 is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
845 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
846 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
847 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
849 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
850 is( $offset->type, 'Fine', 'Account offset type is Fine' );
851 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
853 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
854 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
856 LostItem( $itemnumber, 'test', 1 );
858 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
859 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
860 my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
861 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
863 my $total_due = $dbh->selectrow_array(
864 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
865 undef, $renewing_borrower->{borrowernumber}
868 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
870 C4::Context->dbh->do("DELETE FROM accountlines");
872 C4::Overdues::UpdateFine(
874 issue_id => $issue2->id(),
875 itemnumber => $itemnumber2,
876 borrowernumber => $renewing_borrower->{borrowernumber},
879 due => Koha::DateUtils::output_pref($datedue)
883 LostItem( $itemnumber2, 'test', 0 );
885 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
886 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
887 ok( Koha::Checkouts->find({ itemnumber => $itemnumber2 }), 'LostItem called without forced return has checked in the item' );
889 $total_due = $dbh->selectrow_array(
890 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
891 undef, $renewing_borrower->{borrowernumber}
894 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
896 my $future = dt_from_string();
897 $future->add( days => 7 );
898 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
899 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
901 # Users cannot renew any item if there is an overdue item
902 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
903 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
904 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
905 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
906 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
908 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
909 $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber3 } );
910 LostItem( $itemnumber3, 'test', 0 );
911 my $accountline = Koha::Account::Lines->find( { itemnumber => $itemnumber3 } );
912 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
917 # GetUpcomingDueIssues tests
918 my $barcode = 'R00000342';
919 my $barcode2 = 'R00000343';
920 my $barcode3 = 'R00000344';
921 my $branch = $library2->{branchcode};
923 #Create another record
924 my $title2 = 'Something is worng here';
925 my ($biblionumber2, $biblioitemnumber2) = add_biblio($title2, 'Anonymous');
930 homebranch => $branch,
931 holdingbranch => $branch,
932 barcode => $barcode3,
939 my %a_borrower_data = (
940 firstname => 'Fridolyn',
942 categorycode => $patron_category->{categorycode},
943 branchcode => $branch,
946 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
947 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
949 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
950 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
951 my $today = DateTime->today(time_zone => C4::Context->tz());
953 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
954 my $datedue = dt_from_string( $issue->date_due() );
955 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
956 my $datedue2 = dt_from_string( $issue->date_due() );
960 # GetUpcomingDueIssues tests
962 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
963 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
966 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
967 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
968 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
971 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
972 is ( scalar( @$upcoming_dues ), 1,
973 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
976 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
978 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
980 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
981 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
983 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
984 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
986 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
987 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
989 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
990 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
992 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
993 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
995 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
996 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1001 my $barcode = '1234567890';
1002 my $branch = $library2->{branchcode};
1004 my ($biblionumber, $biblioitemnumber) = add_biblio();
1007 my ( undef, undef, $itemnumber ) = AddItem(
1009 homebranch => $branch,
1010 holdingbranch => $branch,
1011 barcode => $barcode,
1018 my %a_borrower_data = (
1019 firstname => 'Kyle',
1021 categorycode => $patron_category->{categorycode},
1022 branchcode => $branch,
1025 my $borrowernumber = AddMember(%a_borrower_data);
1027 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1028 my $issue = AddIssue( $borrower, $barcode );
1031 issue_id => $issue->id(),
1032 itemnumber => $itemnumber,
1033 borrowernumber => $borrowernumber,
1039 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
1040 my $count = $hr->{count};
1042 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1046 $dbh->do('DELETE FROM issues');
1047 $dbh->do('DELETE FROM items');
1048 $dbh->do('DELETE FROM issuingrules');
1051 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
1052 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1061 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
1063 my $barcode1 = '1234';
1064 my ( undef, undef, $itemnumber1 ) = AddItem(
1066 homebranch => $library2->{branchcode},
1067 holdingbranch => $library2->{branchcode},
1068 barcode => $barcode1,
1073 my $barcode2 = '4321';
1074 my ( undef, undef, $itemnumber2 ) = AddItem(
1076 homebranch => $library2->{branchcode},
1077 holdingbranch => $library2->{branchcode},
1078 barcode => $barcode2,
1084 my $borrowernumber1 = AddMember(
1085 firstname => 'Kyle',
1087 categorycode => $patron_category->{categorycode},
1088 branchcode => $library2->{branchcode},
1090 my $borrowernumber2 = AddMember(
1091 firstname => 'Chelsea',
1093 categorycode => $patron_category->{categorycode},
1094 branchcode => $library2->{branchcode},
1097 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1098 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1100 my $issue = AddIssue( $borrower1, $barcode1 );
1102 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1103 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1106 $library2->{branchcode}, $borrowernumber2, $biblionumber,
1107 '', 1, undef, undef, '',
1111 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1112 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1113 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1114 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1116 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1117 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1118 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1119 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1121 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1122 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1123 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1124 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1126 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1127 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1128 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1129 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1131 # Setting item not checked out to be not for loan but holdable
1132 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
1134 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1135 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' );
1139 # Don't allow renewing onsite checkout
1140 my $barcode = 'R00000XXX';
1141 my $branch = $library->{branchcode};
1143 #Create another record
1144 my ($biblionumber, $biblioitemnumber) = add_biblio('A title', 'Anonymous');
1146 my (undef, undef, $itemnumber) = AddItem(
1148 homebranch => $branch,
1149 holdingbranch => $branch,
1150 barcode => $barcode,
1156 my $borrowernumber = AddMember(
1159 categorycode => $patron_category->{categorycode},
1160 branchcode => $branch,
1163 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1165 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1166 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
1167 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1168 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1172 my $library = $builder->build({ source => 'Branch' });
1174 my ($biblionumber, $biblioitemnumber) = add_biblio();
1176 my $barcode = 'just a barcode';
1177 my ( undef, undef, $itemnumber ) = AddItem(
1179 homebranch => $library->{branchcode},
1180 holdingbranch => $library->{branchcode},
1181 barcode => $barcode,
1187 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1189 my $issue = AddIssue( $patron, $barcode );
1192 issue_id => $issue->id(),
1193 itemnumber => $itemnumber,
1194 borrowernumber => $patron->{borrowernumber},
1201 issue_id => $issue->id(),
1202 itemnumber => $itemnumber,
1203 borrowernumber => $patron->{borrowernumber},
1208 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1211 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1214 my $homebranch = $builder->build( { source => 'Branch' } );
1215 my $holdingbranch = $builder->build( { source => 'Branch' } );
1216 my $otherbranch = $builder->build( { source => 'Branch' } );
1217 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1218 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1220 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1221 my $item = $builder->build(
1224 homebranch => $homebranch->{branchcode},
1225 holdingbranch => $holdingbranch->{branchcode},
1226 biblionumber => $biblioitem->{biblionumber}
1231 set_userenv($holdingbranch);
1233 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1234 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Checkout
1236 my ( $error, $question, $alerts );
1238 # AllowReturnToBranch == anywhere
1239 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1240 ## Test that unknown barcodes don't generate internal server errors
1241 set_userenv($homebranch);
1242 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1243 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1244 ## Can be issued from homebranch
1245 set_userenv($homebranch);
1246 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1247 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1248 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1249 ## Can be issued from holdingbranch
1250 set_userenv($holdingbranch);
1251 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1252 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1253 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1254 ## Can be issued from another branch
1255 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1256 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1257 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1259 # AllowReturnToBranch == holdingbranch
1260 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1261 ## Cannot be issued from homebranch
1262 set_userenv($homebranch);
1263 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1264 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1265 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1266 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1267 ## Can be issued from holdinbranch
1268 set_userenv($holdingbranch);
1269 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1270 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1271 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1272 ## Cannot be issued from another branch
1273 set_userenv($otherbranch);
1274 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1275 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1276 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1277 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1279 # AllowReturnToBranch == homebranch
1280 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1281 ## Can be issued from holdinbranch
1282 set_userenv($homebranch);
1283 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1284 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1285 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1286 ## Cannot be issued from holdinbranch
1287 set_userenv($holdingbranch);
1288 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1289 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1290 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1291 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1292 ## Cannot be issued from holdinbranch
1293 set_userenv($otherbranch);
1294 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1295 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1296 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1297 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1299 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1302 subtest 'AddIssue & AllowReturnToBranch' => sub {
1305 my $homebranch = $builder->build( { source => 'Branch' } );
1306 my $holdingbranch = $builder->build( { source => 'Branch' } );
1307 my $otherbranch = $builder->build( { source => 'Branch' } );
1308 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1309 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1311 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1312 my $item = $builder->build(
1315 homebranch => $homebranch->{branchcode},
1316 holdingbranch => $holdingbranch->{branchcode},
1320 biblionumber => $biblioitem->{biblionumber}
1325 set_userenv($holdingbranch);
1327 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Checkout
1328 my $issue = AddIssue( $patron_1, $item->{barcode} );
1330 my ( $error, $question, $alerts );
1332 # AllowReturnToBranch == homebranch
1333 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1334 ## Can be issued from homebranch
1335 set_userenv($homebranch);
1336 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1337 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1338 ## Can be issued from holdinbranch
1339 set_userenv($holdingbranch);
1340 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1341 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1342 ## Can be issued from another branch
1343 set_userenv($otherbranch);
1344 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1345 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1347 # AllowReturnToBranch == holdinbranch
1348 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1349 ## Cannot be issued from homebranch
1350 set_userenv($homebranch);
1351 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1352 ## Can be issued from holdingbranch
1353 set_userenv($holdingbranch);
1354 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1355 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1356 ## Cannot be issued from another branch
1357 set_userenv($otherbranch);
1358 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1360 # AllowReturnToBranch == homebranch
1361 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1362 ## Can be issued from homebranch
1363 set_userenv($homebranch);
1364 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1365 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1366 ## Cannot be issued from holdinbranch
1367 set_userenv($holdingbranch);
1368 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1369 ## Cannot be issued from another branch
1370 set_userenv($otherbranch);
1371 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1372 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1375 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1378 my $library = $builder->build( { source => 'Branch' } );
1379 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1381 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1382 my $item_1 = $builder->build(
1385 homebranch => $library->{branchcode},
1386 holdingbranch => $library->{branchcode},
1387 biblionumber => $biblioitem_1->{biblionumber}
1391 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1392 my $item_2 = $builder->build(
1395 homebranch => $library->{branchcode},
1396 holdingbranch => $library->{branchcode},
1397 biblionumber => $biblioitem_2->{biblionumber}
1402 my ( $error, $question, $alerts );
1404 # Patron cannot issue item_1, they have overdues
1405 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1406 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1408 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1409 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1410 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1411 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1413 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1414 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1415 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1416 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1418 # Patron cannot issue item_1, they are debarred
1419 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1420 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1421 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1422 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1423 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1425 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1426 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1427 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1428 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1431 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1434 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1435 my $patron_category_x = $builder->build_object(
1437 class => 'Koha::Patron::Categories',
1438 value => { category_type => 'X' }
1441 my $patron = $builder->build_object(
1443 class => 'Koha::Patrons',
1445 categorycode => $patron_category_x->categorycode,
1446 gonenoaddress => undef,
1453 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1454 my $item_1 = $builder->build(
1458 homebranch => $library->branchcode,
1459 holdingbranch => $library->branchcode,
1460 biblionumber => $biblioitem_1->{biblionumber}
1465 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1466 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1468 # TODO There are other tests to provide here
1471 subtest 'MultipleReserves' => sub {
1474 my $title = 'Silence in the library';
1475 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
1477 my $branch = $library2->{branchcode};
1479 my $barcode1 = 'R00110001';
1480 my ( $item_bibnum1, $item_bibitemnum1, $itemnumber1 ) = AddItem(
1482 homebranch => $branch,
1483 holdingbranch => $branch,
1484 barcode => $barcode1,
1485 replacementprice => 12.00,
1491 my $barcode2 = 'R00110002';
1492 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
1494 homebranch => $branch,
1495 holdingbranch => $branch,
1496 barcode => $barcode2,
1497 replacementprice => 12.00,
1505 my $resdate = undef;
1506 my $expdate = undef;
1508 my $checkitem = undef;
1511 my %renewing_borrower_data = (
1512 firstname => 'John',
1513 surname => 'Renewal',
1514 categorycode => $patron_category->{categorycode},
1515 branchcode => $branch,
1517 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
1518 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1519 my $issue = AddIssue( $renewing_borrower, $barcode1);
1520 my $datedue = dt_from_string( $issue->date_due() );
1521 is (defined $issue->date_due(), 1, "item 1 checked out");
1522 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $itemnumber1 })->borrowernumber;
1524 my %reserving_borrower_data1 = (
1525 firstname => 'Katrin',
1526 surname => 'Reservation',
1527 categorycode => $patron_category->{categorycode},
1528 branchcode => $branch,
1530 my $reserving_borrowernumber1 = AddMember(%reserving_borrower_data1);
1532 $branch, $reserving_borrowernumber1, $biblionumber,
1533 $bibitems, $priority, $resdate, $expdate, $notes,
1534 $title, $checkitem, $found
1537 my %reserving_borrower_data2 = (
1538 firstname => 'Kirk',
1539 surname => 'Reservation',
1540 categorycode => $patron_category->{categorycode},
1541 branchcode => $branch,
1543 my $reserving_borrowernumber2 = AddMember(%reserving_borrower_data2);
1545 $branch, $reserving_borrowernumber2, $biblionumber,
1546 $bibitems, $priority, $resdate, $expdate, $notes,
1547 $title, $checkitem, $found
1551 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1552 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1555 my $barcode3 = 'R00110003';
1556 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
1558 homebranch => $branch,
1559 holdingbranch => $branch,
1560 barcode => $barcode3,
1561 replacementprice => 12.00,
1568 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1569 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1573 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1576 my $library = $builder->build( { source => 'Branch' } );
1577 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1579 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1580 my $biblionumber = $biblioitem->{biblionumber};
1581 my $item_1 = $builder->build(
1584 homebranch => $library->{branchcode},
1585 holdingbranch => $library->{branchcode},
1586 biblionumber => $biblionumber,
1590 my $item_2 = $builder->build(
1593 homebranch => $library->{branchcode},
1594 holdingbranch => $library->{branchcode},
1595 biblionumber => $biblionumber,
1600 my ( $error, $question, $alerts );
1601 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1603 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1604 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1605 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1606 is( $question->{BIBLIO_ALREADY_ISSUED}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' . str($error, $question, $alerts) );
1608 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1609 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1610 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1612 # Add a subscription
1613 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1615 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1616 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1617 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str($error, $question, $alerts) );
1619 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1620 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1621 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str($error, $question, $alerts) );
1624 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1627 my $library = $builder->build( { source => 'Branch' } );
1628 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1631 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1632 my $item_1 = $builder->build(
1636 homebranch => $library->{branchcode},
1637 holdingbranch => $library->{branchcode},
1641 biblionumber => $biblioitem_1->{biblionumber}
1645 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1646 my $item_2 = $builder->build(
1650 homebranch => $library->{branchcode},
1651 holdingbranch => $library->{branchcode},
1655 biblionumber => $biblioitem_2->{biblionumber}
1660 # And the issuing rule
1661 Koha::IssuingRules->search->delete;
1662 my $rule = Koha::IssuingRule->new(
1664 categorycode => '*',
1669 firstremind => 1, # 1 day of grace
1670 finedays => 2, # 2 days of fine per day of overdue
1671 lengthunit => 'days',
1676 # Patron cannot issue item_1, they have overdues
1677 my $five_days_ago = dt_from_string->subtract( days => 5 );
1678 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1679 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1680 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1681 ; # Add another overdue
1683 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1684 AddReturn( $item_1->{barcode}, $library->{branchcode},
1685 undef, undef, dt_from_string );
1686 my $debarments = Koha::Patron::Debarments::GetDebarments(
1687 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1688 is( scalar(@$debarments), 1 );
1690 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1691 # Same for the others
1692 my $expected_expiration = output_pref(
1694 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1695 dateformat => 'sql',
1699 is( $debarments->[0]->{expiration}, $expected_expiration );
1701 AddReturn( $item_2->{barcode}, $library->{branchcode},
1702 undef, undef, dt_from_string );
1703 $debarments = Koha::Patron::Debarments::GetDebarments(
1704 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1705 is( scalar(@$debarments), 1 );
1706 $expected_expiration = output_pref(
1708 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1709 dateformat => 'sql',
1713 is( $debarments->[0]->{expiration}, $expected_expiration );
1715 Koha::Patron::Debarments::DelUniqueDebarment(
1716 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1718 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1719 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1720 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1721 ; # Add another overdue
1722 AddReturn( $item_1->{barcode}, $library->{branchcode},
1723 undef, undef, dt_from_string );
1724 $debarments = Koha::Patron::Debarments::GetDebarments(
1725 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1726 is( scalar(@$debarments), 1 );
1727 $expected_expiration = output_pref(
1729 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1730 dateformat => 'sql',
1734 is( $debarments->[0]->{expiration}, $expected_expiration );
1736 AddReturn( $item_2->{barcode}, $library->{branchcode},
1737 undef, undef, dt_from_string );
1738 $debarments = Koha::Patron::Debarments::GetDebarments(
1739 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1740 is( scalar(@$debarments), 1 );
1741 $expected_expiration = output_pref(
1743 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1744 dateformat => 'sql',
1748 is( $debarments->[0]->{expiration}, $expected_expiration );
1751 subtest 'AddReturn + suspension_chargeperiod' => sub {
1754 my $library = $builder->build( { source => 'Branch' } );
1755 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1758 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1759 my $item_1 = $builder->build(
1763 homebranch => $library->{branchcode},
1764 holdingbranch => $library->{branchcode},
1768 biblionumber => $biblioitem_1->{biblionumber}
1773 # And the issuing rule
1774 Koha::IssuingRules->search->delete;
1775 my $rule = Koha::IssuingRule->new(
1777 categorycode => '*',
1782 firstremind => 0, # 0 day of grace
1783 finedays => 2, # 2 days of fine per day of overdue
1784 suspension_chargeperiod => 1,
1785 lengthunit => 'days',
1790 my $five_days_ago = dt_from_string->subtract( days => 5 );
1791 # We want to charge 2 days every day, without grace
1792 # With 5 days of overdue: 5 * Z
1793 my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1794 test_debarment_on_checkout(
1797 library => $library,
1799 due_date => $five_days_ago,
1800 expiration_date => $expected_expiration,
1804 # We want to charge 2 days every 2 days, without grace
1805 # With 5 days of overdue: (5 * 2) / 2
1806 $rule->suspension_chargeperiod(2)->store;
1807 $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1808 test_debarment_on_checkout(
1811 library => $library,
1813 due_date => $five_days_ago,
1814 expiration_date => $expected_expiration,
1818 # We want to charge 2 days every 3 days, with 1 day of grace
1819 # With 5 days of overdue: ((5-1) / 3 ) * 2
1820 $rule->suspension_chargeperiod(3)->store;
1821 $rule->firstremind(1)->store;
1822 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1823 test_debarment_on_checkout(
1826 library => $library,
1828 due_date => $five_days_ago,
1829 expiration_date => $expected_expiration,
1833 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1834 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1835 $rule->finedays(2)->store;
1836 $rule->suspension_chargeperiod(1)->store;
1837 $rule->firstremind(0)->store;
1838 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1840 # Adding a holiday 2 days ago
1841 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1842 my $two_days_ago = dt_from_string->subtract( days => 2 );
1843 $calendar->insert_single_holiday(
1844 day => $two_days_ago->day,
1845 month => $two_days_ago->month,
1846 year => $two_days_ago->year,
1847 title => 'holidayTest-2d',
1848 description => 'holidayDesc 2 days ago'
1850 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1851 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1852 test_debarment_on_checkout(
1855 library => $library,
1857 due_date => $five_days_ago,
1858 expiration_date => $expected_expiration,
1862 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1863 my $two_days_ahead = dt_from_string->add( days => 2 );
1864 $calendar->insert_single_holiday(
1865 day => $two_days_ahead->day,
1866 month => $two_days_ahead->month,
1867 year => $two_days_ahead->year,
1868 title => 'holidayTest+2d',
1869 description => 'holidayDesc 2 days ahead'
1872 # Same as above, but we should skip D+2
1873 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1874 test_debarment_on_checkout(
1877 library => $library,
1879 due_date => $five_days_ago,
1880 expiration_date => $expected_expiration,
1884 # Adding another holiday, day of expiration date
1885 my $expected_expiration_dt = dt_from_string($expected_expiration);
1886 $calendar->insert_single_holiday(
1887 day => $expected_expiration_dt->day,
1888 month => $expected_expiration_dt->month,
1889 year => $expected_expiration_dt->year,
1890 title => 'holidayTest_exp',
1891 description => 'holidayDesc on expiration date'
1893 # Expiration date will be the day after
1894 test_debarment_on_checkout(
1897 library => $library,
1899 due_date => $five_days_ago,
1900 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1904 test_debarment_on_checkout(
1907 library => $library,
1909 return_date => dt_from_string->add(days => 5),
1910 expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1915 subtest 'AddReturn | is_overdue' => sub {
1918 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
1919 t::lib::Mocks::mock_preference('finesMode', 'production');
1920 t::lib::Mocks::mock_preference('MaxFine', '100');
1922 my $library = $builder->build( { source => 'Branch' } );
1923 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1925 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1926 my $item = $builder->build(
1930 homebranch => $library->{branchcode},
1931 holdingbranch => $library->{branchcode},
1935 biblionumber => $biblioitem->{biblionumber},
1940 Koha::IssuingRules->search->delete;
1941 my $rule = Koha::IssuingRule->new(
1943 categorycode => '*',
1948 lengthunit => 'days',
1949 fine => 1, # Charge 1 every day of overdue
1955 my $one_day_ago = dt_from_string->subtract( days => 1 );
1956 my $five_days_ago = dt_from_string->subtract( days => 5 );
1957 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1958 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1960 # No date specify, today will be used
1961 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1962 AddReturn( $item->{barcode}, $library->{branchcode} );
1963 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1964 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1966 # specify return date 5 days before => no overdue
1967 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1968 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $ten_days_ago );
1969 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1970 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1972 # specify return date 5 days later => overdue
1973 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1974 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $five_days_ago );
1975 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1976 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1978 # specify dropbox date 5 days before => no overdue
1979 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1980 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $ten_days_ago );
1981 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1982 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1984 # specify dropbox date 5 days later => overdue, or... not
1985 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1986 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $five_days_ago );
1987 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue in dropbox mode' ); # FIXME? This is weird, the FU fine is created ( _CalculateAndUpdateFine > C4::Overdues::UpdateFine ) then remove later (in _FixOverduesOnReturn). Looks like it is a feature
1988 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1991 subtest '_FixAccountForLostAndReturned' => sub {
1994 # Generate test biblio
1995 my $title = 'Koha for Dummies';
1996 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Daria');
1998 my $barcode = 'KD123456789';
1999 my $branchcode = $library2->{branchcode};
2001 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
2003 homebranch => $branchcode,
2004 holdingbranch => $branchcode,
2005 barcode => $barcode,
2006 replacementprice => 99.00,
2012 my $patron = $builder->build( { source => 'Borrower' } );
2014 Koha::Account::Line->new(
2016 borrowernumber => $patron->{borrowernumber},
2018 itemnumber => $itemnumber,
2020 amountoutstanding => 10.00,
2024 my $accountline = Koha::Account::Line->new(
2026 borrowernumber => $patron->{borrowernumber},
2028 itemnumber => $itemnumber,
2030 amountoutstanding => 99.00,
2034 C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->{borrowernumber} );
2036 $accountline->_result()->discard_changes();
2038 is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount' );
2039 is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )');
2042 subtest '_FixOverduesOnReturn' => sub {
2045 # Generate test biblio
2046 my $title = 'Koha for Dummies';
2047 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Kylie');
2049 my $barcode = 'KD987654321';
2050 my $branchcode = $library2->{branchcode};
2052 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
2054 homebranch => $branchcode,
2055 holdingbranch => $branchcode,
2056 barcode => $barcode,
2057 replacementprice => 99.00,
2063 my $patron = $builder->build( { source => 'Borrower' } );
2065 ## Start with basic call, should just close out the open fine
2066 my $accountline = Koha::Account::Line->new(
2068 borrowernumber => $patron->{borrowernumber},
2069 accounttype => 'FU',
2070 itemnumber => $itemnumber,
2072 amountoutstanding => 99.00,
2073 lastincrement => 9.00,
2077 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber );
2079 $accountline->_result()->discard_changes();
2081 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2082 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2085 ## Run again, with exemptfine enabled
2088 accounttype => 'FU',
2089 amountoutstanding => 99.00,
2093 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 1 );
2095 $accountline->_result()->discard_changes();
2097 is( $accountline->amountoutstanding, '0.000000', 'Fine has been reduced to 0' );
2098 is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2100 ## Run again, with dropbox mode enabled
2103 accounttype => 'FU',
2104 amountoutstanding => 99.00,
2108 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 0, 1 );
2110 $accountline->_result()->discard_changes();
2112 is( $accountline->amountoutstanding, '90.000000', 'Fine has been reduced to 90' );
2113 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2116 subtest 'Set waiting flag' => sub {
2119 my $library_1 = $builder->build( { source => 'Branch' } );
2120 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2121 my $library_2 = $builder->build( { source => 'Branch' } );
2122 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2124 my $biblio = $builder->build( { source => 'Biblio' } );
2125 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2127 my $item = $builder->build(
2131 homebranch => $library_1->{branchcode},
2132 holdingbranch => $library_1->{branchcode},
2136 biblionumber => $biblioitem->{biblionumber},
2141 set_userenv( $library_2 );
2142 my $reserve_id = AddReserve(
2143 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2144 '', 1, undef, undef, '', undef, $item->{itemnumber},
2147 set_userenv( $library_1 );
2148 my $do_transfer = 1;
2149 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2150 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2151 my $hold = Koha::Holds->find( $reserve_id );
2152 is( $hold->found, 'T', 'Hold is in transit' );
2154 my ( $status ) = CheckReserves($item->{itemnumber});
2155 is( $status, 'Reserved', 'Hold is not waiting yet');
2157 set_userenv( $library_2 );
2159 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2160 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2161 $hold = Koha::Holds->find( $reserve_id );
2162 is( $hold->found, 'W', 'Hold is waiting' );
2163 ( $status ) = CheckReserves($item->{itemnumber});
2164 is( $status, 'Waiting', 'Now the hold is waiting');
2167 subtest 'CanBookBeIssued | is_overdue' => sub {
2170 # Set a simple circ policy
2171 $dbh->do('DELETE FROM issuingrules');
2173 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2174 maxissueqty, issuelength, lengthunit,
2175 renewalsallowed, renewalperiod,
2176 norenewalbefore, auto_renew,
2193 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2194 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2195 my $library = $builder->build( { source => 'Branch' } );
2196 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2198 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2199 my $item = $builder->build(
2203 homebranch => $library->{branchcode},
2204 holdingbranch => $library->{branchcode},
2208 biblionumber => $biblioitem->{biblionumber},
2213 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2214 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2215 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2216 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2217 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2218 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2222 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2225 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2226 my $library = $builder->build( { source => 'Branch' } );
2227 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2229 my $itemtype = $builder->build(
2231 source => 'Itemtype',
2232 value => { notforloan => undef, }
2236 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2237 my $item = $builder->build_object(
2239 class => 'Koha::Items',
2241 homebranch => $library->{branchcode},
2242 holdingbranch => $library->{branchcode},
2246 biblionumber => $biblioitem->{biblionumber},
2247 biblioitemnumber => $biblioitem->{biblioitemnumber},
2252 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2253 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2254 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2257 subtest 'CanBookBeIssued | notforloan' => sub {
2260 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2262 my $library = $builder->build( { source => 'Branch' } );
2263 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2265 my $itemtype = $builder->build(
2267 source => 'Itemtype',
2268 value => { notforloan => undef, }
2272 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2273 my $item = $builder->build_object(
2275 class => 'Koha::Items',
2277 homebranch => $library->{branchcode},
2278 holdingbranch => $library->{branchcode},
2282 itype => $itemtype->{itemtype},
2283 biblionumber => $biblioitem->{biblionumber},
2284 biblioitemnumber => $biblioitem->{biblioitemnumber},
2289 my ( $issuingimpossible, $needsconfirmation );
2292 subtest 'item-level_itypes = 1' => sub {
2295 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2296 # Is for loan at item type and item level
2297 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2298 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2299 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2301 # not for loan at item type level
2302 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2303 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2304 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2307 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2308 'Item can not be issued, not for loan at item type level'
2311 # not for loan at item level
2312 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2313 $item->notforloan( 1 )->store;
2314 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2315 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2318 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2319 'Item can not be issued, not for loan at item type level'
2323 subtest 'item-level_itypes = 0' => sub {
2326 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2328 # We set another itemtype for biblioitem
2329 my $itemtype = $builder->build(
2331 source => 'Itemtype',
2332 value => { notforloan => undef, }
2336 # for loan at item type and item level
2337 $item->notforloan(0)->store;
2338 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
2339 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2340 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2341 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2343 # not for loan at item type level
2344 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2345 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2346 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2349 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2350 'Item can not be issued, not for loan at item type level'
2353 # not for loan at item level
2354 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2355 $item->notforloan( 1 )->store;
2356 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2357 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2360 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2361 'Item can not be issued, not for loan at item type level'
2365 # TODO test with AllowNotForLoanOverride = 1
2368 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
2371 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
2372 my $item = $builder->build_object({ class => 'Koha::Items', value => { onloan => '2018-01-01' }});
2373 AddReturn( $item->barcode, $item->homebranch );
2374 $item->discard_changes; # refresh
2375 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
2378 $schema->storage->txn_rollback;
2379 $cache->clear_from_cache('single_holidays');
2382 my ( $library ) = @_;
2383 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
2387 my ( $error, $question, $alert ) = @_;
2389 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
2390 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
2391 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
2396 my ($title, $author) = @_;
2398 my $marcflavour = C4::Context->preference('marcflavour');
2400 my $biblio = MARC::Record->new();
2402 my $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
2403 $biblio->append_fields(
2404 MARC::Field->new($tag, ' ', ' ', a => $title),
2409 my ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
2410 $biblio->append_fields(
2411 MARC::Field->new($tag, ' ', ' ', $code => $author),
2415 return AddBiblio($biblio, '');
2418 sub test_debarment_on_checkout {
2420 my $item = $params->{item};
2421 my $library = $params->{library};
2422 my $patron = $params->{patron};
2423 my $due_date = $params->{due_date} || dt_from_string;
2424 my $return_date = $params->{return_date} || dt_from_string;
2425 my $expected_expiration_date = $params->{expiration_date};
2427 $expected_expiration_date = output_pref(
2429 dt => $expected_expiration_date,
2430 dateformat => 'sql',
2434 my @caller = caller;
2435 my $line_number = $caller[2];
2436 AddIssue( $patron, $item->{barcode}, $due_date );
2438 my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode},
2439 undef, undef, $return_date );
2440 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
2441 or diag('AddReturn returned message ' . Dumper $message );
2442 my $debarments = Koha::Patron::Debarments::GetDebarments(
2443 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
2444 is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
2446 is( $debarments->[0]->{expiration},
2447 $expected_expiration_date, 'Test at line ' . $line_number );
2448 Koha::Patron::Debarments::DelUniqueDebarment(
2449 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );