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 C4::Biblio qw(AddBiblio);
27 use Koha::AuthorisedValues;
33 use_ok('C4::Context');
39 can_ok('C4::Items','GetItemsForInventory');
41 my $schema = Koha::Database->new->schema;
42 my $builder = t::lib::TestBuilder->new;
44 subtest 'Old version is unchanged' => sub {
48 $schema->storage->txn_begin;
50 my $dbh = $schema->storage->dbh;
52 my ($oldResults, $oldCount) = OldWay($dbh);
53 my ($newResults, $newCount) = GetItemsForInventory;
55 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
57 $schema->storage->txn_rollback;
60 subtest 'Skip items with waiting holds' => sub {
64 $schema->storage->txn_begin;
66 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
68 = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
69 my $patron = $builder->build_object(
70 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
72 my $title_1 = 'Title 1';
73 my $title_2 = 'Title 2';
75 my $biblio_1 = create_helper_biblio( $itemtype->itemtype, $title_1 );
76 my $biblio_2 = create_helper_biblio( $itemtype->itemtype, $title_2 );
78 my ( $items_1, $first_items_count ) = GetItemsForInventory();
79 is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
81 # Add two items, so we don't depend on existing data
82 my $item_1 = $builder->build_object(
83 { class => 'Koha::Items',
85 biblionumber => $biblio_1->biblionumber,
86 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
87 homebranch => $library->id,
88 holdingbranch => $library->id,
89 itype => $itemtype->itemtype,
95 my $item_2 = $builder->build_object(
96 { class => 'Koha::Items',
98 biblionumber => $biblio_2->biblionumber,
99 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
100 homebranch => $library->id,
101 holdingbranch => $library->id,
102 itype => $itemtype->itemtype,
108 my ( $items_2, $second_items_count ) = GetItemsForInventory();
109 is( scalar @{$items_2}, $second_items_count, 'Results and count match' );
110 is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
114 = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
115 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
116 $item_1->itemnumber, 'W' );
118 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
119 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
120 is( $new_items->[ scalar @{$new_items} - 1 ]->{title},
121 $title_2, 'Item on hold skipped, last item is the correct one' );
123 $schema->storage->txn_rollback;
129 my $minlocation = '';
130 my $maxlocation = '';
133 my $ignoreissued = '';
134 my $datelastseen = '';
141 my ( @bind_params, @where_strings );
143 my $select_columns = q{
144 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
146 my $select_count = q{SELECT COUNT(*)};
149 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
150 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
153 for my $authvfield (keys %$statushash){
154 if ( scalar @{$statushash->{$authvfield}} > 0 ){
155 my $joinedvals = join ',', @{$statushash->{$authvfield}};
156 push @where_strings, "$authvfield in (" . $joinedvals . ")";
162 push @where_strings, 'itemcallnumber >= ?';
163 push @bind_params, $minlocation;
167 push @where_strings, 'itemcallnumber <= ?';
168 push @bind_params, $maxlocation;
172 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
173 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
174 push @bind_params, $datelastseen;
178 push @where_strings, 'items.location = ?';
179 push @bind_params, $location;
183 if($branch eq "homebranch"){
184 push @where_strings, 'items.homebranch = ?';
186 push @where_strings, 'items.holdingbranch = ?';
188 push @bind_params, $branchcode;
192 push @where_strings, 'biblioitems.itemtype = ?';
193 push @bind_params, $itemtype;
196 if ( $ignoreissued) {
197 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
198 push @where_strings, 'issues.date_due IS NULL';
201 if ( @where_strings ) {
203 $query .= join ' AND ', @where_strings;
205 my $count_query = $select_count . $query;
206 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
207 $query .= " LIMIT $offset, $size" if ($offset and $size);
208 $query = $select_columns . $query;
209 my $sth = $ldbh->prepare($query);
210 $sth->execute( @bind_params );
213 my $tmpresults = $sth->fetchall_arrayref({});
214 $sth = $ldbh->prepare( $count_query );
215 $sth->execute( @bind_params );
216 my ($iTotalRecords) = $sth->fetchrow_array();
218 my $marc_field_mapping;
219 foreach my $row (@$tmpresults) {
222 foreach my $field (sort keys %$row) {
223 # If the koha field is mapped to a marc field
224 my ($f, $sf) = C4::Biblio::GetMarcFromKohaField("items.$field", $row->{'frameworkcode'});
225 if (defined($f) and defined($sf)) {
226 # We replace the code with it's description
228 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
229 $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
231 $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
232 $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
234 my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
235 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
241 return (\@results, $iTotalRecords);
244 # Helper method to set up a Biblio.
245 sub create_helper_biblio {
246 my $itemtype = shift;
248 my $record = MARC::Record->new();
250 $record->append_fields(
251 MARC::Field->new( '245', ' ', ' ', a => $title ),
252 MARC::Field->new( '942', ' ', ' ', c => $itemtype ),
255 my $biblio_id = AddBiblio( $record, '' );
257 return Koha::Biblios->find($biblio_id);