Bug 32373: Show patron restriction date
[srvgit] / tools / viewlog.pl
index f38ca19..9db4e43 100755 (executable)
 
 use Modern::Perl;
 
-use C4::Auth;
+use C4::Auth qw( get_template_and_user );
 use CGI qw ( -utf8 );
 use Text::CSV::Encoded;
 use C4::Context;
-use C4::Koha;
-use C4::Output;
-use C4::Items;
-use C4::Serials;
-use C4::Debug;
-use C4::Search;    # enabled_staff_search_views
+use C4::Output qw( output_html_with_http_headers );
+use C4::Serials qw( CountSubscriptionFromBiblionumber );
+use C4::Search qw( enabled_staff_search_views );
 
 use Koha::ActionLogs;
 use Koha::Database;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Items;
 use Koha::Patrons;
+use Koha::Recalls;
 
-use vars qw($debug $cgi_debug);
 
 =head1 viewlog.pl
 
@@ -45,15 +42,15 @@ plugin that shows stats
 
 =cut
 
-my $input = new CGI;
+my $input = CGI->new;
 
-$debug or $debug = $cgi_debug;
 my $do_it    = $input->param('do_it');
 my @modules  = $input->multi_param("modules");
 my $user     = $input->param("user") // '';
 my @actions  = $input->multi_param("actions");
 my @interfaces  = $input->multi_param("interfaces");
 my $object   = $input->param("object");
+my $object_type = $input->param("object_type") // '';
 my $info     = $input->param("info");
 my $datefrom = $input->param("from");
 my $dateto   = $input->param("to");
@@ -66,9 +63,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
         template_name   => "tools/viewlog.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { tools => 'view_system_logs' },
-        debug           => 1,
     }
 );
 
@@ -88,9 +83,8 @@ if ( $src eq 'circ' ) {
 }
 
 $template->param(
-    debug => $debug,
     C4::Search::enabled_staff_search_views,
-    subscriptionsnumber => CountSubscriptionFromBiblionumber($input->param('object')),
+    subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
     object => $object,
 );
 
@@ -126,14 +120,27 @@ if ($do_it) {
             '<=' => $dtf->format_datetime( $dateto_endday )
         };
     }
+    # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
+    if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
+
     $search_params{user} = $user if $user;
     $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
     $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
-    $search_params{object} = $object if $object;
-    $search_params{info} = $info if $info;
     $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
 
-    my @logs = Koha::ActionLogs->search(\%search_params);
+    if ( @modules == 1 && $object_type eq 'biblio' ) {
+        # Handle 'Modification log' from cataloguing
+        my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
+        $search_params{'-or'} = [
+            { -and => { object => $object, info => { -like => 'biblio%' }}},
+            { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
+        ];
+    } else {
+        $search_params{info} = { -like => '%' . $info . '%' } if $info;
+        $search_params{object} = $object if $object;
+    }
+
+    my @logs = Koha::ActionLogs->search(\%search_params)->as_list;
 
     my @data;
     foreach my $log (@logs) {
@@ -150,6 +157,7 @@ if ($do_it) {
             $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
             my $item = Koha::Items->find($itemnumber);
             if ($item) {
+                $result->{'object_found'}     = 1;
                 $result->{'biblionumber'}     = $item->biblionumber;
                 $result->{'biblioitemnumber'} = $item->biblionumber;
                 $result->{'barcode'}          = $item->barcode;
@@ -159,7 +167,7 @@ if ($do_it) {
         #always add firstname and surname for librarian/user
         if ( $log->user ) {
             my $patron = Koha::Patrons->find( $log->user );
-            if ($patron) {
+            if ($patron && $output eq 'screen') {
                 $result->{librarian} = $patron;
             }
         }
@@ -168,11 +176,30 @@ if ($do_it) {
         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
             if ( $log->object ) {
                 my $patron = Koha::Patrons->find( $log->object );
-                if ($patron) {
+                if ($patron && $output eq 'screen') {
                     $result->{patron} = $patron;
                 }
             }
         }
+
+        if ( $log->module eq 'NOTICES' ) {
+            if ( $log->object ) {
+                my $notice = Koha::Notice::Templates->find( { id => $log->object } );
+                if ($notice && $output eq 'screen') {
+                    $result->{notice} = $notice->unblessed;
+                }
+            }
+        }
+
+        # get recall information
+        if ( $log->module eq "RECALLS" ) {
+            if ( $log->object ) {
+                my $recall = Koha::Recalls->find( $log->object );
+                if ( $recall && $output eq 'screen' ) {
+                    $result->{recall} = $recall;
+                }
+            }
+        }
         push @data, $result;
     }
     if ( $output eq "screen" ) {
@@ -203,8 +230,8 @@ if ($do_it) {
 
         # Printing to a csv file
         my $content = q{};
-        my $delimiter = C4::Context->preference('delimiter') || ',';
         if (@data) {
+            my $delimiter = C4::Context->csv_delimiter;
             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();