1 package C4::Creators::Batch;
6 use autouse 'Data::Dumper' => qw(Dumper);
11 my $given_params = {};
13 my @valid_template_params = (
24 foreach my $key (keys %{$given_params}) {
25 if (!(grep m/$key/, @valid_template_params)) {
26 warn sprintf('Unrecognized parameter type of "%s".', $key);
32 if (!(grep m/$_/, @valid_template_params)) {
33 warn sprintf('Unrecognized parameter type of %s', $_);
41 my ($invocant) = shift;
42 my $type = ref($invocant) || $invocant;
48 batch_stat => 0, # False if any data has changed and the db has not been updated
58 ref($self) =~ m/C4::(.+)::.+$/;
59 my $number_type = ($1 eq 'Patroncards' ? 'borrower_number' : 'item_number');
60 if ($self->{'batch_id'} == 0){ #if this is a new batch batch_id must be created
61 my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM creator_batches;");
63 my $batch_id = $sth->fetchrow_array;
64 $self->{'batch_id'}= ++$batch_id;
66 my $query = "INSERT INTO creator_batches (batch_id, description, $number_type, branch_code, creator) VALUES (?,?,?,?,?);";
67 my $sth = C4::Context->dbh->prepare($query);
68 # $sth->{'TraceLevel'} = 3;
69 $sth->execute($self->{'batch_id'}, $self->{'description'}, $number, $self->{'branch_code'}, $1);
71 warn sprintf('Database returned the following error on attempted INSERT: %s', $sth->errstr);
74 $query = "SELECT max(label_id) FROM creator_batches WHERE batch_id=? AND $number_type=? AND branch_code=?;";
75 my $sth1 = C4::Context->dbh->prepare($query);
76 $sth1->execute($self->{'batch_id'}, $number, $self->{'branch_code'});
77 my $label_id = $sth1->fetchrow_array;
78 push (@{$self->{'items'}}, {$number_type => $number, label_id => $label_id});
79 $self->{'batch_stat'} = 1;
85 return $self->{$_[0]};
91 my $query = "DELETE FROM creator_batches WHERE label_id=? AND batch_id=?;";
92 my $sth = C4::Context->dbh->prepare($query);
93 # $sth->{'TraceLevel'} = 3;
94 $sth->execute($label_id, $self->{'batch_id'});
96 warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
99 @{$self->{'items'}} = grep{$_->{'label_id'} != $label_id} @{$self->{'items'}};
100 $self->{'batch_stat'} = 1;
104 # FIXME: This method is effectively useless the way the current add_item method is written. Ideally, the items should be added to the object
105 # and then the save method called. This does not work well in practice due to the inability to pass objects across cgi script calls.
106 # I'm leaving it here because it should be here and for consistency's sake and once memcached support is fully implemented this should be as well. -cnighswonger
108 #=head2 $batch->save()
110 # Invoking the I<save> method attempts to insert the batch into the database. The method returns
111 # the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
112 # batch_id of 1). Errors are logged to the Apache log.
115 # my $exitstat = $batch->save(); # to save the record behind the $batch object
121 # foreach my $item_number (@{$self->{'items'}}) {
122 # my $query = "INSERT INTO creator_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
123 # my $sth1 = C4::Context->dbh->prepare($query);
124 # $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
126 # warn sprintf('Database returned the following error on attempted INSERT: %s', $sth1->errstr);
129 # $self->{'batch_stat'} = 1;
130 # return $self->{'batch_id'};
135 my $invocant = shift;
137 my $type = ref($invocant) || $invocant;
138 $type =~ m/C4::(.+)::.+$/;
139 my $number_type = ($1 eq 'Patroncards' ? 'borrower_number' : 'item_number');
141 my $query = "SELECT * FROM creator_batches WHERE batch_id = ? ORDER BY label_id";
142 my $sth = C4::Context->dbh->prepare($query);
143 # $sth->{'TraceLevel'} = 3;
144 $sth->execute($opts{'batch_id'});
146 batch_id => $opts{'batch_id'},
149 while (my $record = $sth->fetchrow_hashref) {
150 $self->{'branch_code'} = $record->{'branch_code'};
151 $self->{'creator'} = $record->{'creator'};
152 $self->{'description'} = $record->{'description'};
153 push (@{$self->{'items'}}, {$number_type => $record->{$number_type}, label_id => $record->{'label_id'}});
154 $record_flag = 1; # true if one or more rows were retrieved
156 return -2 if $record_flag == 0; # a hackish sort of way of indicating no such record exists
158 warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
161 $self->{'batch_stat'} = 1;
162 bless ($self, $type);
170 my @query_params = ();
172 $self = shift; # check to see if this is a method call
173 $call_type = 'C4::Labels::Batch->delete'; # seems hackish
174 @query_params = ($self->{'batch_id'}, $self->{'branch_code'});
179 $call_type = 'C4::Labels::Batch::delete';
180 @query_params = ($opts{'batch_id'}, $opts{'branch_code'});
182 if ($query_params[0] eq '') { # If there is no template id then we cannot delete it
183 warn sprintf('%s : Cannot delete batch as the batch id is invalid or non-existent.', $call_type);
186 my $query = "DELETE FROM creator_batches WHERE batch_id = ? AND branch_code =?";
187 my $sth = C4::Context->dbh->prepare($query);
188 # $sth->{'TraceLevel'} = 3;
189 $sth->execute(@query_params);
191 warn sprintf('%s : Database returned the following error on attempted INSERT: %s', $call_type, $sth->errstr);
197 sub remove_duplicates {
200 my $query = "DELETE FROM creator_batches WHERE label_id = ?;"; # ORDER BY timestamp ASC LIMIT ?;";
201 my $sth = C4::Context->dbh->prepare($query);
202 my @duplicate_items = grep{
204 ? $seen{$_->{'item_number'}}++
205 : $seen{$_->{'borrower_number'}}++
206 } @{$self->{'items'}};
207 foreach my $item (@duplicate_items) {
208 $sth->execute($item->{'label_id'});
210 warn sprintf('Database returned the following error on attempted DELETE for label_id %s: %s', $item->{'label_id'}, $sth->errstr);
213 $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."
214 @{$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
216 return scalar(@duplicate_items);
224 C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
228 This module provides methods for creating, and otherwise manipulating batch objects used by Koha to create and export labels.
234 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
235 as in the second example below.
237 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
238 the array elements determines the order of the items in the batch.
241 C<my $batch = C4::Labels::Batch->new(); # Creates and returns a new batch object>
243 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
244 with the branch code passed in.>
246 B<NOTE:> This batch is I<not> written to the database until C<$batch->save()> is invoked. You have been warned!
248 =head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
250 Invoking the I<add_item> method will add the supplied item to the batch object.
253 $batch->add_item(item_number => $item_number, branch_code => $branch_code);
255 =head2 $batch->get_attr($attribute)
257 Invoking the I<get_attr> method will return the requested attribute.
260 my @items = $batch->get_attr('items');
262 =head2 $batch->remove_item($item_number)
264 Invoking the I<remove_item> method will remove the supplied item number from the batch object.
267 $batch->remove_item($item_number);
269 =head2 C4::Labels::Batch->retrieve(batch_id => $batch_id)
271 Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns a new object upon success and 1 upon failure.
272 Errors are logged to the Apache log.
276 my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch 1 and returns an object containing the record
280 Invoking the delete method attempts to delete the template from the database. The method returns -1 upon failure. Errors are logged to the Apache log.
281 NOTE: This method may also be called as a function and passed a key/value pair simply deleteing that batch from the database. See the example below.
284 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
285 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch 1
287 =head2 remove_duplicates()
289 Invoking the remove_duplicates method attempts to remove duplicate items in the batch from the database. The method returns the count of duplicate records removed upon
290 success and -1 upon failure. Errors are logged to the Apache log.
291 NOTE: This method may also be called as a function and passed a key/value pair removing duplicates in the batch passed in. See the example below.
294 my $remove_count = $batch->remove_duplicates(); # to remove duplicates the record behind the $batch object
295 my $remove_count = C4::Labels::Batch->remove_duplicates(batch_id => 1); # to remove duplicates in batch 1
299 Chris Nighswonger <cnighswonger AT foundations DOT edu>
303 Copyright 2009 Foundations Bible College.
307 This file is part of Koha.
309 Koha is free software; you can redistribute it and/or modify it
310 under the terms of the GNU General Public License as published by
311 the Free Software Foundation; either version 3 of the License, or
312 (at your option) any later version.
314 Koha is distributed in the hope that it will be useful, but
315 WITHOUT ANY WARRANTY; without even the implied warranty of
316 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
317 GNU General Public License for more details.
319 You should have received a copy of the GNU General Public License
320 along with Koha; if not, see <http://www.gnu.org/licenses>.
322 =head1 DISCLAIMER OF WARRANTY
324 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
325 A PARTICULAR PURPOSE. See the GNU General Public License for more details.