Bug 22899: Add pending_hold method to Koha::Item
authorNick Clemens <nick@bywatersolutions.com>
Mon, 13 May 2019 20:09:05 +0000 (20:09 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 24 May 2019 13:34:23 +0000 (14:34 +0100)
To test:
 1 - Enable course reserves
 2 - Create a course
 3 - Add an item to the course
 4 - Attempt to view the course on the OPAC
 5 - Internal Server Error
 6 - Apply patch
 7 - Add an item to the holds queue by placing a hold and  running holds queue builder or:
     INSERT INTO tmp_holdsqueue (itemnumber) VALUES (###);
 8 - View the course page, note item appears 'Pending hold'
 9 - Remove the holdsqueue line
10 - View the course page, note item appears 'Available'
11 - prove -v t/db_dependent/Koha/Item.t

Signed-off-by: Hayley Mapley <hayleymapley@catalyst.net.nz>
I see Jonathan's comments about small improvements, but will sign off as
everything works as expected here.
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit f21ebad7d298f25e44771e5833d1d1d4d6d8f777)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/Item.pm
t/db_dependent/Koha/Item.t [new file with mode: 0644]

index f3799ef..bda145a 100644 (file)
@@ -315,6 +315,20 @@ sub add_to_rota {
     return $self;
 }
 
+=head3 pending_hold
+
+  my $is_pending_hold = $item->pending_hold();
+
+This method checks the tmp_holdsqueue to see if this item has been selected for a hold, but not filled yet and returns true or false
+
+=cut
+
+sub pending_hold {
+    my ( $self ) = @_;
+    my $pending_hold = $self->_result->tmp_holdsqueues;
+    return !C4::Context->preference('AllowItemsOnHoldCheckout') && $pending_hold->count ? 1: 0;
+}
+
 =head2 Internal methods
 
 =head3 _type
diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t
new file mode 100644 (file)
index 0000000..0a6a029
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+# Copyright 2019 Koha Development team
+#
+# 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 Test::More tests => 1;
+
+use Koha::Items;
+use Koha::Database;
+
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'pending_hold() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $dbh = C4::Context->dbh;
+    my $item  = $builder->build_sample_item({ itemlost => 0 });
+    my $itemnumber = $item->itemnumber;
+
+    # disable AllowItemsOnHoldCheckout as it ignores pending holds
+    t::lib::Mocks::mock_preference( 'AllowItemsOnHoldCheckout', 0 );
+    $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
+    ok( $item->pending_hold, "Yes, we have a pending hold");
+    t::lib::Mocks::mock_preference( 'AllowItemsOnHoldCheckout', 1 );
+    ok( !$item->pending_hold, "We don't consider a pending hold if hold items can be checked out");
+    t::lib::Mocks::mock_preference( 'AllowItemsOnHoldCheckout', 0 );
+    $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
+    ok( !$item->pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
+};