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);
29 use Koha
::AuthorisedValues
;
35 use_ok
('C4::Context');
41 can_ok
('C4::Items','GetItemsForInventory');
43 my $schema = Koha
::Database
->new->schema;
44 my $builder = t
::lib
::TestBuilder
->new;
46 subtest
'Old version is unchanged' => sub {
50 $schema->storage->txn_begin;
52 my $dbh = $schema->storage->dbh;
54 my ($oldResults, $oldCount) = OldWay
($dbh);
55 my ($newResults, $newCount) = GetItemsForInventory
;
57 is_deeply
($newResults,$oldResults,"Inventory results unchanged.");
59 $schema->storage->txn_rollback;
62 subtest
'Skip items with waiting holds' => sub {
66 $schema->storage->txn_begin;
68 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
70 = $builder->build_object( { class => 'Koha::ItemTypes', value
=> { rentalcharge
=> 0 } } );
71 my $patron = $builder->build_object(
72 { class => 'Koha::Patrons', value
=> { branchcode
=> $library->id } } );
74 my $title_1 = 'Title 1, ';
75 my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
77 my $biblio_1 = create_helper_biblio
( $itemtype->itemtype, $title_1 );
78 my $biblio_2 = create_helper_biblio
( $itemtype->itemtype, $title_2 );
80 my ( $items_1, $first_items_count ) = GetItemsForInventory
();
81 is
( scalar @
{$items_1}, $first_items_count, 'Results and count match' );
83 # Add two items, so we don't depend on existing data
84 my $item_1 = $builder->build_object(
85 { class => 'Koha::Items',
87 biblionumber
=> $biblio_1->biblionumber,
88 biblioitemnumber
=> $biblio_1->biblioitem->biblioitemnumber,
89 homebranch
=> $library->id,
90 holdingbranch
=> $library->id,
91 itype
=> $itemtype->itemtype,
97 my $item_2 = $builder->build_object(
98 { class => 'Koha::Items',
100 biblionumber
=> $biblio_2->biblionumber,
101 biblioitemnumber
=> $biblio_2->biblioitem->biblioitemnumber,
102 homebranch
=> $library->id,
103 holdingbranch
=> $library->id,
104 itype
=> $itemtype->itemtype,
110 my ( $items_2, $second_items_count ) = GetItemsForInventory
();
111 is
( scalar @
{$items_2}, $second_items_count, 'Results and count match' );
112 is
( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
116 = C4
::Reserves
::AddReserve
( $library->branchcode, $patron->borrowernumber,
117 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
118 $item_1->itemnumber, 'W' );
120 my ( $new_items, $new_items_count ) = GetItemsForInventory
( { ignore_waiting_holds
=> 1 } );
121 is
( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
122 ok
( (any
{ $_->{title
} eq $title_2 } @
{$new_items}),
123 'Item on hold skipped, the other one we added is present' );
124 ok
( (none
{ $_->{title
} eq $title_1 } @
{$new_items}),
125 'Item on hold skipped, no one matches' );
127 $schema->storage->txn_rollback;
133 my $minlocation = '';
134 my $maxlocation = '';
137 my $ignoreissued = '';
138 my $datelastseen = '';
145 my ( @bind_params, @where_strings );
147 my $select_columns = q{
148 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
150 my $select_count = q{SELECT COUNT(*)};
153 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
154 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
157 for my $authvfield (keys %$statushash){
158 if ( scalar @
{$statushash->{$authvfield}} > 0 ){
159 my $joinedvals = join ',', @
{$statushash->{$authvfield}};
160 push @where_strings, "$authvfield in (" . $joinedvals . ")";
166 push @where_strings, 'itemcallnumber >= ?';
167 push @bind_params, $minlocation;
171 push @where_strings, 'itemcallnumber <= ?';
172 push @bind_params, $maxlocation;
176 $datelastseen = output_pref
({ str
=> $datelastseen, dateformat
=> 'iso', dateonly
=> 1 });
177 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
178 push @bind_params, $datelastseen;
182 push @where_strings, 'items.location = ?';
183 push @bind_params, $location;
187 if($branch eq "homebranch"){
188 push @where_strings, 'items.homebranch = ?';
190 push @where_strings, 'items.holdingbranch = ?';
192 push @bind_params, $branchcode;
196 push @where_strings, 'biblioitems.itemtype = ?';
197 push @bind_params, $itemtype;
200 if ( $ignoreissued) {
201 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
202 push @where_strings, 'issues.date_due IS NULL';
205 if ( @where_strings ) {
207 $query .= join ' AND ', @where_strings;
209 my $count_query = $select_count . $query;
210 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
211 $query .= " LIMIT $offset, $size" if ($offset and $size);
212 $query = $select_columns . $query;
213 my $sth = $ldbh->prepare($query);
214 $sth->execute( @bind_params );
217 my $tmpresults = $sth->fetchall_arrayref({});
218 $sth = $ldbh->prepare( $count_query );
219 $sth->execute( @bind_params );
220 my ($iTotalRecords) = $sth->fetchrow_array();
222 my $marc_field_mapping;
223 foreach my $row (@
$tmpresults) {
226 foreach my $field (sort keys %$row) {
227 # If the koha field is mapped to a marc field
228 my ($f, $sf) = C4
::Biblio
::GetMarcFromKohaField
("items.$field", $row->{'frameworkcode'});
229 if (defined($f) and defined($sf)) {
230 # We replace the code with it's description
232 if ( exists $marc_field_mapping->{$row->{frameworkcode
}}{$f}{$sf} ) {
233 $avs = $marc_field_mapping->{$row->{frameworkcode
}}{$f}{$sf};
235 $avs = Koha
::AuthorisedValues
->search_by_marc_field({ frameworkcode
=> $row->{frameworkcode
}, tagfield
=> $f, tagsubfield
=> $sf, });
236 $marc_field_mapping->{$row->{frameworkcode
}}{$f}{$sf} = $avs->unblessed;
238 my $authvals = { map { $_->{authorised_value
} => $_->{lib
} } @
{ $marc_field_mapping->{$row->{frameworkcode
}}{$f}{$sf} } };
239 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
245 return (\
@results, $iTotalRecords);
248 # Helper method to set up a Biblio.
249 sub create_helper_biblio
{
250 my $itemtype = shift;
252 my $record = MARC
::Record
->new();
254 $record->append_fields(
255 MARC
::Field
->new( '245', ' ', ' ', a
=> $title ),
256 MARC
::Field
->new( '942', ' ', ' ', c
=> $itemtype ),
259 my ($biblio_id) = AddBiblio
( $record, '' );
261 return Koha
::Biblios
->find($biblio_id);