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