Bug 18999: (QA followup) Minor fixes for tests
[koha.git] / C4 / Serials / Frequency.pm
blob509f6801f5f6ffe1a740cf6717e94613609a7e42
1 package C4::Serials::Frequency;
3 # Copyright 2011-2013 Biblibre SARL
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 use C4::Context;
25 use vars qw(@ISA @EXPORT);
27 BEGIN {
28 require Exporter;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetSubscriptionFrequencies
32 &GetSubscriptionFrequency
33 &AddSubscriptionFrequency
34 &ModSubscriptionFrequency
35 &DelSubscriptionFrequency
37 &GetSubscriptionsWithFrequency
42 =head1 NAME
44 C4::Serials::Frequency - Serials Frequency module
46 =head1 FUNCTIONS
48 =head2 GetSubscriptionFrequencies
50 =over 4
52 =item C<@frequencies> = &GetSubscriptionFrequencies();
54 gets frequencies restricted on filters
56 =back
58 =cut
60 sub GetSubscriptionFrequencies {
61 my $dbh = C4::Context->dbh;
62 my $query = qq{
63 SELECT *
64 FROM subscription_frequencies
65 ORDER BY displayorder
67 my $sth = $dbh->prepare($query);
68 $sth->execute();
70 my $results = $sth->fetchall_arrayref( {} );
71 return @$results;
74 =head2 GetSubscriptionFrequency
76 =over 4
78 =item $frequency = &GetSubscriptionFrequency($frequencyid);
80 gets frequency where $frequencyid is the identifier
82 =back
84 =cut
86 sub GetSubscriptionFrequency {
87 my ($frequencyid) = @_;
89 my $dbh = C4::Context->dbh;
90 my $query = qq{
91 SELECT *
92 FROM subscription_frequencies
93 WHERE id = ?
95 my $sth = $dbh->prepare($query);
96 $sth->execute($frequencyid);
98 return $sth->fetchrow_hashref;
101 =head2 AddSubscriptionFrequency
103 =over 4
105 =item C<$frequencyid> = &AddSubscriptionFrequency($frequency);
107 Add a new frequency
109 =item C<$frequency> is a hashref that can contains the following keys
111 =over 2
113 =item * description
115 =item * unit
117 =item * issuesperunit
119 =item * unitsperissue
121 =item * expectedissuesayear
123 =item * displayorder
125 =back
127 Only description is mandatory.
129 =back
131 =cut
133 sub AddSubscriptionFrequency {
134 my $frequency = shift;
136 unless(ref($frequency) eq 'HASH' && defined $frequency->{'description'} && $frequency->{'description'} ne '') {
137 return;
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->{$_};
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);
156 if(defined $rv) {
157 return $dbh->last_insert_id(undef, undef, "subscription_frequencies", undef);
160 return $rv;
163 =head2 ModSubscriptionFrequency
165 =over 4
167 =item &ModSubscriptionFrequency($frequency);
169 Modifies a frequency
171 =item C<$frequency> is a hashref that can contains the following keys
173 =over 2
175 =item * id
177 =item * description
179 =item * unit
181 =item * issuesperunit
183 =item * unitsperissue
185 =item * expectedissuesayear
187 =item * displayorder
189 =back
191 Only id is mandatory.
193 =back
195 =cut
197 sub ModSubscriptionFrequency {
198 my $frequency = shift;
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'}
209 return;
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->{$_};
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);
227 return $sth->execute(@values, $frequency->{'id'});
230 =head2 DelSubscriptionFrequency
232 =over 4
234 =item &DelSubscriptionFrequency($frequencyid);
236 Delete a frequency
238 =back
240 =cut
242 sub DelSubscriptionFrequency {
243 my $frequencyid = shift;
245 my $dbh = C4::Context->dbh;
246 my $query = qq{
247 DELETE FROM subscription_frequencies
248 WHERE id = ?
250 my $sth = $dbh->prepare($query);
251 $sth->execute($frequencyid);
254 =head2 GetSubscriptionsWithFrequency
256 my @subs = GetSubscriptionsWithFrequency($frequencyid);
258 Returns all subscriptions that are using a particular frequency
260 =cut
262 sub GetSubscriptionsWithFrequency {
263 my ($frequencyid) = @_;
265 return unless $frequencyid;
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 = ?
274 my $sth = $dbh->prepare($query);
275 my @results;
276 if ($sth->execute($frequencyid)) {
277 @results = @{ $sth->fetchall_arrayref({}) };
279 return @results;
284 __END__
286 =head1 AUTHOR
288 Koha Development team <info@koha.org>
290 =cut