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 t::lib::TestBuilder;
32 use Test::More tests => 83;
35 use_ok('C4::Circulation');
38 my $schema = Koha::Database->schema;
39 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
41 my $dbh = C4::Context->dbh;
44 $dbh->{RaiseError} = 1;
46 # Start with a clean slate
47 $dbh->do('DELETE FROM issues');
49 my $library = $builder->build({
52 my $library2 = $builder->build({
56 my $CircControl = C4::Context->preference('CircControl');
57 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
60 homebranch => $library2->{branchcode},
61 holdingbranch => $library2->{branchcode}
65 branchcode => $library2->{branchcode}
68 # No userenv, PickupLibrary
69 C4::Context->set_preference('CircControl', 'PickupLibrary');
71 C4::Context->preference('CircControl'),
73 'CircControl changed to PickupLibrary'
76 C4::Circulation::_GetCircControlBranch($item, $borrower),
77 $item->{$HomeOrHoldingBranch},
78 '_GetCircControlBranch returned item branch (no userenv defined)'
81 # No userenv, PatronLibrary
82 C4::Context->set_preference('CircControl', 'PatronLibrary');
84 C4::Context->preference('CircControl'),
86 'CircControl changed to PatronLibrary'
89 C4::Circulation::_GetCircControlBranch($item, $borrower),
90 $borrower->{branchcode},
91 '_GetCircControlBranch returned borrower branch'
94 # No userenv, ItemHomeLibrary
95 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
97 C4::Context->preference('CircControl'),
99 'CircControl changed to ItemHomeLibrary'
102 $item->{$HomeOrHoldingBranch},
103 C4::Circulation::_GetCircControlBranch($item, $borrower),
104 '_GetCircControlBranch returned item branch'
108 C4::Context->_new_userenv('xxx');
109 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
110 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
112 # Userenv set, PickupLibrary
113 C4::Context->set_preference('CircControl', 'PickupLibrary');
115 C4::Context->preference('CircControl'),
117 'CircControl changed to PickupLibrary'
120 C4::Circulation::_GetCircControlBranch($item, $borrower),
121 $library2->{branchcode},
122 '_GetCircControlBranch returned current branch'
125 # Userenv set, PatronLibrary
126 C4::Context->set_preference('CircControl', 'PatronLibrary');
128 C4::Context->preference('CircControl'),
130 'CircControl changed to PatronLibrary'
133 C4::Circulation::_GetCircControlBranch($item, $borrower),
134 $borrower->{branchcode},
135 '_GetCircControlBranch returned borrower branch'
138 # Userenv set, ItemHomeLibrary
139 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
141 C4::Context->preference('CircControl'),
143 'CircControl changed to ItemHomeLibrary'
146 C4::Circulation::_GetCircControlBranch($item, $borrower),
147 $item->{$HomeOrHoldingBranch},
148 '_GetCircControlBranch returned item branch'
151 # Reset initial configuration
152 C4::Context->set_preference('CircControl', $CircControl);
154 C4::Context->preference('CircControl'),
156 'CircControl reset to its initial value'
159 # Set a simple circ policy
160 $dbh->do('DELETE FROM issuingrules');
162 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
163 maxissueqty, issuelength, lengthunit,
164 renewalsallowed, renewalperiod,
165 norenewalbefore, auto_renew,
182 # Test C4::Circulation::ProcessOfflinePayment
183 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
185 my ( $original_count ) = $sth->fetchrow_array();
187 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', ? )", undef, $library2->{branchcode} );
189 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
192 my ( $new_count ) = $sth->fetchrow_array();
194 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
196 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
197 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
198 C4::Context->dbh->do("DELETE FROM accountlines");
200 # CanBookBeRenewed tests
202 # Generate test biblio
203 my $biblio = MARC::Record->new();
204 my $title = 'Silence in the library';
205 $biblio->append_fields(
206 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
207 MARC::Field->new('245', ' ', ' ', a => $title),
210 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
212 my $barcode = 'R00000342';
213 my $branch = $library2->{branchcode};
215 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
217 homebranch => $branch,
218 holdingbranch => $branch,
220 replacementprice => 12.00
225 my $barcode2 = 'R00000343';
226 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
228 homebranch => $branch,
229 holdingbranch => $branch,
230 barcode => $barcode2,
231 replacementprice => 23.00
236 my $barcode3 = 'R00000346';
237 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
239 homebranch => $branch,
240 holdingbranch => $branch,
241 barcode => $barcode3,
242 replacementprice => 23.00
251 my %renewing_borrower_data = (
253 surname => 'Renewal',
255 branchcode => $branch,
258 my %reserving_borrower_data = (
259 firstname => 'Katrin',
260 surname => 'Reservation',
262 branchcode => $branch,
265 my %hold_waiting_borrower_data = (
267 surname => 'Reservation',
269 branchcode => $branch,
272 my %restricted_borrower_data = (
273 firstname => 'Alice',
274 surname => 'Reservation',
276 debarred => '3228-01-01',
277 branchcode => $branch,
280 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
281 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
282 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
283 my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
285 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
286 my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
293 my $checkitem = undef;
296 my $issue = AddIssue( $renewing_borrower, $barcode);
297 my $datedue = dt_from_string( $issue->date_due() );
298 is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
300 my $issue2 = AddIssue( $renewing_borrower, $barcode2);
301 $datedue = dt_from_string( $issue->date_due() );
302 is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
305 my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
306 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
308 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
309 is( $renewokay, 1, 'Can renew, no holds for this title or item');
312 # Biblio-level hold, renewal test
314 $branch, $reserving_borrowernumber, $biblionumber,
315 $bibitems, $priority, $resdate, $expdate, $notes,
316 $title, $checkitem, $found
319 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
320 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
321 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
322 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
323 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
324 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
325 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
327 # Now let's add an item level hold, we should no longer be able to renew the item
328 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
330 borrowernumber => $hold_waiting_borrowernumber,
331 biblionumber => $biblionumber,
332 itemnumber => $itemnumber,
333 branchcode => $branch,
337 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
338 is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
341 # 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
342 # be able to renew these items
343 $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
345 borrowernumber => $hold_waiting_borrowernumber,
346 biblionumber => $biblionumber,
347 itemnumber => $itemnumber3,
348 branchcode => $branch,
353 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
354 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
355 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
356 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
357 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
359 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
360 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
361 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
363 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
364 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
365 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
367 my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
368 my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
369 AddIssue($reserving_borrower, $barcode3);
370 my $reserve = $dbh->selectrow_hashref(
371 'SELECT * FROM old_reserves WHERE reserve_id = ?',
375 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
377 # Item-level hold, renewal test
379 $branch, $reserving_borrowernumber, $biblionumber,
380 $bibitems, $priority, $resdate, $expdate, $notes,
381 $title, $itemnumber, $found
384 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
385 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
386 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
388 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
389 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
391 # Items can't fill hold for reasons
392 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
393 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
394 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
395 ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
397 # FIXME: Add more for itemtype not for loan etc.
399 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
400 my $barcode5 = 'R00000347';
401 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
403 homebranch => $branch,
404 holdingbranch => $branch,
405 barcode => $barcode5,
406 replacementprice => 23.00
410 my $datedue5 = AddIssue($restricted_borrower, $barcode5);
411 is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
413 C4::Context->set_preference('RestrictionBlockRenewing','1');
414 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
415 is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
416 ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
417 is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
419 # Users cannot renew an overdue item
420 my $barcode6 = 'R00000348';
421 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
423 homebranch => $branch,
424 holdingbranch => $branch,
425 barcode => $barcode6,
426 replacementprice => 23.00
431 my $barcode7 = 'R00000349';
432 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
434 homebranch => $branch,
435 holdingbranch => $branch,
436 barcode => $barcode7,
437 replacementprice => 23.00
441 my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
442 is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
444 my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, DateTime->from_epoch(epoch => 1));
445 is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: $passeddatedue1");
448 C4::Context->set_preference('OverduesBlockRenewing','blockitem');
449 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
450 is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
451 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
452 is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
455 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
456 CancelReserve({ reserve_id => $reserveid });
459 # Test automatic renewal before value for "norenewalbefore" in policy is set
460 # In this case automatic renewal is not permitted prior to due date
461 my $barcode4 = '11235813';
462 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
464 homebranch => $branch,
465 holdingbranch => $branch,
466 barcode => $barcode4,
467 replacementprice => 16.00
472 AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef,
473 { auto_renew => 1 } );
474 ( $renewokay, $error ) =
475 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
476 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
477 is( $error, 'auto_too_soon',
478 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
481 # Test premature manual renewal
482 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
484 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
485 is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
486 is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
489 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
490 C4::Context->set_preference( 'NoRenewalBeforePrecision', 'exact_time' );
492 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
493 $datedue->clone->add( days => -7 ),
494 'Bug 14395: Renewals permitted 7 days before due date, as expected'
498 # Test 'date' setting for syspref NoRenewalBeforePrecision
499 C4::Context->set_preference( 'NoRenewalBeforePrecision', 'date' );
501 GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
502 $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
503 'Bug 14395: Renewals permitted 7 days before due date, as expected'
507 # Test premature automatic renewal
508 ( $renewokay, $error ) =
509 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
510 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
511 is( $error, 'auto_too_soon',
512 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
515 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
516 # and test automatic renewal again
517 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
518 ( $renewokay, $error ) =
519 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
520 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
521 is( $error, 'auto_too_soon',
522 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
525 # Change policy so that loans can be renewed 99 days prior to the due date
526 # and test automatic renewal again
527 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
528 ( $renewokay, $error ) =
529 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
530 is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
531 is( $error, 'auto_renew',
532 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
537 # set policy to forbid renewals
538 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
540 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
541 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
542 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
544 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
545 C4::Context->set_preference('WhenLostForgiveFine','1');
546 C4::Context->set_preference('WhenLostChargeReplacementFee','1');
548 C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
549 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
551 LostItem( $itemnumber, 1 );
553 my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
554 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
556 my $total_due = $dbh->selectrow_array(
557 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
558 undef, $renewing_borrower->{borrowernumber}
561 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
563 C4::Context->dbh->do("DELETE FROM accountlines");
565 C4::Context->set_preference('WhenLostForgiveFine','0');
566 C4::Context->set_preference('WhenLostChargeReplacementFee','0');
568 C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
569 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
571 LostItem( $itemnumber2, 0 );
573 my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
574 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
576 $total_due = $dbh->selectrow_array(
577 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
578 undef, $renewing_borrower->{borrowernumber}
581 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
583 my $now = dt_from_string();
584 my $future = dt_from_string();
585 $future->add( days => 7 );
586 my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
587 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
589 # Users cannot renew any item if there is an overdue item
590 C4::Context->set_preference('OverduesBlockRenewing','block');
591 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
592 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
593 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
594 is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
599 # GetUpcomingDueIssues tests
600 my $barcode = 'R00000342';
601 my $barcode2 = 'R00000343';
602 my $barcode3 = 'R00000344';
603 my $branch = $library2->{branchcode};
605 #Create another record
606 my $biblio2 = MARC::Record->new();
607 my $title2 = 'Something is worng here';
608 $biblio2->append_fields(
609 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
610 MARC::Field->new('245', ' ', ' ', a => $title2),
612 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
617 homebranch => $branch,
618 holdingbranch => $branch,
625 my %a_borrower_data = (
626 firstname => 'Fridolyn',
629 branchcode => $branch,
632 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
633 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
635 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
636 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
637 my $today = DateTime->today(time_zone => C4::Context->tz());
639 my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
640 my $datedue = dt_from_string( $issue->date_due() );
641 my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
642 my $datedue2 = dt_from_string( $issue->date_due() );
646 # GetUpcomingDueIssues tests
648 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
649 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
652 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
653 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
654 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
657 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
658 is ( scalar( @$upcoming_dues ), 1,
659 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
662 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
664 my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
666 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
667 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
669 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
670 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
672 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
673 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
675 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
676 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
678 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
679 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
681 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
682 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
687 my $barcode = '1234567890';
688 my $branch = $library2->{branchcode};
690 my $biblio = MARC::Record->new();
691 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
694 my ( undef, undef, $itemnumber ) = AddItem(
696 homebranch => $branch,
697 holdingbranch => $branch,
704 my %a_borrower_data = (
708 branchcode => $branch,
711 my $borrowernumber = AddMember(%a_borrower_data);
713 UpdateFine( $itemnumber, $borrowernumber, 0 );
715 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
716 my $count = $hr->{count};
718 is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
722 $dbh->do('DELETE FROM issues');
723 $dbh->do('DELETE FROM items');
724 $dbh->do('DELETE FROM issuingrules');
727 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
728 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
737 my $biblio = MARC::Record->new();
738 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
740 my $barcode1 = '1234';
741 my ( undef, undef, $itemnumber1 ) = AddItem(
743 homebranch => $library2->{branchcode},
744 holdingbranch => $library2->{branchcode},
745 barcode => $barcode1,
749 my $barcode2 = '4321';
750 my ( undef, undef, $itemnumber2 ) = AddItem(
752 homebranch => $library2->{branchcode},
753 holdingbranch => $library2->{branchcode},
754 barcode => $barcode2,
759 my $borrowernumber1 = AddMember(
763 branchcode => $library2->{branchcode},
765 my $borrowernumber2 = AddMember(
766 firstname => 'Chelsea',
769 branchcode => $library2->{branchcode},
772 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
773 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
775 my $issue = AddIssue( $borrower1, $barcode1 );
777 my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
778 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
781 $library2->{branchcode}, $borrowernumber2, $biblionumber,
782 '', 1, undef, undef, '',
786 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
787 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
788 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
789 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
791 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
792 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
793 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
794 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
796 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
797 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
798 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
799 is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
801 C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
802 C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
803 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
804 is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
806 # Setting item not checked out to be not for loan but holdable
807 ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
809 ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
810 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' );
814 # Don't allow renewing onsite checkout
815 my $barcode = 'R00000XXX';
816 my $branch = $library->{branchcode};
818 #Create another record
819 my $biblio = MARC::Record->new();
820 $biblio->append_fields(
821 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
822 MARC::Field->new('245', ' ', ' ', a => 'A title'),
824 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
826 my (undef, undef, $itemnumber) = AddItem(
828 homebranch => $branch,
829 holdingbranch => $branch,
835 my $borrowernumber = AddMember(
839 branchcode => $branch,
842 my $borrower = GetMember( borrowernumber => $borrowernumber );
843 my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
844 my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
845 is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
846 is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );