View | Details | Raw Unified | Return to bug 24030
Collapse All | Expand All

(-)a/t/db_dependent/Items/GetItemsForInventory.t (-146 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 7;
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
24
25
use List::MoreUtils qw( any none );
25
use List::MoreUtils qw( any none );
Lines 44-65 can_ok('C4::Items','GetItemsForInventory'); Link Here
44
my $schema  = Koha::Database->new->schema;
44
my $schema  = Koha::Database->new->schema;
45
my $builder = t::lib::TestBuilder->new;
45
my $builder = t::lib::TestBuilder->new;
46
46
47
subtest 'Old version is unchanged' => sub {
48
49
    plan tests => 1;
50
51
    $schema->storage->txn_begin;
52
53
    my $dbh = $schema->storage->dbh;
54
55
    my ($oldResults, $oldCount) = OldWay($dbh);
56
    my ($newResults, $newCount) = GetItemsForInventory;
57
58
    is_deeply($newResults,$oldResults,"Inventory results unchanged.");
59
60
    $schema->storage->txn_rollback;
61
};
62
63
subtest 'Skip items with waiting holds' => sub {
47
subtest 'Skip items with waiting holds' => sub {
64
48
65
    plan tests => 7;
49
    plan tests => 7;
Lines 137-152 subtest 'Skip items with waiting holds' => sub { Link Here
137
    $schema->storage->txn_rollback;
121
    $schema->storage->txn_rollback;
138
};
122
};
139
123
140
subtest 'Verify results with OldWay' => sub {
141
    $schema->storage->txn_begin;
142
    plan tests => 1;
143
144
    my ($oldResults, $oldCount) = OldWay();
145
    my ($newResults, $newCount) = GetItemsForInventory();
146
    is_deeply($newResults,$oldResults,"Inventory results unchanged.");
147
    $schema->storage->txn_rollback;
148
};
149
150
subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
124
subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
151
    $schema->storage->txn_begin;
125
    $schema->storage->txn_begin;
152
    plan tests => 1;
126
    plan tests => 1;
Lines 188-308 subtest 'Use cn_sort rather than callnumber to determine correct location' => su Link Here
188
    $schema->storage->txn_rollback;
162
    $schema->storage->txn_rollback;
189
163
190
};
164
};
191
192
sub OldWay { # FIXME Do we really still need so much code to check results ??
193
    my $ldbh = C4::Context->dbh;
194
195
    my $minlocation  = '';
196
    my $maxlocation  = '';
197
    my $location     = '';
198
    my $itemtype     = '';
199
    my $ignoreissued = '';
200
    my $datelastseen = '';
201
    my $branchcode   = '';
202
    my $branch       = '';
203
    my $offset       = '';
204
    my $size         = '';
205
    my $statushash   = '';
206
207
    my ( @bind_params, @where_strings );
208
209
    my $select_columns = q{
210
        SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber
211
    };
212
    my $select_count = q{SELECT COUNT(*)};
213
    my $query = q{
214
        FROM items
215
        LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
216
        LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
217
    };
218
    if ($statushash){
219
        for my $authvfield (keys %$statushash){
220
            if ( scalar @{$statushash->{$authvfield}} > 0 ){
221
                my $joinedvals = join ',', @{$statushash->{$authvfield}};
222
                push @where_strings, "$authvfield in (" . $joinedvals . ")";
223
            }
224
        }
225
    }
226
227
    if ($minlocation) {
228
        push @where_strings, 'itemcallnumber >= ?';
229
        push @bind_params, $minlocation;
230
    }
231
232
    if ($maxlocation) {
233
        push @where_strings, 'itemcallnumber <= ?';
234
        push @bind_params, $maxlocation;
235
    }
236
237
    if ($datelastseen) {
238
        $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
239
        push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
240
        push @bind_params, $datelastseen;
241
    }
242
243
    if ( $location ) {
244
        push @where_strings, 'items.location = ?';
245
        push @bind_params, $location;
246
    }
247
248
    if ( $branchcode ) {
249
        if($branch eq "homebranch"){
250
        push @where_strings, 'items.homebranch = ?';
251
        }else{
252
            push @where_strings, 'items.holdingbranch = ?';
253
        }
254
        push @bind_params, $branchcode;
255
    }
256
257
    if ( $itemtype ) {
258
        push @where_strings, 'biblioitems.itemtype = ?';
259
        push @bind_params, $itemtype;
260
    }
261
262
    if ( $ignoreissued) {
263
        $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
264
        push @where_strings, 'issues.date_due IS NULL';
265
    }
266
267
    if ( @where_strings ) {
268
        $query .= 'WHERE ';
269
        $query .= join ' AND ', @where_strings;
270
    }
271
    my $count_query = $select_count . $query;
272
    $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
273
    $query .= " LIMIT $offset, $size" if ($offset and $size);
274
    $query = $select_columns . $query;
275
    my $sth = $ldbh->prepare($query);
276
    $sth->execute( @bind_params );
277
278
    my @results = ();
279
    my $tmpresults = $sth->fetchall_arrayref({});
280
    $sth = $ldbh->prepare( $count_query );
281
    $sth->execute( @bind_params );
282
    my ($iTotalRecords) = $sth->fetchrow_array();
283
284
    my $marc_field_mapping;
285
    foreach my $row (@$tmpresults) {
286
287
        # Auth values
288
        foreach my $field (sort keys %$row) {
289
            # If the koha field is mapped to a marc field
290
            my ($f, $sf) = C4::Biblio::GetMarcFromKohaField( "items.$field" );
291
            if (defined($f) and defined($sf)) {
292
                # We replace the code with it's description
293
                my $avs;
294
                if ( exists $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} ) {
295
                    $avs = $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf};
296
                } else {
297
                    $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $row->{frameworkcode}, tagfield => $f, tagsubfield => $sf, });
298
                    $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} = $avs->unblessed;
299
                }
300
                my $authvals = { map { $_->{authorised_value} => $_->{lib} } @{ $marc_field_mapping->{$row->{frameworkcode}}{$f}{$sf} } };
301
                $row->{$field} = $authvals->{$row->{$field}} if defined $authvals && defined $row->{$field} && defined $authvals->{$row->{$field}};
302
            }
303
        }
304
        push @results, $row;
305
    }
306
307
    return (\@results, $iTotalRecords);
308
}
309
- 

Return to bug 24030