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