Bug 27796: Centralise payment/transaction type handling
[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 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 inbound_email_address
147
148   my $to_email = Koha::Library->inbound_email_address;
149
150 Returns an effective email address which should be accessible to librarians at the branch.
151
152 =cut
153
154 sub inbound_email_address {
155     my ($self) = @_;
156
157     return
158          $self->branchreplyto
159       || $self->branchemail
160       || C4::Context->preference('ReplytoDefault')
161       || C4::Context->preference('KohaAdminEmailAddress')
162       || undef;
163 }
164
165 =head3 inbound_ill_address
166
167   my $to_email = Koha::Library->inbound_ill_address;
168
169 Returns an effective email address which should be accessible to librarians at the branch
170 for inter library loans communication.
171
172 =cut
173
174 sub inbound_ill_address {
175     my ($self) = @_;
176
177     return
178          $self->branchillemail
179       || C4::Context->preference('ILLDefaultStaffEmail')
180       || $self->inbound_email_address;
181 }
182
183 =head3 library_groups
184
185 Return the Library groups of this library
186
187 =cut
188
189 sub library_groups {
190     my ( $self ) = @_;
191     my $rs = $self->_result->library_groups;
192     return Koha::Library::Groups->_new_from_dbic( $rs );
193 }
194
195 =head3 cash_registers
196
197 Return Cash::Registers associated with this Library
198
199 =cut
200
201 sub cash_registers {
202     my ( $self ) = @_;
203     my $rs = $self->_result->cash_registers;
204     return Koha::Cash::Registers->_new_from_dbic( $rs );
205 }
206
207 =head3 to_api_mapping
208
209 This method returns the mapping for representing a Koha::Library object
210 on the API.
211
212 =cut
213
214 sub to_api_mapping {
215     return {
216         branchcode       => 'library_id',
217         branchname       => 'name',
218         branchaddress1   => 'address1',
219         branchaddress2   => 'address2',
220         branchaddress3   => 'address3',
221         branchzip        => 'postal_code',
222         branchcity       => 'city',
223         branchstate      => 'state',
224         branchcountry    => 'country',
225         branchphone      => 'phone',
226         branchfax        => 'fax',
227         branchemail      => 'email',
228         branchillemail   => 'illemail',
229         branchreplyto    => 'reply_to_email',
230         branchreturnpath => 'return_path_email',
231         branchurl        => 'url',
232         issuing          => undef,
233         branchip         => 'ip',
234         branchnotes      => 'notes',
235         marcorgcode      => 'marc_org_code',
236     };
237 }
238
239 =head3 get_hold_libraries
240
241 Return all libraries (including self) that belong to the same hold groups
242
243 =cut
244
245 sub get_hold_libraries {
246     my ( $self ) = @_;
247     my $library_groups = $self->library_groups;
248     my @hold_libraries;
249     while ( my $library_group = $library_groups->next ) {
250         my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
251         if($root->ft_local_hold_group) {
252             push @hold_libraries, $root->all_libraries;
253         }
254     }
255
256     my %seen;
257     @hold_libraries =
258       grep { !$seen{ $_->id }++ } @hold_libraries;
259
260     return Koha::Libraries->search({ branchcode => { '-in' => [ keys %seen ] } });
261 }
262
263 =head3 validate_hold_sibling
264
265 Return if given library is a valid hold group member
266
267 =cut
268
269 sub validate_hold_sibling {
270     my ( $self, $params ) = @_;
271
272     return 1 if $params->{branchcode} eq $self->id;
273
274     my $branchcode = $params->{branchcode};
275     return $self->get_hold_libraries->search( { branchcode => $branchcode } )
276       ->count > 0;
277 }
278
279 =head2 Internal methods
280
281 =head3 _type
282
283 =cut
284
285 sub _type {
286     return 'Branch';
287 }
288
289 1;