Bug 19915: Add unit test to GetItemsForInventory.t
[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 => 8;
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 <<<<<<< HEAD
64 subtest 'Skip items with waiting holds' => sub {
65
66     plan tests => 6;
67
68     $schema->storage->txn_begin;
69
70     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
71     my $itemtype
72         = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
73     my $patron = $builder->build_object(
74         { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
75
76     my $title_1 = 'Title 1, ';
77     my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
78
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 });
81
82     my ( $items_1, $first_items_count ) = GetItemsForInventory();
83     is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
84
85     # Add two items, so we don't depend on existing data
86     my $item_1 = $builder->build_object(
87         {   class => 'Koha::Items',
88             value => {
89                 biblionumber     => $biblio_1->biblionumber,
90                 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
91                 homebranch       => $library->id,
92                 holdingbranch    => $library->id,
93                 itype            => $itemtype->itemtype,
94                 reserves         => undef
95             }
96         }
97     );
98
99     my $item_2 = $builder->build_object(
100         {   class => 'Koha::Items',
101             value => {
102                 biblionumber     => $biblio_2->biblionumber,
103                 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
104                 homebranch       => $library->id,
105                 holdingbranch    => $library->id,
106                 itype            => $itemtype->itemtype,
107                 reserves         => undef
108             }
109         }
110     );
111
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' );
115
116     # Add a waiting hold
117     my $reserve_id
118         = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
119         $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
120         $item_1->itemnumber, 'W' );
121
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' );
128
129     $schema->storage->txn_rollback;
130 =======
131 $dbh->rollback;
132 $dbh->{AutoCommit} = 1;
133
134 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
135
136     plan tests => 1;
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;
142
143     my $class_rule = $builder->build({
144         source => 'ClassSortRule',
145         value => { sort_routine => "LCC" }
146     });
147     my $class_source = $builder->build({
148         source => 'ClassSource',
149         value => {
150             class_sort_rule => $class_rule->{class_sort_rule},
151         }
152     });
153     my $item_1 = $builder->build({
154             source => 'Item',
155             value  => {
156                 itemcallnumber => 'GT95',
157                 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
158             }
159     });
160
161     my( undef, $item_count) = GetItemsForInventory({
162         maxlocation => 'GT100',
163         minlocation => 'GT90',
164         class_source => $class_source->{cn_source},
165     });
166     is($item_count,1,"We should return GT95 as between GT90 and GT100");
167     $schema->storage->txn_rollback;
168
169 >>>>>>> Bug 19915: Add unit test to GetItemsForInventory.t
170 };
171
172 sub OldWay {
173     my ($tdbh)       = @_;
174     my $ldbh         = $tdbh;
175     my $minlocation  = '';
176     my $maxlocation  = '';
177     my $location     = '';
178     my $itemtype     = '';
179     my $ignoreissued = '';
180     my $datelastseen = '';
181     my $branchcode   = '';
182     my $branch       = '';
183     my $offset       = '';
184     my $size         = '';
185     my $statushash   = '';
186
187     my ( @bind_params, @where_strings );
188
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
191     };
192     my $select_count = q{SELECT COUNT(*)};
193     my $query = q{
194         FROM items
195         LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
196         LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
197     };
198     if ($statushash){
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 . ")";
203             }
204         }
205     }
206
207     if ($minlocation) {
208         push @where_strings, 'itemcallnumber >= ?';
209         push @bind_params, $minlocation;
210     }
211
212     if ($maxlocation) {
213         push @where_strings, 'itemcallnumber <= ?';
214         push @bind_params, $maxlocation;
215     }
216
217     if ($datelastseen) {
218         $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
219         push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
220         push @bind_params, $datelastseen;
221     }
222
223     if ( $location ) {
224         push @where_strings, 'items.location = ?';
225         push @bind_params, $location;
226     }
227
228     if ( $branchcode ) {
229         if($branch eq "homebranch"){
230         push @where_strings, 'items.homebranch = ?';
231         }else{
232             push @where_strings, 'items.holdingbranch = ?';
233         }
234         push @bind_params, $branchcode;
235     }
236
237     if ( $itemtype ) {
238         push @where_strings, 'biblioitems.itemtype = ?';
239         push @bind_params, $itemtype;
240     }
241
242     if ( $ignoreissued) {
243         $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
244         push @where_strings, 'issues.date_due IS NULL';
245     }
246
247     if ( @where_strings ) {
248         $query .= 'WHERE ';
249         $query .= join ' AND ', @where_strings;
250     }
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 );
257
258     my @results = ();
259     my $tmpresults = $sth->fetchall_arrayref({});
260     $sth = $ldbh->prepare( $count_query );
261     $sth->execute( @bind_params );
262     my ($iTotalRecords) = $sth->fetchrow_array();
263
264     my $marc_field_mapping;
265     foreach my $row (@$tmpresults) {
266
267         # Auth values
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
273                 my $avs;
274                 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
275                     $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
276                 } else {
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;
279                 }
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}};
282             }
283         }
284         push @results, $row;
285     }
286
287     return (\@results, $iTotalRecords);
288 }