3 # This file is part of Koha.
5 # Copyright (c) 2015 Mark Tompsett
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 7;
23 use t::lib::TestBuilder;
25 use List::MoreUtils qw( any none );
27 use C4::Biblio qw(AddBiblio);
30 use Koha::AuthorisedValues;
36 use_ok('C4::Context');
42 can_ok('C4::Items','GetItemsForInventory');
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
47 subtest 'Skip items with waiting holds' => sub {
51 $schema->storage->txn_begin;
53 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
55 = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
56 my $patron_1 = $builder->build_object(
57 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
58 my $patron_2 = $builder->build_object(
59 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
62 my $title_1 = 'Title 1, ';
63 my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
65 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_1 });
66 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_2 });
68 my ( $items_1, $first_items_count ) = GetItemsForInventory();
69 is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
71 # Add two items, so we don't depend on existing data
72 my $item_1 = $builder->build_object(
73 { class => 'Koha::Items',
75 biblionumber => $biblio_1->biblionumber,
76 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
77 homebranch => $library->id,
78 holdingbranch => $library->id,
79 itype => $itemtype->itemtype,
85 my $item_2 = $builder->build_object(
86 { class => 'Koha::Items',
88 biblionumber => $biblio_2->biblionumber,
89 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
90 homebranch => $library->id,
91 holdingbranch => $library->id,
92 itype => $itemtype->itemtype,
98 my ( $items_2, $second_items_count ) = GetItemsForInventory();
99 is( scalar @{$items_2}, $second_items_count, 'Results and count match' );
100 is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
102 my $real_itemtype_count = Koha::Items->search({ itype => $itemtype->itemtype })->count;
103 my $itype_str = "'" . $itemtype->itemtype . "'"; # manipulate string for db query
104 my ( $items_3, $itemtype_count ) = GetItemsForInventory({ itemtypes => [ $itype_str ] });
105 is( $itemtype_count, $real_itemtype_count, 'Itemtype filter gets correct number of inventory items' );
107 # Add 2 waiting holds
108 C4::Reserves::AddReserve(
110 branchcode => $library->branchcode,
111 borrowernumber => $patron_1->borrowernumber,
112 biblionumber => $item_1->biblionumber,
114 itemnumber => $item_1->itemnumber,
118 C4::Reserves::AddReserve(
120 branchcode => $library->branchcode,
121 borrowernumber => $patron_1->borrowernumber,
122 biblionumber => $item_2->biblionumber,
124 itemnumber => $item_2->itemnumber,
127 C4::Reserves::AddReserve(
129 branchcode => $library->branchcode,
130 borrowernumber => $patron_2->borrowernumber,
131 biblionumber => $item_2->biblionumber,
133 itemnumber => $item_2->itemnumber,
137 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
138 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
139 ok( (any { $_->{title} eq $title_2 } @{$new_items}),
140 'Item on hold skipped, the other one we added is present' );
141 ok( (none { $_->{title} eq $title_1 } @{$new_items}),
142 'Item on hold skipped, no one matches' );
143 is( scalar(@$new_items), $new_items_count, 'total and number of items is the same');
145 $schema->storage->txn_rollback;
148 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
149 $schema->storage->txn_begin;
152 my $builder = t::lib::TestBuilder->new;
154 my $class_rule = $builder->build({
155 source => 'ClassSortRule',
156 value => { sort_routine => "LCC" }
158 my $class_source = $builder->build({
159 source => 'ClassSource',
161 class_sort_rule => $class_rule->{class_sort_rule},
165 #Find if we have any items in our test range before we start
166 my( undef, $pre_item_count) = GetItemsForInventory({
167 maxlocation => 'GT100',
168 minlocation => 'GT90',
169 class_source => $class_source->{cn_source},
172 my $item_1 = $builder->build({ # Cannot call build_sample_item or cn_sort will be replaced by Koha::Item->store
175 itemcallnumber => 'GT95',
176 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
180 my( undef, $item_count) = GetItemsForInventory({
181 maxlocation => 'GT100',
182 minlocation => 'GT90',
183 class_source => $class_source->{cn_source},
185 is($item_count,$pre_item_count + 1,"We should return GT95 as between GT90 and GT100");
186 $schema->storage->txn_rollback;