Bug 19809: Re-allow to call Koha::Objects::find in list context
[koha.git] / Koha / Subscription.pm
blob8213818fa5e8f4d4c9a41604287082c80ab45580
1 package Koha::Subscription;
3 # Copyright ByWater Solutions 2015
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
25 use Koha::Biblios;
26 use Koha::Acquisition::Booksellers;
27 use Koha::Biblioitems;
28 use Koha::Subscriptions;
29 use Koha::Subscription::Frequencies;
30 use Koha::Subscription::Numberpatterns;
31 use JSON;
33 use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields);
35 =head1 NAME
37 Koha::Subscription - Koha Subscription Object class
39 =head1 API
41 =head2 Class Methods
43 =cut
45 =head3 biblio
47 Returns the biblio linked to this subscription as a Koha::Biblio object
49 =cut
51 sub biblio {
52 my ($self) = @_;
54 return Koha::Biblios->find($self->biblionumber);
57 =head3 vendor
59 Returns the vendor/supplier linked to this subscription as a Koha::Acquisition::Bookseller object
61 =cut
63 sub vendor {
64 my ($self) = @_;
65 return Koha::Acquisition::Booksellers->find($self->aqbooksellerid);
68 =head3 subscribers
70 my $subscribers = $subscription->subscribers;
72 return a Koha::Patrons object
74 =cut
76 sub subscribers {
77 my ($self) = @_;
78 my $schema = Koha::Database->new->schema;
79 my @borrowernumbers = $schema->resultset('Alert')->search({ externalid => $self->subscriptionid })->get_column( 'borrowernumber' )->all;
80 return Koha::Patrons->search({ borrowernumber => {-in => \@borrowernumbers } });
83 =head3 add_subscriber
85 $subscription->add_subscriber( $patron );
87 Add a new subscriber (Koha::Patron) to this subscription
89 =cut
91 sub add_subscriber {
92 my ( $self, $patron ) = @_;
93 my $schema = Koha::Database->new->schema;
94 my $rs = $schema->resultset('Alert');
95 $rs->create({ externalid => $self->subscriptionid, borrowernumber => $patron->borrowernumber });
98 =head3 remove_subscriber
100 $subscription->remove_subscriber( $subscriber );
102 Remove a subscriber (Koha::Patron) from this subscription
104 =cut
106 sub remove_subscriber {
107 my ($self, $patron) = @_;
108 my $schema = Koha::Database->new->schema;
109 my $rs = $schema->resultset('Alert');
110 my $subscriber = $rs->find({ externalid => $self->subscriptionid, borrowernumber => $patron->borrowernumber });
111 $subscriber->delete if $subscriber;
114 =head3 frequency
116 my $frequency = $subscription->frequency
118 Return the subscription frequency
120 =cut
122 sub frequency {
123 my($self)=@_;
124 my $frequency_rs= $self->_result->periodicity;
125 return Koha::Subscription::Frequency->_new_from_dbic($frequency_rs);
128 =head3 get_search_info
130 =cut
132 sub get_search_info {
133 my $self=shift;
134 my $searched_sub_id = shift;
135 my $biblio = Koha::Biblios->find( { 'biblionumber' => $searched_sub_id } );
136 return unless $biblio;
137 my $biblioitem =
138 Koha::Biblioitems->find( { 'biblionumber' => $searched_sub_id } );
140 my $sub_mana_info = {
141 'title' => $biblio->title,
142 'issn' => $biblioitem->issn,
143 'ean' => $biblioitem->ean,
144 'publishercode' => $biblioitem->publishercode
146 return $sub_mana_info;
149 =head3 get_sharable_info
151 =cut
153 sub get_sharable_info {
154 my $self = shift;
155 my $shared_sub_id = shift;
156 my $subscription = Koha::Subscriptions->find($shared_sub_id);
157 my $biblio = Koha::Biblios->find( $subscription->biblionumber );
158 my $biblioitem = Koha::Biblioitems->find(
159 { 'biblionumber' => $subscription->biblionumber } );
160 my $sub_frequency =
161 Koha::Subscription::Frequencies->find( $subscription->periodicity );
162 my $sub_numberpatteern =
163 Koha::Subscription::Numberpatterns->find( $subscription->numberpattern );
165 my $sub_mana_info = {
166 'title' => $biblio->title,
167 'sfdescription' => $sub_frequency->description,
168 'unit' => $sub_frequency->unit,
169 'unitsperissue' => $sub_frequency->unitsperissue,
170 'issuesperunit' => $sub_frequency->issuesperunit,
171 'label' => $sub_numberpatteern->label,
172 'sndescription' => $sub_numberpatteern->description,
173 'numberingmethod' => $sub_numberpatteern->numberingmethod,
174 'label1' => $sub_numberpatteern->label1,
175 'add1' => $sub_numberpatteern->add1,
176 'every1' => $sub_numberpatteern->every1,
177 'whenmorethan1' => $sub_numberpatteern->whenmorethan1,
178 'setto1' => $sub_numberpatteern->setto1,
179 'numbering1' => $sub_numberpatteern->numbering1,
180 'label2' => $sub_numberpatteern->label2,
181 'add2' => $sub_numberpatteern->add2,
182 'every2' => $sub_numberpatteern->every2,
183 'whenmorethan2' => $sub_numberpatteern->whenmorethan2,
184 'setto2' => $sub_numberpatteern->setto2,
185 'numbering2' => $sub_numberpatteern->numbering2,
186 'label3' => $sub_numberpatteern->label3,
187 'add3' => $sub_numberpatteern->add3,
188 'every3' => $sub_numberpatteern->every3,
189 'whenmorethan3' => $sub_numberpatteern->whenmorethan3,
190 'setto3' => $sub_numberpatteern->setto3,
191 'numbering3' => $sub_numberpatteern->numbering3,
192 'issn' => $biblioitem->issn,
193 'ean' => $biblioitem->ean,
194 'publishercode' => $biblioitem->publishercode
196 return $sub_mana_info;
200 =head3 _type
202 =cut
204 sub _type {
205 return 'Subscription';
208 =head1 AUTHOR
210 Kyle M Hall <kyle@bywatersolutions.com>
212 =cut