1 package C4::Labels::Batch;
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
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
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.
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
23 use Sys::Syslog qw(syslog);
30 use version; our $VERSION = qv('1.0.0_1');
34 my $given_params = {};
36 my @valid_template_params = (
44 foreach my $key (keys %{$given_params}) {
45 if (!(grep m/$key/, @valid_template_params)) {
46 syslog("LOG_ERR", "C4::Labels::Batch : Unrecognized parameter type of \"%s\".", $key);
52 if (!(grep m/$_/, @valid_template_params)) {
53 syslog("LOG_ERR", "C4::Labels::Batch : Unrecognized parameter type of \"%s\".", $_);
62 C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
68 =head2 C4::Labels::Batch->new(layout_id => layout_id, template_id => template_id, profile_id => profile_id)
70 Invoking the I<new> method constructs a new batch object with no items.
73 my $batch = C4::Labels::Batch->new(layout_id => layout_id, template_id => template_id, profile_id => profile_id);
74 # Creates and returns a new batch object
76 B<NOTE:> This batch is I<not> written to the database untill $batch->save() is invoked. You have been warned!
81 my ($invocant, %params) = @_;
82 my $type = ref($invocant) || $invocant;
87 batch_stat => 0, # False if any data has changed and the db has not been updated
93 =head2 $batch->add_item($item_number)
95 Invoking the I<add_item> method will add the supplied item to the batch object.
98 $batch->add_item($item_number);
104 my $item_num = shift;
105 push (@{$self->{'items'}}, $item_num);
106 $self->{'batch_stat'} = 0;
109 =head2 $batch->get_attr()
111 Invoking the I<get_attr> method will return the requested attribute.
114 my @items = $batch->get_attr($attr);
120 return $self->{$_[0]};
123 =head2 $batch->delete_item()
125 Invoking the I<delete_item> method will delete the supplied item from the batch object.
128 $batch->delete_item();
134 my $item_num = shift;
136 ++$index until $$self->{'items'}[$index] == $item_num or $index > $#$self->{'items'};
137 if ($index > $#$self->{'items'}) {
138 syslog("LOG_ERR", "C4::Labels::Batch->delete_item : Item %s does not exist in batch %s.", $item_num, $self->{'batch_id'});
141 delete ($$self->{'items'}[$index]);
142 $self->{'batch_stat'} = 0;
145 =head2 $batch->save()
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.
153 my $exitstat = $batch->save(); # to save the record behind the $batch object
159 if ($self->{'batch_id'} > 0) {
160 foreach my $item_number (@$self->{'items'}) {
161 my $query = "UPDATE labels_batches SET item_number=?, branch_code=? 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->{'branch_code'}, $self->{'batch_id'});
166 syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted UPDATE: %s", $sth->errstr);
172 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
174 my $batch_id = $sth1->fetchrow_array;
175 $self->{'batch_id'} = $batch_id++;
176 foreach my $item_number (@$self->{'items'}) {
177 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
178 warn "DEBUG: Inserting: $query\n" if $debug;
179 my $sth->C4::Context->dbh->prepare($query);
180 $sth->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
182 syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth->errstr);
185 return $self->{'batch_id'};
188 $self->{'batch_stat'} = 1;
191 =head2 C4::Labels::Template->retrieve(template_id)
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.
199 my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
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
204 my $template = C4::Labels::Template->retrieve(template_id => 1, profile_id => profile_id); # Retrieves template record 1, converts the units
205 to points, applies the given profile id, and returns an object containing the record
210 my $invocant = shift;
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'});
217 syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr);
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->{'branch_code'} = $record->{'branch_code'};
226 push (@{$self->{'items'}}, $record->{'item_number'});
228 $self->{'batch_stat'} = 1;
229 bless ($self, $type);
233 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
235 Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
236 and 1 upon failure. Errors are logged to the syslog.
239 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
240 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
248 my $query_param = '';
250 $self = shift; # check to see if this is a method call
251 $call_type = 'C4::Labels::Batch->delete';
252 $query_param = $self->{'batch_id'};
256 $call_type = 'C4::Labels::Batch::delete';
257 $query_param = $opts{'batch_id'};
259 if ($query_param eq '') { # If there is no template id then we cannot delete it
260 syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type);
263 my $query = "DELETE FROM labels_batches WHERE batch_id = ?";
264 my $sth = C4::Context->dbh->prepare($query);
265 # $sth->{'TraceLevel'} = 3;
266 $sth->execute($query_param);
268 syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr);
280 Chris Nighswonger <cnighswonger AT foundations DOT edu>