+=head2 C4::Labels::Lib::get_batch_summary()
+
+ This function returns an arrayref whose elements are hashes containing the batch_ids of current batches along with the item count
+ for each batch upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
+ One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+
+ NOTE: Do not pass in the keyword 'WHERE.'
+
+ examples:
+
+ my $batches = get_batch_summary();
+ my $batches = get_batch_summary(filter => filter_string);
+
+=cut
+
+sub get_batch_summary {
+ my %params = @_;
+ my @batches = ();
+ my $query = "SELECT DISTINCT batch_id FROM labels_batches";
+ $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+ my $sth = C4::Context->dbh->prepare($query);
+# $sth->{'TraceLevel'} = 3;
+ $sth->execute();
+ if ($sth->err) {
+ warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
+ return -1;
+ }
+ ADD_BATCHES:
+ while (my $batch = $sth->fetchrow_hashref) {
+ my $query = "SELECT count(item_number) FROM labels_batches WHERE batch_id=?;";
+ my $sth1 = C4::Context->dbh->prepare($query);
+ $sth1->execute($batch->{'batch_id'});
+ if ($sth1->err) {
+ warn sprintf('Database returned the following error on attempted SELECT count: %s', $sth1->errstr);
+ return -1;
+ }
+ my $count = $sth1->fetchrow_arrayref;
+ $batch->{'_item_count'} = @$count[0];
+ push(@batches, $batch);
+ }
+ return \@batches;
+}
+
+=head2 C4::Labels::Lib::get_label_summary()
+
+ This function returns an arrayref whose elements are hashes containing the label_ids of current labels along with the item count
+ for each label upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
+ One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+
+ NOTE: Do not pass in the keyword 'WHERE.'
+
+ examples:
+
+ my $labels = get_label_summary();
+ my $labels = get_label_summary(items => @item_list);
+
+=cut
+
+sub get_label_summary {
+ my %params = @_;
+ my $label_number = 0;
+ my @label_summaries = ();
+ my $query = "SELECT b.title, b.author, bi.itemtype, i.barcode, i.biblionumber FROM biblio AS b, biblioitems AS bi ,items AS i, labels_batches AS l WHERE itemnumber=? AND l.item_number=i.itemnumber AND i.biblioitemnumber=bi.biblioitemnumber AND bi.biblionumber=b.biblionumber AND l.batch_id=?;";
+ my $sth = C4::Context->dbh->prepare($query);
+ foreach my $item (@{$params{'items'}}) {
+ $label_number++;
+ $sth->execute($item->{'item_number'}, $params{'batch_id'});
+ if ($sth->err) {
+ warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
+ return -1;
+ }
+ my $record = $sth->fetchrow_hashref;
+ my $label_summary->{'_label_number'} = $label_number;
+ $record->{'author'} =~ s/[^\.|\w]$// if $record->{'author'}; # strip off ugly trailing chars... but not periods or word chars
+ $record->{'title'} =~ s/\W*$//; # strip off ugly trailing chars
+ # FIXME contructing staff interface URLs should be done *much* higher up the stack - for the most part, C4 module code
+ # should not know that it's part of a web app
+ $record->{'title'} = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $record->{'biblionumber'} . '"> ' . $record->{'title'} . '</a>';
+ $label_summary->{'_summary'} = $record->{'title'} . " | " . ($record->{'author'} ? $record->{'author'} : 'N/A');
+ $label_summary->{'_item_type'} = $record->{'itemtype'};
+ $label_summary->{'_barcode'} = $record->{'barcode'};
+ $label_summary->{'_item_number'} = $item->{'item_number'};
+ $label_summary->{'_label_id'} = $item->{'label_id'};
+ push (@label_summaries, $label_summary);
+ }
+ return \@label_summaries;
+}
+