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
=> 50;
24 use Test
::Deep
qw( cmp_deeply );
29 use POSIX
qw( floor );
31 use t
::lib
::TestBuilder
;
40 use C4
::Overdues
qw(UpdateFine CalcFine);
44 use Koha
::Item
::Transfers
;
48 use Koha
::CirculationRules
;
49 use Koha
::Subscriptions
;
50 use Koha
::Account
::Lines
;
51 use Koha
::Account
::Offsets
;
56 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library->{branchcode
} });
60 my ( $error, $question, $alert ) = @_;
62 $s = %$error ?
' (error: ' . join( ' ', keys %$error ) . ')' : '';
63 $s .= %$question ?
' (question: ' . join( ' ', keys %$question ) . ')' : '';
64 $s .= %$alert ?
' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
68 sub test_debarment_on_checkout
{
70 my $item = $params->{item
};
71 my $library = $params->{library
};
72 my $patron = $params->{patron
};
73 my $due_date = $params->{due_date
} || dt_from_string
;
74 my $return_date = $params->{return_date
} || dt_from_string
;
75 my $expected_expiration_date = $params->{expiration_date
};
77 $expected_expiration_date = output_pref
(
79 dt
=> $expected_expiration_date,
85 my $line_number = $caller[2];
86 AddIssue
( $patron, $item->barcode, $due_date );
88 my ( undef, $message ) = AddReturn
( $item->barcode, $library->{branchcode
}, undef, $return_date );
89 is
( $message->{WasReturned
} && exists $message->{Debarred
}, 1, 'AddReturn must have debarred the patron' )
90 or diag
('AddReturn returned message ' . Dumper
$message );
91 my $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
92 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
93 is
( scalar(@
$debarments), 1, 'Test at line ' . $line_number );
95 is
( $debarments->[0]->{expiration
},
96 $expected_expiration_date, 'Test at line ' . $line_number );
97 Koha
::Patron
::Debarments
::DelUniqueDebarment
(
98 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
101 my $schema = Koha
::Database
->schema;
102 $schema->storage->txn_begin;
103 my $builder = t
::lib
::TestBuilder
->new;
104 my $dbh = C4
::Context
->dbh;
106 # Prevent random failures by mocking ->now
107 my $now_value = dt_from_string
;
108 my $mocked_datetime = Test
::MockModule
->new('DateTime');
109 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
111 my $cache = Koha
::Caches
->get_instance();
112 $dbh->do(q
|DELETE FROM special_holidays
|);
113 $dbh->do(q
|DELETE FROM repeatable_holidays
|);
114 my $branches = Koha
::Libraries
->search();
115 for my $branch ( $branches->next ) {
116 my $key = $branch->branchcode . "_holidays";
117 $cache->clear_from_cache($key);
120 # Start with a clean slate
121 $dbh->do('DELETE FROM issues');
122 $dbh->do('DELETE FROM borrowers');
124 my $library = $builder->build({
127 my $library2 = $builder->build({
130 my $itemtype = $builder->build(
132 source
=> 'Itemtype',
136 rentalcharge_daily
=> 0,
137 defaultreplacecost
=> undef,
142 my $patron_category = $builder->build(
144 source
=> 'Category',
146 category_type
=> 'P',
148 BlockExpiredPatronOpacActions
=> -1, # Pick the pref value
153 my $CircControl = C4
::Context
->preference('CircControl');
154 my $HomeOrHoldingBranch = C4
::Context
->preference('HomeOrHoldingBranch');
157 homebranch
=> $library2->{branchcode
},
158 holdingbranch
=> $library2->{branchcode
}
162 branchcode
=> $library2->{branchcode
}
165 t
::lib
::Mocks
::mock_preference
('AutoReturnCheckedOutItems', 0);
167 # No userenv, PickupLibrary
168 t
::lib
::Mocks
::mock_preference
('IndependentBranches', '0');
169 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
171 C4
::Context
->preference('CircControl'),
173 'CircControl changed to PickupLibrary'
176 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
177 $item->{$HomeOrHoldingBranch},
178 '_GetCircControlBranch returned item branch (no userenv defined)'
181 # No userenv, PatronLibrary
182 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
184 C4
::Context
->preference('CircControl'),
186 'CircControl changed to PatronLibrary'
189 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
190 $borrower->{branchcode
},
191 '_GetCircControlBranch returned borrower branch'
194 # No userenv, ItemHomeLibrary
195 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
197 C4
::Context
->preference('CircControl'),
199 'CircControl changed to ItemHomeLibrary'
202 $item->{$HomeOrHoldingBranch},
203 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
204 '_GetCircControlBranch returned item branch'
208 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library2->{branchcode
} });
209 is
(C4
::Context
->userenv->{branch
}, $library2->{branchcode
}, 'userenv set');
211 # Userenv set, PickupLibrary
212 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
214 C4
::Context
->preference('CircControl'),
216 'CircControl changed to PickupLibrary'
219 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
220 $library2->{branchcode
},
221 '_GetCircControlBranch returned current branch'
224 # Userenv set, PatronLibrary
225 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
227 C4
::Context
->preference('CircControl'),
229 'CircControl changed to PatronLibrary'
232 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
233 $borrower->{branchcode
},
234 '_GetCircControlBranch returned borrower branch'
237 # Userenv set, ItemHomeLibrary
238 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
240 C4
::Context
->preference('CircControl'),
242 'CircControl changed to ItemHomeLibrary'
245 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
246 $item->{$HomeOrHoldingBranch},
247 '_GetCircControlBranch returned item branch'
250 # Reset initial configuration
251 t
::lib
::Mocks
::mock_preference
('CircControl', $CircControl);
253 C4
::Context
->preference('CircControl'),
255 'CircControl reset to its initial value'
258 # Set a simple circ policy
259 $dbh->do('DELETE FROM circulation_rules');
260 Koha
::CirculationRules
->set_rules(
262 categorycode
=> undef,
266 reservesallowed
=> 25,
268 lengthunit
=> 'days',
269 renewalsallowed
=> 1,
271 norenewalbefore
=> undef,
279 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
280 subtest
"CanBookBeRenewed tests" => sub {
283 C4
::Context
->set_preference('ItemsDeniedRenewal','');
284 # Generate test biblio
285 my $biblio = $builder->build_sample_biblio();
287 my $branch = $library2->{branchcode
};
289 my $item_1 = $builder->build_sample_item(
291 biblionumber
=> $biblio->biblionumber,
293 replacementprice
=> 12.00,
297 $reused_itemnumber_1 = $item_1->itemnumber;
299 my $item_2 = $builder->build_sample_item(
301 biblionumber
=> $biblio->biblionumber,
303 replacementprice
=> 23.00,
307 $reused_itemnumber_2 = $item_2->itemnumber;
309 my $item_3 = $builder->build_sample_item(
311 biblionumber
=> $biblio->biblionumber,
313 replacementprice
=> 23.00,
319 my %renewing_borrower_data = (
321 surname
=> 'Renewal',
322 categorycode
=> $patron_category->{categorycode
},
323 branchcode
=> $branch,
326 my %reserving_borrower_data = (
327 firstname
=> 'Katrin',
328 surname
=> 'Reservation',
329 categorycode
=> $patron_category->{categorycode
},
330 branchcode
=> $branch,
333 my %hold_waiting_borrower_data = (
335 surname
=> 'Reservation',
336 categorycode
=> $patron_category->{categorycode
},
337 branchcode
=> $branch,
340 my %restricted_borrower_data = (
341 firstname
=> 'Alice',
342 surname
=> 'Reservation',
343 categorycode
=> $patron_category->{categorycode
},
344 debarred
=> '3228-01-01',
345 branchcode
=> $branch,
348 my %expired_borrower_data = (
351 categorycode
=> $patron_category->{categorycode
},
352 branchcode
=> $branch,
353 dateexpiry
=> dt_from_string
->subtract( months
=> 1 ),
356 my $renewing_borrowernumber = Koha
::Patron
->new(\
%renewing_borrower_data)->store->borrowernumber;
357 my $reserving_borrowernumber = Koha
::Patron
->new(\
%reserving_borrower_data)->store->borrowernumber;
358 my $hold_waiting_borrowernumber = Koha
::Patron
->new(\
%hold_waiting_borrower_data)->store->borrowernumber;
359 my $restricted_borrowernumber = Koha
::Patron
->new(\
%restricted_borrower_data)->store->borrowernumber;
360 my $expired_borrowernumber = Koha
::Patron
->new(\
%expired_borrower_data)->store->borrowernumber;
362 my $renewing_borrower_obj = Koha
::Patrons
->find( $renewing_borrowernumber );
363 my $renewing_borrower = $renewing_borrower_obj->unblessed;
364 my $restricted_borrower = Koha
::Patrons
->find( $restricted_borrowernumber )->unblessed;
365 my $expired_borrower = Koha
::Patrons
->find( $expired_borrowernumber )->unblessed;
372 my $checkitem = undef;
375 my $issue = AddIssue
( $renewing_borrower, $item_1->barcode);
376 my $datedue = dt_from_string
( $issue->date_due() );
377 is
(defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
379 my $issue2 = AddIssue
( $renewing_borrower, $item_2->barcode);
380 $datedue = dt_from_string
( $issue->date_due() );
381 is
(defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
384 my $borrowing_borrowernumber = Koha
::Checkouts
->find( { itemnumber
=> $item_1->itemnumber } )->borrowernumber;
385 is
($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
387 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
388 is
( $renewokay, 1, 'Can renew, no holds for this title or item');
391 # Biblio-level hold, renewal test
394 branchcode
=> $branch,
395 borrowernumber
=> $reserving_borrowernumber,
396 biblionumber
=> $biblio->biblionumber,
397 priority
=> $priority,
398 reservation_date
=> $resdate,
399 expiration_date
=> $expdate,
401 itemnumber
=> $checkitem,
406 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
407 Koha
::CirculationRules
->set_rule(
409 categorycode
=> undef,
412 rule_name
=> 'onshelfholds',
416 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 1 );
417 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
418 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
419 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
420 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
422 # Now let's add an item level hold, we should no longer be able to renew the item
423 my $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
425 borrowernumber
=> $hold_waiting_borrowernumber,
426 biblionumber
=> $biblio->biblionumber,
427 itemnumber
=> $item_1->itemnumber,
428 branchcode
=> $branch,
432 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
433 is
( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
436 # 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
437 # be able to renew these items
438 $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
440 borrowernumber
=> $hold_waiting_borrowernumber,
441 biblionumber
=> $biblio->biblionumber,
442 itemnumber
=> $item_3->itemnumber,
443 branchcode
=> $branch,
448 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
449 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
450 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
451 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
452 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 0 );
454 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
455 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
456 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
458 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
459 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
460 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
462 my $reserveid = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber, borrowernumber
=> $reserving_borrowernumber })->next->reserve_id;
463 my $reserving_borrower = Koha
::Patrons
->find( $reserving_borrowernumber )->unblessed;
464 AddIssue
($reserving_borrower, $item_3->barcode);
465 my $reserve = $dbh->selectrow_hashref(
466 'SELECT * FROM old_reserves WHERE reserve_id = ?',
470 is
($reserve->{found
}, 'F', 'hold marked completed when checking out item that fills it');
472 # Item-level hold, renewal test
475 branchcode
=> $branch,
476 borrowernumber
=> $reserving_borrowernumber,
477 biblionumber
=> $biblio->biblionumber,
478 priority
=> $priority,
479 reservation_date
=> $resdate,
480 expiration_date
=> $expdate,
482 itemnumber
=> $item_1->itemnumber,
487 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
488 is
( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
489 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
491 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber, 1);
492 is
( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
494 # Items can't fill hold for reasons
495 $item_1->notforloan(1)->store;
496 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
497 is
( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
498 $item_1->set({notforloan
=> 0, itype
=> $itemtype })->store;
500 # FIXME: Add more for itemtype not for loan etc.
502 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
503 my $item_5 = $builder->build_sample_item(
505 biblionumber
=> $biblio->biblionumber,
507 replacementprice
=> 23.00,
511 my $datedue5 = AddIssue
($restricted_borrower, $item_5->barcode);
512 is
(defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
514 t
::lib
::Mocks
::mock_preference
('RestrictionBlockRenewing','1');
515 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
516 is
( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
517 ( $renewokay, $error ) = CanBookBeRenewed
($restricted_borrowernumber, $item_5->itemnumber);
518 is
( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
520 # Users cannot renew an overdue item
521 my $item_6 = $builder->build_sample_item(
523 biblionumber
=> $biblio->biblionumber,
525 replacementprice
=> 23.00,
530 my $item_7 = $builder->build_sample_item(
532 biblionumber
=> $biblio->biblionumber,
534 replacementprice
=> 23.00,
539 my $datedue6 = AddIssue
( $renewing_borrower, $item_6->barcode);
540 is
(defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
542 my $now = dt_from_string
();
543 my $five_weeks = DateTime
::Duration
->new(weeks
=> 5);
544 my $five_weeks_ago = $now - $five_weeks;
545 t
::lib
::Mocks
::mock_preference
('finesMode', 'production');
547 my $passeddatedue1 = AddIssue
($renewing_borrower, $item_7->barcode, $five_weeks_ago);
548 is
(defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
550 my ( $fine ) = CalcFine
( $item_7->unblessed, $renewing_borrower->{categorycode
}, $branch, $five_weeks_ago, $now );
551 C4
::Overdues
::UpdateFine
(
553 issue_id
=> $passeddatedue1->id(),
554 itemnumber
=> $item_7->itemnumber,
555 borrowernumber
=> $renewing_borrower->{borrowernumber
},
557 due
=> Koha
::DateUtils
::output_pref
($five_weeks_ago)
561 t
::lib
::Mocks
::mock_preference
('RenewalLog', 0);
562 my $date = output_pref
( { dt
=> dt_from_string
(), dateonly
=> 1, dateformat
=> 'iso' } );
563 my %params_renewal = (
564 timestamp
=> { -like
=> $date . "%" },
565 module
=> "CIRCULATION",
569 timestamp
=> { -like
=> $date . "%" },
570 module
=> "CIRCULATION",
573 my $old_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
574 my $dt = dt_from_string
();
575 Time
::Fake
->offset( $dt->epoch );
576 my $datedue1 = AddRenewal
( $renewing_borrower->{borrowernumber
}, $item_7->itemnumber, $branch );
577 my $new_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
578 is
($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
579 isnt
(DateTime
->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
582 t
::lib
::Mocks
::mock_preference
('RenewalLog', 1);
583 $date = output_pref
( { dt
=> dt_from_string
(), dateonly
=> 1, dateformat
=> 'iso' } );
584 $old_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
585 AddRenewal
( $renewing_borrower->{borrowernumber
}, $item_7->itemnumber, $branch );
586 $new_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
587 is
($new_log_size, $old_log_size + 1, 'renew log successfully added');
589 my $fines = Koha
::Account
::Lines
->search( { borrowernumber
=> $renewing_borrower->{borrowernumber
}, itemnumber
=> $item_7->itemnumber } );
590 is
( $fines->count, 2, 'AddRenewal left both fines' );
591 isnt
( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
592 isnt
( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
596 my $old_issue_log_size = Koha
::ActionLogs
->count( \
%params_issue );
597 my $old_renew_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
598 AddIssue
( $renewing_borrower,$item_7->barcode,Koha
::DateUtils
::output_pref
({str
=>$datedue6->date_due, dateformat
=>'iso'}),0,$date, 0, undef );
599 $new_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
600 is
($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
601 $new_log_size = Koha
::ActionLogs
->count( \
%params_issue );
602 is
($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
604 $fines = Koha
::Account
::Lines
->search( { borrowernumber
=> $renewing_borrower->{borrowernumber
}, itemnumber
=> $item_7->itemnumber } );
607 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','blockitem');
608 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_6->itemnumber);
609 is
( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
610 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_7->itemnumber);
611 is
( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
614 $hold = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber, borrowernumber
=> $reserving_borrowernumber })->next;
618 # Test automatic renewal before value for "norenewalbefore" in policy is set
619 # In this case automatic renewal is not permitted prior to due date
620 my $item_4 = $builder->build_sample_item(
622 biblionumber
=> $biblio->biblionumber,
624 replacementprice
=> 16.00,
629 $issue = AddIssue
( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew
=> 1 } );
630 ( $renewokay, $error ) =
631 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
632 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
633 is
( $error, 'auto_too_soon',
634 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
637 branchcode
=> $branch,
638 borrowernumber
=> $reserving_borrowernumber,
639 biblionumber
=> $biblio->biblionumber,
640 itemnumber
=> $bibitems,
641 priority
=> $priority,
642 reservation_date
=> $resdate,
643 expiration_date
=> $expdate,
646 itemnumber
=> $item_4->itemnumber,
650 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
651 is
( $renewokay, 0, 'Still should not be able to renew' );
652 is
( $error, 'on_reserve', 'returned code is on_reserve, reserve checked when not checking for cron' );
653 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber, undef, 1 );
654 is
( $renewokay, 0, 'Still should not be able to renew' );
655 is
( $error, 'auto_too_soon', 'returned code is auto_too_soon, reserve not checked when checking for cron' );
656 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber, 1 );
657 is
( $renewokay, 0, 'Still should not be able to renew' );
658 is
( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
659 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber, 1, 1 );
660 is
( $renewokay, 0, 'Still should not be able to renew' );
661 is
( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
662 $dbh->do('UPDATE circulation_rules SET rule_value = 0 where rule_name = "norenewalbefore"');
663 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber, 1 );
664 is
( $renewokay, 0, 'Still should not be able to renew' );
665 is
( $error, 'on_reserve', 'returned code is on_reserve, auto_renew only happens if not on reserve' );
666 ModReserveCancelAll
($item_4->itemnumber, $reserving_borrowernumber);
670 $renewing_borrower_obj->autorenew_checkouts(0)->store;
671 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
672 is
( $renewokay, 1, 'No renewal before is undef, but patron opted out of auto_renewal' );
673 $renewing_borrower_obj->autorenew_checkouts(1)->store;
677 # Test premature manual renewal
678 Koha
::CirculationRules
->set_rule(
680 categorycode
=> undef,
683 rule_name
=> 'norenewalbefore',
688 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
689 is
( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
690 is
( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
693 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
694 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'exact_time' );
696 GetSoonestRenewDate
( $renewing_borrowernumber, $item_1->itemnumber ),
697 $datedue->clone->add( days
=> -7 ),
698 'Bug 14395: Renewals permitted 7 days before due date, as expected'
702 # Test 'date' setting for syspref NoRenewalBeforePrecision
703 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'date' );
705 GetSoonestRenewDate
( $renewing_borrowernumber, $item_1->itemnumber ),
706 $datedue->clone->add( days
=> -7 )->truncate( to
=> 'day' ),
707 'Bug 14395: Renewals permitted 7 days before due date, as expected'
711 # Test premature automatic renewal
712 ( $renewokay, $error ) =
713 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
714 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
715 is
( $error, 'auto_too_soon',
716 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
719 $renewing_borrower_obj->autorenew_checkouts(0)->store;
720 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
721 is
( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
722 is
( $error, 'too_soon', 'Error is too_soon, no auto' );
723 $renewing_borrower_obj->autorenew_checkouts(1)->store;
725 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
726 # and test automatic renewal again
727 $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
728 ( $renewokay, $error ) =
729 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
730 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
731 is
( $error, 'auto_too_soon',
732 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
735 $renewing_borrower_obj->autorenew_checkouts(0)->store;
736 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
737 is
( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
738 is
( $error, 'too_soon', 'Error is too_soon, no auto' );
739 $renewing_borrower_obj->autorenew_checkouts(1)->store;
741 # Change policy so that loans can be renewed 99 days prior to the due date
742 # and test automatic renewal again
743 $dbh->do(q{UPDATE circulation_rules SET rule_value = '99' WHERE rule_name = 'norenewalbefore'});
744 ( $renewokay, $error ) =
745 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
746 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
747 is
( $error, 'auto_renew',
748 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
751 $renewing_borrower_obj->autorenew_checkouts(0)->store;
752 ( $renewokay, $error ) = CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
753 is
( $renewokay, 1, 'No renewal before is 99, patron opted out of auto_renewal so can renew' );
754 $renewing_borrower_obj->autorenew_checkouts(1)->store;
756 subtest
"too_late_renewal / no_auto_renewal_after" => sub {
758 my $item_to_auto_renew = $builder->build_sample_item(
760 biblionumber
=> $biblio->biblionumber,
765 my $ten_days_before = dt_from_string
->add( days
=> -10 );
766 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
767 AddIssue
( $renewing_borrower, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
769 Koha
::CirculationRules
->set_rules(
771 categorycode
=> undef,
775 norenewalbefore
=> '7',
776 no_auto_renewal_after
=> '9',
780 ( $renewokay, $error ) =
781 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
782 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
783 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
785 Koha
::CirculationRules
->set_rules(
787 categorycode
=> undef,
791 norenewalbefore
=> '7',
792 no_auto_renewal_after
=> '10',
796 ( $renewokay, $error ) =
797 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
798 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
799 is
( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
801 Koha
::CirculationRules
->set_rules(
803 categorycode
=> undef,
807 norenewalbefore
=> '7',
808 no_auto_renewal_after
=> '11',
812 ( $renewokay, $error ) =
813 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
814 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
815 is
( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
817 Koha
::CirculationRules
->set_rules(
819 categorycode
=> undef,
823 norenewalbefore
=> '10',
824 no_auto_renewal_after
=> '11',
828 ( $renewokay, $error ) =
829 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
830 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
831 is
( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
833 Koha
::CirculationRules
->set_rules(
835 categorycode
=> undef,
839 norenewalbefore
=> '10',
840 no_auto_renewal_after
=> undef,
841 no_auto_renewal_after_hard_limit
=> dt_from_string
->add( days
=> -1 ),
845 ( $renewokay, $error ) =
846 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
847 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
848 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
850 Koha
::CirculationRules
->set_rules(
852 categorycode
=> undef,
856 norenewalbefore
=> '7',
857 no_auto_renewal_after
=> '15',
858 no_auto_renewal_after_hard_limit
=> dt_from_string
->add( days
=> -1 ),
862 ( $renewokay, $error ) =
863 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
864 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
865 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
867 Koha
::CirculationRules
->set_rules(
869 categorycode
=> undef,
873 norenewalbefore
=> '10',
874 no_auto_renewal_after
=> undef,
875 no_auto_renewal_after_hard_limit
=> dt_from_string
->add( days
=> 1 ),
879 ( $renewokay, $error ) =
880 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
881 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
882 is
( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
885 subtest
"auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew & OPACFineNoRenewalsIncludeCredit" => sub {
887 my $item_to_auto_renew = $builder->build_sample_item(
889 biblionumber
=> $biblio->biblionumber,
894 my $ten_days_before = dt_from_string
->add( days
=> -10 );
895 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
896 AddIssue
( $renewing_borrower, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
898 Koha
::CirculationRules
->set_rules(
900 categorycode
=> undef,
904 norenewalbefore
=> '10',
905 no_auto_renewal_after
=> '11',
909 C4
::Context
->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
910 C4
::Context
->set_preference('OPACFineNoRenewals','10');
911 C4
::Context
->set_preference('OPACFineNoRenewalsIncludeCredit','1');
912 my $fines_amount = 5;
913 my $account = Koha
::Account
->new({patron_id
=> $renewing_borrowernumber});
916 amount
=> $fines_amount,
919 item_id
=> $item_to_auto_renew->itemnumber,
920 description
=> "Some fines"
922 )->status('RETURNED')->store;
923 ( $renewokay, $error ) =
924 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
925 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
926 is
( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
930 amount
=> $fines_amount,
933 item_id
=> $item_to_auto_renew->itemnumber,
934 description
=> "Some fines"
936 )->status('RETURNED')->store;
937 ( $renewokay, $error ) =
938 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
939 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
940 is
( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
944 amount
=> $fines_amount,
947 item_id
=> $item_to_auto_renew->itemnumber,
948 description
=> "Some fines"
950 )->status('RETURNED')->store;
951 ( $renewokay, $error ) =
952 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
953 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
954 is
( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
956 $account->add_credit(
958 amount
=> $fines_amount,
961 description
=> "Some payment"
964 ( $renewokay, $error ) =
965 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
966 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
967 is
( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
969 C4
::Context
->set_preference('OPACFineNoRenewalsIncludeCredit','0');
970 ( $renewokay, $error ) =
971 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
972 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
973 is
( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, OPACFineNoRenewalsIncludeCredit=1, patron has 15 debt, 5 credit' );
975 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
976 C4
::Context
->set_preference('OPACFineNoRenewalsIncludeCredit','1');
979 subtest
"auto_account_expired | BlockExpiredPatronOpacActions" => sub {
981 my $item_to_auto_renew = $builder->build_sample_item(
983 biblionumber
=> $biblio->biblionumber,
988 Koha
::CirculationRules
->set_rules(
990 categorycode
=> undef,
994 norenewalbefore
=> 10,
995 no_auto_renewal_after
=> 11,
1000 my $ten_days_before = dt_from_string
->add( days
=> -10 );
1001 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
1003 # Patron is expired and BlockExpiredPatronOpacActions=0
1004 # => auto renew is allowed
1005 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 0);
1006 my $patron = $expired_borrower;
1007 my $checkout = AddIssue
( $patron, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
1008 ( $renewokay, $error ) =
1009 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->itemnumber );
1010 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
1011 is
( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
1012 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
1015 # Patron is expired and BlockExpiredPatronOpacActions=1
1016 # => auto renew is not allowed
1017 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 1);
1018 $patron = $expired_borrower;
1019 $checkout = AddIssue
( $patron, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
1020 ( $renewokay, $error ) =
1021 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->itemnumber );
1022 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
1023 is
( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
1024 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
1027 # Patron is not expired and BlockExpiredPatronOpacActions=1
1028 # => auto renew is allowed
1029 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 1);
1030 $patron = $renewing_borrower;
1031 $checkout = AddIssue
( $patron, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
1032 ( $renewokay, $error ) =
1033 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->itemnumber );
1034 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
1035 is
( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
1036 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
1039 subtest
"GetLatestAutoRenewDate" => sub {
1041 my $item_to_auto_renew = $builder->build_sample_item(
1043 biblionumber
=> $biblio->biblionumber,
1048 my $ten_days_before = dt_from_string
->add( days
=> -10 );
1049 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
1050 AddIssue
( $renewing_borrower, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
1051 Koha
::CirculationRules
->set_rules(
1053 categorycode
=> undef,
1054 branchcode
=> undef,
1057 norenewalbefore
=> '7',
1058 no_auto_renewal_after
=> '',
1059 no_auto_renewal_after_hard_limit
=> undef,
1063 my $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
1064 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' );
1065 my $five_days_before = dt_from_string
->add( days
=> -5 );
1066 Koha
::CirculationRules
->set_rules(
1068 categorycode
=> undef,
1069 branchcode
=> undef,
1072 norenewalbefore
=> '10',
1073 no_auto_renewal_after
=> '5',
1074 no_auto_renewal_after_hard_limit
=> undef,
1078 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
1079 is
( $latest_auto_renew_date->truncate( to
=> 'minute' ),
1080 $five_days_before->truncate( to
=> 'minute' ),
1081 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
1083 my $five_days_ahead = dt_from_string
->add( days
=> 5 );
1084 $dbh->do(q{UPDATE circulation_rules SET rule_value = '10' WHERE rule_name = 'norenewalbefore'});
1085 $dbh->do(q{UPDATE circulation_rules SET rule_value = '15' WHERE rule_name = 'no_auto_renewal_after'});
1086 $dbh->do(q{UPDATE circulation_rules SET rule_value = NULL WHERE rule_name = 'no_auto_renewal_after_hard_limit'});
1087 Koha
::CirculationRules
->set_rules(
1089 categorycode
=> undef,
1090 branchcode
=> undef,
1093 norenewalbefore
=> '10',
1094 no_auto_renewal_after
=> '15',
1095 no_auto_renewal_after_hard_limit
=> undef,
1099 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
1100 is
( $latest_auto_renew_date->truncate( to
=> 'minute' ),
1101 $five_days_ahead->truncate( to
=> 'minute' ),
1102 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
1104 my $two_days_ahead = dt_from_string
->add( days
=> 2 );
1105 Koha
::CirculationRules
->set_rules(
1107 categorycode
=> undef,
1108 branchcode
=> undef,
1111 norenewalbefore
=> '10',
1112 no_auto_renewal_after
=> '',
1113 no_auto_renewal_after_hard_limit
=> dt_from_string
->add( days
=> 2 ),
1117 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
1118 is
( $latest_auto_renew_date->truncate( to
=> 'day' ),
1119 $two_days_ahead->truncate( to
=> 'day' ),
1120 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
1122 Koha
::CirculationRules
->set_rules(
1124 categorycode
=> undef,
1125 branchcode
=> undef,
1128 norenewalbefore
=> '10',
1129 no_auto_renewal_after
=> '15',
1130 no_auto_renewal_after_hard_limit
=> dt_from_string
->add( days
=> 2 ),
1134 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->itemnumber );
1135 is
( $latest_auto_renew_date->truncate( to
=> 'day' ),
1136 $two_days_ahead->truncate( to
=> 'day' ),
1137 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
1143 # set policy to forbid renewals
1144 Koha
::CirculationRules
->set_rules(
1146 categorycode
=> undef,
1147 branchcode
=> undef,
1150 norenewalbefore
=> undef,
1151 renewalsallowed
=> 0,
1156 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
1157 is
( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
1158 is
( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
1160 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
1161 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','1');
1162 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','1');
1164 C4
::Overdues
::UpdateFine
(
1166 issue_id
=> $issue->id(),
1167 itemnumber
=> $item_1->itemnumber,
1168 borrowernumber
=> $renewing_borrower->{borrowernumber
},
1171 due
=> Koha
::DateUtils
::output_pref
($datedue)
1175 my $line = Koha
::Account
::Lines
->search({ borrowernumber
=> $renewing_borrower->{borrowernumber
} })->next();
1176 is
( $line->debit_type_code, 'OVERDUE', 'Account line type is OVERDUE' );
1177 is
( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
1178 is
( $line->amountoutstanding+0, 15, 'Account line amount outstanding is 15.00' );
1179 is
( $line->amount+0, 15, 'Account line amount is 15.00' );
1180 is
( $line->issue_id, $issue->id, 'Account line issue id matches' );
1182 my $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $line->id })->next();
1183 is
( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
1184 is
( $offset->amount+0, 15, 'Account offset amount is 15.00' );
1186 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','0');
1187 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','0');
1189 LostItem
( $item_1->itemnumber, 'test', 1 );
1191 $line = Koha
::Account
::Lines
->find($line->id);
1192 is
( $line->debit_type_code, 'OVERDUE', 'Account type remains as OVERDUE' );
1193 isnt
( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
1195 my $item = Koha
::Items
->find($item_1->itemnumber);
1196 ok
( !$item->onloan(), "Lost item marked as returned has false onloan value" );
1197 my $checkout = Koha
::Checkouts
->find({ itemnumber
=> $item_1->itemnumber });
1198 is
( $checkout, undef, 'LostItem called with forced return has checked in the item' );
1200 my $total_due = $dbh->selectrow_array(
1201 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
1202 undef, $renewing_borrower->{borrowernumber
}
1205 is
( $total_due+0, 15, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
1207 C4
::Context
->dbh->do("DELETE FROM accountlines");
1209 C4
::Overdues
::UpdateFine
(
1211 issue_id
=> $issue2->id(),
1212 itemnumber
=> $item_2->itemnumber,
1213 borrowernumber
=> $renewing_borrower->{borrowernumber
},
1216 due
=> Koha
::DateUtils
::output_pref
($datedue)
1220 LostItem
( $item_2->itemnumber, 'test', 0 );
1222 my $item2 = Koha
::Items
->find($item_2->itemnumber);
1223 ok
( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
1224 ok
( Koha
::Checkouts
->find({ itemnumber
=> $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
1226 $total_due = $dbh->selectrow_array(
1227 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
1228 undef, $renewing_borrower->{borrowernumber
}
1231 ok
( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
1233 my $future = dt_from_string
();
1234 $future->add( days
=> 7 );
1235 my $units = C4
::Overdues
::get_chargeable_units
('days', $future, $now, $library2->{branchcode
});
1236 ok
( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
1238 # Users cannot renew any item if there is an overdue item
1239 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','block');
1240 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_6->itemnumber);
1241 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
1242 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_7->itemnumber);
1243 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
1245 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1246 t
::lib
::Mocks
::mock_userenv
({ patron
=> $manager,branchcode
=> $manager->branchcode });
1247 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','1');
1248 $checkout = Koha
::Checkouts
->find( { itemnumber
=> $item_3->itemnumber } );
1249 LostItem
( $item_3->itemnumber, 'test', 0 );
1250 my $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item_3->itemnumber } );
1251 is
( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
1253 $accountline->description,
1254 sprintf( "%s %s %s",
1255 $item_3->biblio->title || '',
1256 $item_3->barcode || '',
1257 $item_3->itemcallnumber || '' ),
1258 "Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
1262 subtest
"GetUpcomingDueIssues" => sub {
1265 my $branch = $library2->{branchcode
};
1267 #Create another record
1268 my $biblio2 = $builder->build_sample_biblio();
1271 my $item_1 = Koha
::Items
->find($reused_itemnumber_1);
1272 my $item_2 = Koha
::Items
->find($reused_itemnumber_2);
1273 my $item_3 = $builder->build_sample_item(
1275 biblionumber
=> $biblio2->biblionumber,
1283 my %a_borrower_data = (
1284 firstname
=> 'Fridolyn',
1285 surname
=> 'SOMERS',
1286 categorycode
=> $patron_category->{categorycode
},
1287 branchcode
=> $branch,
1290 my $a_borrower_borrowernumber = Koha
::Patron
->new(\
%a_borrower_data)->store->borrowernumber;
1291 my $a_borrower = Koha
::Patrons
->find( $a_borrower_borrowernumber )->unblessed;
1293 my $yesterday = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> -1 );
1294 my $two_days_ahead = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> 2 );
1295 my $today = DateTime
->today(time_zone
=> C4
::Context
->tz());
1297 my $issue = AddIssue
( $a_borrower, $item_1->barcode, $yesterday );
1298 my $datedue = dt_from_string
( $issue->date_due() );
1299 my $issue2 = AddIssue
( $a_borrower, $item_2->barcode, $two_days_ahead );
1300 my $datedue2 = dt_from_string
( $issue->date_due() );
1304 # GetUpcomingDueIssues tests
1306 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
1307 is
( scalar( @
$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
1310 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
1311 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
1312 is
( scalar ( @
$upcoming_dues), 1, "Only one item due in 2 days or less" );
1315 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
1316 is
( scalar( @
$upcoming_dues ), 1,
1317 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
1320 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
1322 my $issue3 = AddIssue
( $a_borrower, $item_3->barcode, $today );
1324 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> -1 } );
1325 is
( scalar ( @
$upcoming_dues), 0, "Overdues can not be selected" );
1327 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 0 } );
1328 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today" );
1330 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 1 } );
1331 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today, none tomorrow" );
1333 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
1334 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
1336 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 3 } );
1337 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
1339 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
();
1340 is
( scalar ( @
$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1344 subtest
"Bug 13841 - Do not create new 0 amount fines" => sub {
1345 my $branch = $library2->{branchcode
};
1347 my $biblio = $builder->build_sample_biblio();
1350 my $item = $builder->build_sample_item(
1352 biblionumber
=> $biblio->biblionumber,
1359 my %a_borrower_data = (
1360 firstname
=> 'Kyle',
1362 categorycode
=> $patron_category->{categorycode
},
1363 branchcode
=> $branch,
1366 my $borrowernumber = Koha
::Patron
->new(\
%a_borrower_data)->store->borrowernumber;
1368 my $borrower = Koha
::Patrons
->find( $borrowernumber )->unblessed;
1369 my $issue = AddIssue
( $borrower, $item->barcode );
1372 issue_id
=> $issue->id(),
1373 itemnumber
=> $item->itemnumber,
1374 borrowernumber
=> $borrowernumber,
1380 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1381 my $count = $hr->{count
};
1383 is
( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1386 subtest
"AllowRenewalIfOtherItemsAvailable tests" => sub {
1387 $dbh->do('DELETE FROM issues');
1388 $dbh->do('DELETE FROM items');
1389 $dbh->do('DELETE FROM circulation_rules');
1390 Koha
::CirculationRules
->set_rules(
1392 categorycode
=> undef,
1394 branchcode
=> undef,
1396 reservesallowed
=> 25,
1398 lengthunit
=> 'days',
1399 renewalsallowed
=> 1,
1401 norenewalbefore
=> undef,
1409 my $biblio = $builder->build_sample_biblio();
1411 my $item_1 = $builder->build_sample_item(
1413 biblionumber
=> $biblio->biblionumber,
1414 library
=> $library2->{branchcode
},
1419 my $item_2= $builder->build_sample_item(
1421 biblionumber
=> $biblio->biblionumber,
1422 library
=> $library2->{branchcode
},
1427 my $borrowernumber1 = Koha
::Patron
->new({
1428 firstname
=> 'Kyle',
1430 categorycode
=> $patron_category->{categorycode
},
1431 branchcode
=> $library2->{branchcode
},
1432 })->store->borrowernumber;
1433 my $borrowernumber2 = Koha
::Patron
->new({
1434 firstname
=> 'Chelsea',
1436 categorycode
=> $patron_category->{categorycode
},
1437 branchcode
=> $library2->{branchcode
},
1438 })->store->borrowernumber;
1440 my $borrower1 = Koha
::Patrons
->find( $borrowernumber1 )->unblessed;
1441 my $borrower2 = Koha
::Patrons
->find( $borrowernumber2 )->unblessed;
1443 my $issue = AddIssue
( $borrower1, $item_1->barcode );
1445 my ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1446 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1450 branchcode
=> $library2->{branchcode
},
1451 borrowernumber
=> $borrowernumber2,
1452 biblionumber
=> $biblio->biblionumber,
1457 Koha
::CirculationRules
->set_rules(
1459 categorycode
=> undef,
1461 branchcode
=> undef,
1467 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
1468 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1469 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1471 Koha
::CirculationRules
->set_rules(
1473 categorycode
=> undef,
1475 branchcode
=> undef,
1481 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
1482 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1483 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1485 Koha
::CirculationRules
->set_rules(
1487 categorycode
=> undef,
1489 branchcode
=> undef,
1495 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
1496 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1497 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1499 Koha
::CirculationRules
->set_rules(
1501 categorycode
=> undef,
1503 branchcode
=> undef,
1509 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
1510 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1511 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1513 # Setting item not checked out to be not for loan but holdable
1514 $item_2->notforloan(-1)->store;
1516 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1517 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' );
1521 # Don't allow renewing onsite checkout
1522 my $branch = $library->{branchcode
};
1524 #Create another record
1525 my $biblio = $builder->build_sample_biblio();
1527 my $item = $builder->build_sample_item(
1529 biblionumber
=> $biblio->biblionumber,
1535 my $borrowernumber = Koha
::Patron
->new({
1538 categorycode
=> $patron_category->{categorycode
},
1539 branchcode
=> $branch,
1540 })->store->borrowernumber;
1542 my $borrower = Koha
::Patrons
->find( $borrowernumber )->unblessed;
1544 my $issue = AddIssue
( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout
=> 1 } );
1545 my ( $renewed, $error ) = CanBookBeRenewed
( $borrowernumber, $item->itemnumber );
1546 is
( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1547 is
( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1551 my $library = $builder->build({ source
=> 'Branch' });
1553 my $biblio = $builder->build_sample_biblio();
1555 my $item = $builder->build_sample_item(
1557 biblionumber
=> $biblio->biblionumber,
1558 library
=> $library->{branchcode
},
1563 my $patron = $builder->build({ source
=> 'Borrower', value
=> { branchcode
=> $library->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
1565 my $issue = AddIssue
( $patron, $item->barcode );
1568 issue_id
=> $issue->id(),
1569 itemnumber
=> $item->itemnumber,
1570 borrowernumber
=> $patron->{borrowernumber
},
1577 issue_id
=> $issue->id(),
1578 itemnumber
=> $item->itemnumber,
1579 borrowernumber
=> $patron->{borrowernumber
},
1584 is
( Koha
::Account
::Lines
->search({ issue_id
=> $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1587 subtest
'CanBookBeIssued & AllowReturnToBranch' => sub {
1590 my $homebranch = $builder->build( { source
=> 'Branch' } );
1591 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
1592 my $otherbranch = $builder->build( { source
=> 'Branch' } );
1593 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1594 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1596 my $item = $builder->build_sample_item(
1598 homebranch
=> $homebranch->{branchcode
},
1599 holdingbranch
=> $holdingbranch->{branchcode
},
1603 set_userenv
($holdingbranch);
1605 my $issue = AddIssue
( $patron_1->unblessed, $item->barcode );
1606 is
( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
1608 my ( $error, $question, $alerts );
1610 # AllowReturnToBranch == anywhere
1611 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
1612 ## Test that unknown barcodes don't generate internal server errors
1613 set_userenv
($homebranch);
1614 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, 'KohaIsAwesome' );
1615 ok
( $error->{UNKNOWN_BARCODE
}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1616 ## Can be issued from homebranch
1617 set_userenv
($homebranch);
1618 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1619 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1620 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1621 ## Can be issued from holdingbranch
1622 set_userenv
($holdingbranch);
1623 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1624 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1625 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1626 ## Can be issued from another branch
1627 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1628 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1629 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1631 # AllowReturnToBranch == holdingbranch
1632 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
1633 ## Cannot be issued from homebranch
1634 set_userenv
($homebranch);
1635 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1636 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1637 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1638 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
}, 'branch_to_return matched holdingbranch' );
1639 ## Can be issued from holdinbranch
1640 set_userenv
($holdingbranch);
1641 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1642 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1643 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1644 ## Cannot be issued from another branch
1645 set_userenv
($otherbranch);
1646 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1647 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1648 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1649 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
}, 'branch_to_return matches holdingbranch' );
1651 # AllowReturnToBranch == homebranch
1652 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1653 ## Can be issued from holdinbranch
1654 set_userenv
($homebranch);
1655 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1656 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1657 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1658 ## Cannot be issued from holdinbranch
1659 set_userenv
($holdingbranch);
1660 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1661 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1662 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1663 is
( $error->{branch_to_return
}, $homebranch->{branchcode
}, 'branch_to_return matches homebranch' );
1664 ## Cannot be issued from holdinbranch
1665 set_userenv
($otherbranch);
1666 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->barcode );
1667 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1668 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1669 is
( $error->{branch_to_return
}, $homebranch->{branchcode
}, 'branch_to_return matches homebranch' );
1671 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1674 subtest
'AddIssue & AllowReturnToBranch' => sub {
1677 my $homebranch = $builder->build( { source
=> 'Branch' } );
1678 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
1679 my $otherbranch = $builder->build( { source
=> 'Branch' } );
1680 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1681 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1683 my $item = $builder->build_sample_item(
1685 homebranch
=> $homebranch->{branchcode
},
1686 holdingbranch
=> $holdingbranch->{branchcode
},
1690 set_userenv
($holdingbranch);
1692 my $ref_issue = 'Koha::Checkout';
1693 my $issue = AddIssue
( $patron_1, $item->barcode );
1695 my ( $error, $question, $alerts );
1697 # AllowReturnToBranch == homebranch
1698 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
1699 ## Can be issued from homebranch
1700 set_userenv
($homebranch);
1701 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from homebranch');
1702 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->barcode ); # Reinsert the original issue
1703 ## Can be issued from holdinbranch
1704 set_userenv
($holdingbranch);
1705 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from holdingbranch');
1706 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->barcode ); # Reinsert the original issue
1707 ## Can be issued from another branch
1708 set_userenv
($otherbranch);
1709 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - anywhere | Can be issued from otherbranch');
1710 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->barcode ); # Reinsert the original issue
1712 # AllowReturnToBranch == holdinbranch
1713 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
1714 ## Cannot be issued from homebranch
1715 set_userenv
($homebranch);
1716 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - holdingbranch | Cannot be issued from homebranch');
1717 ## Can be issued from holdingbranch
1718 set_userenv
($holdingbranch);
1719 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - holdingbranch | Can be issued from holdingbranch');
1720 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->barcode ); # Reinsert the original issue
1721 ## Cannot be issued from another branch
1722 set_userenv
($otherbranch);
1723 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - holdingbranch | Cannot be issued from otherbranch');
1725 # AllowReturnToBranch == homebranch
1726 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1727 ## Can be issued from homebranch
1728 set_userenv
($homebranch);
1729 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), $ref_issue, 'AllowReturnToBranch - homebranch | Can be issued from homebranch' );
1730 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->barcode ); # Reinsert the original issue
1731 ## Cannot be issued from holdinbranch
1732 set_userenv
($holdingbranch);
1733 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - homebranch | Cannot be issued from holdingbranch' );
1734 ## Cannot be issued from another branch
1735 set_userenv
($otherbranch);
1736 is
( ref( AddIssue
( $patron_2, $item->barcode ) ), '', 'AllowReturnToBranch - homebranch | Cannot be issued from otherbranch' );
1737 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1740 subtest
'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1743 my $library = $builder->build( { source
=> 'Branch' } );
1744 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1745 my $item_1 = $builder->build_sample_item(
1747 library
=> $library->{branchcode
},
1750 my $item_2 = $builder->build_sample_item(
1752 library
=> $library->{branchcode
},
1756 my ( $error, $question, $alerts );
1758 # Patron cannot issue item_1, they have overdues
1759 my $yesterday = DateTime
->today( time_zone
=> C4
::Context
->tz() )->add( days
=> -1 );
1760 my $issue = AddIssue
( $patron->unblessed, $item_1->barcode, $yesterday ); # Add an overdue
1762 t
::lib
::Mocks
::mock_preference
( 'OverduesBlockCirc', 'confirmation' );
1763 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1764 is
( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str
($error, $question, $alerts) );
1765 is
( $question->{USERBLOCKEDOVERDUE
}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1767 t
::lib
::Mocks
::mock_preference
( 'OverduesBlockCirc', 'block' );
1768 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1769 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1770 is
( $error->{USERBLOCKEDOVERDUE
}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1772 # Patron cannot issue item_1, they are debarred
1773 my $tomorrow = DateTime
->today( time_zone
=> C4
::Context
->tz() )->add( days
=> 1 );
1774 Koha
::Patron
::Debarments
::AddDebarment
( { borrowernumber
=> $patron->borrowernumber, expiration
=> $tomorrow } );
1775 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1776 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1777 is
( $error->{USERBLOCKEDWITHENDDATE
}, output_pref
( { dt
=> $tomorrow, dateformat
=> 'sql', dateonly
=> 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1779 Koha
::Patron
::Debarments
::AddDebarment
( { borrowernumber
=> $patron->borrowernumber } );
1780 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1781 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1782 is
( $error->{USERBLOCKEDNOENDDATE
}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1785 subtest
'CanBookBeIssued + Statistic patrons "X"' => sub {
1788 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1789 my $patron_category_x = $builder->build_object(
1791 class => 'Koha::Patron::Categories',
1792 value
=> { category_type
=> 'X' }
1795 my $patron = $builder->build_object(
1797 class => 'Koha::Patrons',
1799 categorycode
=> $patron_category_x->categorycode,
1800 gonenoaddress
=> undef,
1807 my $item_1 = $builder->build_sample_item(
1809 library
=> $library->{branchcode
},
1813 my ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_1->barcode );
1814 is
( $error->{STATS
}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1816 # TODO There are other tests to provide here
1819 subtest
'MultipleReserves' => sub {
1822 my $biblio = $builder->build_sample_biblio();
1824 my $branch = $library2->{branchcode
};
1826 my $item_1 = $builder->build_sample_item(
1828 biblionumber
=> $biblio->biblionumber,
1830 replacementprice
=> 12.00,
1835 my $item_2 = $builder->build_sample_item(
1837 biblionumber
=> $biblio->biblionumber,
1839 replacementprice
=> 12.00,
1846 my $resdate = undef;
1847 my $expdate = undef;
1849 my $checkitem = undef;
1852 my %renewing_borrower_data = (
1853 firstname
=> 'John',
1854 surname
=> 'Renewal',
1855 categorycode
=> $patron_category->{categorycode
},
1856 branchcode
=> $branch,
1858 my $renewing_borrowernumber = Koha
::Patron
->new(\
%renewing_borrower_data)->store->borrowernumber;
1859 my $renewing_borrower = Koha
::Patrons
->find( $renewing_borrowernumber )->unblessed;
1860 my $issue = AddIssue
( $renewing_borrower, $item_1->barcode);
1861 my $datedue = dt_from_string
( $issue->date_due() );
1862 is
(defined $issue->date_due(), 1, "item 1 checked out");
1863 my $borrowing_borrowernumber = Koha
::Checkouts
->find({ itemnumber
=> $item_1->itemnumber })->borrowernumber;
1865 my %reserving_borrower_data1 = (
1866 firstname
=> 'Katrin',
1867 surname
=> 'Reservation',
1868 categorycode
=> $patron_category->{categorycode
},
1869 branchcode
=> $branch,
1871 my $reserving_borrowernumber1 = Koha
::Patron
->new(\
%reserving_borrower_data1)->store->borrowernumber;
1874 branchcode
=> $branch,
1875 borrowernumber
=> $reserving_borrowernumber1,
1876 biblionumber
=> $biblio->biblionumber,
1877 priority
=> $priority,
1878 reservation_date
=> $resdate,
1879 expiration_date
=> $expdate,
1881 itemnumber
=> $checkitem,
1886 my %reserving_borrower_data2 = (
1887 firstname
=> 'Kirk',
1888 surname
=> 'Reservation',
1889 categorycode
=> $patron_category->{categorycode
},
1890 branchcode
=> $branch,
1892 my $reserving_borrowernumber2 = Koha
::Patron
->new(\
%reserving_borrower_data2)->store->borrowernumber;
1895 branchcode
=> $branch,
1896 borrowernumber
=> $reserving_borrowernumber2,
1897 biblionumber
=> $biblio->biblionumber,
1898 priority
=> $priority,
1899 reservation_date
=> $resdate,
1900 expiration_date
=> $expdate,
1902 itemnumber
=> $checkitem,
1908 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
1909 is
($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1912 my $item_3 = $builder->build_sample_item(
1914 biblionumber
=> $biblio->biblionumber,
1916 replacementprice
=> 12.00,
1922 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
1923 is
($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1927 subtest
'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1930 my $library = $builder->build( { source
=> 'Branch' } );
1931 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1933 my $biblionumber = $builder->build_sample_biblio(
1935 branchcode
=> $library->{branchcode
},
1938 my $item_1 = $builder->build_sample_item(
1940 biblionumber
=> $biblionumber,
1941 library
=> $library->{branchcode
},
1945 my $item_2 = $builder->build_sample_item(
1947 biblionumber
=> $biblionumber,
1948 library
=> $library->{branchcode
},
1952 my ( $error, $question, $alerts );
1953 my $issue = AddIssue
( $patron->unblessed, $item_1->barcode, dt_from_string
->add( days
=> 1 ) );
1955 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 0);
1956 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1958 { error
=> $error, alerts
=> $alerts },
1959 { error
=> {}, alerts
=> {} },
1960 'No error or alert should be raised'
1962 is
( $question->{BIBLIO_ALREADY_ISSUED
}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' );
1964 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 1);
1965 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1967 { error
=> $error, question
=> $question, alerts
=> $alerts },
1968 { error
=> {}, question
=> {}, alerts
=> {} },
1969 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1'
1972 # Add a subscription
1973 Koha
::Subscription
->new({ biblionumber
=> $biblionumber })->store;
1975 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 0);
1976 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1978 { error
=> $error, question
=> $question, alerts
=> $alerts },
1979 { error
=> {}, question
=> {}, alerts
=> {} },
1980 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription'
1983 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 1);
1984 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->barcode );
1986 { error
=> $error, question
=> $question, alerts
=> $alerts },
1987 { error
=> {}, question
=> {}, alerts
=> {} },
1988 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription'
1992 subtest
'AddReturn + CumulativeRestrictionPeriods' => sub {
1995 my $library = $builder->build( { source
=> 'Branch' } );
1996 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1999 my $biblionumber = $builder->build_sample_biblio(
2001 branchcode
=> $library->{branchcode
},
2004 my $item_1 = $builder->build_sample_item(
2006 biblionumber
=> $biblionumber,
2007 library
=> $library->{branchcode
},
2010 my $item_2 = $builder->build_sample_item(
2012 biblionumber
=> $biblionumber,
2013 library
=> $library->{branchcode
},
2017 # And the circulation rule
2018 Koha
::CirculationRules
->search->delete;
2019 Koha
::CirculationRules
->set_rules(
2021 categorycode
=> undef,
2023 branchcode
=> undef,
2026 firstremind
=> 1, # 1 day of grace
2027 finedays
=> 2, # 2 days of fine per day of overdue
2028 lengthunit
=> 'days',
2033 # Patron cannot issue item_1, they have overdues
2034 my $now = dt_from_string
;
2035 my $five_days_ago = $now->clone->subtract( days
=> 5 );
2036 my $ten_days_ago = $now->clone->subtract( days
=> 10 );
2037 AddIssue
( $patron, $item_1->barcode, $five_days_ago ); # Add an overdue
2038 AddIssue
( $patron, $item_2->barcode, $ten_days_ago )
2039 ; # Add another overdue
2041 t
::lib
::Mocks
::mock_preference
( 'CumulativeRestrictionPeriods', '0' );
2042 AddReturn
( $item_1->barcode, $library->{branchcode
}, undef, $now );
2043 my $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
2044 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2045 is
( scalar(@
$debarments), 1 );
2047 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
2048 # Same for the others
2049 my $expected_expiration = output_pref
(
2051 dt
=> $now->clone->add( days
=> ( 5 - 1 ) * 2 ),
2052 dateformat
=> 'sql',
2056 is
( $debarments->[0]->{expiration
}, $expected_expiration );
2058 AddReturn
( $item_2->barcode, $library->{branchcode
}, undef, $now );
2059 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
2060 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2061 is
( scalar(@
$debarments), 1 );
2062 $expected_expiration = output_pref
(
2064 dt
=> $now->clone->add( days
=> ( 10 - 1 ) * 2 ),
2065 dateformat
=> 'sql',
2069 is
( $debarments->[0]->{expiration
}, $expected_expiration );
2071 Koha
::Patron
::Debarments
::DelUniqueDebarment
(
2072 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2074 t
::lib
::Mocks
::mock_preference
( 'CumulativeRestrictionPeriods', '1' );
2075 AddIssue
( $patron, $item_1->barcode, $five_days_ago ); # Add an overdue
2076 AddIssue
( $patron, $item_2->barcode, $ten_days_ago )
2077 ; # Add another overdue
2078 AddReturn
( $item_1->barcode, $library->{branchcode
}, undef, $now );
2079 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
2080 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2081 is
( scalar(@
$debarments), 1 );
2082 $expected_expiration = output_pref
(
2084 dt
=> $now->clone->add( days
=> ( 5 - 1 ) * 2 ),
2085 dateformat
=> 'sql',
2089 is
( $debarments->[0]->{expiration
}, $expected_expiration );
2091 AddReturn
( $item_2->barcode, $library->{branchcode
}, undef, $now );
2092 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
2093 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2094 is
( scalar(@
$debarments), 1 );
2095 $expected_expiration = output_pref
(
2097 dt
=> $now->clone->add( days
=> ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
2098 dateformat
=> 'sql',
2102 is
( $debarments->[0]->{expiration
}, $expected_expiration );
2105 subtest
'AddReturn + suspension_chargeperiod' => sub {
2108 my $library = $builder->build( { source
=> 'Branch' } );
2109 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
2111 my $biblionumber = $builder->build_sample_biblio(
2113 branchcode
=> $library->{branchcode
},
2116 my $item_1 = $builder->build_sample_item(
2118 biblionumber
=> $biblionumber,
2119 library
=> $library->{branchcode
},
2123 # And the issuing rule
2124 Koha
::CirculationRules
->search->delete;
2125 Koha
::CirculationRules
->set_rules(
2127 categorycode
=> '*',
2132 firstremind
=> 0, # 0 day of grace
2133 finedays
=> 2, # 2 days of fine per day of overdue
2134 suspension_chargeperiod
=> 1,
2135 lengthunit
=> 'days',
2140 my $now = dt_from_string
;
2141 my $five_days_ago = $now->clone->subtract( days
=> 5 );
2142 # We want to charge 2 days every day, without grace
2143 # With 5 days of overdue: 5 * Z
2144 my $expected_expiration = $now->clone->add( days
=> ( 5 * 2 ) / 1 );
2145 test_debarment_on_checkout
(
2148 library
=> $library,
2150 due_date
=> $five_days_ago,
2151 expiration_date
=> $expected_expiration,
2155 # We want to charge 2 days every 2 days, without grace
2156 # With 5 days of overdue: (5 * 2) / 2
2157 Koha
::CirculationRules
->set_rule(
2159 categorycode
=> undef,
2160 branchcode
=> undef,
2162 rule_name
=> 'suspension_chargeperiod',
2167 $expected_expiration = $now->clone->add( days
=> floor
( 5 * 2 ) / 2 );
2168 test_debarment_on_checkout
(
2171 library
=> $library,
2173 due_date
=> $five_days_ago,
2174 expiration_date
=> $expected_expiration,
2178 # We want to charge 2 days every 3 days, with 1 day of grace
2179 # With 5 days of overdue: ((5-1) / 3 ) * 2
2180 Koha
::CirculationRules
->set_rules(
2182 categorycode
=> undef,
2183 branchcode
=> undef,
2186 suspension_chargeperiod
=> 3,
2191 $expected_expiration = $now->clone->add( days
=> floor
( ( ( 5 - 1 ) / 3 ) * 2 ) );
2192 test_debarment_on_checkout
(
2195 library
=> $library,
2197 due_date
=> $five_days_ago,
2198 expiration_date
=> $expected_expiration,
2202 # Use finesCalendar to know if holiday must be skipped to calculate the due date
2203 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
2204 Koha
::CirculationRules
->set_rules(
2206 categorycode
=> undef,
2207 branchcode
=> undef,
2211 suspension_chargeperiod
=> 1,
2216 t
::lib
::Mocks
::mock_preference
('finesCalendar', 'noFinesWhenClosed');
2217 t
::lib
::Mocks
::mock_preference
('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2219 # Adding a holiday 2 days ago
2220 my $calendar = C4
::Calendar
->new(branchcode
=> $library->{branchcode
});
2221 my $two_days_ago = $now->clone->subtract( days
=> 2 );
2222 $calendar->insert_single_holiday(
2223 day
=> $two_days_ago->day,
2224 month
=> $two_days_ago->month,
2225 year
=> $two_days_ago->year,
2226 title
=> 'holidayTest-2d',
2227 description
=> 'holidayDesc 2 days ago'
2229 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2230 $expected_expiration = $now->clone->add( days
=> floor
( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2231 test_debarment_on_checkout
(
2234 library
=> $library,
2236 due_date
=> $five_days_ago,
2237 expiration_date
=> $expected_expiration,
2241 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2242 my $two_days_ahead = $now->clone->add( days
=> 2 );
2243 $calendar->insert_single_holiday(
2244 day
=> $two_days_ahead->day,
2245 month
=> $two_days_ahead->month,
2246 year
=> $two_days_ahead->year,
2247 title
=> 'holidayTest+2d',
2248 description
=> 'holidayDesc 2 days ahead'
2251 # Same as above, but we should skip D+2
2252 $expected_expiration = $now->clone->add( days
=> floor
( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
2253 test_debarment_on_checkout
(
2256 library
=> $library,
2258 due_date
=> $five_days_ago,
2259 expiration_date
=> $expected_expiration,
2263 # Adding another holiday, day of expiration date
2264 my $expected_expiration_dt = dt_from_string
($expected_expiration);
2265 $calendar->insert_single_holiday(
2266 day
=> $expected_expiration_dt->day,
2267 month
=> $expected_expiration_dt->month,
2268 year
=> $expected_expiration_dt->year,
2269 title
=> 'holidayTest_exp',
2270 description
=> 'holidayDesc on expiration date'
2272 # Expiration date will be the day after
2273 test_debarment_on_checkout
(
2276 library
=> $library,
2278 due_date
=> $five_days_ago,
2279 expiration_date
=> $expected_expiration_dt->clone->add( days
=> 1 ),
2283 test_debarment_on_checkout
(
2286 library
=> $library,
2288 return_date
=> $now->clone->add(days
=> 5),
2289 expiration_date
=> $now->clone->add(days
=> 5 + (5 * 2 - 1) ),
2293 test_debarment_on_checkout
(
2296 library
=> $library,
2298 due_date
=> $now->clone->add(days
=> 1),
2299 return_date
=> $now->clone->add(days
=> 5),
2300 expiration_date
=> $now->clone->add(days
=> 5 + (4 * 2 - 1) ),
2306 subtest
'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
2309 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2310 my $patron1 = $builder->build_object(
2312 class => 'Koha::Patrons',
2314 library
=> $library->branchcode,
2315 categorycode
=> $patron_category->{categorycode
}
2319 my $patron2 = $builder->build_object(
2321 class => 'Koha::Patrons',
2323 library
=> $library->branchcode,
2324 categorycode
=> $patron_category->{categorycode
}
2329 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library->branchcode });
2331 my $item = $builder->build_sample_item(
2333 library
=> $library->branchcode,
2337 my ( $error, $question, $alerts );
2338 my $issue = AddIssue
( $patron1->unblessed, $item->barcode );
2340 t
::lib
::Mocks
::mock_preference
('AutoReturnCheckedOutItems', 0);
2341 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron2, $item->barcode );
2342 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' );
2344 t
::lib
::Mocks
::mock_preference
('AutoReturnCheckedOutItems', 1);
2345 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron2, $item->barcode );
2346 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' );
2348 t
::lib
::Mocks
::mock_preference
('AutoReturnCheckedOutItems', 0);
2352 subtest
'AddReturn | is_overdue' => sub {
2355 t
::lib
::Mocks
::mock_preference
('MarkLostItemsAsReturned', 'batchmod|moredetail|cronjob|additem|pendingreserves|onpayment');
2356 t
::lib
::Mocks
::mock_preference
('CalculateFinesOnReturn', 1);
2357 t
::lib
::Mocks
::mock_preference
('finesMode', 'production');
2358 t
::lib
::Mocks
::mock_preference
('MaxFine', '100');
2360 my $library = $builder->build( { source
=> 'Branch' } );
2361 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
2362 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2363 t
::lib
::Mocks
::mock_userenv
({ patron
=> $manager, branchcode
=> $manager->branchcode });
2365 my $item = $builder->build_sample_item(
2367 library
=> $library->{branchcode
},
2368 replacementprice
=> 7
2372 Koha
::CirculationRules
->search->delete;
2373 Koha
::CirculationRules
->set_rules(
2375 categorycode
=> undef,
2377 branchcode
=> undef,
2380 lengthunit
=> 'days',
2381 fine
=> 1, # Charge 1 every day of overdue
2387 my $now = dt_from_string
;
2388 my $one_day_ago = $now->clone->subtract( days
=> 1 );
2389 my $two_days_ago = $now->clone->subtract( days
=> 2 );
2390 my $five_days_ago = $now->clone->subtract( days
=> 5 );
2391 my $ten_days_ago = $now->clone->subtract( days
=> 10 );
2392 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
2394 # No return date specified, today will be used => 10 days overdue charged
2395 AddIssue
( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2396 AddReturn
( $item->barcode, $library->{branchcode
} );
2397 is
( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
2398 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2400 # specify return date 5 days before => no overdue charged
2401 AddIssue
( $patron->unblessed, $item->barcode, $five_days_ago ); # date due was 5d ago
2402 AddReturn
( $item->barcode, $library->{branchcode
}, undef, $ten_days_ago );
2403 is
( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2404 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2406 # specify return date 5 days later => 5 days overdue charged
2407 AddIssue
( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2408 AddReturn
( $item->barcode, $library->{branchcode
}, undef, $five_days_ago );
2409 is
( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
2410 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2412 # specify return date 5 days later, specify exemptfine => no overdue charge
2413 AddIssue
( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2414 AddReturn
( $item->barcode, $library->{branchcode
}, 1, $five_days_ago );
2415 is
( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2416 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2418 subtest
'bug 22877' => sub {
2422 my $issue = AddIssue
( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2424 # Fake fines cronjob on this checkout
2426 CalcFine
( $item, $patron->categorycode, $library->{branchcode
},
2427 $ten_days_ago, $now );
2430 issue_id
=> $issue->issue_id,
2431 itemnumber
=> $item->itemnumber,
2432 borrowernumber
=> $patron->borrowernumber,
2434 due
=> output_pref
($ten_days_ago)
2437 is
( int( $patron->account->balance() ),
2438 10, "Overdue fine of 10 days overdue" );
2440 # Fake longoverdue with charge and not marking returned
2441 LostItem
( $item->itemnumber, 'cronjob', 0 );
2442 is
( int( $patron->account->balance() ),
2443 17, "Lost fine of 7 plus 10 days overdue" );
2445 # Now we return it today
2446 AddReturn
( $item->barcode, $library->{branchcode
} );
2447 is
( int( $patron->account->balance() ),
2448 17, "Should have a single 10 days overdue fine and lost charge" );
2451 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2454 subtest
'bug 8338 | backdated return resulting in zero amount fine' => sub {
2458 t
::lib
::Mocks
::mock_preference
('CalculateFinesOnBackdate', 1);
2460 my $issue = AddIssue
( $patron->unblessed, $item->barcode, $one_day_ago ); # date due was 1d ago
2462 # Fake fines cronjob on this checkout
2464 CalcFine
( $item, $patron->categorycode, $library->{branchcode
},
2465 $one_day_ago, $now );
2468 issue_id
=> $issue->issue_id,
2469 itemnumber
=> $item->itemnumber,
2470 borrowernumber
=> $patron->borrowernumber,
2472 due
=> output_pref
($one_day_ago)
2475 is
( int( $patron->account->balance() ),
2476 1, "Overdue fine of 1 day overdue" );
2478 # Backdated return (dropbox mode example - charge should be removed)
2479 AddReturn
( $item->barcode, $library->{branchcode
}, 1, $one_day_ago );
2480 is
( int( $patron->account->balance() ),
2481 0, "Overdue fine should be annulled" );
2482 my $lines = Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber });
2483 is
( $lines->count, 0, "Overdue fine accountline has been removed");
2485 $issue = AddIssue
( $patron->unblessed, $item->barcode, $two_days_ago ); # date due was 2d ago
2487 # Fake fines cronjob on this checkout
2489 CalcFine
( $item, $patron->categorycode, $library->{branchcode
},
2490 $two_days_ago, $now );
2493 issue_id
=> $issue->issue_id,
2494 itemnumber
=> $item->itemnumber,
2495 borrowernumber
=> $patron->borrowernumber,
2497 due
=> output_pref
($one_day_ago)
2500 is
( int( $patron->account->balance() ),
2501 2, "Overdue fine of 2 days overdue" );
2503 # Payment made against fine
2504 $lines = Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber });
2505 my $debit = $lines->next;
2506 my $credit = $patron->account->add_credit(
2510 interface
=> 'test',
2514 { debits
=> [ $debit ], offset_type
=> 'Payment' } );
2516 is
( int( $patron->account->balance() ),
2517 0, "Overdue fine should be paid off" );
2518 $lines = Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber });
2519 is
( $lines->count, 2, "Overdue (debit) and Payment (credit) present");
2520 my $line = $lines->next;
2521 is
( $line->amount+0, 2, "Overdue fine amount remains as 2 days");
2522 is
( $line->amountoutstanding+0, 0, "Overdue fine amountoutstanding reduced to 0");
2524 # Backdated return (dropbox mode example - charge should be removed)
2525 AddReturn
( $item->barcode, $library->{branchcode
}, undef, $one_day_ago );
2526 is
( int( $patron->account->balance() ),
2527 -1, "Refund credit has been applied" );
2528 $lines = Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber }, { order_by
=> { '-asc' => 'accountlines_id' }});
2529 is
( $lines->count, 3, "Overdue (debit), Payment (credit) and Refund (credit) are all present");
2531 $line = $lines->next;
2532 is
($line->amount+0,1, "Overdue fine amount has been reduced to 1");
2533 is
($line->amountoutstanding+0,0, "Overdue fine amount outstanding remains at 0");
2534 is
($line->status,'RETURNED', "Overdue fine is fixed");
2535 $line = $lines->next;
2536 is
($line->amount+0,-2, "Original payment amount remains as 2");
2537 is
($line->amountoutstanding+0,0, "Original payment remains applied");
2538 $line = $lines->next;
2539 is
($line->amount+0,-1, "Refund amount correctly set to 1");
2540 is
($line->amountoutstanding+0,-1, "Refund amount outstanding unspent");
2543 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2546 subtest
'bug 25417 | backdated return + exemptfine' => sub {
2550 t
::lib
::Mocks
::mock_preference
('CalculateFinesOnBackdate', 1);
2552 my $issue = AddIssue
( $patron->unblessed, $item->barcode, $one_day_ago ); # date due was 1d ago
2554 # Fake fines cronjob on this checkout
2556 CalcFine
( $item, $patron->categorycode, $library->{branchcode
},
2557 $one_day_ago, $now );
2560 issue_id
=> $issue->issue_id,
2561 itemnumber
=> $item->itemnumber,
2562 borrowernumber
=> $patron->borrowernumber,
2564 due
=> output_pref
($one_day_ago)
2567 is
( int( $patron->account->balance() ),
2568 1, "Overdue fine of 1 day overdue" );
2570 # Backdated return (dropbox mode example - charge should no longer exist)
2571 AddReturn
( $item->barcode, $library->{branchcode
}, 1, $one_day_ago );
2572 is
( int( $patron->account->balance() ),
2573 0, "Overdue fine should be annulled" );
2576 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
2579 subtest
'bug 24075 | backdated return with return datetime matching due datetime' => sub {
2582 t
::lib
::Mocks
::mock_preference
( 'CalculateFinesOnBackdate', 1 );
2584 my $due_date = dt_from_string
;
2585 my $issue = AddIssue
( $patron->unblessed, $item->barcode, $due_date );
2590 issue_id
=> $issue->issue_id,
2591 itemnumber
=> $item->itemnumber,
2592 borrowernumber
=> $patron->borrowernumber,
2594 due
=> output_pref
($due_date)
2597 is
( $patron->account->balance(),
2598 0.25, 'Overdue fine of $0.25 recorded' );
2600 # Backdate return to exact due date and time
2601 my ( undef, $message ) =
2602 AddReturn
( $item->barcode, $library->{branchcode
},
2606 Koha
::Account
::Lines
->find( { issue_id
=> $issue->id } );
2607 ok
( !$accountline, 'accountline removed as expected' );
2610 $issue = AddIssue
( $patron->unblessed, $item->barcode, $due_date );
2615 issue_id
=> $issue->issue_id,
2616 itemnumber
=> $item->itemnumber,
2617 borrowernumber
=> $patron->borrowernumber,
2619 due
=> output_pref
($due_date)
2622 is
( $patron->account->balance(),
2623 0.25, 'Overdue fine of $0.25 recorded' );
2625 # Partial pay accruing fine
2626 my $lines = Koha
::Account
::Lines
->search(
2628 borrowernumber
=> $patron->borrowernumber,
2629 issue_id
=> $issue->id
2632 my $debit = $lines->next;
2633 my $credit = $patron->account->add_credit(
2637 interface
=> 'test',
2640 $credit->apply( { debits
=> [$debit], offset_type
=> 'Payment' } );
2642 is
( $patron->account->balance(), .05, 'Overdue fine reduced to $0.05' );
2644 # Backdate return to exact due date and time
2645 ( undef, $message ) =
2646 AddReturn
( $item->barcode, $library->{branchcode
},
2649 $lines = Koha
::Account
::Lines
->search(
2651 borrowernumber
=> $patron->borrowernumber,
2652 issue_id
=> $issue->id
2655 $accountline = $lines->next;
2656 is
( $accountline->amountoutstanding + 0,
2657 0, 'Partially paid fee amount outstanding was reduced to 0' );
2658 is
( $accountline->amount + 0,
2659 0, 'Partially paid fee amount was reduced to 0' );
2660 is
( $patron->account->balance(), -0.20, 'Patron refund recorded' );
2663 Koha
::Account
::Lines
->search(
2664 { borrowernumber
=> $patron->borrowernumber } )->delete;
2668 subtest
'_FixOverduesOnReturn' => sub {
2671 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2672 t
::lib
::Mocks
::mock_userenv
({ patron
=> $manager, branchcode
=> $manager->branchcode });
2674 my $biblio = $builder->build_sample_biblio({ author
=> 'Hall, Kylie' });
2676 my $branchcode = $library2->{branchcode
};
2678 my $item = $builder->build_sample_item(
2680 biblionumber
=> $biblio->biblionumber,
2681 library
=> $branchcode,
2682 replacementprice
=> 99.00,
2687 my $patron = $builder->build( { source
=> 'Borrower' } );
2689 ## Start with basic call, should just close out the open fine
2690 my $accountline = Koha
::Account
::Line
->new(
2692 borrowernumber
=> $patron->{borrowernumber
},
2693 debit_type_code
=> 'OVERDUE',
2694 status
=> 'UNRETURNED',
2695 itemnumber
=> $item->itemnumber,
2697 amountoutstanding
=> 99.00,
2698 interface
=> 'test',
2702 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber, undef, 'RETURNED' );
2704 $accountline->_result()->discard_changes();
2706 is
( $accountline->amountoutstanding+0, 99, 'Fine has the same amount outstanding as previously' );
2707 isnt
( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2708 is
( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
2710 ## Run again, with exemptfine enabled
2713 debit_type_code
=> 'OVERDUE',
2714 status
=> 'UNRETURNED',
2715 amountoutstanding
=> 99.00,
2719 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber, 1, 'RETURNED' );
2721 $accountline->_result()->discard_changes();
2722 my $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $accountline->id, type
=> 'Forgiven' })->next();
2724 is
( $accountline->amountoutstanding + 0, 0, 'Fine amountoutstanding has been reduced to 0' );
2725 isnt
( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2726 is
( $accountline->status, 'FORGIVEN', 'Open fine ( account type OVERDUE ) has been set to fine forgiven ( status FORGIVEN )');
2727 is
( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2728 is
( $offset->amount + 0, -99, "Amount of offset is correct" );
2729 my $credit = $offset->credit;
2730 is
( ref $credit, "Koha::Account::Line", "Found matching credit for fine forgiveness" );
2731 is
( $credit->amount + 0, -99, "Credit amount is set correctly" );
2732 is
( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
2734 # Bug 25417 - Only forgive fines where there is an amount outstanding to forgive
2737 debit_type_code
=> 'OVERDUE',
2738 status
=> 'UNRETURNED',
2739 amountoutstanding
=> 0.00,
2744 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber, 1, 'RETURNED' );
2746 $accountline->_result()->discard_changes();
2747 $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $accountline->id, type
=> 'Forgiven' })->next();
2748 is
( $offset, undef, "No offset created when trying to forgive fine with no outstanding balance" );
2749 isnt
( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2750 is
( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
2753 subtest
'Set waiting flag' => sub {
2756 my $library_1 = $builder->build( { source
=> 'Branch' } );
2757 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_1->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2758 my $library_2 = $builder->build( { source
=> 'Branch' } );
2759 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_2->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2761 my $item = $builder->build_sample_item(
2763 library
=> $library_1->{branchcode
},
2767 set_userenv
( $library_2 );
2768 my $reserve_id = AddReserve
(
2770 branchcode
=> $library_2->{branchcode
},
2771 borrowernumber
=> $patron_2->{borrowernumber
},
2772 biblionumber
=> $item->biblionumber,
2774 itemnumber
=> $item->itemnumber,
2778 set_userenv
( $library_1 );
2779 my $do_transfer = 1;
2780 my ( $res, $rr ) = AddReturn
( $item->barcode, $library_1->{branchcode
} );
2781 ModReserveAffect
( $item->itemnumber, undef, $do_transfer, $reserve_id );
2782 my $hold = Koha
::Holds
->find( $reserve_id );
2783 is
( $hold->found, 'T', 'Hold is in transit' );
2785 my ( $status ) = CheckReserves
($item->itemnumber);
2786 is
( $status, 'Reserved', 'Hold is not waiting yet');
2788 set_userenv
( $library_2 );
2790 AddReturn
( $item->barcode, $library_2->{branchcode
} );
2791 ModReserveAffect
( $item->itemnumber, undef, $do_transfer, $reserve_id );
2792 $hold = Koha
::Holds
->find( $reserve_id );
2793 is
( $hold->found, 'W', 'Hold is waiting' );
2794 ( $status ) = CheckReserves
($item->itemnumber);
2795 is
( $status, 'Waiting', 'Now the hold is waiting');
2797 #Bug 21944 - Waiting transfer checked in at branch other than pickup location
2798 set_userenv
( $library_1 );
2799 (undef, my $messages, undef, undef ) = AddReturn
( $item->barcode, $library_1->{branchcode
} );
2800 $hold = Koha
::Holds
->find( $reserve_id );
2801 is
( $hold->found, undef, 'Hold is no longer marked waiting' );
2802 is
( $hold->priority, 1, "Hold is now priority one again");
2803 is
( $hold->waitingdate, undef, "Hold no longer has a waiting date");
2804 is
( $hold->itemnumber, $item->itemnumber, "Hold has retained its' itemnumber");
2805 is
( $messages->{ResFound
}->{ResFound
}, "Reserved", "Hold is still returned");
2806 is
( $messages->{ResFound
}->{found
}, undef, "Hold is no longer marked found in return message");
2807 is
( $messages->{ResFound
}->{priority
}, 1, "Hold is priority 1 in return message");
2810 subtest
'Cancel transfers on lost items' => sub {
2812 my $library_1 = $builder->build( { source
=> 'Branch' } );
2813 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_1->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2814 my $library_2 = $builder->build( { source
=> 'Branch' } );
2815 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_2->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2816 my $biblio = $builder->build_sample_biblio({branchcode
=> $library->{branchcode
}});
2817 my $item = $builder->build_sample_item({
2818 biblionumber
=> $biblio->biblionumber,
2819 library
=> $library_1->{branchcode
},
2822 set_userenv
( $library_2 );
2823 my $reserve_id = AddReserve
(
2825 branchcode
=> $library_2->{branchcode
},
2826 borrowernumber
=> $patron_2->{borrowernumber
},
2827 biblionumber
=> $item->biblionumber,
2829 itemnumber
=> $item->itemnumber,
2833 #Return book and add transfer
2834 set_userenv
( $library_1 );
2835 my $do_transfer = 1;
2836 my ( $res, $rr ) = AddReturn
( $item->barcode, $library_1->{branchcode
} );
2837 ModReserveAffect
( $item->itemnumber, undef, $do_transfer, $reserve_id );
2838 C4
::Circulation
::transferbook
({
2839 from_branch
=> $library_1->{branchcode
},
2840 to_branch
=> $library_2->{branchcode
},
2841 barcode
=> $item->barcode,
2843 my $hold = Koha
::Holds
->find( $reserve_id );
2844 is
( $hold->found, 'T', 'Hold is in transit' );
2846 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2847 my ($datesent,$frombranch,$tobranch) = GetTransfers
($item->itemnumber);
2848 is
( $frombranch, $library_1->{branchcode
}, 'The transfer is generated from the correct library');
2849 is
( $tobranch, $library_2->{branchcode
}, 'The transfer is generated to the correct library');
2850 my $itemcheck = Koha
::Items
->find($item->itemnumber);
2851 is
( $itemcheck->holdingbranch, $library_1->{branchcode
}, 'Items holding branch is the transfers origination branch before it is marked as lost' );
2853 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2854 $item->itemlost(1)->store;
2855 LostItem
( $item->itemnumber, 'test', 1 );
2856 ($datesent,$frombranch,$tobranch) = GetTransfers
($item->itemnumber);
2857 is
( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2858 $itemcheck = Koha
::Items
->find($item->itemnumber);
2859 is
( $itemcheck->holdingbranch, $library_1->{branchcode
}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2863 subtest
'CanBookBeIssued | is_overdue' => sub {
2866 # Set a simple circ policy
2867 Koha
::CirculationRules
->set_rules(
2869 categorycode
=> undef,
2870 branchcode
=> undef,
2874 reservesallowed
=> 25,
2876 lengthunit
=> 'days',
2877 renewalsallowed
=> 1,
2879 norenewalbefore
=> undef,
2887 my $now = dt_from_string
;
2888 my $five_days_go = output_pref
({ dt
=> $now->clone->add( days
=> 5 ), dateonly
=> 1});
2889 my $ten_days_go = output_pref
({ dt
=> $now->clone->add( days
=> 10), dateonly
=> 1 });
2890 my $library = $builder->build( { source
=> 'Branch' } );
2891 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
2893 my $item = $builder->build_sample_item(
2895 library
=> $library->{branchcode
},
2899 my $issue = AddIssue
( $patron->unblessed, $item->barcode, $five_days_go ); # date due was 10d ago
2900 my $actualissue = Koha
::Checkouts
->find( { itemnumber
=> $item->itemnumber } );
2901 is
( output_pref
({ str
=> $actualissue->date_due, dateonly
=> 1}), $five_days_go, "First issue works");
2902 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued
($patron,$item->barcode,$ten_days_go, undef, undef, undef);
2903 is
( $needsconfirmation->{RENEW_ISSUE
}, 1, "This is a renewal");
2904 is
( $needsconfirmation->{TOO_MANY
}, undef, "Not too many, is a renewal");
2907 subtest
'ItemsDeniedRenewal preference' => sub {
2910 C4
::Context
->set_preference('ItemsDeniedRenewal','');
2912 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2913 Koha
::CirculationRules
->set_rules(
2915 categorycode
=> '*',
2917 branchcode
=> $idr_lib->branchcode,
2919 reservesallowed
=> 25,
2921 lengthunit
=> 'days',
2922 renewalsallowed
=> 10,
2924 norenewalbefore
=> undef,
2932 my $deny_book = $builder->build_object({ class => 'Koha::Items', value
=> {
2933 homebranch
=> $idr_lib->branchcode,
2937 itemcallnumber
=> undef,
2941 my $allow_book = $builder->build_object({ class => 'Koha::Items', value
=> {
2942 homebranch
=> $idr_lib->branchcode,
2945 location
=> 'NOPROC'
2949 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value
=> {
2950 branchcode
=> $idr_lib->branchcode,
2953 my $future = dt_from_string
->add( days
=> 1 );
2954 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value
=> {
2955 returndate
=> undef,
2958 borrowernumber
=> $idr_borrower->borrowernumber,
2959 itemnumber
=> $deny_book->itemnumber,
2960 onsite_checkout
=> 0,
2961 date_due
=> $future,
2964 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value
=> {
2965 returndate
=> undef,
2968 borrowernumber
=> $idr_borrower->borrowernumber,
2969 itemnumber
=> $allow_book->itemnumber,
2970 onsite_checkout
=> 0,
2971 date_due
=> $future,
2977 my ( $idr_mayrenew, $idr_error ) =
2978 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2979 is
( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2980 is
( $idr_error, undef, 'Renewal allowed when no rules' );
2982 $idr_rules="withdrawn: [1]";
2984 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2985 ( $idr_mayrenew, $idr_error ) =
2986 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2987 is
( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2988 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2989 ( $idr_mayrenew, $idr_error ) =
2990 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2991 is
( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2992 is
( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2994 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2996 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2997 ( $idr_mayrenew, $idr_error ) =
2998 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2999 is
( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
3000 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
3001 ( $idr_mayrenew, $idr_error ) =
3002 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
3003 is
( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
3004 is
( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
3006 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
3008 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
3009 ( $idr_mayrenew, $idr_error ) =
3010 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3011 is
( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
3012 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
3013 ( $idr_mayrenew, $idr_error ) =
3014 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
3015 is
( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
3016 is
( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
3018 $idr_rules="itemcallnumber: [NULL]";
3019 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
3020 ( $idr_mayrenew, $idr_error ) =
3021 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3022 is
( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
3023 $idr_rules="itemcallnumber: ['']";
3024 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
3025 ( $idr_mayrenew, $idr_error ) =
3026 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3027 is
( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
3029 $idr_rules="itemnotes: [NULL]";
3030 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
3031 ( $idr_mayrenew, $idr_error ) =
3032 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3033 is
( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
3034 $idr_rules="itemnotes: ['']";
3035 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
3036 ( $idr_mayrenew, $idr_error ) =
3037 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3038 is
( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
3041 subtest
'CanBookBeIssued | item-level_itypes=biblio' => sub {
3044 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0); # biblio
3045 my $library = $builder->build( { source
=> 'Branch' } );
3046 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } )->store;
3048 my $item = $builder->build_sample_item(
3050 library
=> $library->{branchcode
},
3054 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3055 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
3056 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
3059 subtest
'CanBookBeIssued | notforloan' => sub {
3062 t
::lib
::Mocks
::mock_preference
('AllowNotForLoanOverride', 0);
3064 my $library = $builder->build( { source
=> 'Branch' } );
3065 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } )->store;
3067 my $itemtype = $builder->build(
3069 source
=> 'Itemtype',
3070 value
=> { notforloan
=> undef, }
3073 my $item = $builder->build_sample_item(
3075 library
=> $library->{branchcode
},
3076 itype
=> $itemtype->{itemtype
},
3079 $item->biblioitem->itemtype($itemtype->{itemtype
})->store;
3081 my ( $issuingimpossible, $needsconfirmation );
3084 subtest
'item-level_itypes = 1' => sub {
3087 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1); # item
3088 # Is for loan at item type and item level
3089 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3090 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
3091 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
3093 # not for loan at item type level
3094 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(1)->store;
3095 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3096 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3099 { NOT_FOR_LOAN
=> 1, itemtype_notforloan
=> $itemtype->{itemtype
} },
3100 'Item can not be issued, not for loan at item type level'
3103 # not for loan at item level
3104 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(undef)->store;
3105 $item->notforloan( 1 )->store;
3106 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3107 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3110 { NOT_FOR_LOAN
=> 1, item_notforloan
=> 1 },
3111 'Item can not be issued, not for loan at item type level'
3115 subtest
'item-level_itypes = 0' => sub {
3118 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0); # biblio
3120 # We set another itemtype for biblioitem
3121 my $itemtype = $builder->build(
3123 source
=> 'Itemtype',
3124 value
=> { notforloan
=> undef, }
3128 # for loan at item type and item level
3129 $item->notforloan(0)->store;
3130 $item->biblioitem->itemtype($itemtype->{itemtype
})->store;
3131 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3132 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
3133 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
3135 # not for loan at item type level
3136 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(1)->store;
3137 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3138 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3141 { NOT_FOR_LOAN
=> 1, itemtype_notforloan
=> $itemtype->{itemtype
} },
3142 'Item can not be issued, not for loan at item type level'
3145 # not for loan at item level
3146 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(undef)->store;
3147 $item->notforloan( 1 )->store;
3148 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
3149 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3152 { NOT_FOR_LOAN
=> 1, item_notforloan
=> 1 },
3153 'Item can not be issued, not for loan at item type level'
3157 # TODO test with AllowNotForLoanOverride = 1
3160 subtest
'AddReturn should clear items.onloan for unissued items' => sub {
3163 t
::lib
::Mocks
::mock_preference
( "AllowReturnToBranch", 'anywhere' );
3164 my $item = $builder->build_sample_item(
3166 onloan
=> '2018-01-01',
3170 AddReturn
( $item->barcode, $item->homebranch );
3171 $item->discard_changes; # refresh
3172 is
( $item->onloan, undef, 'AddReturn did clear items.onloan' );
3176 subtest
'AddRenewal and AddIssuingCharge tests' => sub {
3181 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
3183 my $issuing_charges = 15;
3184 my $title = 'A title';
3185 my $author = 'Author, An';
3186 my $barcode = 'WHATARETHEODDS';
3188 my $circ = Test
::MockModule
->new('C4::Circulation');
3190 'GetIssuingCharges',
3192 return $issuing_charges;
3196 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3197 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value
=> { rentalcharge_daily
=> 0.00 }});
3198 my $patron = $builder->build_object({
3199 class => 'Koha::Patrons',
3200 value
=> { branchcode
=> $library->id }
3203 my $biblio = $builder->build_sample_biblio({ title
=> $title, author
=> $author });
3204 my $item_id = Koha
::Item
->new(
3206 biblionumber
=> $biblio->biblionumber,
3207 homebranch
=> $library->id,
3208 holdingbranch
=> $library->id,
3209 barcode
=> $barcode,
3210 replacementprice
=> 23.00,
3211 itype
=> $itemtype->id
3213 )->store->itemnumber;
3214 my $item = Koha
::Items
->find( $item_id );
3216 my $context = Test
::MockModule
->new('C4::Context');
3217 $context->mock( userenv
=> { branch
=> $library->id } );
3219 # Check the item out
3220 AddIssue
( $patron->unblessed, $item->barcode );
3221 t
::lib
::Mocks
::mock_preference
( 'RenewalLog', 0 );
3222 my $date = output_pref
( { dt
=> dt_from_string
(), dateonly
=> 1, dateformat
=> 'iso' } );
3223 my %params_renewal = (
3224 timestamp
=> { -like
=> $date . "%" },
3225 module
=> "CIRCULATION",
3226 action
=> "RENEWAL",
3228 my $old_log_size = Koha
::ActionLogs
->count( \
%params_renewal );;
3229 AddRenewal
( $patron->id, $item->id, $library->id );
3230 my $new_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
3231 is
( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
3233 my $checkouts = $patron->checkouts;
3234 # The following will fail if run on 00:00:00
3235 unlike
( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
3237 my $lines = Koha
::Account
::Lines
->search({
3238 borrowernumber
=> $patron->id,
3239 itemnumber
=> $item->id
3242 is
( $lines->count, 2 );
3244 my $line = $lines->next;
3245 is
( $line->debit_type_code, 'RENT', 'The issue of item with issuing charge generates an accountline of the correct type' );
3246 is
( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
3247 is
( $line->description, '', 'AddIssue does not set a hardcoded description for the accountline' );
3249 $line = $lines->next;
3250 is
( $line->debit_type_code, 'RENT_RENEW', 'The renewal of item with issuing charge generates an accountline of the correct type' );
3251 is
( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
3252 is
( $line->description, '', 'AddRenewal does not set a hardcoded description for the accountline' );
3254 t
::lib
::Mocks
::mock_preference
( 'RenewalLog', 1 );
3256 $context = Test
::MockModule
->new('C4::Context');
3257 $context->mock( userenv
=> { branch
=> undef, interface
=> 'CRON'} ); #Test statistical logging of renewal via cron (atuo_renew)
3259 my $now = dt_from_string
;
3260 $date = output_pref
( { dt
=> $now, dateonly
=> 1, dateformat
=> 'iso' } );
3261 $old_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
3262 my $sth = $dbh->prepare("SELECT COUNT(*) FROM statistics WHERE itemnumber = ? AND branch = ?");
3263 $sth->execute($item->id, $library->id);
3264 my ($old_stats_size) = $sth->fetchrow_array;
3265 AddRenewal
( $patron->id, $item->id, $library->id );
3266 $new_log_size = Koha
::ActionLogs
->count( \
%params_renewal );
3267 $sth->execute($item->id, $library->id);
3268 my ($new_stats_size) = $sth->fetchrow_array;
3269 is
( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
3270 is
( $new_stats_size, $old_stats_size + 1, 'renew statistic successfully added with passed branch' );
3272 AddReturn
( $item->id, $library->id, undef, $date );
3273 AddIssue
( $patron->unblessed, $item->barcode, $now );
3274 AddRenewal
( $patron->id, $item->id, $library->id, undef, undef, 1 );
3275 my $lines_skipped = Koha
::Account
::Lines
->search({
3276 borrowernumber
=> $patron->id,
3277 itemnumber
=> $item->id
3279 is
( $lines_skipped->count, 5, 'Passing skipfinecalc causes fine calculation on renewal to be skipped' );
3283 subtest
'ProcessOfflinePayment() tests' => sub {
3290 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
3291 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3292 my $result = C4
::Circulation
::ProcessOfflinePayment
({ cardnumber
=> $patron->cardnumber, amount
=> $amount, branchcode
=> $library->id });
3294 is
( $result, 'Success.', 'The right string is returned' );
3296 my $lines = $patron->account->lines;
3297 is
( $lines->count, 1, 'line created correctly');
3299 my $line = $lines->next;
3300 is
( $line->amount+0, $amount * -1, 'amount picked from params' );
3301 is
( $line->branchcode, $library->id, 'branchcode set correctly' );
3305 subtest
'Incremented fee tests' => sub {
3308 my $dt = dt_from_string
();
3309 Time
::Fake
->offset( $dt->epoch );
3311 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 1 );
3314 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3316 my $module = new Test
::MockModule
('C4::Context');
3317 $module->mock( 'userenv', sub { { branch
=> $library->id } } );
3319 my $patron = $builder->build_object(
3321 class => 'Koha::Patrons',
3322 value
=> { categorycode
=> $patron_category->{categorycode
} }
3326 my $itemtype = $builder->build_object(
3328 class => 'Koha::ItemTypes',
3330 notforloan
=> undef,
3332 rentalcharge_daily
=> 1,
3333 rentalcharge_daily_calendar
=> 0
3338 my $item = $builder->build_sample_item(
3340 library
=> $library->{branchcode
},
3341 itype
=> $itemtype->id,
3345 is
( $itemtype->rentalcharge_daily+0,
3346 1, 'Daily rental charge stored and retreived correctly' );
3347 is
( $item->effective_itemtype, $itemtype->id,
3348 "Itemtype set correctly for item" );
3350 my $now = dt_from_string
;
3351 my $dt_from = $now->clone;
3352 my $dt_to = $now->clone->add( days
=> 7 );
3353 my $dt_to_renew = $now->clone->add( days
=> 13 );
3357 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3358 my $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3359 is
( $accountline->amount+0, 7,
3360 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0"
3362 $accountline->delete();
3363 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3364 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3365 is
( $accountline->amount+0, 6,
3366 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0, for renewal"
3368 $accountline->delete();
3371 t
::lib
::Mocks
::mock_preference
( 'finesCalendar', 'noFinesWhenClosed' );
3372 $itemtype->rentalcharge_daily_calendar(1)->store();
3374 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3375 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3376 is
( $accountline->amount+0, 7,
3377 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1"
3379 $accountline->delete();
3380 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3381 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3382 is
( $accountline->amount+0, 6,
3383 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1, for renewal"
3385 $accountline->delete();
3388 my $calendar = C4
::Calendar
->new( branchcode
=> $library->id );
3389 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
3391 ( $dt_from->day_of_week == 6 ) ?
0
3392 : ( $dt_from->day_of_week == 7 ) ?
1
3393 : $dt_from->day_of_week + 1;
3394 my $closed_day_name = $dt_from->clone->add(days
=> 1)->day_name;
3395 $calendar->insert_week_day_holiday(
3396 weekday
=> $closed_day,
3397 title
=> 'Test holiday',
3398 description
=> 'Test holiday'
3401 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3402 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3403 is
( $accountline->amount+0, 6,
3404 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name"
3406 $accountline->delete();
3407 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3408 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3409 is
( $accountline->amount+0, 5,
3410 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name, for renewal"
3412 $accountline->delete();
3415 $itemtype->rentalcharge(2)->store;
3416 is
( $itemtype->rentalcharge+0, 2,
3417 'Rental charge updated and retreived correctly' );
3419 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3421 Koha
::Account
::Lines
->search( { itemnumber
=> $item->id } );
3422 is
( $accountlines->count, '2',
3423 "Fixed charge and accrued charge recorded distinctly" );
3424 $accountlines->delete();
3425 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3426 $accountlines = Koha
::Account
::Lines
->search( { itemnumber
=> $item->id } );
3427 is
( $accountlines->count, '2',
3428 "Fixed charge and accrued charge recorded distinctly, for renewal" );
3429 $accountlines->delete();
3431 $itemtype->rentalcharge(0)->store;
3432 is
( $itemtype->rentalcharge+0, 0,
3433 'Rental charge reset and retreived correctly' );
3436 Koha
::CirculationRules
->set_rule(
3438 categorycode
=> $patron->categorycode,
3439 itemtype
=> $itemtype->id,
3440 branchcode
=> $library->id,
3441 rule_name
=> 'lengthunit',
3442 rule_value
=> 'hours',
3446 $itemtype->rentalcharge_hourly('0.25')->store();
3447 is
( $itemtype->rentalcharge_hourly,
3448 '0.25', 'Hourly rental charge stored and retreived correctly' );
3450 $dt_to = $now->clone->add( hours
=> 168 );
3451 $dt_to_renew = $now->clone->add( hours
=> 312 );
3453 $itemtype->rentalcharge_hourly_calendar(0)->store();
3455 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3456 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3457 is
( $accountline->amount + 0, 42,
3458 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0 (168h * 0.25u)" );
3459 $accountline->delete();
3460 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3461 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3462 is
( $accountline->amount + 0, 36,
3463 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0, for renewal (312h - 168h * 0.25u)" );
3464 $accountline->delete();
3467 $itemtype->rentalcharge_hourly_calendar(1)->store();
3469 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3470 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3471 is
( $accountline->amount + 0, 36,
3472 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name (168h - 24h * 0.25u)" );
3473 $accountline->delete();
3474 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3475 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3476 is
( $accountline->amount + 0, 30,
3477 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name, for renewal (312h - 168h - 24h * 0.25u" );
3478 $accountline->delete();
3481 $calendar->delete_holiday( weekday
=> $closed_day );
3483 AddIssue
( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3484 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3485 is
( $accountline->amount + 0, 42,
3486 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 (168h - 0h * 0.25u" );
3487 $accountline->delete();
3488 AddRenewal
( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3489 $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item->id } );
3490 is
( $accountline->amount + 0, 36,
3491 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1, for renewal (312h - 168h - 0h * 0.25u)" );
3492 $accountline->delete();
3497 subtest
'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
3500 t
::lib
::Mocks
::mock_preference
('RentalFeesCheckoutConfirmation', 1);
3501 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
3504 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3505 my $patron = $builder->build_object(
3507 class => 'Koha::Patrons',
3508 value
=> { categorycode
=> $patron_category->{categorycode
} }
3512 my $itemtype = $builder->build_object(
3514 class => 'Koha::ItemTypes',
3518 rentalcharge_daily
=> 0
3523 my $item = $builder->build_sample_item(
3525 library
=> $library->id,
3529 itype
=> $itemtype->id,
3533 my ( $issuingimpossible, $needsconfirmation );
3534 my $dt_from = dt_from_string
();
3535 my $dt_due = $dt_from->clone->add( days
=> 3 );
3537 $itemtype->rentalcharge(1)->store;
3538 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, $dt_due, undef, undef, undef );
3539 is_deeply
( $needsconfirmation, { RENTALCHARGE
=> '1.00' }, 'Item needs rentalcharge confirmation to be issued' );
3540 $itemtype->rentalcharge('0')->store;
3541 $itemtype->rentalcharge_daily(1)->store;
3542 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, $dt_due, undef, undef, undef );
3543 is_deeply
( $needsconfirmation, { RENTALCHARGE
=> '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
3544 $itemtype->rentalcharge_daily('0')->store;
3547 subtest
'CanBookBeIssued & CircConfirmItemParts' => sub {
3550 t
::lib
::Mocks
::mock_preference
('CircConfirmItemParts', 1);
3552 my $patron = $builder->build_object(
3554 class => 'Koha::Patrons',
3555 value
=> { categorycode
=> $patron_category->{categorycode
} }
3559 my $item = $builder->build_sample_item(
3561 materials
=> 'includes DVD',
3565 my $dt_due = dt_from_string
->add( days
=> 3 );
3567 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, $dt_due, undef, undef, undef );
3568 is_deeply
( $needsconfirmation, { ADDITIONAL_MATERIALS
=> 'includes DVD' }, 'Item needs confirmation of additional parts' );
3571 subtest
'Do not return on renewal (LOST charge)' => sub {
3574 t
::lib
::Mocks
::mock_preference
('MarkLostItemsAsReturned', 'onpayment');
3575 my $library = $builder->build_object( { class => "Koha::Libraries" } );
3576 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
3577 t
::lib
::Mocks
::mock_userenv
({ patron
=> $manager,branchcode
=> $manager->branchcode });
3579 my $biblio = $builder->build_sample_biblio;
3581 my $item = $builder->build_sample_item(
3583 biblionumber
=> $biblio->biblionumber,
3584 library
=> $library->branchcode,
3585 replacementprice
=> 99.00,
3590 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3591 AddIssue
( $patron->unblessed, $item->barcode );
3593 my $accountline = Koha
::Account
::Line
->new(
3595 borrowernumber
=> $patron->borrowernumber,
3596 debit_type_code
=> 'LOST',
3598 itemnumber
=> $item->itemnumber,
3600 amountoutstanding
=> 12,
3601 interface
=> 'something',
3605 # AddRenewal doesn't call _FixAccountForLostAndFound
3606 AddIssue
( $patron->unblessed, $item->barcode );
3608 is
( $patron->checkouts->count, 1,
3609 'Renewal should not return the item even if a LOST payment has been made earlier'
3613 subtest
'Filling a hold should cancel existing transfer' => sub {
3616 t
::lib
::Mocks
::mock_preference
('AutomaticItemReturn', 1);
3618 my $libraryA = $builder->build_object( { class => 'Koha::Libraries' } );
3619 my $libraryB = $builder->build_object( { class => 'Koha::Libraries' } );
3620 my $patron = $builder->build_object(
3622 class => 'Koha::Patrons',
3624 categorycode
=> $patron_category->{categorycode
},
3625 branchcode
=> $libraryA->branchcode,
3630 my $item = $builder->build_sample_item({
3631 homebranch
=> $libraryB->branchcode,
3634 my ( undef, $message ) = AddReturn
( $item->barcode, $libraryA->branchcode, undef, undef );
3635 is
( Koha
::Item
::Transfers
->search({ itemnumber
=> $item->itemnumber, datearrived
=> undef })->count, 1, "We generate a transfer on checkin");
3637 branchcode
=> $libraryA->branchcode,
3638 borrowernumber
=> $patron->borrowernumber,
3639 biblionumber
=> $item->biblionumber,
3640 itemnumber
=> $item->itemnumber
3642 my $reserves = Koha
::Holds
->search({ itemnumber
=> $item->itemnumber });
3643 is
( $reserves->count, 1, "Reserve is placed");
3644 ( undef, $message ) = AddReturn
( $item->barcode, $libraryA->branchcode, undef, undef );
3645 my $reserve = $reserves->next;
3646 ModReserveAffect
( $item->itemnumber, $patron->borrowernumber, 0, $reserve->reserve_id );
3647 $reserve->discard_changes;
3648 ok
( $reserve->found eq 'W', "Reserve is marked waiting" );
3649 is
( Koha
::Item
::Transfers
->search({ itemnumber
=> $item->itemnumber, datearrived
=> undef })->count, 0, "No outstanding transfers when hold is waiting");
3652 subtest
'Tests for NoRefundOnLostReturnedItemsAge with AddReturn' => sub {
3656 t
::lib
::Mocks
::mock_preference
('BlockReturnOfLostItems', 0);
3657 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
3658 my $patron = $builder->build_object(
3660 class => 'Koha::Patrons',
3661 value
=> { categorycode
=> $patron_category->{categorycode
} }
3665 my $biblionumber = $builder->build_sample_biblio(
3667 branchcode
=> $library->branchcode,
3671 # And the circulation rule
3672 Koha
::CirculationRules
->search->delete;
3673 Koha
::CirculationRules
->set_rules(
3675 categorycode
=> undef,
3677 branchcode
=> undef,
3680 lengthunit
=> 'days',
3686 source
=> 'CirculationRule',
3688 branchcode
=> undef,
3689 categorycode
=> undef,
3691 rule_name
=> 'refund',
3697 subtest
'NoRefundOnLostReturnedItemsAge = undef' => sub {
3700 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3701 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', undef );
3703 my $lost_on = dt_from_string
->subtract( days
=> 7 )->date;
3705 my $item = $builder->build_sample_item(
3707 biblionumber
=> $biblionumber,
3708 library
=> $library->branchcode,
3709 replacementprice
=> '42',
3712 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3713 LostItem
( $item->itemnumber, 'cli', 0 );
3714 $item->_result->itemlost(1);
3715 $item->_result->itemlost_on( $lost_on );
3716 $item->_result->update();
3718 my $a = Koha
::Account
::Lines
->search(
3720 itemnumber
=> $item->id,
3721 borrowernumber
=> $patron->borrowernumber
3724 ok
( $a, "Found accountline for lost fee" );
3725 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3726 my ( $doreturn, $messages ) = AddReturn
( $item->barcode, $library->branchcode, undef, dt_from_string
);
3727 $a = $a->get_from_storage;
3728 is
( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3732 subtest
'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
3735 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3736 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
3738 my $lost_on = dt_from_string
->subtract( days
=> 6 )->date;
3740 my $item = $builder->build_sample_item(
3742 biblionumber
=> $biblionumber,
3743 library
=> $library->branchcode,
3744 replacementprice
=> '42',
3747 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3748 LostItem
( $item->itemnumber, 'cli', 0 );
3749 $item->_result->itemlost(1);
3750 $item->_result->itemlost_on( $lost_on );
3751 $item->_result->update();
3753 my $a = Koha
::Account
::Lines
->search(
3755 itemnumber
=> $item->id,
3756 borrowernumber
=> $patron->borrowernumber
3759 ok
( $a, "Found accountline for lost fee" );
3760 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3761 my ( $doreturn, $messages ) = AddReturn
( $item->barcode, $library->branchcode, undef, dt_from_string
);
3762 $a = $a->get_from_storage;
3763 is
( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3767 subtest
'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
3770 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3771 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
3773 my $lost_on = dt_from_string
->subtract( days
=> 7 )->date;
3775 my $item = $builder->build_sample_item(
3777 biblionumber
=> $biblionumber,
3778 library
=> $library->branchcode,
3779 replacementprice
=> '42',
3782 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3783 LostItem
( $item->itemnumber, 'cli', 0 );
3784 $item->_result->itemlost(1);
3785 $item->_result->itemlost_on( $lost_on );
3786 $item->_result->update();
3788 my $a = Koha
::Account
::Lines
->search(
3790 itemnumber
=> $item->id,
3791 borrowernumber
=> $patron->borrowernumber
3794 ok
( $a, "Found accountline for lost fee" );
3795 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3796 my ( $doreturn, $messages ) = AddReturn
( $item->barcode, $library->branchcode, undef, dt_from_string
);
3797 $a = $a->get_from_storage;
3798 is
( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
3802 subtest
'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
3805 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3806 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
3808 my $lost_on = dt_from_string
->subtract( days
=> 8 )->date;
3810 my $item = $builder->build_sample_item(
3812 biblionumber
=> $biblionumber,
3813 library
=> $library->branchcode,
3814 replacementprice
=> '42',
3817 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3818 LostItem
( $item->itemnumber, 'cli', 0 );
3819 $item->_result->itemlost(1);
3820 $item->_result->itemlost_on( $lost_on );
3821 $item->_result->update();
3823 my $a = Koha
::Account
::Lines
->search(
3825 itemnumber
=> $item->id,
3826 borrowernumber
=> $patron->borrowernumber
3830 ok
( $a, "Found accountline for lost fee" );
3831 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3832 my ( $doreturn, $messages ) = AddReturn
( $item->barcode, $library->branchcode, undef, dt_from_string
);
3833 $a = $a->get_from_storage;
3834 is
( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
3839 subtest
'Tests for NoRefundOnLostReturnedItemsAge with AddIssue' => sub {
3843 t
::lib
::Mocks
::mock_preference
('BlockReturnOfLostItems', 0);
3844 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
3845 my $patron = $builder->build_object(
3847 class => 'Koha::Patrons',
3848 value
=> { categorycode
=> $patron_category->{categorycode
} }
3851 my $patron2 = $builder->build_object(
3853 class => 'Koha::Patrons',
3854 value
=> { categorycode
=> $patron_category->{categorycode
} }
3858 my $biblionumber = $builder->build_sample_biblio(
3860 branchcode
=> $library->branchcode,
3864 # And the circulation rule
3865 Koha
::CirculationRules
->search->delete;
3866 Koha
::CirculationRules
->set_rules(
3868 categorycode
=> undef,
3870 branchcode
=> undef,
3873 lengthunit
=> 'days',
3879 source
=> 'CirculationRule',
3881 branchcode
=> undef,
3882 categorycode
=> undef,
3884 rule_name
=> 'refund',
3890 subtest
'NoRefundOnLostReturnedItemsAge = undef' => sub {
3893 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3894 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', undef );
3896 my $lost_on = dt_from_string
->subtract( days
=> 7 )->date;
3898 my $item = $builder->build_sample_item(
3900 biblionumber
=> $biblionumber,
3901 library
=> $library->branchcode,
3902 replacementprice
=> '42',
3905 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3906 LostItem
( $item->itemnumber, 'cli', 0 );
3907 $item->_result->itemlost(1);
3908 $item->_result->itemlost_on( $lost_on );
3909 $item->_result->update();
3911 my $a = Koha
::Account
::Lines
->search(
3913 itemnumber
=> $item->id,
3914 borrowernumber
=> $patron->borrowernumber
3917 ok
( $a, "Found accountline for lost fee" );
3918 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3919 $issue = AddIssue
( $patron2->unblessed, $item->barcode );
3920 $a = $a->get_from_storage;
3921 is
( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3926 subtest
'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
3929 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3930 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
3932 my $lost_on = dt_from_string
->subtract( days
=> 6 )->date;
3934 my $item = $builder->build_sample_item(
3936 biblionumber
=> $biblionumber,
3937 library
=> $library->branchcode,
3938 replacementprice
=> '42',
3941 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3942 LostItem
( $item->itemnumber, 'cli', 0 );
3943 $item->_result->itemlost(1);
3944 $item->_result->itemlost_on( $lost_on );
3945 $item->_result->update();
3947 my $a = Koha
::Account
::Lines
->search(
3949 itemnumber
=> $item->id,
3950 borrowernumber
=> $patron->borrowernumber
3953 ok
( $a, "Found accountline for lost fee" );
3954 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3955 $issue = AddIssue
( $patron2->unblessed, $item->barcode );
3956 $a = $a->get_from_storage;
3957 is
( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3961 subtest
'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
3964 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
3965 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
3967 my $lost_on = dt_from_string
->subtract( days
=> 7 )->date;
3969 my $item = $builder->build_sample_item(
3971 biblionumber
=> $biblionumber,
3972 library
=> $library->branchcode,
3973 replacementprice
=> '42',
3976 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
3977 LostItem
( $item->itemnumber, 'cli', 0 );
3978 $item->_result->itemlost(1);
3979 $item->_result->itemlost_on( $lost_on );
3980 $item->_result->update();
3982 my $a = Koha
::Account
::Lines
->search(
3984 itemnumber
=> $item->id,
3985 borrowernumber
=> $patron->borrowernumber
3988 ok
( $a, "Found accountline for lost fee" );
3989 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3990 $issue = AddIssue
( $patron2->unblessed, $item->barcode );
3991 $a = $a->get_from_storage;
3992 is
( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
3996 subtest
'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
3999 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
4000 t
::lib
::Mocks
::mock_preference
( 'NoRefundOnLostReturnedItemsAge', 7 );
4002 my $lost_on = dt_from_string
->subtract( days
=> 8 )->date;
4004 my $item = $builder->build_sample_item(
4006 biblionumber
=> $biblionumber,
4007 library
=> $library->branchcode,
4008 replacementprice
=> '42',
4011 my $issue = AddIssue
( $patron->unblessed, $item->barcode );
4012 LostItem
( $item->itemnumber, 'cli', 0 );
4013 $item->_result->itemlost(1);
4014 $item->_result->itemlost_on( $lost_on );
4015 $item->_result->update();
4017 my $a = Koha
::Account
::Lines
->search(
4019 itemnumber
=> $item->id,
4020 borrowernumber
=> $patron->borrowernumber
4024 ok
( $a, "Found accountline for lost fee" );
4025 is
( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
4026 $issue = AddIssue
( $patron2->unblessed, $item->barcode );
4027 $a = $a->get_from_storage;
4028 is
( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
4033 subtest
'transferbook tests' => sub {
4037 { C4
::Circulation
::transferbook
({}); }
4038 'Koha::Exceptions::MissingParameter',
4039 'Koha::Patron->store raises an exception on missing params';
4042 { C4
::Circulation
::transferbook
({to_branch
=>'anything'}); }
4043 'Koha::Exceptions::MissingParameter',
4044 'Koha::Patron->store raises an exception on missing params';
4047 { C4
::Circulation
::transferbook
({from_branch
=>'anything'}); }
4048 'Koha::Exceptions::MissingParameter',
4049 'Koha::Patron->store raises an exception on missing params';
4051 my ($doreturn,$messages) = C4
::Circulation
::transferbook
({to_branch
=>'there',from_branch
=>'here'});
4052 is
( $doreturn, 0, "No return without barcode");
4053 ok
( exists $messages->{BadBarcode
}, "We get a BadBarcode message if no barcode passed");
4054 is
( $messages->{BadBarcode
}, undef, "No barcode passed means undef BadBarcode" );
4056 ($doreturn,$messages) = C4
::Circulation
::transferbook
({to_branch
=>'there',from_branch
=>'here',barcode
=>'BadBarcode'});
4057 is
( $doreturn, 0, "No return without barcode");
4058 ok
( exists $messages->{BadBarcode
}, "We get a BadBarcode message if no barcode passed");
4059 is
( $messages->{BadBarcode
}, 'BadBarcode', "No barcode passed means undef BadBarcode" );
4063 subtest
'Checkout should correctly terminate a transfer' => sub {
4066 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
4067 my $patron_1 = $builder->build_object(
4069 class => 'Koha::Patrons',
4070 value
=> { branchcode
=> $library_1->branchcode }
4073 my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
4074 my $patron_2 = $builder->build_object(
4076 class => 'Koha::Patrons',
4077 value
=> { branchcode
=> $library_2->branchcode }
4081 my $item = $builder->build_sample_item(
4083 library
=> $library_1->branchcode,
4087 t
::lib
::Mocks
::mock_userenv
( { branchcode
=> $library_1->branchcode } );
4088 my $reserve_id = AddReserve
(
4090 branchcode
=> $library_2->branchcode,
4091 borrowernumber
=> $patron_2->borrowernumber,
4092 biblionumber
=> $item->biblionumber,
4093 itemnumber
=> $item->itemnumber,
4098 my $do_transfer = 1;
4099 ModItemTransfer
( $item->itemnumber, $library_1->branchcode,
4100 $library_2->branchcode );
4101 ModReserveAffect
( $item->itemnumber, undef, $do_transfer, $reserve_id );
4102 GetOtherReserves
( $item->itemnumber )
4103 ; # To put the Reason, it's what does returns.pl...
4104 my $hold = Koha
::Holds
->find($reserve_id);
4105 is
( $hold->found, 'T', 'Hold is in transit' );
4106 my $transfer = $item->get_transfer;
4107 is
( $transfer->frombranch, $library_1->branchcode );
4108 is
( $transfer->tobranch, $library_2->branchcode );
4109 is
( $transfer->reason, 'Reserve' );
4111 t
::lib
::Mocks
::mock_userenv
( { branchcode
=> $library_2->branchcode } );
4112 AddIssue
( $patron_1->unblessed, $item->barcode );
4113 $transfer = $transfer->get_from_storage;
4114 isnt
( $transfer->datearrived, undef );
4115 $hold = $hold->get_from_storage;
4116 is
( $hold->found, undef, 'Hold is waiting' );
4117 is
( $hold->priority, 1, );
4120 $schema->storage->txn_rollback;
4121 C4
::Context
->clear_syspref_cache();
4122 $branches = Koha
::Libraries
->search();
4123 for my $branch ( $branches->next ) {
4124 my $key = $branch->branchcode . "_holidays";
4125 $cache->clear_from_cache($key);