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 => 67;
24 use Test::Deep qw( cmp_deeply );
30 use POSIX qw( floor );
32 use t::lib::TestBuilder;
35 use C4::Calendar qw( new insert_single_holiday insert_week_day_holiday delete_holiday );
36 use C4::Circulation qw( AddIssue AddReturn CanBookBeRenewed GetIssuingCharges AddRenewal GetSoonestRenewDate GetLatestAutoRenewDate LostItem GetUpcomingDueIssues CanBookBeIssued AddIssuingCharge MarkIssueReturned ProcessOfflinePayment transferbook updateWrongTransfer );
38 use C4::Items qw( ModItemTransfer );
40 use C4::Reserves qw( AddReserve ModReserve ModReserveCancelAll ModReserveAffect CheckReserves GetOtherReserves );
41 use C4::Overdues qw( CalcFine UpdateFine get_chargeable_units );
42 use C4::Members::Messaging qw( SetMessagingPreference );
43 use Koha::DateUtils qw( dt_from_string output_pref );
46 use Koha::Item::Transfers;
49 use Koha::Patron::Debarments qw( AddDebarment DelUniqueDebarment );
51 use Koha::CirculationRules;
52 use Koha::Subscriptions;
53 use Koha::Account::Lines;
54 use Koha::Account::Offsets;
56 use Koha::Notice::Messages;
57 use Koha::Cache::Memory::Lite;
59 my $builder = t::lib::TestBuilder->new;
62 my $staff = $builder->build_object({ class => "Koha::Patrons" });
63 t::lib::Mocks::mock_userenv({ patron => $staff, branchcode => $library->{branchcode} });
67 my ( $error, $question, $alert ) = @_;
69 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
70 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
71 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
75 sub test_debarment_on_checkout {
77 my $item = $params->{item};
78 my $library = $params->{library};
79 my $patron = $params->{patron};
80 my $due_date = $params->{due_date} || dt_from_string;
81 my $return_date = $params->{return_date} || dt_from_string;
82 my $expected_expiration_date = $params->{expiration_date};
84 $expected_expiration_date = output_pref(
86 dt => $expected_expiration_date,
92 my $line_number = $caller[2];
93 AddIssue( $patron, $item->barcode, $due_date );
95 my ( undef, $message ) = AddReturn( $item->barcode, $library->{branchcode}, undef, $return_date );
96 is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
97 or diag('AddReturn returned message ' . Dumper $message );
98 my $suspensions = $patron->restrictions->search({ type => 'SUSPENSION' } );
99 is( $suspensions->count, 1, 'Test at line ' . $line_number );
101 my $THE_suspension = $suspensions->next;
102 is( $THE_suspension->expiration,
103 $expected_expiration_date, 'Test at line ' . $line_number );
104 Koha::Patron::Debarments::DelUniqueDebarment(
105 { borrowernumber => $patron->borrowernumber, type => 'SUSPENSION' } );
108 my $schema = Koha::Database->schema;
109 $schema->storage->txn_begin;
110 my $dbh = C4::Context->dbh;
112 # Prevent random failures by mocking ->now
113 my $now_value = dt_from_string;
114 my $mocked_datetime = Test::MockModule->new('DateTime');
115 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
117 my $cache = Koha::Caches->get_instance();
118 $dbh->do(q|DELETE FROM special_holidays|);
119 $dbh->do(q|DELETE FROM repeatable_holidays|);
120 my $branches = Koha::Libraries->search();
121 for my $branch ( $branches->next ) {
122 my $key = $branch->branchcode . "_holidays";
123 $cache->clear_from_cache($key);
126 # Start with a clean slate
127 $dbh->do('DELETE FROM issues');
128 $dbh->do('DELETE FROM borrowers');
130 # Disable recording of the staff who checked out an item until we're ready for it
131 t::lib::Mocks::mock_preference('RecordStaffUserOnCheckout', 0);
133 my $module = Test::MockModule->new('C4::Context');
135 my $library = $builder->build({
138 my $library2 = $builder->build({
141 my $itemtype = $builder->build(
143 source => 'Itemtype',
147 rentalcharge_daily => 0,
148 defaultreplacecost => undef,
153 my $patron_category = $builder->build(
155 source => 'Category',
157 category_type => 'P',
159 BlockExpiredPatronOpacActions => -1, # Pick the pref value
164 my $CircControl = C4::Context->preference('CircControl');
165 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
167 my $item = $builder->build_object({
168 class => 'Koha::Items',
170 homebranch => $library2->{branchcode},
171 holdingbranch => $library2->{branchcode}
175 my $borrower = $builder->build_object({
176 class => 'Koha::Patrons',
177 value => { branchcode => $library2->{branchcode} }
180 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
182 # No userenv, PickupLibrary
183 t::lib::Mocks::mock_preference('IndependentBranches', '0');
184 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
186 C4::Context->preference('CircControl'),
188 'CircControl changed to PickupLibrary'
191 C4::Circulation::_GetCircControlBranch($item, $borrower),
192 $item->get_column($HomeOrHoldingBranch),
193 '_GetCircControlBranch returned item branch (no userenv defined)'
196 # No userenv, PatronLibrary
197 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
199 C4::Context->preference('CircControl'),
201 'CircControl changed to PatronLibrary'
204 C4::Circulation::_GetCircControlBranch($item, $borrower),
205 $borrower->branchcode,
206 '_GetCircControlBranch returned borrower branch'
209 # No userenv, ItemHomeLibrary
210 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
212 C4::Context->preference('CircControl'),
214 'CircControl changed to ItemHomeLibrary'
217 $item->get_column($HomeOrHoldingBranch),
218 C4::Circulation::_GetCircControlBranch($item, $borrower),
219 '_GetCircControlBranch returned item branch'
223 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
224 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
226 # Userenv set, PickupLibrary
227 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
229 C4::Context->preference('CircControl'),
231 'CircControl changed to PickupLibrary'
234 C4::Circulation::_GetCircControlBranch($item, $borrower),
235 $library2->{branchcode},
236 '_GetCircControlBranch returned current branch'
239 # Userenv set, PatronLibrary
240 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
242 C4::Context->preference('CircControl'),
244 'CircControl changed to PatronLibrary'
247 C4::Circulation::_GetCircControlBranch($item, $borrower),
248 $borrower->branchcode,
249 '_GetCircControlBranch returned borrower branch'
252 # Userenv set, ItemHomeLibrary
253 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
255 C4::Context->preference('CircControl'),
257 'CircControl changed to ItemHomeLibrary'
260 C4::Circulation::_GetCircControlBranch($item, $borrower),
261 $item->get_column($HomeOrHoldingBranch),
262 '_GetCircControlBranch returned item branch'
265 # Reset initial configuration
266 t::lib::Mocks::mock_preference('CircControl', $CircControl);
268 C4::Context->preference('CircControl'),
270 'CircControl reset to its initial value'
273 # Set a simple circ policy
274 $dbh->do('DELETE FROM circulation_rules');
275 Koha::CirculationRules->set_rules(
277 categorycode => undef,
281 reservesallowed => 25,
283 lengthunit => 'days',
284 renewalsallowed => 1,
286 norenewalbefore => undef,
287 noautorenewalbefore => undef,
295 subtest "CanBookBeRenewed AllowRenewalIfOtherItemsAvailable multiple borrowers and items tests" => sub {
298 #Can only reserve from home branch
299 Koha::CirculationRules->set_rule(
303 rule_name => 'holdallowed',
307 Koha::CirculationRules->set_rule(
310 categorycode => undef,
312 rule_name => 'onshelfholds',
317 # Patrons from three different branches
318 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
319 my $patron_hold_1 = $builder->build_object({ class => 'Koha::Patrons' });
320 my $patron_hold_2 = $builder->build_object({ class => 'Koha::Patrons' });
321 my $biblio = $builder->build_sample_biblio();
323 # Item at each patron branch
324 my $item_1 = $builder->build_sample_item({
325 biblionumber => $biblio->biblionumber,
326 homebranch => $patron->branchcode
328 my $item_2 = $builder->build_sample_item({
329 biblionumber => $biblio->biblionumber,
330 homebranch => $patron_hold_2->branchcode
332 my $item_3 = $builder->build_sample_item({
333 biblionumber => $biblio->biblionumber,
334 homebranch => $patron_hold_1->branchcode
337 my $issue = AddIssue( $patron, $item_1->barcode);
338 my $datedue = dt_from_string( $issue->date_due() );
339 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
342 my $reserve_1 = AddReserve(
344 branchcode => $patron_hold_1->branchcode,
345 borrowernumber => $patron_hold_1->borrowernumber,
346 biblionumber => $biblio->biblionumber,
348 reservation_date => dt_from_string(),
349 expiration_date => undef,
356 branchcode => $patron_hold_2->branchcode,
357 borrowernumber => $patron_hold_2->borrowernumber,
358 biblionumber => $biblio->biblionumber,
360 reservation_date => dt_from_string(),
361 expiration_date => undef,
366 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
368 my ( $renewokay, $error ) = CanBookBeRenewed($patron, $issue);
369 is( $renewokay, 0, 'Cannot renew, reserved');
370 is( $error, 'on_reserve', 'Cannot renew, reserved (returned error is on_reserve)');
372 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
374 ( $renewokay, $error ) = CanBookBeRenewed($patron, $issue);
375 is( $renewokay, 1, 'Can renew, two items available for two holds');
376 is( $error, undef, 'Can renew, each reserve has an item');
379 my $hold = Koha::Holds->find( $reserve_1 );
380 $hold->itemnumber( $item_1->itemnumber )->store;
382 ( $renewokay, $error ) = CanBookBeRenewed($patron, $issue);
383 is( $renewokay, 0, 'Cannot renew when there is an item specific hold');
384 is( $error, 'on_reserve', 'Cannot renew, only this item can fill the reserve');
387 subtest "GetIssuingCharges tests" => sub {
389 my $branch_discount = $builder->build_object({ class => 'Koha::Libraries' });
390 my $branch_no_discount = $builder->build_object({ class => 'Koha::Libraries' });
391 Koha::CirculationRules->set_rule(
393 categorycode => undef,
394 branchcode => $branch_discount->branchcode,
396 rule_name => 'rentaldiscount',
400 my $itype_charge = $builder->build_object({
401 class => 'Koha::ItemTypes',
406 my $itype_no_charge = $builder->build_object({
407 class => 'Koha::ItemTypes',
412 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
413 my $item_1 = $builder->build_sample_item({ itype => $itype_charge->itemtype });
414 my $item_2 = $builder->build_sample_item({ itype => $itype_no_charge->itemtype });
416 t::lib::Mocks::mock_userenv({ branchcode => $branch_no_discount->branchcode });
417 # For now the sub always uses the env branch, this should follow CircControl instead
418 my ($charge, $itemtype) = GetIssuingCharges( $item_1->itemnumber, $patron->borrowernumber);
419 is( $charge + 0, 10.00, "Charge fetched correctly when no discount exists");
420 ($charge, $itemtype) = GetIssuingCharges( $item_2->itemnumber, $patron->borrowernumber);
421 is( $charge + 0, 0.00, "Charge fetched correctly when no discount exists and no charge");
423 t::lib::Mocks::mock_userenv({ branchcode => $branch_discount->branchcode });
424 # For now the sub always uses the env branch, this should follow CircControl instead
425 ($charge, $itemtype) = GetIssuingCharges( $item_1->itemnumber, $patron->borrowernumber);
426 is( $charge + 0, 8.50, "Charge fetched correctly when discount exists");
427 ($charge, $itemtype) = GetIssuingCharges( $item_2->itemnumber, $patron->borrowernumber);
428 is( $charge + 0, 0.00, "Charge fetched correctly when discount exists and no charge");
432 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
433 subtest "CanBookBeRenewed tests" => sub {
436 C4::Context->set_preference('ItemsDeniedRenewal','');
437 # Generate test biblio
438 my $biblio = $builder->build_sample_biblio();
440 my $branch = $library2->{branchcode};
442 my $item_1 = $builder->build_sample_item(
444 biblionumber => $biblio->biblionumber,
446 replacementprice => 12.00,
450 $reused_itemnumber_1 = $item_1->itemnumber;
452 my $item_2 = $builder->build_sample_item(
454 biblionumber => $biblio->biblionumber,
456 replacementprice => 23.00,
460 $reused_itemnumber_2 = $item_2->itemnumber;
462 my $item_3 = $builder->build_sample_item(
464 biblionumber => $biblio->biblionumber,
466 replacementprice => 23.00,
472 my %renewing_borrower_data = (
474 surname => 'Renewal',
475 categorycode => $patron_category->{categorycode},
476 branchcode => $branch,
477 autorenew_checkouts => 1,
480 my %reserving_borrower_data = (
481 firstname => 'Katrin',
482 surname => 'Reservation',
483 categorycode => $patron_category->{categorycode},
484 branchcode => $branch,
487 my %hold_waiting_borrower_data = (
489 surname => 'Reservation',
490 categorycode => $patron_category->{categorycode},
491 branchcode => $branch,
494 my %restricted_borrower_data = (
495 firstname => 'Alice',
496 surname => 'Reservation',
497 categorycode => $patron_category->{categorycode},
498 debarred => '3228-01-01',
499 branchcode => $branch,
502 my %expired_borrower_data = (
505 categorycode => $patron_category->{categorycode},
506 branchcode => $branch,
507 dateexpiry => dt_from_string->subtract( months => 1 ),
508 autorenew_checkouts => 1,
511 my $renewing_borrower_obj = Koha::Patron->new(\%renewing_borrower_data)->store;
512 my $renewing_borrowernumber = $renewing_borrower_obj->borrowernumber;
513 my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
514 my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
515 my $restricted_borrower_obj = Koha::Patron->new(\%restricted_borrower_data)->store;
517 my $expired_borrower_obj = Koha::Patron->new(\%expired_borrower_data)->store;
524 my $checkitem = undef;
527 my $issue_1 = AddIssue( $renewing_borrower_obj, $item_1->barcode);
528 my $datedue = dt_from_string( $issue_1->date_due() );
529 is (defined $issue_1->date_due(), 1, "Item 1 checked out, due date: " . $issue_1->date_due() );
531 my $issue_2 = AddIssue( $renewing_borrower_obj, $item_2->barcode);
532 is (defined $issue_2, 1, "Item 2 checked out, due date: " . $issue_2->date_due());
534 my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $item_1->itemnumber } )->borrowernumber;
535 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to ".$renewing_borrower_obj->firstname." ".$renewing_borrower_obj->surname);
537 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1, 1);
538 is( $renewokay, 1, 'Can renew, no holds for this title or item');
541 # Biblio-level hold, renewal test
544 branchcode => $branch,
545 borrowernumber => $reserving_borrowernumber,
546 biblionumber => $biblio->biblionumber,
547 priority => $priority,
548 reservation_date => $resdate,
549 expiration_date => $expdate,
551 itemnumber => $checkitem,
556 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
557 Koha::CirculationRules->set_rule(
559 categorycode => undef,
562 rule_name => 'onshelfholds',
566 Koha::CirculationRules->set_rule(
568 categorycode => undef,
571 rule_name => 'renewalsallowed',
575 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
576 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
577 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
578 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_2);
579 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
582 # Second biblio-level hold
583 my $reserve_id = AddReserve(
585 branchcode => $branch,
586 borrowernumber => $reserving_borrowernumber,
587 biblionumber => $biblio->biblionumber,
588 priority => $priority,
589 reservation_date => $resdate,
590 expiration_date => $expdate,
592 itemnumber => $checkitem,
596 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
597 is( $renewokay, 0, 'Renewal not possible when single patron\'s holds exceed the number of available items');
598 Koha::Holds->find($reserve_id)->delete;
600 # Now let's add an item level hold, we should no longer be able to renew the item
601 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
603 borrowernumber => $hold_waiting_borrowernumber,
604 biblionumber => $biblio->biblionumber,
605 itemnumber => $item_1->itemnumber,
606 branchcode => $branch,
608 reservedate => '1999-01-01',
611 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
612 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
615 # 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
616 # be able to renew these items
617 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
619 borrowernumber => $hold_waiting_borrowernumber,
620 biblionumber => $biblio->biblionumber,
621 itemnumber => $item_3->itemnumber,
622 branchcode => $branch,
627 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
628 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
629 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_2);
630 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
631 t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
633 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
634 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
635 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
637 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_2);
638 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
639 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
641 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
642 my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber );
643 AddIssue($reserving_borrower, $item_3->barcode);
644 my $reserve = $dbh->selectrow_hashref(
645 'SELECT * FROM old_reserves WHERE reserve_id = ?',
649 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
651 # Item-level hold, renewal test
654 branchcode => $branch,
655 borrowernumber => $reserving_borrowernumber,
656 biblionumber => $biblio->biblionumber,
657 priority => $priority,
658 reservation_date => $resdate,
659 expiration_date => $expdate,
661 itemnumber => $item_1->itemnumber,
666 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1, 1);
667 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
668 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
670 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_2, 1);
671 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
673 # Items can't fill hold for reasons
674 $issue_1->item->notforloan(1)->store;
675 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1, 1);
676 is( $renewokay, 0, 'Cannot renew, item is marked not for loan, but an item specific hold always blocks');
677 $item_1->set({notforloan => 0, itype => $itemtype })->store;
679 # FIXME: Add more for itemtype not for loan etc.
681 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
682 my $item_5 = $builder->build_sample_item(
684 biblionumber => $biblio->biblionumber,
686 replacementprice => 23.00,
690 my $issue_5 = AddIssue($restricted_borrower_obj, $item_5->barcode);
691 is (defined $issue_5, 1, "Item with date due checked out, due date: ". $issue_5->date_due);
693 t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
694 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_2);
695 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
696 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrower_obj, $issue_5);
697 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
698 is( $error, 'restriction', "Correct error returned");
700 # Users cannot renew an overdue item
701 my $item_6 = $builder->build_sample_item(
703 biblionumber => $biblio->biblionumber,
705 replacementprice => 23.00,
710 my $item_7 = $builder->build_sample_item(
712 biblionumber => $biblio->biblionumber,
714 replacementprice => 23.00,
719 my $issue_6 = AddIssue( $renewing_borrower_obj, $item_6->barcode);
720 is (defined $issue_6, 1, "Item 2 checked out, due date: ".$issue_6->date_due);
722 my $now = dt_from_string();
723 my $five_weeks = DateTime::Duration->new(weeks => 5);
724 my $five_weeks_ago = $now - $five_weeks;
725 t::lib::Mocks::mock_preference('finesMode', 'production');
727 my $issue_7 = AddIssue($renewing_borrower_obj, $item_7->barcode, $five_weeks_ago);
728 is (defined $issue_7, 1, "Item with passed date due checked out, due date: " . $issue_7->date_due);
730 t::lib::Mocks::mock_preference('OverduesBlockRenewing','allow');
731 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_6);
732 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
733 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_7);
734 is( $renewokay, 1, '(Bug 8236), Can renew, this item is overdue but not pref does not block');
736 t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
737 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_6);
738 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is not overdue but patron has overdues');
739 is( $error, 'overdue', "Correct error returned");
740 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_7);
741 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue so patron has overdues');
742 is( $error, 'overdue', "Correct error returned");
744 t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
745 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_6);
746 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
747 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_7);
748 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
749 is( $error, 'overdue', "Correct error returned");
751 my ( $fine ) = CalcFine( $item_7->unblessed, $renewing_borrower_obj->categorycode, $branch, $five_weeks_ago, $now );
752 C4::Overdues::UpdateFine(
754 issue_id => $issue_7->id(),
755 itemnumber => $item_7->itemnumber,
756 borrowernumber => $renewing_borrower_obj->borrowernumber,
758 due => Koha::DateUtils::output_pref($five_weeks_ago)
762 # Make sure fine calculation isn't skipped when adding renewal
763 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
765 # Calculate new due-date based on the present date not to incur
767 t::lib::Mocks::mock_preference('RenewalPeriodBase', 'now');
769 my $staff = $builder->build_object({ class => "Koha::Patrons" });
770 t::lib::Mocks::mock_userenv({ patron => $staff });
772 t::lib::Mocks::mock_preference('RenewalLog', 0);
773 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
774 my %params_renewal = (
775 timestamp => { -like => $date . "%" },
776 module => "CIRCULATION",
780 timestamp => { -like => $date . "%" },
781 module => "CIRCULATION",
784 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
785 my $dt = dt_from_string();
786 Time::Fake->offset( $dt->epoch );
787 my $datedue1 = AddRenewal(
789 borrowernumber => $renewing_borrower_obj->borrowernumber,
790 itemnumber => $item_7->itemnumber,
794 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
795 is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
796 isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
799 t::lib::Mocks::mock_preference('RenewalLog', 1);
800 $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
801 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
804 borrowernumber => $renewing_borrower_obj->borrowernumber,
805 itemnumber => $item_7->itemnumber,
809 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
810 is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
812 my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower_obj->borrowernumber, itemnumber => $item_7->itemnumber } );
813 is( $fines->count, 1, 'AddRenewal left fine' );
814 is( $fines->next->status, 'RENEWED', 'Fine on renewed item is closed out properly' );
817 my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
818 my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
820 $renewing_borrower_obj,
822 Koha::DateUtils::output_pref({str=>$issue_6->date_due, dateformat =>'iso'}),
827 ); # TODO: Already issued???
828 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
829 is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
830 $new_log_size = Koha::ActionLogs->count( \%params_issue );
831 is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
833 $hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next;
837 # Test automatic renewal before value for "norenewalbefore" in policy is set
838 # In this case automatic renewal should still be permitted
839 my $item_4 = $builder->build_sample_item(
841 biblionumber => $biblio->biblionumber,
843 replacementprice => 16.00,
848 my $auto_renew_issue =
849 AddIssue( $renewing_borrower_obj, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
851 ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
852 is( $info->{soonest_renew_date}, undef, "soonest_renew_date is not returned because this issue can be renewed" );
853 is( $renewokay, 1, 'Bug 25393: Can do a manual renew, even if renewal is automatic and premature' );
855 $error, 'auto_renew',
856 'Bug 25393: Can do a manual renew, even if renewal is automatic and premature, norenewalbefore = undef -> manual renewals are allowed at any time'
859 my ( $renewokay_cron, $error_cron, $info_cron ) =
860 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, undef, 1 );
862 $info_cron->{soonest_renew_date}, dt_from_string( $auto_renew_issue->date_due ),
863 "Due date is returned as earliest renewal date when the CanBookBeRenewed check is done by cron and error is 'auto_too_soon'"
867 branchcode => $branch,
868 borrowernumber => $reserving_borrowernumber,
869 biblionumber => $biblio->biblionumber,
870 itemnumber => $bibitems,
871 priority => $priority,
872 reservation_date => $resdate,
873 expiration_date => $expdate,
876 itemnumber => $item_4->itemnumber,
880 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
881 is( $renewokay, 0, 'Still should not be able to renew' );
882 is( $error, 'on_reserve', 'returned code is on_reserve, reserve checked when not checking for cron' );
883 ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, undef, 1 );
884 is( $renewokay, 0, 'Still should not be able to renew' );
885 is( $error, 'auto_too_soon', 'returned code is auto_too_soon, reserve not checked when checking for cron' );
886 is( $info->{soonest_renew_date}, dt_from_string($auto_renew_issue->date_due), "Due date is returned as earliest renewal date when error is 'auto_too_soon'" );
887 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, 1 );
888 is( $renewokay, 0, 'Still should not be able to renew' );
889 is( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
890 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, 1, 1 );
891 is( $renewokay, 0, 'Still should not be able to renew' );
892 is( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
893 $dbh->do('UPDATE circulation_rules SET rule_value = 0 where rule_name = "norenewalbefore"');
894 Koha::Cache::Memory::Lite->flush();
895 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, 1 );
896 is( $renewokay, 0, 'Still should not be able to renew' );
897 is( $error, 'on_reserve', 'returned code is on_reserve, auto_renew only happens if not on reserve' );
898 ModReserveCancelAll($item_4->itemnumber, $reserving_borrowernumber);
900 $renewing_borrower_obj = Koha::Patrons->find($renewing_borrower_obj->borrowernumber);
901 $renewing_borrower_obj->autorenew_checkouts(0)->store;
902 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
903 is( $renewokay, 1, 'No renewal before is undef, but patron opted out of auto_renewal' );
904 $renewing_borrower_obj->autorenew_checkouts(1)->store;
908 # Test premature manual renewal
909 Koha::CirculationRules->set_rule(
911 categorycode => undef,
914 rule_name => 'norenewalbefore',
919 ( $renewokay, $error, $info ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
920 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
921 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
922 is( $info->{soonest_renew_date}, dt_from_string($issue_1->date_due)->subtract( days => 7 ), "Soonest renew date returned when error is 'too_soon'");
925 # Test premature automatic renewal
926 Koha::CirculationRules->set_rule(
928 categorycode => undef,
931 rule_name => 'noautorenewalbefore',
936 ( $renewokay, $error, $info ) =
937 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
938 is( $renewokay, 0, 'Bug 25393: Cannot renew, renewal is premature' );
939 is( $error, 'too_soon',
940 'Bug 25393: Cannot renew, renewal is premature (returned code is too_soon)'
943 $info->{soonest_renew_date}, dt_from_string( $auto_renew_issue->date_due )->subtract( days => 7 ),
944 "Soonest renew date returned when error is 'auto_too_soon'"
947 ( $renewokay_cron, $error_cron, $info_cron ) =
948 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, undef, 1 );
950 $error_cron, 'auto_too_soon',
951 'Bug 25393: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
953 $renewing_borrower_obj->autorenew_checkouts(0)->store;
954 ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
955 is( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
956 is( $error, 'too_soon', 'Error is too_soon, no auto' );
957 is( $info->{soonest_renew_date}, dt_from_string($auto_renew_issue->date_due)->subtract( days => 7 ), "Soonest renew date returned when error is 'too_soon'");
958 $renewing_borrower_obj->autorenew_checkouts(1)->store;
960 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
961 # and test automatic renewal again
962 $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
963 Koha::Cache::Memory::Lite->flush();
964 ( $renewokay, $error, $info ) =
965 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
966 is( $renewokay, 0, 'Bug 25393: Cannot manual renew, renewal is too soon' );
967 is( $error, 'too_soon',
968 'Bug 25393: Cannot manual renew, renewal is too soon, even though it\'s also scheduled for auto renewal'
970 is( $info->{soonest_renew_date}, dt_from_string($auto_renew_issue->date_due), "Soonest renew date returned when error is 'auto_too_soon'");
972 # Now we test for noautorenewalbefore if cron
973 $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'noautorenewalbefore'});
974 Koha::Cache::Memory::Lite->flush();
975 ( $renewokay, $error, $info ) =
976 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, undef, 1 );
977 is( $renewokay, 0, 'Bug 25393: Cannot auto renew, renewal is too soon' );
978 is( $error, 'auto_too_soon',
979 'Bug 25393: Cannot auto renew, auto renewal is too soon'
981 is( $info->{soonest_renew_date}, dt_from_string($auto_renew_issue->date_due), "Soonest renew date returned when error is 'auto_too_soon'");
983 $renewing_borrower_obj->autorenew_checkouts(0)->store;
984 ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
985 is( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
986 is( $error, 'too_soon', 'Error is too_soon, no auto' );
987 is( $info->{soonest_renew_date}, dt_from_string($auto_renew_issue->date_due), "Soonest renew date returned when error is 'auto_too_soon'");
988 $renewing_borrower_obj->autorenew_checkouts(1)->store;
990 # Change policy so that loans can be renewed 99 days prior to the due date
991 # and test automatic renewal again
992 $dbh->do(q{UPDATE circulation_rules SET rule_value = '99' WHERE rule_name = 'norenewalbefore'});
993 Koha::Cache::Memory::Lite->flush();
994 ( $renewokay, $error ) =
995 CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
996 is( $renewokay, 1, 'Bug 25393: Can do a manual renew, even if renewal is automatic and premature' );
997 is( $error, 'auto_renew',
998 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
1001 # Now we test for noautorenewalbefore if cron
1002 $dbh->do(q{UPDATE circulation_rules SET rule_value = '99' WHERE rule_name = 'noautorenewalbefore'});
1003 Koha::Cache::Memory::Lite->flush();
1004 ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue, undef, 1 );
1005 is( $renewokay, 1, 'Bug 25393: Can auto renew' );
1006 is( $error, 'auto_renew', 'Bug 25393: Can auto renew' );
1007 is( $info->{soonest_renew_date}, undef, "soonest_renew_date is not returned because this issue can be renewed" );
1009 $renewing_borrower_obj->autorenew_checkouts(0)->store;
1010 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $auto_renew_issue );
1011 is( $renewokay, 1, 'No renewal before is 99, patron opted out of auto_renewal so can renew' );
1012 $renewing_borrower_obj->autorenew_checkouts(1)->store;
1014 subtest "too_late_renewal / no_auto_renewal_after" => sub {
1016 my $item_to_auto_renew = $builder->build_sample_item(
1018 biblionumber => $biblio->biblionumber,
1023 my $ten_days_before = dt_from_string->add( days => -10 );
1024 my $ten_days_ahead = dt_from_string->add( days => 10 );
1025 my $issue = AddIssue( $renewing_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1027 Koha::CirculationRules->set_rules(
1029 categorycode => undef,
1030 branchcode => undef,
1033 norenewalbefore => '7',
1034 no_auto_renewal_after => '9',
1038 ( $renewokay, $error ) =
1039 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1040 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1041 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
1043 Koha::CirculationRules->set_rules(
1045 categorycode => undef,
1046 branchcode => undef,
1049 norenewalbefore => '7',
1050 no_auto_renewal_after => '10',
1054 ( $renewokay, $error ) =
1055 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1056 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1057 is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
1059 Koha::CirculationRules->set_rules(
1061 categorycode => undef,
1062 branchcode => undef,
1065 norenewalbefore => '7',
1066 no_auto_renewal_after => '11',
1070 ( $renewokay, $error ) =
1071 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1072 is( $renewokay, 0, 'Do not renew, too_soon' );
1073 is( $error, 'too_soon', 'Cannot renew, too_soon - no_auto_renewal_after is defined(returned code is too_soon)' );
1075 # Now check for cron
1076 Koha::CirculationRules->set_rules(
1078 categorycode => undef,
1079 branchcode => undef,
1082 noautorenewalbefore => '9',
1083 no_auto_renewal_after => '11',
1087 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $issue, undef, 1 );
1088 is( $renewokay, 0, 'Do not renew, too_soon' );
1090 $error, 'auto_too_soon',
1091 'Cannot renew, too_soon - no_auto_renewal_after is defined(returned code is too_soon)'
1094 Koha::CirculationRules->set_rules(
1096 categorycode => undef,
1097 branchcode => undef,
1100 norenewalbefore => '10',
1101 no_auto_renewal_after => '11',
1105 ( $renewokay, $error ) =
1106 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1107 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1108 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
1110 Koha::CirculationRules->set_rules(
1112 categorycode => undef,
1113 branchcode => undef,
1116 norenewalbefore => '10',
1117 no_auto_renewal_after => undef,
1118 no_auto_renewal_after_hard_limit => dt_from_string->add( days => -1 ),
1122 ( $renewokay, $error ) =
1123 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1124 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1125 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
1127 Koha::CirculationRules->set_rules(
1129 categorycode => undef,
1130 branchcode => undef,
1133 norenewalbefore => '7',
1134 no_auto_renewal_after => '15',
1135 no_auto_renewal_after_hard_limit => dt_from_string->add( days => -1 ),
1139 ( $renewokay, $error ) =
1140 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1141 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1142 is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
1144 Koha::CirculationRules->set_rules(
1146 categorycode => undef,
1147 branchcode => undef,
1150 norenewalbefore => '10',
1151 no_auto_renewal_after => undef,
1152 no_auto_renewal_after_hard_limit => dt_from_string->add( days => 1 ),
1156 ( $renewokay, $error ) =
1157 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1158 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1159 is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
1162 subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew & OPACFineNoRenewalsIncludeCredit" => sub {
1164 my $item_to_auto_renew = $builder->build_sample_item(
1166 biblionumber => $biblio->biblionumber,
1171 my $ten_days_before = dt_from_string->add( days => -10 );
1172 my $ten_days_ahead = dt_from_string->add( days => 10 );
1173 my $issue = AddIssue( $renewing_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1175 Koha::CirculationRules->set_rules(
1177 categorycode => undef,
1178 branchcode => undef,
1181 norenewalbefore => '10',
1182 no_auto_renewal_after => '11',
1186 C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
1187 C4::Context->set_preference('OPACFineNoRenewals','10');
1188 C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','1');
1189 my $fines_amount = 5;
1190 my $account = Koha::Account->new({patron_id => $renewing_borrowernumber});
1191 $account->add_debit(
1193 amount => $fines_amount,
1194 interface => 'test',
1196 item_id => $item_to_auto_renew->itemnumber,
1197 description => "Some fines"
1199 )->status('RETURNED')->store;
1200 ( $renewokay, $error ) =
1201 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1202 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1203 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
1205 $account->add_debit(
1207 amount => $fines_amount,
1208 interface => 'test',
1210 item_id => $item_to_auto_renew->itemnumber,
1211 description => "Some fines"
1213 )->status('RETURNED')->store;
1214 ( $renewokay, $error ) =
1215 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1216 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1217 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
1219 $account->add_debit(
1221 amount => $fines_amount,
1222 interface => 'test',
1224 item_id => $item_to_auto_renew->itemnumber,
1225 description => "Some fines"
1227 )->status('RETURNED')->store;
1228 ( $renewokay, $error ) =
1229 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1230 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1231 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
1233 $account->add_credit(
1235 amount => $fines_amount,
1236 interface => 'test',
1238 description => "Some payment"
1241 ( $renewokay, $error ) =
1242 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1243 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1244 is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
1246 C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','0');
1247 ( $renewokay, $error ) =
1248 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1249 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1250 is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
1252 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
1253 C4::Context->set_preference('OPACFineNoRenewalsIncludeCredit','1');
1256 subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
1258 my $item_to_auto_renew = $builder->build_sample_item(
1260 biblionumber => $biblio->biblionumber,
1265 Koha::CirculationRules->set_rules(
1267 categorycode => undef,
1268 branchcode => undef,
1271 norenewalbefore => 10,
1272 no_auto_renewal_after => 11,
1277 my $ten_days_before = dt_from_string->add( days => -10 );
1278 my $ten_days_ahead = dt_from_string->add( days => 10 );
1280 # Patron is expired and BlockExpiredPatronOpacActions=0
1281 # => auto renew is allowed
1282 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
1283 my $issue = AddIssue( $expired_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1284 ( $renewokay, $error ) =
1285 CanBookBeRenewed( $expired_borrower_obj, $issue );
1286 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1287 is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
1288 Koha::Checkouts->find( $issue->issue_id )->delete;
1291 # Patron is expired and BlockExpiredPatronOpacActions=1
1292 # => auto renew is not allowed
1293 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
1294 $issue = AddIssue( $expired_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1295 ( $renewokay, $error ) =
1296 CanBookBeRenewed( $expired_borrower_obj, $issue );
1297 is( $renewokay, 0, 'Do not renew, renewal is automatic' );
1298 is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
1301 # Patron is not expired and BlockExpiredPatronOpacActions=1
1302 # => auto renew is allowed
1303 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
1304 $issue = AddIssue( $renewing_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1305 ( $renewokay, $error ) =
1306 CanBookBeRenewed( $renewing_borrower_obj, $issue );
1307 is( $renewokay, 1, 'Renew, even if renewal is automatic' );
1308 is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
1312 subtest "GetLatestAutoRenewDate" => sub {
1314 my $item_to_auto_renew = $builder->build_sample_item(
1316 biblionumber => $biblio->biblionumber,
1321 my $ten_days_before = dt_from_string->add( days => -10 );
1322 my $ten_days_ahead = dt_from_string->add( days => 10 );
1323 my $issue = AddIssue( $renewing_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
1324 Koha::CirculationRules->set_rules(
1326 categorycode => undef,
1327 branchcode => undef,
1330 norenewalbefore => '7',
1331 no_auto_renewal_after => '',
1332 no_auto_renewal_after_hard_limit => undef,
1336 my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrower_obj, $issue );
1337 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' );
1338 my $five_days_before = dt_from_string->add( days => -5 );
1339 Koha::CirculationRules->set_rules(
1341 categorycode => undef,
1342 branchcode => undef,
1345 norenewalbefore => '10',
1346 no_auto_renewal_after => '5',
1347 no_auto_renewal_after_hard_limit => undef,
1351 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrower_obj,, $issue );
1352 is( $latest_auto_renew_date->truncate( to => 'minute' ),
1353 $five_days_before->truncate( to => 'minute' ),
1354 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
1356 my $five_days_ahead = dt_from_string->add( days => 5 );
1357 $dbh->do(q{UPDATE circulation_rules SET rule_value = '10' WHERE rule_name = 'norenewalbefore'});
1358 $dbh->do(q{UPDATE circulation_rules SET rule_value = '15' WHERE rule_name = 'no_auto_renewal_after'});
1359 $dbh->do(q{UPDATE circulation_rules SET rule_value = NULL WHERE rule_name = 'no_auto_renewal_after_hard_limit'});
1360 Koha::Cache::Memory::Lite->flush();
1361 Koha::CirculationRules->set_rules(
1363 categorycode => undef,
1364 branchcode => undef,
1367 norenewalbefore => '10',
1368 no_auto_renewal_after => '15',
1369 no_auto_renewal_after_hard_limit => undef,
1373 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrower_obj, $issue );
1374 is( $latest_auto_renew_date->truncate( to => 'minute' ),
1375 $five_days_ahead->truncate( to => 'minute' ),
1376 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
1378 my $two_days_ahead = dt_from_string->add( days => 2 );
1379 Koha::CirculationRules->set_rules(
1381 categorycode => undef,
1382 branchcode => undef,
1385 norenewalbefore => '10',
1386 no_auto_renewal_after => '',
1387 no_auto_renewal_after_hard_limit => dt_from_string->add( days => 2 ),
1391 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrower_obj, $issue );
1392 is( $latest_auto_renew_date->truncate( to => 'day' ),
1393 $two_days_ahead->truncate( to => 'day' ),
1394 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
1396 Koha::CirculationRules->set_rules(
1398 categorycode => undef,
1399 branchcode => undef,
1402 norenewalbefore => '10',
1403 no_auto_renewal_after => '15',
1404 no_auto_renewal_after_hard_limit => dt_from_string->add( days => 2 ),
1408 $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrower_obj, $issue );
1409 is( $latest_auto_renew_date->truncate( to => 'day' ),
1410 $two_days_ahead->truncate( to => 'day' ),
1411 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
1416 subtest "auto_renew_final" => sub {
1418 my $item_to_auto_renew = $builder->build_sample_item();
1419 Koha::CirculationRules->set_rules(
1421 categorycode => undef,
1422 branchcode => undef,
1423 itemtype => $item_to_auto_renew->itype,
1425 norenewalbefore => undef,
1426 renewalsallowed => 1,
1431 my $ten_days_before = dt_from_string->add( days => -10 );
1432 my $ten_days_ahead = dt_from_string->add( days => 10 );
1433 my $issue = AddIssue(
1434 $renewing_borrower_obj, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before,
1435 undef, { auto_renew => 1 }
1437 $issue->renewals_count(0)->store;
1439 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $issue );
1440 is( $renewokay, 1, 'Success for auto_renewal' );
1441 is( $error, 'auto_renew_final', 'Auto renewal allowed, but it is the last one' );
1443 # Final unseen renewal
1444 Koha::CirculationRules->set_rules(
1446 categorycode => undef,
1447 branchcode => undef,
1448 itemtype => $item_to_auto_renew->itype,
1450 unseen_renewals_allowed => 2,
1451 renewalsallowed => 10,
1455 t::lib::Mocks::mock_preference( 'UnseenRenewals', 1 );
1456 $issue->unseen_renewals(1)->renewals_count(1)->store;
1458 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $issue );
1459 is( $renewokay, 1, 'Success for auto_renewal' );
1460 is( $error, 'auto_unseen_final', 'Final unseen renewal reported, not final overall' );
1462 # Final unseen renewal
1463 Koha::CirculationRules->set_rules(
1465 categorycode => undef,
1466 branchcode => undef,
1467 itemtype => $item_to_auto_renew->itype,
1469 unseen_renewals_allowed => 2,
1470 renewalsallowed => 2,
1474 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $issue );
1475 is( $renewokay, 1, 'Success for auto_renewal' );
1476 is( $error, 'auto_renew_final', 'Final auto renewal reported' );
1481 # set policy to forbid renewals
1482 Koha::CirculationRules->set_rules(
1484 categorycode => undef,
1485 branchcode => undef,
1488 norenewalbefore => undef,
1489 renewalsallowed => 0,
1494 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
1495 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
1496 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
1498 # Too many unseen renewals
1499 Koha::CirculationRules->set_rules(
1501 categorycode => undef,
1502 branchcode => undef,
1505 unseen_renewals_allowed => 2,
1506 renewalsallowed => 10,
1510 t::lib::Mocks::mock_preference('UnseenRenewals', 1);
1512 $issue_1->unseen_renewals(2)->store;
1514 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrower_obj, $issue_1);
1515 is( $renewokay, 0, 'Cannot renew, 0 unseen renewals allowed');
1516 is( $error, 'too_unseen', 'Cannot renew, returned code is too_unseen');
1517 Koha::CirculationRules->set_rules(
1519 categorycode => undef,
1520 branchcode => undef,
1523 norenewalbefore => undef,
1524 renewalsallowed => 0,
1528 t::lib::Mocks::mock_preference('UnseenRenewals', 0);
1530 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
1531 t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
1532 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
1534 C4::Overdues::UpdateFine(
1536 issue_id => $issue_1->id(),
1537 itemnumber => $item_1->itemnumber,
1538 borrowernumber => $renewing_borrower_obj->borrowernumber,
1541 due => Koha::DateUtils::output_pref($datedue)
1545 my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower_obj->borrowernumber })->next();
1546 is( $line->debit_type_code, 'OVERDUE', 'Account line type is OVERDUE' );
1547 is( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
1548 is( $line->amountoutstanding+0, 15, 'Account line amount outstanding is 15.00' );
1549 is( $line->amount+0, 15, 'Account line amount is 15.00' );
1550 is( $line->issue_id, $issue_1->id, 'Account line issue id matches' );
1552 my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
1553 is( $offset->type, 'CREATE', 'Account offset type is CREATE' );
1554 is( $offset->amount+0, 15, 'Account offset amount is 15.00' );
1556 t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
1557 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
1559 LostItem( $item_1->itemnumber, 'test', 1 );
1561 $line = Koha::Account::Lines->find($line->id);
1562 is( $line->debit_type_code, 'OVERDUE', 'Account type remains as OVERDUE' );
1563 isnt( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
1565 my $item = Koha::Items->find($item_1->itemnumber);
1566 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
1567 my $checkout = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber });
1568 is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
1570 my $total_due = $dbh->selectrow_array(
1571 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
1572 undef, $renewing_borrower_obj->borrowernumber
1575 is( $total_due+0, 15, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
1577 C4::Context->dbh->do("DELETE FROM accountlines");
1579 C4::Overdues::UpdateFine(
1581 issue_id => $issue_2->id(),
1582 itemnumber => $item_2->itemnumber,
1583 borrowernumber => $renewing_borrower_obj->borrowernumber,
1586 due => Koha::DateUtils::output_pref($datedue)
1590 LostItem( $item_2->itemnumber, 'test', 0 );
1592 my $item2 = Koha::Items->find($item_2->itemnumber);
1593 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
1594 ok( Koha::Checkouts->find({ itemnumber => $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
1596 $total_due = $dbh->selectrow_array(
1597 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
1598 undef, $renewing_borrower_obj->borrowernumber
1601 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
1603 my $future = dt_from_string();
1604 $future->add( days => 7 );
1605 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
1606 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
1608 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1609 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
1610 t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
1611 $checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
1612 LostItem( $item_3->itemnumber, 'test', 0 );
1613 my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
1614 is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
1616 $accountline->description,
1617 sprintf( "%s %s %s",
1618 $item_3->biblio->title || '',
1619 $item_3->barcode || '',
1620 $item_3->itemcallnumber || '' ),
1621 "Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
1625 t::lib::Mocks::mock_preference('UseRecalls', 1);
1626 Koha::CirculationRules->set_rules({
1627 categorycode => undef,
1628 branchcode => undef,
1631 recalls_allowed => 10,
1632 renewalsallowed => 5,
1635 my $recall_borrower = $builder->build_object({ class => 'Koha::Patrons' });
1636 my $recall_biblio = $builder->build_sample_biblio();
1637 my $recall_item1 = $builder->build_sample_item({ biblionumber => $recall_biblio->biblionumber });
1638 my $recall_item2 = $builder->build_sample_item({ biblionumber => $recall_biblio->biblionumber });
1640 my $recall_issue = AddIssue( $renewing_borrower_obj, $recall_item1->barcode );
1642 # item-level and this item: renewal not allowed
1643 my $recall = Koha::Recall->new({
1644 biblio_id => $recall_item1->biblionumber,
1645 item_id => $recall_item1->itemnumber,
1646 patron_id => $recall_borrower->borrowernumber,
1647 pickup_library_id => $recall_borrower->branchcode,
1650 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $recall_issue );
1651 is( $error, 'recalled', 'Cannot renew item that has been recalled' );
1652 $recall->set_cancelled;
1654 # biblio-level requested recall: renewal not allowed
1655 $recall = Koha::Recall->new({
1656 biblio_id => $recall_item1->biblionumber,
1658 patron_id => $recall_borrower->borrowernumber,
1659 pickup_library_id => $recall_borrower->branchcode,
1662 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $recall_issue );
1663 is( $error, 'recalled', 'Cannot renew item if biblio is recalled and has no item allocated' );
1664 $recall->set_cancelled;
1666 # item-level and not this item: renewal allowed
1667 $recall = Koha::Recall->new({
1668 biblio_id => $recall_item2->biblionumber,
1669 item_id => $recall_item2->itemnumber,
1670 patron_id => $recall_borrower->borrowernumber,
1671 pickup_library_id => $recall_borrower->branchcode,
1674 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $recall_issue );
1675 is( $renewokay, 1, 'Can renew item if item-level recall on biblio is not on this item' );
1676 $recall->set_cancelled;
1678 # biblio-level waiting recall: renewal allowed
1679 $recall = Koha::Recall->new({
1680 biblio_id => $recall_item1->biblionumber,
1682 patron_id => $recall_borrower->borrowernumber,
1683 pickup_library_id => $recall_borrower->branchcode,
1686 $recall->set_waiting({ item => $recall_item1 });
1687 ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrower_obj, $recall_issue );
1688 is( $renewokay, 1, 'Can renew item if biblio-level recall has already been allocated an item' );
1689 $recall->set_cancelled;
1692 subtest "GetUpcomingDueIssues" => sub {
1695 my $branch = $library2->{branchcode};
1697 #Create another record
1698 my $biblio2 = $builder->build_sample_biblio();
1701 my $item_1 = Koha::Items->find($reused_itemnumber_1);
1702 my $item_2 = Koha::Items->find($reused_itemnumber_2);
1703 my $item_3 = $builder->build_sample_item(
1705 biblionumber => $biblio2->biblionumber,
1713 my %a_borrower_data = (
1714 firstname => 'Fridolyn',
1715 surname => 'SOMERS',
1716 categorycode => $patron_category->{categorycode},
1717 branchcode => $branch,
1720 my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1721 my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber );
1723 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
1724 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
1725 my $today = DateTime->today(time_zone => C4::Context->tz());
1727 my $issue = AddIssue( $a_borrower, $item_1->barcode, $yesterday );
1728 my $datedue = dt_from_string( $issue->date_due() );
1729 my $issue_2 = AddIssue( $a_borrower, $item_2->barcode, $two_days_ahead );
1730 my $datedue2 = dt_from_string( $issue->date_due() );
1734 # GetUpcomingDueIssues tests
1736 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1737 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
1740 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
1741 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1742 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
1745 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1746 is ( scalar( @$upcoming_dues ), 1,
1747 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
1750 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
1752 my $issue3 = AddIssue( $a_borrower, $item_3->barcode, $today );
1754 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
1755 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
1757 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
1758 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
1760 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
1761 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
1763 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1764 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1766 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
1767 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1769 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
1770 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1774 subtest "Bug 13841 - Do not create new 0 amount fines" => sub {
1775 my $branch = $library2->{branchcode};
1777 my $biblio = $builder->build_sample_biblio();
1780 my $item = $builder->build_sample_item(
1782 biblionumber => $biblio->biblionumber,
1789 my %a_borrower_data = (
1790 firstname => 'Kyle',
1792 categorycode => $patron_category->{categorycode},
1793 branchcode => $branch,
1796 my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1798 my $borrower = Koha::Patrons->find( $borrowernumber );
1799 my $issue = AddIssue( $borrower, $item->barcode );
1802 issue_id => $issue->id(),
1803 itemnumber => $item->itemnumber,
1804 borrowernumber => $borrowernumber,
1810 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1811 my $count = $hr->{count};
1813 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1816 subtest "AllowRenewalIfOtherItemsAvailable tests" => sub {
1818 my $biblio = $builder->build_sample_biblio();
1819 my $item_1 = $builder->build_sample_item(
1821 biblionumber => $biblio->biblionumber,
1822 library => $library2->{branchcode},
1825 my $item_2= $builder->build_sample_item(
1827 biblionumber => $biblio->biblionumber,
1828 library => $library2->{branchcode},
1829 itype => $item_1->effective_itemtype,
1833 Koha::CirculationRules->set_rules(
1835 categorycode => undef,
1836 itemtype => $item_1->effective_itemtype,
1837 branchcode => undef,
1839 reservesallowed => 25,
1840 holds_per_record => 25,
1842 lengthunit => 'days',
1843 renewalsallowed => 1,
1845 norenewalbefore => undef,
1855 my $borrower1 = Koha::Patron->new({
1856 firstname => 'Kyle',
1858 categorycode => $patron_category->{categorycode},
1859 branchcode => $library2->{branchcode},
1861 my $borrowernumber2 = Koha::Patron->new({
1862 firstname => 'Chelsea',
1864 categorycode => $patron_category->{categorycode},
1865 branchcode => $library2->{branchcode},
1866 })->store->borrowernumber;
1867 my $patron_category_2 = $builder->build(
1869 source => 'Category',
1871 category_type => 'P',
1873 BlockExpiredPatronOpacActions => -1, # Pick the pref value
1877 my $borrowernumber3 = Koha::Patron->new({
1878 firstname => 'Carnegie',
1880 categorycode => $patron_category_2->{categorycode},
1881 branchcode => $library2->{branchcode},
1882 })->store->borrowernumber;
1884 my $issue = AddIssue( $borrower1, $item_1->barcode );
1886 my ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1887 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1891 branchcode => $library2->{branchcode},
1892 borrowernumber => $borrowernumber2,
1893 biblionumber => $biblio->biblionumber,
1898 Koha::CirculationRules->set_rules(
1900 categorycode => undef,
1901 itemtype => $item_1->effective_itemtype,
1902 branchcode => undef,
1908 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1909 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1910 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1912 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1913 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1914 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1916 Koha::CirculationRules->set_rules(
1918 categorycode => undef,
1919 itemtype => $item_1->effective_itemtype,
1920 branchcode => undef,
1926 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1927 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1928 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1930 t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1931 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1932 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1936 branchcode => $library2->{branchcode},
1937 borrowernumber => $borrowernumber3,
1938 biblionumber => $biblio->biblionumber,
1943 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1944 is( $renewokay, 0, 'Verify the borrower cannot renew with 2 holds on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled and one other item on record' );
1946 my $item_3= $builder->build_sample_item(
1948 biblionumber => $biblio->biblionumber,
1949 library => $library2->{branchcode},
1950 itype => $item_1->effective_itemtype,
1954 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1955 is( $renewokay, 1, 'Verify the borrower cannot renew with 2 holds on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled and two other items on record' );
1957 Koha::CirculationRules->set_rules(
1959 categorycode => $patron_category_2->{categorycode},
1960 itemtype => $item_1->effective_itemtype,
1961 branchcode => undef,
1963 reservesallowed => 0,
1968 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1969 is( $renewokay, 0, 'Verify the borrower cannot renew with 2 holds on the record, but only one of those holds can be filled when AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled and two other items on record' );
1971 Koha::CirculationRules->set_rules(
1973 categorycode => $patron_category_2->{categorycode},
1974 itemtype => $item_1->effective_itemtype,
1975 branchcode => undef,
1977 reservesallowed => 25,
1982 # Setting item not checked out to be not for loan but holdable
1983 $item_2->notforloan(-1)->store;
1985 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
1986 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' );
1988 my $mock_circ = Test::MockModule->new("C4::Circulation");
1989 $mock_circ->mock( CanItemBeReserved => sub {
1991 return { status => 'no' }
1994 $item_2->notforloan(0)->store;
1996 # Two items total, one item available, one issued, two holds on record
1999 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
2000 } [], "CanItemBeReserved not called when there are more possible holds than available items";
2001 is( $renewokay, 0, 'Borrower cannot renew when there are more holds than available items' );
2003 $item_3 = $builder->build_sample_item(
2005 biblionumber => $biblio->biblionumber,
2006 library => $library2->{branchcode},
2007 itype => $item_1->effective_itemtype,
2011 Koha::CirculationRules->set_rules(
2013 categorycode => undef,
2014 itemtype => $item_1->effective_itemtype,
2015 branchcode => undef,
2017 reservesallowed => 0,
2023 ( $renewokay, $error ) = CanBookBeRenewed( $borrower1, $issue );
2024 } ["Checked","Checked"], "CanItemBeReserved only called once per available item if it returns a negative result for all items for a borrower";
2025 is( $renewokay, 0, 'Borrower cannot renew when there are more holds than available items' );
2030 # Don't allow renewing onsite checkout
2031 my $branch = $library->{branchcode};
2033 #Create another record
2034 my $biblio = $builder->build_sample_biblio();
2036 my $item = $builder->build_sample_item(
2038 biblionumber => $biblio->biblionumber,
2044 my $borrower = Koha::Patron->new({
2047 categorycode => $patron_category->{categorycode},
2048 branchcode => $branch,
2051 my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
2052 my ( $renewed, $error ) = CanBookBeRenewed( $borrower, $issue );
2053 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
2054 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
2058 my $library = $builder->build({ source => 'Branch' });
2060 my $biblio = $builder->build_sample_biblio();
2062 my $item = $builder->build_sample_item(
2064 biblionumber => $biblio->biblionumber,
2065 library => $library->{branchcode},
2069 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2071 my $issue = AddIssue( $patron, $item->barcode );
2074 issue_id => $issue->id,
2075 itemnumber => $item->itemnumber,
2076 borrowernumber => $patron->borrowernumber,
2083 issue_id => $issue->id,
2084 itemnumber => $item->itemnumber,
2085 borrowernumber => $patron->borrowernumber,
2090 is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
2093 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
2096 my $homebranch = $builder->build( { source => 'Branch' } );
2097 my $holdingbranch = $builder->build( { source => 'Branch' } );
2098 my $otherbranch = $builder->build( { source => 'Branch' } );
2099 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2100 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2102 my $item = $builder->build_sample_item(
2104 homebranch => $homebranch->{branchcode},
2105 holdingbranch => $holdingbranch->{branchcode},
2108 Koha::CirculationRules->set_rules(
2110 categorycode => undef,
2111 itemtype => $item->effective_itemtype,
2112 branchcode => undef,
2114 reservesallowed => 25,
2116 lengthunit => 'days',
2117 renewalsallowed => 1,
2119 norenewalbefore => undef,
2128 set_userenv($holdingbranch);
2130 my $issue = AddIssue( $patron_1, $item->barcode );
2131 is( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
2133 my ( $error, $question, $alerts );
2135 # AllowReturnToBranch == anywhere
2136 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
2137 ## Test that unknown barcodes don't generate internal server errors
2138 set_userenv($homebranch);
2139 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
2140 ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
2141 ## Can be issued from homebranch
2142 set_userenv($homebranch);
2143 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2144 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2145 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
2146 ## Can be issued from holdingbranch
2147 set_userenv($holdingbranch);
2148 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2149 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2150 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
2151 ## Can be issued from another branch
2152 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2153 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2154 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
2156 # AllowReturnToBranch == holdingbranch
2157 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
2158 ## Cannot be issued from homebranch
2159 set_userenv($homebranch);
2160 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2161 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2162 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
2163 is( $error->{branch_to_return}, $holdingbranch->{branchcode}, 'branch_to_return matched holdingbranch' );
2164 ## Can be issued from holdinbranch
2165 set_userenv($holdingbranch);
2166 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2167 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2168 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
2169 ## Cannot be issued from another branch
2170 set_userenv($otherbranch);
2171 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2172 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2173 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
2174 is( $error->{branch_to_return}, $holdingbranch->{branchcode}, 'branch_to_return matches holdingbranch' );
2176 # AllowReturnToBranch == homebranch
2177 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
2178 ## Can be issued from holdinbranch
2179 set_userenv($homebranch);
2180 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2181 is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2182 is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
2183 ## Cannot be issued from holdinbranch
2184 set_userenv($holdingbranch);
2185 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2186 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2187 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
2188 is( $error->{branch_to_return}, $homebranch->{branchcode}, 'branch_to_return matches homebranch' );
2189 ## Cannot be issued from holdinbranch
2190 set_userenv($otherbranch);
2191 ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->barcode );
2192 is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
2193 is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
2194 is( $error->{branch_to_return}, $homebranch->{branchcode}, 'branch_to_return matches homebranch' );
2196 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
2199 subtest 'AddIssue & AllowReturnToBranch' => sub {
2202 my $homebranch = $builder->build( { source => 'Branch' } );
2203 my $holdingbranch = $builder->build( { source => 'Branch' } );
2204 my $otherbranch = $builder->build( { source => 'Branch' } );
2206 my $patron_1 = $builder->build_object({
2207 class => 'Koha::Patrons',
2208 value => { categorycode => $patron_category->{categorycode} }
2211 my $patron_2 = $builder->build_object({
2212 class => 'Koha::Patrons',
2213 value => { categorycode => $patron_category->{categorycode} }
2217 my $item = $builder->build_sample_item(
2219 homebranch => $homebranch->{branchcode},
2220 holdingbranch => $holdingbranch->{branchcode},
2224 set_userenv($holdingbranch);
2226 my $ref_issue = 'Koha::Checkout';
2227 my $issue = AddIssue( $patron_1, $item->barcode );
2229 my ( $error, $question, $alerts );
2231 # AllowReturnToBranch == homebranch
2232 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
2233 ## Can be issued from homebranch
2234 set_userenv($homebranch);
2235 is ( ref( AddIssue( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from homebranch');
2236 set_userenv($holdingbranch); AddIssue( $patron_1, $item->barcode ); # Reinsert the original issue
2237 ## Can be issued from holdinbranch
2238 set_userenv($holdingbranch);
2239 is ( ref( AddIssue( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from holdingbranch');
2240 set_userenv($holdingbranch); AddIssue( $patron_1, $item->barcode ); # Reinsert the original issue
2241 ## Can be issued from another branch
2242 set_userenv($otherbranch);
2243 is ( ref( AddIssue( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from otherbranch');
2244 set_userenv($holdingbranch); AddIssue( $patron_1, $item->barcode ); # Reinsert the original issue
2246 # AllowReturnToBranch == holdinbranch
2247 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
2248 ## Cannot be issued from homebranch
2249 set_userenv($homebranch);
2250 is ( ref( AddIssue( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - holdingbranch | Cannot be issued from homebranch');
2251 ## Can be issued from holdingbranch
2252 set_userenv($holdingbranch);
2253 is ( ref( AddIssue( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - holdingbranch | Can be issued from holdingbranch');
2254 set_userenv($holdingbranch); AddIssue( $patron_1, $item->barcode ); # Reinsert the original issue
2255 ## Cannot be issued from another branch
2256 set_userenv($otherbranch);
2257 is ( ref( AddIssue( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - holdingbranch | Cannot be issued from otherbranch');
2259 # AllowReturnToBranch == homebranch
2260 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
2261 ## Can be issued from homebranch
2262 set_userenv($homebranch);
2263 is ( ref( AddIssue( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - homebranch | Can be issued from homebranch' );
2264 AddIssue( $patron_1, $item->barcode ); # Re-issue to patron 1
2265 ## Cannot be issued from holdinbranch
2266 set_userenv($holdingbranch);
2267 is ( ref( AddIssue( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - homebranch | Cannot be issued from holdingbranch' );
2268 ## Cannot be issued from another branch
2269 set_userenv($otherbranch);
2270 is ( ref( AddIssue( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - homebranch | Cannot be issued from otherbranch' );
2271 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
2274 subtest 'AddIssue | recalls' => sub {
2277 t::lib::Mocks::mock_preference("UseRecalls", 1);
2278 t::lib::Mocks::mock_preference("item-level_itypes", 1);
2279 my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
2280 my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
2281 my $item = $builder->build_sample_item;
2282 Koha::CirculationRules->set_rules({
2283 branchcode => undef,
2285 categorycode => undef,
2287 recalls_allowed => 10,
2291 # checking out item that they have recalled
2292 my $recall1 = Koha::Recall->new(
2293 { patron_id => $patron1->borrowernumber,
2294 biblio_id => $item->biblionumber,
2295 item_id => $item->itemnumber,
2297 pickup_library_id => $patron1->branchcode,
2300 AddIssue( $patron1, $item->barcode, undef, undef, undef, undef, { recall_id => $recall1->id } );
2301 $recall1 = Koha::Recalls->find( $recall1->id );
2302 is( $recall1->fulfilled, 1, 'Recall was fulfilled when patron checked out item' );
2303 AddReturn( $item->barcode, $item->homebranch );
2305 # this item is has a recall request. cancel recall
2306 my $recall2 = Koha::Recall->new(
2307 { patron_id => $patron2->borrowernumber,
2308 biblio_id => $item->biblionumber,
2309 item_id => $item->itemnumber,
2311 pickup_library_id => $patron2->branchcode,
2314 AddIssue( $patron1, $item->barcode, undef, undef, undef, undef, { recall_id => $recall2->id, cancel_recall => 'cancel' } );
2315 $recall2 = Koha::Recalls->find( $recall2->id );
2316 is( $recall2->cancelled, 1, 'Recall was cancelled when patron checked out item' );
2317 AddReturn( $item->barcode, $item->homebranch );
2319 # this item is waiting to fulfill a recall. revert recall
2320 my $recall3 = Koha::Recall->new(
2321 { patron_id => $patron2->borrowernumber,
2322 biblio_id => $item->biblionumber,
2323 item_id => $item->itemnumber,
2325 pickup_library_id => $patron2->branchcode,
2328 $recall3->set_waiting;
2329 AddIssue( $patron1, $item->barcode, undef, undef, undef, undef, { recall_id => $recall3->id, cancel_recall => 'revert' } );
2330 $recall3 = Koha::Recalls->find( $recall3->id );
2331 is( $recall3->requested, 1, 'Recall was reverted from waiting when patron checked out item' );
2332 AddReturn( $item->barcode, $item->homebranch );
2335 subtest 'AddIssue & illrequests.due_date' => sub {
2338 t::lib::Mocks::mock_preference( 'ILLModule', 1 );
2339 my $library = $builder->build( { source => 'Branch' } );
2340 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2341 my $item = $builder->build_sample_item();
2343 set_userenv($library);
2345 my $custom_date_due = '9999-12-18 12:34:56';
2346 my $expected_date_due = '9999-12-18 23:59:00';
2347 my $illrequest = Koha::Illrequest->new({
2348 borrowernumber => $patron->borrowernumber,
2349 biblio_id => $item->biblionumber,
2350 branchcode => $library->{'branchcode'},
2351 due_date => $custom_date_due,
2354 my $issue = AddIssue( $patron, $item->barcode );
2355 is( $issue->date_due, $expected_date_due, 'Custom illrequest date due has been set for this issue');
2357 $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2358 $item = $builder->build_sample_item();
2359 $custom_date_due = '9999-12-19';
2360 $expected_date_due = '9999-12-19 23:59:00';
2361 $illrequest = Koha::Illrequest->new({
2362 borrowernumber => $patron->borrowernumber,
2363 biblio_id => $item->biblionumber,
2364 branchcode => $library->{'branchcode'},
2365 due_date => $custom_date_due,
2368 $issue = AddIssue( $patron, $item->barcode );
2369 is( $issue->date_due, $expected_date_due, 'Custom illrequest date due has been set for this issue');
2372 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
2375 my $library = $builder->build( { source => 'Branch' } );
2376 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2377 my $item_1 = $builder->build_sample_item(
2379 library => $library->{branchcode},
2382 my $item_2 = $builder->build_sample_item(
2384 library => $library->{branchcode},
2387 Koha::CirculationRules->set_rules(
2389 categorycode => undef,
2391 branchcode => $library->{branchcode},
2393 reservesallowed => 25,
2395 lengthunit => 'days',
2396 renewalsallowed => 1,
2398 norenewalbefore => undef,
2407 my ( $error, $question, $alerts );
2409 # Patron cannot issue item_1, they have overdues
2410 my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
2411 my $issue = AddIssue( $patron, $item_1->barcode, $yesterday ); # Add an overdue
2413 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
2414 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2415 is( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str($error, $question, $alerts) );
2416 is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
2418 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
2419 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2420 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
2421 is( $error->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
2423 # Patron cannot issue item_1, they are debarred
2424 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
2425 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
2426 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2427 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
2428 is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
2430 Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
2431 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2432 is( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str($error, $question, $alerts) );
2433 is( $error->{USERBLOCKEDNOENDDATE}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
2436 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
2439 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2440 my $patron_category_x = $builder->build_object(
2442 class => 'Koha::Patron::Categories',
2443 value => { category_type => 'X' }
2446 my $patron = $builder->build_object(
2448 class => 'Koha::Patrons',
2450 categorycode => $patron_category_x->categorycode,
2451 gonenoaddress => undef,
2458 my $item_1 = $builder->build_sample_item(
2460 library => $library->{branchcode},
2464 my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->barcode );
2465 is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
2467 my $stat = Koha::Statistics->search( { itemnumber => $item_1->itemnumber } )->next;
2468 is( $stat->branch, C4::Context->userenv->{'branch'}, 'Recorded a branch' );
2469 is( $stat->type, 'localuse', 'Recorded type as localuse' );
2470 is( $stat->itemnumber, $item_1->itemnumber, 'Recorded an itemnumber' );
2471 is( $stat->itemtype, $item_1->effective_itemtype, 'Recorded an itemtype' );
2472 is( $stat->borrowernumber, $patron->borrowernumber, 'Recorded a borrower number' );
2473 is( $stat->ccode, $item_1->ccode, 'Recorded a collection code' );
2474 is( $stat->categorycode, $patron->categorycode, 'Recorded a categorycode' );
2475 is( $stat->location, $item_1->location, 'Recorded a location' );
2478 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2479 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2480 my $item_2 = $builder->build_sample_item( { library => $library->branchcode } );
2481 my $issue = AddIssue( $patron_2, $item_2->barcode );
2482 $item_2->discard_changes;
2483 ok($item_2->onloan, "Item is checked out");
2485 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2486 $item_2->discard_changes;
2487 ok(!$item_2->onloan, "Checked out item is returned");
2489 # TODO There are other tests to provide here
2492 subtest 'MultipleReserves' => sub {
2495 my $biblio = $builder->build_sample_biblio();
2497 my $branch = $library2->{branchcode};
2499 my $item_1 = $builder->build_sample_item(
2501 biblionumber => $biblio->biblionumber,
2503 replacementprice => 12.00,
2508 my $item_2 = $builder->build_sample_item(
2510 biblionumber => $biblio->biblionumber,
2512 replacementprice => 12.00,
2519 my $resdate = undef;
2520 my $expdate = undef;
2522 my $checkitem = undef;
2525 my %renewing_borrower_data = (
2526 firstname => 'John',
2527 surname => 'Renewal',
2528 categorycode => $patron_category->{categorycode},
2529 branchcode => $branch,
2531 my $patron = Koha::Patron->new(\%renewing_borrower_data)->store;
2532 my $issue = AddIssue( $patron, $item_1->barcode);
2533 my $datedue = dt_from_string( $issue->date_due() );
2534 is (defined $issue->date_due(), 1, "item 1 checked out");
2535 my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber })->borrowernumber;
2537 my %reserving_borrower_data1 = (
2538 firstname => 'Katrin',
2539 surname => 'Reservation',
2540 categorycode => $patron_category->{categorycode},
2541 branchcode => $branch,
2543 my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
2546 branchcode => $branch,
2547 borrowernumber => $reserving_borrowernumber1,
2548 biblionumber => $biblio->biblionumber,
2549 priority => $priority,
2550 reservation_date => $resdate,
2551 expiration_date => $expdate,
2553 itemnumber => $checkitem,
2558 my %reserving_borrower_data2 = (
2559 firstname => 'Kirk',
2560 surname => 'Reservation',
2561 categorycode => $patron_category->{categorycode},
2562 branchcode => $branch,
2564 my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
2567 branchcode => $branch,
2568 borrowernumber => $reserving_borrowernumber2,
2569 biblionumber => $biblio->biblionumber,
2570 priority => $priority,
2571 reservation_date => $resdate,
2572 expiration_date => $expdate,
2574 itemnumber => $checkitem,
2580 my ( $renewokay, $error ) = CanBookBeRenewed($patron, $issue, 1);
2581 is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
2584 my $item_3 = $builder->build_sample_item(
2586 biblionumber => $biblio->biblionumber,
2588 replacementprice => 12.00,
2594 my ( $renewokay, $error ) = CanBookBeRenewed($patron, $issue, 1);
2595 is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
2599 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
2602 my $library = $builder->build( { source => 'Branch' } );
2603 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2605 my $biblionumber = $builder->build_sample_biblio(
2607 branchcode => $library->{branchcode},
2610 my $item_1 = $builder->build_sample_item(
2612 biblionumber => $biblionumber,
2613 library => $library->{branchcode},
2617 my $item_2 = $builder->build_sample_item(
2619 biblionumber => $biblionumber,
2620 library => $library->{branchcode},
2624 Koha::CirculationRules->set_rules(
2626 categorycode => undef,
2628 branchcode => $library->{branchcode},
2630 reservesallowed => 25,
2632 lengthunit => 'days',
2633 renewalsallowed => 1,
2635 norenewalbefore => undef,
2644 my ( $error, $question, $alerts );
2645 my $issue = AddIssue( $patron, $item_1->barcode, dt_from_string->add( days => 1 ) );
2647 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
2648 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2650 { error => $error, alerts => $alerts },
2651 { error => {}, alerts => {} },
2652 'No error or alert should be raised'
2654 is( $question->{BIBLIO_ALREADY_ISSUED}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' );
2656 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
2657 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2659 { error => $error, question => $question, alerts => $alerts },
2660 { error => {}, question => {}, alerts => {} },
2661 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1'
2664 # Add a subscription
2665 Koha::Subscription->new({ biblionumber => $biblionumber })->store;
2667 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
2668 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2670 { error => $error, question => $question, alerts => $alerts },
2671 { error => {}, question => {}, alerts => {} },
2672 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription'
2675 t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
2676 ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->barcode );
2678 { error => $error, question => $question, alerts => $alerts },
2679 { error => {}, question => {}, alerts => {} },
2680 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription'
2684 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
2687 my $library = $builder->build( { source => 'Branch' } );
2688 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2691 my $biblionumber = $builder->build_sample_biblio(
2693 branchcode => $library->{branchcode},
2696 my $item_1 = $builder->build_sample_item(
2698 biblionumber => $biblionumber,
2699 library => $library->{branchcode},
2702 my $item_2 = $builder->build_sample_item(
2704 biblionumber => $biblionumber,
2705 library => $library->{branchcode},
2709 # And the circulation rule
2710 Koha::CirculationRules->search->delete;
2711 Koha::CirculationRules->set_rules(
2713 categorycode => undef,
2715 branchcode => undef,
2718 firstremind => 1, # 1 day of grace
2719 finedays => 2, # 2 days of fine per day of overdue
2720 lengthunit => 'days',
2725 # Patron cannot issue item_1, they have overdues
2726 my $now = dt_from_string;
2727 my $five_days_ago = $now->clone->subtract( days => 5 );
2728 my $ten_days_ago = $now->clone->subtract( days => 10 );
2729 AddIssue( $patron, $item_1->barcode, $five_days_ago ); # Add an overdue
2730 AddIssue( $patron, $item_2->barcode, $ten_days_ago )
2731 ; # Add another overdue
2733 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
2734 AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2735 my $suspensions = $patron->restrictions->search( { type => 'SUSPENSION' } );
2736 is( $suspensions->count, 1, "Suspension added" );
2737 my $THE_suspension = $suspensions->next;
2739 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
2740 # Same for the others
2741 my $expected_expiration = output_pref(
2743 dt => $now->clone->add( days => ( 5 - 1 ) * 2 ),
2744 dateformat => 'sql',
2748 is( $THE_suspension->expiration, $expected_expiration, "Suspesion expiration set" );
2750 AddReturn( $item_2->barcode, $library->{branchcode}, undef, $now );
2751 $suspensions = $patron->restrictions->search( { type => 'SUSPENSION' } );
2752 is( $suspensions->count, 1, "Only one suspension" );
2753 $THE_suspension = $suspensions->next;
2755 $expected_expiration = output_pref(
2757 dt => $now->clone->add( days => ( 10 - 1 ) * 2 ),
2758 dateformat => 'sql',
2762 is( $THE_suspension->expiration, $expected_expiration, "Suspension expiration date updated" );
2764 Koha::Patron::Debarments::DelUniqueDebarment(
2765 { borrowernumber => $patron->borrowernumber, type => 'SUSPENSION' } );
2767 t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
2768 AddIssue( $patron, $item_1->barcode, $five_days_ago ); # Add an overdue
2769 AddIssue( $patron, $item_2->barcode, $ten_days_ago )
2770 ; # Add another overdue
2771 AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2772 $suspensions = $patron->restrictions->search( { type => 'SUSPENSION' } );
2773 is( $suspensions->count, 1, "Only one suspension" );
2774 $THE_suspension = $suspensions->next;
2776 $expected_expiration = output_pref(
2778 dt => $now->clone->add( days => ( 5 - 1 ) * 2 ),
2779 dateformat => 'sql',
2783 is( $THE_suspension->expiration, $expected_expiration, "Suspension expiration date updated" );
2785 AddReturn( $item_2->barcode, $library->{branchcode}, undef, $now );
2786 $suspensions = $patron->restrictions->search( { type => 'SUSPENSION' } );
2787 is( $suspensions->count, 1, "Only one suspension" );
2788 $THE_suspension = $suspensions->next;
2790 $expected_expiration = output_pref(
2792 dt => $now->clone->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
2793 dateformat => 'sql',
2797 is( $THE_suspension->expiration, $expected_expiration, "Suspension expiration date updated" );
2800 subtest 'AddReturn + suspension_chargeperiod' => sub {
2803 my $library = $builder->build( { source => 'Branch' } );
2804 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2806 my $biblionumber = $builder->build_sample_biblio(
2808 branchcode => $library->{branchcode},
2811 my $item_1 = $builder->build_sample_item(
2813 biblionumber => $biblionumber,
2814 library => $library->{branchcode},
2818 # And the issuing rule
2819 Koha::CirculationRules->search->delete;
2820 Koha::CirculationRules->set_rules(
2822 categorycode => '*',
2827 firstremind => 0, # 0 day of grace
2828 finedays => 2, # 2 days of fine per day of overdue
2829 suspension_chargeperiod => 1,
2830 lengthunit => 'days',
2835 my $now = dt_from_string;
2836 my $five_days_ago = $now->clone->subtract( days => 5 );
2837 # We want to charge 2 days every day, without grace
2838 # With 5 days of overdue: 5 * Z
2839 my $expected_expiration = $now->clone->add( days => ( 5 * 2 ) / 1 );
2840 test_debarment_on_checkout(
2843 library => $library,
2845 due_date => $five_days_ago,
2846 expiration_date => $expected_expiration,
2850 # Same with undef firstremind
2851 Koha::CirculationRules->search->delete;
2852 Koha::CirculationRules->set_rules(
2854 categorycode => '*',
2859 firstremind => undef, # 0 day of grace
2860 finedays => 2, # 2 days of fine per day of overdue
2861 suspension_chargeperiod => 1,
2862 lengthunit => 'days',
2867 my $now = dt_from_string;
2868 my $five_days_ago = $now->clone->subtract( days => 5 );
2869 # We want to charge 2 days every day, without grace
2870 # With 5 days of overdue: 5 * Z
2871 my $expected_expiration = $now->clone->add( days => ( 5 * 2 ) / 1 );
2872 test_debarment_on_checkout(
2875 library => $library,
2877 due_date => $five_days_ago,
2878 expiration_date => $expected_expiration,
2882 # We want to charge 2 days every 2 days, without grace
2883 # With 5 days of overdue: (5 * 2) / 2
2884 Koha::CirculationRules->set_rule(
2886 categorycode => undef,
2887 branchcode => undef,
2889 rule_name => 'suspension_chargeperiod',
2894 $expected_expiration = $now->clone->add( days => floor( 5 * 2 ) / 2 );
2895 test_debarment_on_checkout(
2898 library => $library,
2900 due_date => $five_days_ago,
2901 expiration_date => $expected_expiration,
2905 # We want to charge 2 days every 3 days, with 1 day of grace
2906 # With 5 days of overdue: ((5-1) / 3 ) * 2
2907 Koha::CirculationRules->set_rules(
2909 categorycode => undef,
2910 branchcode => undef,
2913 suspension_chargeperiod => 3,
2918 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
2919 test_debarment_on_checkout(
2922 library => $library,
2924 due_date => $five_days_ago,
2925 expiration_date => $expected_expiration,
2929 # Use finesCalendar to know if holiday must be skipped to calculate the due date
2930 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
2931 Koha::CirculationRules->set_rules(
2933 categorycode => undef,
2934 branchcode => undef,
2938 suspension_chargeperiod => 1,
2943 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
2944 t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2946 # Adding a holiday 2 days ago
2947 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
2948 my $two_days_ago = $now->clone->subtract( days => 2 );
2949 $calendar->insert_single_holiday(
2950 day => $two_days_ago->day,
2951 month => $two_days_ago->month,
2952 year => $two_days_ago->year,
2953 title => 'holidayTest-2d',
2954 description => 'holidayDesc 2 days ago'
2956 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2957 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2958 test_debarment_on_checkout(
2961 library => $library,
2963 due_date => $five_days_ago,
2964 expiration_date => $expected_expiration,
2968 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2969 my $two_days_ahead = $now->clone->add( days => 2 );
2970 $calendar->insert_single_holiday(
2971 day => $two_days_ahead->day,
2972 month => $two_days_ahead->month,
2973 year => $two_days_ahead->year,
2974 title => 'holidayTest+2d',
2975 description => 'holidayDesc 2 days ahead'
2978 # Same as above, but we should skip D+2
2979 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
2980 test_debarment_on_checkout(
2983 library => $library,
2985 due_date => $five_days_ago,
2986 expiration_date => $expected_expiration,
2990 # Adding another holiday, day of expiration date
2991 my $expected_expiration_dt = dt_from_string($expected_expiration);
2992 $calendar->insert_single_holiday(
2993 day => $expected_expiration_dt->day,
2994 month => $expected_expiration_dt->month,
2995 year => $expected_expiration_dt->year,
2996 title => 'holidayTest_exp',
2997 description => 'holidayDesc on expiration date'
2999 # Expiration date will be the day after
3000 test_debarment_on_checkout(
3003 library => $library,
3005 due_date => $five_days_ago,
3006 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
3010 test_debarment_on_checkout(
3013 library => $library,
3015 return_date => $now->clone->add(days => 5),
3016 expiration_date => $now->clone->add(days => 5 + (5 * 2 - 1) ),
3020 test_debarment_on_checkout(
3023 library => $library,
3025 due_date => $now->clone->add(days => 1),
3026 return_date => $now->clone->add(days => 5),
3027 expiration_date => $now->clone->add(days => 5 + (4 * 2 - 1) ),
3031 Koha::CirculationRules->search->delete;
3032 Koha::CirculationRules->set_rules(
3034 categorycode => undef,
3036 branchcode => undef,
3039 lengthunit => 'days',
3044 Koha::Patron::Debarments::AddDebarment(
3046 borrowernumber => $patron->borrowernumber,
3047 expiration => '9999-12-31',
3052 AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
3053 my ( undef, $message ) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
3054 is( $message->{WasReturned} && exists $message->{ForeverDebarred}, 1, 'Forever debarred message for Addreturn when overdue');
3056 Koha::Patron::Debarments::DelUniqueDebarment(
3058 borrowernumber => $patron->borrowernumber,
3062 Koha::Patron::Debarments::AddDebarment(
3064 borrowernumber => $patron->borrowernumber,
3065 expiration => $now->clone->add( days => 10 ),
3070 AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
3071 (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
3072 is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
3075 subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
3078 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
3079 my $patron1 = $builder->build_object(
3081 class => 'Koha::Patrons',
3083 branchcode => $library->branchcode,
3084 categorycode => $patron_category->{categorycode}
3088 my $patron2 = $builder->build_object(
3090 class => 'Koha::Patrons',
3092 branchcode => $library->branchcode,
3093 categorycode => $patron_category->{categorycode}
3098 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
3100 my $item = $builder->build_sample_item(
3102 library => $library->branchcode,
3106 my ( $error, $question, $alerts );
3107 my $issue = AddIssue( $patron1, $item->barcode );
3109 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
3110 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->barcode );
3111 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' );
3113 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 1);
3114 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->barcode );
3115 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' );
3117 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
3121 subtest 'AddReturn | is_overdue' => sub {
3124 t::lib::Mocks::mock_preference('MarkLostItemsAsReturned', 'batchmod|moredetail|cronjob|additem|pendingreserves|onpayment');
3125 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
3126 t::lib::Mocks::mock_preference('finesMode', 'production');
3127 t::lib::Mocks::mock_preference('MaxFine', '100');
3129 my $library = $builder->build( { source => 'Branch' } );
3130 my $patron = $builder->build_object(
3132 class => 'Koha::Patrons',
3133 value => { categorycode => $patron_category->{categorycode} }
3136 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
3137 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
3139 my $item = $builder->build_sample_item(
3141 library => $library->{branchcode},
3142 replacementprice => 7
3146 Koha::CirculationRules->search->delete;
3147 Koha::CirculationRules->set_rules(
3149 categorycode => undef,
3151 branchcode => undef,
3154 lengthunit => 'days',
3155 fine => 1, # Charge 1 every day of overdue
3161 my $now = dt_from_string;
3162 my $one_day_ago = $now->clone->subtract( days => 1 );
3163 my $two_days_ago = $now->clone->subtract( days => 2 );
3164 my $five_days_ago = $now->clone->subtract( days => 5 );
3165 my $ten_days_ago = $now->clone->subtract( days => 10 );
3167 # No return date specified, today will be used => 10 days overdue charged
3168 AddIssue( $patron, $item->barcode, $ten_days_ago ); # date due was 10d ago
3169 AddReturn( $item->barcode, $library->{branchcode} );
3170 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
3171 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3173 # specify return date 5 days before => no overdue charged
3174 AddIssue( $patron, $item->barcode, $five_days_ago ); # date due was 5d ago
3175 AddReturn( $item->barcode, $library->{branchcode}, undef, $ten_days_ago );
3176 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
3177 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3179 # specify return date 5 days later => 5 days overdue charged
3180 AddIssue( $patron, $item->barcode, $ten_days_ago ); # date due was 10d ago
3181 AddReturn( $item->barcode, $library->{branchcode}, undef, $five_days_ago );
3182 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
3183 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3185 # specify return date 5 days later, specify exemptfine => no overdue charge
3186 AddIssue( $patron, $item->barcode, $ten_days_ago ); # date due was 10d ago
3187 AddReturn( $item->barcode, $library->{branchcode}, 1, $five_days_ago );
3188 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
3189 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3191 subtest 'bug 22877 | Lost item return' => sub {
3195 my $issue = AddIssue( $patron, $item->barcode, $ten_days_ago ); # date due was 10d ago
3197 # Fake fines cronjob on this checkout
3199 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3200 $ten_days_ago, $now );
3203 issue_id => $issue->issue_id,
3204 itemnumber => $item->itemnumber,
3205 borrowernumber => $patron->borrowernumber,
3207 due => output_pref($ten_days_ago)
3210 is( int( $patron->account->balance() ),
3211 10, "Overdue fine of 10 days overdue" );
3213 # Fake longoverdue with charge and not marking returned
3214 LostItem( $item->itemnumber, 'cronjob', 0 );
3215 is( int( $patron->account->balance() ),
3216 17, "Lost fine of 7 plus 10 days overdue" );
3218 # Now we return it today
3219 AddReturn( $item->barcode, $library->{branchcode} );
3220 is( int( $patron->account->balance() ),
3221 17, "Should have a single 10 days overdue fine and lost charge" );
3224 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3227 subtest 'bug 8338 | backdated return resulting in zero amount fine' => sub {
3231 t::lib::Mocks::mock_preference('CalculateFinesOnBackdate', 1);
3233 my $issue = AddIssue( $patron, $item->barcode, $one_day_ago ); # date due was 1d ago
3235 # Fake fines cronjob on this checkout
3237 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3238 $one_day_ago, $now );
3241 issue_id => $issue->issue_id,
3242 itemnumber => $item->itemnumber,
3243 borrowernumber => $patron->borrowernumber,
3245 due => output_pref($one_day_ago)
3248 is( int( $patron->account->balance() ),
3249 1, "Overdue fine of 1 day overdue" );
3251 # Backdated return (dropbox mode example - charge should be removed)
3252 AddReturn( $item->barcode, $library->{branchcode}, 1, $one_day_ago );
3253 is( int( $patron->account->balance() ),
3254 0, "Overdue fine should be annulled" );
3255 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
3256 is( $lines->count, 0, "Overdue fine accountline has been removed");
3258 $issue = AddIssue( $patron, $item->barcode, $two_days_ago ); # date due was 2d ago
3260 # Fake fines cronjob on this checkout
3262 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3263 $two_days_ago, $now );
3266 issue_id => $issue->issue_id,
3267 itemnumber => $item->itemnumber,
3268 borrowernumber => $patron->borrowernumber,
3270 due => output_pref($one_day_ago)
3273 is( int( $patron->account->balance() ),
3274 2, "Overdue fine of 2 days overdue" );
3276 # Payment made against fine
3277 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
3278 my $debit = $lines->next;
3279 my $credit = $patron->account->add_credit(
3283 interface => 'test',
3286 $credit->apply( { debits => [$debit] } );
3288 is( int( $patron->account->balance() ),
3289 0, "Overdue fine should be paid off" );
3290 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
3291 is ( $lines->count, 2, "Overdue (debit) and Payment (credit) present");
3292 my $line = $lines->next;
3293 is( $line->amount+0, 2, "Overdue fine amount remains as 2 days");
3294 is( $line->amountoutstanding+0, 0, "Overdue fine amountoutstanding reduced to 0");
3296 # Backdated return (dropbox mode example - charge should be removed)
3297 AddReturn( $item->barcode, $library->{branchcode}, undef, $one_day_ago );
3298 is( int( $patron->account->balance() ),
3299 -1, "Refund credit has been applied" );
3300 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber }, { order_by => { '-asc' => 'accountlines_id' }});
3301 is( $lines->count, 3, "Overdue (debit), Payment (credit) and Refund (credit) are all present");
3303 $line = $lines->next;
3304 is($line->amount+0,1, "Overdue fine amount has been reduced to 1");
3305 is($line->amountoutstanding+0,0, "Overdue fine amount outstanding remains at 0");
3306 is($line->status,'RETURNED', "Overdue fine is fixed");
3307 $line = $lines->next;
3308 is($line->amount+0,-2, "Original payment amount remains as 2");
3309 is($line->amountoutstanding+0,0, "Original payment remains applied");
3310 $line = $lines->next;
3311 is($line->amount+0,-1, "Refund amount correctly set to 1");
3312 is($line->amountoutstanding+0,-1, "Refund amount outstanding unspent");
3315 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3318 subtest 'bug 25417 | backdated return + exemptfine' => sub {
3322 t::lib::Mocks::mock_preference('CalculateFinesOnBackdate', 1);
3324 my $issue = AddIssue( $patron, $item->barcode, $one_day_ago ); # date due was 1d ago
3326 # Fake fines cronjob on this checkout
3328 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3329 $one_day_ago, $now );
3332 issue_id => $issue->issue_id,
3333 itemnumber => $item->itemnumber,
3334 borrowernumber => $patron->borrowernumber,
3336 due => output_pref($one_day_ago)
3339 is( int( $patron->account->balance() ),
3340 1, "Overdue fine of 1 day overdue" );
3342 # Backdated return (dropbox mode example - charge should no longer exist)
3343 AddReturn( $item->barcode, $library->{branchcode}, 1, $one_day_ago );
3344 is( int( $patron->account->balance() ),
3345 0, "Overdue fine should be annulled" );
3348 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
3351 subtest 'bug 24075 | backdated return with return datetime matching due datetime' => sub {
3354 t::lib::Mocks::mock_preference( 'CalculateFinesOnBackdate', 1 );
3356 my $due_date = dt_from_string;
3357 my $issue = AddIssue( $patron, $item->barcode, $due_date );
3362 issue_id => $issue->issue_id,
3363 itemnumber => $item->itemnumber,
3364 borrowernumber => $patron->borrowernumber,
3366 due => output_pref($due_date)
3369 is( $patron->account->balance(),
3370 0.25, 'Overdue fine of $0.25 recorded' );
3372 # Backdate return to exact due date and time
3373 my ( undef, $message ) =
3374 AddReturn( $item->barcode, $library->{branchcode},
3378 Koha::Account::Lines->find( { issue_id => $issue->id } );
3379 ok( !$accountline, 'accountline removed as expected' );
3382 $issue = AddIssue( $patron, $item->barcode, $due_date );
3387 issue_id => $issue->issue_id,
3388 itemnumber => $item->itemnumber,
3389 borrowernumber => $patron->borrowernumber,
3391 due => output_pref($due_date)
3394 is( $patron->account->balance(),
3395 0.25, 'Overdue fine of $0.25 recorded' );
3397 # Partial pay accruing fine
3398 my $lines = Koha::Account::Lines->search(
3400 borrowernumber => $patron->borrowernumber,
3401 issue_id => $issue->id
3404 my $debit = $lines->next;
3405 my $credit = $patron->account->add_credit(
3409 interface => 'test',
3412 $credit->apply( { debits => [$debit] } );
3414 is( $patron->account->balance(), .05, 'Overdue fine reduced to $0.05' );
3416 # Backdate return to exact due date and time
3417 ( undef, $message ) =
3418 AddReturn( $item->barcode, $library->{branchcode},
3421 $lines = Koha::Account::Lines->search(
3423 borrowernumber => $patron->borrowernumber,
3424 issue_id => $issue->id
3427 $accountline = $lines->next;
3428 is( $accountline->amountoutstanding + 0,
3429 0, 'Partially paid fee amount outstanding was reduced to 0' );
3430 is( $accountline->amount + 0,
3431 0, 'Partially paid fee amount was reduced to 0' );
3432 is( $patron->account->balance(), -0.20, 'Patron refund recorded' );
3435 Koha::Account::Lines->search(
3436 { borrowernumber => $patron->borrowernumber } )->delete;
3439 subtest 'enh 23091 | Lost item return policies' => sub {
3442 my $manager = $builder->build_object({ class => "Koha::Patrons" });
3444 my $branchcode_false =
3445 $builder->build( { source => 'Branch' } )->{branchcode};
3446 my $specific_rule_false = $builder->build(
3448 source => 'CirculationRule',
3450 branchcode => $branchcode_false,
3451 categorycode => undef,
3453 rule_name => 'lostreturn',
3458 my $branchcode_refund =
3459 $builder->build( { source => 'Branch' } )->{branchcode};
3460 my $specific_rule_refund = $builder->build(
3462 source => 'CirculationRule',
3464 branchcode => $branchcode_refund,
3465 categorycode => undef,
3467 rule_name => 'lostreturn',
3468 rule_value => 'refund'
3472 my $branchcode_restore =
3473 $builder->build( { source => 'Branch' } )->{branchcode};
3474 my $specific_rule_restore = $builder->build(
3476 source => 'CirculationRule',
3478 branchcode => $branchcode_restore,
3479 categorycode => undef,
3481 rule_name => 'lostreturn',
3482 rule_value => 'restore'
3486 my $branchcode_charge =
3487 $builder->build( { source => 'Branch' } )->{branchcode};
3488 my $specific_rule_charge = $builder->build(
3490 source => 'CirculationRule',
3492 branchcode => $branchcode_charge,
3493 categorycode => undef,
3495 rule_name => 'lostreturn',
3496 rule_value => 'charge'
3501 my $branchcode_refund_unpaid =
3502 $builder->build( { source => 'Branch' } )->{branchcode};
3503 my $specific_rule_refund_unpaid = $builder->build(
3505 source => 'CirculationRule',
3507 branchcode => $branchcode_refund_unpaid,
3508 categorycode => undef,
3510 rule_name => 'lostreturn',
3511 rule_value => 'refund_unpaid'
3516 my $replacement_amount = 99.00;
3517 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
3518 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3519 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
3520 t::lib::Mocks::mock_preference( 'BlockReturnOfLostItems', 0 );
3521 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl',
3523 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge',
3526 subtest 'lostreturn | refund_unpaid' => sub {
3529 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $branchcode_refund_unpaid });
3531 my $item = $builder->build_sample_item(
3533 replacementprice => $replacement_amount
3538 my $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
3541 $item->itemlost(3)->store;
3542 C4::Circulation::LostItem( $item->itemnumber, 1 );
3544 my $lost_fee_lines = Koha::Account::Lines->search(
3546 borrowernumber => $patron->id,
3547 itemnumber => $item->itemnumber,
3548 debit_type_code => 'LOST'
3551 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3552 my $lost_fee_line = $lost_fee_lines->next;
3553 is( int($lost_fee_line->amount),
3554 $replacement_amount, 'The right LOST amount is generated' );
3555 is( int($lost_fee_line->amountoutstanding),
3556 $replacement_amount,
3557 'The right LOST amountoutstanding is generated' );
3558 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
3561 int($patron->account->balance),
3562 $replacement_amount ,
3563 "Account balance equals the replacement amount after being charged lost fee when no payments has been made"
3566 # Return lost item without any payments having been made
3567 my ( $returned, $message ) = AddReturn( $item->barcode, $branchcode_refund_unpaid );
3569 $lost_fee_line->discard_changes;
3571 is( int($lost_fee_line->amount), $replacement_amount, 'The LOST amount is left intact' );
3572 is( int($lost_fee_line->amountoutstanding) , 0, 'The LOST amountoutstanding is zero' );
3573 is( $lost_fee_line->status, 'FOUND', 'The FOUND status was set' );
3575 int($patron->account->balance),
3577 'Account balance should be zero after returning item with lost fee when no payments has been made'
3580 # Create a second item
3581 $item = $builder->build_sample_item(
3583 replacementprice => $replacement_amount
3588 $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
3591 $item->itemlost(3)->store;
3592 C4::Circulation::LostItem( $item->itemnumber, 1 );
3594 $lost_fee_lines = Koha::Account::Lines->search(
3596 borrowernumber => $patron->id,
3597 itemnumber => $item->itemnumber,
3598 debit_type_code => 'LOST'
3601 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3602 $lost_fee_line = $lost_fee_lines->next;
3604 # Make partial payment
3605 $patron->account->payin_amount({
3607 interface => 'intranet',
3608 payment_type => 'CASH',
3609 user_id => $patron->borrowernumber,
3611 debits => [$lost_fee_line]
3614 $lost_fee_line->discard_changes;
3616 is( int($lost_fee_line->amountoutstanding),
3618 'The LOST amountoutstanding is the expected amount after partial payment of lost fee'
3622 int($patron->account->balance),
3624 'Account balance is the expected amount after partial payment of lost fee'
3627 # Return lost item with partial payment having been made
3628 ( $returned, $message ) = AddReturn( $item->barcode, $branchcode_refund_unpaid );
3630 $lost_fee_line->discard_changes;
3632 is( int($lost_fee_line->amountoutstanding) , 0, 'The LOST amountoutstanding is zero after returning lost item with partial payment' );
3633 is( $lost_fee_line->status, 'FOUND', 'The FOUND status was set for lost item with partial payment' );
3635 int($patron->account->balance),
3637 'Account balance should be zero after returning item with lost fee when partial payment has been made'
3640 # Create a third item
3641 $item = $builder->build_sample_item(
3643 replacementprice => $replacement_amount
3648 $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
3651 $item->itemlost(3)->store;
3652 C4::Circulation::LostItem( $item->itemnumber, 1 );
3654 $lost_fee_lines = Koha::Account::Lines->search(
3656 borrowernumber => $patron->id,
3657 itemnumber => $item->itemnumber,
3658 debit_type_code => 'LOST'
3661 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3662 $lost_fee_line = $lost_fee_lines->next;
3665 $patron->account->payin_amount({
3667 interface => 'intranet',
3668 payment_type => 'CASH',
3669 user_id => $patron->borrowernumber,
3670 amount => $replacement_amount,
3671 debits => [$lost_fee_line]
3674 $lost_fee_line->discard_changes;
3676 is( int($lost_fee_line->amountoutstanding),
3678 'The LOST amountoutstanding is the expected amount after partial payment of lost fee'
3682 int($patron->account->balance),
3684 'Account balance is the expected amount after partial payment of lost fee'
3687 # Return lost item with partial payment having been made
3688 ( $returned, $message ) = AddReturn( $item->barcode, $branchcode_refund_unpaid );
3690 $lost_fee_line->discard_changes;
3692 is( int($lost_fee_line->amountoutstanding) , 0, 'The LOST amountoutstanding is zero after returning lost item with full payment' );
3693 is( $lost_fee_line->status, 'FOUND', 'The FOUND status was set for lost item with partial payment' );
3695 int($patron->account->balance),
3697 'Account balance should be zero after returning item with lost fee when full payment has been made'
3701 subtest 'lostreturn | false' => sub {
3704 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $branchcode_false });
3706 my $item = $builder->build_sample_item(
3708 replacementprice => $replacement_amount
3713 my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, $ten_days_ago );
3715 # Fake fines cronjob on this checkout
3717 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3718 $ten_days_ago, $now );
3721 issue_id => $issue->issue_id,
3722 itemnumber => $item->itemnumber,
3723 borrowernumber => $patron->borrowernumber,
3725 due => output_pref($ten_days_ago)
3728 my $overdue_fees = Koha::Account::Lines->search(
3730 borrowernumber => $patron->id,
3731 itemnumber => $item->itemnumber,
3732 debit_type_code => 'OVERDUE'
3735 is( $overdue_fees->count, 1, 'Overdue item fee produced' );
3736 my $overdue_fee = $overdue_fees->next;
3737 is( $overdue_fee->amount + 0,
3738 10, 'The right OVERDUE amount is generated' );
3739 is( $overdue_fee->amountoutstanding + 0,
3741 'The right OVERDUE amountoutstanding is generated' );
3743 # Simulate item marked as lost
3744 $item->itemlost(3)->store;
3745 C4::Circulation::LostItem( $item->itemnumber, 1 );
3747 my $lost_fee_lines = Koha::Account::Lines->search(
3749 borrowernumber => $patron->id,
3750 itemnumber => $item->itemnumber,
3751 debit_type_code => 'LOST'
3754 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3755 my $lost_fee_line = $lost_fee_lines->next;
3756 is( $lost_fee_line->amount + 0,
3757 $replacement_amount, 'The right LOST amount is generated' );
3758 is( $lost_fee_line->amountoutstanding + 0,
3759 $replacement_amount,
3760 'The right LOST amountoutstanding is generated' );
3761 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
3764 my ( $returned, $message ) =
3765 AddReturn( $item->barcode, $branchcode_false, undef, $five_days_ago );
3767 $overdue_fee->discard_changes;
3768 is( $overdue_fee->amount + 0,
3769 10, 'The OVERDUE amount is left intact' );
3770 is( $overdue_fee->amountoutstanding + 0,
3772 'The OVERDUE amountoutstanding is left intact' );
3774 $lost_fee_line->discard_changes;
3775 is( $lost_fee_line->amount + 0,
3776 $replacement_amount, 'The LOST amount is left intact' );
3777 is( $lost_fee_line->amountoutstanding + 0,
3778 $replacement_amount,
3779 'The LOST amountoutstanding is left intact' );
3780 # FIXME: Should we set the LOST fee status to 'FOUND' regardless of whether we're refunding or not?
3781 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
3784 subtest 'lostreturn | refund' => sub {
3787 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $branchcode_refund });
3789 my $item = $builder->build_sample_item(
3791 replacementprice => $replacement_amount
3796 my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, $ten_days_ago );
3798 # Fake fines cronjob on this checkout
3800 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3801 $ten_days_ago, $now );
3804 issue_id => $issue->issue_id,
3805 itemnumber => $item->itemnumber,
3806 borrowernumber => $patron->borrowernumber,
3808 due => output_pref($ten_days_ago)
3811 my $overdue_fees = Koha::Account::Lines->search(
3813 borrowernumber => $patron->id,
3814 itemnumber => $item->itemnumber,
3815 debit_type_code => 'OVERDUE'
3818 is( $overdue_fees->count, 1, 'Overdue item fee produced' );
3819 my $overdue_fee = $overdue_fees->next;
3820 is( $overdue_fee->amount + 0,
3821 10, 'The right OVERDUE amount is generated' );
3822 is( $overdue_fee->amountoutstanding + 0,
3824 'The right OVERDUE amountoutstanding is generated' );
3826 # Simulate item marked as lost
3827 $item->itemlost(3)->store;
3828 C4::Circulation::LostItem( $item->itemnumber, 1 );
3830 my $lost_fee_lines = Koha::Account::Lines->search(
3832 borrowernumber => $patron->id,
3833 itemnumber => $item->itemnumber,
3834 debit_type_code => 'LOST'
3837 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3838 my $lost_fee_line = $lost_fee_lines->next;
3839 is( $lost_fee_line->amount + 0,
3840 $replacement_amount, 'The right LOST amount is generated' );
3841 is( $lost_fee_line->amountoutstanding + 0,
3842 $replacement_amount,
3843 'The right LOST amountoutstanding is generated' );
3844 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
3846 # Return the lost item
3847 my ( undef, $message ) =
3848 AddReturn( $item->barcode, $branchcode_refund, undef, $five_days_ago );
3850 $overdue_fee->discard_changes;
3851 is( $overdue_fee->amount + 0,
3852 10, 'The OVERDUE amount is left intact' );
3853 is( $overdue_fee->amountoutstanding + 0,
3855 'The OVERDUE amountoutstanding is left intact' );
3857 $lost_fee_line->discard_changes;
3858 is( $lost_fee_line->amount + 0,
3859 $replacement_amount, 'The LOST amount is left intact' );
3860 is( $lost_fee_line->amountoutstanding + 0,
3862 'The LOST amountoutstanding is refunded' );
3863 is( $lost_fee_line->status, 'FOUND', 'The LOST status was set to FOUND' );
3866 subtest 'lostreturn | restore' => sub {
3869 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $branchcode_restore });
3871 my $item = $builder->build_sample_item(
3873 replacementprice => $replacement_amount
3878 my $issue = C4::Circulation::AddIssue( $patron, $item->barcode , $ten_days_ago);
3880 # Fake fines cronjob on this checkout
3882 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3883 $ten_days_ago, $now );
3886 issue_id => $issue->issue_id,
3887 itemnumber => $item->itemnumber,
3888 borrowernumber => $patron->borrowernumber,
3890 due => output_pref($ten_days_ago)
3893 my $overdue_fees = Koha::Account::Lines->search(
3895 borrowernumber => $patron->id,
3896 itemnumber => $item->itemnumber,
3897 debit_type_code => 'OVERDUE'
3900 is( $overdue_fees->count, 1, 'Overdue item fee produced' );
3901 my $overdue_fee = $overdue_fees->next;
3902 is( $overdue_fee->amount + 0,
3903 10, 'The right OVERDUE amount is generated' );
3904 is( $overdue_fee->amountoutstanding + 0,
3906 'The right OVERDUE amountoutstanding is generated' );
3908 # Simulate item marked as lost
3909 $item->itemlost(3)->store;
3910 C4::Circulation::LostItem( $item->itemnumber, 1 );
3912 my $lost_fee_lines = Koha::Account::Lines->search(
3914 borrowernumber => $patron->id,
3915 itemnumber => $item->itemnumber,
3916 debit_type_code => 'LOST'
3919 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
3920 my $lost_fee_line = $lost_fee_lines->next;
3921 is( $lost_fee_line->amount + 0,
3922 $replacement_amount, 'The right LOST amount is generated' );
3923 is( $lost_fee_line->amountoutstanding + 0,
3924 $replacement_amount,
3925 'The right LOST amountoutstanding is generated' );
3926 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
3928 # Simulate refunding overdue fees upon marking item as lost
3929 my $overdue_forgive = $patron->account->add_credit(
3932 user_id => $manager->borrowernumber,
3933 library_id => $branchcode_restore,
3934 interface => 'test',
3936 item_id => $item->itemnumber
3939 $overdue_forgive->apply( { debits => [$overdue_fee] } );
3940 $overdue_fee->discard_changes;
3941 is($overdue_fee->amountoutstanding + 0, 0, 'Overdue fee forgiven');
3944 my ( undef, $message ) =
3945 AddReturn( $item->barcode, $branchcode_restore, undef, $five_days_ago );
3947 $overdue_fee->discard_changes;
3948 is( $overdue_fee->amount + 0,
3949 10, 'The OVERDUE amount is left intact' );
3950 is( $overdue_fee->amountoutstanding + 0,
3952 'The OVERDUE amountoutstanding is restored' );
3954 $lost_fee_line->discard_changes;
3955 is( $lost_fee_line->amount + 0,
3956 $replacement_amount, 'The LOST amount is left intact' );
3957 is( $lost_fee_line->amountoutstanding + 0,
3959 'The LOST amountoutstanding is refunded' );
3960 is( $lost_fee_line->status, 'FOUND', 'The LOST status was set to FOUND' );
3963 subtest 'lostreturn | charge' => sub {
3966 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $branchcode_charge });
3968 my $item = $builder->build_sample_item(
3970 replacementprice => $replacement_amount
3975 my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, $ten_days_ago );
3977 # Fake fines cronjob on this checkout
3979 CalcFine( $item, $patron->categorycode, $library->{branchcode},
3980 $ten_days_ago, $now );
3983 issue_id => $issue->issue_id,
3984 itemnumber => $item->itemnumber,
3985 borrowernumber => $patron->borrowernumber,
3987 due => output_pref($ten_days_ago)
3990 my $overdue_fees = Koha::Account::Lines->search(
3992 borrowernumber => $patron->id,
3993 itemnumber => $item->itemnumber,
3994 debit_type_code => 'OVERDUE'
3997 is( $overdue_fees->count, 1, 'Overdue item fee produced' );
3998 my $overdue_fee = $overdue_fees->next;
3999 is( $overdue_fee->amount + 0,
4000 10, 'The right OVERDUE amount is generated' );
4001 is( $overdue_fee->amountoutstanding + 0,
4003 'The right OVERDUE amountoutstanding is generated' );
4005 # Simulate item marked as lost
4006 $item->itemlost(3)->store;
4007 C4::Circulation::LostItem( $item->itemnumber, 1 );
4009 my $lost_fee_lines = Koha::Account::Lines->search(
4011 borrowernumber => $patron->id,
4012 itemnumber => $item->itemnumber,
4013 debit_type_code => 'LOST'
4016 is( $lost_fee_lines->count, 1, 'Lost item fee produced' );
4017 my $lost_fee_line = $lost_fee_lines->next;
4018 is( $lost_fee_line->amount + 0,
4019 $replacement_amount, 'The right LOST amount is generated' );
4020 is( $lost_fee_line->amountoutstanding + 0,
4021 $replacement_amount,
4022 'The right LOST amountoutstanding is generated' );
4023 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
4025 # Simulate refunding overdue fees upon marking item as lost
4026 my $overdue_forgive = $patron->account->add_credit(
4029 user_id => $manager->borrowernumber,
4030 library_id => $branchcode_charge,
4031 interface => 'test',
4033 item_id => $item->itemnumber
4036 $overdue_forgive->apply( { debits => [$overdue_fee] } );
4037 $overdue_fee->discard_changes;
4038 is($overdue_fee->amountoutstanding + 0, 0, 'Overdue fee forgiven');
4041 my ( undef, $message ) =
4042 AddReturn( $item->barcode, $branchcode_charge, undef, $five_days_ago );
4044 $lost_fee_line->discard_changes;
4045 is( $lost_fee_line->amount + 0,
4046 $replacement_amount, 'The LOST amount is left intact' );
4047 is( $lost_fee_line->amountoutstanding + 0,
4049 'The LOST amountoutstanding is refunded' );
4050 is( $lost_fee_line->status, 'FOUND', 'The LOST status was set to FOUND' );
4052 $overdue_fees = Koha::Account::Lines->search(
4054 borrowernumber => $patron->id,
4055 itemnumber => $item->itemnumber,
4056 debit_type_code => 'OVERDUE'
4059 order_by => { '-asc' => 'accountlines_id'}
4062 is( $overdue_fees->count, 2, 'A second OVERDUE fee has been added' );
4063 $overdue_fee = $overdue_fees->next;
4064 is( $overdue_fee->amount + 0,
4065 10, 'The original OVERDUE amount is left intact' );
4066 is( $overdue_fee->amountoutstanding + 0,
4068 'The original OVERDUE amountoutstanding is left as forgiven' );
4069 $overdue_fee = $overdue_fees->next;
4070 is( $overdue_fee->amount + 0,
4071 5, 'The new OVERDUE amount is correct for the backdated return' );
4072 is( $overdue_fee->amountoutstanding + 0,
4074 'The new OVERDUE amountoutstanding is correct for the backdated return' );
4079 subtest '_FixOverduesOnReturn' => sub {
4082 my $manager = $builder->build_object({ class => "Koha::Patrons" });
4083 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
4085 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
4087 my $branchcode = $library2->{branchcode};
4089 my $item = $builder->build_sample_item(
4091 biblionumber => $biblio->biblionumber,
4092 library => $branchcode,
4093 replacementprice => 99.00,
4098 my $patron = $builder->build( { source => 'Borrower' } );
4100 ## Start with basic call, should just close out the open fine
4101 my $accountline = Koha::Account::Line->new(
4103 borrowernumber => $patron->{borrowernumber},
4104 debit_type_code => 'OVERDUE',
4105 status => 'UNRETURNED',
4106 itemnumber => $item->itemnumber,
4108 amountoutstanding => 99.00,
4109 interface => 'test',
4113 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, undef, 'RETURNED' );
4115 $accountline->_result()->discard_changes();
4117 is( $accountline->amountoutstanding+0, 99, 'Fine has the same amount outstanding as previously' );
4118 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
4119 is( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
4121 ## Run again, with exemptfine enabled
4124 debit_type_code => 'OVERDUE',
4125 status => 'UNRETURNED',
4126 amountoutstanding => 99.00,
4130 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
4132 $accountline->_result()->discard_changes();
4133 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'APPLY' })->next();
4135 is( $accountline->amountoutstanding + 0, 0, 'Fine amountoutstanding has been reduced to 0' );
4136 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
4137 is( $accountline->status, 'RETURNED', 'Open fine ( account type OVERDUE ) has been set to returned ( status RETURNED )');
4138 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
4139 is( $offset->amount + 0, -99, "Amount of offset is correct" );
4140 my $credit = $offset->credit;
4141 is( ref $credit, "Koha::Account::Line", "Found matching credit for fine forgiveness" );
4142 is( $credit->amount + 0, -99, "Credit amount is set correctly" );
4143 is( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
4145 # Bug 25417 - Only forgive fines where there is an amount outstanding to forgive
4148 debit_type_code => 'OVERDUE',
4149 status => 'UNRETURNED',
4150 amountoutstanding => 0.00,
4155 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
4157 $accountline->_result()->discard_changes();
4158 $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'CREATE' })->next();
4159 is( $offset, undef, "No offset created when trying to forgive fine with no outstanding balance" );
4160 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
4161 is( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
4164 subtest 'Set waiting flag' => sub {
4167 my $library_1 = $builder->build( { source => 'Branch' } );
4168 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
4169 my $library_2 = $builder->build( { source => 'Branch' } );
4170 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
4172 my $item = $builder->build_sample_item(
4174 library => $library_1->{branchcode},
4178 set_userenv( $library_2 );
4179 my $reserve_id = AddReserve(
4181 branchcode => $library_2->{branchcode},
4182 borrowernumber => $patron_2->{borrowernumber},
4183 biblionumber => $item->biblionumber,
4185 itemnumber => $item->itemnumber,
4189 set_userenv( $library_1 );
4190 my $do_transfer = 1;
4191 my ( $res, $rr ) = AddReturn( $item->barcode, $library_1->{branchcode} );
4192 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
4193 my $hold = Koha::Holds->find( $reserve_id );
4194 is( $hold->found, 'T', 'Hold is in transit' );
4196 my ( $status ) = CheckReserves($item);
4197 is( $status, 'Transferred', 'Hold is not waiting yet');
4199 set_userenv( $library_2 );
4201 AddReturn( $item->barcode, $library_2->{branchcode} );
4202 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
4203 $hold = Koha::Holds->find( $reserve_id );
4204 is( $hold->found, 'W', 'Hold is waiting' );
4205 ( $status ) = CheckReserves($item);
4206 is( $status, 'Waiting', 'Now the hold is waiting');
4208 #Bug 21944 - Waiting transfer checked in at branch other than pickup location
4209 set_userenv( $library_1 );
4210 (undef, my $messages, undef, undef ) = AddReturn ( $item->barcode, $library_1->{branchcode} );
4211 $hold = Koha::Holds->find( $reserve_id );
4212 is( $hold->found, undef, 'Hold is no longer marked waiting' );
4213 is( $hold->priority, 1, "Hold is now priority one again");
4214 is( $hold->waitingdate, undef, "Hold no longer has a waiting date");
4215 is( $hold->itemnumber, $item->itemnumber, "Hold has retained its' itemnumber");
4216 is( $messages->{ResFound}->{ResFound}, "Reserved", "Hold is still returned");
4217 is( $messages->{ResFound}->{found}, undef, "Hold is no longer marked found in return message");
4218 is( $messages->{ResFound}->{priority}, 1, "Hold is priority 1 in return message");
4221 subtest 'Cancel transfers on lost items' => sub {
4224 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
4225 my $item = $builder->build_sample_item();
4226 my $holdingbranch = $item->holdingbranch;
4227 # Historic transfer (datearrived is defined)
4228 my $old_transfer = $builder->build_object(
4230 class => 'Koha::Item::Transfers',
4232 itemnumber => $item->itemnumber,
4233 frombranch => $holdingbranch,
4234 tobranch => $library_to->branchcode,
4236 datesent => \'NOW()',
4237 datearrived => \'NOW()',
4238 datecancelled => undef,
4239 daterequested => \'NOW()'
4243 # Queued transfer (datesent is undefined)
4244 my $transfer_1 = $builder->build_object(
4246 class => 'Koha::Item::Transfers',
4248 itemnumber => $item->itemnumber,
4249 frombranch => $holdingbranch,
4250 tobranch => $library_to->branchcode,
4253 datearrived => undef,
4254 datecancelled => undef,
4255 daterequested => \'NOW()'
4259 # In transit transfer (datesent is defined, datearrived and datecancelled are both undefined)
4260 my $transfer_2 = $builder->build_object(
4262 class => 'Koha::Item::Transfers',
4264 itemnumber => $item->itemnumber,
4265 frombranch => $holdingbranch,
4266 tobranch => $library_to->branchcode,
4268 datesent => \'NOW()',
4269 datearrived => undef,
4270 datecancelled => undef,
4271 daterequested => \'NOW()'
4276 # Simulate item being marked as lost
4277 $item->itemlost(1)->store;
4278 LostItem( $item->itemnumber, 'test', 1 );
4280 $transfer_1->discard_changes;
4281 isnt($transfer_1->datecancelled, undef, "Queud transfer was cancelled upon item lost");
4282 is($transfer_1->cancellation_reason, 'ItemLost', "Cancellation reason was set to 'ItemLost'");
4283 $transfer_2->discard_changes;
4284 isnt($transfer_2->datecancelled, undef, "Active transfer was cancelled upon item lost");
4285 is($transfer_2->cancellation_reason, 'ItemLost', "Cancellation reason was set to 'ItemLost'");
4286 $old_transfer->discard_changes;
4287 is($old_transfer->datecancelled, undef, "Old transfers are unaffected");
4288 $item->discard_changes;
4289 is($item->holdingbranch, $holdingbranch, "Items holding branch remains unchanged");
4292 subtest 'CanBookBeIssued | is_overdue' => sub {
4295 # Set a simple circ policy
4296 Koha::CirculationRules->set_rules(
4298 categorycode => undef,
4299 branchcode => undef,
4303 reservesallowed => 25,
4305 lengthunit => 'days',
4306 renewalsallowed => 1,
4308 norenewalbefore => undef,
4316 my $now = dt_from_string()->truncate( to => 'day' );
4317 my $five_days_go = $now->clone->add( days => 5 );
4318 my $ten_days_go = $now->clone->add( days => 10);
4319 my $library = $builder->build( { source => 'Branch' } );
4320 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
4322 my $item = $builder->build_sample_item(
4324 library => $library->{branchcode},
4328 my $issue = AddIssue( $patron, $item->barcode, $five_days_go ); # date due was 10d ago
4329 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
4330 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), output_pref({ str => $five_days_go, dateonly => 1}), "First issue works");
4331 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron, $item->barcode, $ten_days_go, undef, undef, undef);
4332 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
4333 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
4336 subtest 'ItemsDeniedRenewal rules are checked' => sub {
4339 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
4340 Koha::CirculationRules->set_rules(
4342 categorycode => '*',
4344 branchcode => $idr_lib->branchcode,
4346 reservesallowed => 25,
4348 lengthunit => 'days',
4349 renewalsallowed => 10,
4351 norenewalbefore => undef,
4359 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
4360 homebranch => $idr_lib->branchcode,
4363 location => 'NOPROC'
4367 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
4368 branchcode => $idr_lib->branchcode,
4371 my $future = dt_from_string->add( days => 1 );
4372 my $issue = $builder->build_object(
4374 class => 'Koha::Checkouts',
4376 returndate => undef,
4377 renewals_count => 0,
4379 borrowernumber => $idr_borrower->borrowernumber,
4380 itemnumber => $allow_book->itemnumber,
4381 onsite_checkout => 0,
4382 date_due => $future,
4387 my $mock_item_class = Test::MockModule->new("Koha::Item");
4388 $mock_item_class->mock( 'is_denied_renewal', sub { return 1; } );
4390 my ( $mayrenew, $error ) = CanBookBeRenewed( $idr_borrower, $issue );
4391 is( $mayrenew, 0, 'Renewal blocked when $item->is_denied_renewal returns true' );
4392 is( $error, 'item_denied_renewal', 'Renewal blocked when $item->is_denied_renewal returns true' );
4394 $mock_item_class->unmock( 'is_denied_renewal' );
4395 $mock_item_class->mock( 'is_denied_renewal', sub { return 0; } );
4397 ( $mayrenew, $error ) = CanBookBeRenewed( $idr_borrower, $issue );
4398 is( $mayrenew, 1, 'Renewal allowed when $item->is_denied_renewal returns false' );
4399 is( $error, undef, 'Renewal allowed when $item->is_denied_renewal returns false' );
4401 $mock_item_class->unmock( 'is_denied_renewal' );
4404 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
4407 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
4408 my $library = $builder->build( { source => 'Branch' } );
4409 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
4411 my $item = $builder->build_sample_item(
4413 library => $library->{branchcode},
4417 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4418 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
4419 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
4422 subtest 'CanBookBeIssued | notforloan' => sub {
4425 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
4427 my $library = $builder->build( { source => 'Branch' } );
4428 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
4430 my $itemtype = $builder->build(
4432 source => 'Itemtype',
4433 value => { notforloan => undef, }
4436 my $item = $builder->build_sample_item(
4438 library => $library->{branchcode},
4439 itype => $itemtype->{itemtype},
4442 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
4444 my ( $issuingimpossible, $needsconfirmation );
4447 subtest 'item-level_itypes = 1' => sub {
4450 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
4451 # Is for loan at item type and item level
4452 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4453 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
4454 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
4456 # not for loan at item type level
4457 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
4458 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4459 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
4462 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
4463 'Item can not be issued, not for loan at item type level'
4466 # not for loan at item level
4467 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
4468 $item->notforloan( 1 )->store;
4469 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4470 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
4473 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
4474 'Item can not be issued, not for loan at item type level'
4478 subtest 'item-level_itypes = 0' => sub {
4481 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
4483 # We set another itemtype for biblioitem
4484 my $itemtype = $builder->build(
4486 source => 'Itemtype',
4487 value => { notforloan => undef, }
4491 # for loan at item type and item level
4492 $item->notforloan(0)->store;
4493 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
4494 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4495 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
4496 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
4498 # not for loan at item type level
4499 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
4500 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4501 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
4504 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
4505 'Item can not be issued, not for loan at item type level'
4508 # not for loan at item level
4509 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
4510 $item->notforloan( 1 )->store;
4511 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
4512 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
4515 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
4516 'Item can not be issued, not for loan at item type level'
4520 # TODO test with AllowNotForLoanOverride = 1
4523 subtest 'CanBookBeIssued | recalls' => sub {
4526 t::lib::Mocks::mock_preference("UseRecalls", 1);
4527 t::lib::Mocks::mock_preference("item-level_itypes", 1);
4528 my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
4529 my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
4530 my $item = $builder->build_sample_item;
4531 Koha::CirculationRules->set_rules({
4532 branchcode => undef,
4534 categorycode => undef,
4536 recalls_allowed => 10,
4541 my $recall = Koha::Recall->new(
4542 { patron_id => $patron1->borrowernumber,
4543 biblio_id => $item->biblionumber,
4544 item_id => $item->itemnumber,
4546 pickup_library_id => $patron1->branchcode,
4550 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron2, $item->barcode, undef, undef, undef, undef );
4551 is( $needsconfirmation->{RECALLED}->id, $recall->id, "Another patron has placed an item-level recall on this item" );
4553 $recall->set_cancelled;
4555 # biblio-level recall
4556 $recall = Koha::Recall->new(
4557 { patron_id => $patron1->borrowernumber,
4558 biblio_id => $item->biblionumber,
4561 pickup_library_id => $patron1->branchcode,
4565 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron2, $item->barcode, undef, undef, undef, undef );
4566 is( $needsconfirmation->{RECALLED}->id, $recall->id, "Another patron has placed a biblio-level recall and this item is eligible to fill it" );
4568 $recall->set_cancelled;
4570 # biblio-level recall
4571 $recall = Koha::Recall->new(
4572 { patron_id => $patron1->borrowernumber,
4573 biblio_id => $item->biblionumber,
4576 pickup_library_id => $patron1->branchcode,
4579 $recall->set_waiting( { item => $item, expirationdate => dt_from_string() } );
4581 my ( undef, undef, undef, $messages ) = CanBookBeIssued( $patron1, $item->barcode, undef, undef, undef, undef );
4582 is( $messages->{RECALLED}, $recall->id, "This book can be issued by this patron and they have placed a recall" );
4584 $recall->set_cancelled;
4587 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
4590 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
4591 my $item = $builder->build_sample_item(
4593 onloan => '2018-01-01',
4597 AddReturn( $item->barcode, $item->homebranch );
4598 $item->discard_changes; # refresh
4599 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
4602 subtest 'AddReturn | recalls' => sub {
4605 t::lib::Mocks::mock_preference("UseRecalls", 1);
4606 t::lib::Mocks::mock_preference("item-level_itypes", 1);
4607 my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
4608 my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
4609 my $item1 = $builder->build_sample_item;
4610 Koha::CirculationRules->set_rules({
4611 branchcode => undef,
4613 categorycode => undef,
4615 recalls_allowed => 10,
4619 # this item can fill a recall with pickup at this branch
4620 AddIssue( $patron1, $item1->barcode );
4621 my $recall1 = Koha::Recall->new(
4622 { patron_id => $patron2->borrowernumber,
4623 biblio_id => $item1->biblionumber,
4624 item_id => $item1->itemnumber,
4626 pickup_library_id => $item1->homebranch,
4629 my ( $doreturn, $messages, $iteminfo, $borrowerinfo ) = AddReturn( $item1->barcode, $item1->homebranch );
4630 is( $messages->{RecallFound}->id, $recall1->id, "Recall found" );
4631 $recall1->set_cancelled;
4633 # this item can fill a recall but needs transfer
4634 AddIssue( $patron1, $item1->barcode );
4635 $recall1 = Koha::Recall->new(
4636 { patron_id => $patron2->borrowernumber,
4637 biblio_id => $item1->biblionumber,
4638 item_id => $item1->itemnumber,
4640 pickup_library_id => $patron2->branchcode,
4643 ( $doreturn, $messages, $iteminfo, $borrowerinfo ) = AddReturn( $item1->barcode, $item1->homebranch );
4644 is( $messages->{RecallNeedsTransfer}, $item1->homebranch, "Recall requiring transfer found" );
4645 $recall1->set_cancelled;
4647 # this item is already in transit, do not ask to transfer
4648 AddIssue( $patron1, $item1->barcode );
4649 $recall1 = Koha::Recall->new(
4650 { patron_id => $patron2->borrowernumber,
4651 biblio_id => $item1->biblionumber,
4652 item_id => $item1->itemnumber,
4654 pickup_library_id => $patron2->branchcode,
4657 $recall1->start_transfer;
4658 ( $doreturn, $messages, $iteminfo, $borrowerinfo ) = AddReturn( $item1->barcode, $patron2->branchcode );
4659 is( $messages->{TransferredRecall}->id, $recall1->id, "In transit recall found" );
4660 $recall1->set_cancelled;
4663 subtest 'AddReturn | bundles' => sub {
4666 my $schema = Koha::Database->schema;
4667 $schema->storage->txn_begin;
4669 my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
4670 my $host_item1 = $builder->build_sample_item;
4671 my $bundle_item1 = $builder->build_sample_item;
4672 $schema->resultset('ItemBundle')
4674 { host => $host_item1->itemnumber, item => $bundle_item1->itemnumber } );
4676 my ( $doreturn, $messages, $iteminfo, $borrowerinfo ) = AddReturn( $bundle_item1->barcode, $bundle_item1->homebranch );
4677 is($messages->{InBundle}->id, $host_item1->id, 'AddReturn returns InBundle host item when item is part of a bundle');
4679 $schema->storage->txn_rollback;
4682 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
4687 t::lib::Mocks::mock_preference('item-level_itypes', 1);
4689 my $issuing_charges = 15;
4690 my $title = 'A title';
4691 my $author = 'Author, An';
4692 my $barcode = 'WHATARETHEODDS';
4694 my $circ = Test::MockModule->new('C4::Circulation');
4696 'GetIssuingCharges',
4698 return $issuing_charges;
4702 my $library = $builder->build_object({ class => 'Koha::Libraries' });
4703 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
4704 my $patron = $builder->build_object({
4705 class => 'Koha::Patrons',
4706 value => { branchcode => $library->id }
4709 my $biblio = $builder->build_sample_biblio({ title=> $title, author => $author });
4710 my $item_id = Koha::Item->new(
4712 biblionumber => $biblio->biblionumber,
4713 homebranch => $library->id,
4714 holdingbranch => $library->id,
4715 barcode => $barcode,
4716 replacementprice => 23.00,
4717 itype => $itemtype->id
4719 )->store->itemnumber;
4720 my $item = Koha::Items->find( $item_id );
4722 my $context = Test::MockModule->new('C4::Context');
4723 $context->mock( userenv => { branch => $library->id } );
4725 # Check the item out
4726 AddIssue( $patron, $item->barcode );
4731 borrowernumber => $patron->borrowernumber,
4732 itemnumber => $item->itemnumber,
4733 branch => $library->id,
4734 lastreneweddate => { break => "the_renewal" }
4737 } 'Koha::Exceptions::Checkout::FailedRenewal', 'Exception is thrown when renewal update to issues fails';
4739 t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
4740 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
4741 my %params_renewal = (
4742 timestamp => { -like => $date . "%" },
4743 module => "CIRCULATION",
4744 action => "RENEWAL",
4746 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
4749 borrowernumber => $patron->id,
4750 itemnumber => $item->id,
4751 branch => $library->id
4754 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
4755 is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
4757 my $checkouts = $patron->checkouts;
4758 # The following will fail if run on 00:00:00
4759 unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
4761 my $lines = Koha::Account::Lines->search({
4762 borrowernumber => $patron->id,
4763 itemnumber => $item->id
4766 is( $lines->count, 2 );
4768 my $line = $lines->next;
4769 is( $line->debit_type_code, 'RENT', 'The issue of item with issuing charge generates an accountline of the correct type' );
4770 is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
4771 is( $line->description, '', 'AddIssue does not set a hardcoded description for the accountline' );
4773 $line = $lines->next;
4774 is( $line->debit_type_code, 'RENT_RENEW', 'The renewal of item with issuing charge generates an accountline of the correct type' );
4775 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
4776 is( $line->description, '', 'AddRenewal does not set a hardcoded description for the accountline' );
4778 t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
4780 $context = Test::MockModule->new('C4::Context');
4781 $context->mock( userenv => { branch => undef, interface => 'CRON'} ); #Test statistical logging of renewal via cron (atuo_renew)
4783 my $now = dt_from_string;
4784 $date = output_pref( { dt => $now, dateonly => 1, dateformat => 'iso' } );
4785 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
4786 my $sth = $dbh->prepare("SELECT COUNT(*) FROM statistics WHERE itemnumber = ? AND branch = ?");
4787 $sth->execute($item->id, $library->id);
4788 my ($old_stats_size) = $sth->fetchrow_array;
4791 borrowernumber => $patron->id,
4792 itemnumber => $item->id,
4793 branch => $library->id
4796 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
4797 $sth->execute($item->id, $library->id);
4798 my ($new_stats_size) = $sth->fetchrow_array;
4799 is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
4800 is( $new_stats_size, $old_stats_size + 1, 'renew statistic successfully added with passed branch' );
4802 AddReturn( $item->id, $library->id, undef, $date );
4803 AddIssue( $patron, $item->barcode, $now );
4806 borrowernumber => $patron->id,
4807 itemnumber => $item->id,
4808 branch => $library->id,
4812 my $lines_skipped = Koha::Account::Lines->search({
4813 borrowernumber => $patron->id,
4814 itemnumber => $item->id
4816 is( $lines_skipped->count, 5, 'Passing skipfinecalc causes fine calculation on renewal to be skipped' );
4820 subtest 'AddRenewal() adds to renewals' => sub {
4823 my $library = $builder->build_object({ class => 'Koha::Libraries' });
4824 my $patron = $builder->build_object({
4825 class => 'Koha::Patrons',
4826 value => { branchcode => $library->id }
4829 my $item = $builder->build_sample_item();
4831 set_userenv( $library->unblessed );
4833 # Check the item out
4834 my $issue = AddIssue( $patron, $item->barcode );
4835 is(ref($issue), 'Koha::Checkout', 'Issue added');
4838 my $duedate = AddRenewal(
4840 borrowernumber => $patron->id,
4841 itemnumber => $item->id,
4842 branch => $library->id,
4847 ok( $duedate, "Renewal added" );
4849 my $renewals = Koha::Checkouts::Renewals->search({ checkout_id => $issue->issue_id });
4850 is($renewals->count, 1, 'One renewal added');
4851 my $THE_renewal = $renewals->next;
4852 is( $THE_renewal->renewer_id, C4::Context->userenv->{'number'}, 'Renewer recorded from context' );
4853 is( $THE_renewal->renewal_type, 'Automatic', 'AddRenewal "automatic" parameter sets renewal type to "Automatic"');
4856 subtest 'ProcessOfflinePayment() tests' => sub {
4863 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
4864 my $library = $builder->build_object({ class => 'Koha::Libraries' });
4865 my $result = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
4867 is( $result, 'Success.', 'The right string is returned' );
4869 my $lines = $patron->account->lines;
4870 is( $lines->count, 1, 'line created correctly');
4872 my $line = $lines->next;
4873 is( $line->amount+0, $amount * -1, 'amount picked from params' );
4874 is( $line->branchcode, $library->id, 'branchcode set correctly' );
4878 subtest 'Incremented fee tests' => sub {
4881 my $dt = dt_from_string();
4882 Time::Fake->offset( $dt->epoch );
4884 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
4886 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
4888 $module->mock( 'userenv', sub { { branch => $library->id } } );
4890 my $patron = $builder->build_object(
4892 class => 'Koha::Patrons',
4893 value => { categorycode => $patron_category->{categorycode} }
4897 my $itemtype = $builder->build_object(
4899 class => 'Koha::ItemTypes',
4901 notforloan => undef,
4903 rentalcharge_daily => 1,
4904 rentalcharge_daily_calendar => 0
4909 my $item = $builder->build_sample_item(
4911 library => $library->id,
4912 itype => $itemtype->id,
4916 is( $itemtype->rentalcharge_daily + 0,1, 'Daily rental charge stored and retreived correctly' );
4917 is( $item->effective_itemtype, $itemtype->id, "Itemtype set correctly for item" );
4919 my $now = dt_from_string;
4920 my $dt_from = $now->clone;
4921 my $dt_to = $now->clone->add( days => 7 );
4922 my $dt_to_renew = $now->clone->add( days => 13 );
4926 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
4927 my $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4929 $accountline->amount + 0,
4931 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0"
4933 $accountline->delete();
4936 borrowernumber => $patron->id,
4937 itemnumber => $item->id,
4938 branch => $library->id,
4939 datedue => $dt_to_renew,
4940 lastreneweddate => $dt_to
4943 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4945 $accountline->amount + 0,
4947 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0, for renewal"
4949 $accountline->delete();
4952 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
4953 $itemtype->rentalcharge_daily_calendar(1)->store();
4955 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
4956 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4958 $accountline->amount + 0,
4960 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1"
4962 $accountline->delete();
4965 borrowernumber => $patron->id,
4966 itemnumber => $item->id,
4967 branch => $library->id,
4968 datedue => $dt_to_renew,
4969 lastreneweddate => $dt_to
4972 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4974 $accountline->amount + 0,
4976 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1, for renewal"
4978 $accountline->delete();
4981 my $calendar = C4::Calendar->new( branchcode => $library->id );
4982 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
4984 ( $dt_from->day_of_week == 6 ) ? 0
4985 : ( $dt_from->day_of_week == 7 ) ? 1
4986 : $dt_from->day_of_week + 1;
4987 my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
4988 $calendar->insert_week_day_holiday(
4989 weekday => $closed_day,
4990 title => 'Test holiday',
4991 description => 'Test holiday'
4994 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
4995 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4997 $accountline->amount + 0,
4999 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name"
5001 $accountline->delete();
5004 borrowernumber => $patron->id,
5005 itemnumber => $item->id,
5006 branch => $library->id,
5007 datedue => $dt_to_renew,
5008 lastreneweddate => $dt_to
5011 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5013 $accountline->amount + 0,
5015 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name, for renewal"
5017 $accountline->delete();
5020 $itemtype->rentalcharge(2)->store;
5021 is( $itemtype->rentalcharge + 0, 2, 'Rental charge updated and retreived correctly' );
5023 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5025 Koha::Account::Lines->search( { itemnumber => $item->id } );
5026 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly" );
5027 $accountlines->delete();
5030 borrowernumber => $patron->id,
5031 itemnumber => $item->id,
5032 branch => $library->id,
5033 datedue => $dt_to_renew,
5034 lastreneweddate => $dt_to
5037 $accountlines = Koha::Account::Lines->search( { itemnumber => $item->id } );
5038 is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly, for renewal" );
5039 $accountlines->delete();
5041 $itemtype->rentalcharge(0)->store;
5042 is( $itemtype->rentalcharge + 0, 0, 'Rental charge reset and retreived correctly' );
5045 Koha::CirculationRules->set_rule(
5047 categorycode => $patron->categorycode,
5048 itemtype => $itemtype->id,
5049 branchcode => $library->id,
5050 rule_name => 'lengthunit',
5051 rule_value => 'hours',
5055 $itemtype->rentalcharge_hourly('0.25')->store();
5056 is( $itemtype->rentalcharge_hourly, '0.25', 'Hourly rental charge stored and retreived correctly' );
5058 $dt_to = $now->clone->add( hours => 168 );
5059 $dt_to_renew = $now->clone->add( hours => 312 );
5061 $itemtype->rentalcharge_hourly_calendar(0)->store();
5063 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5064 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5066 $accountline->amount + 0,
5068 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0 (168h * 0.25u)"
5070 $accountline->delete();
5073 borrowernumber => $patron->id,
5074 itemnumber => $item->id,
5075 branch => $library->id,
5076 datedue => $dt_to_renew,
5077 lastreneweddate => $dt_to
5080 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5082 $accountline->amount + 0,
5084 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0, for renewal (312h - 168h * 0.25u)"
5086 $accountline->delete();
5089 $itemtype->rentalcharge_hourly_calendar(1)->store();
5091 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5092 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5094 $accountline->amount + 0,
5096 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name (168h - 24h * 0.25u)"
5098 $accountline->delete();
5101 borrowernumber => $patron->id,
5102 itemnumber => $item->id,
5103 branch => $library->id,
5104 datedue => $dt_to_renew,
5105 lastreneweddate => $dt_to
5108 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5110 $accountline->amount + 0,
5112 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name, for renewal (312h - 168h - 24h * 0.25u"
5114 $accountline->delete();
5117 $calendar->delete_holiday( weekday => $closed_day );
5119 AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5120 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5122 $accountline->amount + 0,
5124 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 (168h - 0h * 0.25u"
5126 $accountline->delete();
5129 borrowernumber => $patron->id,
5130 itemnumber => $item->id,
5131 branch => $library->id,
5132 datedue => $dt_to_renew,
5133 lastreneweddate => $dt_to
5136 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5138 $accountline->amount + 0,
5140 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1, for renewal (312h - 168h - 0h * 0.25u)"
5142 $accountline->delete();
5147 subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
5150 t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
5151 t::lib::Mocks::mock_preference('item-level_itypes', 1);
5154 $builder->build_object( { class => 'Koha::Libraries' } )->store;
5155 my $patron = $builder->build_object(
5157 class => 'Koha::Patrons',
5158 value => { categorycode => $patron_category->{categorycode} }
5162 my $itemtype = $builder->build_object(
5164 class => 'Koha::ItemTypes',
5168 rentalcharge_daily => 0
5173 my $item = $builder->build_sample_item(
5175 library => $library->id,
5179 itype => $itemtype->id,
5183 my ( $issuingimpossible, $needsconfirmation );
5184 my $dt_from = dt_from_string();
5185 my $dt_due = $dt_from->clone->add( days => 3 );
5187 $itemtype->rentalcharge(1)->store;
5188 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
5189 is_deeply( $needsconfirmation, { RENTALCHARGE => '1.00' }, 'Item needs rentalcharge confirmation to be issued' );
5190 $itemtype->rentalcharge('0')->store;
5191 $itemtype->rentalcharge_daily(1)->store;
5192 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
5193 is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
5194 $itemtype->rentalcharge_daily('0')->store;
5197 subtest 'CanBookBeIssued & CircConfirmItemParts' => sub {
5200 t::lib::Mocks::mock_preference('CircConfirmItemParts', 1);
5202 my $patron = $builder->build_object(
5204 class => 'Koha::Patrons',
5205 value => { categorycode => $patron_category->{categorycode} }
5209 my $item = $builder->build_sample_item(
5211 materials => 'includes DVD',
5215 my $dt_due = dt_from_string->add( days => 3 );
5217 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
5218 is_deeply( $needsconfirmation, { ADDITIONAL_MATERIALS => 'includes DVD' }, 'Item needs confirmation of additional parts' );
5221 subtest 'Do not return on renewal (LOST charge)' => sub {
5224 t::lib::Mocks::mock_preference('MarkLostItemsAsReturned', 'onpayment');
5225 my $library = $builder->build_object( { class => "Koha::Libraries" } );
5226 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
5227 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
5229 my $biblio = $builder->build_sample_biblio;
5231 my $item = $builder->build_sample_item(
5233 biblionumber => $biblio->biblionumber,
5234 library => $library->branchcode,
5235 replacementprice => 99.00,
5240 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
5241 AddIssue( $patron, $item->barcode );
5243 my $accountline = Koha::Account::Line->new(
5245 borrowernumber => $patron->borrowernumber,
5246 debit_type_code => 'LOST',
5248 itemnumber => $item->itemnumber,
5250 amountoutstanding => 12,
5251 interface => 'something',
5255 # AddRenewal doesn't call _FixAccountForLostAndFound
5256 AddIssue( $patron, $item->barcode );
5258 is( $patron->checkouts->count, 1,
5259 'Renewal should not return the item even if a LOST payment has been made earlier'
5263 subtest 'Filling a hold should cancel existing transfer' => sub {
5266 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
5268 my $libraryA = $builder->build_object( { class => 'Koha::Libraries' } );
5269 my $libraryB = $builder->build_object( { class => 'Koha::Libraries' } );
5270 my $patron = $builder->build_object(
5272 class => 'Koha::Patrons',
5274 categorycode => $patron_category->{categorycode},
5275 branchcode => $libraryA->branchcode,
5280 my $item = $builder->build_sample_item({
5281 homebranch => $libraryB->branchcode,
5284 my ( undef, $message ) = AddReturn( $item->barcode, $libraryA->branchcode, undef, undef );
5285 is( Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef })->count, 1, "We generate a transfer on checkin");
5287 branchcode => $libraryA->branchcode,
5288 borrowernumber => $patron->borrowernumber,
5289 biblionumber => $item->biblionumber,
5290 itemnumber => $item->itemnumber
5292 my $reserves = Koha::Holds->search({ itemnumber => $item->itemnumber });
5293 is( $reserves->count, 1, "Reserve is placed");
5294 ( undef, $message ) = AddReturn( $item->barcode, $libraryA->branchcode, undef, undef );
5295 my $reserve = $reserves->next;
5296 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 0, $reserve->reserve_id );
5297 $reserve->discard_changes;
5298 ok( $reserve->found eq 'W', "Reserve is marked waiting" );
5299 is( Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef })->count, 0, "No outstanding transfers when hold is waiting");
5302 subtest 'Tests for NoRefundOnLostReturnedItemsAge with AddReturn' => sub {
5306 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
5307 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
5308 my $patron = $builder->build_object(
5310 class => 'Koha::Patrons',
5311 value => { categorycode => $patron_category->{categorycode} }
5315 my $biblionumber = $builder->build_sample_biblio(
5317 branchcode => $library->branchcode,
5321 # And the circulation rule
5322 Koha::CirculationRules->search->delete;
5323 Koha::CirculationRules->set_rules(
5325 categorycode => undef,
5327 branchcode => undef,
5330 lengthunit => 'days',
5336 source => 'CirculationRule',
5338 branchcode => undef,
5339 categorycode => undef,
5341 rule_name => 'lostreturn',
5342 rule_value => 'refund'
5347 subtest 'NoRefundOnLostReturnedItemsAge = undef' => sub {
5350 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5351 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef );
5353 my $lost_on = dt_from_string->subtract( days => 7 )->date;
5355 my $item = $builder->build_sample_item(
5357 biblionumber => $biblionumber,
5358 library => $library->branchcode,
5359 replacementprice => '42',
5362 my $issue = AddIssue( $patron, $item->barcode );
5363 LostItem( $item->itemnumber, 'cli', 0 );
5364 $item->_result->itemlost(1);
5365 $item->_result->itemlost_on( $lost_on );
5366 $item->_result->update();
5368 my $a = Koha::Account::Lines->search(
5370 itemnumber => $item->id,
5371 borrowernumber => $patron->borrowernumber
5374 ok( $a, "Found accountline for lost fee" );
5375 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5376 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
5377 $a = $a->get_from_storage;
5378 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
5382 subtest 'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
5385 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5386 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5388 my $lost_on = dt_from_string->subtract( days => 6 )->date;
5390 my $item = $builder->build_sample_item(
5392 biblionumber => $biblionumber,
5393 library => $library->branchcode,
5394 replacementprice => '42',
5397 my $issue = AddIssue( $patron, $item->barcode );
5398 LostItem( $item->itemnumber, 'cli', 0 );
5399 $item->_result->itemlost(1);
5400 $item->_result->itemlost_on( $lost_on );
5401 $item->_result->update();
5403 my $a = Koha::Account::Lines->search(
5405 itemnumber => $item->id,
5406 borrowernumber => $patron->borrowernumber
5409 ok( $a, "Found accountline for lost fee" );
5410 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5411 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
5412 $a = $a->get_from_storage;
5413 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
5417 subtest 'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
5420 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5421 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5423 my $lost_on = dt_from_string->subtract( days => 7 )->date;
5425 my $item = $builder->build_sample_item(
5427 biblionumber => $biblionumber,
5428 library => $library->branchcode,
5429 replacementprice => '42',
5432 my $issue = AddIssue( $patron, $item->barcode );
5433 LostItem( $item->itemnumber, 'cli', 0 );
5434 $item->_result->itemlost(1);
5435 $item->_result->itemlost_on( $lost_on );
5436 $item->_result->update();
5438 my $a = Koha::Account::Lines->search(
5440 itemnumber => $item->id,
5441 borrowernumber => $patron->borrowernumber
5444 ok( $a, "Found accountline for lost fee" );
5445 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5446 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
5447 $a = $a->get_from_storage;
5448 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
5452 subtest 'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
5455 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5456 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5458 my $lost_on = dt_from_string->subtract( days => 8 )->date;
5460 my $item = $builder->build_sample_item(
5462 biblionumber => $biblionumber,
5463 library => $library->branchcode,
5464 replacementprice => '42',
5467 my $issue = AddIssue( $patron, $item->barcode );
5468 LostItem( $item->itemnumber, 'cli', 0 );
5469 $item->_result->itemlost(1);
5470 $item->_result->itemlost_on( $lost_on );
5471 $item->_result->update();
5473 my $a = Koha::Account::Lines->search(
5475 itemnumber => $item->id,
5476 borrowernumber => $patron->borrowernumber
5480 ok( $a, "Found accountline for lost fee" );
5481 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5482 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
5483 $a = $a->get_from_storage;
5484 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
5489 subtest 'Tests for NoRefundOnLostReturnedItemsAge with AddIssue' => sub {
5493 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
5494 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
5495 my $patron = $builder->build_object(
5497 class => 'Koha::Patrons',
5498 value => { categorycode => $patron_category->{categorycode} }
5501 my $patron2 = $builder->build_object(
5503 class => 'Koha::Patrons',
5504 value => { categorycode => $patron_category->{categorycode} }
5508 my $biblionumber = $builder->build_sample_biblio(
5510 branchcode => $library->branchcode,
5514 # And the circulation rule
5515 Koha::CirculationRules->search->delete;
5516 Koha::CirculationRules->set_rules(
5518 categorycode => undef,
5520 branchcode => undef,
5523 lengthunit => 'days',
5529 source => 'CirculationRule',
5531 branchcode => undef,
5532 categorycode => undef,
5534 rule_name => 'lostreturn',
5535 rule_value => 'refund'
5540 subtest 'NoRefundOnLostReturnedItemsAge = undef' => sub {
5543 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5544 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef );
5546 my $lost_on = dt_from_string->subtract( days => 7 )->date;
5548 my $item = $builder->build_sample_item(
5550 biblionumber => $biblionumber,
5551 library => $library->branchcode,
5552 replacementprice => '42',
5555 my $issue = AddIssue( $patron, $item->barcode );
5556 LostItem( $item->itemnumber, 'cli', 0 );
5557 $item->_result->itemlost(1);
5558 $item->_result->itemlost_on( $lost_on );
5559 $item->_result->update();
5561 my $a = Koha::Account::Lines->search(
5563 itemnumber => $item->id,
5564 borrowernumber => $patron->borrowernumber
5567 ok( $a, "Found accountline for lost fee" );
5568 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5569 $issue = AddIssue( $patron2, $item->barcode );
5570 $a = $a->get_from_storage;
5571 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
5576 subtest 'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
5579 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5580 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5582 my $lost_on = dt_from_string->subtract( days => 6 )->date;
5584 my $item = $builder->build_sample_item(
5586 biblionumber => $biblionumber,
5587 library => $library->branchcode,
5588 replacementprice => '42',
5591 my $issue = AddIssue( $patron, $item->barcode );
5592 LostItem( $item->itemnumber, 'cli', 0 );
5593 $item->_result->itemlost(1);
5594 $item->_result->itemlost_on( $lost_on );
5595 $item->_result->update();
5597 my $a = Koha::Account::Lines->search(
5599 itemnumber => $item->id,
5600 borrowernumber => $patron->borrowernumber
5603 ok( $a, "Found accountline for lost fee" );
5604 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5605 $issue = AddIssue( $patron2, $item->barcode );
5606 $a = $a->get_from_storage;
5607 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
5611 subtest 'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
5614 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5615 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5617 my $lost_on = dt_from_string->subtract( days => 7 )->date;
5619 my $item = $builder->build_sample_item(
5621 biblionumber => $biblionumber,
5622 library => $library->branchcode,
5623 replacementprice => '42',
5626 my $issue = AddIssue( $patron, $item->barcode );
5627 LostItem( $item->itemnumber, 'cli', 0 );
5628 $item->_result->itemlost(1);
5629 $item->_result->itemlost_on( $lost_on );
5630 $item->_result->update();
5632 my $a = Koha::Account::Lines->search(
5634 itemnumber => $item->id,
5635 borrowernumber => $patron->borrowernumber
5638 ok( $a, "Found accountline for lost fee" );
5639 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5640 $issue = AddIssue( $patron2, $item->barcode );
5641 $a = $a->get_from_storage;
5642 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
5646 subtest 'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
5649 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
5650 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
5652 my $lost_on = dt_from_string->subtract( days => 8 )->date;
5654 my $item = $builder->build_sample_item(
5656 biblionumber => $biblionumber,
5657 library => $library->branchcode,
5658 replacementprice => '42',
5661 my $issue = AddIssue( $patron, $item->barcode );
5662 LostItem( $item->itemnumber, 'cli', 0 );
5663 $item->_result->itemlost(1);
5664 $item->_result->itemlost_on( $lost_on );
5665 $item->_result->update();
5667 my $a = Koha::Account::Lines->search(
5669 itemnumber => $item->id,
5670 borrowernumber => $patron->borrowernumber
5674 ok( $a, "Found accountline for lost fee" );
5675 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
5676 $issue = AddIssue( $patron2, $item->barcode );
5677 $a = $a->get_from_storage;
5678 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
5683 subtest 'transferbook tests' => sub {
5687 { C4::Circulation::transferbook({}); }
5688 'Koha::Exceptions::MissingParameter',
5689 'Koha::Patron->store raises an exception on missing params';
5692 { C4::Circulation::transferbook({to_branch=>'anything'}); }
5693 'Koha::Exceptions::MissingParameter',
5694 'Koha::Patron->store raises an exception on missing params';
5697 { C4::Circulation::transferbook({from_branch=>'anything'}); }
5698 'Koha::Exceptions::MissingParameter',
5699 'Koha::Patron->store raises an exception on missing params';
5701 my ($doreturn,$messages) = C4::Circulation::transferbook({to_branch=>'there',from_branch=>'here'});
5702 is( $doreturn, 0, "No return without barcode");
5703 ok( exists $messages->{BadBarcode}, "We get a BadBarcode message if no barcode passed");
5704 is( $messages->{BadBarcode}, undef, "No barcode passed means undef BadBarcode" );
5706 ($doreturn,$messages) = C4::Circulation::transferbook({to_branch=>'there',from_branch=>'here',barcode=>'BadBarcode'});
5707 is( $doreturn, 0, "No return without barcode");
5708 ok( exists $messages->{BadBarcode}, "We get a BadBarcode message if no barcode passed");
5709 is( $messages->{BadBarcode}, 'BadBarcode', "No barcode passed means undef BadBarcode" );
5713 subtest 'Checkout should correctly terminate a transfer' => sub {
5716 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
5717 my $patron_1 = $builder->build_object(
5719 class => 'Koha::Patrons',
5720 value => { branchcode => $library_1->branchcode }
5723 my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
5724 my $patron_2 = $builder->build_object(
5726 class => 'Koha::Patrons',
5727 value => { branchcode => $library_2->branchcode }
5731 my $item = $builder->build_sample_item(
5733 library => $library_1->branchcode,
5737 t::lib::Mocks::mock_userenv( { branchcode => $library_1->branchcode } );
5738 my $reserve_id = AddReserve(
5740 branchcode => $library_2->branchcode,
5741 borrowernumber => $patron_2->borrowernumber,
5742 biblionumber => $item->biblionumber,
5743 itemnumber => $item->itemnumber,
5748 my $do_transfer = 1;
5749 ModItemTransfer( $item->itemnumber, $library_1->branchcode,
5750 $library_2->branchcode, 'Manual' );
5751 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
5752 GetOtherReserves( $item->itemnumber )
5753 ; # To put the Reason, it's what does returns.pl...
5754 my $hold = Koha::Holds->find($reserve_id);
5755 is( $hold->found, 'T', 'Hold is in transit' );
5756 my $transfer = $item->get_transfer;
5757 is( $transfer->frombranch, $library_1->branchcode );
5758 is( $transfer->tobranch, $library_2->branchcode );
5759 is( $transfer->reason, 'Reserve' );
5761 t::lib::Mocks::mock_userenv( { branchcode => $library_2->branchcode } );
5762 AddIssue( $patron_1, $item->barcode );
5763 $transfer = $transfer->get_from_storage;
5764 isnt( $transfer->datearrived, undef );
5765 $hold = $hold->get_from_storage;
5766 is( $hold->found, undef, 'Hold is waiting' );
5767 is( $hold->priority, 1, );
5770 subtest 'AddIssue records staff who checked out item if appropriate' => sub {
5773 $module->mock( 'userenv', sub { { branch => $library->{id} } } );
5775 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
5776 my $patron = $builder->build_object(
5778 class => 'Koha::Patrons',
5779 value => { categorycode => $patron_category->{categorycode} }
5782 my $issuer = $builder->build_object(
5784 class => 'Koha::Patrons',
5785 value => { categorycode => $patron_category->{categorycode} }
5788 my $item_1 = $builder->build_sample_item(
5790 library => $library->{branchcode}
5794 my $item_2 = $builder->build_sample_item(
5796 library => $library->branchcode
5800 $module->mock( 'userenv', sub { { branch => $library->id, number => $issuer->borrowernumber } } );
5802 my $dt_from = dt_from_string();
5803 my $dt_to = dt_from_string()->add( days => 7 );
5805 my $issue_1 = AddIssue( $patron, $item_1->barcode, $dt_to, undef, $dt_from );
5807 is( $issue_1->issuer, undef, "Staff who checked out the item not recorded when RecordStaffUserOnCheckout turned off" );
5809 t::lib::Mocks::mock_preference('RecordStaffUserOnCheckout', 1);
5812 AddIssue( $patron, $item_2->barcode, $dt_to, undef, $dt_from );
5814 is( $issue_2->issuer->borrowernumber, $issuer->borrowernumber, "Staff who checked out the item recorded when RecordStaffUserOnCheckout turned on" );
5817 subtest "Item's onloan value should be set if checked out item is checked out to a different patron" => sub {
5820 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
5821 my $patron_1 = $builder->build_object(
5823 class => 'Koha::Patrons',
5824 value => { branchcode => $library_1->branchcode }
5827 my $patron_2 = $builder->build_object(
5829 class => 'Koha::Patrons',
5830 value => { branchcode => $library_1->branchcode }
5834 my $item = $builder->build_sample_item(
5836 library => $library_1->branchcode,
5840 AddIssue( $patron_1, $item->barcode );
5841 ok( $item->get_from_storage->onloan, "Item's onloan column is set after initial checkout" );
5842 AddIssue( $patron_2, $item->barcode );
5843 ok( $item->get_from_storage->onloan, "Item's onloan column is set after second checkout" );
5846 subtest "updateWrongTransfer tests" => sub {
5849 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
5850 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
5851 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
5852 my $item = $builder->build_sample_item(
5854 homebranch => $library1->branchcode,
5855 holdingbranch => $library2->branchcode,
5856 datelastseen => undef
5860 my $transfer = $builder->build_object(
5862 class => 'Koha::Item::Transfers',
5864 itemnumber => $item->itemnumber,
5865 frombranch => $library2->branchcode,
5866 tobranch => $library1->branchcode,
5867 daterequested => dt_from_string,
5868 datesent => dt_from_string,
5869 datecancelled => undef,
5870 datearrived => undef,
5875 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
5877 my $new_transfer = C4::Circulation::updateWrongTransfer($item->itemnumber, $library1->branchcode);
5878 is(ref($new_transfer), 'Koha::Item::Transfer', "updateWrongTransfer returns a 'Koha::Item::Transfer' object");
5879 ok( !$new_transfer->in_transit, "New transfer is NOT created as in transit (or cancelled)");
5881 my $original_transfer = $transfer->get_from_storage;
5882 ok( defined($original_transfer->datecancelled), "Original transfer was cancelled");
5883 is( $original_transfer->cancellation_reason, 'WrongTransfer', "Original transfer cancellation reason is 'WrongTransfer'");
5886 subtest "SendCirculationAlert" => sub {
5889 # When you would unsuspectingly call this unit test (with perl, not prove), you will be bitten by LOCK.
5890 # LOCK will commit changes and ruin your data
5891 # In order to prevent that, we will add KOHA_TESTING to $ENV; see further Circulation.pm
5892 $ENV{KOHA_TESTING} = 1;
5894 # Setup branch, borrowr, and notice
5895 my $library = $builder->build_object({ class => 'Koha::Libraries' });
5896 set_userenv( $library->unblessed);
5897 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
5898 C4::Members::Messaging::SetMessagingPreference({
5899 borrowernumber => $patron->id,
5900 message_transport_types => ['sms'],
5901 message_attribute_id => 5
5903 my $item = $builder->build_sample_item();
5904 my $checkin_notice = $builder->build_object({
5905 class => 'Koha::Notice::Templates',
5907 module => 'circulation',
5909 branchcode => $library->branchcode,
5910 name => 'Test Checkin',
5912 content => "Checkins:\n----\n[% biblio.title %]-[% old_checkout.issue_id %]\n----Thank you.",
5913 message_transport_type => 'sms',
5918 # Checkout an item, mark it returned, generate a notice
5919 my $issue_1 = AddIssue( $patron, $item->barcode);
5920 MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0, { skip_record_index => 1} );
5921 C4::Circulation::SendCirculationAlert({
5923 item => $item->unblessed,
5924 borrower => $patron->unblessed,
5925 branch => $library->branchcode,
5928 my $notice = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'CHECKIN' });
5929 is($notice->content,"Checkins:\n".$item->biblio->title."-".$issue_1->id."\nThank you.", 'Letter generated with expected output on first checkin' );
5930 is($notice->to_address, $patron->smsalertnumber, "Letter has the correct to_address set to smsalertnumber for SMS type notices");
5932 # Checkout an item, mark it returned, generate a notice
5933 my $issue_2 = AddIssue( $patron, $item->barcode);
5934 MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0, { skip_record_index => 1} );
5935 C4::Circulation::SendCirculationAlert({
5937 item => $item->unblessed,
5938 borrower => $patron->unblessed,
5939 branch => $library->branchcode,
5942 $notice->discard_changes();
5943 is($notice->content,"Checkins:\n".$item->biblio->title."-".$issue_1->id."\n".$item->biblio->title."-".$issue_2->id."\nThank you.", 'Letter appended with expected output on second checkin' );
5947 subtest "GetSoonestRenewDate tests" => sub {
5949 Koha::CirculationRules->set_rule(
5951 categorycode => undef,
5952 branchcode => undef,
5954 rule_name => 'norenewalbefore',
5958 my $patron = $builder->build_object(
5960 class => 'Koha::Patrons',
5962 autorenew_checkouts => 1,
5966 my $item = $builder->build_sample_item();
5967 my $issue = AddIssue( $patron, $item->barcode);
5968 my $datedue = dt_from_string( $issue->date_due() );
5971 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
5972 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
5974 GetSoonestRenewDate( $patron, $issue ),
5975 $datedue->clone->add( days => -7 ),
5976 'Bug 14395: Renewals permitted 7 days before due date, as expected'
5980 # Test 'date' setting for syspref NoRenewalBeforePrecision
5981 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
5983 GetSoonestRenewDate( $patron, $issue ),
5984 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
5985 'Bug 14395: Renewals permitted 7 days before due date, as expected'
5989 Koha::CirculationRules->set_rule(
5991 categorycode => undef,
5992 branchcode => undef,
5994 rule_name => 'norenewalbefore',
5995 rule_value => undef,
6000 GetSoonestRenewDate( $patron, $issue ),
6002 'Checkouts without auto-renewal can be renewed immediately if no norenewalbefore'
6005 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
6006 $issue->auto_renew(1)->store;
6008 GetSoonestRenewDate( $patron, $issue, 1 ),
6009 $datedue->clone->truncate( to => 'day' ),
6010 'Checkouts with auto-renewal can be renewed earliest on due date if noautorenewalbefore'
6012 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact' );
6014 GetSoonestRenewDate( $patron, $issue, 1 ),
6016 'Checkouts with auto-renewal can be renewed earliest on due date if noautorenewalbefore'
6019 t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
6020 Koha::CirculationRules->set_rule(
6022 categorycode => undef,
6023 branchcode => undef,
6025 rule_name => 'norenewalbefore',
6029 $issue->date_due(dt_from_string)->store;
6031 GetSoonestRenewDate( $patron, $issue ),
6032 dt_from_string->subtract( days => 1 )->truncate( to => 'day' ),
6033 'Checkouts with auto-renewal can be renewed 1 day before due date if no renewalbefore = 1 and precision = "date"'
6038 subtest "CanBookBeIssued + needsconfirmation message" => sub {
6041 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
6042 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
6043 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
6044 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { biblionumber => $biblio->biblionumber }});
6045 my $item = $builder->build_object({ class => 'Koha::Items' , value => { itype => $itemtype, biblionumber => $biblio->biblionumber }});
6047 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
6048 biblionumber => $item->biblionumber,
6049 branchcode => $library->branchcode,
6050 itemnumber => undef,
6055 item_group_id => undef
6058 my ( $error, $needsconfirmation, $alerts, $messages );
6060 ( $error, $needsconfirmation, $alerts, $messages ) = CanBookBeIssued( $patron, $item->barcode );
6061 is($needsconfirmation->{resbranchcode}, $hold->branchcode, "Branchcodes match when hold exists.");
6063 $hold->priority(0)->store();
6065 $hold->found("W")->store();
6066 ( $error, $needsconfirmation, $alerts, $messages ) = CanBookBeIssued( $patron, $item->barcode );
6067 is($needsconfirmation->{resbranchcode}, $hold->branchcode, "Branchcodes match when hold is waiting.");
6069 $hold->found("T")->store();
6070 ( $error, $needsconfirmation, $alerts, $messages ) = CanBookBeIssued( $patron, $item->barcode );
6071 is($needsconfirmation->{resbranchcode}, $hold->branchcode, "Branchcodes match when hold is being transferred.");
6073 $hold->found("P")->store();
6074 ( $error, $needsconfirmation, $alerts, $messages ) = CanBookBeIssued( $patron, $item->barcode );
6075 is($needsconfirmation->{resbranchcode}, $hold->branchcode, "Branchcodes match when hold is being processed.");
6078 subtest 'Tests for BlockReturnOfWithdrawnItems' => sub {
6082 t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 1);
6083 t::lib::Mocks::mock_preference('RecordLocalUseOnReturn', 0);
6084 my $item = $builder->build_sample_item();
6085 $item->withdrawn(1)->itemlost(1)->store;
6086 my @return = AddReturn( $item->barcode, $item->homebranch, 0, undef );
6089 [ 0, { NotIssued => $item->barcode, withdrawn => 1 }, undef, {} ], "Item returned as withdrawn, no other messages");
6092 subtest 'Tests for transfer not in transit' => sub {
6097 # These tests are to ensure a 'pending' transfer, generated by
6098 # stock rotation, will be advanced when checked in
6100 my $item = $builder->build_sample_item();
6101 my $transfer = $builder->build_object({ class => 'Koha::Item::Transfers', value => {
6102 itemnumber => $item->id,
6103 reason => 'StockrotationRepatriation',
6105 frombranch => $item->homebranch,
6107 my @return = AddReturn( $item->barcode, $item->homebranch, 0, undef );
6110 [ 0, { WasTransfered => $transfer->tobranch, TransferTrigger => 'StockrotationRepatriation', NotIssued => $item->barcode }, undef, {} ], "Item is reported to have been transferred");
6112 $transfer->discard_changes;
6113 ok( $transfer->datesent, 'The datesent field is populated, i.e. transfer is initiated');
6117 subtest 'Tests for RecordLocalUseOnReturn' => sub {
6121 t::lib::Mocks::mock_preference('RecordLocalUseOnReturn', 0);
6122 my $item = $builder->build_sample_item();
6123 $item->withdrawn(1)->itemlost(1)->store;
6124 my @return = AddReturn( $item->barcode, $item->homebranch, 0, undef );
6127 [ 0, { NotIssued => $item->barcode, withdrawn => 1 }, undef, {} ], "RecordLocalUSeOnReturn is off, no local use recorded");
6129 t::lib::Mocks::mock_preference('RecordLocalUseOnReturn', 1);
6130 my @return2 = AddReturn( $item->barcode, $item->homebranch, 0, undef );
6133 [ 0, { NotIssued => $item->barcode, withdrawn => 1, LocalUse => 1 }, undef, {} ], "Local use is recorded");
6136 $schema->storage->txn_rollback;
6137 C4::Context->clear_syspref_cache();
6138 $branches = Koha::Libraries->search();
6139 for my $branch ( $branches->next ) {
6140 my $key = $branch->branchcode . "_holidays";
6141 $cache->clear_from_cache($key);