Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use C4::Auth qw( get_template_and_user );
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Serials qw( CountSubscriptionFromBiblionumber );
29 use C4::Search qw( enabled_staff_search_views );
30
31 use Koha::ActionLogs;
32 use Koha::Database;
33 use Koha::DateUtils qw( dt_from_string );
34 use Koha::Items;
35 use Koha::Patrons;
36
37
38 =head1 viewlog.pl
39
40 plugin that shows stats
41
42 =cut
43
44 my $input = CGI->new;
45
46 my $do_it    = $input->param('do_it');
47 my @modules  = $input->multi_param("modules");
48 my $user     = $input->param("user") // '';
49 my @actions  = $input->multi_param("actions");
50 my @interfaces  = $input->multi_param("interfaces");
51 my $object   = $input->param("object");
52 my $object_type = $input->param("object_type") // '';
53 my $info     = $input->param("info");
54 my $datefrom = $input->param("from");
55 my $dateto   = $input->param("to");
56 my $basename = $input->param("basename");
57 my $output   = $input->param("output") || "screen";
58 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
59
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61     {
62         template_name   => "tools/viewlog.tt",
63         query           => $input,
64         type            => "intranet",
65         flagsrequired   => { tools => 'view_system_logs' },
66     }
67 );
68
69 if ( $src eq 'circ' ) {
70
71     my $borrowernumber = $object;
72     my $patron = Koha::Patrons->find( $borrowernumber );
73     my $circ_info = 1;
74     unless ( $patron ) {
75          $circ_info = 0;
76     }
77
78     $template->param(
79         patron      => $patron,
80         circulation => $circ_info,
81     );
82 }
83
84 $template->param(
85     C4::Search::enabled_staff_search_views,
86     subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
87     object => $object,
88 );
89
90 if ($do_it) {
91     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
92     my %search_params;
93
94     if ($datefrom and $dateto ) {
95         my $dateto_endday = dt_from_string($dateto);
96         $dateto_endday->set( # We set last second of day to see all log from that day
97             hour => 23,
98             minute => 59,
99             second => 59
100         );
101         $search_params{'timestamp'} = {
102             -between => [
103                 $dtf->format_datetime( dt_from_string($datefrom) ),
104                 $dtf->format_datetime( $dateto_endday ),
105             ]
106         };
107     } elsif ($datefrom) {
108         $search_params{'timestamp'} = {
109             '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
110         };
111     } elsif ($dateto) {
112         my $dateto_endday = dt_from_string($dateto);
113         $dateto_endday->set( # We set last second of day to see all log from that day
114             hour => 23,
115             minute => 59,
116             second => 59
117         );
118         $search_params{'timestamp'} = {
119             '<=' => $dtf->format_datetime( $dateto_endday )
120         };
121     }
122     # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
123     if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
124
125     $search_params{user} = $user if $user;
126     $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
127     $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
128     $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
129
130     if ( @modules == 1 && $object_type eq 'biblio' ) {
131         # Handle 'Modification log' from cataloguing
132         my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
133         $search_params{'-or'} = [
134             { -and => { object => $object, info => { -like => 'biblio%' }}},
135             { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
136         ];
137     } else {
138         $search_params{info} = { -like => '%' . $info . '%' } if $info;
139         $search_params{object} = $object if $object;
140     }
141
142     my @logs = Koha::ActionLogs->search(\%search_params);
143
144     my @data;
145     foreach my $log (@logs) {
146         my $result = $log->unblessed;
147         # Init additional columns for CSV export
148         $result->{'biblionumber'}      = q{};
149         $result->{'biblioitemnumber'}  = q{};
150         $result->{'barcode'}           = q{};
151
152         if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
153
154             # get item information so we can create a working link
155             my $itemnumber = $log->object;
156             $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
157             my $item = Koha::Items->find($itemnumber);
158             if ($item) {
159                 $result->{'biblionumber'}     = $item->biblionumber;
160                 $result->{'biblioitemnumber'} = $item->biblionumber;
161                 $result->{'barcode'}          = $item->barcode;
162             }
163         }
164
165         #always add firstname and surname for librarian/user
166         if ( $log->user ) {
167             my $patron = Koha::Patrons->find( $log->user );
168             if ($patron && $output eq 'screen') {
169                 $result->{librarian} = $patron;
170             }
171         }
172
173         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
174         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
175             if ( $log->object ) {
176                 my $patron = Koha::Patrons->find( $log->object );
177                 if ($patron && $output eq 'screen') {
178                     $result->{patron} = $patron;
179                 }
180             }
181         }
182
183         if ( $log->module eq 'NOTICES' ) {
184             if ( $log->object ) {
185                 my $notice = Koha::Notice::Templates->find( { id => $log->object } );
186                 if ($notice && $output eq 'screen') {
187                     $result->{notice} = $notice->unblessed;
188                 }
189             }
190         }
191
192         push @data, $result;
193     }
194     if ( $output eq "screen" ) {
195
196         # Printing results to screen
197         $template->param(
198             logview  => 1,
199             total    => scalar @data,
200             looprow  => \@data,
201             do_it    => 1,
202             datefrom => $datefrom,
203             dateto   => $dateto,
204             user     => $user,
205             info     => $info,
206             src      => $src,
207             modules  => \@modules,
208             actions  => \@actions,
209             interfaces => \@interfaces
210         );
211
212         # Used modules
213         foreach my $module (@modules) {
214             $template->param( $module => 1 );
215         }
216         output_html_with_http_headers $input, $cookie, $template->output;
217     }
218     else {
219
220         # Printing to a csv file
221         my $content = q{};
222         my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
223         if (@data) {
224             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
225             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
226
227             # First line with heading
228             # Exporting bd id seems useless
229             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
230             if ( $csv->combine(@headings) ) {
231                 $content .= $csv->string() . "\n";
232             }
233
234             # Lines of logs
235             foreach my $line (@data) {
236                 my @cells = map { $line->{$_} } @headings;
237                 if ( $csv->combine(@cells) ) {
238                     $content .= $csv->string() . "\n";
239                 }
240             }
241         }
242
243         # Output
244         print $input->header(
245             -type       => 'text/csv',
246             -attachment => $basename . '.csv',
247         );
248         print $content;
249     }
250     exit;
251 }
252 else {
253     output_html_with_http_headers $input, $cookie, $template->output;
254 }