Bug 17047: SQL reports management with Mana-KB
[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 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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
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;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Subscription - Koha Subscription Object class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 biblio
46
47 Returns the biblio linked to this subscription as a Koha::Biblio object
48
49 =cut
50
51 sub biblio {
52     my ($self) = @_;
53
54     return scalar Koha::Biblios->find($self->biblionumber);
55 }
56
57 =head3 vendor
58
59 Returns the vendor/supplier linked to this subscription as a Koha::Acquisition::Bookseller object
60
61 =cut
62
63 sub vendor {
64     my ($self) = @_;
65     return scalar Koha::Acquisition::Booksellers->find($self->aqbooksellerid);
66 }
67
68 =head3 subscribers
69
70 my $subscribers = $subscription->subscribers;
71
72 return a Koha::Patrons object
73
74 =cut
75
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 } });
81 }
82
83 =head3 add_subscriber
84
85 $subscription->add_subscriber( $patron );
86
87 Add a new subscriber (Koha::Patron) to this subscription
88
89 =cut
90
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 });
96 }
97
98 =head3 remove_subscriber
99
100 $subscription->remove_subscriber( $subscriber );
101
102 Remove a subscriber (Koha::Patron) from this subscription
103
104 =cut
105
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;
112 }
113
114 =head3 frequency
115
116 my $frequency = $subscription->frequency
117
118 Return the subscription frequency
119
120 =cut
121
122 sub frequency {
123     my($self)=@_;
124     my $frequency_rs= $self->_result->periodicity;
125     return Koha::Subscription::Frequency->_new_from_dbic($frequency_rs);
126 }
127
128 =head3 get_search_info
129
130 =cut
131
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 } );
139
140     my $sub_mana_info = {
141         'title'         => $biblio->title,
142         'issn'          => $biblioitem->issn,
143         'ean'           => $biblioitem->ean,
144         'publishercode' => $biblioitem->publishercode
145     };
146     return $sub_mana_info;
147 }
148
149 =head3 get_sharable_info
150
151 =cut
152
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 );
164
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
195     };
196     return $sub_mana_info;
197 }
198
199
200 =head3 _type
201
202 =cut
203
204 sub _type {
205     return 'Subscription';
206 }
207
208 =head1 AUTHOR
209
210 Kyle M Hall <kyle@bywatersolutions.com>
211
212 =cut
213
214 1;