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 => 130;
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;
53 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
57 my ( $error, $question, $alert ) = @_;
59 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
60 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
61 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
65 sub test_debarment_on_checkout {
67 my $item = $params->{item};
68 my $library = $params->{library};
69 my $patron = $params->{patron};
70 my $due_date = $params->{due_date} || dt_from_string;
71 my $return_date = $params->{return_date} || dt_from_string;
72 my $expected_expiration_date = $params->{expiration_date};
74 $expected_expiration_date = output_pref(
76 dt => $expected_expiration_date,
82 my $line_number = $caller[2];
83 AddIssue( $patron, $item->{barcode}, $due_date );
85 my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
86 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
87 or diag('AddReturn returned message ' . Dumper $message );
88 my $debarments = Koha::Patron::Debarments::GetDebarments(
89 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
90 is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
92 is( $debarments->[0]->{expiration},
93 $expected_expiration_date, 'Test at line ' . $line_number );
94 Koha::Patron::Debarments::DelUniqueDebarment(
95 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
98 my $schema = Koha::Database->schema;
99 $schema->storage->txn_begin;
100 my $builder = t::lib::TestBuilder->new;
101 my $dbh = C4::Context->dbh;
103 # Prevent random failures by mocking ->now
104 my $now_value = DateTime->now();
105 my $mocked_datetime = Test::MockModule->new('DateTime');
106 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
109 $dbh->{RaiseError} = 1;
111 my $cache = Koha::Caches->get_instance();
112 $dbh->do(q|DELETE FROM special_holidays|);
113 $dbh->do(q|DELETE FROM repeatable_holidays|);
114 $cache->clear_from_cache('single_holidays');
116 # Start with a clean slate
117 $dbh->do('DELETE FROM issues');
118 $dbh->do('DELETE FROM borrowers');
120 my $library = $builder->build({
123 my $library2 = $builder->build({
126 my $itemtype = $builder->build(
128 source => 'Itemtype',
132 rentalcharge_daily => 0,
133 defaultreplacecost => undef,
138 my $patron_category = $builder->build(
140 source => 'Category',
142 category_type => 'P',
144 BlockExpiredPatronOpacActions => -1, # Pick the pref value
149 my $CircControl = C4::Context->preference('CircControl');
150 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
153 homebranch => $library2->{branchcode},
154 holdingbranch => $library2->{branchcode}
158 branchcode => $library2->{branchcode}
161 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
163 # No userenv, PickupLibrary
164 t::lib::Mocks::mock_preference('IndependentBranches', '0');
165 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
167 C4::Context->preference('CircControl'),
169 'CircControl changed to PickupLibrary'
172 C4::Circulation::_GetCircControlBranch($item, $borrower),
173 $item->{$HomeOrHoldingBranch},
174 '_GetCircControlBranch returned item branch (no userenv defined)'
177 # No userenv, PatronLibrary
178 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
180 C4::Context->preference('CircControl'),
182 'CircControl changed to PatronLibrary'
185 C4::Circulation::_GetCircControlBranch($item, $borrower),
186 $borrower->{branchcode},
187 '_GetCircControlBranch returned borrower branch'
190 # No userenv, ItemHomeLibrary
191 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
193 C4::Context->preference('CircControl'),
195 'CircControl changed to ItemHomeLibrary'
198 $item->{$HomeOrHoldingBranch},
199 C4::Circulation::_GetCircControlBranch($item, $borrower),
200 '_GetCircControlBranch returned item branch'
204 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
205 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
207 # Userenv set, PickupLibrary
208 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
210 C4::Context->preference('CircControl'),
212 'CircControl changed to PickupLibrary'
215 C4::Circulation::_GetCircControlBranch($item, $borrower),
216 $library2->{branchcode},
217 '_GetCircControlBranch returned current branch'
220 # Userenv set, PatronLibrary
221 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
223 C4::Context->preference('CircControl'),
225 'CircControl changed to PatronLibrary'
228 C4::Circulation::_GetCircControlBranch($item, $borrower),
229 $borrower->{branchcode},
230 '_GetCircControlBranch returned borrower branch'
233 # Userenv set, ItemHomeLibrary
234 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
236 C4::Context->preference('CircControl'),
238 'CircControl changed to ItemHomeLibrary'
241 C4::Circulation::_GetCircControlBranch($item, $borrower),
242 $item->{$HomeOrHoldingBranch},
243 '_GetCircControlBranch returned item branch'
246 # Reset initial configuration
247 t::lib::Mocks::mock_preference('CircControl', $CircControl);
249 C4::Context->preference('CircControl'),
251 'CircControl reset to its initial value'
254 # Set a simple circ policy
255 $dbh->do('DELETE FROM issuingrules');
256 Koha::CirculationRules->search()->delete();
258 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
259 issuelength, lengthunit,
260 renewalsallowed, renewalperiod,
261 norenewalbefore, auto_renew,
278 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
280 # CanBookBeRenewed tests
281 C4::Context->set_preference('ItemsDeniedRenewal','');
282 # Generate test biblio
283 my $biblio = $builder->build_sample_biblio();
285 my $branch = $library2->{branchcode};
287 my $item_1 = $builder->build_sample_item(
289 biblionumber => $biblio->biblionumber,
291 replacementprice => 12.00,
295 $reused_itemnumber_1 = $item_1->itemnumber;
297 my $item_2 = $builder->build_sample_item(
299 biblionumber => $biblio->biblionumber,
301 replacementprice => 23.00,
305 $reused_itemnumber_2 = $item_2->itemnumber;
307 my $item_3 = $builder->build_sample_item(
309 biblionumber => $biblio->biblionumber,
311 replacementprice => 23.00,
317 my %renewing_borrower_data = (
319 surname => 'Renewal',
320 categorycode => $patron_category->{categorycode},
321 branchcode => $branch,
324 my %reserving_borrower_data = (
325 firstname => 'Katrin',
326 surname => 'Reservation',
327 categorycode => $patron_category->{categorycode},
328 branchcode => $branch,
331 my %hold_waiting_borrower_data = (
333 surname => 'Reservation',
334 categorycode => $patron_category->{categorycode},
335 branchcode => $branch,
338 my %restricted_borrower_data = (
339 firstname => 'Alice',
340 surname => 'Reservation',
341 categorycode => $patron_category->{categorycode},
342 debarred => '3228-01-01',
343 branchcode => $branch,
346 my %expired_borrower_data = (
349 categorycode => $patron_category->{categorycode},
350 branchcode => $branch,
351 dateexpiry => dt_from_string->subtract( months => 1 ),
354 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
355 my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
356 my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
357 my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
358 my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
360 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
361 my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
362 my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
369 my $checkitem = undef;
372 my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
373 my $datedue = dt_from_string( $issue->date_due() );
374 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
376 my $issue2 = AddIssue( $renewing_borrower, $item_2->barcode);
377 $datedue = dt_from_string( $issue->date_due() );
378 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
381 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $item_1->itemnumber } )->borrowernumber;
382 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
384 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
385 is( $renewokay, 1, 'Can renew, no holds for this title or item');
388 # Biblio-level hold, renewal test
390 $branch, $reserving_borrowernumber, $biblio->biblionumber,
391 $bibitems, $priority, $resdate, $expdate, $notes,
392 'a title', $checkitem, $found
395 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
396 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
397 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
398 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
399 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
400 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
401 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
403 # Now let's add an item level hold, we should no longer be able to renew the item
404 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
406 borrowernumber => $hold_waiting_borrowernumber,
407 biblionumber => $biblio->biblionumber,
408 itemnumber => $item_1->itemnumber,
409 branchcode => $branch,
413 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
414 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
417 # 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
418 # be able to renew these items
419 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
421 borrowernumber => $hold_waiting_borrowernumber,
422 biblionumber => $biblio->biblionumber,
423 itemnumber => $item_3->itemnumber,
424 branchcode => $branch,
429 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
430 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
431 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
432 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
433 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
435 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
436 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
437 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
439 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
440 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
441 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
443 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
444 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
445 AddIssue($reserving_borrower, $item_3->barcode);
446 my $reserve = $dbh->selectrow_hashref(
447 'SELECT * FROM old_reserves WHERE reserve_id = ?',
451 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
453 # Item-level hold, renewal test
455 $branch, $reserving_borrowernumber, $biblio->biblionumber,
456 $bibitems, $priority, $resdate, $expdate, $notes,
457 'a title', $item_1->itemnumber, $found
460 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
461 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
462 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
464 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber, 1);
465 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
467 # Items can't fill hold for reasons
468 ModItem({ notforloan => 1 }, $biblio->biblionumber, $item_1->itemnumber);
469 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
470 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
471 ModItem({ notforloan => 0, itype => $itemtype }, $biblio->biblionumber, $item_1->itemnumber);
473 # FIXME: Add more for itemtype not for loan etc.
475 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
476 my $item_5 = $builder->build_sample_item(
478 biblionumber => $biblio->biblionumber,
480 replacementprice => 23.00,
484 my $datedue5 = AddIssue($restricted_borrower, $item_5->barcode);
485 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
487 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
488 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
489 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
490 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $item_5->itemnumber);
491 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
493 # Users cannot renew an overdue item
494 my $item_6 = $builder->build_sample_item(
496 biblionumber => $biblio->biblionumber,
498 replacementprice => 23.00,
503 my $item_7 = $builder->build_sample_item(
505 biblionumber => $biblio->biblionumber,
507 replacementprice => 23.00,
512 my $datedue6 = AddIssue( $renewing_borrower, $item_6->barcode);
513 is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
515 my $now = dt_from_string();
516 my $five_weeks = DateTime::Duration->new(weeks => 5);
517 my $five_weeks_ago = $now - $five_weeks;
518 t::lib::Mocks::mock_preference('finesMode', 'production');
520 my $passeddatedue1 = AddIssue($renewing_borrower, $item_7->barcode, $five_weeks_ago);
521 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
523 my ( $fine ) = CalcFine( $item_7->unblessed, $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
524 C4::Overdues::UpdateFine(
526 issue_id => $passeddatedue1->id(),
527 itemnumber => $item_7->itemnumber,
528 borrowernumber => $renewing_borrower->{borrowernumber},
530 due => Koha::DateUtils::output_pref($five_weeks_ago)
534 t::lib::Mocks::mock_preference('RenewalLog', 0);
535 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
536 my %params_renewal = (
537 timestamp => { -like => $date . "%" },
538 module => "CIRCULATION",
542 timestamp => { -like => $date . "%" },
543 module => "CIRCULATION",
546 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
547 my $dt = dt_from_string();
548 Time::Fake->offset( $dt->epoch );
549 my $datedue1 = AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
550 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
551 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
552 isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
555 t::lib::Mocks::mock_preference('RenewalLog', 1);
556 $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
557 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
558 AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
559 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
560 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
562 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
563 is( $fines->count, 2 );
564 isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
565 isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
569 my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
570 my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
571 AddIssue( $renewing_borrower,$item_7->barcode,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
572 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
573 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
574 $new_log_size = Koha::ActionLogs->count( \%params_issue );
575 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
577 $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
580 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
581 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
582 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
583 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
584 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
587 $hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next;
591 # Test automatic renewal before value for "norenewalbefore" in policy is set
592 # In this case automatic renewal is not permitted prior to due date
593 my $item_4 = $builder->build_sample_item(
595 biblionumber => $biblio->biblionumber,
597 replacementprice => 16.00,
602 $issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
603 ( $renewokay, $error ) =
604 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
605 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
606 is( $error, 'auto_too_soon',
607 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
610 # Test premature manual renewal
611 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
613 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
614 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
615 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
618 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
619 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
621 GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
622 $datedue->clone->add( days => -7 ),
623 'Bug 14395: Renewals permitted 7 days before due date, as expected'
627 # Test 'date' setting for syspref NoRenewalBeforePrecision
628 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
630 GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
631 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
632 'Bug 14395: Renewals permitted 7 days before due date, as expected'
636 # Test premature automatic renewal
637 ( $renewokay, $error ) =
638 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
639 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
640 is( $error, 'auto_too_soon',
641 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
644 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
645 # and test automatic renewal again
646 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
647 ( $renewokay, $error ) =
648 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
649 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
650 is( $error, 'auto_too_soon',
651 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
654 # Change policy so that loans can be renewed 99 days prior to the due date
655 # and test automatic renewal again
656 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
657 ( $renewokay, $error ) =
658 CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
659 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
660 is( $error, 'auto_renew',
661 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
664 subtest "too_late_renewal / no_auto_renewal_after" => sub {
666 my $item_to_auto_renew = $builder->build(
669 biblionumber => $biblio->biblionumber,
670 homebranch => $branch,
671 holdingbranch => $branch,
676 my $ten_days_before = dt_from_string->add( days => -10 );
677 my $ten_days_ahead = dt_from_string->add( days => 10 );
678 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
680 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
681 ( $renewokay, $error ) =
682 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
683 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
684 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
686 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
687 ( $renewokay, $error ) =
688 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
689 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
690 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
692 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
693 ( $renewokay, $error ) =
694 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
695 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
696 is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
698 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
699 ( $renewokay, $error ) =
700 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
701 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
702 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
704 $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 ) );
705 ( $renewokay, $error ) =
706 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
707 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
708 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
710 $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 ) );
711 ( $renewokay, $error ) =
712 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
713 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
714 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
716 $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 ) );
717 ( $renewokay, $error ) =
718 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
719 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
720 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
723 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
725 my $item_to_auto_renew = $builder->build({
728 biblionumber => $biblio->biblionumber,
729 homebranch => $branch,
730 holdingbranch => $branch,
734 my $ten_days_before = dt_from_string->add( days => -10 );
735 my $ten_days_ahead = dt_from_string->add( days => 10 );
736 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
738 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
739 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
740 C4::Context->set_preference('OPACFineNoRenewals','10');
741 my $fines_amount = 5;
742 my $account = Koha::Account->new({patron_id => $renewing_borrowernumber});
745 amount => $fines_amount,
748 item_id => $item_to_auto_renew->{itemnumber},
749 description => "Some fines"
751 )->status('RETURNED')->store;
752 ( $renewokay, $error ) =
753 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
754 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
755 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
759 amount => $fines_amount,
762 item_id => $item_to_auto_renew->{itemnumber},
763 description => "Some fines"
765 )->status('RETURNED')->store;
766 ( $renewokay, $error ) =
767 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
768 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
769 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
773 amount => $fines_amount,
776 item_id => $item_to_auto_renew->{itemnumber},
777 description => "Some fines"
779 )->status('RETURNED')->store;
780 ( $renewokay, $error ) =
781 CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
782 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
783 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
785 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
788 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
790 my $item_to_auto_renew = $builder->build({
793 biblionumber => $biblio->biblionumber,
794 homebranch => $branch,
795 holdingbranch => $branch,
799 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
801 my $ten_days_before = dt_from_string->add( days => -10 );
802 my $ten_days_ahead = dt_from_string->add( days => 10 );
804 # Patron is expired and BlockExpiredPatronOpacActions=0
805 # => auto renew is allowed
806 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
807 my $patron = $expired_borrower;
808 my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
809 ( $renewokay, $error ) =
810 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
811 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
812 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
813 Koha::Checkouts->find( $checkout->issue_id )->delete;
816 # Patron is expired and BlockExpiredPatronOpacActions=1
817 # => auto renew is not allowed
818 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
819 $patron = $expired_borrower;
820 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
821 ( $renewokay, $error ) =
822 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
823 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
824 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
825 Koha::Checkouts->find( $checkout->issue_id )->delete;
828 # Patron is not expired and BlockExpiredPatronOpacActions=1
829 # => auto renew is allowed
830 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
831 $patron = $renewing_borrower;
832 $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
833 ( $renewokay, $error ) =
834 CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
835 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
836 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
837 Koha::Checkouts->find( $checkout->issue_id )->delete;
840 subtest "GetLatestAutoRenewDate" => sub {
842 my $item_to_auto_renew = $builder->build(
845 biblionumber => $biblio->biblionumber,
846 homebranch => $branch,
847 holdingbranch => $branch,
852 my $ten_days_before = dt_from_string->add( days => -10 );
853 my $ten_days_ahead = dt_from_string->add( days => 10 );
854 AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
855 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
856 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
857 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' );
858 my $five_days_before = dt_from_string->add( days => -5 );
859 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
860 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
861 is( $latest_auto_renew_date->truncate( to => 'minute' ),
862 $five_days_before->truncate( to => 'minute' ),
863 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
865 my $five_days_ahead = dt_from_string->add( days => 5 );
866 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
867 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
868 is( $latest_auto_renew_date->truncate( to => 'minute' ),
869 $five_days_ahead->truncate( to => 'minute' ),
870 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
872 my $two_days_ahead = dt_from_string->add( days => 2 );
873 $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 ) );
874 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
875 is( $latest_auto_renew_date->truncate( to => 'day' ),
876 $two_days_ahead->truncate( to => 'day' ),
877 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
879 $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 ) );
880 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
881 is( $latest_auto_renew_date->truncate( to => 'day' ),
882 $two_days_ahead->truncate( to => 'day' ),
883 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
890 # set policy to forbid renewals
891 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
893 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
894 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
895 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
897 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
898 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
899 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
901 C4::Overdues::UpdateFine(
903 issue_id => $issue->id(),
904 itemnumber => $item_1->itemnumber,
905 borrowernumber => $renewing_borrower->{borrowernumber},
908 due => Koha::DateUtils::output_pref($datedue)
912 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
913 is( $line->accounttype, 'OVERDUE', 'Account line type is OVERDUE' );
914 is( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
915 is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
916 is( $line->amount, '15.000000', 'Account line amount is 15.00' );
917 is( $line->issue_id, $issue->id, 'Account line issue id matches' );
919 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
920 is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
921 is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
923 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
924 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
926 LostItem( $item_1->itemnumber, 'test', 1 );
928 $line = Koha::Account::Lines->find($line->id);
929 is( $line->accounttype, 'OVERDUE', 'Account type remains as OVERDUE' );
930 isnt( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
932 my $item = Koha::Items->find($item_1->itemnumber);
933 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
934 my $checkout = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber });
935 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
937 my $total_due = $dbh->selectrow_array(
938 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
939 undef, $renewing_borrower->{borrowernumber}
942 is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
944 C4::Context->dbh->do("DELETE FROM accountlines");
946 C4::Overdues::UpdateFine(
948 issue_id => $issue2->id(),
949 itemnumber => $item_2->itemnumber,
950 borrowernumber => $renewing_borrower->{borrowernumber},
953 due => Koha::DateUtils::output_pref($datedue)
957 LostItem( $item_2->itemnumber, 'test', 0 );
959 my $item2 = Koha::Items->find($item_2->itemnumber);
960 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
961 ok( Koha::Checkouts->find({ itemnumber => $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
963 $total_due = $dbh->selectrow_array(
964 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
965 undef, $renewing_borrower->{borrowernumber}
968 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
970 my $future = dt_from_string();
971 $future->add( days => 7 );
972 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
973 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
975 # Users cannot renew any item if there is an overdue item
976 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
977 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
978 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
979 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
980 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
982 my $manager = $builder->build_object({ class => "Koha::Patrons" });
983 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
984 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
985 $checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
986 LostItem( $item_3->itemnumber, 'test', 0 );
987 my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
988 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
990 $accountline->description,
992 $item_3->biblio->title || '',
993 $item_3->barcode || '',
994 $item_3->itemcallnumber || '' ),
995 "Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
1000 # GetUpcomingDueIssues tests
1001 my $branch = $library2->{branchcode};
1003 #Create another record
1004 my $biblio2 = $builder->build_sample_biblio();
1007 my $item_1 = Koha::Items->find($reused_itemnumber_1);
1008 my $item_2 = Koha::Items->find($reused_itemnumber_2);
1009 my $item_3 = $builder->build_sample_item(
1011 biblionumber => $biblio2->biblionumber,
1019 my %a_borrower_data = (
1020 firstname => 'Fridolyn',
1021 surname => 'SOMERS',
1022 categorycode => $patron_category->{categorycode},
1023 branchcode => $branch,
1026 my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1027 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
1029 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
1030 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
1031 my $today = DateTime->today(time_zone => C4::Context->tz());
1033 my $issue = AddIssue( $a_borrower, $item_1->barcode, $yesterday );
1034 my $datedue = dt_from_string( $issue->date_due() );
1035 my $issue2 = AddIssue( $a_borrower, $item_2->barcode, $two_days_ahead );
1036 my $datedue2 = dt_from_string( $issue->date_due() );
1040 # GetUpcomingDueIssues tests
1042 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1043 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
1046 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
1047 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1048 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
1051 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1052 is ( scalar( @$upcoming_dues ), 1,
1053 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
1056 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
1058 my $issue3 = AddIssue( $a_borrower, $item_3->barcode, $today );
1060 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
1061 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
1063 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
1064 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
1066 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
1067 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
1069 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1070 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1072 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
1073 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1075 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
1076 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1081 my $branch = $library2->{branchcode};
1083 my $biblio = $builder->build_sample_biblio();
1086 my $item = $builder->build_sample_item(
1088 biblionumber => $biblio->biblionumber,
1095 my %a_borrower_data = (
1096 firstname => 'Kyle',
1098 categorycode => $patron_category->{categorycode},
1099 branchcode => $branch,
1102 my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1104 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1105 my $issue = AddIssue( $borrower, $item->barcode );
1108 issue_id => $issue->id(),
1109 itemnumber => $item->itemnumber,
1110 borrowernumber => $borrowernumber,
1116 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1117 my $count = $hr->{count};
1119 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1123 $dbh->do('DELETE FROM issues');
1124 $dbh->do('DELETE FROM items');
1125 $dbh->do('DELETE FROM issuingrules');
1126 Koha::CirculationRules->search()->delete();
1129 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
1130 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1139 Koha::CirculationRules->set_rules(
1141 categorycode => '*',
1149 my $biblio = $builder->build_sample_biblio();
1151 my $item_1 = $builder->build_sample_item(
1153 biblionumber => $biblio->biblionumber,
1154 library => $library2->{branchcode},
1159 my $item_2= $builder->build_sample_item(
1161 biblionumber => $biblio->biblionumber,
1162 library => $library2->{branchcode},
1167 my $borrowernumber1 = Koha::Patron->new({
1168 firstname => 'Kyle',
1170 categorycode => $patron_category->{categorycode},
1171 branchcode => $library2->{branchcode},
1172 })->store->borrowernumber;
1173 my $borrowernumber2 = Koha::Patron->new({
1174 firstname => 'Chelsea',
1176 categorycode => $patron_category->{categorycode},
1177 branchcode => $library2->{branchcode},
1178 })->store->borrowernumber;
1180 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1181 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1183 my $issue = AddIssue( $borrower1, $item_1->barcode );
1185 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1186 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1189 $library2->{branchcode}, $borrowernumber2, $biblio->biblionumber,
1190 '', 1, undef, undef, '',
1194 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1195 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1196 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1197 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1199 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1200 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1201 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1202 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1204 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1205 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1206 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1207 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1209 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1210 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1211 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1212 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1214 # Setting item not checked out to be not for loan but holdable
1215 ModItem({ notforloan => -1 }, $biblio->biblionumber, $item_2->itemnumber);
1217 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1218 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' );
1222 # Don't allow renewing onsite checkout
1223 my $branch = $library->{branchcode};
1225 #Create another record
1226 my $biblio = $builder->build_sample_biblio();
1228 my $item = $builder->build_sample_item(
1230 biblionumber => $biblio->biblionumber,
1236 my $borrowernumber = Koha::Patron->new({
1239 categorycode => $patron_category->{categorycode},
1240 branchcode => $branch,
1241 })->store->borrowernumber;
1243 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1245 my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1246 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $item->itemnumber );
1247 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1248 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1252 my $library = $builder->build({ source => 'Branch' });
1254 my $biblio = $builder->build_sample_biblio();
1256 my $item = $builder->build_sample_item(
1258 biblionumber => $biblio->biblionumber,
1259 library => $library->{branchcode},
1264 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1266 my $issue = AddIssue( $patron, $item->barcode );
1269 issue_id => $issue->id(),
1270 itemnumber => $item->itemnumber,
1271 borrowernumber => $patron->{borrowernumber},
1278 issue_id => $issue->id(),
1279 itemnumber => $item->itemnumber,
1280 borrowernumber => $patron->{borrowernumber},
1285 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1288 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1291 my $homebranch = $builder->build( { source => 'Branch' } );
1292 my $holdingbranch = $builder->build( { source => 'Branch' } );
1293 my $otherbranch = $builder->build( { source => 'Branch' } );
1294 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1295 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1297 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1298 my $item = $builder->build(
1301 homebranch => $homebranch->{branchcode},
1302 holdingbranch => $holdingbranch->{branchcode},
1303 biblionumber => $biblioitem->{biblionumber}
1308 set_userenv($holdingbranch);
1310 my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1311 is( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
1313 my ( $error, $question, $alerts );
1315 # AllowReturnToBranch == anywhere
1316 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1317 ## Test that unknown barcodes don't generate internal server errors
1318 set_userenv($homebranch);
1319 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1320 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1321 ## Can be issued from homebranch
1322 set_userenv($homebranch);
1323 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1324 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1325 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1326 ## Can be issued from holdingbranch
1327 set_userenv($holdingbranch);
1328 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1329 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1330 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1331 ## Can be issued from another branch
1332 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1333 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1334 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1336 # AllowReturnToBranch == holdingbranch
1337 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1338 ## Cannot be issued from homebranch
1339 set_userenv($homebranch);
1340 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1341 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1342 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1343 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1344 ## Can be issued from holdinbranch
1345 set_userenv($holdingbranch);
1346 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1347 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1348 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1349 ## Cannot be issued from another branch
1350 set_userenv($otherbranch);
1351 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1352 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1353 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1354 is( $error->{branch_to_return}, $holdingbranch->{branchcode} );
1356 # AllowReturnToBranch == homebranch
1357 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1358 ## Can be issued from holdinbranch
1359 set_userenv($homebranch);
1360 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1361 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1362 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1363 ## Cannot be issued from holdinbranch
1364 set_userenv($holdingbranch);
1365 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1366 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1367 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1368 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1369 ## Cannot be issued from holdinbranch
1370 set_userenv($otherbranch);
1371 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1372 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1373 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1374 is( $error->{branch_to_return}, $homebranch->{branchcode} );
1376 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1379 subtest 'AddIssue & AllowReturnToBranch' => sub {
1382 my $homebranch = $builder->build( { source => 'Branch' } );
1383 my $holdingbranch = $builder->build( { source => 'Branch' } );
1384 my $otherbranch = $builder->build( { source => 'Branch' } );
1385 my $patron_1 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1386 my $patron_2 = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1388 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1389 my $item = $builder->build(
1392 homebranch => $homebranch->{branchcode},
1393 holdingbranch => $holdingbranch->{branchcode},
1397 biblionumber => $biblioitem->{biblionumber}
1402 set_userenv($holdingbranch);
1404 my $ref_issue = 'Koha::Checkout';
1405 my $issue = AddIssue( $patron_1, $item->{barcode} );
1407 my ( $error, $question, $alerts );
1409 # AllowReturnToBranch == homebranch
1410 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1411 ## Can be issued from homebranch
1412 set_userenv($homebranch);
1413 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1414 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1415 ## Can be issued from holdinbranch
1416 set_userenv($holdingbranch);
1417 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1418 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1419 ## Can be issued from another branch
1420 set_userenv($otherbranch);
1421 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1422 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1424 # AllowReturnToBranch == holdinbranch
1425 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1426 ## Cannot be issued from homebranch
1427 set_userenv($homebranch);
1428 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1429 ## Can be issued from holdingbranch
1430 set_userenv($holdingbranch);
1431 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1432 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1433 ## Cannot be issued from another branch
1434 set_userenv($otherbranch);
1435 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1437 # AllowReturnToBranch == homebranch
1438 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1439 ## Can be issued from homebranch
1440 set_userenv($homebranch);
1441 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1442 set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1443 ## Cannot be issued from holdinbranch
1444 set_userenv($holdingbranch);
1445 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1446 ## Cannot be issued from another branch
1447 set_userenv($otherbranch);
1448 is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1449 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1452 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1455 my $library = $builder->build( { source => 'Branch' } );
1456 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1458 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1459 my $item_1 = $builder->build(
1462 homebranch => $library->{branchcode},
1463 holdingbranch => $library->{branchcode},
1464 biblionumber => $biblioitem_1->{biblionumber}
1468 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1469 my $item_2 = $builder->build(
1472 homebranch => $library->{branchcode},
1473 holdingbranch => $library->{branchcode},
1474 biblionumber => $biblioitem_2->{biblionumber}
1479 my ( $error, $question, $alerts );
1481 # Patron cannot issue item_1, they have overdues
1482 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1483 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday ); # Add an overdue
1485 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1486 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1487 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
1488 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1490 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1491 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1492 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1493 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1495 # Patron cannot issue item_1, they are debarred
1496 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1497 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1498 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1499 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1500 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1502 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1503 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1504 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
1505 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1508 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1511 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1512 my $patron_category_x = $builder->build_object(
1514 class => 'Koha::Patron::Categories',
1515 value => { category_type => 'X' }
1518 my $patron = $builder->build_object(
1520 class => 'Koha::Patrons',
1522 categorycode => $patron_category_x->categorycode,
1523 gonenoaddress => undef,
1530 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1531 my $item_1 = $builder->build(
1535 homebranch => $library->branchcode,
1536 holdingbranch => $library->branchcode,
1537 biblionumber => $biblioitem_1->{biblionumber}
1542 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1543 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1545 # TODO There are other tests to provide here
1548 subtest 'MultipleReserves' => sub {
1551 my $biblio = $builder->build_sample_biblio();
1553 my $branch = $library2->{branchcode};
1555 my $item_1 = $builder->build_sample_item(
1557 biblionumber => $biblio->biblionumber,
1559 replacementprice => 12.00,
1564 my $item_2 = $builder->build_sample_item(
1566 biblionumber => $biblio->biblionumber,
1568 replacementprice => 12.00,
1575 my $resdate = undef;
1576 my $expdate = undef;
1578 my $checkitem = undef;
1581 my %renewing_borrower_data = (
1582 firstname => 'John',
1583 surname => 'Renewal',
1584 categorycode => $patron_category->{categorycode},
1585 branchcode => $branch,
1587 my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
1588 my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1589 my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
1590 my $datedue = dt_from_string( $issue->date_due() );
1591 is (defined $issue->date_due(), 1, "item 1 checked out");
1592 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber })->borrowernumber;
1594 my %reserving_borrower_data1 = (
1595 firstname => 'Katrin',
1596 surname => 'Reservation',
1597 categorycode => $patron_category->{categorycode},
1598 branchcode => $branch,
1600 my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
1602 $branch, $reserving_borrowernumber1, $biblio->biblionumber,
1603 $bibitems, $priority, $resdate, $expdate, $notes,
1604 'a title', $checkitem, $found
1607 my %reserving_borrower_data2 = (
1608 firstname => 'Kirk',
1609 surname => 'Reservation',
1610 categorycode => $patron_category->{categorycode},
1611 branchcode => $branch,
1613 my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
1615 $branch, $reserving_borrowernumber2, $biblio->biblionumber,
1616 $bibitems, $priority, $resdate, $expdate, $notes,
1617 'a title', $checkitem, $found
1621 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1622 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1625 my $item_3 = $builder->build_sample_item(
1627 biblionumber => $biblio->biblionumber,
1629 replacementprice => 12.00,
1635 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1636 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1640 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1643 my $library = $builder->build( { source => 'Branch' } );
1644 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1646 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1647 my $biblionumber = $biblioitem->{biblionumber};
1648 my $item_1 = $builder->build(
1651 homebranch => $library->{branchcode},
1652 holdingbranch => $library->{branchcode},
1653 biblionumber => $biblionumber,
1657 my $item_2 = $builder->build(
1660 homebranch => $library->{branchcode},
1661 holdingbranch => $library->{branchcode},
1662 biblionumber => $biblionumber,
1667 my ( $error, $question, $alerts );
1668 my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1670 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1671 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1672 is( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1673 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) );
1675 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1676 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1677 is( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1679 # Add a subscription
1680 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1682 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1683 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1684 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) );
1686 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1687 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1688 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) );
1691 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1694 my $library = $builder->build( { source => 'Branch' } );
1695 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1698 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1699 my $item_1 = $builder->build(
1703 homebranch => $library->{branchcode},
1704 holdingbranch => $library->{branchcode},
1708 biblionumber => $biblioitem_1->{biblionumber}
1712 my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1713 my $item_2 = $builder->build(
1717 homebranch => $library->{branchcode},
1718 holdingbranch => $library->{branchcode},
1722 biblionumber => $biblioitem_2->{biblionumber}
1727 # And the issuing rule
1728 Koha::IssuingRules->search->delete;
1729 my $rule = Koha::IssuingRule->new(
1731 categorycode => '*',
1735 firstremind => 1, # 1 day of grace
1736 finedays => 2, # 2 days of fine per day of overdue
1737 lengthunit => 'days',
1742 # Patron cannot issue item_1, they have overdues
1743 my $five_days_ago = dt_from_string->subtract( days => 5 );
1744 my $ten_days_ago = dt_from_string->subtract( days => 10 );
1745 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1746 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1747 ; # Add another overdue
1749 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1750 AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1751 my $debarments = Koha::Patron::Debarments::GetDebarments(
1752 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1753 is( scalar(@$debarments), 1 );
1755 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1756 # Same for the others
1757 my $expected_expiration = output_pref(
1759 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1760 dateformat => 'sql',
1764 is( $debarments->[0]->{expiration}, $expected_expiration );
1766 AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1767 $debarments = Koha::Patron::Debarments::GetDebarments(
1768 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1769 is( scalar(@$debarments), 1 );
1770 $expected_expiration = output_pref(
1772 dt => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1773 dateformat => 'sql',
1777 is( $debarments->[0]->{expiration}, $expected_expiration );
1779 Koha::Patron::Debarments::DelUniqueDebarment(
1780 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1782 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1783 AddIssue( $patron, $item_1->{barcode}, $five_days_ago ); # Add an overdue
1784 AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1785 ; # Add another overdue
1786 AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1787 $debarments = Koha::Patron::Debarments::GetDebarments(
1788 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1789 is( scalar(@$debarments), 1 );
1790 $expected_expiration = output_pref(
1792 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1793 dateformat => 'sql',
1797 is( $debarments->[0]->{expiration}, $expected_expiration );
1799 AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1800 $debarments = Koha::Patron::Debarments::GetDebarments(
1801 { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1802 is( scalar(@$debarments), 1 );
1803 $expected_expiration = output_pref(
1805 dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1806 dateformat => 'sql',
1810 is( $debarments->[0]->{expiration}, $expected_expiration );
1813 subtest 'AddReturn + suspension_chargeperiod' => sub {
1816 my $library = $builder->build( { source => 'Branch' } );
1817 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1820 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1821 my $item_1 = $builder->build(
1825 homebranch => $library->{branchcode},
1826 holdingbranch => $library->{branchcode},
1830 biblionumber => $biblioitem_1->{biblionumber}
1835 # And the issuing rule
1836 Koha::IssuingRules->search->delete;
1837 my $rule = Koha::IssuingRule->new(
1839 categorycode => '*',
1843 firstremind => 0, # 0 day of grace
1844 finedays => 2, # 2 days of fine per day of overdue
1845 suspension_chargeperiod => 1,
1846 lengthunit => 'days',
1851 my $five_days_ago = dt_from_string->subtract( days => 5 );
1852 # We want to charge 2 days every day, without grace
1853 # With 5 days of overdue: 5 * Z
1854 my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1855 test_debarment_on_checkout(
1858 library => $library,
1860 due_date => $five_days_ago,
1861 expiration_date => $expected_expiration,
1865 # We want to charge 2 days every 2 days, without grace
1866 # With 5 days of overdue: (5 * 2) / 2
1867 $rule->suspension_chargeperiod(2)->store;
1868 $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1869 test_debarment_on_checkout(
1872 library => $library,
1874 due_date => $five_days_ago,
1875 expiration_date => $expected_expiration,
1879 # We want to charge 2 days every 3 days, with 1 day of grace
1880 # With 5 days of overdue: ((5-1) / 3 ) * 2
1881 $rule->suspension_chargeperiod(3)->store;
1882 $rule->firstremind(1)->store;
1883 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1884 test_debarment_on_checkout(
1887 library => $library,
1889 due_date => $five_days_ago,
1890 expiration_date => $expected_expiration,
1894 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1895 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1896 $rule->finedays(2)->store;
1897 $rule->suspension_chargeperiod(1)->store;
1898 $rule->firstremind(0)->store;
1899 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1901 # Adding a holiday 2 days ago
1902 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1903 my $two_days_ago = dt_from_string->subtract( days => 2 );
1904 $calendar->insert_single_holiday(
1905 day => $two_days_ago->day,
1906 month => $two_days_ago->month,
1907 year => $two_days_ago->year,
1908 title => 'holidayTest-2d',
1909 description => 'holidayDesc 2 days ago'
1911 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1912 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1913 test_debarment_on_checkout(
1916 library => $library,
1918 due_date => $five_days_ago,
1919 expiration_date => $expected_expiration,
1923 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1924 my $two_days_ahead = dt_from_string->add( days => 2 );
1925 $calendar->insert_single_holiday(
1926 day => $two_days_ahead->day,
1927 month => $two_days_ahead->month,
1928 year => $two_days_ahead->year,
1929 title => 'holidayTest+2d',
1930 description => 'holidayDesc 2 days ahead'
1933 # Same as above, but we should skip D+2
1934 $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1935 test_debarment_on_checkout(
1938 library => $library,
1940 due_date => $five_days_ago,
1941 expiration_date => $expected_expiration,
1945 # Adding another holiday, day of expiration date
1946 my $expected_expiration_dt = dt_from_string($expected_expiration);
1947 $calendar->insert_single_holiday(
1948 day => $expected_expiration_dt->day,
1949 month => $expected_expiration_dt->month,
1950 year => $expected_expiration_dt->year,
1951 title => 'holidayTest_exp',
1952 description => 'holidayDesc on expiration date'
1954 # Expiration date will be the day after
1955 test_debarment_on_checkout(
1958 library => $library,
1960 due_date => $five_days_ago,
1961 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1965 test_debarment_on_checkout(
1968 library => $library,
1970 return_date => dt_from_string->add(days => 5),
1971 expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1976 subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
1979 my $library = $builder->build( { source => 'Branch' } );
1980 my $patron1 = $builder->build_object(
1982 class => 'Koha::Patrons',
1984 branchcode => $library->{branchcode},
1985 firstname => "Happy",
1986 surname => "Gilmore",
1990 my $patron2 = $builder->build_object(
1992 class => 'Koha::Patrons',
1994 branchcode => $library->{branchcode},
1995 firstname => "Billy",
1996 surname => "Madison",
2001 C4::Context->_new_userenv('xxx');
2002 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Random Library', '', '', '');
2004 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2005 my $biblionumber = $biblioitem->{biblionumber};
2006 my $item = $builder->build(
2009 homebranch => $library->{branchcode},
2010 holdingbranch => $library->{branchcode},
2014 biblionumber => $biblionumber,
2019 my ( $error, $question, $alerts );
2020 my $issue = AddIssue( $patron1->unblessed, $item->{barcode} );
2022 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2023 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
2024 is( $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER question flag should be set if AutoReturnCheckedOutItems is disabled and item is checked out to another' );
2026 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 1);
2027 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
2028 is( $alerts->{RETURNED_FROM_ANOTHER}->{patron}->borrowernumber, $patron1->borrowernumber, 'RETURNED_FROM_ANOTHER alert flag should be set if AutoReturnCheckedOutItems is enabled and item is checked out to another' );
2030 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2034 subtest 'AddReturn | is_overdue' => sub {
2037 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
2038 t::lib::Mocks::mock_preference('finesMode', 'production');
2039 t::lib::Mocks::mock_preference('MaxFine', '100');
2041 my $library = $builder->build( { source => 'Branch' } );
2042 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
2043 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2044 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2046 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2047 my $item_type = $builder->build_object(
2048 { class => 'Koha::ItemTypes',
2050 notforloan => undef,
2052 defaultreplacecost => undef,
2054 rentalcharge_daily => 0,
2058 my $item = $builder->build(
2062 homebranch => $library->{branchcode},
2063 holdingbranch => $library->{branchcode},
2067 biblionumber => $biblioitem->{biblionumber},
2068 replacementprice => 7,
2069 itype => $item_type->itemtype
2074 Koha::IssuingRules->search->delete;
2075 my $rule = Koha::IssuingRule->new(
2077 categorycode => '*',
2081 lengthunit => 'days',
2082 fine => 1, # Charge 1 every day of overdue
2088 my $now = dt_from_string;
2089 my $one_day_ago = dt_from_string->subtract( days => 1 );
2090 my $five_days_ago = dt_from_string->subtract( days => 5 );
2091 my $ten_days_ago = dt_from_string->subtract( days => 10 );
2092 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
2094 # No return date specified, today will be used => 10 days overdue charged
2095 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2096 AddReturn( $item->{barcode}, $library->{branchcode} );
2097 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
2098 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2100 # specify return date 5 days before => no overdue charged
2101 AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
2102 AddReturn( $item->{barcode}, $library->{branchcode}, undef, $ten_days_ago );
2103 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2104 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2106 # specify return date 5 days later => 5 days overdue charged
2107 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2108 AddReturn( $item->{barcode}, $library->{branchcode}, undef, $five_days_ago );
2109 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
2110 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2112 # specify return date 5 days later, specify exemptfine => no overdue charge
2113 AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2114 AddReturn( $item->{barcode}, $library->{branchcode}, 1, $five_days_ago );
2115 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2116 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2118 subtest 'bug 22877' => sub {
2122 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2124 # Fake fines cronjob on this checkout
2126 CalcFine( $item, $patron->categorycode, $library->{branchcode},
2127 $ten_days_ago, $now );
2130 issue_id => $issue->issue_id,
2131 itemnumber => $item->{itemnumber},
2132 borrowernumber => $patron->borrowernumber,
2134 due => output_pref($ten_days_ago)
2137 is( int( $patron->account->balance() ),
2138 10, "Overdue fine of 10 days overdue" );
2140 # Fake longoverdue with charge and not marking returned
2141 LostItem( $item->{itemnumber}, 'cronjob', 0 );
2142 is( int( $patron->account->balance() ),
2143 17, "Lost fine of 7 plus 10 days overdue" );
2145 # Now we return it today
2146 AddReturn( $item->{barcode}, $library->{branchcode} );
2147 is( int( $patron->account->balance() ),
2148 17, "Should have a single 10 days overdue fine and lost charge" );
2152 subtest '_FixAccountForLostAndReturned' => sub {
2156 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2157 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
2159 my $processfee_amount = 20;
2160 my $replacement_amount = 99.00;
2161 my $item_type = $builder->build_object(
2162 { class => 'Koha::ItemTypes',
2164 notforloan => undef,
2166 defaultreplacecost => undef,
2167 processfee => $processfee_amount,
2168 rentalcharge_daily => 0,
2172 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2174 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Daria' });
2176 subtest 'Full write-off tests' => sub {
2180 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2181 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2182 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
2184 my $item = $builder->build_sample_item(
2186 biblionumber => $biblio->biblionumber,
2187 library => $library->branchcode,
2188 replacementprice => $replacement_amount,
2189 itype => $item_type->itemtype,
2193 AddIssue( $patron->unblessed, $item->barcode );
2195 # Simulate item marked as lost
2196 ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2197 LostItem( $item->itemnumber, 1 );
2199 my $processing_fee_lines = Koha::Account::Lines->search(
2200 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2201 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2202 my $processing_fee_line = $processing_fee_lines->next;
2203 is( $processing_fee_line->amount + 0,
2204 $processfee_amount, 'The right PF amount is generated' );
2205 is( $processing_fee_line->amountoutstanding + 0,
2206 $processfee_amount, 'The right PF amountoutstanding is generated' );
2208 my $lost_fee_lines = Koha::Account::Lines->search(
2209 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2210 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2211 my $lost_fee_line = $lost_fee_lines->next;
2212 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2213 is( $lost_fee_line->amountoutstanding + 0,
2214 $replacement_amount, 'The right L amountoutstanding is generated' );
2216 my $account = $patron->account;
2217 my $debts = $account->outstanding_debits;
2219 # Write off the debt
2220 my $credit = $account->add_credit(
2221 { amount => $account->balance,
2223 interface => 'test',
2226 $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2228 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2229 is( $credit_return_id, undef, 'No CR account line added' );
2231 $lost_fee_line->discard_changes; # reload from DB
2232 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2233 is( $lost_fee_line->accounttype,
2234 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2236 is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2239 subtest 'Full payment tests' => sub {
2243 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2245 my $item = $builder->build_sample_item(
2247 biblionumber => $biblio->biblionumber,
2248 library => $library->branchcode,
2249 replacementprice => $replacement_amount,
2250 itype => $item_type->itemtype
2254 AddIssue( $patron->unblessed, $item->barcode );
2256 # Simulate item marked as lost
2257 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2258 LostItem( $item->itemnumber, 1 );
2260 my $processing_fee_lines = Koha::Account::Lines->search(
2261 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2262 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2263 my $processing_fee_line = $processing_fee_lines->next;
2264 is( $processing_fee_line->amount + 0,
2265 $processfee_amount, 'The right PF amount is generated' );
2266 is( $processing_fee_line->amountoutstanding + 0,
2267 $processfee_amount, 'The right PF amountoutstanding is generated' );
2269 my $lost_fee_lines = Koha::Account::Lines->search(
2270 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2271 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2272 my $lost_fee_line = $lost_fee_lines->next;
2273 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2274 is( $lost_fee_line->amountoutstanding + 0,
2275 $replacement_amount, 'The right L amountountstanding is generated' );
2277 my $account = $patron->account;
2278 my $debts = $account->outstanding_debits;
2280 # Write off the debt
2281 my $credit = $account->add_credit(
2282 { amount => $account->balance,
2284 interface => 'test',
2287 $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2289 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2290 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2292 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2293 is( $credit_return->amount + 0,
2294 -99.00, 'The account line of type CR has an amount of -99' );
2295 is( $credit_return->amountoutstanding + 0,
2296 -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2298 $lost_fee_line->discard_changes;
2299 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2300 is( $lost_fee_line->accounttype,
2301 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2303 is( $patron->account->balance,
2304 -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2307 subtest 'Test without payment or write off' => sub {
2311 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2313 my $item = $builder->build_sample_item(
2315 biblionumber => $biblio->biblionumber,
2316 library => $library->branchcode,
2317 replacementprice => 23.00,
2318 replacementprice => $replacement_amount,
2319 itype => $item_type->itemtype
2323 AddIssue( $patron->unblessed, $item->barcode );
2325 # Simulate item marked as lost
2326 ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2327 LostItem( $item->itemnumber, 1 );
2329 my $processing_fee_lines = Koha::Account::Lines->search(
2330 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2331 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2332 my $processing_fee_line = $processing_fee_lines->next;
2333 is( $processing_fee_line->amount + 0,
2334 $processfee_amount, 'The right PF amount is generated' );
2335 is( $processing_fee_line->amountoutstanding + 0,
2336 $processfee_amount, 'The right PF amountoutstanding is generated' );
2338 my $lost_fee_lines = Koha::Account::Lines->search(
2339 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2340 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2341 my $lost_fee_line = $lost_fee_lines->next;
2342 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2343 is( $lost_fee_line->amountoutstanding + 0,
2344 $replacement_amount, 'The right L amountountstanding is generated' );
2346 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2347 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2349 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2350 is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2351 is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2353 $lost_fee_line->discard_changes;
2354 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2355 is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2357 is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2360 subtest 'Test with partial payement and write off, and remaining debt' => sub {
2364 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2365 my $item = $builder->build_sample_item(
2367 biblionumber => $biblio->biblionumber,
2368 library => $library->branchcode,
2369 replacementprice => $replacement_amount,
2370 itype => $item_type->itemtype
2374 AddIssue( $patron->unblessed, $item->barcode );
2376 # Simulate item marked as lost
2377 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2378 LostItem( $item->itemnumber, 1 );
2380 my $processing_fee_lines = Koha::Account::Lines->search(
2381 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2382 is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2383 my $processing_fee_line = $processing_fee_lines->next;
2384 is( $processing_fee_line->amount + 0,
2385 $processfee_amount, 'The right PF amount is generated' );
2386 is( $processing_fee_line->amountoutstanding + 0,
2387 $processfee_amount, 'The right PF amountoutstanding is generated' );
2389 my $lost_fee_lines = Koha::Account::Lines->search(
2390 { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2391 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2392 my $lost_fee_line = $lost_fee_lines->next;
2393 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2394 is( $lost_fee_line->amountoutstanding + 0,
2395 $replacement_amount, 'The right L amountountstanding is generated' );
2397 my $account = $patron->account;
2398 is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2401 my $payment_amount = 27;
2402 my $payment = $account->add_credit(
2403 { amount => $payment_amount,
2405 interface => 'test',
2409 $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } );
2411 # Partially write off fee
2412 my $write_off_amount = 25;
2413 my $write_off = $account->add_credit(
2414 { amount => $write_off_amount,
2416 interface => 'test',
2419 $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
2421 is( $account->balance,
2422 $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2423 'Payment and write off applied'
2426 # Store the amountoutstanding value
2427 $lost_fee_line->discard_changes;
2428 my $outstanding = $lost_fee_line->amountoutstanding;
2430 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2431 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2433 is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2435 $lost_fee_line->discard_changes;
2436 is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2437 is( $lost_fee_line->accounttype,
2438 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2440 is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2441 is( $credit_return->amount + 0,
2442 ($payment_amount + $outstanding ) * -1,
2443 'The account line of type CR has an amount equal to the payment + outstanding'
2445 is( $credit_return->amountoutstanding + 0,
2446 $payment_amount * -1,
2447 'The account line of type CR has an amountoutstanding equal to the payment'
2450 is( $account->balance,
2451 $processfee_amount - $payment_amount,
2452 'The patron balance is the difference between the PF and the credit'
2456 subtest 'Partial payement, existing debits and AccountAutoReconcile' => sub {
2460 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2461 my $barcode = 'KD123456793';
2462 my $replacement_amount = 100;
2463 my $processfee_amount = 20;
2465 my $item_type = $builder->build_object(
2466 { class => 'Koha::ItemTypes',
2468 notforloan => undef,
2470 defaultreplacecost => undef,
2472 rentalcharge_daily => 0,
2476 my ( undef, undef, $item_id ) = AddItem(
2477 { homebranch => $library->branchcode,
2478 holdingbranch => $library->branchcode,
2479 barcode => $barcode,
2480 replacementprice => $replacement_amount,
2481 itype => $item_type->itemtype
2483 $biblio->biblionumber
2486 AddIssue( $patron->unblessed, $barcode );
2488 # Simulate item marked as lost
2489 ModItem( { itemlost => 1 }, $biblio->biblionumber, $item_id );
2490 LostItem( $item_id, 1 );
2492 my $lost_fee_lines = Koha::Account::Lines->search(
2493 { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2494 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2495 my $lost_fee_line = $lost_fee_lines->next;
2496 is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2497 is( $lost_fee_line->amountoutstanding + 0,
2498 $replacement_amount, 'The right L amountountstanding is generated' );
2500 my $account = $patron->account;
2501 is( $account->balance, $replacement_amount, 'Balance is L' );
2504 my $payment_amount = 27;
2505 my $payment = $account->add_credit(
2506 { amount => $payment_amount,
2508 interface => 'test',
2511 $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' });
2513 is( $account->balance,
2514 $replacement_amount - $payment_amount,
2518 my $manual_debit_amount = 80;
2519 $account->add_debit( { amount => $manual_debit_amount, type => 'overdue', interface =>'test' } );
2521 is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
2523 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
2525 my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2526 my $credit_return = Koha::Account::Lines->find($credit_return_id);
2528 is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2530 my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next;
2531 is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2535 subtest '_FixOverduesOnReturn' => sub {
2538 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2539 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2541 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
2543 my $branchcode = $library2->{branchcode};
2545 my $item = $builder->build_sample_item(
2547 biblionumber => $biblio->biblionumber,
2548 library => $branchcode,
2549 replacementprice => 99.00,
2554 my $patron = $builder->build( { source => 'Borrower' } );
2556 ## Start with basic call, should just close out the open fine
2557 my $accountline = Koha::Account::Line->new(
2559 borrowernumber => $patron->{borrowernumber},
2560 accounttype => 'OVERDUE',
2561 status => 'UNRETURNED',
2562 itemnumber => $item->itemnumber,
2564 amountoutstanding => 99.00,
2565 interface => 'test',
2569 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber );
2571 $accountline->_result()->discard_changes();
2573 is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2574 is( $accountline->status, 'RETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status RETURNED )');
2576 ## Run again, with exemptfine enabled
2579 accounttype => 'OVERDUE',
2580 status => 'UNRETURNED',
2581 amountoutstanding => 99.00,
2585 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1 );
2587 $accountline->_result()->discard_changes();
2588 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2590 is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2591 is( $accountline->status, 'FORGIVEN', 'Open fine ( account type OVERDUE ) has been set to fine forgiven ( status FORGIVEN )');
2592 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2593 is( $offset->amount, '-99.000000', "Amount of offset is correct" );
2594 my $credit = $offset->credit;
2595 is( ref $credit, "Koha::Account::Line", "Found matching credit for fine forgiveness" );
2596 is( $credit->amount, '-99.000000', "Credit amount is set correctly" );
2597 is( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
2600 subtest 'Set waiting flag' => sub {
2603 my $library_1 = $builder->build( { source => 'Branch' } );
2604 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2605 my $library_2 = $builder->build( { source => 'Branch' } );
2606 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2608 my $biblio = $builder->build( { source => 'Biblio' } );
2609 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2611 my $item = $builder->build(
2615 homebranch => $library_1->{branchcode},
2616 holdingbranch => $library_1->{branchcode},
2620 biblionumber => $biblioitem->{biblionumber},
2625 set_userenv( $library_2 );
2626 my $reserve_id = AddReserve(
2627 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2628 '', 1, undef, undef, '', undef, $item->{itemnumber},
2631 set_userenv( $library_1 );
2632 my $do_transfer = 1;
2633 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2634 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2635 my $hold = Koha::Holds->find( $reserve_id );
2636 is( $hold->found, 'T', 'Hold is in transit' );
2638 my ( $status ) = CheckReserves($item->{itemnumber});
2639 is( $status, 'Reserved', 'Hold is not waiting yet');
2641 set_userenv( $library_2 );
2643 AddReturn( $item->{barcode}, $library_2->{branchcode} );
2644 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2645 $hold = Koha::Holds->find( $reserve_id );
2646 is( $hold->found, 'W', 'Hold is waiting' );
2647 ( $status ) = CheckReserves($item->{itemnumber});
2648 is( $status, 'Waiting', 'Now the hold is waiting');
2651 subtest 'Cancel transfers on lost items' => sub {
2653 my $library_1 = $builder->build( { source => 'Branch' } );
2654 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2655 my $library_2 = $builder->build( { source => 'Branch' } );
2656 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2657 my $biblio = $builder->build( { source => 'Biblio' } );
2658 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2659 my $item = $builder->build(
2663 homebranch => $library_1->{branchcode},
2664 holdingbranch => $library_1->{branchcode},
2668 biblionumber => $biblioitem->{biblionumber},
2673 set_userenv( $library_2 );
2674 my $reserve_id = AddReserve(
2675 $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber}, '', 1, undef, undef, '', undef, $item->{itemnumber},
2678 #Return book and add transfer
2679 set_userenv( $library_1 );
2680 my $do_transfer = 1;
2681 my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2682 ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2683 C4::Circulation::transferbook( $library_2->{branchcode}, $item->{barcode} );
2684 my $hold = Koha::Holds->find( $reserve_id );
2685 is( $hold->found, 'T', 'Hold is in transit' );
2687 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2688 my ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2689 is( $tobranch, $library_2->{branchcode}, 'The transfer record exists in the branchtransfers table');
2690 my $itemcheck = Koha::Items->find($item->{itemnumber});
2691 is( $itemcheck->holdingbranch, $library_2->{branchcode}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2693 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2694 ModItem( { itemlost => 1 }, $biblio->{biblionumber}, $item->{itemnumber} );
2695 LostItem( $item->{itemnumber}, 'test', 1 );
2696 ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2697 is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2698 $itemcheck = Koha::Items->find($item->{itemnumber});
2699 is( $itemcheck->holdingbranch, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2702 subtest 'CanBookBeIssued | is_overdue' => sub {
2705 # Set a simple circ policy
2706 $dbh->do('DELETE FROM issuingrules');
2708 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2709 issuelength, lengthunit,
2710 renewalsallowed, renewalperiod,
2711 norenewalbefore, auto_renew,
2728 my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2729 my $ten_days_go = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2730 my $library = $builder->build( { source => 'Branch' } );
2731 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2733 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2734 my $item = $builder->build(
2738 homebranch => $library->{branchcode},
2739 holdingbranch => $library->{branchcode},
2743 biblionumber => $biblioitem->{biblionumber},
2748 my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2749 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2750 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2751 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2752 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2753 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2756 subtest 'ItemsDeniedRenewal preference' => sub {
2759 C4::Context->set_preference('ItemsDeniedRenewal','');
2761 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2764 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
2765 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2768 '*', $idr_lib->branchcode, '*', 25,
2775 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2776 homebranch => $idr_lib->branchcode,
2780 itemcallnumber => undef,
2784 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2785 homebranch => $idr_lib->branchcode,
2788 location => 'NOPROC'
2792 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2793 branchcode => $idr_lib->branchcode,
2796 my $future = dt_from_string->add( days => 1 );
2797 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2798 returndate => undef,
2801 borrowernumber => $idr_borrower->borrowernumber,
2802 itemnumber => $deny_book->itemnumber,
2803 onsite_checkout => 0,
2804 date_due => $future,
2807 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2808 returndate => undef,
2811 borrowernumber => $idr_borrower->borrowernumber,
2812 itemnumber => $allow_book->itemnumber,
2813 onsite_checkout => 0,
2814 date_due => $future,
2820 my ( $idr_mayrenew, $idr_error ) =
2821 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2822 is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2823 is( $idr_error, undef, 'Renewal allowed when no rules' );
2825 $idr_rules="withdrawn: [1]";
2827 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2828 ( $idr_mayrenew, $idr_error ) =
2829 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2830 is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2831 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2832 ( $idr_mayrenew, $idr_error ) =
2833 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2834 is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2835 is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2837 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2839 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2840 ( $idr_mayrenew, $idr_error ) =
2841 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2842 is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2843 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2844 ( $idr_mayrenew, $idr_error ) =
2845 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2846 is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2847 is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2849 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2851 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2852 ( $idr_mayrenew, $idr_error ) =
2853 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2854 is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2855 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2856 ( $idr_mayrenew, $idr_error ) =
2857 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2858 is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2859 is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2861 $idr_rules="itemcallnumber: [NULL]";
2862 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2863 ( $idr_mayrenew, $idr_error ) =
2864 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2865 is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2866 $idr_rules="itemcallnumber: ['']";
2867 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2868 ( $idr_mayrenew, $idr_error ) =
2869 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2870 is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2872 $idr_rules="itemnotes: [NULL]";
2873 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2874 ( $idr_mayrenew, $idr_error ) =
2875 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2876 is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2877 $idr_rules="itemnotes: ['']";
2878 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2879 ( $idr_mayrenew, $idr_error ) =
2880 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2881 is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2884 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2887 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2888 my $library = $builder->build( { source => 'Branch' } );
2889 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2891 my $itemtype = $builder->build(
2893 source => 'Itemtype',
2894 value => { notforloan => undef, }
2898 my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2899 my $item = $builder->build_object(
2901 class => 'Koha::Items',
2903 homebranch => $library->{branchcode},
2904 holdingbranch => $library->{branchcode},
2908 biblionumber => $biblioitem->{biblionumber},
2909 biblioitemnumber => $biblioitem->{biblioitemnumber},
2914 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2915 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2916 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2919 subtest 'CanBookBeIssued | notforloan' => sub {
2922 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2924 my $library = $builder->build( { source => 'Branch' } );
2925 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2927 my $itemtype = $builder->build(
2929 source => 'Itemtype',
2930 value => { notforloan => undef, }
2934 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2935 my $item = $builder->build_object(
2937 class => 'Koha::Items',
2939 homebranch => $library->{branchcode},
2940 holdingbranch => $library->{branchcode},
2944 itype => $itemtype->{itemtype},
2945 biblionumber => $biblioitem->{biblionumber},
2946 biblioitemnumber => $biblioitem->{biblioitemnumber},
2951 my ( $issuingimpossible, $needsconfirmation );
2954 subtest 'item-level_itypes = 1' => sub {
2957 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2958 # Is for loan at item type and item level
2959 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2960 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2961 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2963 # not for loan at item type level
2964 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2965 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2966 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2969 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2970 'Item can not be issued, not for loan at item type level'
2973 # not for loan at item level
2974 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2975 $item->notforloan( 1 )->store;
2976 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2977 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2980 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2981 'Item can not be issued, not for loan at item type level'
2985 subtest 'item-level_itypes = 0' => sub {
2988 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2990 # We set another itemtype for biblioitem
2991 my $itemtype = $builder->build(
2993 source => 'Itemtype',
2994 value => { notforloan => undef, }
2998 # for loan at item type and item level
2999 $item->notforloan(0)->store;
3000 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
3001 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3002 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
3003 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
3005 # not for loan at item type level
3006 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
3007 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3008 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3011 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
3012 'Item can not be issued, not for loan at item type level'
3015 # not for loan at item level
3016 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
3017 $item->notforloan( 1 )->store;
3018 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3019 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3022 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
3023 'Item can not be issued, not for loan at item type level'
3027 # TODO test with AllowNotForLoanOverride = 1
3030 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
3033 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
3034 my $item = $builder->build_object({ class => 'Koha::Items', value => { onloan => '2018-01-01' }});
3035 AddReturn( $item->barcode, $item->homebranch );
3036 $item->discard_changes; # refresh
3037 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
3041 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
3046 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3048 my $issuing_charges = 15;
3049 my $title = 'A title';
3050 my $author = 'Author, An';
3051 my $barcode = 'WHATARETHEODDS';
3053 my $circ = Test::MockModule->new('C4::Circulation');
3055 'GetIssuingCharges',
3057 return $issuing_charges;
3061 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3062 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
3063 my $patron = $builder->build_object({
3064 class => 'Koha::Patrons',
3065 value => { branchcode => $library->id }
3068 my $biblio = $builder->build_sample_biblio({ title=> $title, author => $author });
3069 my ( undef, undef, $item_id ) = AddItem(
3071 homebranch => $library->id,
3072 holdingbranch => $library->id,
3073 barcode => $barcode,
3074 replacementprice => 23.00,
3075 itype => $itemtype->id
3077 $biblio->biblionumber
3079 my $item = Koha::Items->find( $item_id );
3081 my $context = Test::MockModule->new('C4::Context');
3082 $context->mock( userenv => { branch => $library->id } );
3084 # Check the item out
3085 AddIssue( $patron->unblessed, $item->barcode );
3086 t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
3087 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
3088 my %params_renewal = (
3089 timestamp => { -like => $date . "%" },
3090 module => "CIRCULATION",
3091 action => "RENEWAL",
3093 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
3094 AddRenewal( $patron->id, $item->id, $library->id );
3095 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3096 is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
3098 my $checkouts = $patron->checkouts;
3099 # The following will fail if run on 00:00:00
3100 unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
3102 t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
3103 $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
3104 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
3105 AddRenewal( $patron->id, $item->id, $library->id );
3106 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3107 is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
3109 my $lines = Koha::Account::Lines->search({
3110 borrowernumber => $patron->id,
3111 itemnumber => $item->id
3114 is( $lines->count, 3 );
3116 my $line = $lines->next;
3117 is( $line->accounttype, 'Rent', 'The issuing charge generates an accountline' );
3118 is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
3119 is( $line->description, 'Rental', 'AddIssuingCharge set a hardcoded description for the accountline' );
3121 $line = $lines->next;
3122 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
3123 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
3124 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
3126 $line = $lines->next;
3127 is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
3128 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
3129 is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
3133 subtest 'ProcessOfflinePayment() tests' => sub {
3140 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
3141 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3142 my $result = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
3144 is( $result, 'Success.', 'The right string is returned' );
3146 my $lines = $patron->account->lines;
3147 is( $lines->count, 1, 'line created correctly');
3149 my $line = $lines->next;
3150 is( $line->amount+0, $amount * -1, 'amount picked from params' );
3151 is( $line->branchcode, $library->id, 'branchcode set correctly' );
3155 subtest 'Incremented fee tests' => sub {
3158 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3160 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
3162 my $module = new Test::MockModule('C4::Context');
3163 $module->mock('userenv', sub { { branch => $library->id } });
3165 my $patron = $builder->build_object(
3167 class => 'Koha::Patrons',
3168 value => { categorycode => $patron_category->{categorycode} }
3172 my $itemtype = $builder->build_object(
3174 class => 'Koha::ItemTypes',
3176 notforloan => undef,
3178 rentalcharge_daily => 1.000000
3183 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3184 my $item = $builder->build_object(
3186 class => 'Koha::Items',
3188 homebranch => $library->id,
3189 holdingbranch => $library->id,
3193 itype => $itemtype->id,
3194 biblionumber => $biblioitem->{biblionumber},
3195 biblioitemnumber => $biblioitem->{biblioitemnumber},
3200 is( $itemtype->rentalcharge_daily, '1.000000', 'Daily rental charge stored and retreived correctly' );
3201 is( $item->effective_itemtype, $itemtype->id, "Itemtype set correctly for item");
3203 my $dt_from = dt_from_string();
3204 my $dt_to = dt_from_string()->add( days => 7 );
3205 my $dt_to_renew = dt_from_string()->add( days => 13 );
3207 t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
3208 my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3209 my $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3210 is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar" );
3211 $accountline->delete();
3212 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3213 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3214 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar, for renewal" );
3215 $accountline->delete();
3218 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
3219 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3220 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3221 is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed" );
3222 $accountline->delete();
3223 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3224 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3225 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed, for renewal" );
3226 $accountline->delete();
3229 my $calendar = C4::Calendar->new( branchcode => $library->id );
3230 $calendar->insert_week_day_holiday(
3232 title => 'Test holiday',
3233 description => 'Test holiday'
3235 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3236 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3237 is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
3238 $accountline->delete();
3239 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3240 $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3241 is( $accountline->amount, '5.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays, for renewal" );
3242 $accountline->delete();
3245 $itemtype->rentalcharge('2.000000')->store;
3246 is( $itemtype->rentalcharge, '2.000000', 'Rental charge updated and retreived correctly' );
3247 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from);
3248 my $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3249 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly");
3250 $accountlines->delete();
3251 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3252 $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3253 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly, for renewal");
3254 $accountlines->delete();
3258 subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
3261 t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
3262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3265 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3266 my $patron = $builder->build_object(
3268 class => 'Koha::Patrons',
3269 value => { categorycode => $patron_category->{categorycode} }
3273 my $itemtype = $builder->build_object(
3275 class => 'Koha::ItemTypes',
3279 rentalcharge_daily => 0
3284 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3285 my $item = $builder->build_object(
3287 class => 'Koha::Items',
3289 homebranch => $library->id,
3290 holdingbranch => $library->id,
3294 itype => $itemtype->id,
3295 biblionumber => $biblioitem->{biblionumber},
3296 biblioitemnumber => $biblioitem->{biblioitemnumber},
3301 my ( $issuingimpossible, $needsconfirmation );
3302 my $dt_from = dt_from_string();
3303 my $dt_due = dt_from_string()->add( days => 3 );
3305 $itemtype->rentalcharge('1.000000')->store;
3306 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3307 is_deeply( $needsconfirmation, { RENTALCHARGE => '1' }, 'Item needs rentalcharge confirmation to be issued' );
3308 $itemtype->rentalcharge('0')->store;
3309 $itemtype->rentalcharge_daily('1.000000')->store;
3310 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3311 is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
3312 $itemtype->rentalcharge_daily('0')->store;
3315 $schema->storage->txn_rollback;
3316 C4::Context->clear_syspref_cache();
3317 $cache->clear_from_cache('single_holidays');