MT 2285 : enhancement for patronSearch
[koha.git] / C4 / Reports / Guided.pm
index 3c655c4..d8277b7 100644 (file)
@@ -29,6 +29,7 @@ use C4::Output;
 use C4::Dates;
 use XML::Simple;
 use XML::Dumper;
+use Switch;
 use C4::Debug;
 # use Smart::Comments;
 # use Data::Dumper;
@@ -74,18 +75,29 @@ $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
 # have to do someting here to know if its dropdown, free text, date etc
 
 our %criteria;
+# reports on circulation
 $criteria{'1'} = [
     'statistics.type',   'borrowers.categorycode',
     'statistics.branch',
     'biblioitems.publicationyear|date',
     'items.dateaccessioned|date'
 ];
+# reports on catalogue
 $criteria{'2'} =
-  [ 'items.holdingbranch', 'items.homebranch' ,'items.itemlost', 'items.location', 'items.ccode'];
-$criteria{'3'} = ['borrowers.branchcode'];
+  [ 'items.itemnumber|textrange',   'items.biblionumber|textrange',   'items.barcode|textrange', 
+    'biblio.frameworkcode',         'items.holdingbranch',            'items.homebranch', 
+  'biblio.datecreated|daterange',   'biblio.timestamp|daterange',     'items.onloan|daterange', 
+  'items.ccode',                    'items.itemcallnumber|textrange', 'items.itype', 
+  'items.itemlost',                 'items.location' ];
+# reports on borrowers
+$criteria{'3'} = ['borrowers.branchcode', 'borrowers.categorycode'];
+# reports on acquisition
 $criteria{'4'} = ['aqorders.datereceived|date'];
-$criteria{'5'} = ['borrowers.branchcode'];
 
+# reports on accounting
+$criteria{'5'} = ['borrowers.branchcode', 'borrowers.categorycode'];
+
+# Adds itemtypes to criteria, according to the syspref
 if (C4::Context->preference('item-level_itypes')) {
     unshift @{ $criteria{'1'} }, 'items.itype';
     unshift @{ $criteria{'2'} }, 'items.itype';
@@ -243,7 +255,6 @@ sub _build_query {
     my $dbh           = C4::Context->dbh();
     my $joinedtables  = join( ',', @$tables );
     my $joinedcolumns = join( ',', @$columns );
-    my $joinedkeys    = join( ' AND ', @$keys );
     my $query =
       "SELECT $totals $joinedcolumns FROM $tables->[0] ";
        for (my $i=1;$i<@$tables;$i++){
@@ -305,31 +316,65 @@ sub get_criteria {
     foreach my $localcrit (@$crit) {
         my ( $value, $type )   = split( /\|/, $localcrit );
         my ( $table, $column ) = split( /\./, $value );
-        if ( $type eq 'date' ) {
-                       my %temp;
-            $temp{'name'}   = $value;
-            $temp{'date'}   = 1;
-                       $temp{'description'} = $column_defs->{$value};
-            push @criteria_array, \%temp;
-        }
-        else {
-
-            my $query =
-              "SELECT distinct($column) as availablevalues FROM $table";
-            my $sth = $dbh->prepare($query);
-            $sth->execute();
-            my @values;
-            while ( my $row = $sth->fetchrow_hashref() ) {
-                push @values, $row;
-                ### $row;
-            }
-            $sth->finish();
-            my %temp;
-            $temp{'name'}   = $value;
-                       $temp{'description'} = $column_defs->{$value};
-            $temp{'values'} = \@values;
-            push @criteria_array, \%temp;
-        }
+       switch ($type) {
+           case 'textrange' {
+               my %temp;
+               $temp{'name'}        = $value;
+               $temp{'from'}        = "from_" . $value;
+               $temp{'to'}          = "to_" . $value;
+               $temp{'textrange'}   = 1;
+               $temp{'description'} = $column_defs->{$value};
+               push @criteria_array, \%temp;
+           }
+
+           case 'date' {
+               my %temp;
+               $temp{'name'}        = $value;
+               $temp{'date'}        = 1;
+               $temp{'description'} = $column_defs->{$value};
+               push @criteria_array, \%temp;
+           }
+
+           case 'daterange' {
+               my %temp;
+               $temp{'name'}        = $value;
+               $temp{'from'}        = "from_" . $value;
+               $temp{'to'}          = "to_" . $value;
+               $temp{'daterange'}   = 1;
+               $temp{'description'} = $column_defs->{$value};
+               push @criteria_array, \%temp;
+           }
+
+           else {
+               my $query =
+                 "SELECT distinct($column) as availablevalues FROM $table";
+               my $sth = $dbh->prepare($query);
+               $sth->execute();
+               my @values;
+        # push the runtime choosing option
+        my $list;
+        $list='branches' if $column eq 'branchcode' or $column eq 'holdingbranch' or $column eq 'homebranch';
+        $list='categorycode' if $column eq 'categorycode';
+        $list='itemtype' if $column eq 'itype';
+        $list='ccode' if $column eq 'ccode';
+        # TODO : improve to let the librarian choose the description at runtime
+        push @values, { availablevalues => "<<$column".($list?"|$list":'').">>" };
+               while ( my $row = $sth->fetchrow_hashref() ) {
+                   push @values, $row;
+                   if ($row->{'availablevalues'} eq '') { $row->{'default'} = 1 };
+               }
+               $sth->finish();
+
+               my %temp;
+               $temp{'name'}        = $value;
+               $temp{'description'} = $column_defs->{$value};
+               $temp{'values'}      = \@values;
+               
+               push @criteria_array, \%temp;
+     
+           }
+
+       }
     }
     return ( \@criteria_array );
 }