Bug 21046: Return the correct borrowernumber when there is empty cardnumber(s)
[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 use Koha::AuthorisedValues;
24
25 $| = 1;
26
27 BEGIN {
28     use_ok('C4::Context');
29     use_ok('C4::Items');
30     use_ok('C4::Biblio');
31     use_ok('C4::Koha');
32 }
33
34 can_ok('C4::Items','GetItemsForInventory');
35
36 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
39
40 my ($oldResults, $oldCount) = OldWay($dbh);
41 my ($newResults, $newCount) = GetItemsForInventory;
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
62     my ( @bind_params, @where_strings );
63
64     my $select_columns = q{
65         SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
66     };
67     my $select_count = q{SELECT COUNT(*)};
68     my $query = q{
69         FROM items
70         LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
71         LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
72     };
73     if ($statushash){
74         for my $authvfield (keys %$statushash){
75             if ( scalar @{$statushash->{$authvfield}} > 0 ){
76                 my $joinedvals = join ',', @{$statushash->{$authvfield}};
77                 push @where_strings, "$authvfield in (" . $joinedvals . ")";
78             }
79         }
80     }
81
82     if ($minlocation) {
83         push @where_strings, 'itemcallnumber >= ?';
84         push @bind_params, $minlocation;
85     }
86
87     if ($maxlocation) {
88         push @where_strings, 'itemcallnumber <= ?';
89         push @bind_params, $maxlocation;
90     }
91
92     if ($datelastseen) {
93         $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
94         push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
95         push @bind_params, $datelastseen;
96     }
97
98     if ( $location ) {
99         push @where_strings, 'items.location = ?';
100         push @bind_params, $location;
101     }
102
103     if ( $branchcode ) {
104         if($branch eq "homebranch"){
105         push @where_strings, 'items.homebranch = ?';
106         }else{
107             push @where_strings, 'items.holdingbranch = ?';
108         }
109         push @bind_params, $branchcode;
110     }
111
112     if ( $itemtype ) {
113         push @where_strings, 'biblioitems.itemtype = ?';
114         push @bind_params, $itemtype;
115     }
116
117     if ( $ignoreissued) {
118         $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
119         push @where_strings, 'issues.date_due IS NULL';
120     }
121
122     if ( @where_strings ) {
123         $query .= 'WHERE ';
124         $query .= join ' AND ', @where_strings;
125     }
126     my $count_query = $select_count . $query;
127     $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
128     $query .= " LIMIT $offset, $size" if ($offset and $size);
129     $query = $select_columns . $query;
130     my $sth = $ldbh->prepare($query);
131     $sth->execute( @bind_params );
132
133     my @results = ();
134     my $tmpresults = $sth->fetchall_arrayref({});
135     $sth = $ldbh->prepare( $count_query );
136     $sth->execute( @bind_params );
137     my ($iTotalRecords) = $sth->fetchrow_array();
138
139     my $marc_field_mapping;
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 $avs;
149                 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
150                     $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
151                 } else {
152                     $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
153                     $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
154                 }
155                 my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
156                 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
157             }
158         }
159         push @results, $row;
160     }
161
162     return (\@results, $iTotalRecords);
163 }