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
=> 61;
33 use_ok
('C4::Circulation');
36 my $dbh = C4
::Context
->dbh;
37 my $schema = Koha
::Database
->new()->schema();
40 $dbh->{AutoCommit
} = 0;
41 $dbh->{RaiseError
} = 1;
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
238 my %renewing_borrower_data = (
240 surname
=> 'Renewal',
242 branchcode
=> $branch,
245 my %reserving_borrower_data = (
246 firstname
=> 'Katrin',
247 surname
=> 'Reservation',
249 branchcode
=> $branch,
252 my %hold_waiting_borrower_data = (
254 surname
=> 'Reservation',
256 branchcode
=> $branch,
259 my $renewing_borrowernumber = AddMember
(%renewing_borrower_data);
260 my $reserving_borrowernumber = AddMember
(%reserving_borrower_data);
261 my $hold_waiting_borrowernumber = AddMember
(%hold_waiting_borrower_data);
263 my $renewing_borrower = GetMember
( borrowernumber
=> $renewing_borrowernumber );
265 my $constraint = 'a';
271 my $checkitem = undef;
274 my $issue = AddIssue
( $renewing_borrower, $barcode);
275 my $datedue = dt_from_string
( $issue->date_due() );
276 is
(defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
278 my $issue2 = AddIssue
( $renewing_borrower, $barcode2);
279 $datedue = dt_from_string
( $issue->date_due() );
280 is
(defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
282 my $borrowing_borrowernumber = GetItemIssue
($itemnumber)->{borrowernumber
};
283 is
($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
285 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
286 is
( $renewokay, 1, 'Can renew, no holds for this title or item');
289 # Biblio-level hold, renewal test
291 $branch, $reserving_borrowernumber, $biblionumber,
292 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
293 $title, $checkitem, $found
296 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
297 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
298 C4
::Context
->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
299 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
300 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
301 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
302 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
304 # Now let's add an item level hold, we should no longer be able to renew the item
305 my $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
307 borrowernumber
=> $hold_waiting_borrowernumber,
308 biblionumber
=> $biblionumber,
309 itemnumber
=> $itemnumber,
310 branchcode
=> $branch,
314 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
315 is
( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
318 # 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
319 # be able to renew these items
320 $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
322 borrowernumber
=> $hold_waiting_borrowernumber,
323 biblionumber
=> $biblionumber,
324 itemnumber
=> $itemnumber3,
325 branchcode
=> $branch,
330 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
331 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
332 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
333 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
334 C4
::Context
->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
336 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
337 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
338 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
340 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
341 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
342 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
344 my $reserveid = C4
::Reserves
::GetReserveId
({ biblionumber
=> $biblionumber, borrowernumber
=> $reserving_borrowernumber});
345 my $reserving_borrower = GetMember
( borrowernumber
=> $reserving_borrowernumber );
346 AddIssue
($reserving_borrower, $barcode3);
347 my $reserve = $dbh->selectrow_hashref(
348 'SELECT * FROM old_reserves WHERE reserve_id = ?',
352 is
($reserve->{found
}, 'F', 'hold marked completed when checking out item that fills it');
354 # Item-level hold, renewal test
356 $branch, $reserving_borrowernumber, $biblionumber,
357 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
358 $title, $itemnumber, $found
361 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
362 is
( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
363 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
365 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2, 1);
366 is
( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
369 # Items can't fill hold for reasons
370 ModItem
({ notforloan
=> 1 }, $biblionumber, $itemnumber);
371 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
372 is
( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
373 ModItem
({ notforloan
=> 0, itype
=> '' }, $biblionumber, $itemnumber,1);
375 # FIXME: Add more for itemtype not for loan etc.
377 $reserveid = C4
::Reserves
::GetReserveId
({ biblionumber
=> $biblionumber, itemnumber
=> $itemnumber, borrowernumber
=> $reserving_borrowernumber});
378 CancelReserve
({ reserve_id
=> $reserveid });
380 # Test automatic renewal before value for "norenewalbefore" in policy is set
381 my $barcode4 = '11235813';
382 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem
(
384 homebranch
=> $branch,
385 holdingbranch
=> $branch,
386 barcode
=> $barcode4,
387 replacementprice
=> 16.00
392 AddIssue
( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew
=> 1 } );
393 ( $renewokay, $error ) =
394 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
395 is
( $renewokay, 0, 'Cannot renew, renewal is automatic' );
396 is
( $error, 'auto_renew',
397 'Cannot renew, renewal is automatic (returned code is auto_renew)' );
399 # set policy to require that loans cannot be
400 # renewed until seven days prior to the due date
401 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
402 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
403 is
( $renewokay, 0, 'Cannot renew, renewal is premature');
404 is
( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
406 GetSoonestRenewDate
($renewing_borrowernumber, $itemnumber),
407 $datedue->clone->add(days
=> -7),
408 'renewals permitted 7 days before due date, as expected',
411 # Test automatic renewal again
412 ( $renewokay, $error ) =
413 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
414 is
( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
415 is
( $error, 'auto_too_soon',
416 'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
421 # set policy to forbid renewals
422 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
424 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
425 is
( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
426 is
( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
428 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
429 C4
::Context
->set_preference('WhenLostForgiveFine','1');
430 C4
::Context
->set_preference('WhenLostChargeReplacementFee','1');
432 C4
::Overdues
::UpdateFine
( $itemnumber, $renewing_borrower->{borrowernumber
},
433 15.00, q{}, Koha
::DateUtils
::output_pref
($datedue) );
435 LostItem
( $itemnumber, 1 );
437 my $item = $schema->resultset('Item')->find( $itemnumber );
438 ok
( !$item->onloan(), "Lost item marked as returned has false onloan value" );
440 my $total_due = $dbh->selectrow_array(
441 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
442 undef, $renewing_borrower->{borrowernumber
}
445 ok
( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
447 C4
::Context
->dbh->do("DELETE FROM accountlines");
449 C4
::Context
->set_preference('WhenLostForgiveFine','0');
450 C4
::Context
->set_preference('WhenLostChargeReplacementFee','0');
452 C4
::Overdues
::UpdateFine
( $itemnumber2, $renewing_borrower->{borrowernumber
},
453 15.00, q{}, Koha
::DateUtils
::output_pref
($datedue) );
455 LostItem
( $itemnumber2, 0 );
457 my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
458 ok
( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
460 $total_due = $dbh->selectrow_array(
461 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
462 undef, $renewing_borrower->{borrowernumber
}
465 ok
( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
467 my $now = dt_from_string
();
468 my $future = dt_from_string
();
469 $future->add( days
=> 7 );
470 my $units = C4
::Overdues
::get_chargeable_units
('days', $future, $now, 'MPL');
471 ok
( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
475 # GetUpcomingDueIssues tests
476 my $barcode = 'R00000342';
477 my $barcode2 = 'R00000343';
478 my $barcode3 = 'R00000344';
481 #Create another record
482 my $biblio2 = MARC
::Record
->new();
483 my $title2 = 'Something is worng here';
484 $biblio2->append_fields(
485 MARC
::Field
->new('100', ' ', ' ', a
=> 'Anonymous'),
486 MARC
::Field
->new('245', ' ', ' ', a
=> $title2),
488 my ($biblionumber2, $biblioitemnumber2) = AddBiblio
($biblio2, '');
493 homebranch
=> $branch,
494 holdingbranch
=> $branch,
501 my %a_borrower_data = (
502 firstname
=> 'Fridolyn',
505 branchcode
=> $branch,
508 my $a_borrower_borrowernumber = AddMember
(%a_borrower_data);
509 my $a_borrower = GetMember
( borrowernumber
=> $a_borrower_borrowernumber );
511 my $yesterday = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> -1 );
512 my $two_days_ahead = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> 2 );
513 my $today = DateTime
->today(time_zone
=> C4
::Context
->tz());
515 my $issue = AddIssue
( $a_borrower, $barcode, $yesterday );
516 my $datedue = dt_from_string
( $issue->date_due() );
517 my $issue2 = AddIssue
( $a_borrower, $barcode2, $two_days_ahead );
518 my $datedue2 = dt_from_string
( $issue->date_due() );
522 # GetUpcomingDueIssues tests
524 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
525 is
( scalar( @
$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
528 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
529 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
530 is
( scalar ( @
$upcoming_dues), 1, "Only one item due in 2 days or less" );
533 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
534 is
( scalar( @
$upcoming_dues ), 1,
535 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
538 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
540 my $issue3 = AddIssue
( $a_borrower, $barcode3, $today );
542 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> -1 } );
543 is
( scalar ( @
$upcoming_dues), 0, "Overdues can not be selected" );
545 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 0 } );
546 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today" );
548 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 1 } );
549 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today, none tomorrow" );
551 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
552 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
554 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 3 } );
555 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
557 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
();
558 is
( scalar ( @
$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
563 my $barcode = '1234567890';
566 my $biblio = MARC
::Record
->new();
567 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
570 my ( undef, undef, $itemnumber ) = AddItem
(
572 homebranch
=> $branch,
573 holdingbranch
=> $branch,
580 my %a_borrower_data = (
584 branchcode
=> $branch,
587 my $borrowernumber = AddMember
(%a_borrower_data);
589 UpdateFine
( $itemnumber, $borrowernumber, 0 );
591 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
592 my $count = $hr->{count
};
594 is
( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );