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 # Add 2 waiting holds
103 C4::Reserves::AddReserve( $library->branchcode, $patron_1->borrowernumber,
104 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
105 $item_1->itemnumber, 'W' );
106 C4::Reserves::AddReserve( $library->branchcode, $patron_1->borrowernumber,
107 $item_2->biblionumber, '', 1, undef, undef, '', "title for fee",
108 $item_2->itemnumber, undef );
109 C4::Reserves::AddReserve( $library->branchcode, $patron_2->borrowernumber,
110 $item_2->biblionumber, '', 2, undef, undef, '', "title for fee",
111 $item_2->itemnumber, undef );
113 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
114 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
115 ok( (any { $_->{title} eq $title_2 } @{$new_items}),
116 'Item on hold skipped, the other one we added is present' );
117 ok( (none { $_->{title} eq $title_1 } @{$new_items}),
118 'Item on hold skipped, no one matches' );
119 is( scalar(@$new_items), $new_items_count, 'total and number of items is the same');
121 $schema->storage->txn_rollback;
124 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
125 $schema->storage->txn_begin;
128 my $builder = t::lib::TestBuilder->new;
130 my $class_rule = $builder->build({
131 source => 'ClassSortRule',
132 value => { sort_routine => "LCC" }
134 my $class_source = $builder->build({
135 source => 'ClassSource',
137 class_sort_rule => $class_rule->{class_sort_rule},
141 #Find if we have any items in our test range before we start
142 my( undef, $pre_item_count) = GetItemsForInventory({
143 maxlocation => 'GT100',
144 minlocation => 'GT90',
145 class_source => $class_source->{cn_source},
148 my $item_1 = $builder->build({
151 itemcallnumber => 'GT95',
152 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
156 my( undef, $item_count) = GetItemsForInventory({
157 maxlocation => 'GT100',
158 minlocation => 'GT90',
159 class_source => $class_source->{cn_source},
161 is($item_count,$pre_item_count + 1,"We should return GT95 as between GT90 and GT100");
162 $schema->storage->txn_rollback;