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