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_number = shift;
105 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
106 my $sth = C4::Context->dbh->prepare($query);
107 # $sth->{'TraceLevel'} = 3;
108 $sth->execute($item_number, $self->{'batch_id'});
110 syslog("LOG_ERR", "C4::Labels::Batch->add_item : Database returned the following error on attempted INSERT: %s", $sth->errstr);
113 push (@{$self->{'items'}}, $item_number);
114 $self->{'batch_stat'} = 0;
118 =head2 $batch->get_attr()
120 Invoking the I<get_attr> method will return the requested attribute.
123 my @items = $batch->get_attr($attr);
129 return $self->{$_[0]};
132 =head2 $batch->remove_item()
134 Invoking the I<remove_item> method will remove the supplied item from the batch object.
137 $batch->remove_item();
143 my $item_number = shift;
144 my $query = "DELETE FROM labels_batches WHERE item_number=? AND batch_id=?;";
145 my $sth = C4::Context->dbh->prepare($query);
146 # $sth->{'TraceLevel'} = 3;
147 $sth->execute($item_number, $self->{'batch_id'});
149 syslog("LOG_ERR", "C4::Labels::Batch->remove_item : Database returned the following error on attempted DELETE: %s", $sth->errstr);
152 @{$self->{'items'}} = grep{$_ != $item_number} @{$self->{'items'}};
153 $self->{'batch_stat'} = 1;
157 =head2 $batch->save()
159 Invoking the I<save> method attempts to insert the batch into the database. The method returns
160 the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
161 batch_id of 1). Errors are logged to the syslog.
164 my $exitstat = $batch->save(); # to save the record behind the $batch object
170 my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
172 my $batch_id = $sth->fetchrow_array;
173 $self->{'batch_id'} = $batch_id++;
174 foreach my $item_number (@{$self->{'items'}}) {
175 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
176 my $sth1 = C4::Context->dbh->prepare($query);
177 $sth1->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
179 syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
182 $self->{'batch_stat'} = 1;
183 return $self->{'batch_id'};
187 =head2 C4::Labels::Batch->retrieve(batch_id)
189 Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns
190 a new object upon success and 1 upon failure. Errors are logged to the syslog.
194 my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch record 1 and returns an object containing the record
199 my $invocant = shift;
201 my $type = ref($invocant) || $invocant;
202 my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";
203 my $sth = C4::Context->dbh->prepare($query);
204 $sth->execute($opts{'batch_id'});
206 syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr);
212 while (my $record = $sth->fetchrow_hashref) {
213 $self->{'batch_id'} = $record->{'batch_id'}; # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way?
214 $self->{'branch_code'} = $record->{'branch_code'};
215 push (@{$self->{'items'}}, $record->{'item_number'});
217 $self->{'batch_stat'} = 1;
218 bless ($self, $type);
222 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
224 Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
225 and 1 upon failure. Errors are logged to the syslog.
228 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
229 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
237 my @query_params = ();
239 $self = shift; # check to see if this is a method call
240 $call_type = 'C4::Labels::Batch->delete';
241 @query_params = ($self->{'batch_id'}, $self->{'branch_code'});
245 $call_type = 'C4::Labels::Batch::delete';
246 @query_params = ($opts{'batch_id'}, $opts{'branch_code'});
248 if ($query_params[0] eq '') { # If there is no template id then we cannot delete it
249 syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type);
252 my $query = "DELETE FROM labels_batches WHERE batch_id = ? AND branch_code =?";
253 my $sth = C4::Context->dbh->prepare($query);
254 # $sth->{'TraceLevel'} = 3;
255 $sth->execute(@query_params);
257 syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr);
269 Chris Nighswonger <cnighswonger AT foundations DOT edu>