af2319a3f68fd1a88aa8bd710a884524aebefea6
[srvgit] / Koha / Library.pm
1 package Koha::Library;
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
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 Carp;
23
24 use C4::Context;
25
26 use Koha::Database;
27 use Koha::StockRotationStages;
28 use Koha::SMTP::Servers;
29
30 use base qw(Koha::Object);
31
32 =head1 NAME
33
34 Koha::Library - Koha Library Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 stockrotationstages
41
42   my $stages = Koha::Library->stockrotationstages;
43
44 Returns the stockrotation stages associated with this Library.
45
46 =cut
47
48 sub stockrotationstages {
49     my ( $self ) = @_;
50     my $rs = $self->_result->stockrotationstages;
51     return Koha::StockRotationStages->_new_from_dbic( $rs );
52 }
53
54 =head3 outgoing_transfers
55
56   my $outgoing_transfers = Koha::Library->outgoing_transfers;
57
58 Returns the outgoing item transfers associated with this Library.
59
60 =cut
61
62 sub outgoing_transfers {
63     my ( $self ) = @_;
64     my $rs = $self->_result->branchtransfers_frombranches;
65     return Koha::Item::Transfers->_new_from_dbic( $rs );
66 }
67
68 =head3 inbound_transfers
69
70   my $inbound_transfers = Koha::Library->inbound_transfers;
71
72 Returns the inbound item transfers associated with this Library.
73
74 =cut
75
76 sub inbound_transfers {
77     my ( $self ) = @_;
78     my $rs = $self->_result->branchtransfers_tobranches;
79     return Koha::Item::Transfers->_new_from_dbic( $rs );
80 }
81
82 =head3 get_effective_marcorgcode
83
84     my $marcorgcode = Koha::Libraries->find( $library_id )->get_effective_marcorgcode();
85
86 Returns the effective MARC organization code of the library. It falls back to the value
87 from the I<MARCOrgCode> syspref if undefined for the library.
88
89 =cut
90
91 sub get_effective_marcorgcode {
92     my ( $self )  = @_;
93
94     return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
95 }
96
97 =head3 smtp_server
98
99     my $smtp_server = $library->smtp_server;
100     $library->smtp_server({ smtp_server => $smtp_server });
101     $library->smtp_server({ smtp_server => undef });
102
103 Accessor for getting and setting the library's SMTP server.
104
105 Returns the effective SMTP server configuration to be used on the library. The returned
106 value is always a I<Koha::SMTP::Server> object.
107
108 Setting it to undef will remove the link to a specific SMTP server and effectively
109 make the library use the default setting
110
111 =cut
112
113 sub smtp_server {
114     my ( $self, $params ) = @_;
115
116     my $library_smtp_server_rs = $self->_result->library_smtp_server;
117
118     if ( exists $params->{smtp_server} ) {
119
120         $self->_result->result_source->schema->txn_do( sub {
121             $library_smtp_server_rs->delete
122                 if $library_smtp_server_rs;
123
124             if ( defined $params->{smtp_server} ) {
125                 # Set the new server
126                 # Remove any already set SMTP server
127
128                 my $smtp_server = $params->{smtp_server};
129                 $smtp_server->_result->add_to_library_smtp_servers({ library_id => $self->id });
130             }
131         });
132     } # else => reset to default
133     else {
134         # Getter
135         if ( $library_smtp_server_rs ) {
136             return Koha::SMTP::Servers->find(
137                 $library_smtp_server_rs->smtp_server_id );
138         }
139
140         return Koha::SMTP::Servers->get_default;
141     }
142
143     return $self;
144 }
145
146 =head3 from_email_address
147
148   my $from_email = Koha::Library->from_email_address;
149
150 Returns the official 'from' email address for the branch.
151
152 It may well be a 'noreply' or other inaccessible local domain
153 address that is being used to satisfy spam protection filters.
154
155 =cut
156
157 sub from_email_address {
158     my ($self) = @_;
159
160     return
161          $self->branchemail
162       || C4::Context->preference('KohaAdminEmailAddress')
163       || undef;
164 }
165
166 =head3 inbound_email_address
167
168   my $to_email = Koha::Library->inbound_email_address;
169
170 Returns an effective email address which should be accessible to librarians at the branch.
171
172 NOTE: This is the address to use for 'reply_to' or 'to' fields; It should not usually be
173 used as the 'from' address for emails as it may lead to mail being caught by spam filters.
174
175 =cut
176
177 sub inbound_email_address {
178     my ($self) = @_;
179
180     return
181          $self->branchreplyto
182       || $self->branchemail
183       || C4::Context->preference('ReplytoDefault')
184       || C4::Context->preference('KohaAdminEmailAddress')
185       || undef;
186 }
187
188 =head3 inbound_ill_address
189
190   my $to_email = Koha::Library->inbound_ill_address;
191
192 Returns an effective email address which should be accessible to librarians at the branch
193 for inter library loans communication.
194
195 =cut
196
197 sub inbound_ill_address {
198     my ($self) = @_;
199
200     return
201          $self->branchillemail
202       || C4::Context->preference('ILLDefaultStaffEmail')
203       || $self->inbound_email_address;
204 }
205
206 =head3 library_groups
207
208 Return the Library groups of this library
209
210 =cut
211
212 sub library_groups {
213     my ( $self ) = @_;
214     my $rs = $self->_result->library_groups;
215     return Koha::Library::Groups->_new_from_dbic( $rs );
216 }
217
218 =head3 cash_registers
219
220 Return Cash::Registers associated with this Library
221
222 =cut
223
224 sub cash_registers {
225     my ( $self ) = @_;
226     my $rs = $self->_result->cash_registers;
227     return Koha::Cash::Registers->_new_from_dbic( $rs );
228 }
229
230 =head3 to_api_mapping
231
232 This method returns the mapping for representing a Koha::Library object
233 on the API.
234
235 =cut
236
237 sub to_api_mapping {
238     return {
239         branchcode       => 'library_id',
240         branchname       => 'name',
241         branchaddress1   => 'address1',
242         branchaddress2   => 'address2',
243         branchaddress3   => 'address3',
244         branchzip        => 'postal_code',
245         branchcity       => 'city',
246         branchstate      => 'state',
247         branchcountry    => 'country',
248         branchphone      => 'phone',
249         branchfax        => 'fax',
250         branchemail      => 'email',
251         branchillemail   => 'illemail',
252         branchreplyto    => 'reply_to_email',
253         branchreturnpath => 'return_path_email',
254         branchurl        => 'url',
255         issuing          => undef,
256         branchip         => 'ip',
257         branchnotes      => 'notes',
258         marcorgcode      => 'marc_org_code',
259     };
260 }
261
262 =head3 get_hold_libraries
263
264 Return all libraries (including self) that belong to the same hold groups
265
266 =cut
267
268 sub get_hold_libraries {
269     my ( $self ) = @_;
270     my $library_groups = $self->library_groups;
271     my @hold_libraries;
272     while ( my $library_group = $library_groups->next ) {
273         my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
274         if($root->ft_local_hold_group) {
275             push @hold_libraries, $root->all_libraries;
276         }
277     }
278
279     my %seen;
280     @hold_libraries =
281       grep { !$seen{ $_->id }++ } @hold_libraries;
282
283     return Koha::Libraries->search({ branchcode => { '-in' => [ keys %seen ] } });
284 }
285
286 =head3 validate_hold_sibling
287
288 Return if given library is a valid hold group member
289
290 =cut
291
292 sub validate_hold_sibling {
293     my ( $self, $params ) = @_;
294
295     return 1 if $params->{branchcode} eq $self->id;
296
297     my $branchcode = $params->{branchcode};
298     return $self->get_hold_libraries->search( { branchcode => $branchcode } )
299       ->count > 0;
300 }
301
302 =head2 Internal methods
303
304 =head3 _type
305
306 =cut
307
308 sub _type {
309     return 'Branch';
310 }
311
312 1;