Bug 30477: Add new UNIMARC installer translation files
[koha.git] / Koha / Subscription.pm
1 package Koha::Subscription;
2
3 # Copyright ByWater Solutions 2015
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 Koha::Database;
24 use Koha::Biblios;
25 use Koha::Acquisition::Booksellers;
26 use Koha::Biblioitems;
27 use Koha::Subscriptions;
28 use Koha::Subscription::Frequencies;
29 use Koha::Subscription::Numberpatterns;
30
31 use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields);
32
33 =head1 NAME
34
35 Koha::Subscription - Koha Subscription Object class
36
37 =head1 API
38
39 =head2 Class Methods
40
41 =cut
42
43 =head3 biblio
44
45 Returns the biblio linked to this subscription as a Koha::Biblio object
46
47 =cut
48
49 sub biblio {
50     my ($self) = @_;
51
52     return Koha::Biblios->find($self->biblionumber);
53 }
54
55 =head3 vendor
56
57 Returns the vendor/supplier linked to this subscription as a Koha::Acquisition::Bookseller object
58
59 =cut
60
61 sub vendor {
62     my ($self) = @_;
63     return Koha::Acquisition::Booksellers->find($self->aqbooksellerid);
64 }
65
66 =head3 subscribers
67
68 my $subscribers = $subscription->subscribers;
69
70 return a Koha::Patrons object
71
72 =cut
73
74 sub subscribers {
75     my ($self) = @_;
76     my $schema = Koha::Database->new->schema;
77     my @borrowernumbers = $schema->resultset('Alert')->search({ externalid => $self->subscriptionid })->get_column( 'borrowernumber' )->all;
78     return Koha::Patrons->search({ borrowernumber => {-in => \@borrowernumbers } });
79 }
80
81 =head3 add_subscriber
82
83 $subscription->add_subscriber( $patron );
84
85 Add a new subscriber (Koha::Patron) to this subscription
86
87 =cut
88
89 sub add_subscriber {
90     my ( $self, $patron )  = @_;
91     my $schema = Koha::Database->new->schema;
92     my $rs = $schema->resultset('Alert');
93     $rs->create({ externalid => $self->subscriptionid, borrowernumber => $patron->borrowernumber });
94 }
95
96 =head3 remove_subscriber
97
98 $subscription->remove_subscriber( $subscriber );
99
100 Remove a subscriber (Koha::Patron) from this subscription
101
102 =cut
103
104 sub remove_subscriber {
105     my ($self, $patron) = @_;
106     my $schema = Koha::Database->new->schema;
107     my $rs = $schema->resultset('Alert');
108     my $subscriber = $rs->find({ externalid => $self->subscriptionid, borrowernumber => $patron->borrowernumber });
109     $subscriber->delete if $subscriber;
110 }
111
112 =head3 frequency
113
114 my $frequency = $subscription->frequency
115
116 Return the subscription frequency
117
118 =cut
119
120 sub frequency {
121     my($self)=@_;
122     my $frequency_rs= $self->_result->periodicity;
123     return Koha::Subscription::Frequency->_new_from_dbic($frequency_rs);
124 }
125
126 =head3 get_search_info
127
128 =cut
129
130 sub get_search_info {
131     my $self=shift;
132     my $searched_sub_id = shift;
133     my $biblio = Koha::Biblios->find( { 'biblionumber' => $searched_sub_id } );
134     return unless $biblio;
135     my $biblioitem =
136       Koha::Biblioitems->find( { 'biblionumber' => $searched_sub_id } );
137
138     my $sub_mana_info = {
139         'title'         => $biblio->title,
140         'issn'          => $biblioitem->issn,
141         'ean'           => $biblioitem->ean,
142         'publishercode' => $biblioitem->publishercode
143     };
144     return $sub_mana_info;
145 }
146
147 =head3 get_sharable_info
148
149 =cut
150
151 sub get_sharable_info {
152     my $self = shift;
153     my $shared_sub_id = shift;
154     my $subscription  = Koha::Subscriptions->find($shared_sub_id);
155     my $biblio        = Koha::Biblios->find( $subscription->biblionumber );
156     my $biblioitem    = Koha::Biblioitems->find(
157         { 'biblionumber' => $subscription->biblionumber } );
158     my $sub_frequency =
159       Koha::Subscription::Frequencies->find( $subscription->periodicity );
160     my $sub_numberpatteern =
161       Koha::Subscription::Numberpatterns->find( $subscription->numberpattern );
162
163     my $sub_mana_info = {
164         'title'           => $biblio->title,
165         'sfdescription'   => $sub_frequency->description,
166         'unit'            => $sub_frequency->unit,
167         'unitsperissue'   => $sub_frequency->unitsperissue,
168         'issuesperunit'   => $sub_frequency->issuesperunit,
169         'label'           => $sub_numberpatteern->label,
170         'sndescription'   => $sub_numberpatteern->description,
171         'numberingmethod' => $sub_numberpatteern->numberingmethod,
172         'label1'          => $sub_numberpatteern->label1,
173         'add1'            => $sub_numberpatteern->add1,
174         'every1'          => $sub_numberpatteern->every1,
175         'whenmorethan1'   => $sub_numberpatteern->whenmorethan1,
176         'setto1'          => $sub_numberpatteern->setto1,
177         'numbering1'      => $sub_numberpatteern->numbering1,
178         'label2'          => $sub_numberpatteern->label2,
179         'add2'            => $sub_numberpatteern->add2,
180         'every2'          => $sub_numberpatteern->every2,
181         'whenmorethan2'   => $sub_numberpatteern->whenmorethan2,
182         'setto2'          => $sub_numberpatteern->setto2,
183         'numbering2'      => $sub_numberpatteern->numbering2,
184         'label3'          => $sub_numberpatteern->label3,
185         'add3'            => $sub_numberpatteern->add3,
186         'every3'          => $sub_numberpatteern->every3,
187         'whenmorethan3'   => $sub_numberpatteern->whenmorethan3,
188         'setto3'          => $sub_numberpatteern->setto3,
189         'numbering3'      => $sub_numberpatteern->numbering3,
190         'issn'            => $biblioitem->issn,
191         'ean'             => $biblioitem->ean,
192         'publishercode'   => $biblioitem->publishercode
193     };
194     return $sub_mana_info;
195 }
196
197
198 =head3 _type
199
200 =cut
201
202 sub _type {
203     return 'Subscription';
204 }
205
206 =head1 AUTHOR
207
208 Kyle M Hall <kyle@bywatersolutions.com>
209
210 =cut
211
212 1;