Bug 26398: Restore translations for credit/debit type dropdown
[koha.git] / t / db_dependent / Circulation.t
blob51706fcf383ce46eceefaaefa501167bec0e9d70
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
19 use utf8;
21 use Test::More tests => 50;
22 use Test::Exception;
23 use Test::MockModule;
24 use Test::Deep qw( cmp_deeply );
26 use Data::Dumper;
27 use DateTime;
28 use Time::Fake;
29 use POSIX qw( floor );
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
33 use C4::Accounts;
34 use C4::Calendar;
35 use C4::Circulation;
36 use C4::Biblio;
37 use C4::Items;
38 use C4::Log;
39 use C4::Reserves;
40 use C4::Overdues qw(UpdateFine CalcFine);
41 use Koha::DateUtils;
42 use Koha::Database;
43 use Koha::Items;
44 use Koha::Item::Transfers;
45 use Koha::Checkouts;
46 use Koha::Patrons;
47 use Koha::Holds;
48 use Koha::CirculationRules;
49 use Koha::Subscriptions;
50 use Koha::Account::Lines;
51 use Koha::Account::Offsets;
52 use Koha::ActionLogs;
54 sub set_userenv {
55 my ( $library ) = @_;
56 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
59 sub str {
60 my ( $error, $question, $alert ) = @_;
61 my $s;
62 $s = %$error ? ' (error: ' . join( ' ', keys %$error ) . ')' : '';
63 $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
64 $s .= %$alert ? ' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
65 return $s;
68 sub test_debarment_on_checkout {
69 my ($params) = @_;
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,
80 dateformat => 'sql',
81 dateonly => 1,
84 my @caller = caller;
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({
125 source => 'Branch',
127 my $library2 = $builder->build({
128 source => 'Branch',
130 my $itemtype = $builder->build(
132 source => 'Itemtype',
133 value => {
134 notforloan => undef,
135 rentalcharge => 0,
136 rentalcharge_daily => 0,
137 defaultreplacecost => undef,
138 processfee => undef
141 )->{itemtype};
142 my $patron_category = $builder->build(
144 source => 'Category',
145 value => {
146 category_type => 'P',
147 enrolmentfee => 0,
148 BlockExpiredPatronOpacActions => -1, # Pick the pref value
153 my $CircControl = C4::Context->preference('CircControl');
154 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
156 my $item = {
157 homebranch => $library2->{branchcode},
158 holdingbranch => $library2->{branchcode}
161 my $borrower = {
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'),
172 'PickupLibrary',
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'),
185 'PatronLibrary',
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'),
198 'ItemHomeLibrary',
199 'CircControl changed to ItemHomeLibrary'
202 $item->{$HomeOrHoldingBranch},
203 C4::Circulation::_GetCircControlBranch($item, $borrower),
204 '_GetCircControlBranch returned item branch'
207 # Now, set a userenv
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'),
215 'PickupLibrary',
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'),
228 'PatronLibrary',
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'),
241 'ItemHomeLibrary',
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'),
254 $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,
263 branchcode => undef,
264 itemtype => undef,
265 rules => {
266 reservesallowed => 25,
267 issuelength => 14,
268 lengthunit => 'days',
269 renewalsallowed => 1,
270 renewalperiod => 7,
271 norenewalbefore => undef,
272 auto_renew => 0,
273 fine => .10,
274 chargeperiod => 1,
279 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
280 subtest "CanBookBeRenewed tests" => sub {
281 plan tests => 87;
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,
292 library => $branch,
293 replacementprice => 12.00,
294 itype => $itemtype
297 $reused_itemnumber_1 = $item_1->itemnumber;
299 my $item_2 = $builder->build_sample_item(
301 biblionumber => $biblio->biblionumber,
302 library => $branch,
303 replacementprice => 23.00,
304 itype => $itemtype
307 $reused_itemnumber_2 = $item_2->itemnumber;
309 my $item_3 = $builder->build_sample_item(
311 biblionumber => $biblio->biblionumber,
312 library => $branch,
313 replacementprice => 23.00,
314 itype => $itemtype
318 # Create borrowers
319 my %renewing_borrower_data = (
320 firstname => 'John',
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 = (
334 firstname => 'Kyle',
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 = (
349 firstname => 'Ça',
350 surname => 'Glisse',
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;
367 my $bibitems = '';
368 my $priority = '1';
369 my $resdate = undef;
370 my $expdate = undef;
371 my $notes = '';
372 my $checkitem = undef;
373 my $found = 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
392 AddReserve(
394 branchcode => $branch,
395 borrowernumber => $reserving_borrowernumber,
396 biblionumber => $biblio->biblionumber,
397 priority => $priority,
398 reservation_date => $resdate,
399 expiration_date => $expdate,
400 notes => $notes,
401 itemnumber => $checkitem,
402 found => $found,
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,
410 branchcode => undef,
411 itemtype => undef,
412 rule_name => 'onshelfholds',
413 rule_value => '1',
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,
429 priority => 3,
432 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
433 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
434 $hold->delete();
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,
444 priority => 0,
445 found => 'W'
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 = ?',
467 { Slice => {} },
468 $reserveid
470 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
472 # Item-level hold, renewal test
473 AddReserve(
475 branchcode => $branch,
476 borrowernumber => $reserving_borrowernumber,
477 biblionumber => $biblio->biblionumber,
478 priority => $priority,
479 reservation_date => $resdate,
480 expiration_date => $expdate,
481 notes => $notes,
482 itemnumber => $item_1->itemnumber,
483 found => $found,
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,
506 library => $branch,
507 replacementprice => 23.00,
508 itype => $itemtype,
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,
524 library => $branch,
525 replacementprice => 23.00,
526 itype => $itemtype,
530 my $item_7 = $builder->build_sample_item(
532 biblionumber => $biblio->biblionumber,
533 library => $branch,
534 replacementprice => 23.00,
535 itype => $itemtype,
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},
556 amount => $fine,
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",
566 action => "RENEWAL",
568 my %params_issue = (
569 timestamp => { -like => $date . "%" },
570 module => "CIRCULATION",
571 action => "ISSUE"
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");
580 Time::Fake->reset;
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' );
593 $fines->delete();
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 } );
605 $fines->delete();
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;
615 $hold->cancel;
617 # Bug 14101
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,
623 library => $branch,
624 replacementprice => 16.00,
625 itype => $itemtype,
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)' );
635 AddReserve(
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,
644 notes => $notes,
645 title => 'a title',
646 itemnumber => $item_4->itemnumber,
647 found => $found
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;
676 # Bug 7413
677 # Test premature manual renewal
678 Koha::CirculationRules->set_rule(
680 categorycode => undef,
681 branchcode => undef,
682 itemtype => undef,
683 rule_name => 'norenewalbefore',
684 rule_value => '7',
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)');
692 # Bug 14395
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'
701 # Bug 14395
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'
710 # Bug 14101
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 {
757 plan tests => 14;
758 my $item_to_auto_renew = $builder->build_sample_item(
760 biblionumber => $biblio->biblionumber,
761 library => $branch,
765 my $ten_days_before = dt_from_string->add( days => -10 );
766 my $ten_days_ahead = dt_from_string->add( days => 10 );
767 AddIssue( $renewing_borrower, $item_to_auto_renew->barcode, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
769 Koha::CirculationRules->set_rules(
771 categorycode => undef,
772 branchcode => undef,
773 itemtype => undef,
774 rules => {
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,
788 branchcode => undef,
789 itemtype => undef,
790 rules => {
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,
804 branchcode => undef,
805 itemtype => undef,
806 rules => {
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,
820 branchcode => undef,
821 itemtype => undef,
822 rules => {
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,
836 branchcode => undef,
837 itemtype => undef,
838 rules => {
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,
853 branchcode => undef,
854 itemtype => undef,
855 rules => {
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,
870 branchcode => undef,
871 itemtype => undef,
872 rules => {
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 {
886 plan tests => 10;
887 my $item_to_auto_renew = $builder->build_sample_item(
889 biblionumber => $biblio->biblionumber,
890 library => $branch,
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,
901 branchcode => undef,
902 itemtype => undef,
903 rules => {
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});
914 $account->add_debit(
916 amount => $fines_amount,
917 interface => 'test',
918 type => 'OVERDUE',
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' );
928 $account->add_debit(
930 amount => $fines_amount,
931 interface => 'test',
932 type => 'OVERDUE',
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' );
942 $account->add_debit(
944 amount => $fines_amount,
945 interface => 'test',
946 type => 'OVERDUE',
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,
959 interface => 'test',
960 type => 'PAYMENT',
961 description => "Some payment"
963 )->store;
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 {
980 plan tests => 6;
981 my $item_to_auto_renew = $builder->build_sample_item(
983 biblionumber => $biblio->biblionumber,
984 library => $branch,
988 Koha::CirculationRules->set_rules(
990 categorycode => undef,
991 branchcode => undef,
992 itemtype => undef,
993 rules => {
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 {
1040 plan tests => 5;
1041 my $item_to_auto_renew = $builder->build_sample_item(
1043 biblionumber => $biblio->biblionumber,
1044 library => $branch,
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,
1055 itemtype => undef,
1056 rules => {
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,
1070 itemtype => undef,
1071 rules => {
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,
1091 itemtype => undef,
1092 rules => {
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,
1109 itemtype => undef,
1110 rules => {
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,
1126 itemtype => undef,
1127 rules => {
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'
1141 # Too many renewals
1143 # set policy to forbid renewals
1144 Koha::CirculationRules->set_rules(
1146 categorycode => undef,
1147 branchcode => undef,
1148 itemtype => undef,
1149 rules => {
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},
1169 amount => 15.00,
1170 type => q{},
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},
1214 amount => 15.00,
1215 type => q{},
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 {
1263 plan tests => 12;
1265 my $branch = $library2->{branchcode};
1267 #Create another record
1268 my $biblio2 = $builder->build_sample_biblio();
1270 #Create third item
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,
1276 library => $branch,
1277 itype => $itemtype,
1282 # Create a borrower
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() );
1302 my $upcoming_dues;
1304 # GetUpcomingDueIssues tests
1305 for my $i(0..1) {
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" );
1314 for my $i(3..5) {
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();
1349 #Create third item
1350 my $item = $builder->build_sample_item(
1352 biblionumber => $biblio->biblionumber,
1353 library => $branch,
1354 itype => $itemtype,
1358 # Create a borrower
1359 my %a_borrower_data = (
1360 firstname => 'Kyle',
1361 surname => 'Hall',
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 );
1370 UpdateFine(
1372 issue_id => $issue->id(),
1373 itemnumber => $item->itemnumber,
1374 borrowernumber => $borrowernumber,
1375 amount => 0,
1376 type => q{}
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,
1393 itemtype => undef,
1394 branchcode => undef,
1395 rules => {
1396 reservesallowed => 25,
1397 issuelength => 14,
1398 lengthunit => 'days',
1399 renewalsallowed => 1,
1400 renewalperiod => 7,
1401 norenewalbefore => undef,
1402 auto_renew => 0,
1403 fine => .10,
1404 chargeperiod => 1,
1405 maxissueqty => 20
1409 my $biblio = $builder->build_sample_biblio();
1411 my $item_1 = $builder->build_sample_item(
1413 biblionumber => $biblio->biblionumber,
1414 library => $library2->{branchcode},
1415 itype => $itemtype,
1419 my $item_2= $builder->build_sample_item(
1421 biblionumber => $biblio->biblionumber,
1422 library => $library2->{branchcode},
1423 itype => $itemtype,
1427 my $borrowernumber1 = Koha::Patron->new({
1428 firstname => 'Kyle',
1429 surname => 'Hall',
1430 categorycode => $patron_category->{categorycode},
1431 branchcode => $library2->{branchcode},
1432 })->store->borrowernumber;
1433 my $borrowernumber2 = Koha::Patron->new({
1434 firstname => 'Chelsea',
1435 surname => 'Hall',
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' );
1448 AddReserve(
1450 branchcode => $library2->{branchcode},
1451 borrowernumber => $borrowernumber2,
1452 biblionumber => $biblio->biblionumber,
1453 priority => 1,
1457 Koha::CirculationRules->set_rules(
1459 categorycode => undef,
1460 itemtype => undef,
1461 branchcode => undef,
1462 rules => {
1463 onshelfholds => 0,
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,
1474 itemtype => undef,
1475 branchcode => undef,
1476 rules => {
1477 onshelfholds => 0,
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,
1488 itemtype => undef,
1489 branchcode => undef,
1490 rules => {
1491 onshelfholds => 1,
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,
1502 itemtype => undef,
1503 branchcode => undef,
1504 rules => {
1505 onshelfholds => 1,
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,
1530 library => $branch,
1531 itype => $itemtype,
1535 my $borrowernumber = Koha::Patron->new({
1536 firstname => 'fn',
1537 surname => 'dn',
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},
1559 itype => $itemtype,
1563 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1565 my $issue = AddIssue( $patron, $item->barcode );
1566 UpdateFine(
1568 issue_id => $issue->id(),
1569 itemnumber => $item->itemnumber,
1570 borrowernumber => $patron->{borrowernumber},
1571 amount => 1,
1572 type => q{}
1575 UpdateFine(
1577 issue_id => $issue->id(),
1578 itemnumber => $item->itemnumber,
1579 borrowernumber => $patron->{borrowernumber},
1580 amount => 2,
1581 type => q{}
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 {
1588 plan tests => 24;
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 {
1675 plan tests => 9;
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 {
1741 plan tests => 8;
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 {
1786 plan tests => 1;
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',
1798 value => {
1799 categorycode => $patron_category_x->categorycode,
1800 gonenoaddress => undef,
1801 lost => undef,
1802 debarred => undef,
1803 borrowernotes => ""
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 {
1820 plan tests => 3;
1822 my $biblio = $builder->build_sample_biblio();
1824 my $branch = $library2->{branchcode};
1826 my $item_1 = $builder->build_sample_item(
1828 biblionumber => $biblio->biblionumber,
1829 library => $branch,
1830 replacementprice => 12.00,
1831 itype => $itemtype,
1835 my $item_2 = $builder->build_sample_item(
1837 biblionumber => $biblio->biblionumber,
1838 library => $branch,
1839 replacementprice => 12.00,
1840 itype => $itemtype,
1844 my $bibitems = '';
1845 my $priority = '1';
1846 my $resdate = undef;
1847 my $expdate = undef;
1848 my $notes = '';
1849 my $checkitem = undef;
1850 my $found = 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;
1872 AddReserve(
1874 branchcode => $branch,
1875 borrowernumber => $reserving_borrowernumber1,
1876 biblionumber => $biblio->biblionumber,
1877 priority => $priority,
1878 reservation_date => $resdate,
1879 expiration_date => $expdate,
1880 notes => $notes,
1881 itemnumber => $checkitem,
1882 found => $found,
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;
1893 AddReserve(
1895 branchcode => $branch,
1896 borrowernumber => $reserving_borrowernumber2,
1897 biblionumber => $biblio->biblionumber,
1898 priority => $priority,
1899 reservation_date => $resdate,
1900 expiration_date => $expdate,
1901 notes => $notes,
1902 itemnumber => $checkitem,
1903 found => $found,
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,
1915 library => $branch,
1916 replacementprice => 12.00,
1917 itype => $itemtype,
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 {
1928 plan tests => 5;
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},
1937 )->biblionumber;
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 );
1957 cmp_deeply(
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 );
1966 cmp_deeply(
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 );
1977 cmp_deeply(
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 );
1985 cmp_deeply(
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 {
1993 plan tests => 8;
1995 my $library = $builder->build( { source => 'Branch' } );
1996 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1998 # Add 2 items
1999 my $biblionumber = $builder->build_sample_biblio(
2001 branchcode => $library->{branchcode},
2003 )->biblionumber;
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,
2022 itemtype => undef,
2023 branchcode => undef,
2024 rules => {
2025 issuelength => 1,
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',
2053 dateonly => 1
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',
2066 dateonly => 1
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',
2086 dateonly => 1
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',
2099 dateonly => 1
2102 is( $debarments->[0]->{expiration}, $expected_expiration );
2105 subtest 'AddReturn + suspension_chargeperiod' => sub {
2106 plan tests => 27;
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},
2115 )->biblionumber;
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 => '*',
2128 itemtype => '*',
2129 branchcode => '*',
2130 rules => {
2131 issuelength => 1,
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(
2147 item => $item_1,
2148 library => $library,
2149 patron => $patron,
2150 due_date => $five_days_ago,
2151 expiration_date => $expected_expiration,
2155 # Same with undef firstremind
2156 Koha::CirculationRules->search->delete;
2157 Koha::CirculationRules->set_rules(
2159 categorycode => '*',
2160 itemtype => '*',
2161 branchcode => '*',
2162 rules => {
2163 issuelength => 1,
2164 firstremind => undef, # 0 day of grace
2165 finedays => 2, # 2 days of fine per day of overdue
2166 suspension_chargeperiod => 1,
2167 lengthunit => 'days',
2172 my $now = dt_from_string;
2173 my $five_days_ago = $now->clone->subtract( days => 5 );
2174 # We want to charge 2 days every day, without grace
2175 # With 5 days of overdue: 5 * Z
2176 my $expected_expiration = $now->clone->add( days => ( 5 * 2 ) / 1 );
2177 test_debarment_on_checkout(
2179 item => $item_1,
2180 library => $library,
2181 patron => $patron,
2182 due_date => $five_days_ago,
2183 expiration_date => $expected_expiration,
2187 # We want to charge 2 days every 2 days, without grace
2188 # With 5 days of overdue: (5 * 2) / 2
2189 Koha::CirculationRules->set_rule(
2191 categorycode => undef,
2192 branchcode => undef,
2193 itemtype => undef,
2194 rule_name => 'suspension_chargeperiod',
2195 rule_value => '2',
2199 $expected_expiration = $now->clone->add( days => floor( 5 * 2 ) / 2 );
2200 test_debarment_on_checkout(
2202 item => $item_1,
2203 library => $library,
2204 patron => $patron,
2205 due_date => $five_days_ago,
2206 expiration_date => $expected_expiration,
2210 # We want to charge 2 days every 3 days, with 1 day of grace
2211 # With 5 days of overdue: ((5-1) / 3 ) * 2
2212 Koha::CirculationRules->set_rules(
2214 categorycode => undef,
2215 branchcode => undef,
2216 itemtype => undef,
2217 rules => {
2218 suspension_chargeperiod => 3,
2219 firstremind => 1,
2223 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
2224 test_debarment_on_checkout(
2226 item => $item_1,
2227 library => $library,
2228 patron => $patron,
2229 due_date => $five_days_ago,
2230 expiration_date => $expected_expiration,
2234 # Use finesCalendar to know if holiday must be skipped to calculate the due date
2235 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
2236 Koha::CirculationRules->set_rules(
2238 categorycode => undef,
2239 branchcode => undef,
2240 itemtype => undef,
2241 rules => {
2242 finedays => 2,
2243 suspension_chargeperiod => 1,
2244 firstremind => 0,
2248 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
2249 t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2251 # Adding a holiday 2 days ago
2252 my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
2253 my $two_days_ago = $now->clone->subtract( days => 2 );
2254 $calendar->insert_single_holiday(
2255 day => $two_days_ago->day,
2256 month => $two_days_ago->month,
2257 year => $two_days_ago->year,
2258 title => 'holidayTest-2d',
2259 description => 'holidayDesc 2 days ago'
2261 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2262 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2263 test_debarment_on_checkout(
2265 item => $item_1,
2266 library => $library,
2267 patron => $patron,
2268 due_date => $five_days_ago,
2269 expiration_date => $expected_expiration,
2273 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2274 my $two_days_ahead = $now->clone->add( days => 2 );
2275 $calendar->insert_single_holiday(
2276 day => $two_days_ahead->day,
2277 month => $two_days_ahead->month,
2278 year => $two_days_ahead->year,
2279 title => 'holidayTest+2d',
2280 description => 'holidayDesc 2 days ahead'
2283 # Same as above, but we should skip D+2
2284 $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
2285 test_debarment_on_checkout(
2287 item => $item_1,
2288 library => $library,
2289 patron => $patron,
2290 due_date => $five_days_ago,
2291 expiration_date => $expected_expiration,
2295 # Adding another holiday, day of expiration date
2296 my $expected_expiration_dt = dt_from_string($expected_expiration);
2297 $calendar->insert_single_holiday(
2298 day => $expected_expiration_dt->day,
2299 month => $expected_expiration_dt->month,
2300 year => $expected_expiration_dt->year,
2301 title => 'holidayTest_exp',
2302 description => 'holidayDesc on expiration date'
2304 # Expiration date will be the day after
2305 test_debarment_on_checkout(
2307 item => $item_1,
2308 library => $library,
2309 patron => $patron,
2310 due_date => $five_days_ago,
2311 expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
2315 test_debarment_on_checkout(
2317 item => $item_1,
2318 library => $library,
2319 patron => $patron,
2320 return_date => $now->clone->add(days => 5),
2321 expiration_date => $now->clone->add(days => 5 + (5 * 2 - 1) ),
2325 test_debarment_on_checkout(
2327 item => $item_1,
2328 library => $library,
2329 patron => $patron,
2330 due_date => $now->clone->add(days => 1),
2331 return_date => $now->clone->add(days => 5),
2332 expiration_date => $now->clone->add(days => 5 + (4 * 2 - 1) ),
2338 subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
2339 plan tests => 2;
2341 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2342 my $patron1 = $builder->build_object(
2344 class => 'Koha::Patrons',
2345 value => {
2346 library => $library->branchcode,
2347 categorycode => $patron_category->{categorycode}
2351 my $patron2 = $builder->build_object(
2353 class => 'Koha::Patrons',
2354 value => {
2355 library => $library->branchcode,
2356 categorycode => $patron_category->{categorycode}
2361 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2363 my $item = $builder->build_sample_item(
2365 library => $library->branchcode,
2369 my ( $error, $question, $alerts );
2370 my $issue = AddIssue( $patron1->unblessed, $item->barcode );
2372 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2373 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->barcode );
2374 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' );
2376 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 1);
2377 ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->barcode );
2378 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' );
2380 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2384 subtest 'AddReturn | is_overdue' => sub {
2385 plan tests => 8;
2387 t::lib::Mocks::mock_preference('MarkLostItemsAsReturned', 'batchmod|moredetail|cronjob|additem|pendingreserves|onpayment');
2388 t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
2389 t::lib::Mocks::mock_preference('finesMode', 'production');
2390 t::lib::Mocks::mock_preference('MaxFine', '100');
2392 my $library = $builder->build( { source => 'Branch' } );
2393 my $patron = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
2394 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2395 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2397 my $item = $builder->build_sample_item(
2399 library => $library->{branchcode},
2400 replacementprice => 7
2404 Koha::CirculationRules->search->delete;
2405 Koha::CirculationRules->set_rules(
2407 categorycode => undef,
2408 itemtype => undef,
2409 branchcode => undef,
2410 rules => {
2411 issuelength => 6,
2412 lengthunit => 'days',
2413 fine => 1, # Charge 1 every day of overdue
2414 chargeperiod => 1,
2419 my $now = dt_from_string;
2420 my $one_day_ago = $now->clone->subtract( days => 1 );
2421 my $two_days_ago = $now->clone->subtract( days => 2 );
2422 my $five_days_ago = $now->clone->subtract( days => 5 );
2423 my $ten_days_ago = $now->clone->subtract( days => 10 );
2424 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
2426 # No return date specified, today will be used => 10 days overdue charged
2427 AddIssue( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2428 AddReturn( $item->barcode, $library->{branchcode} );
2429 is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
2430 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2432 # specify return date 5 days before => no overdue charged
2433 AddIssue( $patron->unblessed, $item->barcode, $five_days_ago ); # date due was 5d ago
2434 AddReturn( $item->barcode, $library->{branchcode}, undef, $ten_days_ago );
2435 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2436 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2438 # specify return date 5 days later => 5 days overdue charged
2439 AddIssue( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2440 AddReturn( $item->barcode, $library->{branchcode}, undef, $five_days_ago );
2441 is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
2442 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2444 # specify return date 5 days later, specify exemptfine => no overdue charge
2445 AddIssue( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2446 AddReturn( $item->barcode, $library->{branchcode}, 1, $five_days_ago );
2447 is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2448 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2450 subtest 'bug 22877' => sub {
2452 plan tests => 3;
2454 my $issue = AddIssue( $patron->unblessed, $item->barcode, $ten_days_ago ); # date due was 10d ago
2456 # Fake fines cronjob on this checkout
2457 my ($fine) =
2458 CalcFine( $item, $patron->categorycode, $library->{branchcode},
2459 $ten_days_ago, $now );
2460 UpdateFine(
2462 issue_id => $issue->issue_id,
2463 itemnumber => $item->itemnumber,
2464 borrowernumber => $patron->borrowernumber,
2465 amount => $fine,
2466 due => output_pref($ten_days_ago)
2469 is( int( $patron->account->balance() ),
2470 10, "Overdue fine of 10 days overdue" );
2472 # Fake longoverdue with charge and not marking returned
2473 LostItem( $item->itemnumber, 'cronjob', 0 );
2474 is( int( $patron->account->balance() ),
2475 17, "Lost fine of 7 plus 10 days overdue" );
2477 # Now we return it today
2478 AddReturn( $item->barcode, $library->{branchcode} );
2479 is( int( $patron->account->balance() ),
2480 17, "Should have a single 10 days overdue fine and lost charge" );
2482 # Cleanup
2483 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2486 subtest 'bug 8338 | backdated return resulting in zero amount fine' => sub {
2488 plan tests => 17;
2490 t::lib::Mocks::mock_preference('CalculateFinesOnBackdate', 1);
2492 my $issue = AddIssue( $patron->unblessed, $item->barcode, $one_day_ago ); # date due was 1d ago
2494 # Fake fines cronjob on this checkout
2495 my ($fine) =
2496 CalcFine( $item, $patron->categorycode, $library->{branchcode},
2497 $one_day_ago, $now );
2498 UpdateFine(
2500 issue_id => $issue->issue_id,
2501 itemnumber => $item->itemnumber,
2502 borrowernumber => $patron->borrowernumber,
2503 amount => $fine,
2504 due => output_pref($one_day_ago)
2507 is( int( $patron->account->balance() ),
2508 1, "Overdue fine of 1 day overdue" );
2510 # Backdated return (dropbox mode example - charge should be removed)
2511 AddReturn( $item->barcode, $library->{branchcode}, 1, $one_day_ago );
2512 is( int( $patron->account->balance() ),
2513 0, "Overdue fine should be annulled" );
2514 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
2515 is( $lines->count, 0, "Overdue fine accountline has been removed");
2517 $issue = AddIssue( $patron->unblessed, $item->barcode, $two_days_ago ); # date due was 2d ago
2519 # Fake fines cronjob on this checkout
2520 ($fine) =
2521 CalcFine( $item, $patron->categorycode, $library->{branchcode},
2522 $two_days_ago, $now );
2523 UpdateFine(
2525 issue_id => $issue->issue_id,
2526 itemnumber => $item->itemnumber,
2527 borrowernumber => $patron->borrowernumber,
2528 amount => $fine,
2529 due => output_pref($one_day_ago)
2532 is( int( $patron->account->balance() ),
2533 2, "Overdue fine of 2 days overdue" );
2535 # Payment made against fine
2536 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
2537 my $debit = $lines->next;
2538 my $credit = $patron->account->add_credit(
2540 amount => 2,
2541 type => 'PAYMENT',
2542 interface => 'test',
2545 $credit->apply(
2546 { debits => [ $debit ], offset_type => 'Payment' } );
2548 is( int( $patron->account->balance() ),
2549 0, "Overdue fine should be paid off" );
2550 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
2551 is ( $lines->count, 2, "Overdue (debit) and Payment (credit) present");
2552 my $line = $lines->next;
2553 is( $line->amount+0, 2, "Overdue fine amount remains as 2 days");
2554 is( $line->amountoutstanding+0, 0, "Overdue fine amountoutstanding reduced to 0");
2556 # Backdated return (dropbox mode example - charge should be removed)
2557 AddReturn( $item->barcode, $library->{branchcode}, undef, $one_day_ago );
2558 is( int( $patron->account->balance() ),
2559 -1, "Refund credit has been applied" );
2560 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber }, { order_by => { '-asc' => 'accountlines_id' }});
2561 is( $lines->count, 3, "Overdue (debit), Payment (credit) and Refund (credit) are all present");
2563 $line = $lines->next;
2564 is($line->amount+0,1, "Overdue fine amount has been reduced to 1");
2565 is($line->amountoutstanding+0,0, "Overdue fine amount outstanding remains at 0");
2566 is($line->status,'RETURNED', "Overdue fine is fixed");
2567 $line = $lines->next;
2568 is($line->amount+0,-2, "Original payment amount remains as 2");
2569 is($line->amountoutstanding+0,0, "Original payment remains applied");
2570 $line = $lines->next;
2571 is($line->amount+0,-1, "Refund amount correctly set to 1");
2572 is($line->amountoutstanding+0,-1, "Refund amount outstanding unspent");
2574 # Cleanup
2575 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2578 subtest 'bug 25417 | backdated return + exemptfine' => sub {
2580 plan tests => 2;
2582 t::lib::Mocks::mock_preference('CalculateFinesOnBackdate', 1);
2584 my $issue = AddIssue( $patron->unblessed, $item->barcode, $one_day_ago ); # date due was 1d ago
2586 # Fake fines cronjob on this checkout
2587 my ($fine) =
2588 CalcFine( $item, $patron->categorycode, $library->{branchcode},
2589 $one_day_ago, $now );
2590 UpdateFine(
2592 issue_id => $issue->issue_id,
2593 itemnumber => $item->itemnumber,
2594 borrowernumber => $patron->borrowernumber,
2595 amount => $fine,
2596 due => output_pref($one_day_ago)
2599 is( int( $patron->account->balance() ),
2600 1, "Overdue fine of 1 day overdue" );
2602 # Backdated return (dropbox mode example - charge should no longer exist)
2603 AddReturn( $item->barcode, $library->{branchcode}, 1, $one_day_ago );
2604 is( int( $patron->account->balance() ),
2605 0, "Overdue fine should be annulled" );
2607 # Cleanup
2608 Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2611 subtest 'bug 24075 | backdated return with return datetime matching due datetime' => sub {
2612 plan tests => 7;
2614 t::lib::Mocks::mock_preference( 'CalculateFinesOnBackdate', 1 );
2616 my $due_date = dt_from_string;
2617 my $issue = AddIssue( $patron->unblessed, $item->barcode, $due_date );
2619 # Add fine
2620 UpdateFine(
2622 issue_id => $issue->issue_id,
2623 itemnumber => $item->itemnumber,
2624 borrowernumber => $patron->borrowernumber,
2625 amount => 0.25,
2626 due => output_pref($due_date)
2629 is( $patron->account->balance(),
2630 0.25, 'Overdue fine of $0.25 recorded' );
2632 # Backdate return to exact due date and time
2633 my ( undef, $message ) =
2634 AddReturn( $item->barcode, $library->{branchcode},
2635 undef, $due_date );
2637 my $accountline =
2638 Koha::Account::Lines->find( { issue_id => $issue->id } );
2639 ok( !$accountline, 'accountline removed as expected' );
2641 # Re-issue
2642 $issue = AddIssue( $patron->unblessed, $item->barcode, $due_date );
2644 # Add fine
2645 UpdateFine(
2647 issue_id => $issue->issue_id,
2648 itemnumber => $item->itemnumber,
2649 borrowernumber => $patron->borrowernumber,
2650 amount => .25,
2651 due => output_pref($due_date)
2654 is( $patron->account->balance(),
2655 0.25, 'Overdue fine of $0.25 recorded' );
2657 # Partial pay accruing fine
2658 my $lines = Koha::Account::Lines->search(
2660 borrowernumber => $patron->borrowernumber,
2661 issue_id => $issue->id
2664 my $debit = $lines->next;
2665 my $credit = $patron->account->add_credit(
2667 amount => .20,
2668 type => 'PAYMENT',
2669 interface => 'test',
2672 $credit->apply( { debits => [$debit], offset_type => 'Payment' } );
2674 is( $patron->account->balance(), .05, 'Overdue fine reduced to $0.05' );
2676 # Backdate return to exact due date and time
2677 ( undef, $message ) =
2678 AddReturn( $item->barcode, $library->{branchcode},
2679 undef, $due_date );
2681 $lines = Koha::Account::Lines->search(
2683 borrowernumber => $patron->borrowernumber,
2684 issue_id => $issue->id
2687 $accountline = $lines->next;
2688 is( $accountline->amountoutstanding + 0,
2689 0, 'Partially paid fee amount outstanding was reduced to 0' );
2690 is( $accountline->amount + 0,
2691 0, 'Partially paid fee amount was reduced to 0' );
2692 is( $patron->account->balance(), -0.20, 'Patron refund recorded' );
2694 # Cleanup
2695 Koha::Account::Lines->search(
2696 { borrowernumber => $patron->borrowernumber } )->delete;
2700 subtest '_FixOverduesOnReturn' => sub {
2701 plan tests => 14;
2703 my $manager = $builder->build_object({ class => "Koha::Patrons" });
2704 t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2706 my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
2708 my $branchcode = $library2->{branchcode};
2710 my $item = $builder->build_sample_item(
2712 biblionumber => $biblio->biblionumber,
2713 library => $branchcode,
2714 replacementprice => 99.00,
2715 itype => $itemtype,
2719 my $patron = $builder->build( { source => 'Borrower' } );
2721 ## Start with basic call, should just close out the open fine
2722 my $accountline = Koha::Account::Line->new(
2724 borrowernumber => $patron->{borrowernumber},
2725 debit_type_code => 'OVERDUE',
2726 status => 'UNRETURNED',
2727 itemnumber => $item->itemnumber,
2728 amount => 99.00,
2729 amountoutstanding => 99.00,
2730 interface => 'test',
2732 )->store();
2734 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, undef, 'RETURNED' );
2736 $accountline->_result()->discard_changes();
2738 is( $accountline->amountoutstanding+0, 99, 'Fine has the same amount outstanding as previously' );
2739 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2740 is( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
2742 ## Run again, with exemptfine enabled
2743 $accountline->set(
2745 debit_type_code => 'OVERDUE',
2746 status => 'UNRETURNED',
2747 amountoutstanding => 99.00,
2749 )->store();
2751 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
2753 $accountline->_result()->discard_changes();
2754 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2756 is( $accountline->amountoutstanding + 0, 0, 'Fine amountoutstanding has been reduced to 0' );
2757 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2758 is( $accountline->status, 'FORGIVEN', 'Open fine ( account type OVERDUE ) has been set to fine forgiven ( status FORGIVEN )');
2759 is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2760 is( $offset->amount + 0, -99, "Amount of offset is correct" );
2761 my $credit = $offset->credit;
2762 is( ref $credit, "Koha::Account::Line", "Found matching credit for fine forgiveness" );
2763 is( $credit->amount + 0, -99, "Credit amount is set correctly" );
2764 is( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
2766 # Bug 25417 - Only forgive fines where there is an amount outstanding to forgive
2767 $accountline->set(
2769 debit_type_code => 'OVERDUE',
2770 status => 'UNRETURNED',
2771 amountoutstanding => 0.00,
2773 )->store();
2774 $offset->delete;
2776 C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
2778 $accountline->_result()->discard_changes();
2779 $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2780 is( $offset, undef, "No offset created when trying to forgive fine with no outstanding balance" );
2781 isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
2782 is( $accountline->status, 'RETURNED', 'Passed status has been used to set as RETURNED )');
2785 subtest 'Set waiting flag' => sub {
2786 plan tests => 11;
2788 my $library_1 = $builder->build( { source => 'Branch' } );
2789 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2790 my $library_2 = $builder->build( { source => 'Branch' } );
2791 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2793 my $item = $builder->build_sample_item(
2795 library => $library_1->{branchcode},
2799 set_userenv( $library_2 );
2800 my $reserve_id = AddReserve(
2802 branchcode => $library_2->{branchcode},
2803 borrowernumber => $patron_2->{borrowernumber},
2804 biblionumber => $item->biblionumber,
2805 priority => 1,
2806 itemnumber => $item->itemnumber,
2810 set_userenv( $library_1 );
2811 my $do_transfer = 1;
2812 my ( $res, $rr ) = AddReturn( $item->barcode, $library_1->{branchcode} );
2813 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
2814 my $hold = Koha::Holds->find( $reserve_id );
2815 is( $hold->found, 'T', 'Hold is in transit' );
2817 my ( $status ) = CheckReserves($item->itemnumber);
2818 is( $status, 'Reserved', 'Hold is not waiting yet');
2820 set_userenv( $library_2 );
2821 $do_transfer = 0;
2822 AddReturn( $item->barcode, $library_2->{branchcode} );
2823 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
2824 $hold = Koha::Holds->find( $reserve_id );
2825 is( $hold->found, 'W', 'Hold is waiting' );
2826 ( $status ) = CheckReserves($item->itemnumber);
2827 is( $status, 'Waiting', 'Now the hold is waiting');
2829 #Bug 21944 - Waiting transfer checked in at branch other than pickup location
2830 set_userenv( $library_1 );
2831 (undef, my $messages, undef, undef ) = AddReturn ( $item->barcode, $library_1->{branchcode} );
2832 $hold = Koha::Holds->find( $reserve_id );
2833 is( $hold->found, undef, 'Hold is no longer marked waiting' );
2834 is( $hold->priority, 1, "Hold is now priority one again");
2835 is( $hold->waitingdate, undef, "Hold no longer has a waiting date");
2836 is( $hold->itemnumber, $item->itemnumber, "Hold has retained its' itemnumber");
2837 is( $messages->{ResFound}->{ResFound}, "Reserved", "Hold is still returned");
2838 is( $messages->{ResFound}->{found}, undef, "Hold is no longer marked found in return message");
2839 is( $messages->{ResFound}->{priority}, 1, "Hold is priority 1 in return message");
2842 subtest 'Cancel transfers on lost items' => sub {
2843 plan tests => 6;
2844 my $library_1 = $builder->build( { source => 'Branch' } );
2845 my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2846 my $library_2 = $builder->build( { source => 'Branch' } );
2847 my $patron_2 = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2848 my $biblio = $builder->build_sample_biblio({branchcode => $library->{branchcode}});
2849 my $item = $builder->build_sample_item({
2850 biblionumber => $biblio->biblionumber,
2851 library => $library_1->{branchcode},
2854 set_userenv( $library_2 );
2855 my $reserve_id = AddReserve(
2857 branchcode => $library_2->{branchcode},
2858 borrowernumber => $patron_2->{borrowernumber},
2859 biblionumber => $item->biblionumber,
2860 priority => 1,
2861 itemnumber => $item->itemnumber,
2865 #Return book and add transfer
2866 set_userenv( $library_1 );
2867 my $do_transfer = 1;
2868 my ( $res, $rr ) = AddReturn( $item->barcode, $library_1->{branchcode} );
2869 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
2870 C4::Circulation::transferbook({
2871 from_branch => $library_1->{branchcode},
2872 to_branch => $library_2->{branchcode},
2873 barcode => $item->barcode,
2875 my $hold = Koha::Holds->find( $reserve_id );
2876 is( $hold->found, 'T', 'Hold is in transit' );
2878 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2879 my ($datesent,$frombranch,$tobranch) = GetTransfers($item->itemnumber);
2880 is( $frombranch, $library_1->{branchcode}, 'The transfer is generated from the correct library');
2881 is( $tobranch, $library_2->{branchcode}, 'The transfer is generated to the correct library');
2882 my $itemcheck = Koha::Items->find($item->itemnumber);
2883 is( $itemcheck->holdingbranch, $library_1->{branchcode}, 'Items holding branch is the transfers origination branch before it is marked as lost' );
2885 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2886 $item->itemlost(1)->store;
2887 LostItem( $item->itemnumber, 'test', 1 );
2888 ($datesent,$frombranch,$tobranch) = GetTransfers($item->itemnumber);
2889 is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2890 $itemcheck = Koha::Items->find($item->itemnumber);
2891 is( $itemcheck->holdingbranch, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2895 subtest 'CanBookBeIssued | is_overdue' => sub {
2896 plan tests => 3;
2898 # Set a simple circ policy
2899 Koha::CirculationRules->set_rules(
2901 categorycode => undef,
2902 branchcode => undef,
2903 itemtype => undef,
2904 rules => {
2905 maxissueqty => 1,
2906 reservesallowed => 25,
2907 issuelength => 14,
2908 lengthunit => 'days',
2909 renewalsallowed => 1,
2910 renewalperiod => 7,
2911 norenewalbefore => undef,
2912 auto_renew => 0,
2913 fine => .10,
2914 chargeperiod => 1,
2919 my $now = dt_from_string;
2920 my $five_days_go = output_pref({ dt => $now->clone->add( days => 5 ), dateonly => 1});
2921 my $ten_days_go = output_pref({ dt => $now->clone->add( days => 10), dateonly => 1 });
2922 my $library = $builder->build( { source => 'Branch' } );
2923 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2925 my $item = $builder->build_sample_item(
2927 library => $library->{branchcode},
2931 my $issue = AddIssue( $patron->unblessed, $item->barcode, $five_days_go ); # date due was 10d ago
2932 my $actualissue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
2933 is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2934 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->barcode,$ten_days_go, undef, undef, undef);
2935 is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2936 is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2939 subtest 'ItemsDeniedRenewal preference' => sub {
2940 plan tests => 18;
2942 C4::Context->set_preference('ItemsDeniedRenewal','');
2944 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2945 Koha::CirculationRules->set_rules(
2947 categorycode => '*',
2948 itemtype => '*',
2949 branchcode => $idr_lib->branchcode,
2950 rules => {
2951 reservesallowed => 25,
2952 issuelength => 14,
2953 lengthunit => 'days',
2954 renewalsallowed => 10,
2955 renewalperiod => 7,
2956 norenewalbefore => undef,
2957 auto_renew => 0,
2958 fine => .10,
2959 chargeperiod => 1,
2964 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2965 homebranch => $idr_lib->branchcode,
2966 withdrawn => 1,
2967 itype => 'HIDE',
2968 location => 'PROC',
2969 itemcallnumber => undef,
2970 itemnotes => "",
2973 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2974 homebranch => $idr_lib->branchcode,
2975 withdrawn => 0,
2976 itype => 'NOHIDE',
2977 location => 'NOPROC'
2981 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2982 branchcode => $idr_lib->branchcode,
2985 my $future = dt_from_string->add( days => 1 );
2986 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2987 returndate => undef,
2988 renewals => 0,
2989 auto_renew => 0,
2990 borrowernumber => $idr_borrower->borrowernumber,
2991 itemnumber => $deny_book->itemnumber,
2992 onsite_checkout => 0,
2993 date_due => $future,
2996 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2997 returndate => undef,
2998 renewals => 0,
2999 auto_renew => 0,
3000 borrowernumber => $idr_borrower->borrowernumber,
3001 itemnumber => $allow_book->itemnumber,
3002 onsite_checkout => 0,
3003 date_due => $future,
3007 my $idr_rules;
3009 my ( $idr_mayrenew, $idr_error ) =
3010 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3011 is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
3012 is( $idr_error, undef, 'Renewal allowed when no rules' );
3014 $idr_rules="withdrawn: [1]";
3016 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3017 ( $idr_mayrenew, $idr_error ) =
3018 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3019 is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
3020 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
3021 ( $idr_mayrenew, $idr_error ) =
3022 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
3023 is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
3024 is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
3026 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
3028 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3029 ( $idr_mayrenew, $idr_error ) =
3030 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3031 is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
3032 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
3033 ( $idr_mayrenew, $idr_error ) =
3034 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
3035 is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
3036 is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
3038 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
3040 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3041 ( $idr_mayrenew, $idr_error ) =
3042 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3043 is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
3044 is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
3045 ( $idr_mayrenew, $idr_error ) =
3046 CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
3047 is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
3048 is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
3050 $idr_rules="itemcallnumber: [NULL]";
3051 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3052 ( $idr_mayrenew, $idr_error ) =
3053 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3054 is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
3055 $idr_rules="itemcallnumber: ['']";
3056 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3057 ( $idr_mayrenew, $idr_error ) =
3058 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3059 is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
3061 $idr_rules="itemnotes: [NULL]";
3062 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3063 ( $idr_mayrenew, $idr_error ) =
3064 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3065 is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
3066 $idr_rules="itemnotes: ['']";
3067 C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
3068 ( $idr_mayrenew, $idr_error ) =
3069 CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
3070 is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
3073 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
3074 plan tests => 2;
3076 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
3077 my $library = $builder->build( { source => 'Branch' } );
3078 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
3080 my $item = $builder->build_sample_item(
3082 library => $library->{branchcode},
3086 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3087 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
3088 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
3091 subtest 'CanBookBeIssued | notforloan' => sub {
3092 plan tests => 2;
3094 t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
3096 my $library = $builder->build( { source => 'Branch' } );
3097 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
3099 my $itemtype = $builder->build(
3101 source => 'Itemtype',
3102 value => { notforloan => undef, }
3105 my $item = $builder->build_sample_item(
3107 library => $library->{branchcode},
3108 itype => $itemtype->{itemtype},
3111 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
3113 my ( $issuingimpossible, $needsconfirmation );
3116 subtest 'item-level_itypes = 1' => sub {
3117 plan tests => 6;
3119 t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
3120 # Is for loan at item type and item level
3121 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3122 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
3123 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
3125 # not for loan at item type level
3126 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
3127 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3128 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3129 is_deeply(
3130 $issuingimpossible,
3131 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
3132 'Item can not be issued, not for loan at item type level'
3135 # not for loan at item level
3136 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
3137 $item->notforloan( 1 )->store;
3138 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3139 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3140 is_deeply(
3141 $issuingimpossible,
3142 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
3143 'Item can not be issued, not for loan at item type level'
3147 subtest 'item-level_itypes = 0' => sub {
3148 plan tests => 6;
3150 t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
3152 # We set another itemtype for biblioitem
3153 my $itemtype = $builder->build(
3155 source => 'Itemtype',
3156 value => { notforloan => undef, }
3160 # for loan at item type and item level
3161 $item->notforloan(0)->store;
3162 $item->biblioitem->itemtype($itemtype->{itemtype})->store;
3163 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3164 is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
3165 is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
3167 # not for loan at item type level
3168 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
3169 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3170 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3171 is_deeply(
3172 $issuingimpossible,
3173 { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
3174 'Item can not be issued, not for loan at item type level'
3177 # not for loan at item level
3178 Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
3179 $item->notforloan( 1 )->store;
3180 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3181 is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3182 is_deeply(
3183 $issuingimpossible,
3184 { NOT_FOR_LOAN => 1, item_notforloan => 1 },
3185 'Item can not be issued, not for loan at item type level'
3189 # TODO test with AllowNotForLoanOverride = 1
3192 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
3193 plan tests => 1;
3195 t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
3196 my $item = $builder->build_sample_item(
3198 onloan => '2018-01-01',
3202 AddReturn( $item->barcode, $item->homebranch );
3203 $item->discard_changes; # refresh
3204 is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
3208 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
3210 plan tests => 12;
3213 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3215 my $issuing_charges = 15;
3216 my $title = 'A title';
3217 my $author = 'Author, An';
3218 my $barcode = 'WHATARETHEODDS';
3220 my $circ = Test::MockModule->new('C4::Circulation');
3221 $circ->mock(
3222 'GetIssuingCharges',
3223 sub {
3224 return $issuing_charges;
3228 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3229 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
3230 my $patron = $builder->build_object({
3231 class => 'Koha::Patrons',
3232 value => { branchcode => $library->id }
3235 my $biblio = $builder->build_sample_biblio({ title=> $title, author => $author });
3236 my $item_id = Koha::Item->new(
3238 biblionumber => $biblio->biblionumber,
3239 homebranch => $library->id,
3240 holdingbranch => $library->id,
3241 barcode => $barcode,
3242 replacementprice => 23.00,
3243 itype => $itemtype->id
3245 )->store->itemnumber;
3246 my $item = Koha::Items->find( $item_id );
3248 my $context = Test::MockModule->new('C4::Context');
3249 $context->mock( userenv => { branch => $library->id } );
3251 # Check the item out
3252 AddIssue( $patron->unblessed, $item->barcode );
3253 t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
3254 my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
3255 my %params_renewal = (
3256 timestamp => { -like => $date . "%" },
3257 module => "CIRCULATION",
3258 action => "RENEWAL",
3260 my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
3261 AddRenewal( $patron->id, $item->id, $library->id );
3262 my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3263 is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
3265 my $checkouts = $patron->checkouts;
3266 # The following will fail if run on 00:00:00
3267 unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
3269 my $lines = Koha::Account::Lines->search({
3270 borrowernumber => $patron->id,
3271 itemnumber => $item->id
3274 is( $lines->count, 2 );
3276 my $line = $lines->next;
3277 is( $line->debit_type_code, 'RENT', 'The issue of item with issuing charge generates an accountline of the correct type' );
3278 is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
3279 is( $line->description, '', 'AddIssue does not set a hardcoded description for the accountline' );
3281 $line = $lines->next;
3282 is( $line->debit_type_code, 'RENT_RENEW', 'The renewal of item with issuing charge generates an accountline of the correct type' );
3283 is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
3284 is( $line->description, '', 'AddRenewal does not set a hardcoded description for the accountline' );
3286 t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
3288 $context = Test::MockModule->new('C4::Context');
3289 $context->mock( userenv => { branch => undef, interface => 'CRON'} ); #Test statistical logging of renewal via cron (atuo_renew)
3291 my $now = dt_from_string;
3292 $date = output_pref( { dt => $now, dateonly => 1, dateformat => 'iso' } );
3293 $old_log_size = Koha::ActionLogs->count( \%params_renewal );
3294 my $sth = $dbh->prepare("SELECT COUNT(*) FROM statistics WHERE itemnumber = ? AND branch = ?");
3295 $sth->execute($item->id, $library->id);
3296 my ($old_stats_size) = $sth->fetchrow_array;
3297 AddRenewal( $patron->id, $item->id, $library->id );
3298 $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3299 $sth->execute($item->id, $library->id);
3300 my ($new_stats_size) = $sth->fetchrow_array;
3301 is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
3302 is( $new_stats_size, $old_stats_size + 1, 'renew statistic successfully added with passed branch' );
3304 AddReturn( $item->id, $library->id, undef, $date );
3305 AddIssue( $patron->unblessed, $item->barcode, $now );
3306 AddRenewal( $patron->id, $item->id, $library->id, undef, undef, 1 );
3307 my $lines_skipped = Koha::Account::Lines->search({
3308 borrowernumber => $patron->id,
3309 itemnumber => $item->id
3311 is( $lines_skipped->count, 5, 'Passing skipfinecalc causes fine calculation on renewal to be skipped' );
3315 subtest 'ProcessOfflinePayment() tests' => sub {
3317 plan tests => 4;
3320 my $amount = 123;
3322 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
3323 my $library = $builder->build_object({ class => 'Koha::Libraries' });
3324 my $result = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
3326 is( $result, 'Success.', 'The right string is returned' );
3328 my $lines = $patron->account->lines;
3329 is( $lines->count, 1, 'line created correctly');
3331 my $line = $lines->next;
3332 is( $line->amount+0, $amount * -1, 'amount picked from params' );
3333 is( $line->branchcode, $library->id, 'branchcode set correctly' );
3337 subtest 'Incremented fee tests' => sub {
3338 plan tests => 19;
3340 my $dt = dt_from_string();
3341 Time::Fake->offset( $dt->epoch );
3343 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
3345 my $library =
3346 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3348 my $module = Test::MockModule->new('C4::Context');
3349 $module->mock( 'userenv', sub { { branch => $library->id } } );
3351 my $patron = $builder->build_object(
3353 class => 'Koha::Patrons',
3354 value => { categorycode => $patron_category->{categorycode} }
3356 )->store;
3358 my $itemtype = $builder->build_object(
3360 class => 'Koha::ItemTypes',
3361 value => {
3362 notforloan => undef,
3363 rentalcharge => 0,
3364 rentalcharge_daily => 1,
3365 rentalcharge_daily_calendar => 0
3368 )->store;
3370 my $item = $builder->build_sample_item(
3372 library => $library->{branchcode},
3373 itype => $itemtype->id,
3377 is( $itemtype->rentalcharge_daily+0,
3378 1, 'Daily rental charge stored and retreived correctly' );
3379 is( $item->effective_itemtype, $itemtype->id,
3380 "Itemtype set correctly for item" );
3382 my $now = dt_from_string;
3383 my $dt_from = $now->clone;
3384 my $dt_to = $now->clone->add( days => 7 );
3385 my $dt_to_renew = $now->clone->add( days => 13 );
3387 # Daily Tests
3388 my $issue =
3389 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3390 my $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3391 is( $accountline->amount+0, 7,
3392 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0"
3394 $accountline->delete();
3395 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3396 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3397 is( $accountline->amount+0, 6,
3398 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 0, for renewal"
3400 $accountline->delete();
3401 $issue->delete();
3403 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
3404 $itemtype->rentalcharge_daily_calendar(1)->store();
3405 $issue =
3406 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3407 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3408 is( $accountline->amount+0, 7,
3409 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1"
3411 $accountline->delete();
3412 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3413 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3414 is( $accountline->amount+0, 6,
3415 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1, for renewal"
3417 $accountline->delete();
3418 $issue->delete();
3420 my $calendar = C4::Calendar->new( branchcode => $library->id );
3421 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
3422 my $closed_day =
3423 ( $dt_from->day_of_week == 6 ) ? 0
3424 : ( $dt_from->day_of_week == 7 ) ? 1
3425 : $dt_from->day_of_week + 1;
3426 my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
3427 $calendar->insert_week_day_holiday(
3428 weekday => $closed_day,
3429 title => 'Test holiday',
3430 description => 'Test holiday'
3432 $issue =
3433 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3434 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3435 is( $accountline->amount+0, 6,
3436 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name"
3438 $accountline->delete();
3439 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3440 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3441 is( $accountline->amount+0, 5,
3442 "Daily rental charge calculated correctly with rentalcharge_daily_calendar = 1 and closed $closed_day_name, for renewal"
3444 $accountline->delete();
3445 $issue->delete();
3447 $itemtype->rentalcharge(2)->store;
3448 is( $itemtype->rentalcharge+0, 2,
3449 'Rental charge updated and retreived correctly' );
3450 $issue =
3451 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3452 my $accountlines =
3453 Koha::Account::Lines->search( { itemnumber => $item->id } );
3454 is( $accountlines->count, '2',
3455 "Fixed charge and accrued charge recorded distinctly" );
3456 $accountlines->delete();
3457 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3458 $accountlines = Koha::Account::Lines->search( { itemnumber => $item->id } );
3459 is( $accountlines->count, '2',
3460 "Fixed charge and accrued charge recorded distinctly, for renewal" );
3461 $accountlines->delete();
3462 $issue->delete();
3463 $itemtype->rentalcharge(0)->store;
3464 is( $itemtype->rentalcharge+0, 0,
3465 'Rental charge reset and retreived correctly' );
3467 # Hourly
3468 Koha::CirculationRules->set_rule(
3470 categorycode => $patron->categorycode,
3471 itemtype => $itemtype->id,
3472 branchcode => $library->id,
3473 rule_name => 'lengthunit',
3474 rule_value => 'hours',
3478 $itemtype->rentalcharge_hourly('0.25')->store();
3479 is( $itemtype->rentalcharge_hourly,
3480 '0.25', 'Hourly rental charge stored and retreived correctly' );
3482 $dt_to = $now->clone->add( hours => 168 );
3483 $dt_to_renew = $now->clone->add( hours => 312 );
3485 $itemtype->rentalcharge_hourly_calendar(0)->store();
3486 $issue =
3487 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3488 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3489 is( $accountline->amount + 0, 42,
3490 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0 (168h * 0.25u)" );
3491 $accountline->delete();
3492 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3493 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3494 is( $accountline->amount + 0, 36,
3495 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 0, for renewal (312h - 168h * 0.25u)" );
3496 $accountline->delete();
3497 $issue->delete();
3499 $itemtype->rentalcharge_hourly_calendar(1)->store();
3500 $issue =
3501 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3502 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3503 is( $accountline->amount + 0, 36,
3504 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name (168h - 24h * 0.25u)" );
3505 $accountline->delete();
3506 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3507 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3508 is( $accountline->amount + 0, 30,
3509 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 and closed $closed_day_name, for renewal (312h - 168h - 24h * 0.25u" );
3510 $accountline->delete();
3511 $issue->delete();
3513 $calendar->delete_holiday( weekday => $closed_day );
3514 $issue =
3515 AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3516 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3517 is( $accountline->amount + 0, 42,
3518 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1 (168h - 0h * 0.25u" );
3519 $accountline->delete();
3520 AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3521 $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
3522 is( $accountline->amount + 0, 36,
3523 "Hourly rental charge calculated correctly with rentalcharge_hourly_calendar = 1, for renewal (312h - 168h - 0h * 0.25u)" );
3524 $accountline->delete();
3525 $issue->delete();
3526 Time::Fake->reset;
3529 subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
3530 plan tests => 2;
3532 t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
3533 t::lib::Mocks::mock_preference('item-level_itypes', 1);
3535 my $library =
3536 $builder->build_object( { class => 'Koha::Libraries' } )->store;
3537 my $patron = $builder->build_object(
3539 class => 'Koha::Patrons',
3540 value => { categorycode => $patron_category->{categorycode} }
3542 )->store;
3544 my $itemtype = $builder->build_object(
3546 class => 'Koha::ItemTypes',
3547 value => {
3548 notforloan => 0,
3549 rentalcharge => 0,
3550 rentalcharge_daily => 0
3555 my $item = $builder->build_sample_item(
3557 library => $library->id,
3558 notforloan => 0,
3559 itemlost => 0,
3560 withdrawn => 0,
3561 itype => $itemtype->id,
3563 )->store;
3565 my ( $issuingimpossible, $needsconfirmation );
3566 my $dt_from = dt_from_string();
3567 my $dt_due = $dt_from->clone->add( days => 3 );
3569 $itemtype->rentalcharge(1)->store;
3570 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3571 is_deeply( $needsconfirmation, { RENTALCHARGE => '1.00' }, 'Item needs rentalcharge confirmation to be issued' );
3572 $itemtype->rentalcharge('0')->store;
3573 $itemtype->rentalcharge_daily(1)->store;
3574 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3575 is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
3576 $itemtype->rentalcharge_daily('0')->store;
3579 subtest 'CanBookBeIssued & CircConfirmItemParts' => sub {
3580 plan tests => 1;
3582 t::lib::Mocks::mock_preference('CircConfirmItemParts', 1);
3584 my $patron = $builder->build_object(
3586 class => 'Koha::Patrons',
3587 value => { categorycode => $patron_category->{categorycode} }
3589 )->store;
3591 my $item = $builder->build_sample_item(
3593 materials => 'includes DVD',
3595 )->store;
3597 my $dt_due = dt_from_string->add( days => 3 );
3599 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3600 is_deeply( $needsconfirmation, { ADDITIONAL_MATERIALS => 'includes DVD' }, 'Item needs confirmation of additional parts' );
3603 subtest 'Do not return on renewal (LOST charge)' => sub {
3604 plan tests => 1;
3606 t::lib::Mocks::mock_preference('MarkLostItemsAsReturned', 'onpayment');
3607 my $library = $builder->build_object( { class => "Koha::Libraries" } );
3608 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
3609 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
3611 my $biblio = $builder->build_sample_biblio;
3613 my $item = $builder->build_sample_item(
3615 biblionumber => $biblio->biblionumber,
3616 library => $library->branchcode,
3617 replacementprice => 99.00,
3618 itype => $itemtype,
3622 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3623 AddIssue( $patron->unblessed, $item->barcode );
3625 my $accountline = Koha::Account::Line->new(
3627 borrowernumber => $patron->borrowernumber,
3628 debit_type_code => 'LOST',
3629 status => undef,
3630 itemnumber => $item->itemnumber,
3631 amount => 12,
3632 amountoutstanding => 12,
3633 interface => 'something',
3635 )->store();
3637 # AddRenewal doesn't call _FixAccountForLostAndFound
3638 AddIssue( $patron->unblessed, $item->barcode );
3640 is( $patron->checkouts->count, 1,
3641 'Renewal should not return the item even if a LOST payment has been made earlier'
3645 subtest 'Filling a hold should cancel existing transfer' => sub {
3646 plan tests => 4;
3648 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
3650 my $libraryA = $builder->build_object( { class => 'Koha::Libraries' } );
3651 my $libraryB = $builder->build_object( { class => 'Koha::Libraries' } );
3652 my $patron = $builder->build_object(
3654 class => 'Koha::Patrons',
3655 value => {
3656 categorycode => $patron_category->{categorycode},
3657 branchcode => $libraryA->branchcode,
3660 )->store;
3662 my $item = $builder->build_sample_item({
3663 homebranch => $libraryB->branchcode,
3666 my ( undef, $message ) = AddReturn( $item->barcode, $libraryA->branchcode, undef, undef );
3667 is( Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef })->count, 1, "We generate a transfer on checkin");
3668 AddReserve({
3669 branchcode => $libraryA->branchcode,
3670 borrowernumber => $patron->borrowernumber,
3671 biblionumber => $item->biblionumber,
3672 itemnumber => $item->itemnumber
3674 my $reserves = Koha::Holds->search({ itemnumber => $item->itemnumber });
3675 is( $reserves->count, 1, "Reserve is placed");
3676 ( undef, $message ) = AddReturn( $item->barcode, $libraryA->branchcode, undef, undef );
3677 my $reserve = $reserves->next;
3678 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 0, $reserve->reserve_id );
3679 $reserve->discard_changes;
3680 ok( $reserve->found eq 'W', "Reserve is marked waiting" );
3681 is( Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef })->count, 0, "No outstanding transfers when hold is waiting");
3684 subtest 'Tests for NoRefundOnLostReturnedItemsAge with AddReturn' => sub {
3686 plan tests => 4;
3688 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
3689 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
3690 my $patron = $builder->build_object(
3692 class => 'Koha::Patrons',
3693 value => { categorycode => $patron_category->{categorycode} }
3697 my $biblionumber = $builder->build_sample_biblio(
3699 branchcode => $library->branchcode,
3701 )->biblionumber;
3703 # And the circulation rule
3704 Koha::CirculationRules->search->delete;
3705 Koha::CirculationRules->set_rules(
3707 categorycode => undef,
3708 itemtype => undef,
3709 branchcode => undef,
3710 rules => {
3711 issuelength => 14,
3712 lengthunit => 'days',
3716 $builder->build(
3718 source => 'CirculationRule',
3719 value => {
3720 branchcode => undef,
3721 categorycode => undef,
3722 itemtype => undef,
3723 rule_name => 'refund',
3724 rule_value => 1
3729 subtest 'NoRefundOnLostReturnedItemsAge = undef' => sub {
3730 plan tests => 3;
3732 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3733 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef );
3735 my $lost_on = dt_from_string->subtract( days => 7 )->date;
3737 my $item = $builder->build_sample_item(
3739 biblionumber => $biblionumber,
3740 library => $library->branchcode,
3741 replacementprice => '42',
3744 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3745 LostItem( $item->itemnumber, 'cli', 0 );
3746 $item->_result->itemlost(1);
3747 $item->_result->itemlost_on( $lost_on );
3748 $item->_result->update();
3750 my $a = Koha::Account::Lines->search(
3752 itemnumber => $item->id,
3753 borrowernumber => $patron->borrowernumber
3755 )->next;
3756 ok( $a, "Found accountline for lost fee" );
3757 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3758 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
3759 $a = $a->get_from_storage;
3760 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3761 $a->delete;
3764 subtest 'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
3765 plan tests => 3;
3767 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3768 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
3770 my $lost_on = dt_from_string->subtract( days => 6 )->date;
3772 my $item = $builder->build_sample_item(
3774 biblionumber => $biblionumber,
3775 library => $library->branchcode,
3776 replacementprice => '42',
3779 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3780 LostItem( $item->itemnumber, 'cli', 0 );
3781 $item->_result->itemlost(1);
3782 $item->_result->itemlost_on( $lost_on );
3783 $item->_result->update();
3785 my $a = Koha::Account::Lines->search(
3787 itemnumber => $item->id,
3788 borrowernumber => $patron->borrowernumber
3790 )->next;
3791 ok( $a, "Found accountline for lost fee" );
3792 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3793 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
3794 $a = $a->get_from_storage;
3795 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3796 $a->delete;
3799 subtest 'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
3800 plan tests => 3;
3802 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3803 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
3805 my $lost_on = dt_from_string->subtract( days => 7 )->date;
3807 my $item = $builder->build_sample_item(
3809 biblionumber => $biblionumber,
3810 library => $library->branchcode,
3811 replacementprice => '42',
3814 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3815 LostItem( $item->itemnumber, 'cli', 0 );
3816 $item->_result->itemlost(1);
3817 $item->_result->itemlost_on( $lost_on );
3818 $item->_result->update();
3820 my $a = Koha::Account::Lines->search(
3822 itemnumber => $item->id,
3823 borrowernumber => $patron->borrowernumber
3825 )->next;
3826 ok( $a, "Found accountline for lost fee" );
3827 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3828 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
3829 $a = $a->get_from_storage;
3830 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
3831 $a->delete;
3834 subtest 'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
3835 plan tests => 3;
3837 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3838 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
3840 my $lost_on = dt_from_string->subtract( days => 8 )->date;
3842 my $item = $builder->build_sample_item(
3844 biblionumber => $biblionumber,
3845 library => $library->branchcode,
3846 replacementprice => '42',
3849 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3850 LostItem( $item->itemnumber, 'cli', 0 );
3851 $item->_result->itemlost(1);
3852 $item->_result->itemlost_on( $lost_on );
3853 $item->_result->update();
3855 my $a = Koha::Account::Lines->search(
3857 itemnumber => $item->id,
3858 borrowernumber => $patron->borrowernumber
3861 $a = $a->next;
3862 ok( $a, "Found accountline for lost fee" );
3863 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3864 my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string );
3865 $a = $a->get_from_storage;
3866 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
3867 $a->delete;
3871 subtest 'Tests for NoRefundOnLostReturnedItemsAge with AddIssue' => sub {
3873 plan tests => 4;
3875 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
3876 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
3877 my $patron = $builder->build_object(
3879 class => 'Koha::Patrons',
3880 value => { categorycode => $patron_category->{categorycode} }
3883 my $patron2 = $builder->build_object(
3885 class => 'Koha::Patrons',
3886 value => { categorycode => $patron_category->{categorycode} }
3890 my $biblionumber = $builder->build_sample_biblio(
3892 branchcode => $library->branchcode,
3894 )->biblionumber;
3896 # And the circulation rule
3897 Koha::CirculationRules->search->delete;
3898 Koha::CirculationRules->set_rules(
3900 categorycode => undef,
3901 itemtype => undef,
3902 branchcode => undef,
3903 rules => {
3904 issuelength => 14,
3905 lengthunit => 'days',
3909 $builder->build(
3911 source => 'CirculationRule',
3912 value => {
3913 branchcode => undef,
3914 categorycode => undef,
3915 itemtype => undef,
3916 rule_name => 'refund',
3917 rule_value => 1
3922 subtest 'NoRefundOnLostReturnedItemsAge = undef' => sub {
3923 plan tests => 3;
3925 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3926 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef );
3928 my $lost_on = dt_from_string->subtract( days => 7 )->date;
3930 my $item = $builder->build_sample_item(
3932 biblionumber => $biblionumber,
3933 library => $library->branchcode,
3934 replacementprice => '42',
3937 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3938 LostItem( $item->itemnumber, 'cli', 0 );
3939 $item->_result->itemlost(1);
3940 $item->_result->itemlost_on( $lost_on );
3941 $item->_result->update();
3943 my $a = Koha::Account::Lines->search(
3945 itemnumber => $item->id,
3946 borrowernumber => $patron->borrowernumber
3948 )->next;
3949 ok( $a, "Found accountline for lost fee" );
3950 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3951 $issue = AddIssue( $patron2->unblessed, $item->barcode );
3952 $a = $a->get_from_storage;
3953 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3954 $a->delete;
3955 $issue->delete;
3958 subtest 'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub {
3959 plan tests => 3;
3961 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3962 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
3964 my $lost_on = dt_from_string->subtract( days => 6 )->date;
3966 my $item = $builder->build_sample_item(
3968 biblionumber => $biblionumber,
3969 library => $library->branchcode,
3970 replacementprice => '42',
3973 my $issue = AddIssue( $patron->unblessed, $item->barcode );
3974 LostItem( $item->itemnumber, 'cli', 0 );
3975 $item->_result->itemlost(1);
3976 $item->_result->itemlost_on( $lost_on );
3977 $item->_result->update();
3979 my $a = Koha::Account::Lines->search(
3981 itemnumber => $item->id,
3982 borrowernumber => $patron->borrowernumber
3984 )->next;
3985 ok( $a, "Found accountline for lost fee" );
3986 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
3987 $issue = AddIssue( $patron2->unblessed, $item->barcode );
3988 $a = $a->get_from_storage;
3989 is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" );
3990 $a->delete;
3993 subtest 'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub {
3994 plan tests => 3;
3996 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
3997 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
3999 my $lost_on = dt_from_string->subtract( days => 7 )->date;
4001 my $item = $builder->build_sample_item(
4003 biblionumber => $biblionumber,
4004 library => $library->branchcode,
4005 replacementprice => '42',
4008 my $issue = AddIssue( $patron->unblessed, $item->barcode );
4009 LostItem( $item->itemnumber, 'cli', 0 );
4010 $item->_result->itemlost(1);
4011 $item->_result->itemlost_on( $lost_on );
4012 $item->_result->update();
4014 my $a = Koha::Account::Lines->search(
4016 itemnumber => $item->id,
4017 borrowernumber => $patron->borrowernumber
4019 )->next;
4020 ok( $a, "Found accountline for lost fee" );
4021 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
4022 $issue = AddIssue( $patron2->unblessed, $item->barcode );
4023 $a = $a->get_from_storage;
4024 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
4025 $a->delete;
4028 subtest 'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub {
4029 plan tests => 3;
4031 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
4032 t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 );
4034 my $lost_on = dt_from_string->subtract( days => 8 )->date;
4036 my $item = $builder->build_sample_item(
4038 biblionumber => $biblionumber,
4039 library => $library->branchcode,
4040 replacementprice => '42',
4043 my $issue = AddIssue( $patron->unblessed, $item->barcode );
4044 LostItem( $item->itemnumber, 'cli', 0 );
4045 $item->_result->itemlost(1);
4046 $item->_result->itemlost_on( $lost_on );
4047 $item->_result->update();
4049 my $a = Koha::Account::Lines->search(
4051 itemnumber => $item->id,
4052 borrowernumber => $patron->borrowernumber
4055 $a = $a->next;
4056 ok( $a, "Found accountline for lost fee" );
4057 is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" );
4058 $issue = AddIssue( $patron2->unblessed, $item->barcode );
4059 $a = $a->get_from_storage;
4060 is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" );
4061 $a->delete;
4065 subtest 'transferbook tests' => sub {
4066 plan tests => 9;
4068 throws_ok
4069 { C4::Circulation::transferbook({}); }
4070 'Koha::Exceptions::MissingParameter',
4071 'Koha::Patron->store raises an exception on missing params';
4073 throws_ok
4074 { C4::Circulation::transferbook({to_branch=>'anything'}); }
4075 'Koha::Exceptions::MissingParameter',
4076 'Koha::Patron->store raises an exception on missing params';
4078 throws_ok
4079 { C4::Circulation::transferbook({from_branch=>'anything'}); }
4080 'Koha::Exceptions::MissingParameter',
4081 'Koha::Patron->store raises an exception on missing params';
4083 my ($doreturn,$messages) = C4::Circulation::transferbook({to_branch=>'there',from_branch=>'here'});
4084 is( $doreturn, 0, "No return without barcode");
4085 ok( exists $messages->{BadBarcode}, "We get a BadBarcode message if no barcode passed");
4086 is( $messages->{BadBarcode}, undef, "No barcode passed means undef BadBarcode" );
4088 ($doreturn,$messages) = C4::Circulation::transferbook({to_branch=>'there',from_branch=>'here',barcode=>'BadBarcode'});
4089 is( $doreturn, 0, "No return without barcode");
4090 ok( exists $messages->{BadBarcode}, "We get a BadBarcode message if no barcode passed");
4091 is( $messages->{BadBarcode}, 'BadBarcode', "No barcode passed means undef BadBarcode" );
4095 subtest 'Checkout should correctly terminate a transfer' => sub {
4096 plan tests => 7;
4098 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
4099 my $patron_1 = $builder->build_object(
4101 class => 'Koha::Patrons',
4102 value => { branchcode => $library_1->branchcode }
4105 my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
4106 my $patron_2 = $builder->build_object(
4108 class => 'Koha::Patrons',
4109 value => { branchcode => $library_2->branchcode }
4113 my $item = $builder->build_sample_item(
4115 library => $library_1->branchcode,
4119 t::lib::Mocks::mock_userenv( { branchcode => $library_1->branchcode } );
4120 my $reserve_id = AddReserve(
4122 branchcode => $library_2->branchcode,
4123 borrowernumber => $patron_2->borrowernumber,
4124 biblionumber => $item->biblionumber,
4125 itemnumber => $item->itemnumber,
4126 priority => 1,
4130 my $do_transfer = 1;
4131 ModItemTransfer( $item->itemnumber, $library_1->branchcode,
4132 $library_2->branchcode );
4133 ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
4134 GetOtherReserves( $item->itemnumber )
4135 ; # To put the Reason, it's what does returns.pl...
4136 my $hold = Koha::Holds->find($reserve_id);
4137 is( $hold->found, 'T', 'Hold is in transit' );
4138 my $transfer = $item->get_transfer;
4139 is( $transfer->frombranch, $library_1->branchcode );
4140 is( $transfer->tobranch, $library_2->branchcode );
4141 is( $transfer->reason, 'Reserve' );
4143 t::lib::Mocks::mock_userenv( { branchcode => $library_2->branchcode } );
4144 AddIssue( $patron_1->unblessed, $item->barcode );
4145 $transfer = $transfer->get_from_storage;
4146 isnt( $transfer->datearrived, undef );
4147 $hold = $hold->get_from_storage;
4148 is( $hold->found, undef, 'Hold is waiting' );
4149 is( $hold->priority, 1, );
4152 $schema->storage->txn_rollback;
4153 C4::Context->clear_syspref_cache();
4154 $branches = Koha::Libraries->search();
4155 for my $branch ( $branches->next ) {
4156 my $key = $branch->branchcode . "_holidays";
4157 $cache->clear_from_cache($key);