Bug 19893: Support for joined subfields in mappings
[koha.git] / t / db_dependent / Items / GetItemsForInventory.t
blobfddea80ad017c9137a780074dea4b8a0af936e45
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 7;
23 use t::lib::TestBuilder;
25 use List::MoreUtils qw( any none );
27 use C4::Biblio qw(AddBiblio);
28 use C4::Reserves;
29 use Koha::AuthorisedValues;
30 use Koha::Biblios;
31 use Koha::Database;
32 use MARC::Record;
34 BEGIN {
35 use_ok('C4::Context');
36 use_ok('C4::Items');
37 use_ok('C4::Biblio');
38 use_ok('C4::Koha');
41 can_ok('C4::Items','GetItemsForInventory');
43 my $schema = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new;
46 subtest 'Old version is unchanged' => sub {
48 plan tests => 1;
50 $schema->storage->txn_begin;
52 my $dbh = $schema->storage->dbh;
54 my ($oldResults, $oldCount) = OldWay($dbh);
55 my ($newResults, $newCount) = GetItemsForInventory;
57 is_deeply($newResults,$oldResults,"Inventory results unchanged.");
59 $schema->storage->txn_rollback;
62 subtest 'Skip items with waiting holds' => sub {
64 plan tests => 6;
66 $schema->storage->txn_begin;
68 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
69 my $itemtype
70 = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
71 my $patron = $builder->build_object(
72 { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
74 my $title_1 = 'Title 1, ';
75 my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
77 my $biblio_1 = create_helper_biblio( $itemtype->itemtype, $title_1 );
78 my $biblio_2 = create_helper_biblio( $itemtype->itemtype, $title_2 );
80 my ( $items_1, $first_items_count ) = GetItemsForInventory();
81 is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
83 # Add two items, so we don't depend on existing data
84 my $item_1 = $builder->build_object(
85 { class => 'Koha::Items',
86 value => {
87 biblionumber => $biblio_1->biblionumber,
88 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
89 homebranch => $library->id,
90 holdingbranch => $library->id,
91 itype => $itemtype->itemtype,
92 reserves => undef
97 my $item_2 = $builder->build_object(
98 { class => 'Koha::Items',
99 value => {
100 biblionumber => $biblio_2->biblionumber,
101 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
102 homebranch => $library->id,
103 holdingbranch => $library->id,
104 itype => $itemtype->itemtype,
105 reserves => undef
110 my ( $items_2, $second_items_count ) = GetItemsForInventory();
111 is( scalar @{$items_2}, $second_items_count, 'Results and count match' );
112 is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
114 # Add a waiting hold
115 my $reserve_id
116 = C4::Reserves::AddReserve( $library->branchcode, $patron->borrowernumber,
117 $item_1->biblionumber, '', 1, undef, undef, '', "title for fee",
118 $item_1->itemnumber, 'W' );
120 my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
121 is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
122 ok( (any { $_->{title} eq $title_2 } @{$new_items}),
123 'Item on hold skipped, the other one we added is present' );
124 ok( (none { $_->{title} eq $title_1 } @{$new_items}),
125 'Item on hold skipped, no one matches' );
127 $schema->storage->txn_rollback;
130 sub OldWay {
131 my ($tdbh) = @_;
132 my $ldbh = $tdbh;
133 my $minlocation = '';
134 my $maxlocation = '';
135 my $location = '';
136 my $itemtype = '';
137 my $ignoreissued = '';
138 my $datelastseen = '';
139 my $branchcode = '';
140 my $branch = '';
141 my $offset = '';
142 my $size = '';
143 my $statushash = '';
145 my ( @bind_params, @where_strings );
147 my $select_columns = q{
148 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
150 my $select_count = q{SELECT COUNT(*)};
151 my $query = q{
152 FROM items
153 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
154 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
156 if ($statushash){
157 for my $authvfield (keys %$statushash){
158 if ( scalar @{$statushash->{$authvfield}} > 0 ){
159 my $joinedvals = join ',', @{$statushash->{$authvfield}};
160 push @where_strings, "$authvfield in (" . $joinedvals . ")";
165 if ($minlocation) {
166 push @where_strings, 'itemcallnumber >= ?';
167 push @bind_params, $minlocation;
170 if ($maxlocation) {
171 push @where_strings, 'itemcallnumber <= ?';
172 push @bind_params, $maxlocation;
175 if ($datelastseen) {
176 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
177 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
178 push @bind_params, $datelastseen;
181 if ( $location ) {
182 push @where_strings, 'items.location = ?';
183 push @bind_params, $location;
186 if ( $branchcode ) {
187 if($branch eq "homebranch"){
188 push @where_strings, 'items.homebranch = ?';
189 }else{
190 push @where_strings, 'items.holdingbranch = ?';
192 push @bind_params, $branchcode;
195 if ( $itemtype ) {
196 push @where_strings, 'biblioitems.itemtype = ?';
197 push @bind_params, $itemtype;
200 if ( $ignoreissued) {
201 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
202 push @where_strings, 'issues.date_due IS NULL';
205 if ( @where_strings ) {
206 $query .= 'WHERE ';
207 $query .= join ' AND ', @where_strings;
209 my $count_query = $select_count . $query;
210 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
211 $query .= " LIMIT $offset, $size" if ($offset and $size);
212 $query = $select_columns . $query;
213 my $sth = $ldbh->prepare($query);
214 $sth->execute( @bind_params );
216 my @results = ();
217 my $tmpresults = $sth->fetchall_arrayref({});
218 $sth = $ldbh->prepare( $count_query );
219 $sth->execute( @bind_params );
220 my ($iTotalRecords) = $sth->fetchrow_array();
222 my $marc_field_mapping;
223 foreach my $row (@$tmpresults) {
225 # Auth values
226 foreach my $field (sort keys %$row) {
227 # If the koha field is mapped to a marc field
228 my ($f, $sf) = C4::Biblio::GetMarcFromKohaField("items.$field", $row->{'frameworkcode'});
229 if (defined($f) and defined($sf)) {
230 # We replace the code with it's description
231 my $avs;
232 if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
233 $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
234 } else {
235 $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
236 $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
238 my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
239 $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
242 push @results, $row;
245 return (\@results, $iTotalRecords);
248 # Helper method to set up a Biblio.
249 sub create_helper_biblio {
250 my $itemtype = shift;
251 my $title = shift;
252 my $record = MARC::Record->new();
254 $record->append_fields(
255 MARC::Field->new( '245', ' ', ' ', a => $title ),
256 MARC::Field->new( '942', ' ', ' ', c => $itemtype ),
259 my ($biblio_id) = AddBiblio( $record, '' );
261 return Koha::Biblios->find($biblio_id);