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

(-)a/C4/Items.pm (-8 / +33 lines)
Lines 1024-1030 sub GetLostItems { Link Here
1024
1024
1025
=head2 GetItemsForInventory
1025
=head2 GetItemsForInventory
1026
1026
1027
($itemlist, $iTotalRecords)  = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $offset, $size, $statushash);
1027
($itemlist, $iTotalRecords) = GetItemsForInventory( {
1028
  minlocation  => $minlocation,
1029
  maxlocation  => $maxlocation,
1030
  location     => $location,
1031
  itemtype     => $itemtype,
1032
  ignoreissued => $ignoreissued,
1033
  datelastseen => $datelastseen,
1034
  branchcode   => $branchcode,
1035
  branch       => $branch,
1036
  offset       => $offset,
1037
  size         => $size,
1038
  stautshash   => $statushash
1039
  interface    => $interface,
1040
} );
1028
1041
1029
Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
1042
Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
1030
1043
Lines 1042-1048 $iTotalRecords is the number of rows that would have been returned without the $ Link Here
1042
=cut
1055
=cut
1043
1056
1044
sub GetItemsForInventory {
1057
sub GetItemsForInventory {
1045
    my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $size, $statushash ) = @_;
1058
    my ( $parameters ) = @_;
1059
    my $minlocation  = $parameters->{'minlocation'}  // '';
1060
    my $maxlocation  = $parameters->{'maxlocation'}  // '';
1061
    my $location     = $parameters->{'location'}     // '';
1062
    my $itemtype     = $parameters->{'itemtype'}     // '';
1063
    my $ignoreissued = $parameters->{'ignoreissued'} // '';
1064
    my $datelastseen = $parameters->{'datelastseen'} // '';
1065
    my $branchcode   = $parameters->{'branchcode'}   // '';
1066
    my $branch       = $parameters->{'branch'}       // '';
1067
    my $offset       = $parameters->{'offset'}       // '';
1068
    my $size         = $parameters->{'size'}         // '';
1069
    my $statushash   = $parameters->{'statushash'}   // '';
1070
    my $interface    = $parameters->{'interface'}    // '';
1071
1046
    my $dbh = C4::Context->dbh;
1072
    my $dbh = C4::Context->dbh;
1047
    my ( @bind_params, @where_strings );
1073
    my ( @bind_params, @where_strings );
1048
1074
Lines 1121-1136 sub GetItemsForInventory { Link Here
1121
    $sth->execute( @bind_params );
1147
    $sth->execute( @bind_params );
1122
    my ($iTotalRecords) = $sth->fetchrow_array();
1148
    my ($iTotalRecords) = $sth->fetchrow_array();
1123
1149
1150
    my $avmapping = C4::Koha::GetKohaAuthorisedValuesMapping( {
1151
                      interface => $interface
1152
                    } );
1124
    foreach my $row (@$tmpresults) {
1153
    foreach my $row (@$tmpresults) {
1125
1154
1126
        # Auth values
1155
        # Auth values
1127
        foreach (keys %$row) {
1156
        foreach (keys %$row) {
1128
            # If the koha field is mapped to a marc field
1157
            if (defined($avmapping->{"items.$_,".$row->{'frameworkcode'}.",".$row->{$_}})) {
1129
            my ($f, $sf) = GetMarcFromKohaField("items.$_", $row->{'frameworkcode'});
1158
                $row->{$_} = $avmapping->{"items.$_,".$row->{'frameworkcode'}.",".$row->{$_}};
1130
            if ($f and $sf) {
1131
                # We replace the code with it's description
1132
                my $authvals = C4::Koha::GetKohaAuthorisedValuesFromField($f, $sf, $row->{'frameworkcode'});
1133
                $row->{$_} = $authvals->{$row->{$_}} if defined $authvals->{$row->{$_}};
1134
            }
1159
            }
1135
        }
1160
        }
1136
        push @results, $row;
1161
        push @results, $row;
(-)a/C4/Koha.pm (+45 lines)
Lines 62-67 BEGIN { Link Here
62
                &IsAuthorisedValueCategory
62
                &IsAuthorisedValueCategory
63
		&GetKohaAuthorisedValues
63
		&GetKohaAuthorisedValues
64
		&GetKohaAuthorisedValuesFromField
64
		&GetKohaAuthorisedValuesFromField
65
    &GetKohaAuthorisedValuesMapping
65
    &GetKohaAuthorisedValueLib
66
    &GetKohaAuthorisedValueLib
66
    &GetAuthorisedValueByCode
67
    &GetAuthorisedValueByCode
67
    &GetKohaImageurlFromAuthorisedValues
68
    &GetKohaImageurlFromAuthorisedValues
Lines 1327-1332 sub GetKohaAuthorisedValuesFromField { Link Here
1327
  }
1328
  }
1328
}
1329
}
1329
1330
1331
=head2 GetKohaAuthorisedValuesMapping
1332
1333
Takes a hash as a parameter. The interface key indicates the
1334
description to use in the mapping.
1335
1336
Returns hashref of:
1337
 "{kohafield},{frameworkcode},{authorised_value}" => "{description}"
1338
for all the kohafields, frameworkcodes, and authorised values.
1339
1340
Returns undef if nothing is found.
1341
1342
=cut
1343
1344
sub GetKohaAuthorisedValuesMapping {
1345
    my ($parameter) = @_;
1346
    my $interface = $parameter->{'interface'} // '';
1347
1348
    my $query_mapping = q{
1349
SELECT TA.kohafield,TA.authorised_value AS category,
1350
       TA.frameworkcode,TB.authorised_value,
1351
       IF(TB.lib_opac>'',TB.lib_opac,TB.lib) AS OPAC,
1352
       TB.lib AS Intranet,TB.lib_opac
1353
FROM marc_subfield_structure AS TA JOIN
1354
     authorised_values as TB ON
1355
     TA.authorised_value=TB.category
1356
WHERE TA.kohafield>'' AND TA.authorised_value>'';
1357
    };
1358
    my $dbh = C4::Context->dbh;
1359
    my $sth = $dbh->prepare($query_mapping);
1360
    $sth->execute();
1361
    my $avmapping;
1362
    if ($interface eq 'opac') {
1363
        while (my $row = $sth->fetchrow_hashref) {
1364
            $avmapping->{$row->{kohafield}.",".$row->{frameworkcode}.",".$row->{authorised_value}} = $row->{OPAC};
1365
        }
1366
    }
1367
    else {
1368
        while (my $row = $sth->fetchrow_hashref) {
1369
            $avmapping->{$row->{kohafield}.",".$row->{frameworkcode}.",".$row->{authorised_value}} = $row->{Intranet};
1370
        }
1371
    }
1372
    return $avmapping;
1373
}
1374
1330
=head2 xml_escape
1375
=head2 xml_escape
1331
1376
1332
  my $escaped_string = C4::Koha::xml_escape($string);
1377
  my $escaped_string = C4::Koha::xml_escape($string);
(-)a/t/db_dependent/Items/GetItemsForInventory.t (+155 lines)
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
}
(-)a/t/db_dependent/Koha/GetKohaAuthorisedValuesMapping.t (+54 lines)
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
use C4::Context;
23
24
use Test::More tests => 8;
25
26
BEGIN {
27
    use_ok('C4::Context');
28
    use_ok('C4::Koha');
29
}
30
31
can_ok('C4::Koha','GetKohaAuthorisedValuesMapping');
32
33
my $avMappingOPAC;
34
my $avMappingStaff;
35
my $avMappingUndef1;
36
my $avMappingUndef2;
37
my $avMappingUndef3;
38
SKIP: {
39
    my $dbh = C4::Context->dbh;
40
    my $count = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM marc_subfield_structure WHERE kohafield LIKE 'item%';");
41
    skip "Lacking item mappings in marc_subfield_structure",5 unless ($count && $count->[0]>0);
42
    $count = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM authorised_values;");
43
    skip "Lacking authorised_values",5 unless ($count && $count->[0]>0);
44
    $avMappingOPAC   = GetKohaAuthorisedValuesMapping( { interface => 'opac' });
45
    $avMappingStaff  = GetKohaAuthorisedValuesMapping( { interface => 'staff' });
46
    $avMappingUndef1 = GetKohaAuthorisedValuesMapping( { interface => undef });
47
    $avMappingUndef2 = GetKohaAuthorisedValuesMapping( { } );
48
    $avMappingUndef3 = GetKohaAuthorisedValuesMapping();
49
    is_deeply($avMappingUndef1,$avMappingOPAC,"Undefined interface = OPAC test 1");
50
    is_deeply($avMappingUndef2,$avMappingOPAC,"Undefined interface = OPAC test 2");
51
    is_deeply($avMappingUndef3,$avMappingOPAC,"Undefined interface = OPAC test 3");
52
    isnt($avMappingOPAC ,undef,"OPAC has a mapping");
53
    isnt($avMappingStaff,undef,"Staff has a mapping");
54
}
(-)a/tools/inventory.pl (-3 / +28 lines)
Lines 234-243 if ( $markseen or $op ) { Link Here
234
234
235
    # We use datelastseen only when comparing the results to the barcode file.
235
    # We use datelastseen only when comparing the results to the barcode file.
236
    my $paramdatelastseen = ($compareinv2barcd) ? $datelastseen : '';
236
    my $paramdatelastseen = ($compareinv2barcd) ? $datelastseen : '';
237
    ($inventorylist, $totalrecords) = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $paramdatelastseen, $branchcode, $branch, 0, undef, $staton);
237
    ($inventorylist, $totalrecords) = GetItemsForInventory( {
238
      minlocation  => $minlocation,
239
      maxlocation  => $maxlocation,
240
      location     => $location,
241
      itemtype     => $itemtype,
242
      ignoreissued => $ignoreissued,
243
      datelastseen => $paramdatelastseen,
244
      branchcode   => $branchcode,
245
      branch       => $branch,
246
      offset       => 0,
247
      size         => undef,
248
      statushash   => $staton,
249
      interface    => 'staff',
250
    } );
238
251
239
    # For the items that may be marked as "wrong place", we only check the location (callnumbers, location and branch)
252
    # For the items that may be marked as "wrong place", we only check the location (callnumbers, location and branch)
240
    ($wrongplacelist, $totalrecords) = GetItemsForInventory($minlocation, $maxlocation, $location, undef, undef, undef, $branchcode, $branch, 0, undef, undef);
253
    ($wrongplacelist, $totalrecords) = GetItemsForInventory( {
254
      minlocation  => $minlocation,
255
      maxlocation  => $maxlocation,
256
      location     => $location,
257
      itemtype     => undef,
258
      ignoreissued => undef,
259
      datelastseen => undef,
260
      branchcode   => $branchcode,
261
      branch       => $branch,
262
      offset       => 0,
263
      size         => undef,
264
      statushash   => undef,
265
      interface    => 'staff',
266
    } );
241
267
242
}
268
}
243
269
244
- 

Return to bug 14057