Bug 25690: Make CanBookBeIssued return In Processing state as needing confirmation
[koha.git] / C4 / ShelfBrowser.pm
1 package C4::ShelfBrowser;
2
3 # Copyright 2010 Catalyst IT
4 #
5 # This file is part of Koha.
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 strict;
21 use warnings;
22
23 use C4::Biblio;
24 use C4::Context;
25 use C4::Koha;
26 use Koha::Biblios;
27 use Koha::Libraries;
28
29 use vars qw(@ISA @EXPORT @EXPORT_OK);
30
31 BEGIN {
32         require Exporter;
33         @ISA    = qw(Exporter);
34         @EXPORT = qw(
35             &GetNearbyItems
36     );
37     @EXPORT_OK = qw(
38     );
39 }
40
41 =head1 NAME
42
43 C4::ShelfBrowser - functions that deal with the shelf browser feature found in
44 the OPAC.
45
46 =head1 SYNOPSIS
47
48   use C4::ShelfBrowser;
49
50 =head1 DESCRIPTION
51
52 This module provides functions to get items nearby to another item, for use
53 in the shelf browser function.
54
55 'Nearby' is controlled by a handful of system preferences that specify what
56 to take into account.
57
58 =head1 FUNCTIONS
59
60 =head2 GetNearbyItems($itemnumber, [$num_each_side])
61
62   $nearby = GetNearbyItems($itemnumber, [$num_each_side]);
63
64   @items = @{ $nearby->{items} };
65
66   foreach (@items) {
67       # These won't format well like this, but here are the fields
68           print $_->{title};
69           print $_->{biblionumber};
70           print $_->{itemnumber};
71           print $_->{browser_normalized_upc};
72           print $_->{browser_normalized_oclc};
73           print $_->{browser_normalized_isbn};
74       print $_->{browser_normalized_ean};
75   }
76
77   # This is the information required to scroll the browser to the next left
78   # or right set. Can be derived from next/prev, but it's here for convenience.
79   print $nearby->{prev_item}{itemnumber};
80   print $nearby->{next_item}{itemnumber};
81   print $nearby->{prev_item}{biblionumber};
82   print $nearby->{next_item}{biblionumber};
83
84   # These will be undef if the values are not used to calculate the 
85   # nearby items.
86   print $nearby->{starting_homebranch}->{code};
87   print $nearby->{starting_homebranch}->{description};
88   print $nearby->{starting_location}->{code};
89   print $nearby->{starting_location}->{description};
90   print $nearby->{starting_ccode}->{code};
91   print $nearby->{starting_ccode}->{description};
92
93 This finds the items that are nearby to the supplied item, and supplies
94 those previous and next, along with the other useful information for displaying
95 the shelf browser.
96
97 It automatically applies the following user preferences to work out how to
98 calculate things: C<ShelfBrowserUsesLocation>, C<ShelfBrowserUsesHomeBranch>, 
99 C<ShelfBrowserUsesCcode>.
100
101 The option C<$num_each_side> value determines how many items will be fetched
102 each side of the supplied item. Note that the item itself is the first entry
103 in the 'next' set, and counts towards this limit (this is to keep the
104 behaviour consistent with the code that this is a refactor of.) Default is
105 3.
106
107 This will throw an exception if something went wrong.
108
109 =cut
110
111 sub GetNearbyItems {
112     my ( $itemnumber, $num_each_side, $gap) = @_;
113     $num_each_side ||= 3;
114     $gap ||= 7; # Should be > $num_each_side
115     die "BAD CALL in C4::ShelfBrowser::GetNearbyItems, gap should be > num_each_side"
116         if $gap <= $num_each_side;
117
118     my $dbh         = C4::Context->dbh;
119
120     my $sth_get_item_details = $dbh->prepare("SELECT cn_sort,homebranch,location,ccode from items where itemnumber=?");
121     $sth_get_item_details->execute($itemnumber);
122     my $item_details_result = $sth_get_item_details->fetchrow_hashref();
123     die "Unable to find item '$itemnumber' for shelf browser" if (!$sth_get_item_details);
124     my $start_cn_sort = $item_details_result->{'cn_sort'};
125
126     my ($start_homebranch, $start_location, $start_ccode);
127     if (C4::Context->preference('ShelfBrowserUsesHomeBranch') && 
128         defined($item_details_result->{'homebranch'})) {
129         $start_homebranch->{code} = $item_details_result->{'homebranch'};
130         $start_homebranch->{description} = Koha::Libraries->find($item_details_result->{'homebranch'})->branchname;
131     }
132     if (C4::Context->preference('ShelfBrowserUsesLocation') && 
133         defined($item_details_result->{'location'})) {
134         $start_location->{code} = $item_details_result->{'location'};
135         $start_location->{description} = GetAuthorisedValueDesc('','',$item_details_result->{'location'},'','','LOC','opac');
136     }
137     if (C4::Context->preference('ShelfBrowserUsesCcode') && 
138         defined($item_details_result->{'ccode'})) {
139         $start_ccode->{code} = $item_details_result->{'ccode'};
140         $start_ccode->{description} = GetAuthorisedValueDesc('', '', $item_details_result->{'ccode'}, '', '', 'CCODE', 'opac');
141     }
142
143     # Build the query for previous and next items
144     my $prev_query ='
145         SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
146         FROM items
147         WHERE
148             ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) ';
149     my $next_query ='
150         SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
151         FROM items
152         WHERE
153             ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) ';
154     my @params;
155     my $query_cond;
156     push @params, ($start_cn_sort, $itemnumber, $start_cn_sort);
157     if ($start_homebranch) {
158         $query_cond .= 'AND homebranch = ? ';
159         push @params, $start_homebranch->{code};
160     }
161     if ($start_location) {
162         $query_cond .= 'AND location = ? ';
163         push @params, $start_location->{code};
164     }
165     if ($start_ccode) {
166         $query_cond .= 'AND ccode = ? ';
167         push @params, $start_ccode->{code};
168     }
169
170     my @prev_items = @{
171         $dbh->selectall_arrayref(
172             $prev_query . $query_cond . ' ORDER BY cn_sort DESC, itemnumber DESC LIMIT ?',
173             { Slice => {} },
174             ( @params, $gap )
175         )
176     };
177     my @next_items = @{
178         $dbh->selectall_arrayref(
179             $next_query . $query_cond . ' ORDER BY cn_sort, itemnumber LIMIT ?',
180             { Slice => {} },
181             ( @params, $gap + 1 )
182         )
183     };
184
185     my $prev_item = $prev_items[-1];
186     my $next_item = $next_items[-1];
187     @next_items = splice( @next_items, 0, $num_each_side + 1 );
188     @prev_items = reverse splice( @prev_items, 0, $num_each_side );
189     my @items = ( @prev_items, @next_items );
190
191     $next_item = undef
192         if not $next_item
193             or ( $next_item->{itemnumber} == $items[-1]->{itemnumber}
194                 and ( @prev_items or @next_items <= 1 )
195             );
196     $prev_item = undef
197         if not $prev_item
198             or ( $prev_item->{itemnumber} == $items[0]->{itemnumber}
199                 and ( @next_items or @prev_items <= 1 )
200             );
201
202     # populate the items
203     @items = GetShelfInfo( @items );
204
205     return {
206         items               => \@items,
207         next_item           => $next_item,
208         prev_item           => $prev_item,
209         starting_homebranch => $start_homebranch,
210         starting_location   => $start_location,
211         starting_ccode      => $start_ccode,
212     };
213 }
214
215 # populate an item list with its title and upc, oclc and isbn normalized.
216 # Not really intended to be exported.
217 sub GetShelfInfo {
218     my @items = @_;
219     my $marcflavour = C4::Context->preference("marcflavour");
220     my @valid_items;
221     for my $item ( @items ) {
222         my $biblio = Koha::Biblios->find( $item->{biblionumber} );
223         next unless defined $biblio;
224
225         $item->{biblio_object} = $biblio;
226         $item->{biblionumber}  = $biblio->biblionumber;
227         $item->{title}         = $biblio->title;
228         $item->{subtitle}      = $biblio->subtitle;
229         $item->{medium}        = $biblio->medium;
230         $item->{part_number}   = $biblio->part_number;
231         $item->{part_name}     = $biblio->part_name;
232         my $this_record = GetMarcBiblio({ biblionumber => $biblio->biblionumber });
233         $item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
234         $item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
235         $item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
236         $item->{'browser_normalized_ean'} = GetNormalizedEAN($this_record,$marcflavour);
237         push @valid_items, $item;
238     }
239     return @valid_items;
240 }
241
242 1;