|
Line 0
Link Here
|
|
|
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 |
|
| 23 |
use Test::More tests => 5; |
| 24 |
|
| 25 |
BEGIN { |
| 26 |
use_ok('C4::Context'); |
| 27 |
use_ok('C4::Items'); |
| 28 |
use_ok('C4::Koha'); |
| 29 |
} |
| 30 |
|
| 31 |
can_ok('C4::Items','GetItemsForInventory'); |
| 32 |
|
| 33 |
my $dbh = C4::Context->dbh; |
| 34 |
$dbh->{AutoCommit} = 0; |
| 35 |
$dbh->{RaiseError} = 1; |
| 36 |
|
| 37 |
diag("This could take a while for large datasets..."); |
| 38 |
|
| 39 |
my ($oldResults, $oldCount) = OldWay($dbh); |
| 40 |
my ($newResults, $newCount) = GetItemsForInventory( { interface => 'staff' } ); |
| 41 |
|
| 42 |
is_deeply($newResults,$oldResults,"Inventory results unchanged."); |
| 43 |
|
| 44 |
$dbh->rollback; |
| 45 |
|
| 46 |
sub OldWay { |
| 47 |
my ($tdbh) = @_; |
| 48 |
my $ldbh = $tdbh; |
| 49 |
my $minlocation = ''; |
| 50 |
my $maxlocation = ''; |
| 51 |
my $location = ''; |
| 52 |
my $itemtype = ''; |
| 53 |
my $ignoreissued = ''; |
| 54 |
my $datelastseen = ''; |
| 55 |
my $branchcode = ''; |
| 56 |
my $branch = ''; |
| 57 |
my $offset = ''; |
| 58 |
my $size = ''; |
| 59 |
my $statushash = ''; |
| 60 |
my $interface = ''; |
| 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 = format_date_in_iso($datelastseen); |
| 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 |
$query .= ' ORDER BY items.cn_sort, itemcallnumber, title'; |
| 127 |
my $count_query = $select_count . $query; |
| 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 |
foreach my $row (@$tmpresults) { |
| 140 |
|
| 141 |
# Auth values |
| 142 |
foreach (keys %$row) { |
| 143 |
# If the koha field is mapped to a marc field |
| 144 |
my ($f, $sf) = GetMarcFromKohaField("items.$_", $row->{'frameworkcode'}); |
| 145 |
if (defined($f) and defined($sf)) { |
| 146 |
# We replace the code with it's description |
| 147 |
my $authvals = C4::Koha::GetKohaAuthorisedValuesFromField($f, $sf, $row->{'frameworkcode'}); |
| 148 |
$row->{$_} = $authvals->{$row->{$_}} if defined $authvals->{$row->{$_}}; |
| 149 |
} |
| 150 |
} |
| 151 |
push @results, $row; |
| 152 |
} |
| 153 |
|
| 154 |
return (\@results, $iTotalRecords); |
| 155 |
} |