Bug 5786 - Move AllowOnShelfHolds and OPACItemHolds system prefs to the Circulation...
[koha.git] / t / db_dependent / Circulation.t
index a6a8e15..5b5a3e6 100755 (executable)
@@ -1,5 +1,20 @@
 #!/usr/bin/perl
 
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
 use Modern::Perl;
 
 use DateTime;
@@ -8,14 +23,17 @@ use C4::Branch;
 use C4::Items;
 use C4::Members;
 use C4::Reserves;
+use Koha::DateUtils;
+use Koha::Database;
 
-use Test::More tests => 44;
+use Test::More tests => 59;
 
 BEGIN {
     use_ok('C4::Circulation');
 }
 
 my $dbh = C4::Context->dbh;
+my $schema = Koha::Database->new()->schema();
 
 # Start transaction
 $dbh->{AutoCommit} = 0;
@@ -75,9 +93,9 @@ is(
     '_GetCircControlBranch returned item branch'
 );
 
-diag('Now, set a userenv');
+# Now, set a userenv
 C4::Context->_new_userenv('xxx');
-C4::Context::set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
+C4::Context->set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
 is(C4::Context->userenv->{branch}, 'MPL', 'userenv set');
 
 # Userenv set, PickupLibrary
@@ -133,10 +151,12 @@ $dbh->do(
     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
                                 maxissueqty, issuelength, lengthunit,
                                 renewalsallowed, renewalperiod,
+                                norenewalbefore, auto_renew,
                                 fine, chargeperiod)
       VALUES (?, ?, ?, ?,
               ?, ?, ?,
               ?, ?,
+              ?, ?,
               ?, ?
              )
     },
@@ -144,6 +164,7 @@ $dbh->do(
     '*', '*', '*', 25,
     20, 14, 'days',
     1, 7,
+    '', 0,
     .10, 1
 );
 
@@ -201,7 +222,18 @@ C4::Context->dbh->do("DELETE FROM accountlines");
         $biblionumber
     );
 
-    # Create 2 borrowers
+    my $barcode3 = 'R00000346';
+    my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
+        {
+            homebranch       => $branch,
+            holdingbranch    => $branch,
+            barcode          => $barcode3,
+            replacementprice => 23.00
+        },
+        $biblionumber
+    );
+
+    # Create borrowers
     my %renewing_borrower_data = (
         firstname =>  'John',
         surname => 'Renewal',
@@ -216,8 +248,16 @@ C4::Context->dbh->do("DELETE FROM accountlines");
         branchcode => $branch,
     );
 
+    my %hold_waiting_borrower_data = (
+        firstname =>  'Kyle',
+        surname => 'Reservation',
+        categorycode => 'S',
+        branchcode => $branch,
+    );
+
     my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
     my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
+    my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
 
     my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
 
@@ -243,13 +283,38 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     is( $renewokay, 1, 'Can renew, no holds for this title or item');
 
 
-    diag("Biblio-level hold, renewal test");
+    # Biblio-level hold, renewal test
     AddReserve(
         $branch, $reserving_borrowernumber, $biblionumber,
         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
         $title, $checkitem, $found
     );
 
+    # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
+    C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
+    C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
+    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
+    is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
+    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
+    is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
+    # 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
+    # be able to renew these items
+    my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
+        {
+            borrowernumber => $hold_waiting_borrowernumber,
+            biblionumber   => $biblionumber,
+            itemnumber     => $itemnumber3,
+            branchcode     => $branch,
+            priority       => 0,
+            found          => 'W'
+        }
+    );
+    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
+    is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
+    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
+    is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
+    C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
+
     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
@@ -259,10 +324,16 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
 
     my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
-    CancelReserve({ reserve_id => $reserveid });
-
+    my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
+    AddIssue($reserving_borrower, $barcode3);
+    my $reserve = $dbh->selectrow_hashref(
+        'SELECT * FROM old_reserves WHERE reserve_id = ?',
+        { Slice => {} },
+        $reserveid
+    );
+    is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
 
-    diag("Item-level hold, renewal test");
+    # Item-level hold, renewal test
     AddReserve(
         $branch, $reserving_borrowernumber, $biblionumber,
         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
@@ -277,7 +348,7 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
 
 
-    diag("Items can't fill hold for reasons");
+    # Items can't fill hold for reasons
     ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
     is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
@@ -288,17 +359,55 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
     CancelReserve({ reserve_id => $reserveid });
 
-    diag("Too many renewals");
+    # Test automatic renewal before value for "norenewalbefore" in policy is set
+    my $barcode4 = '11235813';
+    my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
+        {
+            homebranch       => $branch,
+            holdingbranch    => $branch,
+            barcode          => $barcode4,
+            replacementprice => 16.00
+        },
+        $biblionumber
+    );
+
+    AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
+    ( $renewokay, $error ) =
+      CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
+    is( $renewokay, 0, 'Cannot renew, renewal is automatic' );
+    is( $error, 'auto_renew',
+        'Cannot renew, renewal is automatic (returned code is auto_renew)' );
+
+    # set policy to require that loans cannot be
+    # renewed until seven days prior to the due date
+    $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
+    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
+    is( $renewokay, 0, 'Cannot renew, renewal is premature');
+    is( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
+    is(
+        GetSoonestRenewDate($renewing_borrowernumber, $itemnumber),
+        $datedue->clone->add(days => -7),
+        'renewals permitted 7 days before due date, as expected',
+    );
+
+    # Test automatic renewal again
+    ( $renewokay, $error ) =
+      CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
+    is( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
+    is( $error, 'auto_too_soon',
+'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
+    );
+
+    # Too many renewals
 
     # set policy to forbid renewals
-    $dbh->do('UPDATE issuingrules SET renewalsallowed = 0');
+    $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
 
     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
     is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
     is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
 
     # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
-    diag("WhenLostForgiveFine and WhenLostChargeReplacementFee");
     C4::Context->set_preference('WhenLostForgiveFine','1');
     C4::Context->set_preference('WhenLostChargeReplacementFee','1');
 
@@ -307,6 +416,9 @@ C4::Context->dbh->do("DELETE FROM accountlines");
 
     LostItem( $itemnumber, 1 );
 
+    my $item = $schema->resultset('Item')->find( $itemnumber );
+    ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
+
     my $total_due = $dbh->selectrow_array(
         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
         undef, $renewing_borrower->{borrowernumber}
@@ -322,7 +434,10 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
         15.00, q{}, Koha::DateUtils::output_pref($datedue) );
 
-    LostItem( $itemnumber2, 1 );
+    LostItem( $itemnumber2, 0 );
+
+    my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
+    ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
 
     $total_due = $dbh->selectrow_array(
         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
@@ -330,6 +445,12 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     );
 
     ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
+
+    my $now = dt_from_string();
+    my $future = dt_from_string();
+    $future->add( days => 7 );
+    my $units = C4::Overdues::_get_chargeable_units('days', $future, $now, 'MPL');
+    ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
 }
 
 {
@@ -378,15 +499,14 @@ C4::Context->dbh->do("DELETE FROM accountlines");
 
     my $upcoming_dues;
 
-    diag( "GetUpcomingDueIssues tests" );
-
+    # GetUpcomingDueIssues tests
     for my $i(0..1) {
         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
         is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
     }
 
     #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
-        $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
+    $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
     is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
 
     for my $i(3..5) {
@@ -420,3 +540,5 @@ C4::Context->dbh->do("DELETE FROM accountlines");
 }
 
 $dbh->rollback;
+
+1;