Bug 7688 follow-up: Small fixes for QA #2
[koha.git] / C4 / Serials / Frequency.pm
1 package C4::Serials::Frequency;
2
3 # Copyright 2000-2002 Biblibre SARL
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 2 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 strict;
21 use warnings;
22
23 use C4::Context;
24
25 use vars qw($VERSION @ISA @EXPORT);
26
27 BEGIN {
28     # set the version for version checking
29     $VERSION = 3.01;
30     require Exporter;
31     @ISA    = qw(Exporter);
32     @EXPORT = qw(
33       &GetSubscriptionFrequencies
34       &GetSubscriptionFrequency
35       &AddSubscriptionFrequency
36       &ModSubscriptionFrequency
37       &DelSubscriptionFrequency
38
39       &GetSubscriptionsWithFrequency
40     );
41 }
42
43 =head3 GetSubscriptionFrequencies
44
45 =over 4
46
47 =item C<@frequencies> = &GetSubscriptionFrequencies();
48
49 gets frequencies restricted on filters
50
51 =back
52
53 =cut
54
55 sub GetSubscriptionFrequencies {
56     my $dbh = C4::Context->dbh;
57     my $query = qq{
58         SELECT *
59         FROM subscription_frequencies
60         ORDER BY displayorder
61     };
62     my $sth = $dbh->prepare($query);
63     $sth->execute();
64
65     my $results = $sth->fetchall_arrayref( {} );
66     return @$results;
67 }
68
69 =head3 GetSubscriptionFrequency
70
71 =over 4
72
73 =item $frequency = &GetSubscriptionFrequency($frequencyid);
74
75 gets frequency where $frequencyid is the identifier
76
77 =back
78
79 =cut
80
81 sub GetSubscriptionFrequency {
82     my ($frequencyid) = @_;
83
84     my $dbh = C4::Context->dbh;
85     my $query = qq{
86         SELECT *
87         FROM subscription_frequencies
88         WHERE id = ?
89     };
90     my $sth = $dbh->prepare($query);
91     $sth->execute($frequencyid);
92
93     return $sth->fetchrow_hashref;
94 }
95
96 =head3 AddSubscriptionFrequency
97
98 =over 4
99
100 =item C<$frequencyid> = &AddSubscriptionFrequency($frequency);
101
102 Add a new frequency
103
104 =item C<$frequency> is a hashref that can contains the following keys
105
106 =over 2
107
108 =item * description
109
110 =item * unit
111
112 =item * issuesperunit
113
114 =item * unitsperissue
115
116 =item * expectedissuesayear
117
118 =item * displayorder
119
120 =back
121
122 Only description is mandatory.
123
124 =back
125
126 =cut
127
128 sub AddSubscriptionFrequency {
129     my $frequency = shift;
130
131     unless(ref($frequency) eq 'HASH' && defined $frequency->{'description'} && $frequency->{'description'} ne '') {
132         return;
133     }
134
135     my @keys;
136     my @values;
137     foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
138         if(exists $frequency->{$_}) {
139             push @keys, $_;
140             push @values, $frequency->{$_};
141         }
142     }
143
144     my $dbh = C4::Context->dbh;
145     my $query = "INSERT INTO subscription_frequencies";
146     $query .= '(' . join(',', @keys) . ')';
147     $query .= ' VALUES (' . ('?,' x (scalar(@keys)-1)) . '?)';
148     my $sth = $dbh->prepare($query);
149     my $rv = $sth->execute(@values);
150
151     if(defined $rv) {
152         return $dbh->last_insert_id(undef, undef, "subscription_frequencies", undef);
153     }
154
155     return $rv;
156 }
157
158 =head3 ModSubscriptionFrequency
159
160 =over 4
161
162 =item &ModSubscriptionFrequency($frequency);
163
164 Modifies a frequency
165
166 =item C<$frequency> is a hashref that can contains the following keys
167
168 =over 2
169
170 =item * id
171
172 =item * description
173
174 =item * unit
175
176 =item * issuesperunit
177
178 =item * unitsperissue
179
180 =item * expectedissuesayear
181
182 =item * displayorder
183
184 =back
185
186 Only id is mandatory.
187
188 =back
189
190 =cut
191
192 sub ModSubscriptionFrequency {
193     my $frequency = shift;
194
195     unless(
196       ref($frequency) eq 'HASH'
197       && defined $frequency->{'id'} && $frequency->{'id'} > 0
198       && (
199         (defined $frequency->{'description'}
200         && $frequency->{'description'} ne '')
201         || !defined $frequency->{'description'}
202       )
203     ) {
204         return;
205     }
206
207     my @keys;
208     my @values;
209     foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
210         if(exists $frequency->{$_}) {
211             push @keys, $_;
212             push @values, $frequency->{$_};
213         }
214     }
215
216     my $dbh = C4::Context->dbh;
217     my $query = "UPDATE subscription_frequencies";
218     $query .= ' SET ' . join(' = ?,', @keys) . ' = ?';
219     $query .= ' WHERE id = ?';
220     my $sth = $dbh->prepare($query);
221
222     return $sth->execute(@values, $frequency->{'id'});
223 }
224
225 =head3 DelSubscriptionFrequency
226
227 =over 4
228
229 =item &DelSubscriptionFrequency($frequencyid);
230
231 Delete a frequency
232
233 =back
234
235 =cut
236
237 sub DelSubscriptionFrequency {
238     my $frequencyid = shift;
239
240     my $dbh = C4::Context->dbh;
241     my $query = qq{
242         DELETE FROM subscription_frequencies
243         WHERE id = ?
244     };
245     my $sth = $dbh->prepare($query);
246     $sth->execute($frequencyid);
247 }
248
249 =head3 GetSubscriptionsWithFrequency
250
251     my @subs = GetSubscriptionsWithFrequency($frequencyid);
252
253 Returns all subscriptions that are using a particular frequency
254
255 =cut
256
257 sub GetSubscriptionsWithFrequency {
258     my ($frequencyid) = @_;
259
260     return unless $frequencyid;
261
262     my $dbh = C4::Context->dbh;
263     my $query = qq{
264         SELECT *
265         FROM subscription
266           LEFT JOIN biblio ON subscription.biblionumber = biblio.biblionumber
267         WHERE periodicity = ?
268     };
269     my $sth = $dbh->prepare($query);
270     my @results;
271     if ($sth->execute($frequencyid)) {
272         @results = @{ $sth->fetchall_arrayref({}) };
273     }
274     return @results;
275 }
276
277 1;
278
279 __END__
280
281 =head1 AUTHOR
282
283 Koha Developement team <info@koha.org>
284
285 =cut