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>.
21 use Test::More tests => 123;
25 use POSIX qw( floor );
27 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 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
138 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
140 # Userenv set, PickupLibrary
141 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
143 C4::Context->preference('CircControl'),
145 'CircControl changed to PickupLibrary'
148 C4::Circulation::_GetCircControlBranch($item, $borrower),
149 $library2->{branchcode},
150 '_GetCircControlBranch returned current branch'
153 # Userenv set, PatronLibrary
154 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
156 C4::Context->preference('CircControl'),
158 'CircControl changed to PatronLibrary'
161 C4::Circulation::_GetCircControlBranch($item, $borrower),
162 $borrower->{branchcode},
163 '_GetCircControlBranch returned borrower branch'
166 # Userenv set, ItemHomeLibrary
167 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
169 C4::Context->preference('CircControl'),
171 'CircControl changed to ItemHomeLibrary'
174 C4::Circulation::_GetCircControlBranch($item, $borrower),
175 $item->{$HomeOrHoldingBranch},
176 '_GetCircControlBranch returned item branch'
179 # Reset initial configuration
180 t::lib::Mocks::mock_preference('CircControl', $CircControl);
182 C4::Context->preference('CircControl'),
184 'CircControl reset to its initial value'
187 # Set a simple circ policy
188 $dbh->do('DELETE FROM issuingrules');
190 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
191 maxissueqty, issuelength, lengthunit,
192 renewalsallowed, renewalperiod,
193 norenewalbefore, auto_renew,
210 # Test C4::Circulation::ProcessOfflinePayment
211 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
213 my ( $original_count ) = $sth->fetchrow_array();
215 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', ?, ? )", undef, $patron_category->{categorycode}, $library2->{branchcode} );
217 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
220 my ( $new_count ) = $sth->fetchrow_array();
222 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
224 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
225 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
226 C4::Context->dbh->do("DELETE FROM accountlines");
228 # CanBookBeRenewed tests
229 C4::Context->set_preference('ItemsDeniedRenewal','');
230 # Generate test biblio
231 my $title = 'Silence in the library';
232 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
234 my $barcode = 'R00000342';
235 my $branch = $library2->{branchcode};
237 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
239 homebranch => $branch,
240 holdingbranch => $branch,
242 replacementprice => 12.00,
248 my $barcode2 = 'R00000343';
249 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
251 homebranch => $branch,
252 holdingbranch => $branch,
253 barcode => $barcode2,
254 replacementprice => 23.00,
260 my $barcode3 = 'R00000346';
261 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
263 homebranch => $branch,
264 holdingbranch => $branch,
265 barcode => $barcode3,
266 replacementprice => 23.00,
273 my %renewing_borrower_data = (
275 surname => 'Renewal',
276 categorycode => $patron_category->{categorycode},
277 branchcode => $branch,
280 my %reserving_borrower_data = (
281 firstname => 'Katrin',
282 surname => 'Reservation',
283 categorycode => $patron_category->{categorycode},
284 branchcode => $branch,
287 my %hold_waiting_borrower_data = (
289 surname => 'Reservation',
290 categorycode => $patron_category->{categorycode},
291 branchcode => $branch,
294 my %restricted_borrower_data = (
295 firstname => 'Alice',
296 surname => 'Reservation',
297 categorycode => $patron_category->{categorycode},
298 debarred => '3228-01-01',
299 branchcode => $branch,
302 my %expired_borrower_data = (
305 categorycode => $patron_category->{categorycode},
306 branchcode => $branch,
307 dateexpiry => dt_from_string->subtract( months => 1 ),
310 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
311 my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
312 my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
313 my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
314 my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
316 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
317 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
318 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
325 my $checkitem = undef;
328 my $issue = AddIssue( $renewing_borrower, $barcode);
329 my $datedue = dt_from_string( $issue->date_due() );
330 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
332 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
333 $datedue = dt_from_string( $issue->date_due() );
334 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
337 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $itemnumber } )->borrowernumber;
338 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
340 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
341 is( $renewokay, 1, 'Can renew, no holds for this title or item');
344 # Biblio-level hold, renewal test
346 $branch, $reserving_borrowernumber, $biblionumber,
347 $bibitems, $priority, $resdate, $expdate, $notes,
348 $title, $checkitem, $found
351 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
352 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
353 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
354 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
355 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
356 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
357 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
359 # Now let's add an item level hold, we should no longer be able to renew the item
360 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
362 borrowernumber => $hold_waiting_borrowernumber,
363 biblionumber => $biblionumber,
364 itemnumber => $itemnumber,
365 branchcode => $branch,
369 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
370 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
373 # 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
374 # be able to renew these items
375 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
377 borrowernumber => $hold_waiting_borrowernumber,
378 biblionumber => $biblionumber,
379 itemnumber => $itemnumber3,
380 branchcode => $branch,
385 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
386 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
387 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
388 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
389 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
391 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
392 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
393 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
395 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
396 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
397 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
399 my $reserveid = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
400 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
401 AddIssue($reserving_borrower, $barcode3);
402 my $reserve = $dbh->selectrow_hashref(
403 'SELECT * FROM old_reserves WHERE reserve_id = ?',
407 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
409 # Item-level hold, renewal test
411 $branch, $reserving_borrowernumber, $biblionumber,
412 $bibitems, $priority, $resdate, $expdate, $notes,
413 $title, $itemnumber, $found
416 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
417 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
418 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
420 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
421 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
423 # Items can't fill hold for reasons
424 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
425 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
426 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
427 ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber);
429 # FIXME: Add more for itemtype not for loan etc.
431 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
432 my $barcode5 = 'R00000347';
433 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
435 homebranch => $branch,
436 holdingbranch => $branch,
437 barcode => $barcode5,
438 replacementprice => 23.00,
443 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
444 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
446 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
447 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
448 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
449 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
450 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
452 # Users cannot renew an overdue item
453 my $barcode6 = 'R00000348';
454 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
456 homebranch => $branch,
457 holdingbranch => $branch,
458 barcode => $barcode6,
459 replacementprice => 23.00,
465 my $barcode7 = 'R00000349';
466 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
468 homebranch => $branch,
469 holdingbranch => $branch,
470 barcode => $barcode7,
471 replacementprice => 23.00,
476 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
477 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
479 my $now = dt_from_string();
480 my $five_weeks = DateTime::Duration->new(weeks => 5);
481 my $five_weeks_ago = $now - $five_weeks;
482 t::lib::Mocks::mock_preference('finesMode', 'production');
484 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
485 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
487 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
488 C4::Overdues::UpdateFine(
490 issue_id => $passeddatedue1->id(),
491 itemnumber => $itemnumber7,
492 borrowernumber => $renewing_borrower->{borrowernumber},
495 due => Koha::DateUtils::output_pref($five_weeks_ago)
499 t::lib::Mocks::mock_preference('RenewalLog', 0);
500 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
501 my $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
502 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
503 my $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
504 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
506 t::lib::Mocks::mock_preference('RenewalLog', 1);
507 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
508 $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
509 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
510 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
511 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
513 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
514 is( $fines->count, 2 );
515 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
516 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
520 my $old_issue_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
521 my $old_renew_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
522 AddIssue( $renewing_borrower,$barcode7,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
523 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
524 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
525 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
526 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
528 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
531 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
532 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
533 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
534 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
535 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
538 $hold = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next;
542 # Test automatic renewal before value for "norenewalbefore" in policy is set
543 # In this case automatic renewal is not permitted prior to due date
544 my $barcode4 = '11235813';
545 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
547 homebranch => $branch,
548 holdingbranch => $branch,
549 barcode => $barcode4,
550 replacementprice => 16.00,
556 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
557 ( $renewokay, $error ) =
558 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
559 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
560 is( $error, 'auto_too_soon',
561 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
564 # Test premature manual renewal
565 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
567 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
568 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
569 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
572 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
573 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
575 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
576 $datedue->clone->add( days => -7 ),
577 'Bug 14395: Renewals permitted 7 days before due date, as expected'
581 # Test 'date' setting for syspref NoRenewalBeforePrecision
582 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
584 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
585 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
586 'Bug 14395: Renewals permitted 7 days before due date, as expected'
590 # Test premature automatic renewal
591 ( $renewokay, $error ) =
592 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
593 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
594 is( $error, 'auto_too_soon',
595 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
598 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
599 # and test automatic renewal again
600 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
601 ( $renewokay, $error ) =
602 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
603 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
604 is( $error, 'auto_too_soon',
605 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
608 # Change policy so that loans can be renewed 99 days prior to the due date
609 # and test automatic renewal again
610 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
611 ( $renewokay, $error ) =
612 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
613 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
614 is( $error, 'auto_renew',
615 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
618 subtest "too_late_renewal / no_auto_renewal_after" => sub {
620 my $item_to_auto_renew = $builder->build(
623 biblionumber => $biblionumber,
624 homebranch => $branch,
625 holdingbranch => $branch,
630 my $ten_days_before = dt_from_string->add( days => -10 );
631 my $ten_days_ahead = dt_from_string->add( days => 10 );
632 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
634 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
635 ( $renewokay, $error ) =
636 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
637 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
638 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
640 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
641 ( $renewokay, $error ) =
642 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
643 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
644 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
646 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
647 ( $renewokay, $error ) =
648 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
649 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
650 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
652 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
653 ( $renewokay, $error ) =
654 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
655 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
656 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
658 $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 ) );
659 ( $renewokay, $error ) =
660 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
661 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
662 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
664 $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 ) );
665 ( $renewokay, $error ) =
666 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
667 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
668 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
670 $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 ) );
671 ( $renewokay, $error ) =
672 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
673 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
674 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
677 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
679 my $item_to_auto_renew = $builder->build({
682 biblionumber => $biblionumber,
683 homebranch => $branch,
684 holdingbranch => $branch,
688 my $ten_days_before = dt_from_string->add( days => -10 );
689 my $ten_days_ahead = dt_from_string->add( days => 10 );
690 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
692 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
693 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
694 C4::Context->set_preference('OPACFineNoRenewals','10');
695 my $fines_amount = 5;
696 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
697 ( $renewokay, $error ) =
698 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
699 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
700 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
702 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
703 ( $renewokay, $error ) =
704 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
705 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
706 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
708 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
709 ( $renewokay, $error ) =
710 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
711 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
712 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
714 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
717 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
719 my $item_to_auto_renew = $builder->build({
722 biblionumber => $biblionumber,
723 homebranch => $branch,
724 holdingbranch => $branch,
728 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
730 my $ten_days_before = dt_from_string->add( days => -10 );
731 my $ten_days_ahead = dt_from_string->add( days => 10 );
733 # Patron is expired and BlockExpiredPatronOpacActions=0
734 # => auto renew is allowed
735 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
736 my $patron = $expired_borrower;
737 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
738 ( $renewokay, $error ) =
739 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
740 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
741 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
742 Koha::Checkouts->find( $checkout->issue_id )->delete;
745 # Patron is expired and BlockExpiredPatronOpacActions=1
746 # => auto renew is not allowed
747 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
748 $patron = $expired_borrower;
749 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
750 ( $renewokay, $error ) =
751 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
752 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
753 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
754 Koha::Checkouts->find( $checkout->issue_id )->delete;
757 # Patron is not expired and BlockExpiredPatronOpacActions=1
758 # => auto renew is allowed
759 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
760 $patron = $renewing_borrower;
761 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
762 ( $renewokay, $error ) =
763 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
764 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
765 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
766 Koha::Checkouts->find( $checkout->issue_id )->delete;
769 subtest "GetLatestAutoRenewDate" => sub {
771 my $item_to_auto_renew = $builder->build(
774 biblionumber => $biblionumber,
775 homebranch => $branch,
776 holdingbranch => $branch,
781 my $ten_days_before = dt_from_string->add( days => -10 );
782 my $ten_days_ahead = dt_from_string->add( days => 10 );
783 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
784 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
785 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
786 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' );
787 my $five_days_before = dt_from_string->add( days => -5 );
788 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
789 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
790 is( $latest_auto_renew_date->truncate( to => 'minute' ),
791 $five_days_before->truncate( to => 'minute' ),
792 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
794 my $five_days_ahead = dt_from_string->add( days => 5 );
795 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
796 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
797 is( $latest_auto_renew_date->truncate( to => 'minute' ),
798 $five_days_ahead->truncate( to => 'minute' ),
799 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
801 my $two_days_ahead = dt_from_string->add( days => 2 );
802 $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 ) );
803 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
804 is( $latest_auto_renew_date->truncate( to => 'day' ),
805 $two_days_ahead->truncate( to => 'day' ),
806 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
808 $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 ) );
809 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
810 is( $latest_auto_renew_date->truncate( to => 'day' ),
811 $two_days_ahead->truncate( to => 'day' ),
812 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
819 # set policy to forbid renewals
820 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
822 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
823 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
824 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
826 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
827 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
828 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
830 C4::Overdues::UpdateFine(
832 issue_id => $issue->id(),
833 itemnumber => $itemnumber,
834 borrowernumber => $renewing_borrower->{borrowernumber},
837 due => Koha::DateUtils::output_pref($datedue)
841 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
842 is( $line->accounttype, 'FU', 'Account line type is FU' );
843 is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
844 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
845 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
846 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
848 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
849 is( $offset->type, 'Fine', 'Account offset type is Fine' );
850 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
852 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
853 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
855 LostItem( $itemnumber, 'test', 1 );
857 $line = Koha::Account::Lines->find($line->id);
858 is( $line->accounttype, 'F', 'Account type correctly changed from FU to F' );
860 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
861 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
862 my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
863 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
865 my $total_due = $dbh->selectrow_array(
866 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
867 undef, $renewing_borrower->{borrowernumber}
870 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
872 C4::Context->dbh->do("DELETE FROM accountlines");
874 C4::Overdues::UpdateFine(
876 issue_id => $issue2->id(),
877 itemnumber => $itemnumber2,
878 borrowernumber => $renewing_borrower->{borrowernumber},
881 due => Koha::DateUtils::output_pref($datedue)
885 LostItem( $itemnumber2, 'test', 0 );
887 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
888 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
889 ok( Koha::Checkouts->find({ itemnumber => $itemnumber2 }), 'LostItem called without forced return has checked in the item' );
891 $total_due = $dbh->selectrow_array(
892 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
893 undef, $renewing_borrower->{borrowernumber}
896 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
898 my $future = dt_from_string();
899 $future->add( days => 7 );
900 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
901 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
903 # Users cannot renew any item if there is an overdue item
904 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
905 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
906 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
907 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
908 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
910 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
911 $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber3 } );
912 LostItem( $itemnumber3, 'test', 0 );
913 my $accountline = Koha::Account::Lines->find( { itemnumber => $itemnumber3 } );
914 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
919 # GetUpcomingDueIssues tests
920 my $barcode = 'R00000342';
921 my $barcode2 = 'R00000343';
922 my $barcode3 = 'R00000344';
923 my $branch = $library2->{branchcode};
925 #Create another record
926 my $title2 = 'Something is worng here';
927 my ($biblionumber2, $biblioitemnumber2) = add_biblio($title2, 'Anonymous');
932 homebranch => $branch,
933 holdingbranch => $branch,
934 barcode => $barcode3,
941 my %a_borrower_data = (
942 firstname => 'Fridolyn',
944 categorycode => $patron_category->{categorycode},
945 branchcode => $branch,
948 my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
949 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
951 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
952 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
953 my $today = DateTime->today(time_zone => C4::Context->tz());
955 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
956 my $datedue = dt_from_string( $issue->date_due() );
957 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
958 my $datedue2 = dt_from_string( $issue->date_due() );
962 # GetUpcomingDueIssues tests
964 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
965 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
968 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
969 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
970 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
973 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
974 is ( scalar( @$upcoming_dues ), 1,
975 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
978 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
980 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
982 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
983 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
985 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
986 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
988 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
989 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
991 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
992 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
994 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
995 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
997 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
998 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1003 my $barcode = '1234567890';
1004 my $branch = $library2->{branchcode};
1006 my ($biblionumber, $biblioitemnumber) = add_biblio();
1009 my ( undef, undef, $itemnumber ) = AddItem(
1011 homebranch => $branch,
1012 holdingbranch => $branch,
1013 barcode => $barcode,
1020 my %a_borrower_data = (
1021 firstname => 'Kyle',
1023 categorycode => $patron_category->{categorycode},
1024 branchcode => $branch,
1027 my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1029 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1030 my $issue = AddIssue( $borrower, $barcode );
1033 issue_id => $issue->id(),
1034 itemnumber => $itemnumber,
1035 borrowernumber => $borrowernumber,
1041 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
1042 my $count = $hr->{count};
1044 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1048 $dbh->do('DELETE FROM issues');
1049 $dbh->do('DELETE FROM items');
1050 $dbh->do('DELETE FROM issuingrules');
1053 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
1054 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1063 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
1065 my $barcode1 = '1234';
1066 my ( undef, undef, $itemnumber1 ) = AddItem(
1068 homebranch => $library2->{branchcode},
1069 holdingbranch => $library2->{branchcode},
1070 barcode => $barcode1,
1075 my $barcode2 = '4321';
1076 my ( undef, undef, $itemnumber2 ) = AddItem(
1078 homebranch => $library2->{branchcode},
1079 holdingbranch => $library2->{branchcode},
1080 barcode => $barcode2,
1086 my $borrowernumber1 = Koha::Patron->new({
1087 firstname => 'Kyle',
1089 categorycode => $patron_category->{categorycode},
1090 branchcode => $library2->{branchcode},
1091 })->store->borrowernumber;
1092 my $borrowernumber2 = Koha::Patron->new({
1093 firstname => 'Chelsea',
1095 categorycode => $patron_category->{categorycode},
1096 branchcode => $library2->{branchcode},
1097 })->store->borrowernumber;
1099 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1100 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1102 my $issue = AddIssue( $borrower1, $barcode1 );
1104 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1105 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1108 $library2->{branchcode}, $borrowernumber2, $biblionumber,
1109 '', 1, undef, undef, '',
1113 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1114 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1115 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1116 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1118 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1119 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1120 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1121 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1123 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1124 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1125 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1126 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1128 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1129 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1130 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1131 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1133 # Setting item not checked out to be not for loan but holdable
1134 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
1136 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1137 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' );
1141 # Don't allow renewing onsite checkout
1142 my $barcode = 'R00000XXX';
1143 my $branch = $library->{branchcode};
1145 #Create another record
1146 my ($biblionumber, $biblioitemnumber) = add_biblio('A title', 'Anonymous');
1148 my (undef, undef, $itemnumber) = AddItem(
1150 homebranch => $branch,
1151 holdingbranch => $branch,
1152 barcode => $barcode,
1158 my $borrowernumber = Koha::Patron->new({
1161 categorycode => $patron_category->{categorycode},
1162 branchcode => $branch,
1163 })->store->borrowernumber;
1165 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1167 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1168 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
1169 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1170 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1174 my $library = $builder->build({ source => 'Branch' });
1176 my ($biblionumber, $biblioitemnumber) = add_biblio();
1178 my $barcode = 'just a barcode';
1179 my ( undef, undef, $itemnumber ) = AddItem(
1181 homebranch => $library->{branchcode},
1182 holdingbranch => $library->{branchcode},
1183 barcode => $barcode,
1189 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1191 my $issue = AddIssue( $patron, $barcode );
1194 issue_id => $issue->id(),
1195 itemnumber => $itemnumber,
1196 borrowernumber => $patron->{borrowernumber},
1203 issue_id => $issue->id(),
1204 itemnumber => $itemnumber,
1205 borrowernumber => $patron->{borrowernumber},
1210 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1213 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1216 my $homebranch = $builder->build( { source => 'Branch' } );
1217 my $holdingbranch = $builder->build( { source => 'Branch' } );
1218 my $otherbranch = $builder->build( { source => 'Branch' } );
1219 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1220 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1222 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1223 my $item = $builder->build(
1226 homebranch => $homebranch->{branchcode},
1227 holdingbranch => $holdingbranch->{branchcode},
1228 biblionumber => $biblioitem->{biblionumber}
1233 set_userenv($holdingbranch);
1235 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1236 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Checkout
1238 my ( $error, $question, $alerts );
1240 # AllowReturnToBranch == anywhere
1241 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1242 ## Test that unknown barcodes don't generate internal server errors
1243 set_userenv($homebranch);
1244 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1245 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1246 ## Can be issued from homebranch
1247 set_userenv($homebranch);
1248 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1249 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1250 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1251 ## Can be issued from holdingbranch
1252 set_userenv($holdingbranch);
1253 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1254 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1255 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1256 ## Can be issued from another branch
1257 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1258 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1259 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1261 # AllowReturnToBranch == holdingbranch
1262 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1263 ## Cannot be issued from homebranch
1264 set_userenv($homebranch);
1265 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1266 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1267 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1268 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1269 ## Can be issued from holdinbranch
1270 set_userenv($holdingbranch);
1271 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1272 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1273 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1274 ## Cannot be issued from another branch
1275 set_userenv($otherbranch);
1276 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1277 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1278 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1279 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1281 # AllowReturnToBranch == homebranch
1282 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1283 ## Can be issued from holdinbranch
1284 set_userenv($homebranch);
1285 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1286 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1287 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1288 ## Cannot be issued from holdinbranch
1289 set_userenv($holdingbranch);
1290 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1291 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1292 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1293 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1294 ## Cannot be issued from holdinbranch
1295 set_userenv($otherbranch);
1296 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1297 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1298 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1299 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1301 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1304 subtest 'AddIssue & AllowReturnToBranch' => sub {
1307 my $homebranch = $builder->build( { source => 'Branch' } );
1308 my $holdingbranch = $builder->build( { source => 'Branch' } );
1309 my $otherbranch = $builder->build( { source => 'Branch' } );
1310 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1311 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1313 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1314 my $item = $builder->build(
1317 homebranch => $homebranch->{branchcode},
1318 holdingbranch => $holdingbranch->{branchcode},
1322 biblionumber => $biblioitem->{biblionumber}
1327 set_userenv($holdingbranch);
1329 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Checkout
1330 my $issue = AddIssue( $patron_1, $item->{barcode} );
1332 my ( $error, $question, $alerts );
1334 # AllowReturnToBranch == homebranch
1335 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1336 ## Can be issued from homebranch
1337 set_userenv($homebranch);
1338 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1339 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1340 ## Can be issued from holdinbranch
1341 set_userenv($holdingbranch);
1342 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1343 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1344 ## Can be issued from another branch
1345 set_userenv($otherbranch);
1346 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1347 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1349 # AllowReturnToBranch == holdinbranch
1350 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1351 ## Cannot be issued from homebranch
1352 set_userenv($homebranch);
1353 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1354 ## Can be issued from holdingbranch
1355 set_userenv($holdingbranch);
1356 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1357 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1358 ## Cannot be issued from another branch
1359 set_userenv($otherbranch);
1360 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1362 # AllowReturnToBranch == homebranch
1363 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1364 ## Can be issued from homebranch
1365 set_userenv($homebranch);
1366 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1367 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1368 ## Cannot be issued from holdinbranch
1369 set_userenv($holdingbranch);
1370 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1371 ## Cannot be issued from another branch
1372 set_userenv($otherbranch);
1373 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1374 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1377 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1380 my $library = $builder->build( { source => 'Branch' } );
1381 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1383 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1384 my $item_1 = $builder->build(
1387 homebranch => $library->{branchcode},
1388 holdingbranch => $library->{branchcode},
1389 biblionumber => $biblioitem_1->{biblionumber}
1393 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1394 my $item_2 = $builder->build(
1397 homebranch => $library->{branchcode},
1398 holdingbranch => $library->{branchcode},
1399 biblionumber => $biblioitem_2->{biblionumber}
1404 my ( $error, $question, $alerts );
1406 # Patron cannot issue item_1, they have overdues
1407 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1408 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1410 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1411 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1412 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1413 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1415 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1416 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1417 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1418 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1420 # Patron cannot issue item_1, they are debarred
1421 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1422 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1423 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1424 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1425 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1427 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1428 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1429 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1430 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1433 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1436 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1437 my $patron_category_x = $builder->build_object(
1439 class => 'Koha::Patron::Categories',
1440 value => { category_type => 'X' }
1443 my $patron = $builder->build_object(
1445 class => 'Koha::Patrons',
1447 categorycode => $patron_category_x->categorycode,
1448 gonenoaddress => undef,
1455 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1456 my $item_1 = $builder->build(
1460 homebranch => $library->branchcode,
1461 holdingbranch => $library->branchcode,
1462 biblionumber => $biblioitem_1->{biblionumber}
1467 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1468 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1470 # TODO There are other tests to provide here
1473 subtest 'MultipleReserves' => sub {
1476 my $title = 'Silence in the library';
1477 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
1479 my $branch = $library2->{branchcode};
1481 my $barcode1 = 'R00110001';
1482 my ( $item_bibnum1, $item_bibitemnum1, $itemnumber1 ) = AddItem(
1484 homebranch => $branch,
1485 holdingbranch => $branch,
1486 barcode => $barcode1,
1487 replacementprice => 12.00,
1493 my $barcode2 = 'R00110002';
1494 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
1496 homebranch => $branch,
1497 holdingbranch => $branch,
1498 barcode => $barcode2,
1499 replacementprice => 12.00,
1507 my $resdate = undef;
1508 my $expdate = undef;
1510 my $checkitem = undef;
1513 my %renewing_borrower_data = (
1514 firstname => 'John',
1515 surname => 'Renewal',
1516 categorycode => $patron_category->{categorycode},
1517 branchcode => $branch,
1519 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
1520 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1521 my $issue = AddIssue( $renewing_borrower, $barcode1);
1522 my $datedue = dt_from_string( $issue->date_due() );
1523 is (defined $issue->date_due(), 1, "item 1 checked out");
1524 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $itemnumber1 })->borrowernumber;
1526 my %reserving_borrower_data1 = (
1527 firstname => 'Katrin',
1528 surname => 'Reservation',
1529 categorycode => $patron_category->{categorycode},
1530 branchcode => $branch,
1532 my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
1534 $branch, $reserving_borrowernumber1, $biblionumber,
1535 $bibitems, $priority, $resdate, $expdate, $notes,
1536 $title, $checkitem, $found
1539 my %reserving_borrower_data2 = (
1540 firstname => 'Kirk',
1541 surname => 'Reservation',
1542 categorycode => $patron_category->{categorycode},
1543 branchcode => $branch,
1545 my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
1547 $branch, $reserving_borrowernumber2, $biblionumber,
1548 $bibitems, $priority, $resdate, $expdate, $notes,
1549 $title, $checkitem, $found
1553 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1554 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1557 my $barcode3 = 'R00110003';
1558 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
1560 homebranch => $branch,
1561 holdingbranch => $branch,
1562 barcode => $barcode3,
1563 replacementprice => 12.00,
1570 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1571 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1575 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1578 my $library = $builder->build( { source => 'Branch' } );
1579 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1581 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1582 my $biblionumber = $biblioitem->{biblionumber};
1583 my $item_1 = $builder->build(
1586 homebranch => $library->{branchcode},
1587 holdingbranch => $library->{branchcode},
1588 biblionumber => $biblionumber,
1592 my $item_2 = $builder->build(
1595 homebranch => $library->{branchcode},
1596 holdingbranch => $library->{branchcode},
1597 biblionumber => $biblionumber,
1602 my ( $error, $question, $alerts );
1603 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1605 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1606 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1607 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1608 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) );
1610 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1611 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1612 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1614 # Add a subscription
1615 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1617 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1618 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1619 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) );
1621 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1622 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1623 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) );
1626 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1629 my $library = $builder->build( { source => 'Branch' } );
1630 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1633 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1634 my $item_1 = $builder->build(
1638 homebranch => $library->{branchcode},
1639 holdingbranch => $library->{branchcode},
1643 biblionumber => $biblioitem_1->{biblionumber}
1647 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1648 my $item_2 = $builder->build(
1652 homebranch => $library->{branchcode},
1653 holdingbranch => $library->{branchcode},
1657 biblionumber => $biblioitem_2->{biblionumber}
1662 # And the issuing rule
1663 Koha::IssuingRules->search->delete;
1664 my $rule = Koha::IssuingRule->new(
1666 categorycode => '*',
1671 firstremind => 1, # 1 day of grace
1672 finedays => 2, # 2 days of fine per day of overdue
1673 lengthunit => 'days',
1678 # Patron cannot issue item_1, they have overdues
1679 my $five_days_ago = dt_from_string->subtract( days => 5 );
1680 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1681 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1682 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1683 ; # Add another overdue
1685 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1686 AddReturn( $item_1->{barcode}, $library->{branchcode},
1687 undef, undef, dt_from_string );
1688 my $debarments = Koha::Patron::Debarments::GetDebarments(
1689 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1690 is( scalar(@$debarments), 1 );
1692 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1693 # Same for the others
1694 my $expected_expiration = output_pref(
1696 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1697 dateformat => 'sql',
1701 is( $debarments->[0]->{expiration}, $expected_expiration );
1703 AddReturn( $item_2->{barcode}, $library->{branchcode},
1704 undef, undef, dt_from_string );
1705 $debarments = Koha::Patron::Debarments::GetDebarments(
1706 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1707 is( scalar(@$debarments), 1 );
1708 $expected_expiration = output_pref(
1710 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1711 dateformat => 'sql',
1715 is( $debarments->[0]->{expiration}, $expected_expiration );
1717 Koha::Patron::Debarments::DelUniqueDebarment(
1718 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1720 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1721 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1722 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1723 ; # Add another overdue
1724 AddReturn( $item_1->{barcode}, $library->{branchcode},
1725 undef, undef, dt_from_string );
1726 $debarments = Koha::Patron::Debarments::GetDebarments(
1727 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1728 is( scalar(@$debarments), 1 );
1729 $expected_expiration = output_pref(
1731 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1732 dateformat => 'sql',
1736 is( $debarments->[0]->{expiration}, $expected_expiration );
1738 AddReturn( $item_2->{barcode}, $library->{branchcode},
1739 undef, undef, dt_from_string );
1740 $debarments = Koha::Patron::Debarments::GetDebarments(
1741 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1742 is( scalar(@$debarments), 1 );
1743 $expected_expiration = output_pref(
1745 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1746 dateformat => 'sql',
1750 is( $debarments->[0]->{expiration}, $expected_expiration );
1753 subtest 'AddReturn + suspension_chargeperiod' => sub {
1756 my $library = $builder->build( { source => 'Branch' } );
1757 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1760 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1761 my $item_1 = $builder->build(
1765 homebranch => $library->{branchcode},
1766 holdingbranch => $library->{branchcode},
1770 biblionumber => $biblioitem_1->{biblionumber}
1775 # And the issuing rule
1776 Koha::IssuingRules->search->delete;
1777 my $rule = Koha::IssuingRule->new(
1779 categorycode => '*',
1784 firstremind => 0, # 0 day of grace
1785 finedays => 2, # 2 days of fine per day of overdue
1786 suspension_chargeperiod => 1,
1787 lengthunit => 'days',
1792 my $five_days_ago = dt_from_string->subtract( days => 5 );
1793 # We want to charge 2 days every day, without grace
1794 # With 5 days of overdue: 5 * Z
1795 my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1796 test_debarment_on_checkout(
1799 library => $library,
1801 due_date => $five_days_ago,
1802 expiration_date => $expected_expiration,
1806 # We want to charge 2 days every 2 days, without grace
1807 # With 5 days of overdue: (5 * 2) / 2
1808 $rule->suspension_chargeperiod(2)->store;
1809 $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1810 test_debarment_on_checkout(
1813 library => $library,
1815 due_date => $five_days_ago,
1816 expiration_date => $expected_expiration,
1820 # We want to charge 2 days every 3 days, with 1 day of grace
1821 # With 5 days of overdue: ((5-1) / 3 ) * 2
1822 $rule->suspension_chargeperiod(3)->store;
1823 $rule->firstremind(1)->store;
1824 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1825 test_debarment_on_checkout(
1828 library => $library,
1830 due_date => $five_days_ago,
1831 expiration_date => $expected_expiration,
1835 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1836 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1837 $rule->finedays(2)->store;
1838 $rule->suspension_chargeperiod(1)->store;
1839 $rule->firstremind(0)->store;
1840 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1842 # Adding a holiday 2 days ago
1843 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1844 my $two_days_ago = dt_from_string->subtract( days => 2 );
1845 $calendar->insert_single_holiday(
1846 day => $two_days_ago->day,
1847 month => $two_days_ago->month,
1848 year => $two_days_ago->year,
1849 title => 'holidayTest-2d',
1850 description => 'holidayDesc 2 days ago'
1852 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1853 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1854 test_debarment_on_checkout(
1857 library => $library,
1859 due_date => $five_days_ago,
1860 expiration_date => $expected_expiration,
1864 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1865 my $two_days_ahead = dt_from_string->add( days => 2 );
1866 $calendar->insert_single_holiday(
1867 day => $two_days_ahead->day,
1868 month => $two_days_ahead->month,
1869 year => $two_days_ahead->year,
1870 title => 'holidayTest+2d',
1871 description => 'holidayDesc 2 days ahead'
1874 # Same as above, but we should skip D+2
1875 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1876 test_debarment_on_checkout(
1879 library => $library,
1881 due_date => $five_days_ago,
1882 expiration_date => $expected_expiration,
1886 # Adding another holiday, day of expiration date
1887 my $expected_expiration_dt = dt_from_string($expected_expiration);
1888 $calendar->insert_single_holiday(
1889 day => $expected_expiration_dt->day,
1890 month => $expected_expiration_dt->month,
1891 year => $expected_expiration_dt->year,
1892 title => 'holidayTest_exp',
1893 description => 'holidayDesc on expiration date'
1895 # Expiration date will be the day after
1896 test_debarment_on_checkout(
1899 library => $library,
1901 due_date => $five_days_ago,
1902 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1906 test_debarment_on_checkout(
1909 library => $library,
1911 return_date => dt_from_string->add(days => 5),
1912 expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1917 subtest 'AddReturn | is_overdue' => sub {
1920 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
1921 t::lib::Mocks::mock_preference('finesMode', 'production');
1922 t::lib::Mocks::mock_preference('MaxFine', '100');
1924 my $library = $builder->build( { source => 'Branch' } );
1925 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1927 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1928 my $item = $builder->build(
1932 homebranch => $library->{branchcode},
1933 holdingbranch => $library->{branchcode},
1937 biblionumber => $biblioitem->{biblionumber},
1942 Koha::IssuingRules->search->delete;
1943 my $rule = Koha::IssuingRule->new(
1945 categorycode => '*',
1950 lengthunit => 'days',
1951 fine => 1, # Charge 1 every day of overdue
1957 my $one_day_ago = dt_from_string->subtract( days => 1 );
1958 my $five_days_ago = dt_from_string->subtract( days => 5 );
1959 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1960 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1962 # No date specify, today will be used
1963 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1964 AddReturn( $item->{barcode}, $library->{branchcode} );
1965 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1966 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1968 # specify return date 5 days before => no overdue
1969 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1970 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $ten_days_ago );
1971 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1972 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1974 # specify return date 5 days later => overdue
1975 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1976 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $five_days_ago );
1977 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1978 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1980 # specify dropbox date 5 days before => no overdue
1981 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1982 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $ten_days_ago );
1983 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1984 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1986 # specify dropbox date 5 days later => overdue, or... not
1987 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1988 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $five_days_ago );
1989 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
1990 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1993 subtest '_FixAccountForLostAndReturned' => sub {
1997 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1998 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
2000 my $processfee_amount = 20;
2001 my $replacement_amount = 99.00;
2002 my $item_type = $builder->build_object(
2003 { class => 'Koha::ItemTypes',
2005 notforloan => undef,
2007 defaultreplacecost => undef,
2008 processfee => $processfee_amount
2012 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2014 # Generate test biblio
2015 my $title = 'Koha for Dummies';
2016 my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, 'Hall, Daria' );
2018 subtest 'Full write-off tests' => sub {
2022 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2023 my $barcode = 'KD123456789';
2025 my ( undef, undef, $item_id ) = AddItem(
2026 { homebranch => $library->branchcode,
2027 holdingbranch => $library->branchcode,
2028 barcode => $barcode,
2029 replacementprice => $replacement_amount,
2030 itype => $item_type->itemtype
2035 AddIssue( $patron->unblessed, $barcode );
2037 # Simulate item marked as lost
2038 ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2039 LostItem( $item_id, 1 );
2041 my $processing_fee_lines = Koha::Account::Lines->search(
2042 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2043 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2044 my $processing_fee_line = $processing_fee_lines->next;
2045 is( $processing_fee_line->amount + 0,
2046 $processfee_amount, 'The right PF amount is generated' );
2047 is( $processing_fee_line->amountoutstanding + 0,
2048 $processfee_amount, 'The right PF amountoutstanding is generated' );
2050 my $lost_fee_lines = Koha::Account::Lines->search(
2051 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2052 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2053 my $lost_fee_line = $lost_fee_lines->next;
2054 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2055 is( $lost_fee_line->amountoutstanding + 0,
2056 $replacement_amount, 'The right L amountoutstanding is generated' );
2058 my $account = $patron->account;
2059 my $debts = $account->outstanding_debits;
2061 # Write off the debt
2062 my $credit = $account->add_credit(
2063 { amount => $account->balance,
2067 $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2069 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2070 is( $credit_return_id, undef, 'No CR account line added' );
2072 $lost_fee_line->discard_changes; # reload from DB
2073 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2074 is( $lost_fee_line->accounttype,
2075 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2077 is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2080 subtest 'Full payment tests' => sub {
2084 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2085 my $barcode = 'KD123456790';
2087 my ( undef, undef, $item_id ) = AddItem(
2088 { homebranch => $library->branchcode,
2089 holdingbranch => $library->branchcode,
2090 barcode => $barcode,
2091 replacementprice => $replacement_amount,
2092 itype => $item_type->itemtype
2097 AddIssue( $patron->unblessed, $barcode );
2099 # Simulate item marked as lost
2100 ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2101 LostItem( $item_id, 1 );
2103 my $processing_fee_lines = Koha::Account::Lines->search(
2104 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2105 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2106 my $processing_fee_line = $processing_fee_lines->next;
2107 is( $processing_fee_line->amount + 0,
2108 $processfee_amount, 'The right PF amount is generated' );
2109 is( $processing_fee_line->amountoutstanding + 0,
2110 $processfee_amount, 'The right PF amountoutstanding is generated' );
2112 my $lost_fee_lines = Koha::Account::Lines->search(
2113 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2114 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2115 my $lost_fee_line = $lost_fee_lines->next;
2116 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2117 is( $lost_fee_line->amountoutstanding + 0,
2118 $replacement_amount, 'The right L amountountstanding is generated' );
2120 my $account = $patron->account;
2121 my $debts = $account->outstanding_debits;
2123 # Write off the debt
2124 my $credit = $account->add_credit(
2125 { amount => $account->balance,
2129 $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2131 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2132 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2134 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2135 is( $credit_return->amount + 0,
2136 -99.00, 'The account line of type CR has an amount of -99' );
2137 is( $credit_return->amountoutstanding + 0,
2138 -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2140 $lost_fee_line->discard_changes;
2141 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2142 is( $lost_fee_line->accounttype,
2143 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2145 is( $patron->account->balance,
2146 -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2149 subtest 'Test without payment or write off' => sub {
2153 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2154 my $barcode = 'KD123456791';
2156 my ( undef, undef, $item_id ) = AddItem(
2157 { homebranch => $library->branchcode,
2158 holdingbranch => $library->branchcode,
2159 barcode => $barcode,
2160 replacementprice => $replacement_amount,
2161 itype => $item_type->itemtype
2166 AddIssue( $patron->unblessed, $barcode );
2168 # Simulate item marked as lost
2169 ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2170 LostItem( $item_id, 1 );
2172 my $processing_fee_lines = Koha::Account::Lines->search(
2173 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2174 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2175 my $processing_fee_line = $processing_fee_lines->next;
2176 is( $processing_fee_line->amount + 0,
2177 $processfee_amount, 'The right PF amount is generated' );
2178 is( $processing_fee_line->amountoutstanding + 0,
2179 $processfee_amount, 'The right PF amountoutstanding is generated' );
2181 my $lost_fee_lines = Koha::Account::Lines->search(
2182 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2183 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2184 my $lost_fee_line = $lost_fee_lines->next;
2185 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2186 is( $lost_fee_line->amountoutstanding + 0,
2187 $replacement_amount, 'The right L amountountstanding is generated' );
2189 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2190 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2192 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2193 is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2194 is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2196 $lost_fee_line->discard_changes;
2197 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2198 is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2200 is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2203 subtest 'Test with partial payement and write off, and remaining debt' => sub {
2207 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2208 my $barcode = 'KD123456792';
2210 my ( undef, undef, $item_id ) = AddItem(
2211 { homebranch => $library->branchcode,
2212 holdingbranch => $library->branchcode,
2213 barcode => $barcode,
2214 replacementprice => $replacement_amount,
2215 itype => $item_type->itemtype
2220 AddIssue( $patron->unblessed, $barcode );
2222 # Simulate item marked as lost
2223 ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2224 LostItem( $item_id, 1 );
2226 my $processing_fee_lines = Koha::Account::Lines->search(
2227 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2228 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2229 my $processing_fee_line = $processing_fee_lines->next;
2230 is( $processing_fee_line->amount + 0,
2231 $processfee_amount, 'The right PF amount is generated' );
2232 is( $processing_fee_line->amountoutstanding + 0,
2233 $processfee_amount, 'The right PF amountoutstanding is generated' );
2235 my $lost_fee_lines = Koha::Account::Lines->search(
2236 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2237 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2238 my $lost_fee_line = $lost_fee_lines->next;
2239 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2240 is( $lost_fee_line->amountoutstanding + 0,
2241 $replacement_amount, 'The right L amountountstanding is generated' );
2243 my $account = $patron->account;
2244 is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2247 my $payment_amount = 27;
2248 my $payment = $account->add_credit(
2249 { amount => $payment_amount,
2254 $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } );
2256 # Partially write off fee
2257 my $write_off_amount = 25;
2258 my $write_off = $account->add_credit(
2259 { amount => $write_off_amount,
2263 $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
2265 is( $account->balance,
2266 $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2267 'Payment and write off applied'
2270 # Store the amountoutstanding value
2271 $lost_fee_line->discard_changes;
2272 my $outstanding = $lost_fee_line->amountoutstanding;
2274 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2275 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2277 is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2279 $lost_fee_line->discard_changes;
2280 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2281 is( $lost_fee_line->accounttype,
2282 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2284 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2285 is( $credit_return->amount + 0,
2286 ($payment_amount + $outstanding ) * -1,
2287 'The account line of type CR has an amount equal to the payment + outstanding'
2289 is( $credit_return->amountoutstanding + 0,
2290 $payment_amount * -1,
2291 'The account line of type CR has an amountoutstanding equal to the payment'
2294 is( $account->balance,
2295 $processfee_amount - $payment_amount,
2296 'The patron balance is the difference between the PF and the credit'
2301 subtest '_FixOverduesOnReturn' => sub {
2304 # Generate test biblio
2305 my $title = 'Koha for Dummies';
2306 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Kylie');
2308 my $barcode = 'KD987654321';
2309 my $branchcode = $library2->{branchcode};
2311 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
2313 homebranch => $branchcode,
2314 holdingbranch => $branchcode,
2315 barcode => $barcode,
2316 replacementprice => 99.00,
2322 my $patron = $builder->build( { source => 'Borrower' } );
2324 ## Start with basic call, should just close out the open fine
2325 my $accountline = Koha::Account::Line->new(
2327 borrowernumber => $patron->{borrowernumber},
2328 accounttype => 'FU',
2329 itemnumber => $itemnumber,
2331 amountoutstanding => 99.00,
2332 lastincrement => 9.00,
2336 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber );
2338 $accountline->_result()->discard_changes();
2340 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2341 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2344 ## Run again, with exemptfine enabled
2347 accounttype => 'FU',
2348 amountoutstanding => 99.00,
2352 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 1 );
2354 $accountline->_result()->discard_changes();
2355 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2357 is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2358 is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2359 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2360 is( $offset->amount, '-99.000000', "Amount of offset is correct" );
2362 ## Run again, with dropbox mode enabled
2365 accounttype => 'FU',
2366 amountoutstanding => 99.00,
2370 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 0, 1 );
2372 $accountline->_result()->discard_changes();
2373 $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Dropbox' })->next();
2375 is( $accountline->amountoutstanding + 0, 90, 'Fine has been reduced to 90' );
2376 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2377 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via dropbox" );
2378 is( $offset->amount, '-9.000000', "Amount of offset is correct" );
2381 subtest 'Set waiting flag' => sub {
2384 my $library_1 = $builder->build( { source => 'Branch' } );
2385 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2386 my $library_2 = $builder->build( { source => 'Branch' } );
2387 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2389 my $biblio = $builder->build( { source => 'Biblio' } );
2390 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2392 my $item = $builder->build(
2396 homebranch => $library_1->{branchcode},
2397 holdingbranch => $library_1->{branchcode},
2401 biblionumber => $biblioitem->{biblionumber},
2406 set_userenv( $library_2 );
2407 my $reserve_id = AddReserve(
2408 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2409 '', 1, undef, undef, '', undef, $item->{itemnumber},
2412 set_userenv( $library_1 );
2413 my $do_transfer = 1;
2414 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2415 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2416 my $hold = Koha::Holds->find( $reserve_id );
2417 is( $hold->found, 'T', 'Hold is in transit' );
2419 my ( $status ) = CheckReserves($item->{itemnumber});
2420 is( $status, 'Reserved', 'Hold is not waiting yet');
2422 set_userenv( $library_2 );
2424 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2425 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2426 $hold = Koha::Holds->find( $reserve_id );
2427 is( $hold->found, 'W', 'Hold is waiting' );
2428 ( $status ) = CheckReserves($item->{itemnumber});
2429 is( $status, 'Waiting', 'Now the hold is waiting');
2432 subtest 'Cancel transfers on lost items' => sub {
2434 my $library_1 = $builder->build( { source => 'Branch' } );
2435 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2436 my $library_2 = $builder->build( { source => 'Branch' } );
2437 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2438 my $biblio = $builder->build( { source => 'Biblio' } );
2439 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2440 my $item = $builder->build(
2444 homebranch => $library_1->{branchcode},
2445 holdingbranch => $library_1->{branchcode},
2449 biblionumber => $biblioitem->{biblionumber},
2454 set_userenv( $library_2 );
2455 my $reserve_id = AddReserve(
2456 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber}, '', 1, undef, undef, '', undef, $item->{itemnumber},
2459 #Return book and add transfer
2460 set_userenv( $library_1 );
2461 my $do_transfer = 1;
2462 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2463 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2464 C4::Circulation::transferbook( $library_2->{branchcode}, $item->{barcode} );
2465 my $hold = Koha::Holds->find( $reserve_id );
2466 is( $hold->found, 'T', 'Hold is in transit' );
2468 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2469 my ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2470 is( $tobranch, $library_2->{branchcode}, 'The transfer record exists in the branchtransfers table');
2471 my $itemcheck = GetItem($item->{itemnumber});
2472 is( $itemcheck->{holdingbranch}, $library_2->{branchcode}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2474 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2475 ModItem( { itemlost => 1 }, $biblio->{biblionumber}, $item->{itemnumber} );
2476 LostItem( $item->{itemnumber}, 'test', 1 );
2477 ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2478 is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2479 $itemcheck = GetItem($item->{itemnumber});
2480 is( $itemcheck->{holdingbranch}, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2483 subtest 'CanBookBeIssued | is_overdue' => sub {
2486 # Set a simple circ policy
2487 $dbh->do('DELETE FROM issuingrules');
2489 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2490 maxissueqty, issuelength, lengthunit,
2491 renewalsallowed, renewalperiod,
2492 norenewalbefore, auto_renew,
2509 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2510 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2511 my $library = $builder->build( { source => 'Branch' } );
2512 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2514 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2515 my $item = $builder->build(
2519 homebranch => $library->{branchcode},
2520 holdingbranch => $library->{branchcode},
2524 biblionumber => $biblioitem->{biblionumber},
2529 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2530 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2531 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2532 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2533 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2534 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2537 subtest 'ItemsDeniedRenewal preference' => sub {
2540 C4::Context->set_preference('ItemsDeniedRenewal','');
2542 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2545 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
2546 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2549 '*', $idr_lib->branchcode, '*', 25,
2556 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2557 homebranch => $idr_lib->branchcode,
2561 itemcallnumber => undef,
2565 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2566 homebranch => $idr_lib->branchcode,
2569 location => 'NOPROC'
2573 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2574 branchcode => $idr_lib->branchcode,
2577 my $future = dt_from_string->add( days => 1 );
2578 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2579 returndate => undef,
2582 borrowernumber => $idr_borrower->borrowernumber,
2583 itemnumber => $deny_book->itemnumber,
2584 onsite_checkout => 0,
2585 date_due => $future,
2588 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2589 returndate => undef,
2592 borrowernumber => $idr_borrower->borrowernumber,
2593 itemnumber => $allow_book->itemnumber,
2594 onsite_checkout => 0,
2595 date_due => $future,
2601 my ( $idr_mayrenew, $idr_error ) =
2602 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2603 is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2604 is( $idr_error, undef, 'Renewal allowed when no rules' );
2606 $idr_rules="withdrawn: [1]";
2608 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2609 ( $idr_mayrenew, $idr_error ) =
2610 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2611 is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2612 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2613 ( $idr_mayrenew, $idr_error ) =
2614 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2615 is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2616 is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2618 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2620 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2621 ( $idr_mayrenew, $idr_error ) =
2622 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2623 is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2624 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2625 ( $idr_mayrenew, $idr_error ) =
2626 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2627 is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2628 is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2630 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2632 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2633 ( $idr_mayrenew, $idr_error ) =
2634 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2635 is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2636 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2637 ( $idr_mayrenew, $idr_error ) =
2638 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2639 is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2640 is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2642 $idr_rules="itemcallnumber: [NULL]";
2643 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2644 ( $idr_mayrenew, $idr_error ) =
2645 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2646 is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2647 $idr_rules="itemcallnumber: ['']";
2648 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2649 ( $idr_mayrenew, $idr_error ) =
2650 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2651 is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2653 $idr_rules="itemnotes: [NULL]";
2654 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2655 ( $idr_mayrenew, $idr_error ) =
2656 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2657 is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2658 $idr_rules="itemnotes: ['']";
2659 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2660 ( $idr_mayrenew, $idr_error ) =
2661 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2662 is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2665 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2668 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2669 my $library = $builder->build( { source => 'Branch' } );
2670 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2672 my $itemtype = $builder->build(
2674 source => 'Itemtype',
2675 value => { notforloan => undef, }
2679 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2680 my $item = $builder->build_object(
2682 class => 'Koha::Items',
2684 homebranch => $library->{branchcode},
2685 holdingbranch => $library->{branchcode},
2689 biblionumber => $biblioitem->{biblionumber},
2690 biblioitemnumber => $biblioitem->{biblioitemnumber},
2695 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2696 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2697 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2700 subtest 'CanBookBeIssued | notforloan' => sub {
2703 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2705 my $library = $builder->build( { source => 'Branch' } );
2706 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2708 my $itemtype = $builder->build(
2710 source => 'Itemtype',
2711 value => { notforloan => undef, }
2715 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2716 my $item = $builder->build_object(
2718 class => 'Koha::Items',
2720 homebranch => $library->{branchcode},
2721 holdingbranch => $library->{branchcode},
2725 itype => $itemtype->{itemtype},
2726 biblionumber => $biblioitem->{biblionumber},
2727 biblioitemnumber => $biblioitem->{biblioitemnumber},
2732 my ( $issuingimpossible, $needsconfirmation );
2735 subtest 'item-level_itypes = 1' => sub {
2738 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2739 # Is for loan at item type and item level
2740 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2741 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2742 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2744 # not for loan at item type level
2745 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2746 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2747 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2750 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2751 'Item can not be issued, not for loan at item type level'
2754 # not for loan at item level
2755 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2756 $item->notforloan( 1 )->store;
2757 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2758 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2761 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2762 'Item can not be issued, not for loan at item type level'
2766 subtest 'item-level_itypes = 0' => sub {
2769 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2771 # We set another itemtype for biblioitem
2772 my $itemtype = $builder->build(
2774 source => 'Itemtype',
2775 value => { notforloan => undef, }
2779 # for loan at item type and item level
2780 $item->notforloan(0)->store;
2781 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
2782 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2783 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2784 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2786 # not for loan at item type level
2787 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2788 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2789 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2792 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2793 'Item can not be issued, not for loan at item type level'
2796 # not for loan at item level
2797 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2798 $item->notforloan( 1 )->store;
2799 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2800 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2803 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2804 'Item can not be issued, not for loan at item type level'
2808 # TODO test with AllowNotForLoanOverride = 1
2811 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
2814 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
2815 my $item = $builder->build_object({ class => 'Koha::Items', value => { onloan => '2018-01-01' }});
2816 AddReturn( $item->barcode, $item->homebranch );
2817 $item->discard_changes; # refresh
2818 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
2821 $schema->storage->txn_rollback;
2822 C4::Context->clear_syspref_cache();
2823 $cache->clear_from_cache('single_holidays');
2826 my ( $library ) = @_;
2827 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
2831 my ( $error, $question, $alert ) = @_;
2833 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
2834 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
2835 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
2840 my ($title, $author) = @_;
2842 my $marcflavour = C4::Context->preference('marcflavour');
2844 my $biblio = MARC::Record->new();
2846 my $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
2847 $biblio->append_fields(
2848 MARC::Field->new($tag, ' ', ' ', a => $title),
2853 my ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
2854 $biblio->append_fields(
2855 MARC::Field->new($tag, ' ', ' ', $code => $author),
2859 return AddBiblio($biblio, '');
2862 sub test_debarment_on_checkout {
2864 my $item = $params->{item};
2865 my $library = $params->{library};
2866 my $patron = $params->{patron};
2867 my $due_date = $params->{due_date} || dt_from_string;
2868 my $return_date = $params->{return_date} || dt_from_string;
2869 my $expected_expiration_date = $params->{expiration_date};
2871 $expected_expiration_date = output_pref(
2873 dt => $expected_expiration_date,
2874 dateformat => 'sql',
2878 my @caller = caller;
2879 my $line_number = $caller[2];
2880 AddIssue( $patron, $item->{barcode}, $due_date );
2882 my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode},
2883 undef, undef, $return_date );
2884 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
2885 or diag('AddReturn returned message ' . Dumper $message );
2886 my $debarments = Koha::Patron::Debarments::GetDebarments(
2887 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
2888 is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
2890 is( $debarments->[0]->{expiration},
2891 $expected_expiration_date, 'Test at line ' . $line_number );
2892 Koha::Patron::Debarments::DelUniqueDebarment(
2893 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );