Bug 24760: Use C4::BackgroundJob->fetch in tests
[koha.git] / Koha / Libraries.pm
blob27e4a232ed375d450be994927079774f35c6d3de
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
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.
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.
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>.
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;
31 use Koha::Patrons;
33 use base qw(Koha::Objects);
35 =head1 NAME
37 Koha::Libraries - Koha Library Object set class
39 =head1 API
41 =head2 Class Methods
43 =cut
45 =head3 pickup_locations
47 Returns available pickup locations (Koha::Library objects) for
48 A. a specific item
49 B. a biblio
50 C. none of the above, simply all libraries with pickup_location => 1
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
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
61 If no parameters are given, all libraries with pickup_location => 1 are returned.
63 =cut
65 sub pickup_locations {
66 my ($self, $params) = @_;
68 my $item = $params->{'item'};
69 my $biblio = $params->{'biblio'};
70 if ($biblio && $item) {
71 Koha::Exceptions::BadParameter->throw(
72 error => "Koha::Libraries->pickup_locations takes either 'biblio' or "
73 ." 'item' as parameter, but not both."
77 # Select libraries that are configured as pickup locations
78 my $libraries = $self->search({
79 pickup_location => 1
80 }, {
81 order_by => ['branchname']
82 });
84 return $libraries->unblessed unless $item or $biblio;
85 return $libraries->unblessed
86 unless C4::Context->preference('UseBranchTransferLimits');
87 my $limittype = C4::Context->preference('BranchTransferLimitsType');
89 if ($item) {
90 unless (ref($item) eq 'Koha::Item') {
91 $item = Koha::Items->find($item);
92 return $libraries->unblessed unless $item;
94 } else {
95 unless (ref($biblio) eq 'Koha::Biblio') {
96 $biblio = Koha::Biblios->find($biblio);
97 return $libraries->unblessed unless $biblio;
101 my @pickup_locations;
102 foreach my $library ($libraries->as_list) {
103 if ($item && $item->can_be_transferred({ to => $library })) {
104 push @pickup_locations, $library->unblessed;
105 } elsif ($biblio && $biblio->can_be_transferred({ to => $library })) {
106 push @pickup_locations, $library->unblessed;
110 return wantarray ? @pickup_locations : \@pickup_locations;
113 =head3 search_filtered
115 =cut
117 sub search_filtered {
118 my ( $self, $params, $attributes ) = @_;
120 my @branchcodes;
121 my $userenv = C4::Context->userenv;
122 if ( $userenv and $userenv->{number} ) {
123 my $only_from_group = $params->{only_from_group};
124 if ( $only_from_group ) {
125 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
126 my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
127 $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
128 } else {
129 if ( C4::Context::only_my_library ) {
130 $params->{branchcode} = C4::Context->userenv->{branch};
134 delete $params->{only_from_group};
135 return $self->SUPER::search( $params, $attributes );
138 =head3 type
140 =cut
142 sub _type {
143 return 'Branch';
146 sub object_class {
147 return 'Koha::Library';