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