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>.
26 use C4::Overdues qw(UpdateFine);
30 use Test::More tests => 70 ;
33 use_ok('C4::Circulation');
36 my $dbh = C4::Context->dbh;
37 my $schema = Koha::Database->new()->schema();
40 $dbh->{RaiseError} = 1;
41 $schema->storage->txn_begin();
43 # Start with a clean slate
44 $dbh->do('DELETE FROM issues');
46 my $CircControl = C4::Context->preference('CircControl');
47 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
51 holdingbranch => 'MPL'
58 # No userenv, PickupLibrary
59 C4::Context->set_preference('CircControl', 'PickupLibrary');
61 C4::Context->preference('CircControl'),
63 'CircControl changed to PickupLibrary'
66 C4::Circulation::_GetCircControlBranch($item, $borrower),
67 $item->{$HomeOrHoldingBranch},
68 '_GetCircControlBranch returned item branch (no userenv defined)'
71 # No userenv, PatronLibrary
72 C4::Context->set_preference('CircControl', 'PatronLibrary');
74 C4::Context->preference('CircControl'),
76 'CircControl changed to PatronLibrary'
79 C4::Circulation::_GetCircControlBranch($item, $borrower),
80 $borrower->{branchcode},
81 '_GetCircControlBranch returned borrower branch'
84 # No userenv, ItemHomeLibrary
85 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
87 C4::Context->preference('CircControl'),
89 'CircControl changed to ItemHomeLibrary'
92 $item->{$HomeOrHoldingBranch},
93 C4::Circulation::_GetCircControlBranch($item, $borrower),
94 '_GetCircControlBranch returned item branch'
98 C4::Context->_new_userenv('xxx');
99 C4::Context->set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
100 is(C4::Context->userenv->{branch}, 'MPL', 'userenv set');
102 # Userenv set, PickupLibrary
103 C4::Context->set_preference('CircControl', 'PickupLibrary');
105 C4::Context->preference('CircControl'),
107 'CircControl changed to PickupLibrary'
110 C4::Circulation::_GetCircControlBranch($item, $borrower),
112 '_GetCircControlBranch returned current branch'
115 # Userenv set, PatronLibrary
116 C4::Context->set_preference('CircControl', 'PatronLibrary');
118 C4::Context->preference('CircControl'),
120 'CircControl changed to PatronLibrary'
123 C4::Circulation::_GetCircControlBranch($item, $borrower),
124 $borrower->{branchcode},
125 '_GetCircControlBranch returned borrower branch'
128 # Userenv set, ItemHomeLibrary
129 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
131 C4::Context->preference('CircControl'),
133 'CircControl changed to ItemHomeLibrary'
136 C4::Circulation::_GetCircControlBranch($item, $borrower),
137 $item->{$HomeOrHoldingBranch},
138 '_GetCircControlBranch returned item branch'
141 # Reset initial configuration
142 C4::Context->set_preference('CircControl', $CircControl);
144 C4::Context->preference('CircControl'),
146 'CircControl reset to its initial value'
149 # Set a simple circ policy
150 $dbh->do('DELETE FROM issuingrules');
152 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
153 maxissueqty, issuelength, lengthunit,
154 renewalsallowed, renewalperiod,
155 norenewalbefore, auto_renew,
172 # Test C4::Circulation::ProcessOfflinePayment
173 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
175 my ( $original_count ) = $sth->fetchrow_array();
177 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', 'MPL' )");
179 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
182 my ( $new_count ) = $sth->fetchrow_array();
184 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
186 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
187 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
188 C4::Context->dbh->do("DELETE FROM accountlines");
190 # CanBookBeRenewed tests
192 # Generate test biblio
193 my $biblio = MARC::Record->new();
194 my $title = 'Silence in the library';
195 $biblio->append_fields(
196 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
197 MARC::Field->new('245', ' ', ' ', a => $title),
200 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
202 my $barcode = 'R00000342';
205 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
207 homebranch => $branch,
208 holdingbranch => $branch,
210 replacementprice => 12.00
215 my $barcode2 = 'R00000343';
216 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
218 homebranch => $branch,
219 holdingbranch => $branch,
220 barcode => $barcode2,
221 replacementprice => 23.00
226 my $barcode3 = 'R00000346';
227 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
229 homebranch => $branch,
230 holdingbranch => $branch,
231 barcode => $barcode3,
232 replacementprice => 23.00
241 my %renewing_borrower_data = (
243 surname => 'Renewal',
245 branchcode => $branch,
248 my %reserving_borrower_data = (
249 firstname => 'Katrin',
250 surname => 'Reservation',
252 branchcode => $branch,
255 my %hold_waiting_borrower_data = (
257 surname => 'Reservation',
259 branchcode => $branch,
262 my %restricted_borrower_data = (
263 firstname => 'Alice',
264 surname => 'Reservation',
266 debarred => '3228-01-01',
267 branchcode => $branch,
270 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
271 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
272 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
273 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
275 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
276 my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
283 my $checkitem = undef;
286 my $issue = AddIssue( $renewing_borrower, $barcode);
287 my $datedue = dt_from_string( $issue->date_due() );
288 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
290 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
291 $datedue = dt_from_string( $issue->date_due() );
292 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
295 my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
296 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
298 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
299 is( $renewokay, 1, 'Can renew, no holds for this title or item');
302 # Biblio-level hold, renewal test
304 $branch, $reserving_borrowernumber, $biblionumber,
305 $bibitems, $priority, $resdate, $expdate, $notes,
306 $title, $checkitem, $found
309 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
310 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
311 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
312 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
313 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
314 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
315 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
317 # Now let's add an item level hold, we should no longer be able to renew the item
318 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
320 borrowernumber => $hold_waiting_borrowernumber,
321 biblionumber => $biblionumber,
322 itemnumber => $itemnumber,
323 branchcode => $branch,
327 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
328 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
331 # 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
332 # be able to renew these items
333 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
335 borrowernumber => $hold_waiting_borrowernumber,
336 biblionumber => $biblionumber,
337 itemnumber => $itemnumber3,
338 branchcode => $branch,
343 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
344 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
345 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
346 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
347 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
349 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
350 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
351 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
353 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
354 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
355 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
357 my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
358 my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
359 AddIssue($reserving_borrower, $barcode3);
360 my $reserve = $dbh->selectrow_hashref(
361 'SELECT * FROM old_reserves WHERE reserve_id = ?',
365 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
367 # Item-level hold, renewal test
369 $branch, $reserving_borrowernumber, $biblionumber,
370 $bibitems, $priority, $resdate, $expdate, $notes,
371 $title, $itemnumber, $found
374 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
375 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
376 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
378 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
379 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
382 # Items can't fill hold for reasons
383 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
384 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
385 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
386 ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
388 # FIXME: Add more for itemtype not for loan etc.
390 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
391 my $barcode5 = 'R00000347';
392 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
394 homebranch => $branch,
395 holdingbranch => $branch,
396 barcode => $barcode5,
397 replacementprice => 23.00
401 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
402 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
404 C4::Context->set_preference('RestrictionBlockRenewing','1');
405 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
406 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
407 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
408 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
410 # Users cannot renew an overdue item
411 my $barcode6 = 'R00000348';
412 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
414 homebranch => $branch,
415 holdingbranch => $branch,
416 barcode => $barcode6,
417 replacementprice => 23.00
422 my $barcode7 = 'R00000349';
423 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
425 homebranch => $branch,
426 holdingbranch => $branch,
427 barcode => $barcode7,
428 replacementprice => 23.00
432 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
433 is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
435 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, DateTime->from_epoch(epoch => 1));
436 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: $passeddatedue1");
439 C4::Context->set_preference('OverduesBlockRenewing','blockitem');
440 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
441 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
442 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
443 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
446 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
447 CancelReserve({ reserve_id => $reserveid });
449 # Test automatic renewal before value for "norenewalbefore" in policy is set
450 my $barcode4 = '11235813';
451 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
453 homebranch => $branch,
454 holdingbranch => $branch,
455 barcode => $barcode4,
456 replacementprice => 16.00
461 AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
462 ( $renewokay, $error ) =
463 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
464 is( $renewokay, 0, 'Cannot renew, renewal is automatic' );
465 is( $error, 'auto_renew',
466 'Cannot renew, renewal is automatic (returned code is auto_renew)' );
468 # set policy to require that loans cannot be
469 # renewed until seven days prior to the due date
470 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
471 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
472 is( $renewokay, 0, 'Cannot renew, renewal is premature');
473 is( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
475 GetSoonestRenewDate($renewing_borrowernumber, $itemnumber),
476 $datedue->clone->add(days => -7),
477 'renewals permitted 7 days before due date, as expected',
480 # Test automatic renewal again
481 ( $renewokay, $error ) =
482 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
483 is( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
484 is( $error, 'auto_too_soon',
485 'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
490 # set policy to forbid renewals
491 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
493 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
494 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
495 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
497 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
498 C4::Context->set_preference('WhenLostForgiveFine','1');
499 C4::Context->set_preference('WhenLostChargeReplacementFee','1');
501 C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
502 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
504 LostItem( $itemnumber, 1 );
506 my $item = $schema->resultset('Item')->find( $itemnumber );
507 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
509 my $total_due = $dbh->selectrow_array(
510 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
511 undef, $renewing_borrower->{borrowernumber}
514 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
516 C4::Context->dbh->do("DELETE FROM accountlines");
518 C4::Context->set_preference('WhenLostForgiveFine','0');
519 C4::Context->set_preference('WhenLostChargeReplacementFee','0');
521 C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
522 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
524 LostItem( $itemnumber2, 0 );
526 my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
527 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
529 $total_due = $dbh->selectrow_array(
530 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
531 undef, $renewing_borrower->{borrowernumber}
534 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
536 my $now = dt_from_string();
537 my $future = dt_from_string();
538 $future->add( days => 7 );
539 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, 'MPL');
540 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
542 # Users cannot renew any item if there is an overdue item
543 C4::Context->set_preference('OverduesBlockRenewing','block');
544 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
545 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
546 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
547 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
552 # GetUpcomingDueIssues tests
553 my $barcode = 'R00000342';
554 my $barcode2 = 'R00000343';
555 my $barcode3 = 'R00000344';
558 #Create another record
559 my $biblio2 = MARC::Record->new();
560 my $title2 = 'Something is worng here';
561 $biblio2->append_fields(
562 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
563 MARC::Field->new('245', ' ', ' ', a => $title2),
565 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
570 homebranch => $branch,
571 holdingbranch => $branch,
578 my %a_borrower_data = (
579 firstname => 'Fridolyn',
582 branchcode => $branch,
585 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
586 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
588 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
589 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
590 my $today = DateTime->today(time_zone => C4::Context->tz());
592 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
593 my $datedue = dt_from_string( $issue->date_due() );
594 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
595 my $datedue2 = dt_from_string( $issue->date_due() );
599 # GetUpcomingDueIssues tests
601 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
602 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
605 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
606 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
607 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
610 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
611 is ( scalar( @$upcoming_dues ), 1,
612 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
615 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
617 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
619 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
620 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
622 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
623 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
625 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
626 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
628 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
629 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
631 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
632 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
634 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
635 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
640 my $barcode = '1234567890';
643 my $biblio = MARC::Record->new();
644 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
647 my ( undef, undef, $itemnumber ) = AddItem(
649 homebranch => $branch,
650 holdingbranch => $branch,
657 my %a_borrower_data = (
661 branchcode => $branch,
664 my $borrowernumber = AddMember(%a_borrower_data);
666 UpdateFine( $itemnumber, $borrowernumber, 0 );
668 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
669 my $count = $hr->{count};
671 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
675 $dbh->do('DELETE FROM issues');
676 $dbh->do('DELETE FROM items');
677 $dbh->do('DELETE FROM issuingrules');
680 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
681 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
690 my $biblio = MARC::Record->new();
691 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
693 my $barcode1 = '1234';
694 my ( undef, undef, $itemnumber1 ) = AddItem(
697 holdingbranch => 'MPL',
698 barcode => $barcode1,
702 my $barcode2 = '4321';
703 my ( undef, undef, $itemnumber2 ) = AddItem(
706 holdingbranch => 'MPL',
707 barcode => $barcode2,
712 my $borrowernumber1 = AddMember(
718 my $borrowernumber2 = AddMember(
719 firstname => 'Chelsea',
725 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
726 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
728 my $issue = AddIssue( $borrower1, $barcode1 );
730 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
731 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
734 'MPL', $borrowernumber2, $biblionumber,
735 '', 1, undef, undef, '',
739 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
740 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
741 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
742 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
744 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
745 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
746 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
747 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
749 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
750 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
751 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
752 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
754 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
755 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
756 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
757 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
759 # Setting item not checked out to be not for loan but holdable
760 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
762 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
763 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' );
767 # Don't allow renewing onsite checkout
768 my $barcode = 'R00000XXX';
771 #Create another record
772 my $biblio = MARC::Record->new();
773 $biblio->append_fields(
774 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
775 MARC::Field->new('245', ' ', ' ', a => 'A title'),
777 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
779 my (undef, undef, $itemnumber) = AddItem(
781 homebranch => $branch,
782 holdingbranch => $branch,
788 my $borrowernumber = AddMember(
792 branchcode => $branch,
795 my $borrower = GetMember( borrowernumber => $borrowernumber );
796 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
797 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
798 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
799 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
802 $schema->storage->txn_rollback();