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