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>.
27 use Koha::StockRotationStages;
28 use Koha::SMTP::Servers;
30 use base qw(Koha::Object);
34 Koha::Library - Koha Library Object class
40 =head3 stockrotationstages
42 my $stages = Koha::Library->stockrotationstages;
44 Returns the stockrotation stages associated with this Library.
48 sub stockrotationstages {
50 my $rs = $self->_result->stockrotationstages;
51 return Koha::StockRotationStages->_new_from_dbic( $rs );
54 =head3 outgoing_transfers
56 my $outgoing_transfers = Koha::Library->outgoing_transfers;
58 Returns the outgoing item transfers associated with this Library.
62 sub outgoing_transfers {
64 my $rs = $self->_result->branchtransfers_frombranches;
65 return Koha::Item::Transfers->_new_from_dbic( $rs );
68 =head3 inbound_transfers
70 my $inbound_transfers = Koha::Library->inbound_transfers;
72 Returns the inbound item transfers associated with this Library.
76 sub inbound_transfers {
78 my $rs = $self->_result->branchtransfers_tobranches;
79 return Koha::Item::Transfers->_new_from_dbic( $rs );
82 =head3 get_effective_marcorgcode
84 my $marcorgcode = Koha::Libraries->find( $library_id )->get_effective_marcorgcode();
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.
91 sub get_effective_marcorgcode {
94 return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
99 my $smtp_server = $library->smtp_server;
100 $library->smtp_server({ smtp_server => $smtp_server });
101 $library->smtp_server({ smtp_server => undef });
103 Accessor for getting and setting the library's SMTP server.
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.
108 Setting it to undef will remove the link to a specific SMTP server and effectively
109 make the library use the default setting
114 my ( $self, $params ) = @_;
116 my $library_smtp_server_rs = $self->_result->library_smtp_server;
118 if ( exists $params->{smtp_server} ) {
120 $self->_result->result_source->schema->txn_do( sub {
121 $library_smtp_server_rs->delete
122 if $library_smtp_server_rs;
124 if ( defined $params->{smtp_server} ) {
126 # Remove any already set SMTP server
128 my $smtp_server = $params->{smtp_server};
129 $smtp_server->_result->add_to_library_smtp_servers({ library_id => $self->id });
132 } # else => reset to default
135 if ( $library_smtp_server_rs ) {
136 return Koha::SMTP::Servers->find(
137 $library_smtp_server_rs->smtp_server_id );
140 return Koha::SMTP::Servers->get_default;
146 =head3 from_email_address
148 my $from_email = Koha::Library->from_email_address;
150 Returns the official 'from' email address for the branch.
152 It may well be a 'noreply' or other inaccessible local domain
153 address that is being used to satisfy spam protection filters.
157 sub from_email_address {
162 || C4::Context->preference('KohaAdminEmailAddress')
166 =head3 inbound_email_address
168 my $to_email = Koha::Library->inbound_email_address;
170 Returns an effective email address which should be accessible to librarians at the branch.
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.
177 sub inbound_email_address {
182 || $self->branchemail
183 || C4::Context->preference('ReplytoDefault')
184 || C4::Context->preference('KohaAdminEmailAddress')
188 =head3 inbound_ill_address
190 my $to_email = Koha::Library->inbound_ill_address;
192 Returns an effective email address which should be accessible to librarians at the branch
193 for inter library loans communication.
197 sub inbound_ill_address {
201 $self->branchillemail
202 || C4::Context->preference('ILLDefaultStaffEmail')
203 || $self->inbound_email_address;
206 =head3 library_groups
208 Return the Library groups of this library
214 my $rs = $self->_result->library_groups;
215 return Koha::Library::Groups->_new_from_dbic( $rs );
218 =head3 cash_registers
220 Return Cash::Registers associated with this Library
226 my $rs = $self->_result->cash_registers;
227 return Koha::Cash::Registers->_new_from_dbic( $rs );
230 =head3 to_api_mapping
232 This method returns the mapping for representing a Koha::Library object
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',
250 branchemail => 'email',
251 branchillemail => 'illemail',
252 branchreplyto => 'reply_to_email',
253 branchreturnpath => 'return_path_email',
257 branchnotes => 'notes',
258 marcorgcode => 'marc_org_code',
262 =head3 get_hold_libraries
264 Return all libraries (including self) that belong to the same hold groups
268 sub get_hold_libraries {
270 my $library_groups = $self->library_groups;
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;
281 grep { !$seen{ $_->id }++ } @hold_libraries;
283 return Koha::Libraries->search({ branchcode => { '-in' => [ keys %seen ] } });
286 =head3 validate_hold_sibling
288 Return if given library is a valid hold group member
292 sub validate_hold_sibling {
293 my ( $self, $params ) = @_;
295 return 1 if $params->{branchcode} eq $self->id;
297 my $branchcode = $params->{branchcode};
298 return $self->get_hold_libraries->search( { branchcode => $branchcode } )
302 =head2 Internal methods