added intranetbookbag syspref to install scripts
[koha.git] / C4 / Reports / Guided.pm
index 669b3eb..d8277b7 100644 (file)
@@ -24,10 +24,12 @@ use Carp;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 use C4::Context;
+use C4::Dates qw/format_date/;
 use C4::Output;
 use C4::Dates;
 use XML::Simple;
 use XML::Dumper;
+use Switch;
 use C4::Debug;
 # use Smart::Comments;
 # use Data::Dumper;
@@ -39,10 +41,10 @@ BEGIN {
        @ISA = qw(Exporter);
        @EXPORT = qw(
                get_report_types get_report_areas get_columns build_query get_criteria
-               save_report get_saved_reports execute_query get_saved_report create_compound run_compound
+           save_report get_saved_reports execute_query get_saved_report create_compound run_compound
                get_column_type get_distinct_values save_dictionary get_from_dictionary
                delete_definition delete_report format_results get_sql
-        select_2_select_count_value
+        select_2_select_count_value update_sql
        );
 }
 
@@ -73,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';
@@ -242,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++){
@@ -304,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 );
 }
@@ -437,13 +483,26 @@ Given some sql and a name this will saved it so that it can resued
 =cut
 
 sub save_report {
-    my ( $sql, $name, $type, $notes ) = @_;
+    my ( $borrowernumber, $sql, $name, $type, $notes ) = @_;
     my $dbh = C4::Context->dbh();
     $sql =~ s/(\s*\;\s*)$//; # removes trailing whitespace and /;/
     my $query =
 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes)  VALUES (?,now(),now(),?,?,?,?)";
     my $sth = $dbh->prepare($query);
-    $sth->execute( 0, $sql, $name, $type, $notes );
+    $sth->execute( $borrowernumber, $sql, $name, $type, $notes );
+}
+
+sub update_sql {
+    my $id = shift || croak "No Id given";
+    my $sql = shift;
+    my $reportname = shift;
+    my $notes = shift;
+    my $dbh = C4::Context->dbh();
+    $sql =~ s/(\s*\;\s*)$//; # removes trailing whitespace and /;/
+    my $query = "UPDATE saved_sql SET savedsql = ?, last_modified = now(), report_name = ?, notes = ? WHERE id = ? ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $sql, $reportname, $notes, $id );
+    $sth->finish();
 }
 
 sub store_results {
@@ -504,7 +563,16 @@ sub get_saved_reports {
     ORDER by date_created";
     my $sth   = $dbh->prepare($query);
     $sth->execute();
-    return $sth->fetchall_arrayref({});
+    
+    my $result = $sth->fetchall_arrayref({});
+    foreach (@$result){
+        $_->{date_created} = format_date($_->{date_created}); 
+        
+        my $member = C4::Members::GetMember(borrowernumber=>$_->{borrowernumber});
+        $_->{borrowerfirstname} = $member->{firstname};
+        $_->{borrowersurname}   = $member->{surname};
+    }
+    return $result;
 }
 
 sub get_saved_report {