3 # This file is part of Koha.
5 # Copyright (c) 2015 Mark Tompsett
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.
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.
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>.
21 use Test::More tests => 6;
23 use Koha::AuthorisedValues;
28 use_ok('C4::Context');
34 can_ok('C4::Items','GetItemsForInventory');
36 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
40 my ($oldResults, $oldCount) = OldWay($dbh);
41 my ($newResults, $newCount) = GetItemsForInventory;
43 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
54 my $ignoreissued = '';
55 my $datelastseen = '';
62 my ( @bind_params, @where_strings );
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
67 my $select_count = q{SELECT COUNT(*)};
70 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
71 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
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 . ")";
83 push @where_strings, 'itemcallnumber >= ?';
84 push @bind_params, $minlocation;
88 push @where_strings, 'itemcallnumber <= ?';
89 push @bind_params, $maxlocation;
93 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
94 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
95 push @bind_params, $datelastseen;
99 push @where_strings, 'items.location = ?';
100 push @bind_params, $location;
104 if($branch eq "homebranch"){
105 push @where_strings, 'items.homebranch = ?';
107 push @where_strings, 'items.holdingbranch = ?';
109 push @bind_params, $branchcode;
113 push @where_strings, 'biblioitems.itemtype = ?';
114 push @bind_params, $itemtype;
117 if ( $ignoreissued) {
118 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
119 push @where_strings, 'issues.date_due IS NULL';
122 if ( @where_strings ) {
124 $query .= join ' AND ', @where_strings;
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 );
134 my $tmpresults = $sth->fetchall_arrayref({});
135 $sth = $ldbh->prepare( $count_query );
136 $sth->execute( @bind_params );
137 my ($iTotalRecords) = $sth->fetchrow_array();
139 my $marc_field_mapping;
140 foreach my $row (@$tmpresults) {
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
149 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
150 $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
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;
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}};
162 return (\@results, $iTotalRecords);