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>.
27 use C4
::Overdues
qw(UpdateFine CalcFine);
31 use t
::lib
::TestBuilder
;
33 use Test
::More tests
=> 87;
36 use_ok
('C4::Circulation');
39 my $schema = Koha
::Database
->schema;
40 $schema->storage->txn_begin;
41 my $builder = t
::lib
::TestBuilder
->new;
42 my $dbh = C4
::Context
->dbh;
45 $dbh->{RaiseError
} = 1;
47 # Start with a clean slate
48 $dbh->do('DELETE FROM issues');
50 my $library = $builder->build({
53 my $library2 = $builder->build({
56 my $itemtype = $builder->build(
57 { source
=> 'Itemtype',
58 value
=> { notforloan
=> undef, rentalcharge
=> 0 }
62 my $CircControl = C4
::Context
->preference('CircControl');
63 my $HomeOrHoldingBranch = C4
::Context
->preference('HomeOrHoldingBranch');
66 homebranch
=> $library2->{branchcode
},
67 holdingbranch
=> $library2->{branchcode
}
71 branchcode
=> $library2->{branchcode
}
74 # No userenv, PickupLibrary
75 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
77 C4
::Context
->preference('CircControl'),
79 'CircControl changed to PickupLibrary'
82 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
83 $item->{$HomeOrHoldingBranch},
84 '_GetCircControlBranch returned item branch (no userenv defined)'
87 # No userenv, PatronLibrary
88 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
90 C4
::Context
->preference('CircControl'),
92 'CircControl changed to PatronLibrary'
95 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
96 $borrower->{branchcode
},
97 '_GetCircControlBranch returned borrower branch'
100 # No userenv, ItemHomeLibrary
101 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
103 C4
::Context
->preference('CircControl'),
105 'CircControl changed to ItemHomeLibrary'
108 $item->{$HomeOrHoldingBranch},
109 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
110 '_GetCircControlBranch returned item branch'
114 C4
::Context
->_new_userenv('xxx');
115 C4
::Context
->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode
}, 'Midway Public Library', '', '', '');
116 is
(C4
::Context
->userenv->{branch
}, $library2->{branchcode
}, 'userenv set');
118 # Userenv set, PickupLibrary
119 t
::lib
::Mocks
::mock_preference
('CircControl', 'PickupLibrary');
121 C4
::Context
->preference('CircControl'),
123 'CircControl changed to PickupLibrary'
126 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
127 $library2->{branchcode
},
128 '_GetCircControlBranch returned current branch'
131 # Userenv set, PatronLibrary
132 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
134 C4
::Context
->preference('CircControl'),
136 'CircControl changed to PatronLibrary'
139 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
140 $borrower->{branchcode
},
141 '_GetCircControlBranch returned borrower branch'
144 # Userenv set, ItemHomeLibrary
145 t
::lib
::Mocks
::mock_preference
('CircControl', 'ItemHomeLibrary');
147 C4
::Context
->preference('CircControl'),
149 'CircControl changed to ItemHomeLibrary'
152 C4
::Circulation
::_GetCircControlBranch
($item, $borrower),
153 $item->{$HomeOrHoldingBranch},
154 '_GetCircControlBranch returned item branch'
157 # Reset initial configuration
158 t
::lib
::Mocks
::mock_preference
('CircControl', $CircControl);
160 C4
::Context
->preference('CircControl'),
162 'CircControl reset to its initial value'
165 # Set a simple circ policy
166 $dbh->do('DELETE FROM issuingrules');
168 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
169 maxissueqty, issuelength, lengthunit,
170 renewalsallowed, renewalperiod,
171 norenewalbefore, auto_renew,
188 # Test C4::Circulation::ProcessOfflinePayment
189 my $sth = C4
::Context
->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
191 my ( $original_count ) = $sth->fetchrow_array();
193 C4
::Context
->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', ? )", undef, $library2->{branchcode
} );
195 C4
::Circulation
::ProcessOfflinePayment
({ cardnumber
=> '99999999999', amount
=> '123.45' });
198 my ( $new_count ) = $sth->fetchrow_array();
200 ok
( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' );
202 C4
::Context
->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
203 C4
::Context
->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
204 C4
::Context
->dbh->do("DELETE FROM accountlines");
206 # CanBookBeRenewed tests
208 # Generate test biblio
209 my $biblio = MARC
::Record
->new();
210 my $title = 'Silence in the library';
211 $biblio->append_fields(
212 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
213 MARC
::Field
->new('245', ' ', ' ', a
=> $title),
216 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
218 my $barcode = 'R00000342';
219 my $branch = $library2->{branchcode
};
221 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem
(
223 homebranch
=> $branch,
224 holdingbranch
=> $branch,
226 replacementprice
=> 12.00,
232 my $barcode2 = 'R00000343';
233 my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem
(
235 homebranch
=> $branch,
236 holdingbranch
=> $branch,
237 barcode
=> $barcode2,
238 replacementprice
=> 23.00,
244 my $barcode3 = 'R00000346';
245 my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem
(
247 homebranch
=> $branch,
248 holdingbranch
=> $branch,
249 barcode
=> $barcode3,
250 replacementprice
=> 23.00,
260 my %renewing_borrower_data = (
262 surname
=> 'Renewal',
264 branchcode
=> $branch,
267 my %reserving_borrower_data = (
268 firstname
=> 'Katrin',
269 surname
=> 'Reservation',
271 branchcode
=> $branch,
274 my %hold_waiting_borrower_data = (
276 surname
=> 'Reservation',
278 branchcode
=> $branch,
281 my %restricted_borrower_data = (
282 firstname
=> 'Alice',
283 surname
=> 'Reservation',
285 debarred
=> '3228-01-01',
286 branchcode
=> $branch,
289 my $renewing_borrowernumber = AddMember
(%renewing_borrower_data);
290 my $reserving_borrowernumber = AddMember
(%reserving_borrower_data);
291 my $hold_waiting_borrowernumber = AddMember
(%hold_waiting_borrower_data);
292 my $restricted_borrowernumber = AddMember
(%restricted_borrower_data);
294 my $renewing_borrower = GetMember
( borrowernumber
=> $renewing_borrowernumber );
295 my $restricted_borrower = GetMember
( borrowernumber
=> $restricted_borrowernumber );
302 my $checkitem = undef;
305 my $issue = AddIssue
( $renewing_borrower, $barcode);
306 my $datedue = dt_from_string
( $issue->date_due() );
307 is
(defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
309 my $issue2 = AddIssue
( $renewing_borrower, $barcode2);
310 $datedue = dt_from_string
( $issue->date_due() );
311 is
(defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
314 my $borrowing_borrowernumber = GetItemIssue
($itemnumber)->{borrowernumber
};
315 is
($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
317 my ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
318 is
( $renewokay, 1, 'Can renew, no holds for this title or item');
321 # Biblio-level hold, renewal test
323 $branch, $reserving_borrowernumber, $biblionumber,
324 $bibitems, $priority, $resdate, $expdate, $notes,
325 $title, $checkitem, $found
328 # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
329 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
330 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 1 );
331 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
332 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
333 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
334 is
( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
336 # Now let's add an item level hold, we should no longer be able to renew the item
337 my $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
339 borrowernumber
=> $hold_waiting_borrowernumber,
340 biblionumber
=> $biblionumber,
341 itemnumber
=> $itemnumber,
342 branchcode
=> $branch,
346 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
347 is
( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
350 # 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
351 # be able to renew these items
352 $hold = Koha
::Database
->new()->schema()->resultset('Reserve')->create(
354 borrowernumber
=> $hold_waiting_borrowernumber,
355 biblionumber
=> $biblionumber,
356 itemnumber
=> $itemnumber3,
357 branchcode
=> $branch,
362 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
363 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
364 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
365 is
( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
366 t
::lib
::Mocks
::mock_preference
('AllowRenewalIfOtherItemsAvailable', 0 );
368 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
369 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
370 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
372 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
373 is
( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
374 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
376 my $reserveid = C4
::Reserves
::GetReserveId
({ biblionumber
=> $biblionumber, borrowernumber
=> $reserving_borrowernumber});
377 my $reserving_borrower = GetMember
( borrowernumber
=> $reserving_borrowernumber );
378 AddIssue
($reserving_borrower, $barcode3);
379 my $reserve = $dbh->selectrow_hashref(
380 'SELECT * FROM old_reserves WHERE reserve_id = ?',
384 is
($reserve->{found
}, 'F', 'hold marked completed when checking out item that fills it');
386 # Item-level hold, renewal test
388 $branch, $reserving_borrowernumber, $biblionumber,
389 $bibitems, $priority, $resdate, $expdate, $notes,
390 $title, $itemnumber, $found
393 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
394 is
( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
395 is
( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
397 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2, 1);
398 is
( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
400 # Items can't fill hold for reasons
401 ModItem
({ notforloan
=> 1 }, $biblionumber, $itemnumber);
402 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber, 1);
403 is
( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
404 ModItem
({ notforloan
=> 0, itype
=> $itemtype }, $biblionumber, $itemnumber,1);
406 # FIXME: Add more for itemtype not for loan etc.
408 # Restricted users cannot renew when RestrictionBlockRenewing is enabled
409 my $barcode5 = 'R00000347';
410 my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem
(
412 homebranch
=> $branch,
413 holdingbranch
=> $branch,
414 barcode
=> $barcode5,
415 replacementprice
=> 23.00,
420 my $datedue5 = AddIssue
($restricted_borrower, $barcode5);
421 is
(defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
423 t
::lib
::Mocks
::mock_preference
('RestrictionBlockRenewing','1');
424 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber2);
425 is
( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
426 ( $renewokay, $error ) = CanBookBeRenewed
($restricted_borrowernumber, $itemnumber5);
427 is
( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
429 # Users cannot renew an overdue item
430 my $barcode6 = 'R00000348';
431 my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem
(
433 homebranch
=> $branch,
434 holdingbranch
=> $branch,
435 barcode
=> $barcode6,
436 replacementprice
=> 23.00,
442 my $barcode7 = 'R00000349';
443 my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem
(
445 homebranch
=> $branch,
446 holdingbranch
=> $branch,
447 barcode
=> $barcode7,
448 replacementprice
=> 23.00,
453 my $datedue6 = AddIssue
( $renewing_borrower, $barcode6);
454 is
(defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
456 my $now = dt_from_string
();
457 my $five_weeks = DateTime
::Duration
->new(weeks
=> 5);
458 my $five_weeks_ago = $now - $five_weeks;
460 my $passeddatedue1 = AddIssue
($renewing_borrower, $barcode7, $five_weeks_ago);
461 is
(defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
463 my ( $fine ) = CalcFine
( GetItem
(undef, $barcode7), $renewing_borrower->{categorycode
}, $branch, $five_weeks_ago, $now );
464 C4
::Overdues
::UpdateFine
(
466 issue_id
=> $passeddatedue1->id(),
467 itemnumber
=> $itemnumber7,
468 borrowernumber
=> $renewing_borrower->{borrowernumber
},
471 due
=> Koha
::DateUtils
::output_pref
($five_weeks_ago)
474 AddRenewal
( $renewing_borrower->{borrowernumber
}, $itemnumber7, $branch );
475 $fine = $schema->resultset('Accountline')->single( { borrowernumber
=> $renewing_borrower->{borrowernumber
}, itemnumber
=> $itemnumber7 } );
476 is
( $fine->accounttype, 'F', 'Fine on renewed item is closed out properly' );
479 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','blockitem');
480 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber6);
481 is
( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
482 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber7);
483 is
( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
486 $reserveid = C4
::Reserves
::GetReserveId
({ biblionumber
=> $biblionumber, itemnumber
=> $itemnumber, borrowernumber
=> $reserving_borrowernumber});
487 CancelReserve
({ reserve_id
=> $reserveid });
490 # Test automatic renewal before value for "norenewalbefore" in policy is set
491 # In this case automatic renewal is not permitted prior to due date
492 my $barcode4 = '11235813';
493 my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem
(
495 homebranch
=> $branch,
496 holdingbranch
=> $branch,
497 barcode
=> $barcode4,
498 replacementprice
=> 16.00,
504 $issue = AddIssue
( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew
=> 1 } );
505 ( $renewokay, $error ) =
506 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
507 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
508 is
( $error, 'auto_too_soon',
509 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
512 # Test premature manual renewal
513 $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
515 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
516 is
( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
517 is
( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
520 # Test 'exact time' setting for syspref NoRenewalBeforePrecision
521 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'exact_time' );
523 GetSoonestRenewDate
( $renewing_borrowernumber, $itemnumber ),
524 $datedue->clone->add( days
=> -7 ),
525 'Bug 14395: Renewals permitted 7 days before due date, as expected'
529 # Test 'date' setting for syspref NoRenewalBeforePrecision
530 t
::lib
::Mocks
::mock_preference
( 'NoRenewalBeforePrecision', 'date' );
532 GetSoonestRenewDate
( $renewing_borrowernumber, $itemnumber ),
533 $datedue->clone->add( days
=> -7 )->truncate( to
=> 'day' ),
534 'Bug 14395: Renewals permitted 7 days before due date, as expected'
538 # Test premature automatic renewal
539 ( $renewokay, $error ) =
540 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
541 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
542 is
( $error, 'auto_too_soon',
543 'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
546 # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
547 # and test automatic renewal again
548 $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
549 ( $renewokay, $error ) =
550 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
551 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
552 is
( $error, 'auto_too_soon',
553 'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
556 # Change policy so that loans can be renewed 99 days prior to the due date
557 # and test automatic renewal again
558 $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
559 ( $renewokay, $error ) =
560 CanBookBeRenewed
( $renewing_borrowernumber, $itemnumber4 );
561 is
( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
562 is
( $error, 'auto_renew',
563 'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
568 # set policy to forbid renewals
569 $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
571 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber);
572 is
( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
573 is
( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
575 # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
576 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','1');
577 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','1');
579 C4
::Overdues
::UpdateFine
(
581 issue_id
=> $issue->id(),
582 itemnumber
=> $itemnumber,
583 borrowernumber
=> $renewing_borrower->{borrowernumber
},
586 due
=> Koha
::DateUtils
::output_pref
($datedue)
590 LostItem
( $itemnumber, 1 );
592 my $item = Koha
::Database
->new()->schema()->resultset('Item')->find($itemnumber);
593 ok
( !$item->onloan(), "Lost item marked as returned has false onloan value" );
595 my $total_due = $dbh->selectrow_array(
596 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
597 undef, $renewing_borrower->{borrowernumber
}
600 ok
( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
602 C4
::Context
->dbh->do("DELETE FROM accountlines");
604 t
::lib
::Mocks
::mock_preference
('WhenLostForgiveFine','0');
605 t
::lib
::Mocks
::mock_preference
('WhenLostChargeReplacementFee','0');
607 C4
::Overdues
::UpdateFine
(
609 issue_id
=> $issue2->id(),
610 itemnumber
=> $itemnumber2,
611 borrowernumber
=> $renewing_borrower->{borrowernumber
},
614 due
=> Koha
::DateUtils
::output_pref
($datedue)
618 LostItem
( $itemnumber2, 0 );
620 my $item2 = Koha
::Database
->new()->schema()->resultset('Item')->find($itemnumber2);
621 ok
( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
623 $total_due = $dbh->selectrow_array(
624 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
625 undef, $renewing_borrower->{borrowernumber
}
628 ok
( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
630 my $future = dt_from_string
();
631 $future->add( days
=> 7 );
632 my $units = C4
::Overdues
::get_chargeable_units
('days', $future, $now, $library2->{branchcode
});
633 ok
( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
635 # Users cannot renew any item if there is an overdue item
636 t
::lib
::Mocks
::mock_preference
('OverduesBlockRenewing','block');
637 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber6);
638 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
639 ( $renewokay, $error ) = CanBookBeRenewed
($renewing_borrowernumber, $itemnumber7);
640 is
( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
645 # GetUpcomingDueIssues tests
646 my $barcode = 'R00000342';
647 my $barcode2 = 'R00000343';
648 my $barcode3 = 'R00000344';
649 my $branch = $library2->{branchcode
};
651 #Create another record
652 my $biblio2 = MARC
::Record
->new();
653 my $title2 = 'Something is worng here';
654 $biblio2->append_fields(
655 MARC
::Field
->new('100', ' ', ' ', a
=> 'Anonymous'),
656 MARC
::Field
->new('245', ' ', ' ', a
=> $title2),
658 my ($biblionumber2, $biblioitemnumber2) = AddBiblio
($biblio2, '');
663 homebranch
=> $branch,
664 holdingbranch
=> $branch,
665 barcode
=> $barcode3,
672 my %a_borrower_data = (
673 firstname
=> 'Fridolyn',
676 branchcode
=> $branch,
679 my $a_borrower_borrowernumber = AddMember
(%a_borrower_data);
680 my $a_borrower = GetMember
( borrowernumber
=> $a_borrower_borrowernumber );
682 my $yesterday = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> -1 );
683 my $two_days_ahead = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> 2 );
684 my $today = DateTime
->today(time_zone
=> C4
::Context
->tz());
686 my $issue = AddIssue
( $a_borrower, $barcode, $yesterday );
687 my $datedue = dt_from_string
( $issue->date_due() );
688 my $issue2 = AddIssue
( $a_borrower, $barcode2, $two_days_ahead );
689 my $datedue2 = dt_from_string
( $issue->date_due() );
693 # GetUpcomingDueIssues tests
695 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
696 is
( scalar( @
$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
699 #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
700 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
701 is
( scalar ( @
$upcoming_dues), 1, "Only one item due in 2 days or less" );
704 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> $i } );
705 is
( scalar( @
$upcoming_dues ), 1,
706 "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
709 # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
711 my $issue3 = AddIssue
( $a_borrower, $barcode3, $today );
713 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> -1 } );
714 is
( scalar ( @
$upcoming_dues), 0, "Overdues can not be selected" );
716 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 0 } );
717 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today" );
719 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 1 } );
720 is
( scalar ( @
$upcoming_dues), 1, "1 item is due today, none tomorrow" );
722 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 2 } );
723 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
725 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
( { days_in_advance
=> 3 } );
726 is
( scalar ( @
$upcoming_dues), 2, "2 items are due withing 2 days" );
728 $upcoming_dues = C4
::Circulation
::GetUpcomingDueIssues
();
729 is
( scalar ( @
$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
734 my $barcode = '1234567890';
735 my $branch = $library2->{branchcode
};
737 my $biblio = MARC
::Record
->new();
738 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
741 my ( undef, undef, $itemnumber ) = AddItem
(
743 homebranch
=> $branch,
744 holdingbranch
=> $branch,
752 my %a_borrower_data = (
756 branchcode
=> $branch,
759 my $borrowernumber = AddMember
(%a_borrower_data);
761 my $issue = AddIssue
( GetMember
( borrowernumber
=> $borrowernumber ), $barcode );
764 issue_id
=> $issue->id(),
765 itemnumber
=> $itemnumber,
766 borrowernumber
=> $borrowernumber,
772 my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
773 my $count = $hr->{count
};
775 is
( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
779 $dbh->do('DELETE FROM issues');
780 $dbh->do('DELETE FROM items');
781 $dbh->do('DELETE FROM issuingrules');
784 INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
785 norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
794 my $biblio = MARC
::Record
->new();
795 my ( $biblionumber, $biblioitemnumber ) = AddBiblio
( $biblio, '' );
797 my $barcode1 = '1234';
798 my ( undef, undef, $itemnumber1 ) = AddItem
(
800 homebranch
=> $library2->{branchcode
},
801 holdingbranch
=> $library2->{branchcode
},
802 barcode
=> $barcode1,
807 my $barcode2 = '4321';
808 my ( undef, undef, $itemnumber2 ) = AddItem
(
810 homebranch
=> $library2->{branchcode
},
811 holdingbranch
=> $library2->{branchcode
},
812 barcode
=> $barcode2,
818 my $borrowernumber1 = AddMember
(
822 branchcode
=> $library2->{branchcode
},
824 my $borrowernumber2 = AddMember
(
825 firstname
=> 'Chelsea',
828 branchcode
=> $library2->{branchcode
},
831 my $borrower1 = GetMember
( borrowernumber
=> $borrowernumber1 );
832 my $borrower2 = GetMember
( borrowernumber
=> $borrowernumber2 );
834 my $issue = AddIssue
( $borrower1, $barcode1 );
836 my ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
837 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
840 $library2->{branchcode
}, $borrowernumber2, $biblionumber,
841 '', 1, undef, undef, '',
845 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
846 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
847 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
848 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
850 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
851 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
852 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
853 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
855 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
856 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 0 );
857 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
858 is
( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
860 C4
::Context
->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
861 t
::lib
::Mocks
::mock_preference
( 'AllowRenewalIfOtherItemsAvailable', 1 );
862 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
863 is
( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
865 # Setting item not checked out to be not for loan but holdable
866 ModItem
({ notforloan
=> -1 }, $biblionumber, $itemnumber2);
868 ( $renewokay, $error ) = CanBookBeRenewed
( $borrowernumber1, $itemnumber1 );
869 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' );
873 # Don't allow renewing onsite checkout
874 my $barcode = 'R00000XXX';
875 my $branch = $library->{branchcode
};
877 #Create another record
878 my $biblio = MARC
::Record
->new();
879 $biblio->append_fields(
880 MARC
::Field
->new('100', ' ', ' ', a
=> 'Anonymous'),
881 MARC
::Field
->new('245', ' ', ' ', a
=> 'A title'),
883 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
885 my (undef, undef, $itemnumber) = AddItem
(
887 homebranch
=> $branch,
888 holdingbranch
=> $branch,
895 my $borrowernumber = AddMember
(
899 branchcode
=> $branch,
902 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
903 my $issue = AddIssue
( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout
=> 1 } );
904 my ( $renewed, $error ) = CanBookBeRenewed
( $borrowernumber, $itemnumber );
905 is
( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
906 is
( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
910 my $library = $builder->build({ source
=> 'Branch' });
912 my $biblio = MARC
::Record
->new();
913 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
915 my $barcode = 'just a barcode';
916 my ( undef, undef, $itemnumber ) = AddItem
(
918 homebranch
=> $library->{branchcode
},
919 holdingbranch
=> $library->{branchcode
},
926 my $patron = $builder->build({ source
=> 'Borrower', value
=> { branchcode
=> $library->{branchcode
} } } );
928 my $issue = AddIssue
( GetMember
( borrowernumber
=> $patron->{borrowernumber
} ), $barcode );
931 issue_id
=> $issue->id(),
932 itemnumber
=> $itemnumber,
933 borrowernumber
=> $patron->{borrowernumber
},
940 issue_id
=> $issue->id(),
941 itemnumber
=> $itemnumber,
942 borrowernumber
=> $patron->{borrowernumber
},
947 is
( Koha
::Account
::Lines
->search({ issue_id
=> $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
950 subtest
'CanBookBeIssued & AllowReturnToBranch' => sub {
953 my $homebranch = $builder->build( { source
=> 'Branch' } );
954 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
955 my $otherbranch = $builder->build( { source
=> 'Branch' } );
956 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
957 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
959 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
960 my $item = $builder->build(
963 homebranch
=> $homebranch->{branchcode
},
964 holdingbranch
=> $holdingbranch->{branchcode
},
968 biblionumber
=> $biblioitem->{biblionumber
}
973 set_userenv
($holdingbranch);
975 my $issue = AddIssue
( $patron_1, $item->{barcode
} );
976 is
( ref($issue), 'Koha::Schema::Result::Issue' ); # FIXME Should be Koha::Issue
978 my ( $error, $question, $alerts );
980 # AllowReturnToBranch == anywhere
981 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
982 ## Can be issued from homebranch
983 set_userenv
($homebranch);
984 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
985 is
( keys(%$error) + keys(%$alerts), 0 );
986 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1 );
987 ## Can be issued from holdingbranch
988 set_userenv
($holdingbranch);
989 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
990 is
( keys(%$error) + keys(%$alerts), 0 );
991 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1 );
992 ## Can be issued from another branch
993 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
994 is
( keys(%$error) + keys(%$alerts), 0 );
995 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1 );
997 # AllowReturnToBranch == holdingbranch
998 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
999 ## Cannot be issued from homebranch
1000 set_userenv
($homebranch);
1001 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1002 is
( keys(%$question) + keys(%$alerts), 0 );
1003 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1 );
1004 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
} );
1005 ## Can be issued from holdinbranch
1006 set_userenv
($holdingbranch);
1007 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1008 is
( keys(%$error) + keys(%$alerts), 0 );
1009 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1 );
1010 ## Cannot be issued from another branch
1011 set_userenv
($otherbranch);
1012 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1013 is
( keys(%$question) + keys(%$alerts), 0 );
1014 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1 );
1015 is
( $error->{branch_to_return
}, $holdingbranch->{branchcode
} );
1017 # AllowReturnToBranch == homebranch
1018 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1019 ## Can be issued from holdinbranch
1020 set_userenv
($homebranch);
1021 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1022 is
( keys(%$error) + keys(%$alerts), 0 );
1023 is
( exists $question->{ISSUED_TO_ANOTHER
}, 1 );
1024 ## Cannot be issued from holdinbranch
1025 set_userenv
($holdingbranch);
1026 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1027 is
( keys(%$question) + keys(%$alerts), 0 );
1028 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1 );
1029 is
( $error->{branch_to_return
}, $homebranch->{branchcode
} );
1030 ## Cannot be issued from holdinbranch
1031 set_userenv
($otherbranch);
1032 ( $error, $question, $alerts ) = CanBookBeIssued
( $patron_2, $item->{barcode
} );
1033 is
( keys(%$question) + keys(%$alerts), 0 );
1034 is
( exists $error->{RETURN_IMPOSSIBLE
}, 1 );
1035 is
( $error->{branch_to_return
}, $homebranch->{branchcode
} );
1037 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1040 subtest
'AddIssue & AllowReturnToBranch' => sub {
1043 my $homebranch = $builder->build( { source
=> 'Branch' } );
1044 my $holdingbranch = $builder->build( { source
=> 'Branch' } );
1045 my $otherbranch = $builder->build( { source
=> 'Branch' } );
1046 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
1047 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
1049 my $biblioitem = $builder->build( { source
=> 'Biblioitem' } );
1050 my $item = $builder->build(
1053 homebranch
=> $homebranch->{branchcode
},
1054 holdingbranch
=> $holdingbranch->{branchcode
},
1058 biblionumber
=> $biblioitem->{biblionumber
}
1063 set_userenv
($holdingbranch);
1065 my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Issue
1066 my $issue = AddIssue
( $patron_1, $item->{barcode
} );
1068 my ( $error, $question, $alerts );
1070 # AllowReturnToBranch == homebranch
1071 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'anywhere' );
1072 ## Can be issued from homebranch
1073 set_userenv
($homebranch);
1074 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1075 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1076 ## Can be issued from holdinbranch
1077 set_userenv
($holdingbranch);
1078 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1079 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1080 ## Can be issued from another branch
1081 set_userenv
($otherbranch);
1082 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1083 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1085 # AllowReturnToBranch == holdinbranch
1086 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'holdingbranch' );
1087 ## Cannot be issued from homebranch
1088 set_userenv
($homebranch);
1089 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1090 ## Can be issued from holdingbranch
1091 set_userenv
($holdingbranch);
1092 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1093 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1094 ## Cannot be issued from another branch
1095 set_userenv
($otherbranch);
1096 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1098 # AllowReturnToBranch == homebranch
1099 t
::lib
::Mocks
::mock_preference
( 'AllowReturnToBranch', 'homebranch' );
1100 ## Can be issued from homebranch
1101 set_userenv
($homebranch);
1102 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), $ref_issue );
1103 set_userenv
($holdingbranch); AddIssue
( $patron_1, $item->{barcode
} ); # Reinsert the original issue
1104 ## Cannot be issued from holdinbranch
1105 set_userenv
($holdingbranch);
1106 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1107 ## Cannot be issued from another branch
1108 set_userenv
($otherbranch);
1109 is
( ref( AddIssue
( $patron_2, $item->{barcode
} ) ), '' );
1110 # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1115 my ( $library ) = @_;
1116 C4
::Context
->set_userenv(0,0,0,'firstname','surname', $library->{branchcode
}, $library->{branchname
}, '', '', '');