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 => 126;
27 use POSIX qw( floor );
29 use t::lib::TestBuilder;
38 use C4::Overdues qw(UpdateFine CalcFine);
41 use Koha::IssuingRules;
45 use Koha::CirculationRules;
46 use Koha::Subscriptions;
47 use Koha::Account::Lines;
48 use Koha::Account::Offsets;
51 my $schema = Koha::Database->schema;
52 $schema->storage->txn_begin;
53 my $builder = t::lib::TestBuilder->new;
54 my $dbh = C4::Context->dbh;
57 $dbh->{RaiseError} = 1;
59 my $cache = Koha::Caches->get_instance();
60 $dbh->do(q|DELETE FROM special_holidays|);
61 $dbh->do(q|DELETE FROM repeatable_holidays|);
62 $cache->clear_from_cache('single_holidays');
64 # Start with a clean slate
65 $dbh->do('DELETE FROM issues');
66 $dbh->do('DELETE FROM borrowers');
68 my $library = $builder->build({
71 my $library2 = $builder->build({
74 my $itemtype = $builder->build(
80 rentalcharge_daily => 0,
81 defaultreplacecost => undef,
86 my $patron_category = $builder->build(
92 BlockExpiredPatronOpacActions => -1, # Pick the pref value
97 my $CircControl = C4::Context->preference('CircControl');
98 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
101 homebranch => $library2->{branchcode},
102 holdingbranch => $library2->{branchcode}
106 branchcode => $library2->{branchcode}
109 # No userenv, PickupLibrary
110 t::lib::Mocks::mock_preference('IndependentBranches', '0');
111 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
113 C4::Context->preference('CircControl'),
115 'CircControl changed to PickupLibrary'
118 C4::Circulation::_GetCircControlBranch($item, $borrower),
119 $item->{$HomeOrHoldingBranch},
120 '_GetCircControlBranch returned item branch (no userenv defined)'
123 # No userenv, PatronLibrary
124 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
126 C4::Context->preference('CircControl'),
128 'CircControl changed to PatronLibrary'
131 C4::Circulation::_GetCircControlBranch($item, $borrower),
132 $borrower->{branchcode},
133 '_GetCircControlBranch returned borrower branch'
136 # No userenv, ItemHomeLibrary
137 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
139 C4::Context->preference('CircControl'),
141 'CircControl changed to ItemHomeLibrary'
144 $item->{$HomeOrHoldingBranch},
145 C4::Circulation::_GetCircControlBranch($item, $borrower),
146 '_GetCircControlBranch returned item branch'
150 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
151 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
153 # Userenv set, PickupLibrary
154 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
156 C4::Context->preference('CircControl'),
158 'CircControl changed to PickupLibrary'
161 C4::Circulation::_GetCircControlBranch($item, $borrower),
162 $library2->{branchcode},
163 '_GetCircControlBranch returned current branch'
166 # Userenv set, PatronLibrary
167 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
169 C4::Context->preference('CircControl'),
171 'CircControl changed to PatronLibrary'
174 C4::Circulation::_GetCircControlBranch($item, $borrower),
175 $borrower->{branchcode},
176 '_GetCircControlBranch returned borrower branch'
179 # Userenv set, ItemHomeLibrary
180 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
182 C4::Context->preference('CircControl'),
184 'CircControl changed to ItemHomeLibrary'
187 C4::Circulation::_GetCircControlBranch($item, $borrower),
188 $item->{$HomeOrHoldingBranch},
189 '_GetCircControlBranch returned item branch'
192 # Reset initial configuration
193 t::lib::Mocks::mock_preference('CircControl', $CircControl);
195 C4::Context->preference('CircControl'),
197 'CircControl reset to its initial value'
200 # Set a simple circ policy
201 $dbh->do('DELETE FROM issuingrules');
202 Koha::CirculationRules->search()->delete();
204 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
205 issuelength, lengthunit,
206 renewalsallowed, renewalperiod,
207 norenewalbefore, auto_renew,
224 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
226 # CanBookBeRenewed tests
227 C4::Context->set_preference('ItemsDeniedRenewal','');
228 # Generate test biblio
229 my $biblio = $builder->build_sample_biblio();
231 my $branch = $library2->{branchcode};
233 my $item_1 = $builder->build_sample_item(
235 biblionumber => $biblio->biblionumber,
237 replacementprice => 12.00,
241 $reused_itemnumber_1 = $item_1->itemnumber;
243 my $item_2 = $builder->build_sample_item(
245 biblionumber => $biblio->biblionumber,
247 replacementprice => 23.00,
251 $reused_itemnumber_2 = $item_2->itemnumber;
253 my $item_3 = $builder->build_sample_item(
255 biblionumber => $biblio->biblionumber,
257 replacementprice => 23.00,
263 my %renewing_borrower_data = (
265 surname => 'Renewal',
266 categorycode => $patron_category->{categorycode},
267 branchcode => $branch,
270 my %reserving_borrower_data = (
271 firstname => 'Katrin',
272 surname => 'Reservation',
273 categorycode => $patron_category->{categorycode},
274 branchcode => $branch,
277 my %hold_waiting_borrower_data = (
279 surname => 'Reservation',
280 categorycode => $patron_category->{categorycode},
281 branchcode => $branch,
284 my %restricted_borrower_data = (
285 firstname => 'Alice',
286 surname => 'Reservation',
287 categorycode => $patron_category->{categorycode},
288 debarred => '3228-01-01',
289 branchcode => $branch,
292 my %expired_borrower_data = (
295 categorycode => $patron_category->{categorycode},
296 branchcode => $branch,
297 dateexpiry => dt_from_string->subtract( months => 1 ),
300 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
301 my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
302 my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
303 my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
304 my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
306 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
307 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
308 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
315 my $checkitem = undef;
318 my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
319 my $datedue = dt_from_string( $issue->date_due() );
320 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
322 my $issue2 = AddIssue( $renewing_borrower, $item_2->barcode);
323 $datedue = dt_from_string( $issue->date_due() );
324 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
327 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $item_1->itemnumber } )->borrowernumber;
328 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
330 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
331 is( $renewokay, 1, 'Can renew, no holds for this title or item');
334 # Biblio-level hold, renewal test
336 $branch, $reserving_borrowernumber, $biblio->biblionumber,
337 $bibitems, $priority, $resdate, $expdate, $notes,
338 'a title', $checkitem, $found
341 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
342 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
343 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
344 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
345 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
346 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
347 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
349 # Now let's add an item level hold, we should no longer be able to renew the item
350 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
352 borrowernumber => $hold_waiting_borrowernumber,
353 biblionumber => $biblio->biblionumber,
354 itemnumber => $item_1->itemnumber,
355 branchcode => $branch,
359 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
360 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
363 # 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
364 # be able to renew these items
365 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
367 borrowernumber => $hold_waiting_borrowernumber,
368 biblionumber => $biblio->biblionumber,
369 itemnumber => $item_3->itemnumber,
370 branchcode => $branch,
375 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
376 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
377 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
378 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
379 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
381 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
382 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
383 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
385 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
386 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
387 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
389 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
390 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
391 AddIssue($reserving_borrower, $item_3->barcode);
392 my $reserve = $dbh->selectrow_hashref(
393 'SELECT * FROM old_reserves WHERE reserve_id = ?',
397 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
399 # Item-level hold, renewal test
401 $branch, $reserving_borrowernumber, $biblio->biblionumber,
402 $bibitems, $priority, $resdate, $expdate, $notes,
403 'a title', $item_1->itemnumber, $found
406 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
407 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
408 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
410 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber, 1);
411 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
413 # Items can't fill hold for reasons
414 ModItem({ notforloan => 1 }, $biblio->biblionumber, $item_1->itemnumber);
415 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
416 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
417 ModItem({ notforloan => 0, itype => $itemtype }, $biblio->biblionumber, $item_1->itemnumber);
419 # FIXME: Add more for itemtype not for loan etc.
421 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
422 my $item_5 = $builder->build_sample_item(
424 biblionumber => $biblio->biblionumber,
426 replacementprice => 23.00,
430 my $datedue5 = AddIssue($restricted_borrower, $item_5->barcode);
431 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
433 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
434 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
435 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
436 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $item_5->itemnumber);
437 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
439 # Users cannot renew an overdue item
440 my $item_6 = $builder->build_sample_item(
442 biblionumber => $biblio->biblionumber,
444 replacementprice => 23.00,
449 my $item_7 = $builder->build_sample_item(
451 biblionumber => $biblio->biblionumber,
453 replacementprice => 23.00,
458 my $datedue6 = AddIssue( $renewing_borrower, $item_6->barcode);
459 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
461 my $now = dt_from_string();
462 my $five_weeks = DateTime::Duration->new(weeks => 5);
463 my $five_weeks_ago = $now - $five_weeks;
464 t::lib::Mocks::mock_preference('finesMode', 'production');
466 my $passeddatedue1 = AddIssue($renewing_borrower, $item_7->barcode, $five_weeks_ago);
467 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
469 my ( $fine ) = CalcFine( $item_7->unblessed, $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
470 C4::Overdues::UpdateFine(
472 issue_id => $passeddatedue1->id(),
473 itemnumber => $item_7->itemnumber,
474 borrowernumber => $renewing_borrower->{borrowernumber},
476 due => Koha::DateUtils::output_pref($five_weeks_ago)
480 t::lib::Mocks::mock_preference('RenewalLog', 0);
481 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
482 my %params_renewal = (
483 timestamp => { -like => $date . "%" },
484 module => "CIRCULATION",
488 timestamp => { -like => $date . "%" },
489 module => "CIRCULATION",
492 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
493 my $dt = dt_from_string();
494 Time::Fake->offset( $dt->epoch );
495 my $datedue1 = AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
496 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
497 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
498 isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
501 t::lib::Mocks::mock_preference('RenewalLog', 1);
502 $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
503 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
504 AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
505 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
506 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
508 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
509 is( $fines->count, 2 );
510 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
511 is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
515 my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
516 my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
517 AddIssue( $renewing_borrower,$item_7->barcode,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
518 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
519 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
520 $new_log_size = Koha::ActionLogs->count( \%params_issue );
521 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
523 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
526 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
527 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
528 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
529 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
530 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
533 $hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next;
537 # Test automatic renewal before value for "norenewalbefore" in policy is set
538 # In this case automatic renewal is not permitted prior to due date
539 my $item_4 = $builder->build_sample_item(
541 biblionumber => $biblio->biblionumber,
543 replacementprice => 16.00,
548 $issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
549 ( $renewokay, $error ) =
550 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
551 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
552 is( $error, 'auto_too_soon',
553 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
556 # Test premature manual renewal
557 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
559 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
560 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
561 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
564 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
565 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
567 GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
568 $datedue->clone->add( days => -7 ),
569 'Bug 14395: Renewals permitted 7 days before due date, as expected'
573 # Test 'date' setting for syspref NoRenewalBeforePrecision
574 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
576 GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
577 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
578 'Bug 14395: Renewals permitted 7 days before due date, as expected'
582 # Test premature automatic renewal
583 ( $renewokay, $error ) =
584 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
585 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
586 is( $error, 'auto_too_soon',
587 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
590 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
591 # and test automatic renewal again
592 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
593 ( $renewokay, $error ) =
594 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
595 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
596 is( $error, 'auto_too_soon',
597 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
600 # Change policy so that loans can be renewed 99 days prior to the due date
601 # and test automatic renewal again
602 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
603 ( $renewokay, $error ) =
604 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
605 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
606 is( $error, 'auto_renew',
607 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
610 subtest "too_late_renewal / no_auto_renewal_after" => sub {
612 my $item_to_auto_renew = $builder->build(
615 biblionumber => $biblio->biblionumber,
616 homebranch => $branch,
617 holdingbranch => $branch,
622 my $ten_days_before = dt_from_string->add( days => -10 );
623 my $ten_days_ahead = dt_from_string->add( days => 10 );
624 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
626 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
627 ( $renewokay, $error ) =
628 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
629 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
630 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
632 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
633 ( $renewokay, $error ) =
634 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
635 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
636 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
638 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
639 ( $renewokay, $error ) =
640 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
641 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
642 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
644 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
645 ( $renewokay, $error ) =
646 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
647 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
648 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
650 $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 ) );
651 ( $renewokay, $error ) =
652 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
653 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
654 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
656 $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 ) );
657 ( $renewokay, $error ) =
658 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
659 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
660 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
662 $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 ) );
663 ( $renewokay, $error ) =
664 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
665 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
666 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
669 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
671 my $item_to_auto_renew = $builder->build({
674 biblionumber => $biblio->biblionumber,
675 homebranch => $branch,
676 holdingbranch => $branch,
680 my $ten_days_before = dt_from_string->add( days => -10 );
681 my $ten_days_ahead = dt_from_string->add( days => 10 );
682 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
684 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
685 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
686 C4::Context->set_preference('OPACFineNoRenewals','10');
687 my $fines_amount = 5;
688 my $account = Koha::Account->new({patron_id => $renewing_borrowernumber});
691 amount => $fines_amount,
694 item_id => $item_to_auto_renew->{itemnumber},
695 description => "Some fines"
697 )->accounttype('F')->store;
698 ( $renewokay, $error ) =
699 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
700 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
701 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
705 amount => $fines_amount,
708 item_id => $item_to_auto_renew->{itemnumber},
709 description => "Some fines"
711 )->accounttype('F')->store;
712 ( $renewokay, $error ) =
713 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
714 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
715 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
719 amount => $fines_amount,
722 item_id => $item_to_auto_renew->{itemnumber},
723 description => "Some fines"
725 )->accounttype('F')->store;
726 ( $renewokay, $error ) =
727 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
728 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
729 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
731 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
734 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
736 my $item_to_auto_renew = $builder->build({
739 biblionumber => $biblio->biblionumber,
740 homebranch => $branch,
741 holdingbranch => $branch,
745 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
747 my $ten_days_before = dt_from_string->add( days => -10 );
748 my $ten_days_ahead = dt_from_string->add( days => 10 );
750 # Patron is expired and BlockExpiredPatronOpacActions=0
751 # => auto renew is allowed
752 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
753 my $patron = $expired_borrower;
754 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
755 ( $renewokay, $error ) =
756 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
757 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
758 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
759 Koha::Checkouts->find( $checkout->issue_id )->delete;
762 # Patron is expired and BlockExpiredPatronOpacActions=1
763 # => auto renew is not allowed
764 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
765 $patron = $expired_borrower;
766 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
767 ( $renewokay, $error ) =
768 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
769 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
770 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
771 Koha::Checkouts->find( $checkout->issue_id )->delete;
774 # Patron is not expired and BlockExpiredPatronOpacActions=1
775 # => auto renew is allowed
776 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
777 $patron = $renewing_borrower;
778 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
779 ( $renewokay, $error ) =
780 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
781 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
782 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
783 Koha::Checkouts->find( $checkout->issue_id )->delete;
786 subtest "GetLatestAutoRenewDate" => sub {
788 my $item_to_auto_renew = $builder->build(
791 biblionumber => $biblio->biblionumber,
792 homebranch => $branch,
793 holdingbranch => $branch,
798 my $ten_days_before = dt_from_string->add( days => -10 );
799 my $ten_days_ahead = dt_from_string->add( days => 10 );
800 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
801 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
802 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
803 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' );
804 my $five_days_before = dt_from_string->add( days => -5 );
805 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
806 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
807 is( $latest_auto_renew_date->truncate( to => 'minute' ),
808 $five_days_before->truncate( to => 'minute' ),
809 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
811 my $five_days_ahead = dt_from_string->add( days => 5 );
812 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
813 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
814 is( $latest_auto_renew_date->truncate( to => 'minute' ),
815 $five_days_ahead->truncate( to => 'minute' ),
816 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
818 my $two_days_ahead = dt_from_string->add( days => 2 );
819 $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 ) );
820 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
821 is( $latest_auto_renew_date->truncate( to => 'day' ),
822 $two_days_ahead->truncate( to => 'day' ),
823 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
825 $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 ) );
826 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
827 is( $latest_auto_renew_date->truncate( to => 'day' ),
828 $two_days_ahead->truncate( to => 'day' ),
829 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
836 # set policy to forbid renewals
837 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
839 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
840 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
841 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
843 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
844 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
845 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
847 C4::Overdues::UpdateFine(
849 issue_id => $issue->id(),
850 itemnumber => $item_1->itemnumber,
851 borrowernumber => $renewing_borrower->{borrowernumber},
854 due => Koha::DateUtils::output_pref($datedue)
858 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
859 is( $line->accounttype, 'FU', 'Account line type is FU' );
860 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
861 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
862 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
864 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
865 is( $offset->type, 'Fine', 'Account offset type is Fine' );
866 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
868 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
869 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
871 LostItem( $item_1->itemnumber, 'test', 1 );
873 $line = Koha::Account::Lines->find($line->id);
874 is( $line->accounttype, 'F', 'Account type correctly changed from FU to F' );
876 my $item = Koha::Items->find($item_1->itemnumber);
877 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
878 my $checkout = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber });
879 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
881 my $total_due = $dbh->selectrow_array(
882 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
883 undef, $renewing_borrower->{borrowernumber}
886 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
888 C4::Context->dbh->do("DELETE FROM accountlines");
890 C4::Overdues::UpdateFine(
892 issue_id => $issue2->id(),
893 itemnumber => $item_2->itemnumber,
894 borrowernumber => $renewing_borrower->{borrowernumber},
897 due => Koha::DateUtils::output_pref($datedue)
901 LostItem( $item_2->itemnumber, 'test', 0 );
903 my $item2 = Koha::Items->find($item_2->itemnumber);
904 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
905 ok( Koha::Checkouts->find({ itemnumber => $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
907 $total_due = $dbh->selectrow_array(
908 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
909 undef, $renewing_borrower->{borrowernumber}
912 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
914 my $future = dt_from_string();
915 $future->add( days => 7 );
916 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
917 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
919 # Users cannot renew any item if there is an overdue item
920 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
921 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
922 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
923 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
924 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
926 my $manager = $builder->build_object({ class => "Koha::Patrons" });
927 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
928 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
929 $checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
930 LostItem( $item_3->itemnumber, 'test', 0 );
931 my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
932 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
936 # GetUpcomingDueIssues tests
937 my $branch = $library2->{branchcode};
939 #Create another record
940 my $biblio2 = $builder->build_sample_biblio();
943 my $item_1 = Koha::Items->find($reused_itemnumber_1);
944 my $item_2 = Koha::Items->find($reused_itemnumber_2);
945 my $item_3 = $builder->build_sample_item(
947 biblionumber => $biblio2->biblionumber,
955 my %a_borrower_data = (
956 firstname => 'Fridolyn',
958 categorycode => $patron_category->{categorycode},
959 branchcode => $branch,
962 my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
963 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
965 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
966 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
967 my $today = DateTime->today(time_zone => C4::Context->tz());
969 my $issue = AddIssue( $a_borrower, $item_1->barcode, $yesterday );
970 my $datedue = dt_from_string( $issue->date_due() );
971 my $issue2 = AddIssue( $a_borrower, $item_2->barcode, $two_days_ahead );
972 my $datedue2 = dt_from_string( $issue->date_due() );
976 # GetUpcomingDueIssues tests
978 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
979 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
982 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
983 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
984 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
987 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
988 is ( scalar( @$upcoming_dues ), 1,
989 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
992 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
994 my $issue3 = AddIssue( $a_borrower, $item_3->barcode, $today );
996 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
997 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
999 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
1000 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
1002 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
1003 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
1005 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1006 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1008 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
1009 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1011 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
1012 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1017 my $branch = $library2->{branchcode};
1019 my $biblio = $builder->build_sample_biblio();
1022 my $item = $builder->build_sample_item(
1024 biblionumber => $biblio->biblionumber,
1031 my %a_borrower_data = (
1032 firstname => 'Kyle',
1034 categorycode => $patron_category->{categorycode},
1035 branchcode => $branch,
1038 my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1040 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1041 my $issue = AddIssue( $borrower, $item->barcode );
1044 issue_id => $issue->id(),
1045 itemnumber => $item->itemnumber,
1046 borrowernumber => $borrowernumber,
1052 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1053 my $count = $hr->{count};
1055 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1059 $dbh->do('DELETE FROM issues');
1060 $dbh->do('DELETE FROM items');
1061 $dbh->do('DELETE FROM issuingrules');
1062 Koha::CirculationRules->search()->delete();
1065 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
1066 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1075 Koha::CirculationRules->set_rules(
1077 categorycode => '*',
1085 my $biblio = $builder->build_sample_biblio();
1087 my $item_1 = $builder->build_sample_item(
1089 biblionumber => $biblio->biblionumber,
1090 library => $library2->{branchcode},
1095 my $item_2= $builder->build_sample_item(
1097 biblionumber => $biblio->biblionumber,
1098 library => $library2->{branchcode},
1103 my $borrowernumber1 = Koha::Patron->new({
1104 firstname => 'Kyle',
1106 categorycode => $patron_category->{categorycode},
1107 branchcode => $library2->{branchcode},
1108 })->store->borrowernumber;
1109 my $borrowernumber2 = Koha::Patron->new({
1110 firstname => 'Chelsea',
1112 categorycode => $patron_category->{categorycode},
1113 branchcode => $library2->{branchcode},
1114 })->store->borrowernumber;
1116 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1117 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1119 my $issue = AddIssue( $borrower1, $item_1->barcode );
1121 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1122 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1125 $library2->{branchcode}, $borrowernumber2, $biblio->biblionumber,
1126 '', 1, undef, undef, '',
1130 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1131 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1132 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1133 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1135 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1136 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1137 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1138 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1140 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1141 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1142 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1143 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1145 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1146 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1147 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1148 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1150 # Setting item not checked out to be not for loan but holdable
1151 ModItem({ notforloan => -1 }, $biblio->biblionumber, $item_2->itemnumber);
1153 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1154 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' );
1158 # Don't allow renewing onsite checkout
1159 my $branch = $library->{branchcode};
1161 #Create another record
1162 my $biblio = $builder->build_sample_biblio();
1164 my $item = $builder->build_sample_item(
1166 biblionumber => $biblio->biblionumber,
1172 my $borrowernumber = Koha::Patron->new({
1175 categorycode => $patron_category->{categorycode},
1176 branchcode => $branch,
1177 })->store->borrowernumber;
1179 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1181 my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1182 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $item->itemnumber );
1183 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1184 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1188 my $library = $builder->build({ source => 'Branch' });
1190 my $biblio = $builder->build_sample_biblio();
1192 my $item = $builder->build_sample_item(
1194 biblionumber => $biblio->biblionumber,
1195 library => $library->{branchcode},
1200 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1202 my $issue = AddIssue( $patron, $item->barcode );
1205 issue_id => $issue->id(),
1206 itemnumber => $item->itemnumber,
1207 borrowernumber => $patron->{borrowernumber},
1214 issue_id => $issue->id(),
1215 itemnumber => $item->itemnumber,
1216 borrowernumber => $patron->{borrowernumber},
1221 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1224 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1227 my $homebranch = $builder->build( { source => 'Branch' } );
1228 my $holdingbranch = $builder->build( { source => 'Branch' } );
1229 my $otherbranch = $builder->build( { source => 'Branch' } );
1230 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1231 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1233 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1234 my $item = $builder->build(
1237 homebranch => $homebranch->{branchcode},
1238 holdingbranch => $holdingbranch->{branchcode},
1239 biblionumber => $biblioitem->{biblionumber}
1244 set_userenv($holdingbranch);
1246 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1247 is( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
1249 my ( $error, $question, $alerts );
1251 # AllowReturnToBranch == anywhere
1252 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1253 ## Test that unknown barcodes don't generate internal server errors
1254 set_userenv($homebranch);
1255 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1256 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1257 ## Can be issued from homebranch
1258 set_userenv($homebranch);
1259 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1260 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1261 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1262 ## Can be issued from holdingbranch
1263 set_userenv($holdingbranch);
1264 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1265 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1266 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1267 ## Can be issued from another branch
1268 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1269 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1270 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1272 # AllowReturnToBranch == holdingbranch
1273 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1274 ## Cannot be issued from homebranch
1275 set_userenv($homebranch);
1276 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1277 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1278 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1279 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1280 ## Can be issued from holdinbranch
1281 set_userenv($holdingbranch);
1282 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1283 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1284 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1285 ## Cannot be issued from another branch
1286 set_userenv($otherbranch);
1287 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1288 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1289 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1290 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1292 # AllowReturnToBranch == homebranch
1293 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1294 ## Can be issued from holdinbranch
1295 set_userenv($homebranch);
1296 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1297 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1298 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1299 ## Cannot be issued from holdinbranch
1300 set_userenv($holdingbranch);
1301 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1302 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1303 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1304 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1305 ## Cannot be issued from holdinbranch
1306 set_userenv($otherbranch);
1307 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1308 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1309 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1310 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1312 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1315 subtest 'AddIssue & AllowReturnToBranch' => sub {
1318 my $homebranch = $builder->build( { source => 'Branch' } );
1319 my $holdingbranch = $builder->build( { source => 'Branch' } );
1320 my $otherbranch = $builder->build( { source => 'Branch' } );
1321 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1322 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1324 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1325 my $item = $builder->build(
1328 homebranch => $homebranch->{branchcode},
1329 holdingbranch => $holdingbranch->{branchcode},
1333 biblionumber => $biblioitem->{biblionumber}
1338 set_userenv($holdingbranch);
1340 my $ref_issue = 'Koha::Checkout';
1341 my $issue = AddIssue( $patron_1, $item->{barcode} );
1343 my ( $error, $question, $alerts );
1345 # AllowReturnToBranch == homebranch
1346 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1347 ## Can be issued from homebranch
1348 set_userenv($homebranch);
1349 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1350 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1351 ## Can be issued from holdinbranch
1352 set_userenv($holdingbranch);
1353 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1354 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1355 ## Can be issued from another branch
1356 set_userenv($otherbranch);
1357 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1358 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1360 # AllowReturnToBranch == holdinbranch
1361 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1362 ## Cannot be issued from homebranch
1363 set_userenv($homebranch);
1364 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1365 ## Can be issued from holdingbranch
1366 set_userenv($holdingbranch);
1367 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1368 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1369 ## Cannot be issued from another branch
1370 set_userenv($otherbranch);
1371 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1373 # AllowReturnToBranch == homebranch
1374 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1375 ## Can be issued from homebranch
1376 set_userenv($homebranch);
1377 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1378 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1379 ## Cannot be issued from holdinbranch
1380 set_userenv($holdingbranch);
1381 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1382 ## Cannot be issued from another branch
1383 set_userenv($otherbranch);
1384 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1385 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1388 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1391 my $library = $builder->build( { source => 'Branch' } );
1392 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1394 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1395 my $item_1 = $builder->build(
1398 homebranch => $library->{branchcode},
1399 holdingbranch => $library->{branchcode},
1400 biblionumber => $biblioitem_1->{biblionumber}
1404 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1405 my $item_2 = $builder->build(
1408 homebranch => $library->{branchcode},
1409 holdingbranch => $library->{branchcode},
1410 biblionumber => $biblioitem_2->{biblionumber}
1415 my ( $error, $question, $alerts );
1417 # Patron cannot issue item_1, they have overdues
1418 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1419 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1421 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1422 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1423 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1424 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1426 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1427 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1428 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1429 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1431 # Patron cannot issue item_1, they are debarred
1432 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1433 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1434 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1435 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1436 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1438 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1439 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1440 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1441 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1444 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1447 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1448 my $patron_category_x = $builder->build_object(
1450 class => 'Koha::Patron::Categories',
1451 value => { category_type => 'X' }
1454 my $patron = $builder->build_object(
1456 class => 'Koha::Patrons',
1458 categorycode => $patron_category_x->categorycode,
1459 gonenoaddress => undef,
1466 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1467 my $item_1 = $builder->build(
1471 homebranch => $library->branchcode,
1472 holdingbranch => $library->branchcode,
1473 biblionumber => $biblioitem_1->{biblionumber}
1478 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1479 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1481 # TODO There are other tests to provide here
1484 subtest 'MultipleReserves' => sub {
1487 my $biblio = $builder->build_sample_biblio();
1489 my $branch = $library2->{branchcode};
1491 my $item_1 = $builder->build_sample_item(
1493 biblionumber => $biblio->biblionumber,
1495 replacementprice => 12.00,
1500 my $item_2 = $builder->build_sample_item(
1502 biblionumber => $biblio->biblionumber,
1504 replacementprice => 12.00,
1511 my $resdate = undef;
1512 my $expdate = undef;
1514 my $checkitem = undef;
1517 my %renewing_borrower_data = (
1518 firstname => 'John',
1519 surname => 'Renewal',
1520 categorycode => $patron_category->{categorycode},
1521 branchcode => $branch,
1523 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
1524 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1525 my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
1526 my $datedue = dt_from_string( $issue->date_due() );
1527 is (defined $issue->date_due(), 1, "item 1 checked out");
1528 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber })->borrowernumber;
1530 my %reserving_borrower_data1 = (
1531 firstname => 'Katrin',
1532 surname => 'Reservation',
1533 categorycode => $patron_category->{categorycode},
1534 branchcode => $branch,
1536 my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
1538 $branch, $reserving_borrowernumber1, $biblio->biblionumber,
1539 $bibitems, $priority, $resdate, $expdate, $notes,
1540 'a title', $checkitem, $found
1543 my %reserving_borrower_data2 = (
1544 firstname => 'Kirk',
1545 surname => 'Reservation',
1546 categorycode => $patron_category->{categorycode},
1547 branchcode => $branch,
1549 my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
1551 $branch, $reserving_borrowernumber2, $biblio->biblionumber,
1552 $bibitems, $priority, $resdate, $expdate, $notes,
1553 'a title', $checkitem, $found
1557 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1558 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1561 my $item_3 = $builder->build_sample_item(
1563 biblionumber => $biblio->biblionumber,
1565 replacementprice => 12.00,
1571 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1572 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1576 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1579 my $library = $builder->build( { source => 'Branch' } );
1580 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1582 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1583 my $biblionumber = $biblioitem->{biblionumber};
1584 my $item_1 = $builder->build(
1587 homebranch => $library->{branchcode},
1588 holdingbranch => $library->{branchcode},
1589 biblionumber => $biblionumber,
1593 my $item_2 = $builder->build(
1596 homebranch => $library->{branchcode},
1597 holdingbranch => $library->{branchcode},
1598 biblionumber => $biblionumber,
1603 my ( $error, $question, $alerts );
1604 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1606 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1607 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1608 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1609 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) );
1611 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1612 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1613 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1615 # Add a subscription
1616 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1618 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1619 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1620 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) );
1622 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1623 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1624 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) );
1627 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1630 my $library = $builder->build( { source => 'Branch' } );
1631 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1634 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1635 my $item_1 = $builder->build(
1639 homebranch => $library->{branchcode},
1640 holdingbranch => $library->{branchcode},
1644 biblionumber => $biblioitem_1->{biblionumber}
1648 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1649 my $item_2 = $builder->build(
1653 homebranch => $library->{branchcode},
1654 holdingbranch => $library->{branchcode},
1658 biblionumber => $biblioitem_2->{biblionumber}
1663 # And the issuing rule
1664 Koha::IssuingRules->search->delete;
1665 my $rule = Koha::IssuingRule->new(
1667 categorycode => '*',
1671 firstremind => 1, # 1 day of grace
1672 finedays => 2, # 2 days of fine per day of overdue
1673 lengthunit => 'days',
1678 # Patron cannot issue item_1, they have overdues
1679 my $five_days_ago = dt_from_string->subtract( days => 5 );
1680 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1681 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1682 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1683 ; # Add another overdue
1685 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1686 AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1687 my $debarments = Koha::Patron::Debarments::GetDebarments(
1688 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1689 is( scalar(@$debarments), 1 );
1691 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1692 # Same for the others
1693 my $expected_expiration = output_pref(
1695 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1696 dateformat => 'sql',
1700 is( $debarments->[0]->{expiration}, $expected_expiration );
1702 AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1703 $debarments = Koha::Patron::Debarments::GetDebarments(
1704 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1705 is( scalar(@$debarments), 1 );
1706 $expected_expiration = output_pref(
1708 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1709 dateformat => 'sql',
1713 is( $debarments->[0]->{expiration}, $expected_expiration );
1715 Koha::Patron::Debarments::DelUniqueDebarment(
1716 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1718 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1719 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1720 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1721 ; # Add another overdue
1722 AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1723 $debarments = Koha::Patron::Debarments::GetDebarments(
1724 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1725 is( scalar(@$debarments), 1 );
1726 $expected_expiration = output_pref(
1728 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1729 dateformat => 'sql',
1733 is( $debarments->[0]->{expiration}, $expected_expiration );
1735 AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1736 $debarments = Koha::Patron::Debarments::GetDebarments(
1737 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1738 is( scalar(@$debarments), 1 );
1739 $expected_expiration = output_pref(
1741 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1742 dateformat => 'sql',
1746 is( $debarments->[0]->{expiration}, $expected_expiration );
1749 subtest 'AddReturn + suspension_chargeperiod' => sub {
1752 my $library = $builder->build( { source => 'Branch' } );
1753 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1756 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1757 my $item_1 = $builder->build(
1761 homebranch => $library->{branchcode},
1762 holdingbranch => $library->{branchcode},
1766 biblionumber => $biblioitem_1->{biblionumber}
1771 # And the issuing rule
1772 Koha::IssuingRules->search->delete;
1773 my $rule = Koha::IssuingRule->new(
1775 categorycode => '*',
1779 firstremind => 0, # 0 day of grace
1780 finedays => 2, # 2 days of fine per day of overdue
1781 suspension_chargeperiod => 1,
1782 lengthunit => 'days',
1787 my $five_days_ago = dt_from_string->subtract( days => 5 );
1788 # We want to charge 2 days every day, without grace
1789 # With 5 days of overdue: 5 * Z
1790 my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1791 test_debarment_on_checkout(
1794 library => $library,
1796 due_date => $five_days_ago,
1797 expiration_date => $expected_expiration,
1801 # We want to charge 2 days every 2 days, without grace
1802 # With 5 days of overdue: (5 * 2) / 2
1803 $rule->suspension_chargeperiod(2)->store;
1804 $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1805 test_debarment_on_checkout(
1808 library => $library,
1810 due_date => $five_days_ago,
1811 expiration_date => $expected_expiration,
1815 # We want to charge 2 days every 3 days, with 1 day of grace
1816 # With 5 days of overdue: ((5-1) / 3 ) * 2
1817 $rule->suspension_chargeperiod(3)->store;
1818 $rule->firstremind(1)->store;
1819 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1820 test_debarment_on_checkout(
1823 library => $library,
1825 due_date => $five_days_ago,
1826 expiration_date => $expected_expiration,
1830 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1831 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1832 $rule->finedays(2)->store;
1833 $rule->suspension_chargeperiod(1)->store;
1834 $rule->firstremind(0)->store;
1835 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1837 # Adding a holiday 2 days ago
1838 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1839 my $two_days_ago = dt_from_string->subtract( days => 2 );
1840 $calendar->insert_single_holiday(
1841 day => $two_days_ago->day,
1842 month => $two_days_ago->month,
1843 year => $two_days_ago->year,
1844 title => 'holidayTest-2d',
1845 description => 'holidayDesc 2 days ago'
1847 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1848 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1849 test_debarment_on_checkout(
1852 library => $library,
1854 due_date => $five_days_ago,
1855 expiration_date => $expected_expiration,
1859 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1860 my $two_days_ahead = dt_from_string->add( days => 2 );
1861 $calendar->insert_single_holiday(
1862 day => $two_days_ahead->day,
1863 month => $two_days_ahead->month,
1864 year => $two_days_ahead->year,
1865 title => 'holidayTest+2d',
1866 description => 'holidayDesc 2 days ahead'
1869 # Same as above, but we should skip D+2
1870 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1871 test_debarment_on_checkout(
1874 library => $library,
1876 due_date => $five_days_ago,
1877 expiration_date => $expected_expiration,
1881 # Adding another holiday, day of expiration date
1882 my $expected_expiration_dt = dt_from_string($expected_expiration);
1883 $calendar->insert_single_holiday(
1884 day => $expected_expiration_dt->day,
1885 month => $expected_expiration_dt->month,
1886 year => $expected_expiration_dt->year,
1887 title => 'holidayTest_exp',
1888 description => 'holidayDesc on expiration date'
1890 # Expiration date will be the day after
1891 test_debarment_on_checkout(
1894 library => $library,
1896 due_date => $five_days_ago,
1897 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1901 test_debarment_on_checkout(
1904 library => $library,
1906 return_date => dt_from_string->add(days => 5),
1907 expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1912 subtest 'AddReturn | is_overdue' => sub {
1915 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
1916 t::lib::Mocks::mock_preference('finesMode', 'production');
1917 t::lib::Mocks::mock_preference('MaxFine', '100');
1919 my $library = $builder->build( { source => 'Branch' } );
1920 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1922 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1923 my $item = $builder->build(
1927 homebranch => $library->{branchcode},
1928 holdingbranch => $library->{branchcode},
1932 biblionumber => $biblioitem->{biblionumber},
1937 Koha::IssuingRules->search->delete;
1938 my $rule = Koha::IssuingRule->new(
1940 categorycode => '*',
1944 lengthunit => 'days',
1945 fine => 1, # Charge 1 every day of overdue
1951 my $one_day_ago = dt_from_string->subtract( days => 1 );
1952 my $five_days_ago = dt_from_string->subtract( days => 5 );
1953 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1954 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1956 # No date specify, today will be used
1957 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1958 AddReturn( $item->{barcode}, $library->{branchcode} );
1959 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1960 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1962 # specify return date 5 days before => no overdue
1963 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1964 AddReturn( $item->{barcode}, $library->{branchcode}, undef, $ten_days_ago );
1965 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1966 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1968 # specify return date 5 days later => overdue
1969 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1970 AddReturn( $item->{barcode}, $library->{branchcode}, undef, $five_days_ago );
1971 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1972 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1974 # specify dropbox date 5 days before => no overdue
1975 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
1976 AddReturn( $item->{barcode}, $library->{branchcode}, $ten_days_ago );
1977 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1978 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1980 # specify dropbox date 5 days later => overdue, or... not
1981 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
1982 AddReturn( $item->{barcode}, $library->{branchcode}, $five_days_ago );
1983 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
1984 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
1987 subtest '_FixAccountForLostAndReturned' => sub {
1991 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1992 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
1994 my $processfee_amount = 20;
1995 my $replacement_amount = 99.00;
1996 my $item_type = $builder->build_object(
1997 { class => 'Koha::ItemTypes',
1999 notforloan => undef,
2001 defaultreplacecost => undef,
2002 processfee => $processfee_amount,
2003 rentalcharge_daily => 0,
2007 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2009 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Daria' });
2011 subtest 'Full write-off tests' => sub {
2015 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2016 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2017 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
2019 my $item = $builder->build_sample_item(
2021 biblionumber => $biblio->biblionumber,
2022 library => $library->branchcode,
2023 replacementprice => $replacement_amount,
2024 itype => $item_type->itemtype,
2028 AddIssue( $patron->unblessed, $item->barcode );
2030 # Simulate item marked as lost
2031 ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2032 LostItem( $item->itemnumber, 1 );
2034 my $processing_fee_lines = Koha::Account::Lines->search(
2035 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2036 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2037 my $processing_fee_line = $processing_fee_lines->next;
2038 is( $processing_fee_line->amount + 0,
2039 $processfee_amount, 'The right PF amount is generated' );
2040 is( $processing_fee_line->amountoutstanding + 0,
2041 $processfee_amount, 'The right PF amountoutstanding is generated' );
2043 my $lost_fee_lines = Koha::Account::Lines->search(
2044 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2045 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2046 my $lost_fee_line = $lost_fee_lines->next;
2047 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2048 is( $lost_fee_line->amountoutstanding + 0,
2049 $replacement_amount, 'The right L amountoutstanding is generated' );
2051 my $account = $patron->account;
2052 my $debts = $account->outstanding_debits;
2054 # Write off the debt
2055 my $credit = $account->add_credit(
2056 { amount => $account->balance,
2058 interface => 'test',
2061 $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2063 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2064 is( $credit_return_id, undef, 'No CR account line added' );
2066 $lost_fee_line->discard_changes; # reload from DB
2067 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2068 is( $lost_fee_line->accounttype,
2069 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2071 is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2074 subtest 'Full payment tests' => sub {
2078 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2080 my $item = $builder->build_sample_item(
2082 biblionumber => $biblio->biblionumber,
2083 library => $library->branchcode,
2084 replacementprice => $replacement_amount,
2085 itype => $item_type->itemtype
2089 AddIssue( $patron->unblessed, $item->barcode );
2091 # Simulate item marked as lost
2092 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2093 LostItem( $item->itemnumber, 1 );
2095 my $processing_fee_lines = Koha::Account::Lines->search(
2096 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2097 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2098 my $processing_fee_line = $processing_fee_lines->next;
2099 is( $processing_fee_line->amount + 0,
2100 $processfee_amount, 'The right PF amount is generated' );
2101 is( $processing_fee_line->amountoutstanding + 0,
2102 $processfee_amount, 'The right PF amountoutstanding is generated' );
2104 my $lost_fee_lines = Koha::Account::Lines->search(
2105 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2106 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2107 my $lost_fee_line = $lost_fee_lines->next;
2108 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2109 is( $lost_fee_line->amountoutstanding + 0,
2110 $replacement_amount, 'The right L amountountstanding is generated' );
2112 my $account = $patron->account;
2113 my $debts = $account->outstanding_debits;
2115 # Write off the debt
2116 my $credit = $account->add_credit(
2117 { amount => $account->balance,
2119 interface => 'test',
2122 $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2124 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2125 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2127 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2128 is( $credit_return->amount + 0,
2129 -99.00, 'The account line of type CR has an amount of -99' );
2130 is( $credit_return->amountoutstanding + 0,
2131 -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2133 $lost_fee_line->discard_changes;
2134 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2135 is( $lost_fee_line->accounttype,
2136 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2138 is( $patron->account->balance,
2139 -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2142 subtest 'Test without payment or write off' => sub {
2146 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2148 my $item = $builder->build_sample_item(
2150 biblionumber => $biblio->biblionumber,
2151 library => $library->branchcode,
2152 replacementprice => 23.00,
2153 replacementprice => $replacement_amount,
2154 itype => $item_type->itemtype
2158 AddIssue( $patron->unblessed, $item->barcode );
2160 # Simulate item marked as lost
2161 ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2162 LostItem( $item->itemnumber, 1 );
2164 my $processing_fee_lines = Koha::Account::Lines->search(
2165 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2166 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2167 my $processing_fee_line = $processing_fee_lines->next;
2168 is( $processing_fee_line->amount + 0,
2169 $processfee_amount, 'The right PF amount is generated' );
2170 is( $processing_fee_line->amountoutstanding + 0,
2171 $processfee_amount, 'The right PF amountoutstanding is generated' );
2173 my $lost_fee_lines = Koha::Account::Lines->search(
2174 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2175 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2176 my $lost_fee_line = $lost_fee_lines->next;
2177 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2178 is( $lost_fee_line->amountoutstanding + 0,
2179 $replacement_amount, 'The right L amountountstanding is generated' );
2181 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2182 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2184 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2185 is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2186 is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2188 $lost_fee_line->discard_changes;
2189 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2190 is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2192 is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2195 subtest 'Test with partial payement and write off, and remaining debt' => sub {
2199 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2200 my $item = $builder->build_sample_item(
2202 biblionumber => $biblio->biblionumber,
2203 library => $library->branchcode,
2204 replacementprice => $replacement_amount,
2205 itype => $item_type->itemtype
2209 AddIssue( $patron->unblessed, $item->barcode );
2211 # Simulate item marked as lost
2212 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2213 LostItem( $item->itemnumber, 1 );
2215 my $processing_fee_lines = Koha::Account::Lines->search(
2216 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2217 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2218 my $processing_fee_line = $processing_fee_lines->next;
2219 is( $processing_fee_line->amount + 0,
2220 $processfee_amount, 'The right PF amount is generated' );
2221 is( $processing_fee_line->amountoutstanding + 0,
2222 $processfee_amount, 'The right PF amountoutstanding is generated' );
2224 my $lost_fee_lines = Koha::Account::Lines->search(
2225 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2226 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2227 my $lost_fee_line = $lost_fee_lines->next;
2228 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2229 is( $lost_fee_line->amountoutstanding + 0,
2230 $replacement_amount, 'The right L amountountstanding is generated' );
2232 my $account = $patron->account;
2233 is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2236 my $payment_amount = 27;
2237 my $payment = $account->add_credit(
2238 { amount => $payment_amount,
2240 interface => 'test',
2244 $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } );
2246 # Partially write off fee
2247 my $write_off_amount = 25;
2248 my $write_off = $account->add_credit(
2249 { amount => $write_off_amount,
2251 interface => 'test',
2254 $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
2256 is( $account->balance,
2257 $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2258 'Payment and write off applied'
2261 # Store the amountoutstanding value
2262 $lost_fee_line->discard_changes;
2263 my $outstanding = $lost_fee_line->amountoutstanding;
2265 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2266 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2268 is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2270 $lost_fee_line->discard_changes;
2271 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2272 is( $lost_fee_line->accounttype,
2273 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2275 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2276 is( $credit_return->amount + 0,
2277 ($payment_amount + $outstanding ) * -1,
2278 'The account line of type CR has an amount equal to the payment + outstanding'
2280 is( $credit_return->amountoutstanding + 0,
2281 $payment_amount * -1,
2282 'The account line of type CR has an amountoutstanding equal to the payment'
2285 is( $account->balance,
2286 $processfee_amount - $payment_amount,
2287 'The patron balance is the difference between the PF and the credit'
2291 subtest 'Partial payement, existing debits and AccountAutoReconcile' => sub {
2295 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2296 my $barcode = 'KD123456793';
2297 my $replacement_amount = 100;
2298 my $processfee_amount = 20;
2300 my $item_type = $builder->build_object(
2301 { class => 'Koha::ItemTypes',
2303 notforloan => undef,
2305 defaultreplacecost => undef,
2307 rentalcharge_daily => 0,
2311 my ( undef, undef, $item_id ) = AddItem(
2312 { homebranch => $library->branchcode,
2313 holdingbranch => $library->branchcode,
2314 barcode => $barcode,
2315 replacementprice => $replacement_amount,
2316 itype => $item_type->itemtype
2318 $biblio->biblionumber
2321 AddIssue( $patron->unblessed, $barcode );
2323 # Simulate item marked as lost
2324 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item_id );
2325 LostItem( $item_id, 1 );
2327 my $lost_fee_lines = Koha::Account::Lines->search(
2328 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2329 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2330 my $lost_fee_line = $lost_fee_lines->next;
2331 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2332 is( $lost_fee_line->amountoutstanding + 0,
2333 $replacement_amount, 'The right L amountountstanding is generated' );
2335 my $account = $patron->account;
2336 is( $account->balance, $replacement_amount, 'Balance is L' );
2339 my $payment_amount = 27;
2340 my $payment = $account->add_credit(
2341 { amount => $payment_amount,
2343 interface => 'test',
2346 $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' });
2348 is( $account->balance,
2349 $replacement_amount - $payment_amount,
2353 my $manual_debit_amount = 80;
2354 $account->add_debit( { amount => $manual_debit_amount, type => 'fine', interface =>'test' } );
2356 is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
2358 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
2360 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2361 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2363 is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2365 my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'FU' })->next;
2366 is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2370 subtest '_FixOverduesOnReturn' => sub {
2373 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
2375 my $branchcode = $library2->{branchcode};
2377 my $item = $builder->build_sample_item(
2379 biblionumber => $biblio->biblionumber,
2380 library => $branchcode,
2381 replacementprice => 99.00,
2386 my $patron = $builder->build( { source => 'Borrower' } );
2388 ## Start with basic call, should just close out the open fine
2389 my $accountline = Koha::Account::Line->new(
2391 borrowernumber => $patron->{borrowernumber},
2392 accounttype => 'FU',
2393 itemnumber => $item->itemnumber,
2395 amountoutstanding => 99.00,
2396 interface => 'test',
2400 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber );
2402 $accountline->_result()->discard_changes();
2404 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2405 is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2408 ## Run again, with exemptfine enabled
2411 accounttype => 'FU',
2412 amountoutstanding => 99.00,
2416 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1 );
2418 $accountline->_result()->discard_changes();
2419 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2421 is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2422 is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2423 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2424 is( $offset->amount, '-99.000000', "Amount of offset is correct" );
2427 subtest 'Set waiting flag' => sub {
2430 my $library_1 = $builder->build( { source => 'Branch' } );
2431 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2432 my $library_2 = $builder->build( { source => 'Branch' } );
2433 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2435 my $biblio = $builder->build( { source => 'Biblio' } );
2436 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2438 my $item = $builder->build(
2442 homebranch => $library_1->{branchcode},
2443 holdingbranch => $library_1->{branchcode},
2447 biblionumber => $biblioitem->{biblionumber},
2452 set_userenv( $library_2 );
2453 my $reserve_id = AddReserve(
2454 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2455 '', 1, undef, undef, '', undef, $item->{itemnumber},
2458 set_userenv( $library_1 );
2459 my $do_transfer = 1;
2460 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2461 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2462 my $hold = Koha::Holds->find( $reserve_id );
2463 is( $hold->found, 'T', 'Hold is in transit' );
2465 my ( $status ) = CheckReserves($item->{itemnumber});
2466 is( $status, 'Reserved', 'Hold is not waiting yet');
2468 set_userenv( $library_2 );
2470 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2471 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2472 $hold = Koha::Holds->find( $reserve_id );
2473 is( $hold->found, 'W', 'Hold is waiting' );
2474 ( $status ) = CheckReserves($item->{itemnumber});
2475 is( $status, 'Waiting', 'Now the hold is waiting');
2478 subtest 'Cancel transfers on lost items' => sub {
2480 my $library_1 = $builder->build( { source => 'Branch' } );
2481 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2482 my $library_2 = $builder->build( { source => 'Branch' } );
2483 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2484 my $biblio = $builder->build( { source => 'Biblio' } );
2485 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2486 my $item = $builder->build(
2490 homebranch => $library_1->{branchcode},
2491 holdingbranch => $library_1->{branchcode},
2495 biblionumber => $biblioitem->{biblionumber},
2500 set_userenv( $library_2 );
2501 my $reserve_id = AddReserve(
2502 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber}, '', 1, undef, undef, '', undef, $item->{itemnumber},
2505 #Return book and add transfer
2506 set_userenv( $library_1 );
2507 my $do_transfer = 1;
2508 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2509 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2510 C4::Circulation::transferbook( $library_2->{branchcode}, $item->{barcode} );
2511 my $hold = Koha::Holds->find( $reserve_id );
2512 is( $hold->found, 'T', 'Hold is in transit' );
2514 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2515 my ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2516 is( $tobranch, $library_2->{branchcode}, 'The transfer record exists in the branchtransfers table');
2517 my $itemcheck = Koha::Items->find($item->{itemnumber});
2518 is( $itemcheck->holdingbranch, $library_2->{branchcode}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2520 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2521 ModItem( { itemlost => 1 }, $biblio->{biblionumber}, $item->{itemnumber} );
2522 LostItem( $item->{itemnumber}, 'test', 1 );
2523 ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2524 is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2525 $itemcheck = Koha::Items->find($item->{itemnumber});
2526 is( $itemcheck->holdingbranch, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2529 subtest 'CanBookBeIssued | is_overdue' => sub {
2532 # Set a simple circ policy
2533 $dbh->do('DELETE FROM issuingrules');
2535 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2536 issuelength, lengthunit,
2537 renewalsallowed, renewalperiod,
2538 norenewalbefore, auto_renew,
2555 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2556 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2557 my $library = $builder->build( { source => 'Branch' } );
2558 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2560 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2561 my $item = $builder->build(
2565 homebranch => $library->{branchcode},
2566 holdingbranch => $library->{branchcode},
2570 biblionumber => $biblioitem->{biblionumber},
2575 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2576 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2577 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2578 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2579 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2580 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2583 subtest 'ItemsDeniedRenewal preference' => sub {
2586 C4::Context->set_preference('ItemsDeniedRenewal','');
2588 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2591 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
2592 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2595 '*', $idr_lib->branchcode, '*', 25,
2602 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2603 homebranch => $idr_lib->branchcode,
2607 itemcallnumber => undef,
2611 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2612 homebranch => $idr_lib->branchcode,
2615 location => 'NOPROC'
2619 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2620 branchcode => $idr_lib->branchcode,
2623 my $future = dt_from_string->add( days => 1 );
2624 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2625 returndate => undef,
2628 borrowernumber => $idr_borrower->borrowernumber,
2629 itemnumber => $deny_book->itemnumber,
2630 onsite_checkout => 0,
2631 date_due => $future,
2634 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2635 returndate => undef,
2638 borrowernumber => $idr_borrower->borrowernumber,
2639 itemnumber => $allow_book->itemnumber,
2640 onsite_checkout => 0,
2641 date_due => $future,
2647 my ( $idr_mayrenew, $idr_error ) =
2648 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2649 is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2650 is( $idr_error, undef, 'Renewal allowed when no rules' );
2652 $idr_rules="withdrawn: [1]";
2654 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2655 ( $idr_mayrenew, $idr_error ) =
2656 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2657 is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2658 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2659 ( $idr_mayrenew, $idr_error ) =
2660 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2661 is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2662 is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2664 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2666 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2667 ( $idr_mayrenew, $idr_error ) =
2668 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2669 is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2670 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2671 ( $idr_mayrenew, $idr_error ) =
2672 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2673 is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2674 is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2676 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2678 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2679 ( $idr_mayrenew, $idr_error ) =
2680 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2681 is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2682 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2683 ( $idr_mayrenew, $idr_error ) =
2684 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2685 is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2686 is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2688 $idr_rules="itemcallnumber: [NULL]";
2689 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2690 ( $idr_mayrenew, $idr_error ) =
2691 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2692 is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2693 $idr_rules="itemcallnumber: ['']";
2694 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2695 ( $idr_mayrenew, $idr_error ) =
2696 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2697 is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2699 $idr_rules="itemnotes: [NULL]";
2700 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2701 ( $idr_mayrenew, $idr_error ) =
2702 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2703 is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2704 $idr_rules="itemnotes: ['']";
2705 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2706 ( $idr_mayrenew, $idr_error ) =
2707 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2708 is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2711 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2714 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2715 my $library = $builder->build( { source => 'Branch' } );
2716 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2718 my $itemtype = $builder->build(
2720 source => 'Itemtype',
2721 value => { notforloan => undef, }
2725 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2726 my $item = $builder->build_object(
2728 class => 'Koha::Items',
2730 homebranch => $library->{branchcode},
2731 holdingbranch => $library->{branchcode},
2735 biblionumber => $biblioitem->{biblionumber},
2736 biblioitemnumber => $biblioitem->{biblioitemnumber},
2741 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2742 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2743 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2746 subtest 'CanBookBeIssued | notforloan' => sub {
2749 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2751 my $library = $builder->build( { source => 'Branch' } );
2752 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2754 my $itemtype = $builder->build(
2756 source => 'Itemtype',
2757 value => { notforloan => undef, }
2761 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2762 my $item = $builder->build_object(
2764 class => 'Koha::Items',
2766 homebranch => $library->{branchcode},
2767 holdingbranch => $library->{branchcode},
2771 itype => $itemtype->{itemtype},
2772 biblionumber => $biblioitem->{biblionumber},
2773 biblioitemnumber => $biblioitem->{biblioitemnumber},
2778 my ( $issuingimpossible, $needsconfirmation );
2781 subtest 'item-level_itypes = 1' => sub {
2784 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2785 # Is for loan at item type and item level
2786 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2787 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2788 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2790 # not for loan at item type level
2791 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2792 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2793 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2796 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2797 'Item can not be issued, not for loan at item type level'
2800 # not for loan at item level
2801 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2802 $item->notforloan( 1 )->store;
2803 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2804 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2807 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2808 'Item can not be issued, not for loan at item type level'
2812 subtest 'item-level_itypes = 0' => sub {
2815 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2817 # We set another itemtype for biblioitem
2818 my $itemtype = $builder->build(
2820 source => 'Itemtype',
2821 value => { notforloan => undef, }
2825 # for loan at item type and item level
2826 $item->notforloan(0)->store;
2827 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
2828 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2829 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2830 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2832 # not for loan at item type level
2833 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2834 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2835 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2838 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2839 'Item can not be issued, not for loan at item type level'
2842 # not for loan at item level
2843 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2844 $item->notforloan( 1 )->store;
2845 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2846 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2849 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2850 'Item can not be issued, not for loan at item type level'
2854 # TODO test with AllowNotForLoanOverride = 1
2857 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
2860 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
2861 my $item = $builder->build_object({ class => 'Koha::Items', value => { onloan => '2018-01-01' }});
2862 AddReturn( $item->barcode, $item->homebranch );
2863 $item->discard_changes; # refresh
2864 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
2867 $schema->storage->txn_rollback;
2868 C4::Context->clear_syspref_cache();
2869 $cache->clear_from_cache('single_holidays');
2871 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
2875 $schema->storage->txn_begin;
2877 t::lib::Mocks::mock_preference('item-level_itypes', 1);
2879 my $issuing_charges = 15;
2880 my $title = 'A title';
2881 my $author = 'Author, An';
2882 my $barcode = 'WHATARETHEODDS';
2884 my $circ = Test::MockModule->new('C4::Circulation');
2886 'GetIssuingCharges',
2888 return $issuing_charges;
2892 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2893 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
2894 my $patron = $builder->build_object({
2895 class => 'Koha::Patrons',
2896 value => { branchcode => $library->id }
2899 my $biblio = $builder->build_sample_biblio({ title=> $title, author => $author });
2900 my ( undef, undef, $item_id ) = AddItem(
2902 homebranch => $library->id,
2903 holdingbranch => $library->id,
2904 barcode => $barcode,
2905 replacementprice => 23.00,
2906 itype => $itemtype->id
2908 $biblio->biblionumber
2910 my $item = Koha::Items->find( $item_id );
2912 my $context = Test::MockModule->new('C4::Context');
2913 $context->mock( userenv => { branch => $library->id } );
2915 # Check the item out
2916 AddIssue( $patron->unblessed, $item->barcode );
2917 t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
2918 my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
2919 my %params_renewal = (
2920 timestamp => { -like => $date . "%" },
2921 module => "CIRCULATION",
2922 action => "RENEWAL",
2924 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
2925 AddRenewal( $patron->id, $item->id, $library->id );
2926 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
2927 is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
2929 my $checkouts = $patron->checkouts;
2930 # The following will fail if run on 00:00:00
2931 unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
2933 t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
2934 $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
2935 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
2936 AddRenewal( $patron->id, $item->id, $library->id );
2937 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
2938 is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
2940 my $lines = Koha::Account::Lines->search({
2941 borrowernumber => $patron->id,
2942 itemnumber => $item->id
2945 is( $lines->count, 3 );
2947 my $line = $lines->next;
2948 is( $line->accounttype, 'Rent', 'The issuing charge generates an accountline' );
2949 is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
2950 is( $line->description, 'Rental', 'AddIssuingCharge set a hardcoded description for the accountline' );
2952 $line = $lines->next;
2953 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2954 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2955 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2957 $line = $lines->next;
2958 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2959 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2960 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2962 $schema->storage->txn_rollback;
2965 subtest 'ProcessOfflinePayment() tests' => sub {
2969 $schema->storage->txn_begin;
2973 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2974 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2975 my $result = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
2977 is( $result, 'Success.', 'The right string is returned' );
2979 my $lines = $patron->account->lines;
2980 is( $lines->count, 1, 'line created correctly');
2982 my $line = $lines->next;
2983 is( $line->amount+0, $amount * -1, 'amount picked from params' );
2984 is( $line->branchcode, $library->id, 'branchcode set correctly' );
2986 $schema->storage->txn_rollback;
2992 my ( $library ) = @_;
2993 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
2997 my ( $error, $question, $alert ) = @_;
2999 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
3000 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
3001 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
3005 sub test_debarment_on_checkout {
3007 my $item = $params->{item};
3008 my $library = $params->{library};
3009 my $patron = $params->{patron};
3010 my $due_date = $params->{due_date} || dt_from_string;
3011 my $return_date = $params->{return_date} || dt_from_string;
3012 my $expected_expiration_date = $params->{expiration_date};
3014 $expected_expiration_date = output_pref(
3016 dt => $expected_expiration_date,
3017 dateformat => 'sql',
3021 my @caller = caller;
3022 my $line_number = $caller[2];
3023 AddIssue( $patron, $item->{barcode}, $due_date );
3025 my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
3026 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
3027 or diag('AddReturn returned message ' . Dumper $message );
3028 my $debarments = Koha::Patron::Debarments::GetDebarments(
3029 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
3030 is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
3032 is( $debarments->[0]->{expiration},
3033 $expected_expiration_date, 'Test at line ' . $line_number );
3034 Koha::Patron::Debarments::DelUniqueDebarment(
3035 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
3038 subtest 'Incremented fee tests' => sub {
3041 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3043 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
3045 my $module = new Test::MockModule('C4::Context');
3046 $module->mock('userenv', sub { { branch => $library->id } });
3048 my $patron = $builder->build_object(
3050 class => 'Koha::Patrons',
3051 value => { categorycode => $patron_category->{categorycode} }
3055 my $itemtype = $builder->build_object(
3057 class => 'Koha::ItemTypes',
3059 notforloan => undef,
3061 rentalcharge_daily => 1.000000
3066 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3067 my $item = $builder->build_object(
3069 class => 'Koha::Items',
3071 homebranch => $library->id,
3072 holdingbranch => $library->id,
3076 itype => $itemtype->id,
3077 biblionumber => $biblioitem->{biblionumber},
3078 biblioitemnumber => $biblioitem->{biblioitemnumber},
3083 is( $itemtype->rentalcharge_daily, '1.000000', 'Daily rental charge stored and retreived correctly' );
3084 is( $item->effective_itemtype, $itemtype->id, "Itemtype set correctly for item");
3086 my $dt_from = dt_from_string();
3087 my $dt_to = dt_from_string()->add( days => 7 );
3088 my $dt_to_renew = dt_from_string()->add( days => 13 );
3090 t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
3091 my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3092 my $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3093 is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar" );
3094 $accountline->delete();
3095 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3096 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3097 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar, for renewal" );
3098 $accountline->delete();
3101 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
3102 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3103 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3104 is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed" );
3105 $accountline->delete();
3106 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3107 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3108 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed, for renewal" );
3109 $accountline->delete();
3112 my $calendar = C4::Calendar->new( branchcode => $library->id );
3113 $calendar->insert_week_day_holiday(
3115 title => 'Test holiday',
3116 description => 'Test holiday'
3118 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3119 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3120 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
3121 $accountline->delete();
3122 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3123 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3124 is( $accountline->amount, '5.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays, for renewal" );
3125 $accountline->delete();
3128 $itemtype->rentalcharge('2.000000')->store;
3129 is( $itemtype->rentalcharge, '2.000000', 'Rental charge updated and retreived correctly' );
3130 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from);
3131 my $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3132 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly");
3133 $accountlines->delete();
3134 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3135 $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3136 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly, for renewal");
3137 $accountlines->delete();
3141 subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
3144 t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
3145 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3148 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3149 my $patron = $builder->build_object(
3151 class => 'Koha::Patrons',
3152 value => { categorycode => $patron_category->{categorycode} }
3156 my $itemtype = $builder->build_object(
3158 class => 'Koha::ItemTypes',
3162 rentalcharge_daily => 0
3167 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3168 my $item = $builder->build_object(
3170 class => 'Koha::Items',
3172 homebranch => $library->id,
3173 holdingbranch => $library->id,
3177 itype => $itemtype->id,
3178 biblionumber => $biblioitem->{biblionumber},
3179 biblioitemnumber => $biblioitem->{biblioitemnumber},
3184 my ( $issuingimpossible, $needsconfirmation );
3185 my $dt_from = dt_from_string();
3186 my $dt_due = dt_from_string()->add( days => 3 );
3188 $itemtype->rentalcharge('1.000000')->store;
3189 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3190 is_deeply( $needsconfirmation, { RENTALCHARGE => '1' }, 'Item needs rentalcharge confirmation to be issued' );
3191 $itemtype->rentalcharge('0')->store;
3192 $itemtype->rentalcharge_daily('1.000000')->store;
3193 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3194 is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
3195 $itemtype->rentalcharge_daily('0')->store;