Bug 18044: Label Batches not displaying
[koha-ffzg.git] / C4 / Creators / Lib.pm
index 3f4c617..eb97bc1 100644 (file)
@@ -26,7 +26,6 @@ use C4::Context;
 use C4::Debug;
 
 BEGIN {
-    use version; our $VERSION = qv('3.07.00.049');
     use base qw(Exporter);
     our @EXPORT = qw(get_all_templates
                      get_all_layouts
@@ -143,6 +142,27 @@ my $output_formats = [
     {type       => 'csv',       desc    => 'CSV File'},
 ];
 
+sub _build_query {
+    my ( $params, $table ) = @_;
+    my @fields = exists $params->{fields} ? @{ $params->{fields} } : ();
+    my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table";
+    my @where_args;
+    if ( exists $params->{filters} ) {
+        $query .= ' WHERE 1 ';
+        while ( my ( $field, $values ) = each %{ $params->{filters} } ) {
+            if ( ref( $values ) ) {
+                $query .= " AND $field IN ( " . ( ('?,') x (@$values-1) ) . "? ) "; # a comma separates elements in a list...
+                push @where_args, @$values;
+            } else {
+                $query .= " AND $field = ? ";
+                push @where_args, $values;
+            }
+        }
+    }
+    $query .= (exists $params->{orderby} ? " ORDER BY $params->{orderby} " : '');
+    return ( $query, @where_args );
+}
+
 =head2 C4::Creators::Lib::get_all_templates()
 
   my $templates = get_all_templates();
@@ -152,13 +172,11 @@ This function returns a reference to a hash containing all templates upon succes
 =cut
 
 sub get_all_templates {
-    my %params = @_;
+    my ( $params ) = @_;
     my @templates = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_templates";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
-    $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
+    my ( $query, @where_args ) = _build_query( $params, 'creator_templates' );
     my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -179,13 +197,11 @@ This function returns a reference to a hash containing all layouts upon success
 =cut
 
 sub get_all_layouts {
-    my %params = @_;
+    my ( $params ) = @_;
     my @layouts = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_layouts";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
-    $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
+    my ( $query, @where_args ) = _build_query( $params, 'creator_layouts' );
     my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -201,7 +217,7 @@ sub get_all_layouts {
 
   my $profiles = get_all_profiles();
 
-  my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
+  my $profiles = get_all_profiles({ fields => [@fields], filters => { filters => [$value1, $value2] } });
 
 This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
 to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
@@ -212,13 +228,11 @@ NOTE: Do not pass in the keyword 'WHERE.'
 =cut
 
 sub get_all_profiles {
-    my %params = @_;
+    my ( $params ) = @_;
     my @profiles = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+    my ( $query, @where_args ) = _build_query( $params, 'printers_profile' );
     my $sth = C4::Context->dbh->prepare($query);
-#    $sth->{'TraceLevel'} = 3 if $debug;
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -263,14 +277,13 @@ NOTE: Do not pass in the keyword 'WHERE.'
 =cut
 
 sub get_batch_summary {
-    my %params = @_;
+    my ( $params ) = @_;
     my @batches = ();
-    my $query = "SELECT batch_id,count(batch_id) as _item_count FROM creator_batches WHERE creator=?";
-    $query .= ($params{'filter'} ? " AND $params{'filter'}" : '');
+    $params->{fields} = ['batch_id', 'count(batch_id) as _item_count'];
+    my ( $query, @where_args ) = _build_query( $params, 'creator_batches' );
     $query .= " GROUP BY batch_id";
     my $sth = C4::Context->dbh->prepare($query);
-#    $sth->{'TraceLevel'} = 3;
-    $sth->execute($params{'creator'});
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
         return -1;
@@ -299,7 +312,7 @@ 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, i.itype
+    my $query = "     SELECT b.title, b.author, bi.itemtype, i.barcode, i.itemcallnumber, i.biblionumber, i.itype
                       FROM creator_batches AS c LEFT JOIN items AS i ON (c.item_number=i.itemnumber)
                       LEFT JOIN biblioitems AS bi ON (i.biblioitemnumber=bi.biblioitemnumber)
                       LEFT JOIN biblio AS b ON (bi.biblionumber=b.biblionumber)
@@ -325,6 +338,7 @@ sub get_label_summary {
         $label_summary->{'_item_type'} = C4::Context->preference("item-level_itypes") ? $record->{'itype'} : $record->{'itemtype'};
         $label_summary->{'_barcode'} = $record->{'barcode'};
         $label_summary->{'_item_number'} = $item->{'item_number'};
+        $label_summary->{'_item_cn'} = $record->{'itemcallnumber'};
         $label_summary->{'_label_id'} = $item->{'label_id'};
         push (@label_summaries, $label_summary);
     }