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 => 8;
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;
64 subtest 'Skip items with waiting holds' => sub {
68 $schema->storage->txn_begin;
70 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
72 = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
73 my $patron = $builder->build_object(
74 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
76 my $title_1 = 'Title 1, ';
77 my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
79 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_1 });
80 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_2 });
82 my ( $items_1, $first_items_count ) = GetItemsForInventory();
83 is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
85 # Add two items, so we don't depend on existing data
86 my $item_1 = $builder->build_object(
87 { class => 'Koha::Items',
89 biblionumber => $biblio_1->biblionumber,
90 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
91 homebranch => $library->id,
92 holdingbranch => $library->id,
93 itype => $itemtype->itemtype,
99 my $item_2 = $builder->build_object(
100 { class => 'Koha::Items',
102 biblionumber => $biblio_2->biblionumber,
103 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
104 homebranch => $library->id,
105 holdingbranch => $library->id,
106 itype => $itemtype->itemtype,
112 my ( $items_2, $second_items_count ) = GetItemsForInventory();
113 is( scalar @{$items_2}, $second_items_count, 'Results and count match' );
114 is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
118 = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
119 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
120 $item_1->itemnumber, 'W' );
122 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
123 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
124 ok( (any { $_->{title} eq $title_2 } @{$new_items}),
125 'Item on hold skipped, the other one we added is present' );
126 ok( (none { $_->{title} eq $title_1 } @{$new_items}),
127 'Item on hold skipped, no one matches' );
129 $schema->storage->txn_rollback;
132 $dbh->{AutoCommit} = 1;
134 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
137 my $builder = t::lib::TestBuilder->new;
138 my $schema = Koha::Database->new->schema;
139 $schema->storage->txn_begin;
140 $builder->schema->resultset( 'Issue' )->delete_all;
141 $builder->schema->resultset( 'Item' )->delete_all;
143 my $class_rule = $builder->build({
144 source => 'ClassSortRule',
145 value => { sort_routine => "LCC" }
147 my $class_source = $builder->build({
148 source => 'ClassSource',
150 class_sort_rule => $class_rule->{class_sort_rule},
153 my $item_1 = $builder->build({
156 itemcallnumber => 'GT95',
157 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
161 my( undef, $item_count) = GetItemsForInventory({
162 maxlocation => 'GT100',
163 minlocation => 'GT90',
164 class_source => $class_source->{cn_source},
166 is($item_count,1,"We should return GT95 as between GT90 and GT100");
167 $schema->storage->txn_rollback;
169 >>>>>>> Bug 19915: Add unit test to GetItemsForInventory.t
175 my $minlocation = '';
176 my $maxlocation = '';
179 my $ignoreissued = '';
180 my $datelastseen = '';
187 my ( @bind_params, @where_strings );
189 my $select_columns = q{
190 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
192 my $select_count = q{SELECT COUNT(*)};
195 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
196 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
199 for my $authvfield (keys %$statushash){
200 if ( scalar @{$statushash->{$authvfield}} > 0 ){
201 my $joinedvals = join ',', @{$statushash->{$authvfield}};
202 push @where_strings, "$authvfield in (" . $joinedvals . ")";
208 push @where_strings, 'itemcallnumber >= ?';
209 push @bind_params, $minlocation;
213 push @where_strings, 'itemcallnumber <= ?';
214 push @bind_params, $maxlocation;
218 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
219 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
220 push @bind_params, $datelastseen;
224 push @where_strings, 'items.location = ?';
225 push @bind_params, $location;
229 if($branch eq "homebranch"){
230 push @where_strings, 'items.homebranch = ?';
232 push @where_strings, 'items.holdingbranch = ?';
234 push @bind_params, $branchcode;
238 push @where_strings, 'biblioitems.itemtype = ?';
239 push @bind_params, $itemtype;
242 if ( $ignoreissued) {
243 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
244 push @where_strings, 'issues.date_due IS NULL';
247 if ( @where_strings ) {
249 $query .= join ' AND ', @where_strings;
251 my $count_query = $select_count . $query;
252 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
253 $query .= " LIMIT $offset, $size" if ($offset and $size);
254 $query = $select_columns . $query;
255 my $sth = $ldbh->prepare($query);
256 $sth->execute( @bind_params );
259 my $tmpresults = $sth->fetchall_arrayref({});
260 $sth = $ldbh->prepare( $count_query );
261 $sth->execute( @bind_params );
262 my ($iTotalRecords) = $sth->fetchrow_array();
264 my $marc_field_mapping;
265 foreach my $row (@$tmpresults) {
268 foreach my $field (sort keys %$row) {
269 # If the koha field is mapped to a marc field
270 my ($f, $sf) = C4::Biblio::GetMarcFromKohaField("items.$field", $row->{'frameworkcode'});
271 if (defined($f) and defined($sf)) {
272 # We replace the code with it's description
274 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
275 $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
277 $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
278 $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
280 my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
281 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
287 return (\@results, $iTotalRecords);