3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Test
::More tests
=> 124;
26 use POSIX
qw( floor );
28 use t
::lib
::TestBuilder
;
37 use C4
::Overdues
qw(UpdateFine CalcFine);
40 use Koha
::IssuingRules
;
44 use Koha
::Subscriptions
;
45 use Koha
::Account
::Lines
;
46 use Koha
::Account
::Offsets
;
48 my $schema = Koha
::Database
->schema;
49 $schema->storage->txn_begin;
50 my $builder = t
::lib
::TestBuilder
->new;
51 my $dbh = C4
::Context
->dbh;
54 $dbh->{RaiseError
} = 1;
56 my $cache = Koha
::Caches
->get_instance();
57 $dbh->do(q
|DELETE FROM special_holidays
|);
58 $dbh->do(q
|DELETE FROM repeatable_holidays
|);
59 $cache->clear_from_cache('single_holidays');
61 # Start with a clean slate
62 $dbh->do('DELETE FROM issues');
63 $dbh->do('DELETE FROM borrowers');
65 my $library = $builder->build({
68 my $library2 = $builder->build({
71 my $itemtype = $builder->build(
72 { source
=> 'Itemtype',
73 value
=> { notforloan
=> undef, rentalcharge
=> 0, defaultreplacecost
=> undef, processfee
=> undef }
76 my $patron_category = $builder->build(
82 BlockExpiredPatronOpacActions
=> -1, # Pick the pref value
87 my $CircControl = C4
::Context
->preference('CircControl');
88 my $HomeOrHoldingBranch = C4
::Context
->preference('HomeOrHoldingBranch');
91 homebranch
=> $library2->{branchcode
},
92 holdingbranch
=> $library2->{branchcode
}
96 branchcode
=> $library2->{branchcode
}
99 # No userenv, PickupLibrary
100 t
::lib
::Mocks
::mock_preference
('IndependentBranches', '0');
101 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
103 C4
::Context
->preference('CircControl'),
105 'CircControl changed to PickupLibrary'
108 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
109 $item->{$HomeOrHoldingBranch},
110 '_GetCircControlBranch returned item branch (no userenv defined)'
113 # No userenv, PatronLibrary
114 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
116 C4
::Context
->preference('CircControl'),
118 'CircControl changed to PatronLibrary'
121 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
122 $borrower->{branchcode
},
123 '_GetCircControlBranch returned borrower branch'
126 # No userenv, ItemHomeLibrary
127 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
129 C4
::Context
->preference('CircControl'),
131 'CircControl changed to ItemHomeLibrary'
134 $item->{$HomeOrHoldingBranch},
135 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
136 '_GetCircControlBranch returned item branch'
140 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library2->{branchcode
} });
141 is
(C4
::Context
->userenv->{branch
}, $library2->{branchcode
}, 'userenv set');
143 # Userenv set, PickupLibrary
144 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
146 C4
::Context
->preference('CircControl'),
148 'CircControl changed to PickupLibrary'
151 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
152 $library2->{branchcode
},
153 '_GetCircControlBranch returned current branch'
156 # Userenv set, PatronLibrary
157 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
159 C4
::Context
->preference('CircControl'),
161 'CircControl changed to PatronLibrary'
164 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
165 $borrower->{branchcode
},
166 '_GetCircControlBranch returned borrower branch'
169 # Userenv set, ItemHomeLibrary
170 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
172 C4
::Context
->preference('CircControl'),
174 'CircControl changed to ItemHomeLibrary'
177 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
178 $item->{$HomeOrHoldingBranch},
179 '_GetCircControlBranch returned item branch'
182 # Reset initial configuration
183 t
::lib
::Mocks
::mock_preference
('CircControl', $CircControl);
185 C4
::Context
->preference('CircControl'),
187 'CircControl reset to its initial value'
190 # Set a simple circ policy
191 $dbh->do('DELETE FROM issuingrules');
193 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
194 maxissueqty, issuelength, lengthunit,
195 renewalsallowed, renewalperiod,
196 norenewalbefore, auto_renew,
213 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
215 # CanBookBeRenewed tests
216 C4
::Context
->set_preference('ItemsDeniedRenewal','');
217 # Generate test biblio
218 my $biblio = $builder->build_sample_biblio();
220 my $branch = $library2->{branchcode
};
222 my $item_1 = $builder->build_sample_item(
224 biblionumber
=> $biblio->biblionumber,
226 replacementprice
=> 12.00,
230 $reused_itemnumber_1 = $item_1->itemnumber;
232 my $item_2 = $builder->build_sample_item(
234 biblionumber
=> $biblio->biblionumber,
236 replacementprice
=> 23.00,
240 $reused_itemnumber_2 = $item_2->itemnumber;
242 my $item_3 = $builder->build_sample_item(
244 biblionumber
=> $biblio->biblionumber,
246 replacementprice
=> 23.00,
252 my %renewing_borrower_data = (
254 surname
=> 'Renewal',
255 categorycode
=> $patron_category->{categorycode
},
256 branchcode
=> $branch,
259 my %reserving_borrower_data = (
260 firstname
=> 'Katrin',
261 surname
=> 'Reservation',
262 categorycode
=> $patron_category->{categorycode
},
263 branchcode
=> $branch,
266 my %hold_waiting_borrower_data = (
268 surname
=> 'Reservation',
269 categorycode
=> $patron_category->{categorycode
},
270 branchcode
=> $branch,
273 my %restricted_borrower_data = (
274 firstname
=> 'Alice',
275 surname
=> 'Reservation',
276 categorycode
=> $patron_category->{categorycode
},
277 debarred
=> '3228-01-01',
278 branchcode
=> $branch,
281 my %expired_borrower_data = (
284 categorycode
=> $patron_category->{categorycode
},
285 branchcode
=> $branch,
286 dateexpiry
=> dt_from_string
->subtract( months
=> 1 ),
289 my $renewing_borrowernumber = Koha
::Patron
->new(\
%renewing_borrower_data)->store->borrowernumber;
290 my $reserving_borrowernumber = Koha
::Patron
->new(\
%reserving_borrower_data)->store->borrowernumber;
291 my $hold_waiting_borrowernumber = Koha
::Patron
->new(\
%hold_waiting_borrower_data)->store->borrowernumber;
292 my $restricted_borrowernumber = Koha
::Patron
->new(\
%restricted_borrower_data)->store->borrowernumber;
293 my $expired_borrowernumber = Koha
::Patron
->new(\
%expired_borrower_data)->store->borrowernumber;
295 my $renewing_borrower = Koha
::Patrons
->find( $renewing_borrowernumber )->unblessed;
296 my $restricted_borrower = Koha
::Patrons
->find( $restricted_borrowernumber )->unblessed;
297 my $expired_borrower = Koha
::Patrons
->find( $expired_borrowernumber )->unblessed;
304 my $checkitem = undef;
307 my $issue = AddIssue
( $renewing_borrower, $item_1->barcode);
308 my $datedue = dt_from_string
( $issue->date_due() );
309 is
(defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
311 my $issue2 = AddIssue
( $renewing_borrower, $item_2->barcode);
312 $datedue = dt_from_string
( $issue->date_due() );
313 is
(defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
316 my $borrowing_borrowernumber = Koha
::Checkouts
->find( { itemnumber
=> $item_1->itemnumber } )->borrowernumber;
317 is
($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
319 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
320 is
( $renewokay, 1, 'Can renew, no holds for this title or item');
323 # Biblio-level hold, renewal test
325 $branch, $reserving_borrowernumber, $biblio->biblionumber,
326 $bibitems, $priority, $resdate, $expdate, $notes,
327 'a title', $checkitem, $found
330 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
331 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
332 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 1 );
333 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
334 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
335 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
336 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
338 # Now let's add an item level hold, we should no longer be able to renew the item
339 my $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
341 borrowernumber
=> $hold_waiting_borrowernumber,
342 biblionumber
=> $biblio->biblionumber,
343 itemnumber
=> $item_1->itemnumber,
344 branchcode
=> $branch,
348 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
349 is
( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
352 # 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
353 # be able to renew these items
354 $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
356 borrowernumber
=> $hold_waiting_borrowernumber,
357 biblionumber
=> $biblio->biblionumber,
358 itemnumber
=> $item_3->itemnumber,
359 branchcode
=> $branch,
364 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
365 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
366 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
367 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
368 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 0 );
370 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
371 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
372 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
374 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
375 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
376 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
378 my $reserveid = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber, borrowernumber
=> $reserving_borrowernumber })->next->reserve_id;
379 my $reserving_borrower = Koha
::Patrons
->find( $reserving_borrowernumber )->unblessed;
380 AddIssue
($reserving_borrower, $item_3->barcode);
381 my $reserve = $dbh->selectrow_hashref(
382 'SELECT * FROM old_reserves WHERE reserve_id = ?',
386 is
($reserve->{found
}, 'F', 'hold marked completed when checking out item that fills it');
388 # Item-level hold, renewal test
390 $branch, $reserving_borrowernumber, $biblio->biblionumber,
391 $bibitems, $priority, $resdate, $expdate, $notes,
392 'a title', $item_1->itemnumber, $found
395 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
396 is
( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
397 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
399 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber, 1);
400 is
( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
402 # Items can't fill hold for reasons
403 ModItem
({ notforloan
=> 1 }, $biblio->biblionumber, $item_1->itemnumber);
404 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
405 is
( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
406 ModItem
({ notforloan
=> 0, itype
=> $itemtype }, $biblio->biblionumber, $item_1->itemnumber);
408 # FIXME: Add more for itemtype not for loan etc.
410 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
411 my $item_5 = $builder->build_sample_item(
413 biblionumber
=> $biblio->biblionumber,
415 replacementprice
=> 23.00,
419 my $datedue5 = AddIssue
($restricted_borrower, $item_5->barcode);
420 is
(defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
422 t
::lib
::Mocks
::mock_preference
('RestrictionBlockRenewing','1');
423 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_2->itemnumber);
424 is
( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
425 ( $renewokay, $error ) = CanBookBeRenewed
($restricted_borrowernumber, $item_5->itemnumber);
426 is
( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
428 # Users cannot renew an overdue item
429 my $item_6 = $builder->build_sample_item(
431 biblionumber
=> $biblio->biblionumber,
433 replacementprice
=> 23.00,
438 my $item_7 = $builder->build_sample_item(
440 biblionumber
=> $biblio->biblionumber,
442 replacementprice
=> 23.00,
447 my $datedue6 = AddIssue
( $renewing_borrower, $item_6->barcode);
448 is
(defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
450 my $now = dt_from_string
();
451 my $five_weeks = DateTime
::Duration
->new(weeks
=> 5);
452 my $five_weeks_ago = $now - $five_weeks;
453 t
::lib
::Mocks
::mock_preference
('finesMode', 'production');
455 my $passeddatedue1 = AddIssue
($renewing_borrower, $item_7->barcode, $five_weeks_ago);
456 is
(defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
458 my ( $fine ) = CalcFine
( $item_7->unblessed, $renewing_borrower->{categorycode
}, $branch, $five_weeks_ago, $now );
459 C4
::Overdues
::UpdateFine
(
461 issue_id
=> $passeddatedue1->id(),
462 itemnumber
=> $item_7->itemnumber,
463 borrowernumber
=> $renewing_borrower->{borrowernumber
},
465 due
=> Koha
::DateUtils
::output_pref
($five_weeks_ago)
469 t
::lib
::Mocks
::mock_preference
('RenewalLog', 0);
470 my $date = output_pref
( { dt
=> dt_from_string
(), datenonly
=> 1, dateformat
=> 'iso' } );
471 my $old_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
472 AddRenewal
( $renewing_borrower->{borrowernumber
}, $item_7->itemnumber, $branch );
473 my $new_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
474 is
($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
476 t
::lib
::Mocks
::mock_preference
('RenewalLog', 1);
477 $date = output_pref
( { dt
=> dt_from_string
(), datenonly
=> 1, dateformat
=> 'iso' } );
478 $old_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
479 AddRenewal
( $renewing_borrower->{borrowernumber
}, $item_7->itemnumber, $branch );
480 $new_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
481 is
($new_log_size, $old_log_size + 1, 'renew log successfully added');
483 my $fines = Koha
::Account
::Lines
->search( { borrowernumber
=> $renewing_borrower->{borrowernumber
}, itemnumber
=> $item_7->itemnumber } );
484 is
( $fines->count, 2 );
485 is
( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
486 is
( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
490 my $old_issue_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
491 my $old_renew_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
492 AddIssue
( $renewing_borrower,$item_7->barcode,Koha
::DateUtils
::output_pref
({str
=>$datedue6->date_due, dateformat
=>'iso'}),0,$date, 0, undef );
493 $new_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
494 is
($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
495 $new_log_size = scalar(@
{GetLogs
( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
496 is
($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
498 $fines = Koha
::Account
::Lines
->search( { borrowernumber
=> $renewing_borrower->{borrowernumber
}, itemnumber
=> $item_7->itemnumber } );
501 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','blockitem');
502 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_6->itemnumber);
503 is
( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
504 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_7->itemnumber);
505 is
( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
508 $hold = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber, borrowernumber
=> $reserving_borrowernumber })->next;
512 # Test automatic renewal before value for "norenewalbefore" in policy is set
513 # In this case automatic renewal is not permitted prior to due date
514 my $item_4 = $builder->build_sample_item(
516 biblionumber
=> $biblio->biblionumber,
518 replacementprice
=> 16.00,
523 $issue = AddIssue
( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew
=> 1 } );
524 ( $renewokay, $error ) =
525 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
526 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
527 is
( $error, 'auto_too_soon',
528 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
531 # Test premature manual renewal
532 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
534 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
535 is
( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
536 is
( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
539 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
540 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'exact_time' );
542 GetSoonestRenewDate
( $renewing_borrowernumber, $item_1->itemnumber ),
543 $datedue->clone->add( days
=> -7 ),
544 'Bug 14395: Renewals permitted 7 days before due date, as expected'
548 # Test 'date' setting for syspref NoRenewalBeforePrecision
549 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'date' );
551 GetSoonestRenewDate
( $renewing_borrowernumber, $item_1->itemnumber ),
552 $datedue->clone->add( days
=> -7 )->truncate( to
=> 'day' ),
553 'Bug 14395: Renewals permitted 7 days before due date, as expected'
557 # Test premature automatic renewal
558 ( $renewokay, $error ) =
559 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
560 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
561 is
( $error, 'auto_too_soon',
562 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
565 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
566 # and test automatic renewal again
567 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
568 ( $renewokay, $error ) =
569 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
570 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
571 is
( $error, 'auto_too_soon',
572 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
575 # Change policy so that loans can be renewed 99 days prior to the due date
576 # and test automatic renewal again
577 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
578 ( $renewokay, $error ) =
579 CanBookBeRenewed
( $renewing_borrowernumber, $item_4->itemnumber );
580 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
581 is
( $error, 'auto_renew',
582 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
585 subtest
"too_late_renewal / no_auto_renewal_after" => sub {
587 my $item_to_auto_renew = $builder->build(
590 biblionumber
=> $biblio->biblionumber,
591 homebranch
=> $branch,
592 holdingbranch
=> $branch,
597 my $ten_days_before = dt_from_string
->add( days
=> -10 );
598 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
599 AddIssue
( $renewing_borrower, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
601 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
602 ( $renewokay, $error ) =
603 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
604 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
605 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
607 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
608 ( $renewokay, $error ) =
609 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
610 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
611 is
( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
613 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
614 ( $renewokay, $error ) =
615 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
616 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
617 is
( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
619 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
620 ( $renewokay, $error ) =
621 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
622 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
623 is
( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
625 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string
->add( days
=> -1 ) );
626 ( $renewokay, $error ) =
627 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
628 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
629 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
631 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string
->add( days
=> -1 ) );
632 ( $renewokay, $error ) =
633 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
634 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
635 is
( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
637 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string
->add( days
=> 1 ) );
638 ( $renewokay, $error ) =
639 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
640 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
641 is
( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
644 subtest
"auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
646 my $item_to_auto_renew = $builder->build({
649 biblionumber
=> $biblio->biblionumber,
650 homebranch
=> $branch,
651 holdingbranch
=> $branch,
655 my $ten_days_before = dt_from_string
->add( days
=> -10 );
656 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
657 AddIssue
( $renewing_borrower, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
659 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
660 C4
::Context
->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
661 C4
::Context
->set_preference('OPACFineNoRenewals','10');
662 my $fines_amount = 5;
663 C4
::Accounts
::manualinvoice
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
}, "Some fines", 'F', $fines_amount );
664 ( $renewokay, $error ) =
665 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
666 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
667 is
( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
669 C4
::Accounts
::manualinvoice
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
}, "Some fines", 'F', $fines_amount );
670 ( $renewokay, $error ) =
671 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
672 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
673 is
( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
675 C4
::Accounts
::manualinvoice
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
}, "Some fines", 'F', $fines_amount );
676 ( $renewokay, $error ) =
677 CanBookBeRenewed
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
678 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
679 is
( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
681 $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
684 subtest
"auto_account_expired | BlockExpiredPatronOpacActions" => sub {
686 my $item_to_auto_renew = $builder->build({
689 biblionumber
=> $biblio->biblionumber,
690 homebranch
=> $branch,
691 holdingbranch
=> $branch,
695 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
697 my $ten_days_before = dt_from_string
->add( days
=> -10 );
698 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
700 # Patron is expired and BlockExpiredPatronOpacActions=0
701 # => auto renew is allowed
702 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 0);
703 my $patron = $expired_borrower;
704 my $checkout = AddIssue
( $patron, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
705 ( $renewokay, $error ) =
706 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->{itemnumber
} );
707 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
708 is
( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
709 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
712 # Patron is expired and BlockExpiredPatronOpacActions=1
713 # => auto renew is not allowed
714 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 1);
715 $patron = $expired_borrower;
716 $checkout = AddIssue
( $patron, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
717 ( $renewokay, $error ) =
718 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->{itemnumber
} );
719 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
720 is
( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
721 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
724 # Patron is not expired and BlockExpiredPatronOpacActions=1
725 # => auto renew is allowed
726 t
::lib
::Mocks
::mock_preference
('BlockExpiredPatronOpacActions', 1);
727 $patron = $renewing_borrower;
728 $checkout = AddIssue
( $patron, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
729 ( $renewokay, $error ) =
730 CanBookBeRenewed
( $patron->{borrowernumber
}, $item_to_auto_renew->{itemnumber
} );
731 is
( $renewokay, 0, 'Do not renew, renewal is automatic' );
732 is
( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
733 Koha
::Checkouts
->find( $checkout->issue_id )->delete;
736 subtest
"GetLatestAutoRenewDate" => sub {
738 my $item_to_auto_renew = $builder->build(
741 biblionumber
=> $biblio->biblionumber,
742 homebranch
=> $branch,
743 holdingbranch
=> $branch,
748 my $ten_days_before = dt_from_string
->add( days
=> -10 );
749 my $ten_days_ahead = dt_from_string
->add( days
=> 10 );
750 AddIssue
( $renewing_borrower, $item_to_auto_renew->{barcode
}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew
=> 1 } );
751 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
752 my $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
753 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' );
754 my $five_days_before = dt_from_string
->add( days
=> -5 );
755 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
756 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
757 is
( $latest_auto_renew_date->truncate( to
=> 'minute' ),
758 $five_days_before->truncate( to
=> 'minute' ),
759 'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
761 my $five_days_ahead = dt_from_string
->add( days
=> 5 );
762 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
763 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
764 is
( $latest_auto_renew_date->truncate( to
=> 'minute' ),
765 $five_days_ahead->truncate( to
=> 'minute' ),
766 'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
768 my $two_days_ahead = dt_from_string
->add( days
=> 2 );
769 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string
->add( days
=> 2 ) );
770 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
771 is
( $latest_auto_renew_date->truncate( to
=> 'day' ),
772 $two_days_ahead->truncate( to
=> 'day' ),
773 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
775 $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string
->add( days
=> 2 ) );
776 $latest_auto_renew_date = GetLatestAutoRenewDate
( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber
} );
777 is
( $latest_auto_renew_date->truncate( to
=> 'day' ),
778 $two_days_ahead->truncate( to
=> 'day' ),
779 'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
786 # set policy to forbid renewals
787 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
789 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber);
790 is
( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
791 is
( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
793 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
794 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','1');
795 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','1');
797 C4
::Overdues
::UpdateFine
(
799 issue_id
=> $issue->id(),
800 itemnumber
=> $item_1->itemnumber,
801 borrowernumber
=> $renewing_borrower->{borrowernumber
},
804 due
=> Koha
::DateUtils
::output_pref
($datedue)
808 my $line = Koha
::Account
::Lines
->search({ borrowernumber
=> $renewing_borrower->{borrowernumber
} })->next();
809 is
( $line->accounttype, 'FU', 'Account line type is FU' );
810 is
( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
811 is
( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
812 is
( $line->amount, '15.000000', 'Account line amount is 15.00' );
813 is
( $line->issue_id, $issue->id, 'Account line issue id matches' );
815 my $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $line->id })->next();
816 is
( $offset->type, 'Fine', 'Account offset type is Fine' );
817 is
( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
819 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','0');
820 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','0');
822 LostItem
( $item_1->itemnumber, 'test', 1 );
824 $line = Koha
::Account
::Lines
->find($line->id);
825 is
( $line->accounttype, 'F', 'Account type correctly changed from FU to F' );
827 my $item = Koha
::Items
->find($item_1->itemnumber);
828 ok
( !$item->onloan(), "Lost item marked as returned has false onloan value" );
829 my $checkout = Koha
::Checkouts
->find({ itemnumber
=> $item_1->itemnumber });
830 is
( $checkout, undef, 'LostItem called with forced return has checked in the item' );
832 my $total_due = $dbh->selectrow_array(
833 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
834 undef, $renewing_borrower->{borrowernumber
}
837 is
( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
839 C4
::Context
->dbh->do("DELETE FROM accountlines");
841 C4
::Overdues
::UpdateFine
(
843 issue_id
=> $issue2->id(),
844 itemnumber
=> $item_2->itemnumber,
845 borrowernumber
=> $renewing_borrower->{borrowernumber
},
848 due
=> Koha
::DateUtils
::output_pref
($datedue)
852 LostItem
( $item_2->itemnumber, 'test', 0 );
854 my $item2 = Koha
::Items
->find($item_2->itemnumber);
855 ok
( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
856 ok
( Koha
::Checkouts
->find({ itemnumber
=> $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
858 $total_due = $dbh->selectrow_array(
859 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
860 undef, $renewing_borrower->{borrowernumber
}
863 ok
( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
865 my $future = dt_from_string
();
866 $future->add( days
=> 7 );
867 my $units = C4
::Overdues
::get_chargeable_units
('days', $future, $now, $library2->{branchcode
});
868 ok
( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
870 # Users cannot renew any item if there is an overdue item
871 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','block');
872 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_6->itemnumber);
873 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
874 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_7->itemnumber);
875 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
877 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','1');
878 $checkout = Koha
::Checkouts
->find( { itemnumber
=> $item_3->itemnumber } );
879 LostItem
( $item_3->itemnumber, 'test', 0 );
880 my $accountline = Koha
::Account
::Lines
->find( { itemnumber
=> $item_3->itemnumber } );
881 is
( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
885 # GetUpcomingDueIssues tests
886 my $branch = $library2->{branchcode
};
888 #Create another record
889 my $biblio2 = $builder->build_sample_biblio();
892 my $item_1 = Koha
::Items
->find($reused_itemnumber_1);
893 my $item_2 = Koha
::Items
->find($reused_itemnumber_2);
894 my $item_3 = $builder->build_sample_item(
896 biblionumber
=> $biblio2->biblionumber,
904 my %a_borrower_data = (
905 firstname
=> 'Fridolyn',
907 categorycode
=> $patron_category->{categorycode
},
908 branchcode
=> $branch,
911 my $a_borrower_borrowernumber = Koha
::Patron
->new(\
%a_borrower_data)->store->borrowernumber;
912 my $a_borrower = Koha
::Patrons
->find( $a_borrower_borrowernumber )->unblessed;
914 my $yesterday = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> -1 );
915 my $two_days_ahead = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> 2 );
916 my $today = DateTime
->today(time_zone
=> C4
::Context
->tz());
918 my $issue = AddIssue
( $a_borrower, $item_1->barcode, $yesterday );
919 my $datedue = dt_from_string
( $issue->date_due() );
920 my $issue2 = AddIssue
( $a_borrower, $item_2->barcode, $two_days_ahead );
921 my $datedue2 = dt_from_string
( $issue->date_due() );
925 # GetUpcomingDueIssues tests
927 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
928 is
( scalar( @
$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
931 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
932 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
933 is
( scalar ( @
$upcoming_dues), 1, "Only one item due in 2 days or less" );
936 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
937 is
( scalar( @
$upcoming_dues ), 1,
938 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
941 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
943 my $issue3 = AddIssue
( $a_borrower, $item_3->barcode, $today );
945 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> -1 } );
946 is
( scalar ( @
$upcoming_dues), 0, "Overdues can not be selected" );
948 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 0 } );
949 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today" );
951 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 1 } );
952 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today, none tomorrow" );
954 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
955 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
957 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 3 } );
958 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
960 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
();
961 is
( scalar ( @
$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
966 my $branch = $library2->{branchcode
};
968 my $biblio = $builder->build_sample_biblio();
971 my $item = $builder->build_sample_item(
973 biblionumber
=> $biblio->biblionumber,
980 my %a_borrower_data = (
983 categorycode
=> $patron_category->{categorycode
},
984 branchcode
=> $branch,
987 my $borrowernumber = Koha
::Patron
->new(\
%a_borrower_data)->store->borrowernumber;
989 my $borrower = Koha
::Patrons
->find( $borrowernumber )->unblessed;
990 my $issue = AddIssue
( $borrower, $item->barcode );
993 issue_id
=> $issue->id(),
994 itemnumber
=> $item->itemnumber,
995 borrowernumber
=> $borrowernumber,
1001 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1002 my $count = $hr->{count
};
1004 is
( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1008 $dbh->do('DELETE FROM issues');
1009 $dbh->do('DELETE FROM items');
1010 $dbh->do('DELETE FROM issuingrules');
1013 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
1014 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1023 my $biblio = $builder->build_sample_biblio();
1025 my $item_1 = $builder->build_sample_item(
1027 biblionumber
=> $biblio->biblionumber,
1028 library
=> $library2->{branchcode
},
1033 my $item_2= $builder->build_sample_item(
1035 biblionumber
=> $biblio->biblionumber,
1036 library
=> $library2->{branchcode
},
1041 my $borrowernumber1 = Koha
::Patron
->new({
1042 firstname
=> 'Kyle',
1044 categorycode
=> $patron_category->{categorycode
},
1045 branchcode
=> $library2->{branchcode
},
1046 })->store->borrowernumber;
1047 my $borrowernumber2 = Koha
::Patron
->new({
1048 firstname
=> 'Chelsea',
1050 categorycode
=> $patron_category->{categorycode
},
1051 branchcode
=> $library2->{branchcode
},
1052 })->store->borrowernumber;
1054 my $borrower1 = Koha
::Patrons
->find( $borrowernumber1 )->unblessed;
1055 my $borrower2 = Koha
::Patrons
->find( $borrowernumber2 )->unblessed;
1057 my $issue = AddIssue
( $borrower1, $item_1->barcode );
1059 my ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1060 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1063 $library2->{branchcode
}, $borrowernumber2, $biblio->biblionumber,
1064 '', 1, undef, undef, '',
1068 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1069 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
1070 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1071 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1073 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1074 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
1075 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1076 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1078 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1079 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
1080 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1081 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1083 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1084 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
1085 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1086 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1088 # Setting item not checked out to be not for loan but holdable
1089 ModItem
({ notforloan
=> -1 }, $biblio->biblionumber, $item_2->itemnumber);
1091 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $item_1->itemnumber );
1092 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' );
1096 # Don't allow renewing onsite checkout
1097 my $branch = $library->{branchcode
};
1099 #Create another record
1100 my $biblio = $builder->build_sample_biblio();
1102 my $item = $builder->build_sample_item(
1104 biblionumber
=> $biblio->biblionumber,
1110 my $borrowernumber = Koha
::Patron
->new({
1113 categorycode
=> $patron_category->{categorycode
},
1114 branchcode
=> $branch,
1115 })->store->borrowernumber;
1117 my $borrower = Koha
::Patrons
->find( $borrowernumber )->unblessed;
1119 my $issue = AddIssue
( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout
=> 1 } );
1120 my ( $renewed, $error ) = CanBookBeRenewed
( $borrowernumber, $item->itemnumber );
1121 is
( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1122 is
( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1126 my $library = $builder->build({ source
=> 'Branch' });
1128 my $biblio = $builder->build_sample_biblio();
1130 my $item = $builder->build_sample_item(
1132 biblionumber
=> $biblio->biblionumber,
1133 library
=> $library->{branchcode
},
1138 my $patron = $builder->build({ source
=> 'Borrower', value
=> { branchcode
=> $library->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
1140 my $issue = AddIssue
( $patron, $item->barcode );
1143 issue_id
=> $issue->id(),
1144 itemnumber
=> $item->itemnumber,
1145 borrowernumber
=> $patron->{borrowernumber
},
1152 issue_id
=> $issue->id(),
1153 itemnumber
=> $item->itemnumber,
1154 borrowernumber
=> $patron->{borrowernumber
},
1159 is
( Koha
::Account
::Lines
->search({ issue_id
=> $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1162 subtest
'CanBookBeIssued & AllowReturnToBranch' => sub {
1165 my $homebranch = $builder->build( { source
=> 'Branch' } );
1166 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
1167 my $otherbranch = $builder->build( { source
=> 'Branch' } );
1168 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1169 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1171 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
1172 my $item = $builder->build(
1175 homebranch
=> $homebranch->{branchcode
},
1176 holdingbranch
=> $holdingbranch->{branchcode
},
1177 biblionumber
=> $biblioitem->{biblionumber
}
1182 set_userenv
($holdingbranch);
1184 my $issue = AddIssue
( $patron_1->unblessed, $item->{barcode
} );
1185 is
( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
1187 my ( $error, $question, $alerts );
1189 # AllowReturnToBranch == anywhere
1190 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
1191 ## Test that unknown barcodes don't generate internal server errors
1192 set_userenv
($homebranch);
1193 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, 'KohaIsAwesome' );
1194 ok
( $error->{UNKNOWN_BARCODE
}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1195 ## Can be issued from homebranch
1196 set_userenv
($homebranch);
1197 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1198 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1199 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1200 ## Can be issued from holdingbranch
1201 set_userenv
($holdingbranch);
1202 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1203 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1204 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1205 ## Can be issued from another branch
1206 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1207 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1208 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1210 # AllowReturnToBranch == holdingbranch
1211 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
1212 ## Cannot be issued from homebranch
1213 set_userenv
($homebranch);
1214 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1215 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1216 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1217 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
} );
1218 ## Can be issued from holdinbranch
1219 set_userenv
($holdingbranch);
1220 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1221 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1222 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1223 ## Cannot be issued from another branch
1224 set_userenv
($otherbranch);
1225 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1226 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1227 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1228 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
} );
1230 # AllowReturnToBranch == homebranch
1231 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1232 ## Can be issued from holdinbranch
1233 set_userenv
($homebranch);
1234 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1235 is
( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1236 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1, 'ISSUED_TO_ANOTHER must be set' );
1237 ## Cannot be issued from holdinbranch
1238 set_userenv
($holdingbranch);
1239 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1240 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1241 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1242 is
( $error->{branch_to_return
}, $homebranch->{branchcode
} );
1243 ## Cannot be issued from holdinbranch
1244 set_userenv
($otherbranch);
1245 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1246 is
( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str
($error, $question, $alerts) );
1247 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1, 'RETURN_IMPOSSIBLE must be set' );
1248 is
( $error->{branch_to_return
}, $homebranch->{branchcode
} );
1250 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1253 subtest
'AddIssue & AllowReturnToBranch' => sub {
1256 my $homebranch = $builder->build( { source
=> 'Branch' } );
1257 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
1258 my $otherbranch = $builder->build( { source
=> 'Branch' } );
1259 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1260 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1262 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
1263 my $item = $builder->build(
1266 homebranch
=> $homebranch->{branchcode
},
1267 holdingbranch
=> $holdingbranch->{branchcode
},
1271 biblionumber
=> $biblioitem->{biblionumber
}
1276 set_userenv
($holdingbranch);
1278 my $ref_issue = 'Koha::Checkout';
1279 my $issue = AddIssue
( $patron_1, $item->{barcode
} );
1281 my ( $error, $question, $alerts );
1283 # AllowReturnToBranch == homebranch
1284 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
1285 ## Can be issued from homebranch
1286 set_userenv
($homebranch);
1287 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1288 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1289 ## Can be issued from holdinbranch
1290 set_userenv
($holdingbranch);
1291 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1292 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1293 ## Can be issued from another branch
1294 set_userenv
($otherbranch);
1295 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1296 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1298 # AllowReturnToBranch == holdinbranch
1299 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
1300 ## Cannot be issued from homebranch
1301 set_userenv
($homebranch);
1302 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1303 ## Can be issued from holdingbranch
1304 set_userenv
($holdingbranch);
1305 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1306 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1307 ## Cannot be issued from another branch
1308 set_userenv
($otherbranch);
1309 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1311 # AllowReturnToBranch == homebranch
1312 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1313 ## Can be issued from homebranch
1314 set_userenv
($homebranch);
1315 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1316 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1317 ## Cannot be issued from holdinbranch
1318 set_userenv
($holdingbranch);
1319 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1320 ## Cannot be issued from another branch
1321 set_userenv
($otherbranch);
1322 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1323 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1326 subtest
'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1329 my $library = $builder->build( { source
=> 'Branch' } );
1330 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1332 my $biblioitem_1 = $builder->build( { source
=> 'Biblioitem' } );
1333 my $item_1 = $builder->build(
1336 homebranch
=> $library->{branchcode
},
1337 holdingbranch
=> $library->{branchcode
},
1338 biblionumber
=> $biblioitem_1->{biblionumber
}
1342 my $biblioitem_2 = $builder->build( { source
=> 'Biblioitem' } );
1343 my $item_2 = $builder->build(
1346 homebranch
=> $library->{branchcode
},
1347 holdingbranch
=> $library->{branchcode
},
1348 biblionumber
=> $biblioitem_2->{biblionumber
}
1353 my ( $error, $question, $alerts );
1355 # Patron cannot issue item_1, they have overdues
1356 my $yesterday = DateTime
->today( time_zone
=> C4
::Context
->tz() )->add( days
=> -1 );
1357 my $issue = AddIssue
( $patron->unblessed, $item_1->{barcode
}, $yesterday ); # Add an overdue
1359 t
::lib
::Mocks
::mock_preference
( 'OverduesBlockCirc', 'confirmation' );
1360 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1361 is
( keys(%$error) + keys(%$alerts), 0, 'No key for error and alert' . str
($error, $question, $alerts) );
1362 is
( $question->{USERBLOCKEDOVERDUE
}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1364 t
::lib
::Mocks
::mock_preference
( 'OverduesBlockCirc', 'block' );
1365 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1366 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1367 is
( $error->{USERBLOCKEDOVERDUE
}, 1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1369 # Patron cannot issue item_1, they are debarred
1370 my $tomorrow = DateTime
->today( time_zone
=> C4
::Context
->tz() )->add( days
=> 1 );
1371 Koha
::Patron
::Debarments
::AddDebarment
( { borrowernumber
=> $patron->borrowernumber, expiration
=> $tomorrow } );
1372 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1373 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1374 is
( $error->{USERBLOCKEDWITHENDDATE
}, output_pref
( { dt
=> $tomorrow, dateformat
=> 'sql', dateonly
=> 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1376 Koha
::Patron
::Debarments
::AddDebarment
( { borrowernumber
=> $patron->borrowernumber } );
1377 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1378 is
( keys(%$question) + keys(%$alerts), 0, 'No key for question and alert ' . str
($error, $question, $alerts) );
1379 is
( $error->{USERBLOCKEDNOENDDATE
}, '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1382 subtest
'CanBookBeIssued + Statistic patrons "X"' => sub {
1385 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1386 my $patron_category_x = $builder->build_object(
1388 class => 'Koha::Patron::Categories',
1389 value
=> { category_type
=> 'X' }
1392 my $patron = $builder->build_object(
1394 class => 'Koha::Patrons',
1396 categorycode
=> $patron_category_x->categorycode,
1397 gonenoaddress
=> undef,
1404 my $biblioitem_1 = $builder->build( { source
=> 'Biblioitem' } );
1405 my $item_1 = $builder->build(
1409 homebranch
=> $library->branchcode,
1410 holdingbranch
=> $library->branchcode,
1411 biblionumber
=> $biblioitem_1->{biblionumber
}
1416 my ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_1->{barcode
} );
1417 is
( $error->{STATS
}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1419 # TODO There are other tests to provide here
1422 subtest
'MultipleReserves' => sub {
1425 my $biblio = $builder->build_sample_biblio();
1427 my $branch = $library2->{branchcode
};
1429 my $item_1 = $builder->build_sample_item(
1431 biblionumber
=> $biblio->biblionumber,
1433 replacementprice
=> 12.00,
1438 my $item_2 = $builder->build_sample_item(
1440 biblionumber
=> $biblio->biblionumber,
1442 replacementprice
=> 12.00,
1449 my $resdate = undef;
1450 my $expdate = undef;
1452 my $checkitem = undef;
1455 my %renewing_borrower_data = (
1456 firstname
=> 'John',
1457 surname
=> 'Renewal',
1458 categorycode
=> $patron_category->{categorycode
},
1459 branchcode
=> $branch,
1461 my $renewing_borrowernumber = Koha
::Patron
->new(\
%renewing_borrower_data)->store->borrowernumber;
1462 my $renewing_borrower = Koha
::Patrons
->find( $renewing_borrowernumber )->unblessed;
1463 my $issue = AddIssue
( $renewing_borrower, $item_1->barcode);
1464 my $datedue = dt_from_string
( $issue->date_due() );
1465 is
(defined $issue->date_due(), 1, "item 1 checked out");
1466 my $borrowing_borrowernumber = Koha
::Checkouts
->find({ itemnumber
=> $item_1->itemnumber })->borrowernumber;
1468 my %reserving_borrower_data1 = (
1469 firstname
=> 'Katrin',
1470 surname
=> 'Reservation',
1471 categorycode
=> $patron_category->{categorycode
},
1472 branchcode
=> $branch,
1474 my $reserving_borrowernumber1 = Koha
::Patron
->new(\
%reserving_borrower_data1)->store->borrowernumber;
1476 $branch, $reserving_borrowernumber1, $biblio->biblionumber,
1477 $bibitems, $priority, $resdate, $expdate, $notes,
1478 'a title', $checkitem, $found
1481 my %reserving_borrower_data2 = (
1482 firstname
=> 'Kirk',
1483 surname
=> 'Reservation',
1484 categorycode
=> $patron_category->{categorycode
},
1485 branchcode
=> $branch,
1487 my $reserving_borrowernumber2 = Koha
::Patron
->new(\
%reserving_borrower_data2)->store->borrowernumber;
1489 $branch, $reserving_borrowernumber2, $biblio->biblionumber,
1490 $bibitems, $priority, $resdate, $expdate, $notes,
1491 'a title', $checkitem, $found
1495 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
1496 is
($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1499 my $item_3 = $builder->build_sample_item(
1501 biblionumber
=> $biblio->biblionumber,
1503 replacementprice
=> 12.00,
1509 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $item_1->itemnumber, 1);
1510 is
($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1514 subtest
'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1517 my $library = $builder->build( { source
=> 'Branch' } );
1518 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1520 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
1521 my $biblionumber = $biblioitem->{biblionumber
};
1522 my $item_1 = $builder->build(
1525 homebranch
=> $library->{branchcode
},
1526 holdingbranch
=> $library->{branchcode
},
1527 biblionumber
=> $biblionumber,
1531 my $item_2 = $builder->build(
1534 homebranch
=> $library->{branchcode
},
1535 holdingbranch
=> $library->{branchcode
},
1536 biblionumber
=> $biblionumber,
1541 my ( $error, $question, $alerts );
1542 my $issue = AddIssue
( $patron->unblessed, $item_1->{barcode
}, dt_from_string
->add( days
=> 1 ) );
1544 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 0);
1545 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1546 is
( keys(%$error) + keys(%$alerts), 0, 'No error or alert should be raised' . str
($error, $question, $alerts) );
1547 is
( $question->{BIBLIO_ALREADY_ISSUED
}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' . str
($error, $question, $alerts) );
1549 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 1);
1550 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1551 is
( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str
($error, $question, $alerts) );
1553 # Add a subscription
1554 Koha
::Subscription
->new({ biblionumber
=> $biblionumber })->store;
1556 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 0);
1557 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1558 is
( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str
($error, $question, $alerts) );
1560 t
::lib
::Mocks
::mock_preference
('AllowMultipleIssuesOnABiblio', 1);
1561 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron, $item_2->{barcode
} );
1562 is
( keys(%$error) + keys(%$question) + keys(%$alerts), 0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str
($error, $question, $alerts) );
1565 subtest
'AddReturn + CumulativeRestrictionPeriods' => sub {
1568 my $library = $builder->build( { source
=> 'Branch' } );
1569 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1572 my $biblioitem_1 = $builder->build( { source
=> 'Biblioitem' } );
1573 my $item_1 = $builder->build(
1577 homebranch
=> $library->{branchcode
},
1578 holdingbranch
=> $library->{branchcode
},
1582 biblionumber
=> $biblioitem_1->{biblionumber
}
1586 my $biblioitem_2 = $builder->build( { source
=> 'Biblioitem' } );
1587 my $item_2 = $builder->build(
1591 homebranch
=> $library->{branchcode
},
1592 holdingbranch
=> $library->{branchcode
},
1596 biblionumber
=> $biblioitem_2->{biblionumber
}
1601 # And the issuing rule
1602 Koha
::IssuingRules
->search->delete;
1603 my $rule = Koha
::IssuingRule
->new(
1605 categorycode
=> '*',
1610 firstremind
=> 1, # 1 day of grace
1611 finedays
=> 2, # 2 days of fine per day of overdue
1612 lengthunit
=> 'days',
1617 # Patron cannot issue item_1, they have overdues
1618 my $five_days_ago = dt_from_string
->subtract( days
=> 5 );
1619 my $ten_days_ago = dt_from_string
->subtract( days
=> 10 );
1620 AddIssue
( $patron, $item_1->{barcode
}, $five_days_ago ); # Add an overdue
1621 AddIssue
( $patron, $item_2->{barcode
}, $ten_days_ago )
1622 ; # Add another overdue
1624 t
::lib
::Mocks
::mock_preference
( 'CumulativeRestrictionPeriods', '0' );
1625 AddReturn
( $item_1->{barcode
}, $library->{branchcode
},
1626 undef, undef, dt_from_string
);
1627 my $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
1628 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
1629 is
( scalar(@
$debarments), 1 );
1631 # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1632 # Same for the others
1633 my $expected_expiration = output_pref
(
1635 dt
=> dt_from_string
->add( days
=> ( 5 - 1 ) * 2 ),
1636 dateformat
=> 'sql',
1640 is
( $debarments->[0]->{expiration
}, $expected_expiration );
1642 AddReturn
( $item_2->{barcode
}, $library->{branchcode
},
1643 undef, undef, dt_from_string
);
1644 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
1645 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
1646 is
( scalar(@
$debarments), 1 );
1647 $expected_expiration = output_pref
(
1649 dt
=> dt_from_string
->add( days
=> ( 10 - 1 ) * 2 ),
1650 dateformat
=> 'sql',
1654 is
( $debarments->[0]->{expiration
}, $expected_expiration );
1656 Koha
::Patron
::Debarments
::DelUniqueDebarment
(
1657 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
1659 t
::lib
::Mocks
::mock_preference
( 'CumulativeRestrictionPeriods', '1' );
1660 AddIssue
( $patron, $item_1->{barcode
}, $five_days_ago ); # Add an overdue
1661 AddIssue
( $patron, $item_2->{barcode
}, $ten_days_ago )
1662 ; # Add another overdue
1663 AddReturn
( $item_1->{barcode
}, $library->{branchcode
},
1664 undef, undef, dt_from_string
);
1665 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
1666 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
1667 is
( scalar(@
$debarments), 1 );
1668 $expected_expiration = output_pref
(
1670 dt
=> dt_from_string
->add( days
=> ( 5 - 1 ) * 2 ),
1671 dateformat
=> 'sql',
1675 is
( $debarments->[0]->{expiration
}, $expected_expiration );
1677 AddReturn
( $item_2->{barcode
}, $library->{branchcode
},
1678 undef, undef, dt_from_string
);
1679 $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
1680 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
1681 is
( scalar(@
$debarments), 1 );
1682 $expected_expiration = output_pref
(
1684 dt
=> dt_from_string
->add( days
=> ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1685 dateformat
=> 'sql',
1689 is
( $debarments->[0]->{expiration
}, $expected_expiration );
1692 subtest
'AddReturn + suspension_chargeperiod' => sub {
1695 my $library = $builder->build( { source
=> 'Branch' } );
1696 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1699 my $biblioitem_1 = $builder->build( { source
=> 'Biblioitem' } );
1700 my $item_1 = $builder->build(
1704 homebranch
=> $library->{branchcode
},
1705 holdingbranch
=> $library->{branchcode
},
1709 biblionumber
=> $biblioitem_1->{biblionumber
}
1714 # And the issuing rule
1715 Koha
::IssuingRules
->search->delete;
1716 my $rule = Koha
::IssuingRule
->new(
1718 categorycode
=> '*',
1723 firstremind
=> 0, # 0 day of grace
1724 finedays
=> 2, # 2 days of fine per day of overdue
1725 suspension_chargeperiod
=> 1,
1726 lengthunit
=> 'days',
1731 my $five_days_ago = dt_from_string
->subtract( days
=> 5 );
1732 # We want to charge 2 days every day, without grace
1733 # With 5 days of overdue: 5 * Z
1734 my $expected_expiration = dt_from_string
->add( days
=> ( 5 * 2 ) / 1 );
1735 test_debarment_on_checkout
(
1738 library
=> $library,
1740 due_date
=> $five_days_ago,
1741 expiration_date
=> $expected_expiration,
1745 # We want to charge 2 days every 2 days, without grace
1746 # With 5 days of overdue: (5 * 2) / 2
1747 $rule->suspension_chargeperiod(2)->store;
1748 $expected_expiration = dt_from_string
->add( days
=> floor
( 5 * 2 ) / 2 );
1749 test_debarment_on_checkout
(
1752 library
=> $library,
1754 due_date
=> $five_days_ago,
1755 expiration_date
=> $expected_expiration,
1759 # We want to charge 2 days every 3 days, with 1 day of grace
1760 # With 5 days of overdue: ((5-1) / 3 ) * 2
1761 $rule->suspension_chargeperiod(3)->store;
1762 $rule->firstremind(1)->store;
1763 $expected_expiration = dt_from_string
->add( days
=> floor
( ( ( 5 - 1 ) / 3 ) * 2 ) );
1764 test_debarment_on_checkout
(
1767 library
=> $library,
1769 due_date
=> $five_days_ago,
1770 expiration_date
=> $expected_expiration,
1774 # Use finesCalendar to know if holiday must be skipped to calculate the due date
1775 # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1776 $rule->finedays(2)->store;
1777 $rule->suspension_chargeperiod(1)->store;
1778 $rule->firstremind(0)->store;
1779 t
::lib
::Mocks
::mock_preference
('finesCalendar', 'noFinesWhenClosed');
1781 # Adding a holiday 2 days ago
1782 my $calendar = C4
::Calendar
->new(branchcode
=> $library->{branchcode
});
1783 my $two_days_ago = dt_from_string
->subtract( days
=> 2 );
1784 $calendar->insert_single_holiday(
1785 day
=> $two_days_ago->day,
1786 month
=> $two_days_ago->month,
1787 year
=> $two_days_ago->year,
1788 title
=> 'holidayTest-2d',
1789 description
=> 'holidayDesc 2 days ago'
1791 # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1792 $expected_expiration = dt_from_string
->add( days
=> floor
( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1793 test_debarment_on_checkout
(
1796 library
=> $library,
1798 due_date
=> $five_days_ago,
1799 expiration_date
=> $expected_expiration,
1803 # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1804 my $two_days_ahead = dt_from_string
->add( days
=> 2 );
1805 $calendar->insert_single_holiday(
1806 day
=> $two_days_ahead->day,
1807 month
=> $two_days_ahead->month,
1808 year
=> $two_days_ahead->year,
1809 title
=> 'holidayTest+2d',
1810 description
=> 'holidayDesc 2 days ahead'
1813 # Same as above, but we should skip D+2
1814 $expected_expiration = dt_from_string
->add( days
=> floor
( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1815 test_debarment_on_checkout
(
1818 library
=> $library,
1820 due_date
=> $five_days_ago,
1821 expiration_date
=> $expected_expiration,
1825 # Adding another holiday, day of expiration date
1826 my $expected_expiration_dt = dt_from_string
($expected_expiration);
1827 $calendar->insert_single_holiday(
1828 day
=> $expected_expiration_dt->day,
1829 month
=> $expected_expiration_dt->month,
1830 year
=> $expected_expiration_dt->year,
1831 title
=> 'holidayTest_exp',
1832 description
=> 'holidayDesc on expiration date'
1834 # Expiration date will be the day after
1835 test_debarment_on_checkout
(
1838 library
=> $library,
1840 due_date
=> $five_days_ago,
1841 expiration_date
=> $expected_expiration_dt->clone->add( days
=> 1 ),
1845 test_debarment_on_checkout
(
1848 library
=> $library,
1850 return_date
=> dt_from_string
->add(days
=> 5),
1851 expiration_date
=> dt_from_string
->add(days
=> 5 + (5 * 2 - 1) ),
1856 subtest
'AddReturn | is_overdue' => sub {
1859 t
::lib
::Mocks
::mock_preference
('CalculateFinesOnReturn', 1);
1860 t
::lib
::Mocks
::mock_preference
('finesMode', 'production');
1861 t
::lib
::Mocks
::mock_preference
('MaxFine', '100');
1863 my $library = $builder->build( { source
=> 'Branch' } );
1864 my $patron = $builder->build( { source
=> 'Borrower', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
1866 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
1867 my $item = $builder->build(
1871 homebranch
=> $library->{branchcode
},
1872 holdingbranch
=> $library->{branchcode
},
1876 biblionumber
=> $biblioitem->{biblionumber
},
1881 Koha
::IssuingRules
->search->delete;
1882 my $rule = Koha
::IssuingRule
->new(
1884 categorycode
=> '*',
1889 lengthunit
=> 'days',
1890 fine
=> 1, # Charge 1 every day of overdue
1896 my $one_day_ago = dt_from_string
->subtract( days
=> 1 );
1897 my $five_days_ago = dt_from_string
->subtract( days
=> 5 );
1898 my $ten_days_ago = dt_from_string
->subtract( days
=> 10 );
1899 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
1901 # No date specify, today will be used
1902 AddIssue
( $patron->unblessed, $item->{barcode
}, $ten_days_ago ); # date due was 10d ago
1903 AddReturn
( $item->{barcode
}, $library->{branchcode
} );
1904 is
( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
1905 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
1907 # specify return date 5 days before => no overdue
1908 AddIssue
( $patron->unblessed, $item->{barcode
}, $five_days_ago ); # date due was 5d ago
1909 AddReturn
( $item->{barcode
}, $library->{branchcode
}, undef, undef, $ten_days_ago );
1910 is
( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1911 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
1913 # specify return date 5 days later => overdue
1914 AddIssue
( $patron->unblessed, $item->{barcode
}, $ten_days_ago ); # date due was 10d ago
1915 AddReturn
( $item->{barcode
}, $library->{branchcode
}, undef, undef, $five_days_ago );
1916 is
( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
1917 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
1919 # specify dropbox date 5 days before => no overdue
1920 AddIssue
( $patron->unblessed, $item->{barcode
}, $five_days_ago ); # date due was 5d ago
1921 AddReturn
( $item->{barcode
}, $library->{branchcode
}, undef, 1, undef, $ten_days_ago );
1922 is
( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
1923 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
1925 # specify dropbox date 5 days later => overdue, or... not
1926 AddIssue
( $patron->unblessed, $item->{barcode
}, $ten_days_ago ); # date due was 10d ago
1927 AddReturn
( $item->{barcode
}, $library->{branchcode
}, undef, 1, undef, $five_days_ago );
1928 is
( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue in dropbox mode' ); # FIXME? This is weird, the FU fine is created ( _CalculateAndUpdateFine > C4::Overdues::UpdateFine ) then remove later (in _FixOverduesOnReturn). Looks like it is a feature
1929 Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->borrowernumber })->delete;
1932 subtest
'_FixAccountForLostAndReturned' => sub {
1936 t
::lib
::Mocks
::mock_preference
( 'WhenLostChargeReplacementFee', 1 );
1937 t
::lib
::Mocks
::mock_preference
( 'WhenLostForgiveFine', 0 );
1939 my $processfee_amount = 20;
1940 my $replacement_amount = 99.00;
1941 my $item_type = $builder->build_object(
1942 { class => 'Koha::ItemTypes',
1944 notforloan
=> undef,
1946 defaultreplacecost
=> undef,
1947 processfee
=> $processfee_amount
1951 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1953 my $biblio = $builder->build_sample_biblio({ author
=> 'Hall, Daria' });
1955 subtest
'Full write-off tests' => sub {
1959 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1961 my $item = $builder->build_sample_item(
1963 biblionumber
=> $biblio->biblionumber,
1964 library
=> $library->branchcode,
1965 replacementprice
=> $replacement_amount,
1966 itype
=> $item_type->itemtype,
1970 AddIssue
( $patron->unblessed, $item->barcode );
1972 # Simulate item marked as lost
1973 ModItem
( { itemlost
=> 3 }, $biblio->biblionumber, $item->itemnumber );
1974 LostItem
( $item->itemnumber, 1 );
1976 my $processing_fee_lines = Koha
::Account
::Lines
->search(
1977 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'PF' } );
1978 is
( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
1979 my $processing_fee_line = $processing_fee_lines->next;
1980 is
( $processing_fee_line->amount + 0,
1981 $processfee_amount, 'The right PF amount is generated' );
1982 is
( $processing_fee_line->amountoutstanding + 0,
1983 $processfee_amount, 'The right PF amountoutstanding is generated' );
1985 my $lost_fee_lines = Koha
::Account
::Lines
->search(
1986 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'L' } );
1987 is
( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1988 my $lost_fee_line = $lost_fee_lines->next;
1989 is
( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
1990 is
( $lost_fee_line->amountoutstanding + 0,
1991 $replacement_amount, 'The right L amountoutstanding is generated' );
1993 my $account = $patron->account;
1994 my $debts = $account->outstanding_debits;
1996 # Write off the debt
1997 my $credit = $account->add_credit(
1998 { amount
=> $account->balance,
2002 $credit->apply( { debits
=> $debts, offset_type
=> 'Writeoff' } );
2004 my $credit_return_id = C4
::Circulation
::_FixAccountForLostAndReturned
( $item->itemnumber, $patron->id );
2005 is
( $credit_return_id, undef, 'No CR account line added' );
2007 $lost_fee_line->discard_changes; # reload from DB
2008 is
( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2009 is
( $lost_fee_line->accounttype,
2010 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2012 is
( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2015 subtest
'Full payment tests' => sub {
2019 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2021 my $item = $builder->build_sample_item(
2023 biblionumber
=> $biblio->biblionumber,
2024 library
=> $library->branchcode,
2025 replacementprice
=> $replacement_amount,
2026 itype
=> $item_type->itemtype
2030 AddIssue
( $patron->unblessed, $item->barcode );
2032 # Simulate item marked as lost
2033 ModItem
( { itemlost
=> 1 }, $biblio->biblionumber, $item->itemnumber );
2034 LostItem
( $item->itemnumber, 1 );
2036 my $processing_fee_lines = Koha
::Account
::Lines
->search(
2037 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'PF' } );
2038 is
( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2039 my $processing_fee_line = $processing_fee_lines->next;
2040 is
( $processing_fee_line->amount + 0,
2041 $processfee_amount, 'The right PF amount is generated' );
2042 is
( $processing_fee_line->amountoutstanding + 0,
2043 $processfee_amount, 'The right PF amountoutstanding is generated' );
2045 my $lost_fee_lines = Koha
::Account
::Lines
->search(
2046 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'L' } );
2047 is
( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2048 my $lost_fee_line = $lost_fee_lines->next;
2049 is
( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2050 is
( $lost_fee_line->amountoutstanding + 0,
2051 $replacement_amount, 'The right L amountountstanding is generated' );
2053 my $account = $patron->account;
2054 my $debts = $account->outstanding_debits;
2056 # Write off the debt
2057 my $credit = $account->add_credit(
2058 { amount
=> $account->balance,
2062 $credit->apply( { debits
=> $debts, offset_type
=> 'Payment' } );
2064 my $credit_return_id = C4
::Circulation
::_FixAccountForLostAndReturned
( $item->itemnumber, $patron->id );
2065 my $credit_return = Koha
::Account
::Lines
->find($credit_return_id);
2067 is
( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2068 is
( $credit_return->amount + 0,
2069 -99.00, 'The account line of type CR has an amount of -99' );
2070 is
( $credit_return->amountoutstanding + 0,
2071 -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2073 $lost_fee_line->discard_changes;
2074 is
( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2075 is
( $lost_fee_line->accounttype,
2076 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2078 is
( $patron->account->balance,
2079 -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2082 subtest
'Test without payment or write off' => sub {
2086 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2088 my $item = $builder->build_sample_item(
2090 biblionumber
=> $biblio->biblionumber,
2091 library
=> $library->branchcode,
2092 replacementprice
=> 23.00,
2093 replacementprice
=> $replacement_amount,
2094 itype
=> $item_type->itemtype
2098 AddIssue
( $patron->unblessed, $item->barcode );
2100 # Simulate item marked as lost
2101 ModItem
( { itemlost
=> 3 }, $biblio->biblionumber, $item->itemnumber );
2102 LostItem
( $item->itemnumber, 1 );
2104 my $processing_fee_lines = Koha
::Account
::Lines
->search(
2105 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'PF' } );
2106 is
( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2107 my $processing_fee_line = $processing_fee_lines->next;
2108 is
( $processing_fee_line->amount + 0,
2109 $processfee_amount, 'The right PF amount is generated' );
2110 is
( $processing_fee_line->amountoutstanding + 0,
2111 $processfee_amount, 'The right PF amountoutstanding is generated' );
2113 my $lost_fee_lines = Koha
::Account
::Lines
->search(
2114 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'L' } );
2115 is
( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2116 my $lost_fee_line = $lost_fee_lines->next;
2117 is
( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2118 is
( $lost_fee_line->amountoutstanding + 0,
2119 $replacement_amount, 'The right L amountountstanding is generated' );
2121 my $credit_return_id = C4
::Circulation
::_FixAccountForLostAndReturned
( $item->itemnumber, $patron->id );
2122 my $credit_return = Koha
::Account
::Lines
->find($credit_return_id);
2124 is
( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2125 is
( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2126 is
( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2128 $lost_fee_line->discard_changes;
2129 is
( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2130 is
( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2132 is
( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2135 subtest
'Test with partial payement and write off, and remaining debt' => sub {
2139 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2140 my $item = $builder->build_sample_item(
2142 biblionumber
=> $biblio->biblionumber,
2143 library
=> $library->branchcode,
2144 replacementprice
=> $replacement_amount,
2145 itype
=> $item_type->itemtype
2149 AddIssue
( $patron->unblessed, $item->barcode );
2151 # Simulate item marked as lost
2152 ModItem
( { itemlost
=> 1 }, $biblio->biblionumber, $item->itemnumber );
2153 LostItem
( $item->itemnumber, 1 );
2155 my $processing_fee_lines = Koha
::Account
::Lines
->search(
2156 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'PF' } );
2157 is
( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2158 my $processing_fee_line = $processing_fee_lines->next;
2159 is
( $processing_fee_line->amount + 0,
2160 $processfee_amount, 'The right PF amount is generated' );
2161 is
( $processing_fee_line->amountoutstanding + 0,
2162 $processfee_amount, 'The right PF amountoutstanding is generated' );
2164 my $lost_fee_lines = Koha
::Account
::Lines
->search(
2165 { borrowernumber
=> $patron->id, itemnumber
=> $item->itemnumber, accounttype
=> 'L' } );
2166 is
( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2167 my $lost_fee_line = $lost_fee_lines->next;
2168 is
( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2169 is
( $lost_fee_line->amountoutstanding + 0,
2170 $replacement_amount, 'The right L amountountstanding is generated' );
2172 my $account = $patron->account;
2173 is
( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2176 my $payment_amount = 27;
2177 my $payment = $account->add_credit(
2178 { amount
=> $payment_amount,
2183 $payment->apply( { debits
=> $lost_fee_lines->reset, offset_type
=> 'Payment' } );
2185 # Partially write off fee
2186 my $write_off_amount = 25;
2187 my $write_off = $account->add_credit(
2188 { amount
=> $write_off_amount,
2192 $write_off->apply( { debits
=> $lost_fee_lines->reset, offset_type
=> 'Writeoff' } );
2194 is
( $account->balance,
2195 $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2196 'Payment and write off applied'
2199 # Store the amountoutstanding value
2200 $lost_fee_line->discard_changes;
2201 my $outstanding = $lost_fee_line->amountoutstanding;
2203 my $credit_return_id = C4
::Circulation
::_FixAccountForLostAndReturned
( $item->itemnumber, $patron->id );
2204 my $credit_return = Koha
::Account
::Lines
->find($credit_return_id);
2206 is
( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2208 $lost_fee_line->discard_changes;
2209 is
( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2210 is
( $lost_fee_line->accounttype,
2211 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2213 is
( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2214 is
( $credit_return->amount + 0,
2215 ($payment_amount + $outstanding ) * -1,
2216 'The account line of type CR has an amount equal to the payment + outstanding'
2218 is
( $credit_return->amountoutstanding + 0,
2219 $payment_amount * -1,
2220 'The account line of type CR has an amountoutstanding equal to the payment'
2223 is
( $account->balance,
2224 $processfee_amount - $payment_amount,
2225 'The patron balance is the difference between the PF and the credit'
2229 subtest
'Partial payement, existing debits and AccountAutoReconcile' => sub {
2233 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2234 my $barcode = 'KD123456793';
2235 my $replacement_amount = 100;
2236 my $processfee_amount = 20;
2238 my $item_type = $builder->build_object(
2239 { class => 'Koha::ItemTypes',
2241 notforloan
=> undef,
2243 defaultreplacecost
=> undef,
2248 my ( undef, undef, $item_id ) = AddItem
(
2249 { homebranch
=> $library->branchcode,
2250 holdingbranch
=> $library->branchcode,
2251 barcode
=> $barcode,
2252 replacementprice
=> $replacement_amount,
2253 itype
=> $item_type->itemtype
2255 $biblio->biblionumber
2258 AddIssue
( $patron->unblessed, $barcode );
2260 # Simulate item marked as lost
2261 ModItem
( { itemlost
=> 1 }, $biblio->biblionumber, $item_id );
2262 LostItem
( $item_id, 1 );
2264 my $lost_fee_lines = Koha
::Account
::Lines
->search(
2265 { borrowernumber
=> $patron->id, itemnumber
=> $item_id, accounttype
=> 'L' } );
2266 is
( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2267 my $lost_fee_line = $lost_fee_lines->next;
2268 is
( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2269 is
( $lost_fee_line->amountoutstanding + 0,
2270 $replacement_amount, 'The right L amountountstanding is generated' );
2272 my $account = $patron->account;
2273 is
( $account->balance, $replacement_amount, 'Balance is L' );
2276 my $payment_amount = 27;
2277 my $payment = $account->add_credit(
2278 { amount
=> $payment_amount,
2282 $payment->apply({ debits
=> $lost_fee_lines->reset, offset_type
=> 'Payment' });
2284 is
( $account->balance,
2285 $replacement_amount - $payment_amount,
2289 # TODO use add_debit when time comes
2290 my $manual_debit_amount = 80;
2291 C4
::Accounts
::manualinvoice
( $patron->id, undef, undef, 'FU', $manual_debit_amount );
2293 is
( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
2295 t
::lib
::Mocks
::mock_preference
( 'AccountAutoReconcile', 1 );
2297 my $credit_return_id = C4
::Circulation
::_FixAccountForLostAndReturned
( $item_id, $patron->id );
2298 my $credit_return = Koha
::Account
::Lines
->find($credit_return_id);
2300 is
( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2302 my $manual_debit = Koha
::Account
::Lines
->search({ borrowernumber
=> $patron->id, accounttype
=> 'FU' })->next;
2303 is
( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2307 subtest
'_FixOverduesOnReturn' => sub {
2310 my $biblio = $builder->build_sample_biblio({ author
=> 'Hall, Kylie' });
2312 my $branchcode = $library2->{branchcode
};
2314 my $item = $builder->build_sample_item(
2316 biblionumber
=> $biblio->biblionumber,
2317 library
=> $branchcode,
2318 replacementprice
=> 99.00,
2323 my $patron = $builder->build( { source
=> 'Borrower' } );
2325 ## Start with basic call, should just close out the open fine
2326 my $accountline = Koha
::Account
::Line
->new(
2328 borrowernumber
=> $patron->{borrowernumber
},
2329 accounttype
=> 'FU',
2330 itemnumber
=> $item->itemnumber,
2332 amountoutstanding
=> 99.00,
2333 lastincrement
=> 9.00,
2337 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber );
2339 $accountline->_result()->discard_changes();
2341 is
( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2342 is
( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2345 ## Run again, with exemptfine enabled
2348 accounttype
=> 'FU',
2349 amountoutstanding
=> 99.00,
2353 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber, 1 );
2355 $accountline->_result()->discard_changes();
2356 my $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $accountline->id, type
=> 'Forgiven' })->next();
2358 is
( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2359 is
( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
2360 is
( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2361 is
( $offset->amount, '-99.000000', "Amount of offset is correct" );
2363 ## Run again, with dropbox mode enabled
2366 accounttype
=> 'FU',
2367 amountoutstanding
=> 99.00,
2371 C4
::Circulation
::_FixOverduesOnReturn
( $patron->{borrowernumber
}, $item->itemnumber, 0, 1 );
2373 $accountline->_result()->discard_changes();
2374 $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $accountline->id, type
=> 'Dropbox' })->next();
2376 is
( $accountline->amountoutstanding + 0, 90, 'Fine has been reduced to 90' );
2377 is
( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
2378 is
( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via dropbox" );
2379 is
( $offset->amount, '-9.000000', "Amount of offset is correct" );
2382 subtest
'Set waiting flag' => sub {
2385 my $library_1 = $builder->build( { source
=> 'Branch' } );
2386 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_1->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2387 my $library_2 = $builder->build( { source
=> 'Branch' } );
2388 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_2->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2390 my $biblio = $builder->build( { source
=> 'Biblio' } );
2391 my $biblioitem = $builder->build( { source
=> 'Biblioitem', value
=> { biblionumber
=> $biblio->{biblionumber
} } } );
2393 my $item = $builder->build(
2397 homebranch
=> $library_1->{branchcode
},
2398 holdingbranch
=> $library_1->{branchcode
},
2402 biblionumber
=> $biblioitem->{biblionumber
},
2407 set_userenv
( $library_2 );
2408 my $reserve_id = AddReserve
(
2409 $library_2->{branchcode
}, $patron_2->{borrowernumber
}, $biblioitem->{biblionumber
},
2410 '', 1, undef, undef, '', undef, $item->{itemnumber
},
2413 set_userenv
( $library_1 );
2414 my $do_transfer = 1;
2415 my ( $res, $rr ) = AddReturn
( $item->{barcode
}, $library_1->{branchcode
} );
2416 ModReserveAffect
( $item->{itemnumber
}, undef, $do_transfer, $reserve_id );
2417 my $hold = Koha
::Holds
->find( $reserve_id );
2418 is
( $hold->found, 'T', 'Hold is in transit' );
2420 my ( $status ) = CheckReserves
($item->{itemnumber
});
2421 is
( $status, 'Reserved', 'Hold is not waiting yet');
2423 set_userenv
( $library_2 );
2425 AddReturn
( $item->{barcode
}, $library_2->{branchcode
} );
2426 ModReserveAffect
( $item->{itemnumber
}, undef, $do_transfer, $reserve_id );
2427 $hold = Koha
::Holds
->find( $reserve_id );
2428 is
( $hold->found, 'W', 'Hold is waiting' );
2429 ( $status ) = CheckReserves
($item->{itemnumber
});
2430 is
( $status, 'Waiting', 'Now the hold is waiting');
2433 subtest
'Cancel transfers on lost items' => sub {
2435 my $library_1 = $builder->build( { source
=> 'Branch' } );
2436 my $patron_1 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_1->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2437 my $library_2 = $builder->build( { source
=> 'Branch' } );
2438 my $patron_2 = $builder->build( { source
=> 'Borrower', value
=> { branchcode
=> $library_2->{branchcode
}, categorycode
=> $patron_category->{categorycode
} } } );
2439 my $biblio = $builder->build( { source
=> 'Biblio' } );
2440 my $biblioitem = $builder->build( { source
=> 'Biblioitem', value
=> { biblionumber
=> $biblio->{biblionumber
} } } );
2441 my $item = $builder->build(
2445 homebranch
=> $library_1->{branchcode
},
2446 holdingbranch
=> $library_1->{branchcode
},
2450 biblionumber
=> $biblioitem->{biblionumber
},
2455 set_userenv
( $library_2 );
2456 my $reserve_id = AddReserve
(
2457 $library_2->{branchcode
}, $patron_2->{borrowernumber
}, $biblioitem->{biblionumber
}, '', 1, undef, undef, '', undef, $item->{itemnumber
},
2460 #Return book and add transfer
2461 set_userenv
( $library_1 );
2462 my $do_transfer = 1;
2463 my ( $res, $rr ) = AddReturn
( $item->{barcode
}, $library_1->{branchcode
} );
2464 ModReserveAffect
( $item->{itemnumber
}, undef, $do_transfer, $reserve_id );
2465 C4
::Circulation
::transferbook
( $library_2->{branchcode
}, $item->{barcode
} );
2466 my $hold = Koha
::Holds
->find( $reserve_id );
2467 is
( $hold->found, 'T', 'Hold is in transit' );
2469 #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2470 my ($datesent,$frombranch,$tobranch) = GetTransfers
($item->{itemnumber
});
2471 is
( $tobranch, $library_2->{branchcode
}, 'The transfer record exists in the branchtransfers table');
2472 my $itemcheck = Koha
::Items
->find($item->{itemnumber
});
2473 is
( $itemcheck->holdingbranch, $library_2->{branchcode
}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2475 #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2476 ModItem
( { itemlost
=> 1 }, $biblio->{biblionumber
}, $item->{itemnumber
} );
2477 LostItem
( $item->{itemnumber
}, 'test', 1 );
2478 ($datesent,$frombranch,$tobranch) = GetTransfers
($item->{itemnumber
});
2479 is
( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2480 $itemcheck = Koha
::Items
->find($item->{itemnumber
});
2481 is
( $itemcheck->holdingbranch, $library_1->{branchcode
}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2484 subtest
'CanBookBeIssued | is_overdue' => sub {
2487 # Set a simple circ policy
2488 $dbh->do('DELETE FROM issuingrules');
2490 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2491 maxissueqty, issuelength, lengthunit,
2492 renewalsallowed, renewalperiod,
2493 norenewalbefore, auto_renew,
2510 my $five_days_go = output_pref
({ dt
=> dt_from_string
->add( days
=> 5 ), dateonly
=> 1});
2511 my $ten_days_go = output_pref
({ dt
=> dt_from_string
->add( days
=> 10), dateonly
=> 1 });
2512 my $library = $builder->build( { source
=> 'Branch' } );
2513 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } );
2515 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
2516 my $item = $builder->build(
2520 homebranch
=> $library->{branchcode
},
2521 holdingbranch
=> $library->{branchcode
},
2525 biblionumber
=> $biblioitem->{biblionumber
},
2530 my $issue = AddIssue
( $patron->unblessed, $item->{barcode
}, $five_days_go ); # date due was 10d ago
2531 my $actualissue = Koha
::Checkouts
->find( { itemnumber
=> $item->{itemnumber
} } );
2532 is
( output_pref
({ str
=> $actualissue->date_due, dateonly
=> 1}), $five_days_go, "First issue works");
2533 my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued
($patron,$item->{barcode
},$ten_days_go, undef, undef, undef);
2534 is
( $needsconfirmation->{RENEW_ISSUE
}, 1, "This is a renewal");
2535 is
( $needsconfirmation->{TOO_MANY
}, undef, "Not too many, is a renewal");
2538 subtest
'ItemsDeniedRenewal preference' => sub {
2541 C4
::Context
->set_preference('ItemsDeniedRenewal','');
2543 my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2546 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
2547 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2550 '*', $idr_lib->branchcode, '*', 25,
2557 my $deny_book = $builder->build_object({ class => 'Koha::Items', value
=> {
2558 homebranch
=> $idr_lib->branchcode,
2562 itemcallnumber
=> undef,
2566 my $allow_book = $builder->build_object({ class => 'Koha::Items', value
=> {
2567 homebranch
=> $idr_lib->branchcode,
2570 location
=> 'NOPROC'
2574 my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value
=> {
2575 branchcode
=> $idr_lib->branchcode,
2578 my $future = dt_from_string
->add( days
=> 1 );
2579 my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value
=> {
2580 returndate
=> undef,
2583 borrowernumber
=> $idr_borrower->borrowernumber,
2584 itemnumber
=> $deny_book->itemnumber,
2585 onsite_checkout
=> 0,
2586 date_due
=> $future,
2589 my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value
=> {
2590 returndate
=> undef,
2593 borrowernumber
=> $idr_borrower->borrowernumber,
2594 itemnumber
=> $allow_book->itemnumber,
2595 onsite_checkout
=> 0,
2596 date_due
=> $future,
2602 my ( $idr_mayrenew, $idr_error ) =
2603 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2604 is
( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2605 is
( $idr_error, undef, 'Renewal allowed when no rules' );
2607 $idr_rules="withdrawn: [1]";
2609 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2610 ( $idr_mayrenew, $idr_error ) =
2611 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2612 is
( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2613 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2614 ( $idr_mayrenew, $idr_error ) =
2615 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2616 is
( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2617 is
( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2619 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2621 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2622 ( $idr_mayrenew, $idr_error ) =
2623 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2624 is
( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2625 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2626 ( $idr_mayrenew, $idr_error ) =
2627 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2628 is
( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2629 is
( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2631 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2633 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2634 ( $idr_mayrenew, $idr_error ) =
2635 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2636 is
( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2637 is
( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2638 ( $idr_mayrenew, $idr_error ) =
2639 CanBookBeRenewed
( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2640 is
( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2641 is
( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2643 $idr_rules="itemcallnumber: [NULL]";
2644 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2645 ( $idr_mayrenew, $idr_error ) =
2646 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2647 is
( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2648 $idr_rules="itemcallnumber: ['']";
2649 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2650 ( $idr_mayrenew, $idr_error ) =
2651 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2652 is
( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2654 $idr_rules="itemnotes: [NULL]";
2655 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2656 ( $idr_mayrenew, $idr_error ) =
2657 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2658 is
( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2659 $idr_rules="itemnotes: ['']";
2660 C4
::Context
->set_preference('ItemsDeniedRenewal',$idr_rules);
2661 ( $idr_mayrenew, $idr_error ) =
2662 CanBookBeRenewed
( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2663 is
( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2666 subtest
'CanBookBeIssued | item-level_itypes=biblio' => sub {
2669 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0); # biblio
2670 my $library = $builder->build( { source
=> 'Branch' } );
2671 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } )->store;
2673 my $itemtype = $builder->build(
2675 source
=> 'Itemtype',
2676 value
=> { notforloan
=> undef, }
2680 my $biblioitem = $builder->build( { source
=> 'Biblioitem', value
=> { itemtype
=> $itemtype->{itemtype
} } } );
2681 my $item = $builder->build_object(
2683 class => 'Koha::Items',
2685 homebranch
=> $library->{branchcode
},
2686 holdingbranch
=> $library->{branchcode
},
2690 biblionumber
=> $biblioitem->{biblionumber
},
2691 biblioitemnumber
=> $biblioitem->{biblioitemnumber
},
2696 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2697 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
2698 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
2701 subtest
'CanBookBeIssued | notforloan' => sub {
2704 t
::lib
::Mocks
::mock_preference
('AllowNotForLoanOverride', 0);
2706 my $library = $builder->build( { source
=> 'Branch' } );
2707 my $patron = $builder->build_object( { class => 'Koha::Patrons', value
=> { categorycode
=> $patron_category->{categorycode
} } } )->store;
2709 my $itemtype = $builder->build(
2711 source
=> 'Itemtype',
2712 value
=> { notforloan
=> undef, }
2716 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
2717 my $item = $builder->build_object(
2719 class => 'Koha::Items',
2721 homebranch
=> $library->{branchcode
},
2722 holdingbranch
=> $library->{branchcode
},
2726 itype
=> $itemtype->{itemtype
},
2727 biblionumber
=> $biblioitem->{biblionumber
},
2728 biblioitemnumber
=> $biblioitem->{biblioitemnumber
},
2733 my ( $issuingimpossible, $needsconfirmation );
2736 subtest
'item-level_itypes = 1' => sub {
2739 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1); # item
2740 # Is for loan at item type and item level
2741 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2742 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
2743 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
2745 # not for loan at item type level
2746 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(1)->store;
2747 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2748 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2751 { NOT_FOR_LOAN
=> 1, itemtype_notforloan
=> $itemtype->{itemtype
} },
2752 'Item can not be issued, not for loan at item type level'
2755 # not for loan at item level
2756 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(undef)->store;
2757 $item->notforloan( 1 )->store;
2758 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2759 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2762 { NOT_FOR_LOAN
=> 1, item_notforloan
=> 1 },
2763 'Item can not be issued, not for loan at item type level'
2767 subtest
'item-level_itypes = 0' => sub {
2770 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0); # biblio
2772 # We set another itemtype for biblioitem
2773 my $itemtype = $builder->build(
2775 source
=> 'Itemtype',
2776 value
=> { notforloan
=> undef, }
2780 # for loan at item type and item level
2781 $item->notforloan(0)->store;
2782 $item->biblioitem->itemtype($itemtype->{itemtype
})->store;
2783 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2784 is_deeply
( $needsconfirmation, {}, 'Item can be issued to this patron' );
2785 is_deeply
( $issuingimpossible, {}, 'Item can be issued to this patron' );
2787 # not for loan at item type level
2788 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(1)->store;
2789 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2790 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2793 { NOT_FOR_LOAN
=> 1, itemtype_notforloan
=> $itemtype->{itemtype
} },
2794 'Item can not be issued, not for loan at item type level'
2797 # not for loan at item level
2798 Koha
::ItemTypes
->find( $itemtype->{itemtype
} )->notforloan(undef)->store;
2799 $item->notforloan( 1 )->store;
2800 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued
( $patron, $item->barcode, undef, undef, undef, undef );
2801 is_deeply
( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2804 { NOT_FOR_LOAN
=> 1, item_notforloan
=> 1 },
2805 'Item can not be issued, not for loan at item type level'
2809 # TODO test with AllowNotForLoanOverride = 1
2812 subtest
'AddReturn should clear items.onloan for unissued items' => sub {
2815 t
::lib
::Mocks
::mock_preference
( "AllowReturnToBranch", 'anywhere' );
2816 my $item = $builder->build_object({ class => 'Koha::Items', value
=> { onloan
=> '2018-01-01' }});
2817 AddReturn
( $item->barcode, $item->homebranch );
2818 $item->discard_changes; # refresh
2819 is
( $item->onloan, undef, 'AddReturn did clear items.onloan' );
2822 $schema->storage->txn_rollback;
2823 C4
::Context
->clear_syspref_cache();
2824 $cache->clear_from_cache('single_holidays');
2826 subtest
'AddRenewal and AddIssuingCharge tests' => sub {
2830 $schema->storage->txn_begin;
2832 my $issuing_charges = 15;
2833 my $title = 'A title';
2834 my $author = 'Author, An';
2835 my $barcode = 'WHATARETHEODDS';
2837 my $circ = Test
::MockModule
->new('C4::Circulation');
2839 'GetIssuingCharges',
2841 return $issuing_charges;
2845 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2846 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
2847 my $patron = $builder->build_object({
2848 class => 'Koha::Patrons',
2849 value
=> { branchcode
=> $library->id }
2852 my $biblio = $builder->build_sample_biblio({ title
=> $title, author
=> $author });
2853 my ( undef, undef, $item_id ) = AddItem
(
2855 homebranch
=> $library->id,
2856 holdingbranch
=> $library->id,
2857 barcode
=> $barcode,
2858 replacementprice
=> 23.00,
2859 itype
=> $itemtype->id
2861 $biblio->biblionumber
2863 my $item = Koha
::Items
->find( $item_id );
2865 my $context = Test
::MockModule
->new('C4::Context');
2866 $context->mock( userenv
=> { branch
=> $library->id } );
2868 # Check the item out
2869 AddIssue
( $patron->unblessed, $item->barcode );
2871 t
::lib
::Mocks
::mock_preference
( 'RenewalLog', 0 );
2872 my $date = output_pref
( { dt
=> dt_from_string
(), datenonly
=> 1, dateformat
=> 'iso' } );
2873 my $old_log_size = scalar( @
{ GetLogs
( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2874 AddRenewal
( $patron->id, $item->id, $library->id );
2875 my $new_log_size = scalar( @
{ GetLogs
( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2876 is
( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
2878 my $checkouts = $patron->checkouts;
2879 # The following will fail if run on 00:00:00
2880 unlike
( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
2882 t
::lib
::Mocks
::mock_preference
( 'RenewalLog', 1 );
2883 $date = output_pref
( { dt
=> dt_from_string
(), datenonly
=> 1, dateformat
=> 'iso' } );
2884 $old_log_size = scalar( @
{ GetLogs
( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2885 AddRenewal
( $patron->id, $item->id, $library->id );
2886 $new_log_size = scalar( @
{ GetLogs
( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
2887 is
( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
2889 my $lines = Koha
::Account
::Lines
->search({
2890 borrowernumber
=> $patron->id,
2891 itemnumber
=> $item->id
2894 is
( $lines->count, 3 );
2896 my $line = $lines->next;
2897 is
( $line->accounttype, 'Rent', 'The issuing charge generates an accountline' );
2898 is
( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' );
2899 is
( $line->description, 'Rental', 'AddIssuingCharge set a hardcoded description for the accountline' );
2901 $line = $lines->next;
2902 is
( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2903 is
( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2904 is
( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2906 $line = $lines->next;
2907 is
( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
2908 is
( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' );
2909 is
( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
2911 $schema->storage->txn_rollback;
2914 subtest
'ProcessOfflinePayment() tests' => sub {
2918 $schema->storage->txn_begin;
2922 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2923 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2924 my $result = C4
::Circulation
::ProcessOfflinePayment
({ cardnumber
=> $patron->cardnumber, amount
=> $amount, branchcode
=> $library->id });
2926 is
( $result, 'Success.', 'The right string is returned' );
2928 my $lines = $patron->account->lines;
2929 is
( $lines->count, 1, 'line created correctly');
2931 my $line = $lines->next;
2932 is
( $line->amount+0, $amount * -1, 'amount picked from params' );
2933 is
( $line->branchcode, $library->id, 'branchcode set correctly' );
2935 $schema->storage->txn_rollback;
2941 my ( $library ) = @_;
2942 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library->{branchcode
} });
2946 my ( $error, $question, $alert ) = @_;
2948 $s = %$error ?
' (error: ' . join( ' ', keys %$error ) . ')' : '';
2949 $s .= %$question ?
' (question: ' . join( ' ', keys %$question ) . ')' : '';
2950 $s .= %$alert ?
' (alert: ' . join( ' ', keys %$alert ) . ')' : '';
2954 sub test_debarment_on_checkout
{
2956 my $item = $params->{item
};
2957 my $library = $params->{library
};
2958 my $patron = $params->{patron
};
2959 my $due_date = $params->{due_date
} || dt_from_string
;
2960 my $return_date = $params->{return_date
} || dt_from_string
;
2961 my $expected_expiration_date = $params->{expiration_date
};
2963 $expected_expiration_date = output_pref
(
2965 dt
=> $expected_expiration_date,
2966 dateformat
=> 'sql',
2970 my @caller = caller;
2971 my $line_number = $caller[2];
2972 AddIssue
( $patron, $item->{barcode
}, $due_date );
2974 my ( undef, $message ) = AddReturn
( $item->{barcode
}, $library->{branchcode
},
2975 undef, undef, $return_date );
2976 is
( $message->{WasReturned
} && exists $message->{Debarred
}, 1, 'AddReturn must have debarred the patron' )
2977 or diag
('AddReturn returned message ' . Dumper
$message );
2978 my $debarments = Koha
::Patron
::Debarments
::GetDebarments
(
2979 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );
2980 is
( scalar(@
$debarments), 1, 'Test at line ' . $line_number );
2982 is
( $debarments->[0]->{expiration
},
2983 $expected_expiration_date, 'Test at line ' . $line_number );
2984 Koha
::Patron
::Debarments
::DelUniqueDebarment
(
2985 { borrowernumber
=> $patron->{borrowernumber
}, type
=> 'SUSPENSION' } );