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