Bug 22284: Add "patron's hold group" as new hold_fulfillment_policy option
[koha.git] / Koha / Libraries.pm
1 package Koha::Libraries;
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use C4::Context;
25
26 use Koha::Biblios;
27 use Koha::Database;
28 use Koha::Item::Transfer::Limits;
29 use Koha::Items;
30 use Koha::Library;
31 use Koha::Patrons;
32
33 use base qw(Koha::Objects);
34
35 =head1 NAME
36
37 Koha::Libraries - Koha Library Object set class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 pickup_locations
46
47 Returns available pickup locations for
48     A. a specific item
49     B. a biblio
50     C. none of the above, simply all libraries with pickup_location => 1
51
52 This method determines the pickup location by two factors:
53     1. is the library configured as pickup location
54     2. can a specific item / at least one of the items of a biblio be transferred
55        into the library
56
57 OPTIONAL PARAMETERS:
58     item   # Koha::Item object / itemnumber, find pickup locations for item
59     biblio # Koha::Biblio object / biblionumber, find pickup locations for biblio
60
61 If no parameters are given, all libraries with pickup_location => 1 are returned.
62
63 =cut
64
65 sub pickup_locations {
66     my ($self, $params) = @_;
67
68     my $item = $params->{'item'};
69     my $biblio = $params->{'biblio'};
70     my $patron = $params->{'patron'};
71
72     if ($biblio && $item) {
73         Koha::Exceptions::BadParameter->throw(
74             error => "Koha::Libraries->pickup_locations takes either 'biblio' or "
75             ." 'item' as parameter, but not both."
76         );
77     }
78     unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
79         $patron = Koha::Patrons->find($patron);
80     }
81
82     # Select libraries that are configured as pickup locations
83     my $libraries = $self->search({
84         pickup_location => 1
85     }, {
86         order_by => ['branchname']
87     });
88
89     return $libraries->unblessed unless $item or $biblio;
90     if($item) {
91         unless (ref($item) eq 'Koha::Item') {
92             $item = Koha::Items->find($item);
93             return $libraries->unblessed unless $item;
94         }
95         return $item->pickup_locations( {patron => $patron} );
96     } else {
97         unless (ref($biblio) eq 'Koha::Biblio') {
98             $biblio = Koha::Biblios->find($biblio);
99             return $libraries->unblessed unless $biblio;
100         }
101         return $biblio->pickup_locations( {patron => $patron} );
102     }
103 }
104
105 =head3 search_filtered
106
107 =cut
108
109 sub search_filtered {
110     my ( $self, $params, $attributes ) = @_;
111
112     my @branchcodes;
113     my $userenv = C4::Context->userenv;
114     if ( $userenv and $userenv->{number} ) {
115         my $only_from_group = $params->{only_from_group};
116         if ( $only_from_group ) {
117             my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
118             my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
119             $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
120         } else {
121             if ( C4::Context::only_my_library ) {
122                 $params->{branchcode} = C4::Context->userenv->{branch};
123             }
124         }
125     }
126     delete $params->{only_from_group};
127     return $self->SUPER::search( $params, $attributes );
128 }
129
130 =head3 type
131
132 =cut
133
134 sub _type {
135     return 'Branch';
136 }
137
138 sub object_class {
139     return 'Koha::Library';
140 }
141
142 1;