[4/40] Work on C4::Labels::Batch module
[koha.git] / C4 / Labels / Batch.pm
1 package C4::Labels::Batch;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use Sys::Syslog qw(syslog);
24
25 use C4::Context;
26 use C4::Debug;
27 use C4::Biblio;
28 use C4::Labels::Layout 1.000000;        # use version 1.0.0 or better
29 use C4::Labels::Template 1.000000;
30 use Data::Dumper;
31
32 BEGIN {
33     use version; our $VERSION = qv('1.0.0_1');
34 }
35
36 sub _check_params {
37     my $given_params = {};
38     my $exit_code = 0;
39     my @valid_template_params = (
40         'layout_id',
41         'tmpl_id',
42         'prof_id',
43     );
44     if (scalar(@_) >1) {
45         $given_params = {@_};
46         foreach my $key (keys %{$given_params}) {
47             if (!(grep m/$key/, @valid_template_params)) {
48                 syslog("LOG_ERR", "C4::Labels::Batch : Unrecognized parameter type of \"%s\".", $key);
49                 $exit_code = 1;
50             }
51         }
52     }
53     else {
54         if (!(grep m/$_/, @valid_template_params)) {
55             syslog("LOG_ERR", "C4::Labels::Batch : Unrecognized parameter type of \"%s\".", $_);
56             $exit_code = 1;
57         }
58     }
59     return $exit_code;
60 }
61
62 =head1 NAME
63
64 C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
65
66 =cut
67
68 =head1 METHODS
69
70 =head2 C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id)
71
72     Invoking the I<new> method constructs a new batch object with no items.
73
74     example:
75         my $batch = C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id);
76             # Creates and returns a new batch object
77
78     B<NOTE:> This batch is I<not> written to the database untill $batch->save() is invoked. You have been warned!
79
80 =cut
81
82 sub new {
83     my ($invocant, %params) = @_;
84     my $type = ref($invocant) || $invocant;
85     my $self = {
86         batch_id        => 0,
87         layout_id       => $params{layout_id},
88         tmpl_id         => $params{tmpl_id},
89         prof_id      => $params{prof_id},
90         items           => [],
91         batch_stat      => 0,   # False if any data has changed and the db has not been updated
92     };
93     bless ($self, $type);
94     return $self;
95 }
96
97 =head2 $batch->add_item($item_number)
98
99     Invoking the I<add_item> method will add the supplied item to the batch object.
100
101     example:
102         $batch->add_item($item_number);
103
104 =cut
105
106 sub add_item {
107     my $self = shift;
108     my $item_num = shift;
109     push (@{$self->{items}}, $item_num);
110     $self->{batch_stat} = 0;
111 }
112
113 =head2 $batch->get_attr()
114
115     Invoking the I<get_attr> method will return the requested attribute.
116
117     example:
118         my @items = $batch->get_attr($attr);
119
120 =cut
121
122 sub get_attr {
123     my $self = shift;
124     return $self->{$_[0]};
125 }
126
127 =head2 $batch->delete_item()
128
129     Invoking the I<delete_item> method will delete the supplied item from the batch object.
130
131     example:
132         $batch->delete_item();
133
134 =cut
135
136 sub delete_item {
137     my $self = shift;
138     my $item_num = shift;
139     my $index = 0;
140     ++$index until $$self->{items}[$index] == $item_num or $item_num > $#$self->{items};
141     delete ($$self->{items}[$index]);
142     $self->{batch_stat} = 0;
143 }
144
145 =head2 $batch->save()
146
147     Invoking the I<save> method attempts to insert the batch into the database if the batch is new and
148     update the existing batch record if the batch exists. The method returns the new record batch_id upon
149     success and -1 upon failure (This avoids conflicting with a record batch_id of 1). Errors are
150     logged to the syslog.
151
152     example:
153         my $exitstat = $batch->save(); # to save the record behind the $batch object
154
155 =cut
156
157 sub save {
158     my $self = shift;
159     if ($self->{batch_id} > 0) {
160         foreach my $item_number (@$self->{items}) {
161             my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, tmpl_id=?, prof_id=? WHERE batch_id=?;";
162             warn "DEBUG: Updating: $query\n" if $debug;
163             my $sth->C4::Context->dbh->prepare($query);
164             $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id}, $self->{batch_id});
165             if ($sth->err) {
166                 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
167                 return -1;
168             }
169         }
170     }
171     else {
172         foreach my $item_number (@$self->{items}) {
173             my $query = "INSERT INTO labels_batches (item_number, layout_id, tmpl_id, prof_id) VALUES (?,?,?,?);";
174             warn "DEBUG: Inserting: $query\n" if $debug;
175             my $sth->C4::Context->dbh->prepare($query);
176             $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id});
177             if ($sth->err) {
178                 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
179                 return -1;
180             }
181             my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
182             $sth1->execute();
183             my $batch_id = $sth1->fetchrow_array;
184             $self->{batch_id} = $batch_id;
185             return $batch_id;
186         }
187     }
188     $self->{batch_stat} = 1;
189 }
190
191 =head2 C4::Labels::Template->retrieve(template_id)
192
193     Invoking the I<retrieve> method constructs a new template object containing the current values for template_id. The method returns
194     a new object upon success and 1 upon failure. Errors are logged to the syslog. Two further options may be accessed. See the example
195     below for further description.
196
197     examples:
198
199         my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
200
201         my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points,
202             and returns an object containing the record
203
204         my $template = C4::Labels::Template->retrieve(template_id => 1, prof_id => prof_id); # Retrieves template record 1, converts the units
205             to points, applies the given profile id, and returns an object containing the record
206
207 =cut
208
209 sub retrieve {
210     my $invocant = shift;
211     my %opts = @_;
212     my $type = ref($invocant) || $invocant;
213     my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";  
214     my $sth = C4::Context->dbh->prepare($query);
215     $sth->execute($opts{batch_id});
216     if ($sth->err) {
217         syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
218         return 1;
219     }
220     my $self = {
221         items   => [],
222     };
223     while (my $record = $sth->fetchrow_hashref) {
224         $self->{batch_id} = $record->{batch_id};        # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way?
225         $self->{layout_id} = $record->{layout_id};
226         $self->{tmpl_id} = $record->{tmpl_id};
227         $self->{prof_id} = $record->{prof_id};
228         push (@{$self->{items}}, $record->{item_number});
229     }
230     $self->{batch_stat} = 1;
231     bless ($self, $type);
232     return $self;
233 }
234
235 =head2 C4::Labels::Batch->delete(batch_id => batch_id) |  $batch->delete()
236
237     Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
238     and 1 upon failure. Errors are logged to the syslog.
239
240     examples:
241         my $exitstat = $batch->delete(); # to delete the record behind the $batch object
242         my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
243
244 =cut
245
246 sub delete {
247     my $self = shift;
248     my %opts = @_;
249     if ((ref $self) && !$self->{'batch_id'}) {   # If there is no batch batch_id then we cannot delete it from the db
250         syslog("LOG_ERR", "Cannot delete batch: Batch has not been saved.");
251         return 1;
252     }
253     elsif (!$opts{batch_id}) {
254         syslog("LOG_ERR", "Cannot delete batch: Missing batch_id.");
255         return 1;
256     }
257     my $query = "DELETE FROM labels_batches WHERE batch_id = ?";
258     my $sth = C4::Context->dbh->prepare($query);
259     $sth->execute($self->{'batch_id'});
260     return 0;
261 }
262
263
264 1;
265 __END__
266
267 =head1 AUTHOR
268
269 Chris Nighswonger <cnighswonger AT foundations DOT edu>
270
271 =cut
272