Bug 14057: Inventory is painfully slow
[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 use Test::More tests => 6;
22
23 $| = 1;
24
25 BEGIN {
26     use_ok('C4::Context');
27     use_ok('C4::Items');
28     use_ok('C4::Biblio');
29     use_ok('C4::Koha');
30 }
31
32 can_ok('C4::Items','GetItemsForInventory');
33
34 my $dbh = C4::Context->dbh;
35 $dbh->{AutoCommit} = 0;
36 $dbh->{RaiseError} = 1;
37
38 diag("This could take a while for large datasets...");
39
40 my ($oldResults, $oldCount) = OldWay($dbh);
41 my ($newResults, $newCount) = GetItemsForInventory( { interface => 'staff' } );
42
43 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
44
45 $dbh->rollback;
46
47 sub OldWay {
48     my ($tdbh)       = @_;
49     my $ldbh         = $tdbh;
50     my $minlocation  = '';
51     my $maxlocation  = '';
52     my $location     = '';
53     my $itemtype     = '';
54     my $ignoreissued = '';
55     my $datelastseen = '';
56     my $branchcode   = '';
57     my $branch       = '';
58     my $offset       = '';
59     my $size         = '';
60     my $statushash   = '';
61     my $interface    = '';
62
63     my ( @bind_params, @where_strings );
64
65     my $select_columns = q{
66         SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
67     };
68     my $select_count = q{SELECT COUNT(*)};
69     my $query = q{
70         FROM items
71         LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
72         LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
73     };
74     if ($statushash){
75         for my $authvfield (keys %$statushash){
76             if ( scalar @{$statushash->{$authvfield}} > 0 ){
77                 my $joinedvals = join ',', @{$statushash->{$authvfield}};
78                 push @where_strings, "$authvfield in (" . $joinedvals . ")";
79             }
80         }
81     }
82
83     if ($minlocation) {
84         push @where_strings, 'itemcallnumber >= ?';
85         push @bind_params, $minlocation;
86     }
87
88     if ($maxlocation) {
89         push @where_strings, 'itemcallnumber <= ?';
90         push @bind_params, $maxlocation;
91     }
92
93     if ($datelastseen) {
94         $datelastseen = format_date_in_iso($datelastseen);
95         push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
96         push @bind_params, $datelastseen;
97     }
98
99     if ( $location ) {
100         push @where_strings, 'items.location = ?';
101         push @bind_params, $location;
102     }
103
104     if ( $branchcode ) {
105         if($branch eq "homebranch"){
106         push @where_strings, 'items.homebranch = ?';
107         }else{
108             push @where_strings, 'items.holdingbranch = ?';
109         }
110         push @bind_params, $branchcode;
111     }
112
113     if ( $itemtype ) {
114         push @where_strings, 'biblioitems.itemtype = ?';
115         push @bind_params, $itemtype;
116     }
117
118     if ( $ignoreissued) {
119         $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
120         push @where_strings, 'issues.date_due IS NULL';
121     }
122
123     if ( @where_strings ) {
124         $query .= 'WHERE ';
125         $query .= join ' AND ', @where_strings;
126     }
127     $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
128     my $count_query = $select_count . $query;
129     $query .= " LIMIT $offset, $size" if ($offset and $size);
130     $query = $select_columns . $query;
131     my $sth = $ldbh->prepare($query);
132     $sth->execute( @bind_params );
133
134     my @results = ();
135     my $tmpresults = $sth->fetchall_arrayref({});
136     $sth = $ldbh->prepare( $count_query );
137     $sth->execute( @bind_params );
138     my ($iTotalRecords) = $sth->fetchrow_array();
139
140     foreach my $row (@$tmpresults) {
141
142         # Auth values
143         foreach my $field (sort keys %$row) {
144             # If the koha field is mapped to a marc field
145             my ($f, $sf) = C4::Biblio::GetMarcFromKohaField("items.$field", $row->{'frameworkcode'});
146             if (defined($f) and defined($sf)) {
147                 # We replace the code with it's description
148                 my $authvals = C4::Koha::GetKohaAuthorisedValuesFromField($f, $sf, $row->{'frameworkcode'});
149                 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
150             }
151         }
152         push @results, $row;
153     }
154
155     return (\@results, $iTotalRecords);
156 }