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 => 9;
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 'Old version is unchanged' => sub {
51 $schema->storage->txn_begin;
53 my $dbh = $schema->storage->dbh;
55 my ($oldResults, $oldCount) = OldWay($dbh);
56 my ($newResults, $newCount) = GetItemsForInventory;
58 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
60 $schema->storage->txn_rollback;
63 subtest 'Skip items with waiting holds' => sub {
67 $schema->storage->txn_begin;
69 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
71 = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
72 my $patron = $builder->build_object(
73 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
75 my $title_1 = 'Title 1, ';
76 my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
78 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_1 });
79 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_2 });
81 my ( $items_1, $first_items_count ) = GetItemsForInventory();
82 is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
84 # Add two items, so we don't depend on existing data
85 my $item_1 = $builder->build_object(
86 { class => 'Koha::Items',
88 biblionumber => $biblio_1->biblionumber,
89 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
90 homebranch => $library->id,
91 holdingbranch => $library->id,
92 itype => $itemtype->itemtype,
98 my $item_2 = $builder->build_object(
99 { class => 'Koha::Items',
101 biblionumber => $biblio_2->biblionumber,
102 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
103 homebranch => $library->id,
104 holdingbranch => $library->id,
105 itype => $itemtype->itemtype,
111 my ( $items_2, $second_items_count ) = GetItemsForInventory();
112 is( scalar @{$items_2}, $second_items_count, 'Results and count match' );
113 is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
117 = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
118 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
119 $item_1->itemnumber, 'W' );
121 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
122 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
123 ok( (any { $_->{title} eq $title_2 } @{$new_items}),
124 'Item on hold skipped, the other one we added is present' );
125 ok( (none { $_->{title} eq $title_1 } @{$new_items}),
126 'Item on hold skipped, no one matches' );
128 $schema->storage->txn_rollback;
131 subtest 'Verify results with OldWay' => sub {
132 $schema->storage->txn_begin;
135 my ($oldResults, $oldCount) = OldWay();
136 my ($newResults, $newCount) = GetItemsForInventory();
137 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
138 $schema->storage->txn_rollback;
141 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
142 $schema->storage->txn_begin;
145 my $builder = t::lib::TestBuilder->new;
147 my $class_rule = $builder->build({
148 source => 'ClassSortRule',
149 value => { sort_routine => "LCC" }
151 my $class_source = $builder->build({
152 source => 'ClassSource',
154 class_sort_rule => $class_rule->{class_sort_rule},
158 #Find if we have any items in our test range before we start
159 my( undef, $pre_item_count) = GetItemsForInventory({
160 maxlocation => 'GT100',
161 minlocation => 'GT90',
162 class_source => $class_source->{cn_source},
165 my $item_1 = $builder->build({
168 itemcallnumber => 'GT95',
169 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
173 my( undef, $item_count) = GetItemsForInventory({
174 maxlocation => 'GT100',
175 minlocation => 'GT90',
176 class_source => $class_source->{cn_source},
178 is($item_count,$pre_item_count + 1,"We should return GT95 as between GT90 and GT100");
179 $schema->storage->txn_rollback;
183 sub OldWay { # FIXME Do we really still need so much code to check results ??
184 my $ldbh = C4::Context->dbh;
186 my $minlocation = '';
187 my $maxlocation = '';
190 my $ignoreissued = '';
191 my $datelastseen = '';
198 my ( @bind_params, @where_strings );
200 my $select_columns = q{
201 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
203 my $select_count = q{SELECT COUNT(*)};
206 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
207 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
210 for my $authvfield (keys %$statushash){
211 if ( scalar @{$statushash->{$authvfield}} > 0 ){
212 my $joinedvals = join ',', @{$statushash->{$authvfield}};
213 push @where_strings, "$authvfield in (" . $joinedvals . ")";
219 push @where_strings, 'itemcallnumber >= ?';
220 push @bind_params, $minlocation;
224 push @where_strings, 'itemcallnumber <= ?';
225 push @bind_params, $maxlocation;
229 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
230 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
231 push @bind_params, $datelastseen;
235 push @where_strings, 'items.location = ?';
236 push @bind_params, $location;
240 if($branch eq "homebranch"){
241 push @where_strings, 'items.homebranch = ?';
243 push @where_strings, 'items.holdingbranch = ?';
245 push @bind_params, $branchcode;
249 push @where_strings, 'biblioitems.itemtype = ?';
250 push @bind_params, $itemtype;
253 if ( $ignoreissued) {
254 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
255 push @where_strings, 'issues.date_due IS NULL';
258 if ( @where_strings ) {
260 $query .= join ' AND ', @where_strings;
262 my $count_query = $select_count . $query;
263 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
264 $query .= " LIMIT $offset, $size" if ($offset and $size);
265 $query = $select_columns . $query;
266 my $sth = $ldbh->prepare($query);
267 $sth->execute( @bind_params );
270 my $tmpresults = $sth->fetchall_arrayref({});
271 $sth = $ldbh->prepare( $count_query );
272 $sth->execute( @bind_params );
273 my ($iTotalRecords) = $sth->fetchrow_array();
275 my $marc_field_mapping;
276 foreach my $row (@$tmpresults) {
279 foreach my $field (sort keys %$row) {
280 # If the koha field is mapped to a marc field
281 my ($f, $sf) = C4::Biblio::GetMarcFromKohaField("items.$field", $row->{'frameworkcode'});
282 if (defined($f) and defined($sf)) {
283 # We replace the code with it's description
285 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
286 $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
288 $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
289 $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
291 my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
292 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
298 return (\@results, $iTotalRecords);