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()
70 Invoking the I<new> method constructs a new batch object with no items. It is possible to pre-populate the batch with items and a branch code by passing them
71 as in the second example below.
73 B<NOTE:> The items list must be an arrayref pointing to an array of hashes containing a key/data pair after this fashion: {item_number => item_number}. The order of
74 the array elements determines the order of the items in the batch.
77 C<my $batch = C4::Labels::Batch->new(); # Creates and returns a new batch object>
79 C<my $batch = C4::Labels::Batch->new(items => $arrayref, branch_code => branch_code) # Creates and returns a new batch object containing the items passed in
80 with the branch code passed in.>
82 B<NOTE:> This batch is I<not> written to the database until C<$batch->save()> is invoked. You have been warned!
87 my ($invocant) = shift;
88 my $type = ref($invocant) || $invocant;
93 batch_stat => 0, # False if any data has changed and the db has not been updated
96 my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
98 my $batch_id = $sth->fetchrow_array;
99 $self->{'batch_id'} = ++$batch_id;
100 bless ($self, $type);
104 =head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
106 Invoking the I<add_item> method will add the supplied item to the batch object.
109 $batch->add_item(item_number => $item_number, branch_code => $branch_code);
115 my $item_number = shift;
116 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
117 my $sth = C4::Context->dbh->prepare($query);
118 # $sth->{'TraceLevel'} = 3;
119 $sth->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
121 syslog("LOG_ERR", "C4::Labels::Batch->add_item : Database returned the following error on attempted INSERT: %s", $sth->errstr);
124 $query = "SELECT max(label_id) FROM labels_batches WHERE batch_id=? AND item_number=? AND branch_code=?;";
125 my $sth1 = C4::Context->dbh->prepare($query);
126 $sth1->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
127 my $label_id = $sth1->fetchrow_array;
128 push (@{$self->{'items'}}, {item_number => $item_number, label_id => $label_id});
129 $self->{'batch_stat'} = 0;
133 =head2 $batch->get_attr()
135 Invoking the I<get_attr> method will return the requested attribute.
138 my @items = $batch->get_attr($attr);
144 return $self->{$_[0]};
147 =head2 $batch->remove_item()
149 Invoking the I<remove_item> method will remove the supplied item from the batch object.
152 $batch->remove_item();
158 my $label_id = shift;
159 my $query = "DELETE FROM labels_batches WHERE label_id=? AND batch_id=?;";
160 my $sth = C4::Context->dbh->prepare($query);
161 # $sth->{'TraceLevel'} = 3;
162 $sth->execute($label_id, $self->{'batch_id'});
164 syslog("LOG_ERR", "C4::Labels::Batch->remove_item : Database returned the following error on attempted DELETE: %s", $sth->errstr);
167 @{$self->{'items'}} = grep{$_->{'label_id'} != $label_id} @{$self->{'items'}};
168 $self->{'batch_stat'} = 1;
172 =head2 $batch->save()
174 Invoking the I<save> method attempts to insert the batch into the database. The method returns
175 the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
176 batch_id of 1). Errors are logged to the syslog.
179 my $exitstat = $batch->save(); # to save the record behind the $batch object
185 foreach my $item_number (@{$self->{'items'}}) {
186 my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
187 my $sth1 = C4::Context->dbh->prepare($query);
188 $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
190 syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
193 $self->{'batch_stat'} = 1;
194 return $self->{'batch_id'};
198 =head2 C4::Labels::Batch->retrieve(batch_id)
200 Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns
201 a new object upon success and 1 upon failure. Errors are logged to the syslog.
205 my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch record 1 and returns an object containing the record
210 my $invocant = shift;
212 my $type = ref($invocant) || $invocant;
214 my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";
215 my $sth = C4::Context->dbh->prepare($query);
216 # $sth->{'TraceLevel'} = 3;
217 $sth->execute($opts{'batch_id'});
219 batch_id => $opts{'batch_id'},
222 while (my $record = $sth->fetchrow_hashref) {
223 $self->{'branch_code'} = $record->{'branch_code'};
224 push (@{$self->{'items'}}, {item_number => $record->{'item_number'}, label_id => $record->{'label_id'}});
225 $record_flag = 1; # true if one or more rows were retrieved
227 return -2 if $record_flag == 0; # a hackish sort of way of indicating no such record exists
229 syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr);
232 $self->{'batch_stat'} = 1;
233 bless ($self, $type);
237 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
239 Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
240 and 1 upon failure. Errors are logged to the syslog.
243 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
244 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
252 my @query_params = ();
254 $self = shift; # check to see if this is a method call
255 $call_type = 'C4::Labels::Batch->delete';
256 @query_params = ($self->{'batch_id'}, $self->{'branch_code'});
260 $call_type = 'C4::Labels::Batch::delete';
261 @query_params = ($opts{'batch_id'}, $opts{'branch_code'});
263 if ($query_params[0] eq '') { # If there is no template id then we cannot delete it
264 syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type);
267 my $query = "DELETE FROM labels_batches WHERE batch_id = ? AND branch_code =?";
268 my $sth = C4::Context->dbh->prepare($query);
269 # $sth->{'TraceLevel'} = 3;
270 $sth->execute(@query_params);
272 syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr);
278 =head2 C4::Labels::Batch->remove_duplicates(batch_id => batch_id) | $batch->remove_duplicates()
280 Invoking the remove_duplicates method attempts to remove duplicates the batch from the database. The method returns the count of duplicate
281 records removed upon success and -1 upon failure. Errors are logged to the syslog.
284 my $remove_count = $batch->remove_duplicates(); # to remove duplicates the record behind the $batch object
288 sub remove_duplicates {
291 my $query = "DELETE FROM labels_batches WHERE label_id = ?;"; # ORDER BY timestamp ASC LIMIT ?;";
292 my $sth = C4::Context->dbh->prepare($query);
293 my @duplicate_items = grep{$seen{$_->{'item_number'}}++} @{$self->{'items'}};
294 foreach my $item (@duplicate_items) {
295 $sth->execute($item->{'label_id'});
297 syslog("LOG_ERR", "C4::Labels::Batch->remove_duplicates() : Database returned the following error on attempted DELETE for label_id %s: %s", $item->{'label_id'}, $sth->errstr);
300 $sth->finish(); # Per DBI.pm docs: "If execute() is called on a statement handle that's still active ($sth->{Active} is true) then it should effectively call finish() to tidy up the previous execution results before starting this new execution."
301 @{$self->{'items'}} = grep{$_->{'label_id'} != $item->{'label_id'}} @{$self->{'items'}}; # the correct label/item must be removed from the current batch object as well; this should be done *after* each sql DELETE in case the DELETE fails
303 return scalar(@duplicate_items);
311 Chris Nighswonger <cnighswonger AT foundations DOT edu>