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 => 124;
26 use POSIX qw( floor );
28 use t::lib::TestBuilder;
36 use C4::Overdues qw(UpdateFine CalcFine);
39 use Koha::IssuingRules;
42 use Koha::Subscriptions;
43 use Koha::Account::Lines;
44 use Koha::Account::Offsets;
46 my $schema = Koha::Database->schema;
47 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new;
49 my $dbh = C4::Context->dbh;
52 $dbh->{RaiseError} = 1;
54 my $cache = Koha::Caches->get_instance();
55 $dbh->do(q|DELETE FROM special_holidays|);
56 $dbh->do(q|DELETE FROM repeatable_holidays|);
57 $cache->clear_from_cache('single_holidays');
59 # Start with a clean slate
60 $dbh->do('DELETE FROM issues');
61 $dbh->do('DELETE FROM borrowers');
63 my $library = $builder->build({
66 my $library2 = $builder->build({
69 my $itemtype = $builder->build(
70 { source => 'Itemtype',
71 value => { notforloan => undef, rentalcharge => 0, defaultreplacecost => undef, processfee => undef }
74 my $patron_category = $builder->build(
80 BlockExpiredPatronOpacActions => -1, # Pick the pref value
85 my $CircControl = C4::Context->preference('CircControl');
86 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
89 homebranch => $library2->{branchcode},
90 holdingbranch => $library2->{branchcode}
94 branchcode => $library2->{branchcode}
97 # No userenv, PickupLibrary
98 t::lib::Mocks::mock_preference('IndependentBranches', '0');
99 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
101 C4::Context->preference('CircControl'),
103 'CircControl changed to PickupLibrary'
106 C4::Circulation::_GetCircControlBranch($item, $borrower),
107 $item->{$HomeOrHoldingBranch},
108 '_GetCircControlBranch returned item branch (no userenv defined)'
111 # No userenv, PatronLibrary
112 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
114 C4::Context->preference('CircControl'),
116 'CircControl changed to PatronLibrary'
119 C4::Circulation::_GetCircControlBranch($item, $borrower),
120 $borrower->{branchcode},
121 '_GetCircControlBranch returned borrower branch'
124 # No userenv, ItemHomeLibrary
125 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
127 C4::Context->preference('CircControl'),
129 'CircControl changed to ItemHomeLibrary'
132 $item->{$HomeOrHoldingBranch},
133 C4::Circulation::_GetCircControlBranch($item, $borrower),
134 '_GetCircControlBranch returned item branch'
138 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
139 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
141 # Userenv set, PickupLibrary
142 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
144 C4::Context->preference('CircControl'),
146 'CircControl changed to PickupLibrary'
149 C4::Circulation::_GetCircControlBranch($item, $borrower),
150 $library2->{branchcode},
151 '_GetCircControlBranch returned current branch'
154 # Userenv set, PatronLibrary
155 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
157 C4::Context->preference('CircControl'),
159 'CircControl changed to PatronLibrary'
162 C4::Circulation::_GetCircControlBranch($item, $borrower),
163 $borrower->{branchcode},
164 '_GetCircControlBranch returned borrower branch'
167 # Userenv set, ItemHomeLibrary
168 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
170 C4::Context->preference('CircControl'),
172 'CircControl changed to ItemHomeLibrary'
175 C4::Circulation::_GetCircControlBranch($item, $borrower),
176 $item->{$HomeOrHoldingBranch},
177 '_GetCircControlBranch returned item branch'
180 # Reset initial configuration
181 t::lib::Mocks::mock_preference('CircControl', $CircControl);
183 C4::Context->preference('CircControl'),
185 'CircControl reset to its initial value'
188 # Set a simple circ policy
189 $dbh->do('DELETE FROM issuingrules');
191 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
192 maxissueqty, issuelength, lengthunit,
193 renewalsallowed, renewalperiod,
194 norenewalbefore, auto_renew,
212 # CanBookBeRenewed tests
213 C4::Context->set_preference('ItemsDeniedRenewal','');
214 # Generate test biblio
215 my $title = 'Silence in the library';
216 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
218 my $barcode = 'R00000342';
219 my $branch = $library2->{branchcode};
221 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
223 homebranch => $branch,
224 holdingbranch => $branch,
226 replacementprice => 12.00,
232 my $barcode2 = 'R00000343';
233 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
235 homebranch => $branch,
236 holdingbranch => $branch,
237 barcode => $barcode2,
238 replacementprice => 23.00,
244 my $barcode3 = 'R00000346';
245 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
247 homebranch => $branch,
248 holdingbranch => $branch,
249 barcode => $barcode3,
250 replacementprice => 23.00,
257 my %renewing_borrower_data = (
259 surname => 'Renewal',
260 categorycode => $patron_category->{categorycode},
261 branchcode => $branch,
264 my %reserving_borrower_data = (
265 firstname => 'Katrin',
266 surname => 'Reservation',
267 categorycode => $patron_category->{categorycode},
268 branchcode => $branch,
271 my %hold_waiting_borrower_data = (
273 surname => 'Reservation',
274 categorycode => $patron_category->{categorycode},
275 branchcode => $branch,
278 my %restricted_borrower_data = (
279 firstname => 'Alice',
280 surname => 'Reservation',
281 categorycode => $patron_category->{categorycode},
282 debarred => '3228-01-01',
283 branchcode => $branch,
286 my %expired_borrower_data = (
289 categorycode => $patron_category->{categorycode},
290 branchcode => $branch,
291 dateexpiry => dt_from_string->subtract( months => 1 ),
294 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
295 my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
296 my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
297 my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
298 my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
300 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
301 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
302 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
309 my $checkitem = undef;
312 my $issue = AddIssue( $renewing_borrower, $barcode);
313 my $datedue = dt_from_string( $issue->date_due() );
314 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
316 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
317 $datedue = dt_from_string( $issue->date_due() );
318 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
321 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $itemnumber } )->borrowernumber;
322 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
324 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
325 is( $renewokay, 1, 'Can renew, no holds for this title or item');
328 # Biblio-level hold, renewal test
330 $branch, $reserving_borrowernumber, $biblionumber,
331 $bibitems, $priority, $resdate, $expdate, $notes,
332 $title, $checkitem, $found
335 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
336 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
337 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
338 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
339 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
340 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
341 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
343 # Now let's add an item level hold, we should no longer be able to renew the item
344 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
346 borrowernumber => $hold_waiting_borrowernumber,
347 biblionumber => $biblionumber,
348 itemnumber => $itemnumber,
349 branchcode => $branch,
353 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
354 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
357 # 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
358 # be able to renew these items
359 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
361 borrowernumber => $hold_waiting_borrowernumber,
362 biblionumber => $biblionumber,
363 itemnumber => $itemnumber3,
364 branchcode => $branch,
369 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
370 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
371 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
372 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
373 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
375 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
376 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
377 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
379 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
380 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
381 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
383 my $reserveid = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
384 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
385 AddIssue($reserving_borrower, $barcode3);
386 my $reserve = $dbh->selectrow_hashref(
387 'SELECT * FROM old_reserves WHERE reserve_id = ?',
391 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
393 # Item-level hold, renewal test
395 $branch, $reserving_borrowernumber, $biblionumber,
396 $bibitems, $priority, $resdate, $expdate, $notes,
397 $title, $itemnumber, $found
400 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
401 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
402 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
404 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
405 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
407 # Items can't fill hold for reasons
408 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
409 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
410 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
411 ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber);
413 # FIXME: Add more for itemtype not for loan etc.
415 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
416 my $barcode5 = 'R00000347';
417 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
419 homebranch => $branch,
420 holdingbranch => $branch,
421 barcode => $barcode5,
422 replacementprice => 23.00,
427 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
428 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
430 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
431 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
432 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
433 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
434 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
436 # Users cannot renew an overdue item
437 my $barcode6 = 'R00000348';
438 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
440 homebranch => $branch,
441 holdingbranch => $branch,
442 barcode => $barcode6,
443 replacementprice => 23.00,
449 my $barcode7 = 'R00000349';
450 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
452 homebranch => $branch,
453 holdingbranch => $branch,
454 barcode => $barcode7,
455 replacementprice => 23.00,
460 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
461 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
463 my $now = dt_from_string();
464 my $five_weeks = DateTime::Duration->new(weeks => 5);
465 my $five_weeks_ago = $now - $five_weeks;
466 t::lib::Mocks::mock_preference('finesMode', 'production');
468 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
469 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
471 my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
472 C4::Overdues::UpdateFine(
474 issue_id => $passeddatedue1->id(),
475 itemnumber => $itemnumber7,
476 borrowernumber => $renewing_borrower->{borrowernumber},
479 due => Koha::DateUtils::output_pref($five_weeks_ago)
483 t::lib::Mocks::mock_preference('RenewalLog', 0);
484 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
485 my $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
486 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
487 my $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
488 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
490 t::lib::Mocks::mock_preference('RenewalLog', 1);
491 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
492 $old_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
493 AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
494 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
495 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
497 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
498 is( $fines->count, 2 );
499 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
500 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
504 my $old_issue_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
505 my $old_renew_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
506 AddIssue( $renewing_borrower,$barcode7,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
507 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
508 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
509 $new_log_size = scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
510 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
512 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
515 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
516 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
517 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
518 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
519 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
522 $hold = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next;
526 # Test automatic renewal before value for "norenewalbefore" in policy is set
527 # In this case automatic renewal is not permitted prior to due date
528 my $barcode4 = '11235813';
529 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
531 homebranch => $branch,
532 holdingbranch => $branch,
533 barcode => $barcode4,
534 replacementprice => 16.00,
540 $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
541 ( $renewokay, $error ) =
542 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
543 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
544 is( $error, 'auto_too_soon',
545 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
548 # Test premature manual renewal
549 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
551 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
552 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
553 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
556 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
557 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
559 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
560 $datedue->clone->add( days => -7 ),
561 'Bug 14395: Renewals permitted 7 days before due date, as expected'
565 # Test 'date' setting for syspref NoRenewalBeforePrecision
566 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
568 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
569 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
570 'Bug 14395: Renewals permitted 7 days before due date, as expected'
574 # Test premature automatic renewal
575 ( $renewokay, $error ) =
576 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
577 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
578 is( $error, 'auto_too_soon',
579 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
582 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
583 # and test automatic renewal again
584 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
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, "No renewal before" = 0 (returned code is auto_too_soon)'
592 # Change policy so that loans can be renewed 99 days prior to the due date
593 # and test automatic renewal again
594 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
595 ( $renewokay, $error ) =
596 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
597 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
598 is( $error, 'auto_renew',
599 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
602 subtest "too_late_renewal / no_auto_renewal_after" => sub {
604 my $item_to_auto_renew = $builder->build(
607 biblionumber => $biblionumber,
608 homebranch => $branch,
609 holdingbranch => $branch,
614 my $ten_days_before = dt_from_string->add( days => -10 );
615 my $ten_days_ahead = dt_from_string->add( days => 10 );
616 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
618 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
619 ( $renewokay, $error ) =
620 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
621 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
622 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
624 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
625 ( $renewokay, $error ) =
626 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
627 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
628 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
630 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
631 ( $renewokay, $error ) =
632 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
633 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
634 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
636 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
637 ( $renewokay, $error ) =
638 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
639 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
640 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
642 $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 ) );
643 ( $renewokay, $error ) =
644 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
645 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
646 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
648 $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 ) );
649 ( $renewokay, $error ) =
650 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
651 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
652 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
654 $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 ) );
655 ( $renewokay, $error ) =
656 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
657 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
658 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
661 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
663 my $item_to_auto_renew = $builder->build({
666 biblionumber => $biblionumber,
667 homebranch => $branch,
668 holdingbranch => $branch,
672 my $ten_days_before = dt_from_string->add( days => -10 );
673 my $ten_days_ahead = dt_from_string->add( days => 10 );
674 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
676 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
677 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
678 C4::Context->set_preference('OPACFineNoRenewals','10');
679 my $fines_amount = 5;
680 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
681 ( $renewokay, $error ) =
682 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
683 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
684 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
686 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
687 ( $renewokay, $error ) =
688 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
689 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
690 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
692 C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
693 ( $renewokay, $error ) =
694 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
695 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
696 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
698 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
701 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
703 my $item_to_auto_renew = $builder->build({
706 biblionumber => $biblionumber,
707 homebranch => $branch,
708 holdingbranch => $branch,
712 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
714 my $ten_days_before = dt_from_string->add( days => -10 );
715 my $ten_days_ahead = dt_from_string->add( days => 10 );
717 # Patron is expired and BlockExpiredPatronOpacActions=0
718 # => auto renew is allowed
719 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
720 my $patron = $expired_borrower;
721 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
722 ( $renewokay, $error ) =
723 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
724 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
725 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
726 Koha::Checkouts->find( $checkout->issue_id )->delete;
729 # Patron is expired and BlockExpiredPatronOpacActions=1
730 # => auto renew is not allowed
731 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
732 $patron = $expired_borrower;
733 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
734 ( $renewokay, $error ) =
735 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
736 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
737 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
738 Koha::Checkouts->find( $checkout->issue_id )->delete;
741 # Patron is not expired and BlockExpiredPatronOpacActions=1
742 # => auto renew is allowed
743 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
744 $patron = $renewing_borrower;
745 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
746 ( $renewokay, $error ) =
747 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
748 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
749 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
750 Koha::Checkouts->find( $checkout->issue_id )->delete;
753 subtest "GetLatestAutoRenewDate" => sub {
755 my $item_to_auto_renew = $builder->build(
758 biblionumber => $biblionumber,
759 homebranch => $branch,
760 holdingbranch => $branch,
765 my $ten_days_before = dt_from_string->add( days => -10 );
766 my $ten_days_ahead = dt_from_string->add( days => 10 );
767 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
768 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
769 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
770 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' );
771 my $five_days_before = dt_from_string->add( days => -5 );
772 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
773 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
774 is( $latest_auto_renew_date->truncate( to => 'minute' ),
775 $five_days_before->truncate( to => 'minute' ),
776 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
778 my $five_days_ahead = dt_from_string->add( days => 5 );
779 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
780 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
781 is( $latest_auto_renew_date->truncate( to => 'minute' ),
782 $five_days_ahead->truncate( to => 'minute' ),
783 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
785 my $two_days_ahead = dt_from_string->add( days => 2 );
786 $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 ) );
787 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
788 is( $latest_auto_renew_date->truncate( to => 'day' ),
789 $two_days_ahead->truncate( to => 'day' ),
790 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
792 $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 ) );
793 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
794 is( $latest_auto_renew_date->truncate( to => 'day' ),
795 $two_days_ahead->truncate( to => 'day' ),
796 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
803 # set policy to forbid renewals
804 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
806 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
807 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
808 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
810 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
811 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
812 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
814 C4::Overdues::UpdateFine(
816 issue_id => $issue->id(),
817 itemnumber => $itemnumber,
818 borrowernumber => $renewing_borrower->{borrowernumber},
821 due => Koha::DateUtils::output_pref($datedue)
825 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
826 is( $line->accounttype, 'FU', 'Account line type is FU' );
827 is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
828 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
829 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
830 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
832 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
833 is( $offset->type, 'Fine', 'Account offset type is Fine' );
834 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
836 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
837 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
839 LostItem( $itemnumber, 'test', 1 );
841 $line = Koha::Account::Lines->find($line->id);
842 is( $line->accounttype, 'F', 'Account type correctly changed from FU to F' );
844 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
845 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
846 my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
847 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
849 my $total_due = $dbh->selectrow_array(
850 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
851 undef, $renewing_borrower->{borrowernumber}
854 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
856 C4::Context->dbh->do("DELETE FROM accountlines");
858 C4::Overdues::UpdateFine(
860 issue_id => $issue2->id(),
861 itemnumber => $itemnumber2,
862 borrowernumber => $renewing_borrower->{borrowernumber},
865 due => Koha::DateUtils::output_pref($datedue)
869 LostItem( $itemnumber2, 'test', 0 );
871 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
872 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
873 ok( Koha::Checkouts->find({ itemnumber => $itemnumber2 }), 'LostItem called without forced return has checked in the item' );
875 $total_due = $dbh->selectrow_array(
876 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
877 undef, $renewing_borrower->{borrowernumber}
880 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
882 my $future = dt_from_string();
883 $future->add( days => 7 );
884 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
885 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
887 # Users cannot renew any item if there is an overdue item
888 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
889 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
890 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
891 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
892 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
894 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
895 $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber3 } );
896 LostItem( $itemnumber3, 'test', 0 );
897 my $accountline = Koha::Account::Lines->find( { itemnumber => $itemnumber3 } );
898 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
903 # GetUpcomingDueIssues tests
904 my $barcode = 'R00000342';
905 my $barcode2 = 'R00000343';
906 my $barcode3 = 'R00000344';
907 my $branch = $library2->{branchcode};
909 #Create another record
910 my $title2 = 'Something is worng here';
911 my ($biblionumber2, $biblioitemnumber2) = add_biblio($title2, 'Anonymous');
916 homebranch => $branch,
917 holdingbranch => $branch,
918 barcode => $barcode3,
925 my %a_borrower_data = (
926 firstname => 'Fridolyn',
928 categorycode => $patron_category->{categorycode},
929 branchcode => $branch,
932 my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
933 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
935 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
936 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
937 my $today = DateTime->today(time_zone => C4::Context->tz());
939 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
940 my $datedue = dt_from_string( $issue->date_due() );
941 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
942 my $datedue2 = dt_from_string( $issue->date_due() );
946 # GetUpcomingDueIssues tests
948 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
949 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
952 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
953 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
954 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
957 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
958 is ( scalar( @$upcoming_dues ), 1,
959 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
962 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
964 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
966 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
967 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
969 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
970 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
972 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
973 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
975 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
976 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
978 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
979 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
981 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
982 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
987 my $barcode = '1234567890';
988 my $branch = $library2->{branchcode};
990 my ($biblionumber, $biblioitemnumber) = add_biblio();
993 my ( undef, undef, $itemnumber ) = AddItem(
995 homebranch => $branch,
996 holdingbranch => $branch,
1004 my %a_borrower_data = (
1005 firstname => 'Kyle',
1007 categorycode => $patron_category->{categorycode},
1008 branchcode => $branch,
1011 my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1013 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1014 my $issue = AddIssue( $borrower, $barcode );
1017 issue_id => $issue->id(),
1018 itemnumber => $itemnumber,
1019 borrowernumber => $borrowernumber,
1025 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
1026 my $count = $hr->{count};
1028 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1032 $dbh->do('DELETE FROM issues');
1033 $dbh->do('DELETE FROM items');
1034 $dbh->do('DELETE FROM issuingrules');
1037 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
1038 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1047 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
1049 my $barcode1 = '1234';
1050 my ( undef, undef, $itemnumber1 ) = AddItem(
1052 homebranch => $library2->{branchcode},
1053 holdingbranch => $library2->{branchcode},
1054 barcode => $barcode1,
1059 my $barcode2 = '4321';
1060 my ( undef, undef, $itemnumber2 ) = AddItem(
1062 homebranch => $library2->{branchcode},
1063 holdingbranch => $library2->{branchcode},
1064 barcode => $barcode2,
1070 my $borrowernumber1 = Koha::Patron->new({
1071 firstname => 'Kyle',
1073 categorycode => $patron_category->{categorycode},
1074 branchcode => $library2->{branchcode},
1075 })->store->borrowernumber;
1076 my $borrowernumber2 = Koha::Patron->new({
1077 firstname => 'Chelsea',
1079 categorycode => $patron_category->{categorycode},
1080 branchcode => $library2->{branchcode},
1081 })->store->borrowernumber;
1083 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1084 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1086 my $issue = AddIssue( $borrower1, $barcode1 );
1088 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1089 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1092 $library2->{branchcode}, $borrowernumber2, $biblionumber,
1093 '', 1, undef, undef, '',
1097 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1098 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1099 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1100 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1102 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1103 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1104 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1105 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1107 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1108 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1109 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1110 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1112 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1113 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1114 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1115 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1117 # Setting item not checked out to be not for loan but holdable
1118 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
1120 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
1121 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' );
1125 # Don't allow renewing onsite checkout
1126 my $barcode = 'R00000XXX';
1127 my $branch = $library->{branchcode};
1129 #Create another record
1130 my ($biblionumber, $biblioitemnumber) = add_biblio('A title', 'Anonymous');
1132 my (undef, undef, $itemnumber) = AddItem(
1134 homebranch => $branch,
1135 holdingbranch => $branch,
1136 barcode => $barcode,
1142 my $borrowernumber = Koha::Patron->new({
1145 categorycode => $patron_category->{categorycode},
1146 branchcode => $branch,
1147 })->store->borrowernumber;
1149 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1151 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1152 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
1153 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1154 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1158 my $library = $builder->build({ source => 'Branch' });
1160 my ($biblionumber, $biblioitemnumber) = add_biblio();
1162 my $barcode = 'just a barcode';
1163 my ( undef, undef, $itemnumber ) = AddItem(
1165 homebranch => $library->{branchcode},
1166 holdingbranch => $library->{branchcode},
1167 barcode => $barcode,
1173 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1175 my $issue = AddIssue( $patron, $barcode );
1178 issue_id => $issue->id(),
1179 itemnumber => $itemnumber,
1180 borrowernumber => $patron->{borrowernumber},
1187 issue_id => $issue->id(),
1188 itemnumber => $itemnumber,
1189 borrowernumber => $patron->{borrowernumber},
1194 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1197 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1200 my $homebranch = $builder->build( { source => 'Branch' } );
1201 my $holdingbranch = $builder->build( { source => 'Branch' } );
1202 my $otherbranch = $builder->build( { source => 'Branch' } );
1203 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1204 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1206 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1207 my $item = $builder->build(
1210 homebranch => $homebranch->{branchcode},
1211 holdingbranch => $holdingbranch->{branchcode},
1212 biblionumber => $biblioitem->{biblionumber}
1217 set_userenv($holdingbranch);
1219 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1220 is( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Checkout
1222 my ( $error, $question, $alerts );
1224 # AllowReturnToBranch == anywhere
1225 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1226 ## Test that unknown barcodes don't generate internal server errors
1227 set_userenv($homebranch);
1228 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1229 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1230 ## Can be issued from homebranch
1231 set_userenv($homebranch);
1232 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1233 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1234 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1235 ## Can be issued from holdingbranch
1236 set_userenv($holdingbranch);
1237 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1238 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1239 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1240 ## Can be issued from another branch
1241 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1242 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1243 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1245 # AllowReturnToBranch == holdingbranch
1246 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1247 ## Cannot be issued from homebranch
1248 set_userenv($homebranch);
1249 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1250 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1251 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1252 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1253 ## Can be issued from holdinbranch
1254 set_userenv($holdingbranch);
1255 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1256 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1257 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1258 ## Cannot be issued from another branch
1259 set_userenv($otherbranch);
1260 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1261 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1262 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1263 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1265 # AllowReturnToBranch == homebranch
1266 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1267 ## Can be issued from holdinbranch
1268 set_userenv($homebranch);
1269 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1270 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1271 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1272 ## Cannot be issued from holdinbranch
1273 set_userenv($holdingbranch);
1274 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1275 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1276 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1277 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1278 ## Cannot be issued from holdinbranch
1279 set_userenv($otherbranch);
1280 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1281 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1282 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1283 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1285 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1288 subtest 'AddIssue & AllowReturnToBranch' => sub {
1291 my $homebranch = $builder->build( { source => 'Branch' } );
1292 my $holdingbranch = $builder->build( { source => 'Branch' } );
1293 my $otherbranch = $builder->build( { source => 'Branch' } );
1294 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1295 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1297 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1298 my $item = $builder->build(
1301 homebranch => $homebranch->{branchcode},
1302 holdingbranch => $holdingbranch->{branchcode},
1306 biblionumber => $biblioitem->{biblionumber}
1311 set_userenv($holdingbranch);
1313 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Checkout
1314 my $issue = AddIssue( $patron_1, $item->{barcode} );
1316 my ( $error, $question, $alerts );
1318 # AllowReturnToBranch == homebranch
1319 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1320 ## Can be issued from homebranch
1321 set_userenv($homebranch);
1322 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1323 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1324 ## Can be issued from holdinbranch
1325 set_userenv($holdingbranch);
1326 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1327 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1328 ## Can be issued from another branch
1329 set_userenv($otherbranch);
1330 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1331 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1333 # AllowReturnToBranch == holdinbranch
1334 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1335 ## Cannot be issued from homebranch
1336 set_userenv($homebranch);
1337 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1338 ## Can be issued from holdingbranch
1339 set_userenv($holdingbranch);
1340 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1341 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1342 ## Cannot be issued from another branch
1343 set_userenv($otherbranch);
1344 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1346 # AllowReturnToBranch == homebranch
1347 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1348 ## Can be issued from homebranch
1349 set_userenv($homebranch);
1350 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1351 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1352 ## Cannot be issued from holdinbranch
1353 set_userenv($holdingbranch);
1354 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1355 ## Cannot be issued from another branch
1356 set_userenv($otherbranch);
1357 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1358 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1361 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1364 my $library = $builder->build( { source => 'Branch' } );
1365 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1367 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1368 my $item_1 = $builder->build(
1371 homebranch => $library->{branchcode},
1372 holdingbranch => $library->{branchcode},
1373 biblionumber => $biblioitem_1->{biblionumber}
1377 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1378 my $item_2 = $builder->build(
1381 homebranch => $library->{branchcode},
1382 holdingbranch => $library->{branchcode},
1383 biblionumber => $biblioitem_2->{biblionumber}
1388 my ( $error, $question, $alerts );
1390 # Patron cannot issue item_1, they have overdues
1391 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1392 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1394 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1395 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1396 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1397 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1399 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1400 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1401 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1402 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1404 # Patron cannot issue item_1, they are debarred
1405 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1406 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1407 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1408 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1409 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1411 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1412 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1413 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1414 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1417 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1420 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1421 my $patron_category_x = $builder->build_object(
1423 class => 'Koha::Patron::Categories',
1424 value => { category_type => 'X' }
1427 my $patron = $builder->build_object(
1429 class => 'Koha::Patrons',
1431 categorycode => $patron_category_x->categorycode,
1432 gonenoaddress => undef,
1439 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1440 my $item_1 = $builder->build(
1444 homebranch => $library->branchcode,
1445 holdingbranch => $library->branchcode,
1446 biblionumber => $biblioitem_1->{biblionumber}
1451 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1452 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1454 # TODO There are other tests to provide here
1457 subtest 'MultipleReserves' => sub {
1460 my $title = 'Silence in the library';
1461 my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
1463 my $branch = $library2->{branchcode};
1465 my $barcode1 = 'R00110001';
1466 my ( $item_bibnum1, $item_bibitemnum1, $itemnumber1 ) = AddItem(
1468 homebranch => $branch,
1469 holdingbranch => $branch,
1470 barcode => $barcode1,
1471 replacementprice => 12.00,
1477 my $barcode2 = 'R00110002';
1478 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
1480 homebranch => $branch,
1481 holdingbranch => $branch,
1482 barcode => $barcode2,
1483 replacementprice => 12.00,
1491 my $resdate = undef;
1492 my $expdate = undef;
1494 my $checkitem = undef;
1497 my %renewing_borrower_data = (
1498 firstname => 'John',
1499 surname => 'Renewal',
1500 categorycode => $patron_category->{categorycode},
1501 branchcode => $branch,
1503 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
1504 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1505 my $issue = AddIssue( $renewing_borrower, $barcode1);
1506 my $datedue = dt_from_string( $issue->date_due() );
1507 is (defined $issue->date_due(), 1, "item 1 checked out");
1508 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $itemnumber1 })->borrowernumber;
1510 my %reserving_borrower_data1 = (
1511 firstname => 'Katrin',
1512 surname => 'Reservation',
1513 categorycode => $patron_category->{categorycode},
1514 branchcode => $branch,
1516 my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
1518 $branch, $reserving_borrowernumber1, $biblionumber,
1519 $bibitems, $priority, $resdate, $expdate, $notes,
1520 $title, $checkitem, $found
1523 my %reserving_borrower_data2 = (
1524 firstname => 'Kirk',
1525 surname => 'Reservation',
1526 categorycode => $patron_category->{categorycode},
1527 branchcode => $branch,
1529 my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
1531 $branch, $reserving_borrowernumber2, $biblionumber,
1532 $bibitems, $priority, $resdate, $expdate, $notes,
1533 $title, $checkitem, $found
1537 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1538 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1541 my $barcode3 = 'R00110003';
1542 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
1544 homebranch => $branch,
1545 holdingbranch => $branch,
1546 barcode => $barcode3,
1547 replacementprice => 12.00,
1554 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber1, 1);
1555 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1559 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1562 my $library = $builder->build( { source => 'Branch' } );
1563 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1565 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1566 my $biblionumber = $biblioitem->{biblionumber};
1567 my $item_1 = $builder->build(
1570 homebranch => $library->{branchcode},
1571 holdingbranch => $library->{branchcode},
1572 biblionumber => $biblionumber,
1576 my $item_2 = $builder->build(
1579 homebranch => $library->{branchcode},
1580 holdingbranch => $library->{branchcode},
1581 biblionumber => $biblionumber,
1586 my ( $error, $question, $alerts );
1587 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1589 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1590 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1591 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1592 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) );
1594 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1595 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1596 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1598 # Add a subscription
1599 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1601 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1602 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1603 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) );
1605 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1606 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1607 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) );
1610 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1613 my $library = $builder->build( { source => 'Branch' } );
1614 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1617 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1618 my $item_1 = $builder->build(
1622 homebranch => $library->{branchcode},
1623 holdingbranch => $library->{branchcode},
1627 biblionumber => $biblioitem_1->{biblionumber}
1631 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1632 my $item_2 = $builder->build(
1636 homebranch => $library->{branchcode},
1637 holdingbranch => $library->{branchcode},
1641 biblionumber => $biblioitem_2->{biblionumber}
1646 # And the issuing rule
1647 Koha::IssuingRules->search->delete;
1648 my $rule = Koha::IssuingRule->new(
1650 categorycode => '*',
1655 firstremind => 1, # 1 day of grace
1656 finedays => 2, # 2 days of fine per day of overdue
1657 lengthunit => 'days',
1662 # Patron cannot issue item_1, they have overdues
1663 my $five_days_ago = dt_from_string->subtract( days => 5 );
1664 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1665 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1666 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1667 ; # Add another overdue
1669 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1670 AddReturn( $item_1->{barcode}, $library->{branchcode},
1671 undef, undef, dt_from_string );
1672 my $debarments = Koha::Patron::Debarments::GetDebarments(
1673 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1674 is( scalar(@$debarments), 1 );
1676 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1677 # Same for the others
1678 my $expected_expiration = output_pref(
1680 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1681 dateformat => 'sql',
1685 is( $debarments->[0]->{expiration}, $expected_expiration );
1687 AddReturn( $item_2->{barcode}, $library->{branchcode},
1688 undef, undef, dt_from_string );
1689 $debarments = Koha::Patron::Debarments::GetDebarments(
1690 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1691 is( scalar(@$debarments), 1 );
1692 $expected_expiration = output_pref(
1694 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1695 dateformat => 'sql',
1699 is( $debarments->[0]->{expiration}, $expected_expiration );
1701 Koha::Patron::Debarments::DelUniqueDebarment(
1702 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1704 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1705 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1706 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1707 ; # Add another overdue
1708 AddReturn( $item_1->{barcode}, $library->{branchcode},
1709 undef, undef, dt_from_string );
1710 $debarments = Koha::Patron::Debarments::GetDebarments(
1711 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1712 is( scalar(@$debarments), 1 );
1713 $expected_expiration = output_pref(
1715 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1716 dateformat => 'sql',
1720 is( $debarments->[0]->{expiration}, $expected_expiration );
1722 AddReturn( $item_2->{barcode}, $library->{branchcode},
1723 undef, undef, dt_from_string );
1724 $debarments = Koha::Patron::Debarments::GetDebarments(
1725 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1726 is( scalar(@$debarments), 1 );
1727 $expected_expiration = output_pref(
1729 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1730 dateformat => 'sql',
1734 is( $debarments->[0]->{expiration}, $expected_expiration );
1737 subtest 'AddReturn + suspension_chargeperiod' => sub {
1740 my $library = $builder->build( { source => 'Branch' } );
1741 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1744 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1745 my $item_1 = $builder->build(
1749 homebranch => $library->{branchcode},
1750 holdingbranch => $library->{branchcode},
1754 biblionumber => $biblioitem_1->{biblionumber}
1759 # And the issuing rule
1760 Koha::IssuingRules->search->delete;
1761 my $rule = Koha::IssuingRule->new(
1763 categorycode => '*',
1768 firstremind => 0, # 0 day of grace
1769 finedays => 2, # 2 days of fine per day of overdue
1770 suspension_chargeperiod => 1,
1771 lengthunit => 'days',
1776 my $five_days_ago = dt_from_string->subtract( days => 5 );
1777 # We want to charge 2 days every day, without grace
1778 # With 5 days of overdue: 5 * Z
1779 my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1780 test_debarment_on_checkout(
1783 library => $library,
1785 due_date => $five_days_ago,
1786 expiration_date => $expected_expiration,
1790 # We want to charge 2 days every 2 days, without grace
1791 # With 5 days of overdue: (5 * 2) / 2
1792 $rule->suspension_chargeperiod(2)->store;
1793 $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1794 test_debarment_on_checkout(
1797 library => $library,
1799 due_date => $five_days_ago,
1800 expiration_date => $expected_expiration,
1804 # We want to charge 2 days every 3 days, with 1 day of grace
1805 # With 5 days of overdue: ((5-1) / 3 ) * 2
1806 $rule->suspension_chargeperiod(3)->store;
1807 $rule->firstremind(1)->store;
1808 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1809 test_debarment_on_checkout(
1812 library => $library,
1814 due_date => $five_days_ago,
1815 expiration_date => $expected_expiration,
1819 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1820 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1821 $rule->finedays(2)->store;
1822 $rule->suspension_chargeperiod(1)->store;
1823 $rule->firstremind(0)->store;
1824 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1826 # Adding a holiday 2 days ago
1827 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1828 my $two_days_ago = dt_from_string->subtract( days => 2 );
1829 $calendar->insert_single_holiday(
1830 day => $two_days_ago->day,
1831 month => $two_days_ago->month,
1832 year => $two_days_ago->year,
1833 title => 'holidayTest-2d',
1834 description => 'holidayDesc 2 days ago'
1836 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1837 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1838 test_debarment_on_checkout(
1841 library => $library,
1843 due_date => $five_days_ago,
1844 expiration_date => $expected_expiration,
1848 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1849 my $two_days_ahead = dt_from_string->add( days => 2 );
1850 $calendar->insert_single_holiday(
1851 day => $two_days_ahead->day,
1852 month => $two_days_ahead->month,
1853 year => $two_days_ahead->year,
1854 title => 'holidayTest+2d',
1855 description => 'holidayDesc 2 days ahead'
1858 # Same as above, but we should skip D+2
1859 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1860 test_debarment_on_checkout(
1863 library => $library,
1865 due_date => $five_days_ago,
1866 expiration_date => $expected_expiration,
1870 # Adding another holiday, day of expiration date
1871 my $expected_expiration_dt = dt_from_string($expected_expiration);
1872 $calendar->insert_single_holiday(
1873 day => $expected_expiration_dt->day,
1874 month => $expected_expiration_dt->month,
1875 year => $expected_expiration_dt->year,
1876 title => 'holidayTest_exp',
1877 description => 'holidayDesc on expiration date'
1879 # Expiration date will be the day after
1880 test_debarment_on_checkout(
1883 library => $library,
1885 due_date => $five_days_ago,
1886 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1890 test_debarment_on_checkout(
1893 library => $library,
1895 return_date => dt_from_string->add(days => 5),
1896 expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1901 subtest 'AddReturn | is_overdue' => sub {
1904 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
1905 t::lib::Mocks::mock_preference('finesMode', 'production');
1906 t::lib::Mocks::mock_preference('MaxFine', '100');
1908 my $library = $builder->build( { source => 'Branch' } );
1909 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1911 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1912 my $item = $builder->build(
1916 homebranch => $library->{branchcode},
1917 holdingbranch => $library->{branchcode},
1921 biblionumber => $biblioitem->{biblionumber},
1926 Koha::IssuingRules->search->delete;
1927 my $rule = Koha::IssuingRule->new(
1929 categorycode => '*',
1934 lengthunit => 'days',
1935 fine => 1, # Charge 1 every day of overdue
1941 my $one_day_ago = dt_from_string->subtract( days => 1 );
1942 my $five_days_ago = dt_from_string->subtract( days => 5 );
1943 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1944 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1946 # No date specify, today will be used
1947 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1948 AddReturn( $item->{barcode}, $library->{branchcode} );
1949 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1950 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1952 # specify return date 5 days before => no overdue
1953 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1954 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $ten_days_ago );
1955 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1956 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1958 # specify return date 5 days later => overdue
1959 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1960 AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $five_days_ago );
1961 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1962 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1964 # specify dropbox date 5 days before => no overdue
1965 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1966 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $ten_days_ago );
1967 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1968 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1970 # specify dropbox date 5 days later => overdue, or... not
1971 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1972 AddReturn( $item->{barcode}, $library->{branchcode}, undef, 1, undef, $five_days_ago );
1973 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
1974 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1977 subtest '_FixAccountForLostAndReturned' => sub {
1981 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1982 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
1984 my $processfee_amount = 20;
1985 my $replacement_amount = 99.00;
1986 my $item_type = $builder->build_object(
1987 { class => 'Koha::ItemTypes',
1989 notforloan => undef,
1991 defaultreplacecost => undef,
1992 processfee => $processfee_amount
1996 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1998 # Generate test biblio
1999 my $title = 'Koha for Dummies';
2000 my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, 'Hall, Daria' );
2002 subtest 'Full write-off tests' => sub {
2006 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2007 my $barcode = 'KD123456789';
2009 my ( undef, undef, $item_id ) = AddItem(
2010 { homebranch => $library->branchcode,
2011 holdingbranch => $library->branchcode,
2012 barcode => $barcode,
2013 replacementprice => $replacement_amount,
2014 itype => $item_type->itemtype
2019 AddIssue( $patron->unblessed, $barcode );
2021 # Simulate item marked as lost
2022 ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2023 LostItem( $item_id, 1 );
2025 my $processing_fee_lines = Koha::Account::Lines->search(
2026 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2027 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2028 my $processing_fee_line = $processing_fee_lines->next;
2029 is( $processing_fee_line->amount + 0,
2030 $processfee_amount, 'The right PF amount is generated' );
2031 is( $processing_fee_line->amountoutstanding + 0,
2032 $processfee_amount, 'The right PF amountoutstanding is generated' );
2034 my $lost_fee_lines = Koha::Account::Lines->search(
2035 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2036 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2037 my $lost_fee_line = $lost_fee_lines->next;
2038 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2039 is( $lost_fee_line->amountoutstanding + 0,
2040 $replacement_amount, 'The right L amountoutstanding is generated' );
2042 my $account = $patron->account;
2043 my $debts = $account->outstanding_debits;
2045 # Write off the debt
2046 my $credit = $account->add_credit(
2047 { amount => $account->balance,
2051 $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2053 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2054 is( $credit_return_id, undef, 'No CR account line added' );
2056 $lost_fee_line->discard_changes; # reload from DB
2057 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2058 is( $lost_fee_line->accounttype,
2059 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2061 is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2064 subtest 'Full payment tests' => sub {
2068 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2069 my $barcode = 'KD123456790';
2071 my ( undef, undef, $item_id ) = AddItem(
2072 { homebranch => $library->branchcode,
2073 holdingbranch => $library->branchcode,
2074 barcode => $barcode,
2075 replacementprice => $replacement_amount,
2076 itype => $item_type->itemtype
2081 AddIssue( $patron->unblessed, $barcode );
2083 # Simulate item marked as lost
2084 ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2085 LostItem( $item_id, 1 );
2087 my $processing_fee_lines = Koha::Account::Lines->search(
2088 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2089 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2090 my $processing_fee_line = $processing_fee_lines->next;
2091 is( $processing_fee_line->amount + 0,
2092 $processfee_amount, 'The right PF amount is generated' );
2093 is( $processing_fee_line->amountoutstanding + 0,
2094 $processfee_amount, 'The right PF amountoutstanding is generated' );
2096 my $lost_fee_lines = Koha::Account::Lines->search(
2097 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2098 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2099 my $lost_fee_line = $lost_fee_lines->next;
2100 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2101 is( $lost_fee_line->amountoutstanding + 0,
2102 $replacement_amount, 'The right L amountountstanding is generated' );
2104 my $account = $patron->account;
2105 my $debts = $account->outstanding_debits;
2107 # Write off the debt
2108 my $credit = $account->add_credit(
2109 { amount => $account->balance,
2113 $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2115 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2116 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2118 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2119 is( $credit_return->amount + 0,
2120 -99.00, 'The account line of type CR has an amount of -99' );
2121 is( $credit_return->amountoutstanding + 0,
2122 -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2124 $lost_fee_line->discard_changes;
2125 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2126 is( $lost_fee_line->accounttype,
2127 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2129 is( $patron->account->balance,
2130 -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2133 subtest 'Test without payment or write off' => sub {
2137 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2138 my $barcode = 'KD123456791';
2140 my ( undef, undef, $item_id ) = AddItem(
2141 { homebranch => $library->branchcode,
2142 holdingbranch => $library->branchcode,
2143 barcode => $barcode,
2144 replacementprice => $replacement_amount,
2145 itype => $item_type->itemtype
2150 AddIssue( $patron->unblessed, $barcode );
2152 # Simulate item marked as lost
2153 ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2154 LostItem( $item_id, 1 );
2156 my $processing_fee_lines = Koha::Account::Lines->search(
2157 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2158 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2159 my $processing_fee_line = $processing_fee_lines->next;
2160 is( $processing_fee_line->amount + 0,
2161 $processfee_amount, 'The right PF amount is generated' );
2162 is( $processing_fee_line->amountoutstanding + 0,
2163 $processfee_amount, 'The right PF amountoutstanding is generated' );
2165 my $lost_fee_lines = Koha::Account::Lines->search(
2166 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2167 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2168 my $lost_fee_line = $lost_fee_lines->next;
2169 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2170 is( $lost_fee_line->amountoutstanding + 0,
2171 $replacement_amount, 'The right L amountountstanding is generated' );
2173 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2174 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2176 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2177 is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2178 is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2180 $lost_fee_line->discard_changes;
2181 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2182 is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2184 is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2187 subtest 'Test with partial payement and write off, and remaining debt' => sub {
2191 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2192 my $barcode = 'KD123456792';
2194 my ( undef, undef, $item_id ) = AddItem(
2195 { homebranch => $library->branchcode,
2196 holdingbranch => $library->branchcode,
2197 barcode => $barcode,
2198 replacementprice => $replacement_amount,
2199 itype => $item_type->itemtype
2204 AddIssue( $patron->unblessed, $barcode );
2206 # Simulate item marked as lost
2207 ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2208 LostItem( $item_id, 1 );
2210 my $processing_fee_lines = Koha::Account::Lines->search(
2211 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2212 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2213 my $processing_fee_line = $processing_fee_lines->next;
2214 is( $processing_fee_line->amount + 0,
2215 $processfee_amount, 'The right PF amount is generated' );
2216 is( $processing_fee_line->amountoutstanding + 0,
2217 $processfee_amount, 'The right PF amountoutstanding is generated' );
2219 my $lost_fee_lines = Koha::Account::Lines->search(
2220 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2221 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2222 my $lost_fee_line = $lost_fee_lines->next;
2223 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2224 is( $lost_fee_line->amountoutstanding + 0,
2225 $replacement_amount, 'The right L amountountstanding is generated' );
2227 my $account = $patron->account;
2228 is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2231 my $payment_amount = 27;
2232 my $payment = $account->add_credit(
2233 { amount => $payment_amount,
2238 $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } );
2240 # Partially write off fee
2241 my $write_off_amount = 25;
2242 my $write_off = $account->add_credit(
2243 { amount => $write_off_amount,
2247 $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
2249 is( $account->balance,
2250 $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2251 'Payment and write off applied'
2254 # Store the amountoutstanding value
2255 $lost_fee_line->discard_changes;
2256 my $outstanding = $lost_fee_line->amountoutstanding;
2258 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2259 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2261 is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2263 $lost_fee_line->discard_changes;
2264 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2265 is( $lost_fee_line->accounttype,
2266 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2268 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2269 is( $credit_return->amount + 0,
2270 ($payment_amount + $outstanding ) * -1,
2271 'The account line of type CR has an amount equal to the payment + outstanding'
2273 is( $credit_return->amountoutstanding + 0,
2274 $payment_amount * -1,
2275 'The account line of type CR has an amountoutstanding equal to the payment'
2278 is( $account->balance,
2279 $processfee_amount - $payment_amount,
2280 'The patron balance is the difference between the PF and the credit'
2285 subtest '_FixOverduesOnReturn' => sub {
2288 # Generate test biblio
2289 my $title = 'Koha for Dummies';
2290 my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Kylie');
2292 my $barcode = 'KD987654321';
2293 my $branchcode = $library2->{branchcode};
2295 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
2297 homebranch => $branchcode,
2298 holdingbranch => $branchcode,
2299 barcode => $barcode,
2300 replacementprice => 99.00,
2306 my $patron = $builder->build( { source => 'Borrower' } );
2308 ## Start with basic call, should just close out the open fine
2309 my $accountline = Koha::Account::Line->new(
2311 borrowernumber => $patron->{borrowernumber},
2312 accounttype => 'FU',
2313 itemnumber => $itemnumber,
2315 amountoutstanding => 99.00,
2316 lastincrement => 9.00,
2320 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber );
2322 $accountline->_result()->discard_changes();
2324 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2325 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2328 ## Run again, with exemptfine enabled
2331 accounttype => 'FU',
2332 amountoutstanding => 99.00,
2336 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 1 );
2338 $accountline->_result()->discard_changes();
2339 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2341 is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2342 is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2343 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2344 is( $offset->amount, '-99.000000', "Amount of offset is correct" );
2346 ## Run again, with dropbox mode enabled
2349 accounttype => 'FU',
2350 amountoutstanding => 99.00,
2354 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $itemnumber, 0, 1 );
2356 $accountline->_result()->discard_changes();
2357 $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Dropbox' })->next();
2359 is( $accountline->amountoutstanding + 0, 90, 'Fine has been reduced to 90' );
2360 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2361 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via dropbox" );
2362 is( $offset->amount, '-9.000000', "Amount of offset is correct" );
2365 subtest 'Set waiting flag' => sub {
2368 my $library_1 = $builder->build( { source => 'Branch' } );
2369 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2370 my $library_2 = $builder->build( { source => 'Branch' } );
2371 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2373 my $biblio = $builder->build( { source => 'Biblio' } );
2374 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2376 my $item = $builder->build(
2380 homebranch => $library_1->{branchcode},
2381 holdingbranch => $library_1->{branchcode},
2385 biblionumber => $biblioitem->{biblionumber},
2390 set_userenv( $library_2 );
2391 my $reserve_id = AddReserve(
2392 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2393 '', 1, undef, undef, '', undef, $item->{itemnumber},
2396 set_userenv( $library_1 );
2397 my $do_transfer = 1;
2398 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2399 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2400 my $hold = Koha::Holds->find( $reserve_id );
2401 is( $hold->found, 'T', 'Hold is in transit' );
2403 my ( $status ) = CheckReserves($item->{itemnumber});
2404 is( $status, 'Reserved', 'Hold is not waiting yet');
2406 set_userenv( $library_2 );
2408 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2409 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2410 $hold = Koha::Holds->find( $reserve_id );
2411 is( $hold->found, 'W', 'Hold is waiting' );
2412 ( $status ) = CheckReserves($item->{itemnumber});
2413 is( $status, 'Waiting', 'Now the hold is waiting');
2416 subtest 'Cancel transfers on lost items' => sub {
2418 my $library_1 = $builder->build( { source => 'Branch' } );
2419 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2420 my $library_2 = $builder->build( { source => 'Branch' } );
2421 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2422 my $biblio = $builder->build( { source => 'Biblio' } );
2423 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2424 my $item = $builder->build(
2428 homebranch => $library_1->{branchcode},
2429 holdingbranch => $library_1->{branchcode},
2433 biblionumber => $biblioitem->{biblionumber},
2438 set_userenv( $library_2 );
2439 my $reserve_id = AddReserve(
2440 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber}, '', 1, undef, undef, '', undef, $item->{itemnumber},
2443 #Return book and add transfer
2444 set_userenv( $library_1 );
2445 my $do_transfer = 1;
2446 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2447 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2448 C4::Circulation::transferbook( $library_2->{branchcode}, $item->{barcode} );
2449 my $hold = Koha::Holds->find( $reserve_id );
2450 is( $hold->found, 'T', 'Hold is in transit' );
2452 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2453 my ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2454 is( $tobranch, $library_2->{branchcode}, 'The transfer record exists in the branchtransfers table');
2455 my $itemcheck = GetItem($item->{itemnumber});
2456 is( $itemcheck->{holdingbranch}, $library_2->{branchcode}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2458 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2459 ModItem( { itemlost => 1 }, $biblio->{biblionumber}, $item->{itemnumber} );
2460 LostItem( $item->{itemnumber}, 'test', 1 );
2461 ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2462 is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2463 $itemcheck = GetItem($item->{itemnumber});
2464 is( $itemcheck->{holdingbranch}, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2467 subtest 'CanBookBeIssued | is_overdue' => sub {
2470 # Set a simple circ policy
2471 $dbh->do('DELETE FROM issuingrules');
2473 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2474 maxissueqty, issuelength, lengthunit,
2475 renewalsallowed, renewalperiod,
2476 norenewalbefore, auto_renew,
2493 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2494 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2495 my $library = $builder->build( { source => 'Branch' } );
2496 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2498 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2499 my $item = $builder->build(
2503 homebranch => $library->{branchcode},
2504 holdingbranch => $library->{branchcode},
2508 biblionumber => $biblioitem->{biblionumber},
2513 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2514 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2515 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2516 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2517 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2518 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2521 subtest 'ItemsDeniedRenewal preference' => sub {
2524 C4::Context->set_preference('ItemsDeniedRenewal','');
2526 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2529 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
2530 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2533 '*', $idr_lib->branchcode, '*', 25,
2540 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2541 homebranch => $idr_lib->branchcode,
2545 itemcallnumber => undef,
2549 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2550 homebranch => $idr_lib->branchcode,
2553 location => 'NOPROC'
2557 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2558 branchcode => $idr_lib->branchcode,
2561 my $future = dt_from_string->add( days => 1 );
2562 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2563 returndate => undef,
2566 borrowernumber => $idr_borrower->borrowernumber,
2567 itemnumber => $deny_book->itemnumber,
2568 onsite_checkout => 0,
2569 date_due => $future,
2572 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2573 returndate => undef,
2576 borrowernumber => $idr_borrower->borrowernumber,
2577 itemnumber => $allow_book->itemnumber,
2578 onsite_checkout => 0,
2579 date_due => $future,
2585 my ( $idr_mayrenew, $idr_error ) =
2586 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2587 is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2588 is( $idr_error, undef, 'Renewal allowed when no rules' );
2590 $idr_rules="withdrawn: [1]";
2592 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2593 ( $idr_mayrenew, $idr_error ) =
2594 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2595 is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2596 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2597 ( $idr_mayrenew, $idr_error ) =
2598 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2599 is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2600 is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2602 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2604 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2605 ( $idr_mayrenew, $idr_error ) =
2606 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2607 is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2608 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2609 ( $idr_mayrenew, $idr_error ) =
2610 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2611 is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2612 is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2614 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2616 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2617 ( $idr_mayrenew, $idr_error ) =
2618 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2619 is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2620 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2621 ( $idr_mayrenew, $idr_error ) =
2622 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2623 is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2624 is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2626 $idr_rules="itemcallnumber: [NULL]";
2627 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2628 ( $idr_mayrenew, $idr_error ) =
2629 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2630 is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2631 $idr_rules="itemcallnumber: ['']";
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, 1, 'Renewal not blocked for undef when "" in pref' );
2637 $idr_rules="itemnotes: [NULL]";
2638 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2639 ( $idr_mayrenew, $idr_error ) =
2640 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2641 is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2642 $idr_rules="itemnotes: ['']";
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 empty string when "" in pref' );
2649 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2652 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2653 my $library = $builder->build( { source => 'Branch' } );
2654 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2656 my $itemtype = $builder->build(
2658 source => 'Itemtype',
2659 value => { notforloan => undef, }
2663 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2664 my $item = $builder->build_object(
2666 class => 'Koha::Items',
2668 homebranch => $library->{branchcode},
2669 holdingbranch => $library->{branchcode},
2673 biblionumber => $biblioitem->{biblionumber},
2674 biblioitemnumber => $biblioitem->{biblioitemnumber},
2679 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2680 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2681 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2684 subtest 'CanBookBeIssued | notforloan' => sub {
2687 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2689 my $library = $builder->build( { source => 'Branch' } );
2690 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2692 my $itemtype = $builder->build(
2694 source => 'Itemtype',
2695 value => { notforloan => undef, }
2699 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2700 my $item = $builder->build_object(
2702 class => 'Koha::Items',
2704 homebranch => $library->{branchcode},
2705 holdingbranch => $library->{branchcode},
2709 itype => $itemtype->{itemtype},
2710 biblionumber => $biblioitem->{biblionumber},
2711 biblioitemnumber => $biblioitem->{biblioitemnumber},
2716 my ( $issuingimpossible, $needsconfirmation );
2719 subtest 'item-level_itypes = 1' => sub {
2722 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2723 # Is for loan at item type and item level
2724 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2725 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2726 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2728 # not for loan at item type level
2729 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2730 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2731 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2734 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2735 'Item can not be issued, not for loan at item type level'
2738 # not for loan at item level
2739 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2740 $item->notforloan( 1 )->store;
2741 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2742 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2745 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2746 'Item can not be issued, not for loan at item type level'
2750 subtest 'item-level_itypes = 0' => sub {
2753 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2755 # We set another itemtype for biblioitem
2756 my $itemtype = $builder->build(
2758 source => 'Itemtype',
2759 value => { notforloan => undef, }
2763 # for loan at item type and item level
2764 $item->notforloan(0)->store;
2765 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
2766 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2767 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2768 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2770 # not for loan at item type level
2771 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2772 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2773 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2776 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2777 'Item can not be issued, not for loan at item type level'
2780 # not for loan at item level
2781 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2782 $item->notforloan( 1 )->store;
2783 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2784 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2787 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2788 'Item can not be issued, not for loan at item type level'
2792 # TODO test with AllowNotForLoanOverride = 1
2795 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
2798 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
2799 my $item = $builder->build_object({ class => 'Koha::Items', value => { onloan => '2018-01-01' }});
2800 AddReturn( $item->barcode, $item->homebranch );
2801 $item->discard_changes; # refresh
2802 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
2805 $schema->storage->txn_rollback;
2806 C4::Context->clear_syspref_cache();
2807 $cache->clear_from_cache('single_holidays');
2809 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
2813 $schema->storage->txn_begin;
2815 my $issuing_charges = 15;
2816 my $title = 'A title';
2817 my $author = 'Author, An';
2818 my $barcode = 'WHATARETHEODDS';
2820 my $circ = Test::MockModule->new('C4::Circulation');
2822 'GetIssuingCharges',
2824 return $issuing_charges;
2828 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2829 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
2830 my $patron = $builder->build_object({
2831 class => 'Koha::Patrons',
2832 value => { branchcode => $library->id }
2835 my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, $author );
2836 my ( undef, undef, $item_id ) = AddItem(
2838 homebranch => $library->id,
2839 holdingbranch => $library->id,
2840 barcode => $barcode,
2841 replacementprice => 23.00,
2842 itype => $itemtype->id
2846 my $item = Koha::Items->find( $item_id );
2848 my $items = Test::MockModule->new('C4::Items');
2849 $items->mock( GetItem => $item->unblessed );
2850 my $context = Test::MockModule->new('C4::Context');
2851 $context->mock( userenv => { branch => $library->id } );
2853 # Check the item out
2854 AddIssue( $patron->unblessed, $item->barcode );
2856 t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
2857 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
2858 my $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2859 AddRenewal( $patron->id, $item->id, $library->id );
2860 my $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2861 is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
2863 t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
2864 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
2865 $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2866 AddRenewal( $patron->id, $item->id, $library->id );
2867 $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2868 is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
2870 my $lines = Koha::Account::Lines->search({
2871 borrowernumber => $patron->id,
2872 itemnumber => $item->id
2875 is( $lines->count, 3 );
2877 my $line = $lines->next;
2878 is( $line->accounttype, 'Rent', 'The issuing charge generates an accountline' );
2879 is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
2880 is( $line->description, 'Rental', 'AddIssuingCharge set a hardcoded description for the accountline' );
2882 $line = $lines->next;
2883 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2884 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2885 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2887 $line = $lines->next;
2888 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2889 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2890 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2892 $schema->storage->txn_rollback;
2895 subtest 'ProcessOfflinePayment() tests' => sub {
2899 $schema->storage->txn_begin;
2903 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2904 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2905 my $result = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
2907 is( $result, 'Success.', 'The right string is returned' );
2909 my $lines = $patron->account->lines;
2910 is( $lines->count, 1, 'line created correctly');
2912 my $line = $lines->next;
2913 is( $line->amount+0, $amount * -1, 'amount picked from params' );
2914 is( $line->branchcode, $library->id, 'branchcode set correctly' );
2916 $schema->storage->txn_rollback;
2922 my ( $library ) = @_;
2923 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
2927 my ( $error, $question, $alert ) = @_;
2929 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
2930 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
2931 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
2936 my ($title, $author) = @_;
2938 my $marcflavour = C4::Context->preference('marcflavour');
2940 my $biblio = MARC::Record->new();
2942 my $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
2943 $biblio->append_fields(
2944 MARC::Field->new($tag, ' ', ' ', a => $title),
2949 my ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
2950 $biblio->append_fields(
2951 MARC::Field->new($tag, ' ', ' ', $code => $author),
2955 return AddBiblio($biblio, '');
2958 sub test_debarment_on_checkout {
2960 my $item = $params->{item};
2961 my $library = $params->{library};
2962 my $patron = $params->{patron};
2963 my $due_date = $params->{due_date} || dt_from_string;
2964 my $return_date = $params->{return_date} || dt_from_string;
2965 my $expected_expiration_date = $params->{expiration_date};
2967 $expected_expiration_date = output_pref(
2969 dt => $expected_expiration_date,
2970 dateformat => 'sql',
2974 my @caller = caller;
2975 my $line_number = $caller[2];
2976 AddIssue( $patron, $item->{barcode}, $due_date );
2978 my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode},
2979 undef, undef, $return_date );
2980 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
2981 or diag('AddReturn returned message ' . Dumper $message );
2982 my $debarments = Koha::Patron::Debarments::GetDebarments(
2983 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
2984 is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
2986 is( $debarments->[0]->{expiration},
2987 $expected_expiration_date, 'Test at line ' . $line_number );
2988 Koha::Patron::Debarments::DelUniqueDebarment(
2989 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );