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) = shift;
82 my $type = ref($invocant) || $invocant;
87 batch_stat => 0, # False if any data has changed and the db has not been updated
90 my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
92 my $batch_id = $sth->fetchrow_array;
93 $self->{'batch_id'} = ++$batch_id;
98 =head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
100 Invoking the I<add_item> method will add the supplied item to the batch object.
103 $batch->add_item(item_number => $item_number, branch_code => $branch_code);
109 my $item_number = shift;
110 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
111 my $sth = C4::Context->dbh->prepare($query);
112 # $sth->{'TraceLevel'} = 3;
113 $sth->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
115 syslog("LOG_ERR", "C4::Labels::Batch->add_item : Database returned the following error on attempted INSERT: %s", $sth->errstr);
118 $query = "SELECT max(label_id) FROM labels_batches WHERE batch_id=? AND item_number=? AND branch_code=?;";
119 my $sth1 = C4::Context->dbh->prepare($query);
120 $sth1->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
121 my $label_id = $sth1->fetchrow_array;
122 push (@{$self->{'items'}}, {item_number => $item_number, label_id => $label_id});
123 $self->{'batch_stat'} = 0;
127 =head2 $batch->get_attr()
129 Invoking the I<get_attr> method will return the requested attribute.
132 my @items = $batch->get_attr($attr);
138 return $self->{$_[0]};
141 =head2 $batch->remove_item()
143 Invoking the I<remove_item> method will remove the supplied item from the batch object.
146 $batch->remove_item();
152 my $label_id = shift;
153 my $query = "DELETE FROM labels_batches WHERE label_id=? AND batch_id=?;";
154 my $sth = C4::Context->dbh->prepare($query);
155 # $sth->{'TraceLevel'} = 3;
156 $sth->execute($label_id, $self->{'batch_id'});
158 syslog("LOG_ERR", "C4::Labels::Batch->remove_item : Database returned the following error on attempted DELETE: %s", $sth->errstr);
161 @{$self->{'items'}} = grep{$_->{'label_id'} != $label_id} @{$self->{'items'}};
162 $self->{'batch_stat'} = 1;
166 =head2 $batch->save()
168 Invoking the I<save> method attempts to insert the batch into the database. The method returns
169 the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
170 batch_id of 1). Errors are logged to the syslog.
173 my $exitstat = $batch->save(); # to save the record behind the $batch object
179 foreach my $item_number (@{$self->{'items'}}) {
180 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
181 my $sth1 = C4::Context->dbh->prepare($query);
182 $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
184 syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
187 $self->{'batch_stat'} = 1;
188 return $self->{'batch_id'};
192 =head2 C4::Labels::Batch->retrieve(batch_id)
194 Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns
195 a new object upon success and 1 upon failure. Errors are logged to the syslog.
199 my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch record 1 and returns an object containing the record
204 my $invocant = shift;
206 my $type = ref($invocant) || $invocant;
208 my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";
209 my $sth = C4::Context->dbh->prepare($query);
210 # $sth->{'TraceLevel'} = 3;
211 $sth->execute($opts{'batch_id'});
213 batch_id => $opts{'batch_id'},
216 while (my $record = $sth->fetchrow_hashref) {
217 $self->{'branch_code'} = $record->{'branch_code'};
218 push (@{$self->{'items'}}, {item_number => $record->{'item_number'}, label_id => $record->{'label_id'}});
221 return -2 if $record_flag == 0; # a hackish sort of way of indicating no such record exists
223 syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr);
226 $self->{'batch_stat'} = 1;
227 bless ($self, $type);
231 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
233 Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
234 and 1 upon failure. Errors are logged to the syslog.
237 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
238 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
246 my @query_params = ();
248 $self = shift; # check to see if this is a method call
249 $call_type = 'C4::Labels::Batch->delete';
250 @query_params = ($self->{'batch_id'}, $self->{'branch_code'});
254 $call_type = 'C4::Labels::Batch::delete';
255 @query_params = ($opts{'batch_id'}, $opts{'branch_code'});
257 if ($query_params[0] eq '') { # If there is no template id then we cannot delete it
258 syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type);
261 my $query = "DELETE FROM labels_batches WHERE batch_id = ? AND branch_code =?";
262 my $sth = C4::Context->dbh->prepare($query);
263 # $sth->{'TraceLevel'} = 3;
264 $sth->execute(@query_params);
266 syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr);
278 Chris Nighswonger <cnighswonger AT foundations DOT edu>