Bug 23425: (QA follow-up) Keep it generic
[koha.git] / Koha / Libraries.pm
blob56eaf4feab38a17df3bc7999b502e80a69e94159
1 package Koha::Libraries;
3 # Copyright 2015 Koha Development team
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Carp;
24 use C4::Context;
26 use Koha::Biblios;
27 use Koha::Database;
28 use Koha::Item::Transfer::Limits;
29 use Koha::Items;
30 use Koha::Library;
32 use base qw(Koha::Objects);
34 =head1 NAME
36 Koha::Libraries - Koha Library Object set class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 pickup_locations
46 Returns available pickup locations for
47 A. a specific item
48 B. a biblio
49 C. none of the above, simply all libraries with pickup_location => 1
51 This method determines the pickup location by two factors:
52 1. is the library configured as pickup location
53 2. can a specific item / at least one of the items of a biblio be transferred
54 into the library
56 OPTIONAL PARAMETERS:
57 item # Koha::Item object / itemnumber, find pickup locations for item
58 biblio # Koha::Biblio object / biblionumber, find pickup locations for biblio
60 If no parameters are given, all libraries with pickup_location => 1 are returned.
62 =cut
64 sub pickup_locations {
65 my ($self, $params) = @_;
67 my $item = $params->{'item'};
68 my $biblio = $params->{'biblio'};
69 if ($biblio && $item) {
70 Koha::Exceptions::BadParameter->throw(
71 error => "Koha::Libraries->pickup_locations takes either 'biblio' or "
72 ." 'item' as parameter, but not both."
76 # Select libraries that are configured as pickup locations
77 my $libraries = $self->search({
78 pickup_location => 1
79 }, {
80 order_by => ['branchname']
81 });
83 return $libraries->unblessed unless $item or $biblio;
84 return $libraries->unblessed
85 unless C4::Context->preference('UseBranchTransferLimits');
86 my $limittype = C4::Context->preference('BranchTransferLimitsType');
88 if ($item) {
89 unless (ref($item) eq 'Koha::Item') {
90 $item = Koha::Items->find($item);
91 return $libraries->unblessed unless $item;
93 } else {
94 unless (ref($biblio) eq 'Koha::Biblio') {
95 $biblio = Koha::Biblios->find($biblio);
96 return $libraries->unblessed unless $biblio;
100 my @pickup_locations;
101 foreach my $library ($libraries->as_list) {
102 if ($item && $item->can_be_transferred({ to => $library })) {
103 push @pickup_locations, $library->unblessed;
104 } elsif ($biblio && $biblio->can_be_transferred({ to => $library })) {
105 push @pickup_locations, $library->unblessed;
109 return wantarray ? @pickup_locations : \@pickup_locations;
112 =head3 search_filtered
114 =cut
116 sub search_filtered {
117 my ( $self, $params, $attributes ) = @_;
119 my @branchcodes;
120 my $userenv = C4::Context->userenv;
121 if ( $userenv and $userenv->{number} ) {
122 my $only_from_group = $params->{only_from_group};
123 if ( $only_from_group ) {
124 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
125 my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
126 $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
127 } else {
128 if ( C4::Context::only_my_library ) {
129 $params->{branchcode} = C4::Context->userenv->{branch};
133 delete $params->{only_from_group};
134 return $self->SUPER::search( $params, $attributes );
137 =head3 type
139 =cut
141 sub _type {
142 return 'Branch';
145 sub object_class {
146 return 'Koha::Library';