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>.
29 use Test::More tests => 59;
32 use_ok('C4::Circulation');
35 my $dbh = C4::Context->dbh;
36 my $schema = Koha::Database->new()->schema();
39 $dbh->{AutoCommit} = 0;
40 $dbh->{RaiseError} = 1;
42 # Start with a clean slate
43 $dbh->do('DELETE FROM issues');
45 my $CircControl = C4::Context->preference('CircControl');
46 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
50 holdingbranch => 'MPL'
57 # No userenv, PickupLibrary
58 C4::Context->set_preference('CircControl', 'PickupLibrary');
60 C4::Context->preference('CircControl'),
62 'CircControl changed to PickupLibrary'
65 C4::Circulation::_GetCircControlBranch($item, $borrower),
66 $item->{$HomeOrHoldingBranch},
67 '_GetCircControlBranch returned item branch (no userenv defined)'
70 # No userenv, PatronLibrary
71 C4::Context->set_preference('CircControl', 'PatronLibrary');
73 C4::Context->preference('CircControl'),
75 'CircControl changed to PatronLibrary'
78 C4::Circulation::_GetCircControlBranch($item, $borrower),
79 $borrower->{branchcode},
80 '_GetCircControlBranch returned borrower branch'
83 # No userenv, ItemHomeLibrary
84 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
86 C4::Context->preference('CircControl'),
88 'CircControl changed to ItemHomeLibrary'
91 $item->{$HomeOrHoldingBranch},
92 C4::Circulation::_GetCircControlBranch($item, $borrower),
93 '_GetCircControlBranch returned item branch'
97 C4::Context->_new_userenv('xxx');
98 C4::Context::set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
99 is(C4::Context->userenv->{branch}, 'MPL', 'userenv set');
101 # Userenv set, PickupLibrary
102 C4::Context->set_preference('CircControl', 'PickupLibrary');
104 C4::Context->preference('CircControl'),
106 'CircControl changed to PickupLibrary'
109 C4::Circulation::_GetCircControlBranch($item, $borrower),
111 '_GetCircControlBranch returned current branch'
114 # Userenv set, PatronLibrary
115 C4::Context->set_preference('CircControl', 'PatronLibrary');
117 C4::Context->preference('CircControl'),
119 'CircControl changed to PatronLibrary'
122 C4::Circulation::_GetCircControlBranch($item, $borrower),
123 $borrower->{branchcode},
124 '_GetCircControlBranch returned borrower branch'
127 # Userenv set, ItemHomeLibrary
128 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
130 C4::Context->preference('CircControl'),
132 'CircControl changed to ItemHomeLibrary'
135 C4::Circulation::_GetCircControlBranch($item, $borrower),
136 $item->{$HomeOrHoldingBranch},
137 '_GetCircControlBranch returned item branch'
140 # Reset initial configuration
141 C4::Context->set_preference('CircControl', $CircControl);
143 C4::Context->preference('CircControl'),
145 'CircControl reset to its initial value'
148 # Set a simple circ policy
149 $dbh->do('DELETE FROM issuingrules');
151 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
152 maxissueqty, issuelength, lengthunit,
153 renewalsallowed, renewalperiod,
154 norenewalbefore, auto_renew,
171 # Test C4::Circulation::ProcessOfflinePayment
172 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
174 my ( $original_count ) = $sth->fetchrow_array();
176 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', 'MPL' )");
178 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
181 my ( $new_count ) = $sth->fetchrow_array();
183 ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
185 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
186 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
187 C4::Context->dbh->do("DELETE FROM accountlines");
189 # CanBookBeRenewed tests
191 # Generate test biblio
192 my $biblio = MARC::Record->new();
193 my $title = 'Silence in the library';
194 $biblio->append_fields(
195 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
196 MARC::Field->new('245', ' ', ' ', a => $title),
199 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
201 my $barcode = 'R00000342';
204 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
206 homebranch => $branch,
207 holdingbranch => $branch,
209 replacementprice => 12.00
214 my $barcode2 = 'R00000343';
215 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
217 homebranch => $branch,
218 holdingbranch => $branch,
219 barcode => $barcode2,
220 replacementprice => 23.00
225 my $barcode3 = 'R00000346';
226 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
228 homebranch => $branch,
229 holdingbranch => $branch,
230 barcode => $barcode3,
231 replacementprice => 23.00
237 my %renewing_borrower_data = (
239 surname => 'Renewal',
241 branchcode => $branch,
244 my %reserving_borrower_data = (
245 firstname => 'Katrin',
246 surname => 'Reservation',
248 branchcode => $branch,
251 my %hold_waiting_borrower_data = (
253 surname => 'Reservation',
255 branchcode => $branch,
258 my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
259 my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
260 my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
262 my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
264 my $constraint = 'a';
270 my $checkitem = undef;
273 my $datedue = AddIssue( $renewing_borrower, $barcode);
274 is (defined $datedue, 1, "Item 1 checked out, due date: $datedue");
276 my $datedue2 = AddIssue( $renewing_borrower, $barcode2);
277 is (defined $datedue2, 1, "Item 2 checked out, due date: $datedue2");
279 my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
280 is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
282 my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
283 is( $renewokay, 1, 'Can renew, no holds for this title or item');
286 # Biblio-level hold, renewal test
288 $branch, $reserving_borrowernumber, $biblionumber,
289 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
290 $title, $checkitem, $found
293 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
294 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
295 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
296 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
297 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
298 is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
299 # 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
300 # be able to renew these items
301 my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
303 borrowernumber => $hold_waiting_borrowernumber,
304 biblionumber => $biblionumber,
305 itemnumber => $itemnumber3,
306 branchcode => $branch,
311 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
312 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
313 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
314 is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
315 C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
317 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
318 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
319 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
321 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
322 is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
323 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
325 my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
326 my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
327 AddIssue($reserving_borrower, $barcode3);
328 my $reserve = $dbh->selectrow_hashref(
329 'SELECT * FROM old_reserves WHERE reserve_id = ?',
333 is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
335 # Item-level hold, renewal test
337 $branch, $reserving_borrowernumber, $biblionumber,
338 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
339 $title, $itemnumber, $found
342 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
343 is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
344 is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
346 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
347 is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
350 # Items can't fill hold for reasons
351 ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
352 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
353 is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
354 ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
356 # FIXME: Add more for itemtype not for loan etc.
358 $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
359 CancelReserve({ reserve_id => $reserveid });
361 # Test automatic renewal before value for "norenewalbefore" in policy is set
362 my $barcode4 = '11235813';
363 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
365 homebranch => $branch,
366 holdingbranch => $branch,
367 barcode => $barcode4,
368 replacementprice => 16.00
373 AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
374 ( $renewokay, $error ) =
375 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
376 is( $renewokay, 0, 'Cannot renew, renewal is automatic' );
377 is( $error, 'auto_renew',
378 'Cannot renew, renewal is automatic (returned code is auto_renew)' );
380 # set policy to require that loans cannot be
381 # renewed until seven days prior to the due date
382 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
383 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
384 is( $renewokay, 0, 'Cannot renew, renewal is premature');
385 is( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
387 GetSoonestRenewDate($renewing_borrowernumber, $itemnumber),
388 $datedue->clone->add(days => -7),
389 'renewals permitted 7 days before due date, as expected',
392 # Test automatic renewal again
393 ( $renewokay, $error ) =
394 CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
395 is( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
396 is( $error, 'auto_too_soon',
397 'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
402 # set policy to forbid renewals
403 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
405 ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
406 is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
407 is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
409 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
410 C4::Context->set_preference('WhenLostForgiveFine','1');
411 C4::Context->set_preference('WhenLostChargeReplacementFee','1');
413 C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
414 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
416 LostItem( $itemnumber, 1 );
418 my $item = $schema->resultset('Item')->find( $itemnumber );
419 ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
421 my $total_due = $dbh->selectrow_array(
422 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
423 undef, $renewing_borrower->{borrowernumber}
426 ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
428 C4::Context->dbh->do("DELETE FROM accountlines");
430 C4::Context->set_preference('WhenLostForgiveFine','0');
431 C4::Context->set_preference('WhenLostChargeReplacementFee','0');
433 C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
434 15.00, q{}, Koha::DateUtils::output_pref($datedue) );
436 LostItem( $itemnumber2, 0 );
438 my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
439 ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
441 $total_due = $dbh->selectrow_array(
442 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
443 undef, $renewing_borrower->{borrowernumber}
446 ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
448 my $now = dt_from_string();
449 my $future = dt_from_string();
450 $future->add( days => 7 );
451 my $units = C4::Overdues::_get_chargeable_units('days', $future, $now, 'MPL');
452 ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
456 # GetUpcomingDueIssues tests
457 my $barcode = 'R00000342';
458 my $barcode2 = 'R00000343';
459 my $barcode3 = 'R00000344';
462 #Create another record
463 my $biblio2 = MARC::Record->new();
464 my $title2 = 'Something is worng here';
465 $biblio2->append_fields(
466 MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
467 MARC::Field->new('245', ' ', ' ', a => $title2),
469 my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
474 homebranch => $branch,
475 holdingbranch => $branch,
482 my %a_borrower_data = (
483 firstname => 'Fridolyn',
486 branchcode => $branch,
489 my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
490 my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
492 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
493 my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
494 my $today = DateTime->today(time_zone => C4::Context->tz());
496 my $datedue = AddIssue( $a_borrower, $barcode, $yesterday );
497 my $datedue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
501 # GetUpcomingDueIssues tests
503 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
504 is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
507 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
508 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
509 is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
512 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
513 is ( scalar( @$upcoming_dues ), 1,
514 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
517 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
519 my $datedue3 = AddIssue( $a_borrower, $barcode3, $today );
521 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
522 is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
524 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
525 is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
527 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
528 is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
530 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
531 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
533 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
534 is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
536 $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
537 is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );