Bug 21798: replace gimme_a_biblio with build_sample_biblio
[koha.git] / t / db_dependent / Items / GetItemsForInventory.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Copyright (c) 2015   Mark Tompsett
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 9;
23 use t::lib::TestBuilder;
24
25 use List::MoreUtils qw( any none );
26
27 use C4::Biblio qw(AddBiblio);
28 use C4::Reserves;
29 use C4::ClassSource;
30 use Koha::AuthorisedValues;
31 use Koha::Biblios;
32 use Koha::Database;
33 use MARC::Record;
34
35 BEGIN {
36     use_ok('C4::Context');
37     use_ok('C4::Items');
38     use_ok('C4::Biblio');
39     use_ok('C4::Koha');
40 }
41
42 can_ok('C4::Items','GetItemsForInventory');
43
44 my $schema  = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
46
47 subtest 'Old version is unchanged' => sub {
48
49     plan tests => 1;
50
51     $schema->storage->txn_begin;
52
53     my $dbh = $schema->storage->dbh;
54
55     my ($oldResults, $oldCount) = OldWay($dbh);
56     my ($newResults, $newCount) = GetItemsForInventory;
57
58     is_deeply($newResults,$oldResults,"Inventory results unchanged.");
59
60     $schema->storage->txn_rollback;
61 };
62
63 subtest 'Skip items with waiting holds' => sub {
64
65     plan tests => 6;
66
67     $schema->storage->txn_begin;
68
69     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
70     my $itemtype
71         = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
72     my $patron = $builder->build_object(
73         { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
74
75     my $title_1 = 'Title 1, ';
76     my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
77
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 });
80
81     my ( $items_1, $first_items_count ) = GetItemsForInventory();
82     is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
83
84     # Add two items, so we don't depend on existing data
85     my $item_1 = $builder->build_object(
86         {   class => 'Koha::Items',
87             value => {
88                 biblionumber     => $biblio_1->biblionumber,
89                 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
90                 homebranch       => $library->id,
91                 holdingbranch    => $library->id,
92                 itype            => $itemtype->itemtype,
93                 reserves         => undef
94             }
95         }
96     );
97
98     my $item_2 = $builder->build_object(
99         {   class => 'Koha::Items',
100             value => {
101                 biblionumber     => $biblio_2->biblionumber,
102                 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
103                 homebranch       => $library->id,
104                 holdingbranch    => $library->id,
105                 itype            => $itemtype->itemtype,
106                 reserves         => undef
107             }
108         }
109     );
110
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' );
114
115     # Add a waiting hold
116     my $reserve_id
117         = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
118         $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
119         $item_1->itemnumber, 'W' );
120
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' );
127
128     $schema->storage->txn_rollback;
129 };
130
131 subtest 'Verify results with OldWay' => sub {
132     $schema->storage->txn_begin;
133     plan tests => 1;
134
135     my ($oldResults, $oldCount) = OldWay();
136     my ($newResults, $newCount) = GetItemsForInventory();
137     is_deeply($newResults,$oldResults,"Inventory results unchanged.");
138     $schema->storage->txn_rollback;
139 };
140
141 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
142     $schema->storage->txn_begin;
143     plan tests => 1;
144
145     my $builder = t::lib::TestBuilder->new;
146
147     my $class_rule = $builder->build({
148         source => 'ClassSortRule',
149         value => { sort_routine => "LCC" }
150     });
151     my $class_source = $builder->build({
152         source => 'ClassSource',
153         value => {
154             class_sort_rule => $class_rule->{class_sort_rule},
155         }
156     });
157
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},
163     });
164
165     my $item_1 = $builder->build({
166             source => 'Item',
167             value  => {
168                 itemcallnumber => 'GT95',
169                 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
170             }
171     });
172
173     my( undef, $item_count) = GetItemsForInventory({
174         maxlocation => 'GT100',
175         minlocation => 'GT90',
176         class_source => $class_source->{cn_source},
177     });
178     is($item_count,$pre_item_count + 1,"We should return GT95 as between GT90 and GT100");
179     $schema->storage->txn_rollback;
180
181 };
182
183 sub OldWay { # FIXME Do we really still need so much code to check results ??
184     my $ldbh = C4::Context->dbh;
185
186     my $minlocation  = '';
187     my $maxlocation  = '';
188     my $location     = '';
189     my $itemtype     = '';
190     my $ignoreissued = '';
191     my $datelastseen = '';
192     my $branchcode   = '';
193     my $branch       = '';
194     my $offset       = '';
195     my $size         = '';
196     my $statushash   = '';
197
198     my ( @bind_params, @where_strings );
199
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
202     };
203     my $select_count = q{SELECT COUNT(*)};
204     my $query = q{
205         FROM items
206         LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
207         LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
208     };
209     if ($statushash){
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 . ")";
214             }
215         }
216     }
217
218     if ($minlocation) {
219         push @where_strings, 'itemcallnumber >= ?';
220         push @bind_params, $minlocation;
221     }
222
223     if ($maxlocation) {
224         push @where_strings, 'itemcallnumber <= ?';
225         push @bind_params, $maxlocation;
226     }
227
228     if ($datelastseen) {
229         $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
230         push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
231         push @bind_params, $datelastseen;
232     }
233
234     if ( $location ) {
235         push @where_strings, 'items.location = ?';
236         push @bind_params, $location;
237     }
238
239     if ( $branchcode ) {
240         if($branch eq "homebranch"){
241         push @where_strings, 'items.homebranch = ?';
242         }else{
243             push @where_strings, 'items.holdingbranch = ?';
244         }
245         push @bind_params, $branchcode;
246     }
247
248     if ( $itemtype ) {
249         push @where_strings, 'biblioitems.itemtype = ?';
250         push @bind_params, $itemtype;
251     }
252
253     if ( $ignoreissued) {
254         $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
255         push @where_strings, 'issues.date_due IS NULL';
256     }
257
258     if ( @where_strings ) {
259         $query .= 'WHERE ';
260         $query .= join ' AND ', @where_strings;
261     }
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 );
268
269     my @results = ();
270     my $tmpresults = $sth->fetchall_arrayref({});
271     $sth = $ldbh->prepare( $count_query );
272     $sth->execute( @bind_params );
273     my ($iTotalRecords) = $sth->fetchrow_array();
274
275     my $marc_field_mapping;
276     foreach my $row (@$tmpresults) {
277
278         # Auth values
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
284                 my $avs;
285                 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
286                     $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
287                 } else {
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;
290                 }
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}};
293             }
294         }
295         push @results, $row;
296     }
297
298     return (\@results, $iTotalRecords);
299 }