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>.
25 use vars qw(@ISA @EXPORT);
28 # set the version for version checking
32 &GetSubscriptionFrequencies
33 &GetSubscriptionFrequency
34 &AddSubscriptionFrequency
35 &ModSubscriptionFrequency
36 &DelSubscriptionFrequency
38 &GetSubscriptionsWithFrequency
45 C4::Serials::Frequency - Serials Frequency module
49 =head2 GetSubscriptionFrequencies
53 =item C<@frequencies> = &GetSubscriptionFrequencies();
55 gets frequencies restricted on filters
61 sub GetSubscriptionFrequencies {
62 my $dbh = C4::Context->dbh;
65 FROM subscription_frequencies
68 my $sth = $dbh->prepare($query);
71 my $results = $sth->fetchall_arrayref( {} );
75 =head2 GetSubscriptionFrequency
79 =item $frequency = &GetSubscriptionFrequency($frequencyid);
81 gets frequency where $frequencyid is the identifier
87 sub GetSubscriptionFrequency {
88 my ($frequencyid) = @_;
90 my $dbh = C4::Context->dbh;
93 FROM subscription_frequencies
96 my $sth = $dbh->prepare($query);
97 $sth->execute($frequencyid);
99 return $sth->fetchrow_hashref;
102 =head2 AddSubscriptionFrequency
106 =item C<$frequencyid> = &AddSubscriptionFrequency($frequency);
110 =item C<$frequency> is a hashref that can contains the following keys
118 =item * issuesperunit
120 =item * unitsperissue
122 =item * expectedissuesayear
128 Only description is mandatory.
134 sub AddSubscriptionFrequency {
135 my $frequency = shift;
137 unless(ref($frequency) eq 'HASH' && defined $frequency->{'description'} && $frequency->{'description'} ne '') {
143 foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
144 if(exists $frequency->{$_}) {
146 push @values, $frequency->{$_};
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);
158 return $dbh->last_insert_id(undef, undef, "subscription_frequencies", undef);
164 =head2 ModSubscriptionFrequency
168 =item &ModSubscriptionFrequency($frequency);
172 =item C<$frequency> is a hashref that can contains the following keys
182 =item * issuesperunit
184 =item * unitsperissue
186 =item * expectedissuesayear
192 Only id is mandatory.
198 sub ModSubscriptionFrequency {
199 my $frequency = shift;
202 ref($frequency) eq 'HASH'
203 && defined $frequency->{'id'} && $frequency->{'id'} > 0
205 (defined $frequency->{'description'}
206 && $frequency->{'description'} ne '')
207 || !defined $frequency->{'description'}
215 foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
216 if(exists $frequency->{$_}) {
218 push @values, $frequency->{$_};
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);
228 return $sth->execute(@values, $frequency->{'id'});
231 =head2 DelSubscriptionFrequency
235 =item &DelSubscriptionFrequency($frequencyid);
243 sub DelSubscriptionFrequency {
244 my $frequencyid = shift;
246 my $dbh = C4::Context->dbh;
248 DELETE FROM subscription_frequencies
251 my $sth = $dbh->prepare($query);
252 $sth->execute($frequencyid);
255 =head2 GetSubscriptionsWithFrequency
257 my @subs = GetSubscriptionsWithFrequency($frequencyid);
259 Returns all subscriptions that are using a particular frequency
263 sub GetSubscriptionsWithFrequency {
264 my ($frequencyid) = @_;
266 return unless $frequencyid;
268 my $dbh = C4::Context->dbh;
272 LEFT JOIN biblio ON subscription.biblionumber = biblio.biblionumber
273 WHERE periodicity = ?
275 my $sth = $dbh->prepare($query);
277 if ($sth->execute($frequencyid)) {
278 @results = @{ $sth->fetchall_arrayref({}) };
289 Koha Development team <info@koha.org>