3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 114;
23 use POSIX qw( floor );
25 use t::lib::TestBuilder;
33 use C4::Overdues qw(UpdateFine CalcFine);
36 use Koha::IssuingRules;
39 use Koha::Subscriptions;
40 use Koha::Account::Lines;
41 use Koha::Account::Offsets;
43 my $schema = Koha::Database->schema;
44 $schema->storage->txn_begin;
45 my $builder = t::lib::TestBuilder->new;
46 my $dbh = C4::Context->dbh;
49 $dbh->{RaiseError} = 1;
51 # Start with a clean slate
52 $dbh->do('DELETE FROM issues');
53 $dbh->do('DELETE FROM borrowers');
55 my $library = $builder->build({
58 my $library2 = $builder->build({
61 my $itemtype = $builder->build(
62 { source => 'Itemtype',
63 value => { notforloan => undef, rentalcharge => 0, defaultreplacecost => undef, processfee => undef }
66 my $patron_category = $builder->build(
72 BlockExpiredPatronOpacActions => -1, # Pick the pref value
77 my $CircControl = C4::Context->preference('CircControl');
78 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
81 homebranch => $library2->{branchcode},
82 holdingbranch => $library2->{branchcode}
86 branchcode => $library2->{branchcode}
89 # No userenv, PickupLibrary
90 t::lib::Mocks::mock_preference('IndependentBranches', '0');
91 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
93 C4::Context->preference('CircControl'),
95 'CircControl changed to PickupLibrary'
98 C4::Circulation::_GetCircControlBranch($item, $borrower),
99 $item->{$HomeOrHoldingBranch},
100 '_GetCircControlBranch returned item branch (no userenv defined)'
103 # No userenv, PatronLibrary
104 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
106 C4::Context->preference('CircControl'),
108 'CircControl changed to PatronLibrary'
111 C4::Circulation::_GetCircControlBranch($item, $borrower),
112 $borrower->{branchcode},
113 '_GetCircControlBranch returned borrower branch'
116 # No userenv, ItemHomeLibrary
117 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
119 C4::Context->preference('CircControl'),
121 'CircControl changed to ItemHomeLibrary'
124 $item->{$HomeOrHoldingBranch},
125 C4::Circulation::_GetCircControlBranch($item, $borrower),
126 '_GetCircControlBranch returned item branch'
130 C4::Context->_new_userenv('xxx');
131 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
132 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
134 # Userenv set, PickupLibrary
135 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
137 C4::Context->preference('CircControl'),
139 'CircControl changed to PickupLibrary'
142 C4::Circulation::_GetCircControlBranch($item, $borrower),
143 $library2->{branchcode},
144 '_GetCircControlBranch returned current branch'
147 # Userenv set, PatronLibrary
148 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
150 C4::Context->preference('CircControl'),
152 'CircControl changed to PatronLibrary'
155 C4::Circulation::_GetCircControlBranch($item, $borrower),
156 $borrower->{branchcode},
157 '_GetCircControlBranch returned borrower branch'
160 # Userenv set, ItemHomeLibrary
161 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
163 C4::Context->preference('CircControl'),
165 'CircControl changed to ItemHomeLibrary'
168 C4::Circulation::_GetCircControlBranch($item, $borrower),
169 $item->{$HomeOrHoldingBranch},
170 '_GetCircControlBranch returned item branch'
173 # Reset initial configuration
174 t::lib::Mocks::mock_preference('CircControl', $CircControl);
176 C4::Context->preference('CircControl'),
178 'CircControl reset to its initial value'
181 # Set a simple circ policy
182 $dbh->do('DELETE FROM issuingrules');
184 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
185 maxissueqty, issuelength, lengthunit,
186 renewalsallowed, renewalperiod,
187 norenewalbefore, auto_renew,
204 # Test C4::Circulation::ProcessOfflinePayment
205 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
207 my ( $original_count ) = $sth->fetchrow_array();
209 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', ?, ? )", undef, $patron_category->{categorycode}, $library2->{branchcode} );
211 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
214 my ( $new_count ) = $sth->fetchrow_array();
216 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
218 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
219 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
220 C4::Context->dbh->do("DELETE FROM accountlines");
222 # CanBookBeRenewed tests
224 # Generate test biblio
225 my $title = 'Silence in the library';
226 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
228 my $barcode = 'R00000342';
229 my $branch = $library2->{branchcode};
231 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
233 homebranch => $branch,
234 holdingbranch => $branch,
236 replacementprice => 12.00,
242 my $barcode2 = 'R00000343';
243 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
245 homebranch => $branch,
246 holdingbranch => $branch,
247 barcode => $barcode2,
248 replacementprice => 23.00,
254 my $barcode3 = 'R00000346';
255 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
257 homebranch => $branch,
258 holdingbranch => $branch,
259 barcode => $barcode3,
260 replacementprice => 23.00,
267 my %renewing_borrower_data = (
269 surname => 'Renewal',
270 categorycode => $patron_category->{categorycode},
271 branchcode => $branch,
274 my %reserving_borrower_data = (
275 firstname => 'Katrin',
276 surname => 'Reservation',
277 categorycode => $patron_category->{categorycode},
278 branchcode => $branch,
281 my %hold_waiting_borrower_data = (
283 surname => 'Reservation',
284 categorycode => $patron_category->{categorycode},
285 branchcode => $branch,
288 my %restricted_borrower_data = (
289 firstname => 'Alice',
290 surname => 'Reservation',
291 categorycode => $patron_category->{categorycode},
292 debarred => '3228-01-01',
293 branchcode => $branch,
296 my %expired_borrower_data = (
299 categorycode => $patron_category->{categorycode},
300 branchcode => $branch,
301 dateexpiry => dt_from_string->subtract( months => 1 ),
304 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
305 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
306 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
307 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
308 my $expired_borrowernumber = AddMember(%expired_borrower_data);
310 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
311 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
312 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
319 my $checkitem = undef;
322 my $issue = AddIssue( $renewing_borrower, $barcode);
323 my $datedue = dt_from_string( $issue->date_due() );
324 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
326 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
327 $datedue = dt_from_string( $issue->date_due() );
328 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
331 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $itemnumber } )->borrowernumber;
332 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
334 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
335 is( $renewokay, 1, 'Can renew, no holds for this title or item');
338 # Biblio-level hold, renewal test
340 $branch, $reserving_borrowernumber, $biblionumber,
341 $bibitems, $priority, $resdate, $expdate, $notes,
342 $title, $checkitem, $found
345 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
346 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
347 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
348 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
349 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
350 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
351 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
353 # Now let's add an item level hold, we should no longer be able to renew the item
354 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
356 borrowernumber => $hold_waiting_borrowernumber,
357 biblionumber => $biblionumber,
358 itemnumber => $itemnumber,
359 branchcode => $branch,
363 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
364 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
367 # 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
368 # be able to renew these items
369 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
371 borrowernumber => $hold_waiting_borrowernumber,
372 biblionumber => $biblionumber,
373 itemnumber => $itemnumber3,
374 branchcode => $branch,
379 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
380 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
381 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
382 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
383 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
385 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
386 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
387 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
389 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
390 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
391 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
393 my $reserveid = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
394 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
395 AddIssue($reserving_borrower, $barcode3);
396 my $reserve = $dbh->selectrow_hashref(
397 'SELECT * FROM old_reserves WHERE reserve_id = ?',
401 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
403 # Item-level hold, renewal test
405 $branch, $reserving_borrowernumber, $biblionumber,
406 $bibitems, $priority, $resdate, $expdate, $notes,
407 $title, $itemnumber, $found
410 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
411 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
412 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
414 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
415 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
417 # Items can't fill hold for reasons
418 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
419 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
420 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
421 ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber);
423 # FIXME: Add more for itemtype not for loan etc.
425 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
426 my $barcode5 = 'R00000347';
427 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
429 homebranch => $branch,
430 holdingbranch => $branch,
431 barcode => $barcode5,
432 replacementprice => 23.00,
437 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
438 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
440 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
441 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
442 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
443 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
444 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
446 # Users cannot renew an overdue item
447 my $barcode6 = 'R00000348';
448 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
450 homebranch => $branch,
451 holdingbranch => $branch,
452 barcode => $barcode6,
453 replacementprice => 23.00,
459 my $barcode7 = 'R00000349';
460 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
462 homebranch => $branch,
463 holdingbranch => $branch,
464 barcode => $barcode7,
465 replacementprice => 23.00,
470 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
471 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
473 my $now = dt_from_string();
474 my $five_weeks = DateTime::Duration->new(weeks => 5);
475 my $five_weeks_ago = $now - $five_weeks;
476 t::lib::Mocks::mock_preference('finesMode', 'production');
478 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
479 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
481 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
482 C4::Overdues::UpdateFine(
484 issue_id => $passeddatedue1->id(),
485 itemnumber => $itemnumber7,
486 borrowernumber => $renewing_borrower->{borrowernumber},
489 due => Koha::DateUtils::output_pref($five_weeks_ago)
493 t::lib::Mocks::mock_preference('RenewalLog', 0);
494 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
495 my $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
496 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
497 my $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
498 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
500 t::lib::Mocks::mock_preference('RenewalLog', 1);
501 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
502 $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
503 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
504 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
505 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
507 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
508 is( $fines->count, 2 );
509 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
510 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
514 my $old_issue_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
515 my $old_renew_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
516 AddIssue( $renewing_borrower,$barcode7,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
517 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
518 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
519 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
520 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
522 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
525 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
526 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
527 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
528 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
529 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
532 $hold = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next;
536 # Test automatic renewal before value for "norenewalbefore" in policy is set
537 # In this case automatic renewal is not permitted prior to due date
538 my $barcode4 = '11235813';
539 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
541 homebranch => $branch,
542 holdingbranch => $branch,
543 barcode => $barcode4,
544 replacementprice => 16.00,
550 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
551 ( $renewokay, $error ) =
552 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
553 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
554 is( $error, 'auto_too_soon',
555 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
558 # Test premature manual renewal
559 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
561 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
562 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
563 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
566 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
567 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
569 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
570 $datedue->clone->add( days => -7 ),
571 'Bug 14395: Renewals permitted 7 days before due date, as expected'
575 # Test 'date' setting for syspref NoRenewalBeforePrecision
576 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
578 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
579 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
580 'Bug 14395: Renewals permitted 7 days before due date, as expected'
584 # Test premature automatic renewal
585 ( $renewokay, $error ) =
586 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
587 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
588 is( $error, 'auto_too_soon',
589 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
592 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
593 # and test automatic renewal again
594 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
595 ( $renewokay, $error ) =
596 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
597 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
598 is( $error, 'auto_too_soon',
599 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
602 # Change policy so that loans can be renewed 99 days prior to the due date
603 # and test automatic renewal again
604 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
605 ( $renewokay, $error ) =
606 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
607 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
608 is( $error, 'auto_renew',
609 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
612 subtest "too_late_renewal / no_auto_renewal_after" => sub {
614 my $item_to_auto_renew = $builder->build(
617 biblionumber => $biblionumber,
618 homebranch => $branch,
619 holdingbranch => $branch,
624 my $ten_days_before = dt_from_string->add( days => -10 );
625 my $ten_days_ahead = dt_from_string->add( days => 10 );
626 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
628 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
629 ( $renewokay, $error ) =
630 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
631 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
632 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
634 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
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 auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
640 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
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_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
646 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, 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_renew', 'Cannot renew, renew is automatic' );
652 $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 ) );
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_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
658 $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 ) );
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 = 10, no_auto_renewal_after = NULL, 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_renew', 'Cannot renew, renew is automatic' );
671 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
673 my $item_to_auto_renew = $builder->build({
676 biblionumber => $biblionumber,
677 homebranch => $branch,
678 holdingbranch => $branch,
682 my $ten_days_before = dt_from_string->add( days => -10 );
683 my $ten_days_ahead = dt_from_string->add( days => 10 );
684 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
686 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
687 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
688 C4::Context->set_preference('OPACFineNoRenewals','10');
689 my $fines_amount = 5;
690 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
691 ( $renewokay, $error ) =
692 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
693 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
694 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 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 10' );
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_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
708 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
711 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
713 my $item_to_auto_renew = $builder->build({
716 biblionumber => $biblionumber,
717 homebranch => $branch,
718 holdingbranch => $branch,
722 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
724 my $ten_days_before = dt_from_string->add( days => -10 );
725 my $ten_days_ahead = dt_from_string->add( days => 10 );
727 # Patron is expired and BlockExpiredPatronOpacActions=0
728 # => auto renew is allowed
729 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
730 my $patron = $expired_borrower;
731 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
732 ( $renewokay, $error ) =
733 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
734 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
735 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
736 Koha::Checkouts->find( $checkout->issue_id )->delete;
739 # Patron is expired and BlockExpiredPatronOpacActions=1
740 # => auto renew is not allowed
741 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
742 $patron = $expired_borrower;
743 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
744 ( $renewokay, $error ) =
745 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
746 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
747 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
748 Koha::Checkouts->find( $checkout->issue_id )->delete;
751 # Patron is not expired and BlockExpiredPatronOpacActions=1
752 # => auto renew is allowed
753 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
754 $patron = $renewing_borrower;
755 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
756 ( $renewokay, $error ) =
757 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
758 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
759 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
760 Koha::Checkouts->find( $checkout->issue_id )->delete;
763 subtest "GetLatestAutoRenewDate" => sub {
765 my $item_to_auto_renew = $builder->build(
768 biblionumber => $biblionumber,
769 homebranch => $branch,
770 holdingbranch => $branch,
775 my $ten_days_before = dt_from_string->add( days => -10 );
776 my $ten_days_ahead = dt_from_string->add( days => 10 );
777 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
778 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
779 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
780 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' );
781 my $five_days_before = dt_from_string->add( days => -5 );
782 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
783 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
784 is( $latest_auto_renew_date->truncate( to => 'minute' ),
785 $five_days_before->truncate( to => 'minute' ),
786 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
788 my $five_days_ahead = dt_from_string->add( days => 5 );
789 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
790 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
791 is( $latest_auto_renew_date->truncate( to => 'minute' ),
792 $five_days_ahead->truncate( to => 'minute' ),
793 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
795 my $two_days_ahead = dt_from_string->add( days => 2 );
796 $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 ) );
797 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
798 is( $latest_auto_renew_date->truncate( to => 'day' ),
799 $two_days_ahead->truncate( to => 'day' ),
800 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
802 $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 ) );
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 < no_auto_renewal_after'
813 # set policy to forbid renewals
814 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
816 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
817 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
818 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
820 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
821 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
822 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
824 C4::Overdues::UpdateFine(
826 issue_id => $issue->id(),
827 itemnumber => $itemnumber,
828 borrowernumber => $renewing_borrower->{borrowernumber},
831 due => Koha::DateUtils::output_pref($datedue)
835 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
836 is( $line->accounttype, 'FU', 'Account line type is FU' );
837 is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
838 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
839 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
840 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
842 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
843 is( $offset->type, 'Fine', 'Account offset type is Fine' );
844 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
846 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
847 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
849 LostItem( $itemnumber, 1 );
851 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
852 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
854 my $total_due = $dbh->selectrow_array(
855 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
856 undef, $renewing_borrower->{borrowernumber}
859 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
861 C4::Context->dbh->do("DELETE FROM accountlines");
863 C4::Overdues::UpdateFine(
865 issue_id => $issue2->id(),
866 itemnumber => $itemnumber2,
867 borrowernumber => $renewing_borrower->{borrowernumber},
870 due => Koha::DateUtils::output_pref($datedue)
874 LostItem( $itemnumber2, 0 );
876 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
877 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
879 $total_due = $dbh->selectrow_array(
880 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
881 undef, $renewing_borrower->{borrowernumber}
884 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
886 my $future = dt_from_string();
887 $future->add( days => 7 );
888 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
889 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
891 # Users cannot renew any item if there is an overdue item
892 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
893 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
894 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
895 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
896 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
901 # GetUpcomingDueIssues tests
902 my $barcode = 'R00000342';
903 my $barcode2 = 'R00000343';
904 my $barcode3 = 'R00000344';
905 my $branch = $library2->{branchcode};
907 #Create another record
908 my $title2 = 'Something is worng here';
909 my ($biblionumber2, $biblioitemnumber2) = add_biblio($title2, 'Anonymous');
914 homebranch => $branch,
915 holdingbranch => $branch,
916 barcode => $barcode3,
923 my %a_borrower_data = (
924 firstname => 'Fridolyn',
926 categorycode => $patron_category->{categorycode},
927 branchcode => $branch,
930 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
931 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
933 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
934 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
935 my $today = DateTime->today(time_zone => C4::Context->tz());
937 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
938 my $datedue = dt_from_string( $issue->date_due() );
939 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
940 my $datedue2 = dt_from_string( $issue->date_due() );
944 # GetUpcomingDueIssues tests
946 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
947 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
950 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
951 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
952 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
955 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
956 is ( scalar( @$upcoming_dues ), 1,
957 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
960 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
962 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
964 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
965 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
967 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
968 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
970 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
971 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
973 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
974 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
976 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
977 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
979 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
980 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
985 my $barcode = '1234567890';
986 my $branch = $library2->{branchcode};
988 my ($biblionumber, $biblioitemnumber) = add_biblio();
991 my ( undef, undef, $itemnumber ) = AddItem(
993 homebranch => $branch,
994 holdingbranch => $branch,
1002 my %a_borrower_data = (
1003 firstname => 'Kyle',
1005 categorycode => $patron_category->{categorycode},
1006 branchcode => $branch,
1009 my $borrowernumber = AddMember(%a_borrower_data);
1011 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1012 my $issue = AddIssue( $borrower, $barcode );
1015 issue_id => $issue->id(),
1016 itemnumber => $itemnumber,
1017 borrowernumber => $borrowernumber,
1023 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
1024 my $count = $hr->{count};
1026 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1030 $dbh->do('DELETE FROM issues');
1031 $dbh->do('DELETE FROM items');
1032 $dbh->do('DELETE FROM issuingrules');
1035 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
1036 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1045 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
1047 my $barcode1 = '1234';
1048 my ( undef, undef, $itemnumber1 ) = AddItem(
1050 homebranch => $library2->{branchcode},
1051 holdingbranch => $library2->{branchcode},
1052 barcode => $barcode1,
1057 my $barcode2 = '4321';
1058 my ( undef, undef, $itemnumber2 ) = AddItem(
1060 homebranch => $library2->{branchcode},
1061 holdingbranch => $library2->{branchcode},
1062 barcode => $barcode2,
1068 my $borrowernumber1 = AddMember(
1069 firstname => 'Kyle',
1071 categorycode => $patron_category->{categorycode},
1072 branchcode => $library2->{branchcode},
1074 my $borrowernumber2 = AddMember(
1075 firstname => 'Chelsea',
1077 categorycode => $patron_category->{categorycode},
1078 branchcode => $library2->{branchcode},
1081 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1082 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1084 my $issue = AddIssue( $borrower1, $barcode1 );
1086 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1087 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1090 $library2->{branchcode}, $borrowernumber2, $biblionumber,
1091 '', 1, undef, undef, '',
1095 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1096 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1097 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1098 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1100 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1101 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1102 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1103 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1105 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1106 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1107 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1108 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1110 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1111 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1112 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1113 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1115 # Setting item not checked out to be not for loan but holdable
1116 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
1118 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1119 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' );
1123 # Don't allow renewing onsite checkout
1124 my $barcode = 'R00000XXX';
1125 my $branch = $library->{branchcode};
1127 #Create another record
1128 my ($biblionumber, $biblioitemnumber) = add_biblio('A title', 'Anonymous');
1130 my (undef, undef, $itemnumber) = AddItem(
1132 homebranch => $branch,
1133 holdingbranch => $branch,
1134 barcode => $barcode,
1140 my $borrowernumber = AddMember(
1143 categorycode => $patron_category->{categorycode},
1144 branchcode => $branch,
1147 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1149 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1150 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
1151 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1152 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1156 my $library = $builder->build({ source => 'Branch' });
1158 my ($biblionumber, $biblioitemnumber) = add_biblio();
1160 my $barcode = 'just a barcode';
1161 my ( undef, undef, $itemnumber ) = AddItem(
1163 homebranch => $library->{branchcode},
1164 holdingbranch => $library->{branchcode},
1165 barcode => $barcode,
1171 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1173 my $issue = AddIssue( $patron, $barcode );
1176 issue_id => $issue->id(),
1177 itemnumber => $itemnumber,
1178 borrowernumber => $patron->{borrowernumber},
1185 issue_id => $issue->id(),
1186 itemnumber => $itemnumber,
1187 borrowernumber => $patron->{borrowernumber},
1192 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1195 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1198 my $homebranch = $builder->build( { source => 'Branch' } );
1199 my $holdingbranch = $builder->build( { source => 'Branch' } );
1200 my $otherbranch = $builder->build( { source => 'Branch' } );
1201 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1202 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1204 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1205 my $item = $builder->build(
1208 homebranch => $homebranch->{branchcode},
1209 holdingbranch => $holdingbranch->{branchcode},
1214 biblionumber => $biblioitem->{biblionumber}
1219 set_userenv($holdingbranch);
1221 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1222 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Checkout
1224 my ( $error, $question, $alerts );
1226 # AllowReturnToBranch == anywhere
1227 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1228 ## Can be issued from homebranch
1229 set_userenv($homebranch);
1230 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1231 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1232 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1233 ## Can be issued from holdingbranch
1234 set_userenv($holdingbranch);
1235 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1236 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1237 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1238 ## Can be issued from another branch
1239 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1240 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1241 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1243 # AllowReturnToBranch == holdingbranch
1244 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1245 ## Cannot be issued from homebranch
1246 set_userenv($homebranch);
1247 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1248 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1249 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1250 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1251 ## Can be issued from holdinbranch
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 ## Cannot be issued from another branch
1257 set_userenv($otherbranch);
1258 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1259 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1260 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1261 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1263 # AllowReturnToBranch == homebranch
1264 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1265 ## Can be issued from holdinbranch
1266 set_userenv($homebranch);
1267 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1268 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1269 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1270 ## Cannot be issued from holdinbranch
1271 set_userenv($holdingbranch);
1272 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1273 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1274 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1275 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1276 ## Cannot be issued from holdinbranch
1277 set_userenv($otherbranch);
1278 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1279 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1280 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1281 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1283 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1286 subtest 'AddIssue & AllowReturnToBranch' => sub {
1289 my $homebranch = $builder->build( { source => 'Branch' } );
1290 my $holdingbranch = $builder->build( { source => 'Branch' } );
1291 my $otherbranch = $builder->build( { source => 'Branch' } );
1292 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1293 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1295 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1296 my $item = $builder->build(
1299 homebranch => $homebranch->{branchcode},
1300 holdingbranch => $holdingbranch->{branchcode},
1304 biblionumber => $biblioitem->{biblionumber}
1309 set_userenv($holdingbranch);
1311 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Checkout
1312 my $issue = AddIssue( $patron_1, $item->{barcode} );
1314 my ( $error, $question, $alerts );
1316 # AllowReturnToBranch == homebranch
1317 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1318 ## Can be issued from homebranch
1319 set_userenv($homebranch);
1320 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1321 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1322 ## Can be issued from holdinbranch
1323 set_userenv($holdingbranch);
1324 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1325 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1326 ## Can be issued from another branch
1327 set_userenv($otherbranch);
1328 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1329 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1331 # AllowReturnToBranch == holdinbranch
1332 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1333 ## Cannot be issued from homebranch
1334 set_userenv($homebranch);
1335 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1336 ## Can be issued from holdingbranch
1337 set_userenv($holdingbranch);
1338 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1339 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1340 ## Cannot be issued from another branch
1341 set_userenv($otherbranch);
1342 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1344 # AllowReturnToBranch == homebranch
1345 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1346 ## Can be issued from homebranch
1347 set_userenv($homebranch);
1348 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1349 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1350 ## Cannot be issued from holdinbranch
1351 set_userenv($holdingbranch);
1352 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1353 ## Cannot be issued from another branch
1354 set_userenv($otherbranch);
1355 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1356 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1359 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1362 my $library = $builder->build( { source => 'Branch' } );
1363 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1365 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1366 my $item_1 = $builder->build(
1369 homebranch => $library->{branchcode},
1370 holdingbranch => $library->{branchcode},
1375 biblionumber => $biblioitem_1->{biblionumber}
1379 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1380 my $item_2 = $builder->build(
1383 homebranch => $library->{branchcode},
1384 holdingbranch => $library->{branchcode},
1389 biblionumber => $biblioitem_2->{biblionumber}
1394 my ( $error, $question, $alerts );
1396 # Patron cannot issue item_1, they have overdues
1397 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1398 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1400 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1401 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1402 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1403 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1405 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1406 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1407 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1408 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1410 # Patron cannot issue item_1, they are debarred
1411 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1412 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1413 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1414 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1415 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1417 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1418 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1419 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1420 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1423 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1426 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1427 my $patron_category_x = $builder->build_object(
1429 class => 'Koha::Patron::Categories',
1430 value => { category_type => 'X' }
1433 my $patron = $builder->build_object(
1435 class => 'Koha::Patrons',
1437 categorycode => $patron_category_x->categorycode,
1438 gonenoaddress => undef,
1445 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1446 my $item_1 = $builder->build(
1450 homebranch => $library->branchcode,
1451 holdingbranch => $library->branchcode,
1456 biblionumber => $biblioitem_1->{biblionumber}
1461 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1462 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1464 # TODO There are other tests to provide here
1467 subtest 'MultipleReserves' => sub {
1470 my $title = 'Silence in the library';
1471 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
1473 my $branch = $library2->{branchcode};
1475 my $barcode1 = 'R00110001';
1476 my ( $item_bibnum1, $item_bibitemnum1, $itemnumber1 ) = AddItem(
1478 homebranch => $branch,
1479 holdingbranch => $branch,
1480 barcode => $barcode1,
1481 replacementprice => 12.00,
1487 my $barcode2 = 'R00110002';
1488 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
1490 homebranch => $branch,
1491 holdingbranch => $branch,
1492 barcode => $barcode2,
1493 replacementprice => 12.00,
1501 my $resdate = undef;
1502 my $expdate = undef;
1504 my $checkitem = undef;
1507 my %renewing_borrower_data = (
1508 firstname => 'John',
1509 surname => 'Renewal',
1510 categorycode => $patron_category->{categorycode},
1511 branchcode => $branch,
1513 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
1514 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1515 my $issue = AddIssue( $renewing_borrower, $barcode1);
1516 my $datedue = dt_from_string( $issue->date_due() );
1517 is (defined $issue->date_due(), 1, "item 1 checked out");
1518 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $itemnumber1 })->borrowernumber;
1520 my %reserving_borrower_data1 = (
1521 firstname => 'Katrin',
1522 surname => 'Reservation',
1523 categorycode => $patron_category->{categorycode},
1524 branchcode => $branch,
1526 my $reserving_borrowernumber1 = AddMember(%reserving_borrower_data1);
1528 $branch, $reserving_borrowernumber1, $biblionumber,
1529 $bibitems, $priority, $resdate, $expdate, $notes,
1530 $title, $checkitem, $found
1533 my %reserving_borrower_data2 = (
1534 firstname => 'Kirk',
1535 surname => 'Reservation',
1536 categorycode => $patron_category->{categorycode},
1537 branchcode => $branch,
1539 my $reserving_borrowernumber2 = AddMember(%reserving_borrower_data2);
1541 $branch, $reserving_borrowernumber2, $biblionumber,
1542 $bibitems, $priority, $resdate, $expdate, $notes,
1543 $title, $checkitem, $found
1547 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1548 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1551 my $barcode3 = 'R00110003';
1552 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
1554 homebranch => $branch,
1555 holdingbranch => $branch,
1556 barcode => $barcode3,
1557 replacementprice => 12.00,
1564 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1565 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1569 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1572 my $library = $builder->build( { source => 'Branch' } );
1573 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1575 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1576 my $biblionumber = $biblioitem->{biblionumber};
1577 my $item_1 = $builder->build(
1580 homebranch => $library->{branchcode},
1581 holdingbranch => $library->{branchcode},
1585 biblionumber => $biblionumber,
1589 my $item_2 = $builder->build(
1592 homebranch => $library->{branchcode},
1593 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 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1795 # We want to charge 2 days every day, without grace
1796 # With 5 days of overdue: 5 * Z
1797 AddReturn( $item_1->{barcode}, $library->{branchcode},
1798 undef, undef, dt_from_string );
1799 my $debarments = Koha::Patron::Debarments::GetDebarments(
1800 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1801 is( scalar(@$debarments), 1 );
1803 my $expected_expiration = output_pref(
1805 dt => dt_from_string->add( days => ( 5 * 2 ) / 1 ),
1806 dateformat => 'sql',
1810 is( $debarments->[0]->{expiration}, $expected_expiration );
1811 Koha::Patron::Debarments::DelUniqueDebarment(
1812 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1814 # We want to charge 2 days every 2 days, without grace
1815 # With 5 days of overdue: (5 * 2) / 2
1816 $rule->suspension_chargeperiod(2)->store;
1817 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1819 AddReturn( $item_1->{barcode}, $library->{branchcode},
1820 undef, undef, dt_from_string );
1821 $debarments = Koha::Patron::Debarments::GetDebarments(
1822 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1823 is( scalar(@$debarments), 1 );
1825 $expected_expiration = output_pref(
1827 dt => dt_from_string->add( days => floor( 5 * 2 ) / 2 ),
1828 dateformat => 'sql',
1832 is( $debarments->[0]->{expiration}, $expected_expiration );
1833 Koha::Patron::Debarments::DelUniqueDebarment(
1834 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1836 # We want to charge 2 days every 3 days, with 1 day of grace
1837 # With 5 days of overdue: ((5-1) / 3 ) * 2
1838 $rule->suspension_chargeperiod(3)->store;
1839 $rule->firstremind(1)->store;
1840 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1842 AddReturn( $item_1->{barcode}, $library->{branchcode},
1843 undef, undef, dt_from_string );
1844 $debarments = Koha::Patron::Debarments::GetDebarments(
1845 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1846 is( scalar(@$debarments), 1 );
1848 $expected_expiration = output_pref(
1850 dt => dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) ),
1851 dateformat => 'sql',
1855 is( $debarments->[0]->{expiration}, $expected_expiration );
1856 Koha::Patron::Debarments::DelUniqueDebarment(
1857 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1862 subtest 'AddReturn | is_overdue' => sub {
1865 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
1866 t::lib::Mocks::mock_preference('finesMode', 'production');
1867 t::lib::Mocks::mock_preference('MaxFine', '100');
1869 my $library = $builder->build( { source => 'Branch' } );
1870 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1872 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1873 my $item = $builder->build(
1877 homebranch => $library->{branchcode},
1878 holdingbranch => $library->{branchcode},
1882 biblionumber => $biblioitem->{biblionumber},
1887 Koha::IssuingRules->search->delete;
1888 my $rule = Koha::IssuingRule->new(
1890 categorycode => '*',
1895 lengthunit => 'days',
1896 fine => 1, # Charge 1 every day of overdue
1902 my $one_day_ago = dt_from_string->subtract( days => 1 );
1903 my $five_days_ago = dt_from_string->subtract( days => 5 );
1904 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1905 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1907 # No date specify, today will be used
1908 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1909 AddReturn( $item->{barcode}, $library->{branchcode} );
1910 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1911 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1913 # specify return date 5 days before => no overdue
1914 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1915 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $ten_days_ago );
1916 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1917 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1919 # specify return date 5 days later => overdue
1920 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1921 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $five_days_ago );
1922 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1923 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1925 # specify dropbox date 5 days before => no overdue
1926 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1927 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $ten_days_ago );
1928 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1929 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1931 # specify dropbox date 5 days later => overdue, or... not
1932 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1933 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $five_days_ago );
1934 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
1935 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1938 subtest '_FixAccountForLostAndReturned' => sub {
1941 # Generate test biblio
1942 my $title = 'Koha for Dummies';
1943 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Daria');
1945 my $barcode = 'KD123456789';
1946 my $branchcode = $library2->{branchcode};
1948 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
1950 homebranch => $branchcode,
1951 holdingbranch => $branchcode,
1952 barcode => $barcode,
1953 replacementprice => 99.00,
1959 my $patron = $builder->build( { source => 'Borrower' } );
1961 my $accountline = Koha::Account::Line->new(
1963 borrowernumber => $patron->{borrowernumber},
1965 itemnumber => $itemnumber,
1967 amountoutstanding => 99.00,
1971 C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->{borrowernumber} );
1973 $accountline->_result()->discard_changes();
1975 is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount' );
1976 is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )');
1979 subtest '_FixOverduesOnReturn' => sub {
1982 # Generate test biblio
1983 my $title = 'Koha for Dummies';
1984 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Kylie');
1986 my $barcode = 'KD987654321';
1987 my $branchcode = $library2->{branchcode};
1989 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
1991 homebranch => $branchcode,
1992 holdingbranch => $branchcode,
1993 barcode => $barcode,
1994 replacementprice => 99.00,
2000 my $patron = $builder->build( { source => 'Borrower' } );
2002 ## Start with basic call, should just close out the open fine
2003 my $accountline = Koha::Account::Line->new(
2005 borrowernumber => $patron->{borrowernumber},
2006 accounttype => 'FU',
2007 itemnumber => $itemnumber,
2009 amountoutstanding => 99.00,
2010 lastincrement => 9.00,
2014 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber );
2016 $accountline->_result()->discard_changes();
2018 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2019 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2022 ## Run again, with exemptfine enabled
2025 accounttype => 'FU',
2026 amountoutstanding => 99.00,
2030 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 1 );
2032 $accountline->_result()->discard_changes();
2034 is( $accountline->amountoutstanding, '0.000000', 'Fine has been reduced to 0' );
2035 is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2037 ## Run again, with dropbox mode enabled
2040 accounttype => 'FU',
2041 amountoutstanding => 99.00,
2045 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 0, 1 );
2047 $accountline->_result()->discard_changes();
2049 is( $accountline->amountoutstanding, '90.000000', 'Fine has been reduced to 90' );
2050 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2053 subtest 'Set waiting flag' => sub {
2056 my $library_1 = $builder->build( { source => 'Branch' } );
2057 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2058 my $library_2 = $builder->build( { source => 'Branch' } );
2059 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2061 my $biblio = $builder->build( { source => 'Biblio' } );
2062 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2064 my $item = $builder->build(
2068 homebranch => $library_1->{branchcode},
2069 holdingbranch => $library_1->{branchcode},
2073 biblionumber => $biblioitem->{biblionumber},
2078 set_userenv( $library_2 );
2079 my $reserve_id = AddReserve(
2080 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2081 '', 1, undef, undef, '', undef, $item->{itemnumber},
2084 set_userenv( $library_1 );
2085 my $do_transfer = 1;
2086 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2087 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2088 my $hold = Koha::Holds->find( $reserve_id );
2089 is( $hold->found, 'T', 'Hold is in transit' );
2091 my ( $status ) = CheckReserves($item->{itemnumber});
2092 is( $status, 'Reserved', 'Hold is not waiting yet');
2094 set_userenv( $library_2 );
2096 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2097 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2098 $hold = Koha::Holds->find( $reserve_id );
2099 is( $hold->found, 'W', 'Hold is waiting' );
2100 ( $status ) = CheckReserves($item->{itemnumber});
2101 is( $status, 'Waiting', 'Now the hold is waiting');
2104 subtest 'CanBookBeIssued | is_overdue' => sub {
2107 # Set a simple circ policy
2108 $dbh->do('DELETE FROM issuingrules');
2110 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2111 maxissueqty, issuelength, lengthunit,
2112 renewalsallowed, renewalperiod,
2113 norenewalbefore, auto_renew,
2130 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2131 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2132 my $library = $builder->build( { source => 'Branch' } );
2133 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2135 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2136 my $item = $builder->build(
2140 homebranch => $library->{branchcode},
2141 holdingbranch => $library->{branchcode},
2145 biblionumber => $biblioitem->{biblionumber},
2150 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2151 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2152 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2153 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2154 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2155 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2160 my ( $library ) = @_;
2161 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
2165 my ( $error, $question, $alert ) = @_;
2167 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
2168 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
2169 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
2174 my ($title, $author) = @_;
2176 my $marcflavour = C4::Context->preference('marcflavour');
2178 my $biblio = MARC::Record->new();
2180 my $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
2181 $biblio->append_fields(
2182 MARC::Field->new($tag, ' ', ' ', a => $title),
2187 my ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
2188 $biblio->append_fields(
2189 MARC::Field->new($tag, ' ', ' ', $code => $author),
2193 return AddBiblio($biblio, '');