Bug 10005: Inventory - Add an option to not check in items
[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;
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Dates;
29 use C4::Output;
30 use C4::Log;
31 use C4::Items;
32 use C4::Branch;
33 use C4::Debug;
34 use C4::Search;    # enabled_staff_search_views
35
36 use vars qw($debug $cgi_debug);
37
38 =head1 viewlog.pl
39
40 plugin that shows stats
41
42 =cut
43
44 my $input = new CGI;
45
46 $debug or $debug = $cgi_debug;
47 my $do_it    = $input->param('do_it');
48 my @modules  = $input->param("modules");
49 my $user     = $input->param("user") // '';
50 my @actions  = $input->param("actions");
51 my $object   = $input->param("object");
52 my $info     = $input->param("info");
53 my $datefrom = $input->param("from");
54 my $dateto   = $input->param("to");
55 my $basename = $input->param("basename");
56 my $output   = $input->param("output") || "screen";
57 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
58
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/viewlog.tt",
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'view_system_logs' },
66         debug           => 1,
67     }
68 );
69
70 if ( $src eq 'circ' ) {
71
72     # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
73     use C4::Members;
74     use C4::Members::Attributes qw(GetBorrowerAttributes);
75     my $borrowernumber = $object;
76     my $data = GetMember( 'borrowernumber' => $borrowernumber );
77     my ( $picture, $dberror ) = GetPatronImage( $data->{'borrowernumber'} );
78     $template->param( picture => 1 ) if $picture;
79
80     if ( C4::Context->preference('ExtendedPatronAttributes') ) {
81         my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
82         $template->param(
83             ExtendedPatronAttributes => 1,
84             extendedattributes       => $attributes
85         );
86     }
87
88     my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
89     $template->param(%$data);
90
91     $template->param(
92         menu           => 1,
93         borrowernumber => $borrowernumber,
94         categoryname   => $data->{'description'},
95         roadtype       => $roadtype,
96         branchname     => GetBranchName( $data->{'branchcode'} ),
97         RoutingSerials => C4::Context->preference('RoutingSerials'),
98     );
99 }
100
101 $template->param(
102     debug => $debug,
103     C4::Search::enabled_staff_search_views,
104     object => $object,
105 );
106
107 if ($do_it) {
108
109     my @data;
110     my ( $results, $modules, $actions );
111     if ( defined $actions[0] && $actions[0] ne '' ) { $actions  = \@actions; }     # match All means no limit
112     if ( $modules[0] ne '' ) { $modules = \@modules; }    # match All means no limit
113     $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
114     @data = @$results;
115     foreach my $result (@data) {
116
117         # Init additional columns for CSV export
118         $result->{'biblionumber'}      = q{};
119         $result->{'biblioitemnumber'}  = q{};
120         $result->{'barcode'}           = q{};
121         $result->{'userfirstname'}     = q{};
122         $result->{'usersurname'}       = q{};
123         $result->{'borrowerfirstname'} = q{};
124         $result->{'borrowersurname'}   = q{};
125
126         if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
127
128             # get item information so we can create a working link
129             my $itemnumber = $result->{'object'};
130             $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
131             my $item = GetItem($itemnumber);
132             if ($item) {
133                 $result->{'biblionumber'}     = $item->{'biblionumber'};
134                 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
135                 $result->{'barcode'}          = $item->{'barcode'};
136             }
137         }
138
139         #always add firstname and surname for librarian/user
140         if ( $result->{'user'} ) {
141             my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
142             if ($userdetails) {
143                 $result->{'userfirstname'} = $userdetails->{'firstname'};
144                 $result->{'usersurname'}   = $userdetails->{'surname'};
145             }
146         }
147
148         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
149         if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
150             if ( $result->{'object'} ) {
151                 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
152                 if ($borrowerdetails) {
153                     $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
154                     $result->{'borrowersurname'}   = $borrowerdetails->{'surname'};
155                 }
156             }
157         }
158     }
159
160     if ( $output eq "screen" ) {
161
162         # Printing results to screen
163         $template->param(
164             logview  => 1,
165             total    => scalar @data,
166             looprow  => \@data,
167             do_it    => 1,
168             datefrom => $datefrom,
169             dateto   => $dateto,
170             user     => $user,
171             info     => $info,
172             src      => $src,
173             modules  => \@modules,
174             actions  => \@actions,
175         );
176
177         # Used modules
178         foreach my $module (@modules) {
179             $template->param( $module => 1 );
180         }
181
182         output_html_with_http_headers $input, $cookie, $template->output;
183     }
184     else {
185
186         # Printing to a csv file
187         my $content = q{};
188         my $delimiter = C4::Context->preference('delimiter') || ',';
189         if (@data) {
190             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
191             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
192
193             # First line with heading
194             # Exporting bd id seems useless
195             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
196             if ( $csv->combine(@headings) ) {
197                 $content .= $csv->string() . "\n";
198             }
199
200             # Lines of logs
201             foreach my $line (@data) {
202                 my @cells = map { $line->{$_} } @headings;
203                 if ( $csv->combine(@cells) ) {
204                     $content .= $csv->string() . "\n";
205                 }
206             }
207         }
208
209         # Output
210         print $input->header(
211             -type       => 'text/csv',
212             -attachment => $basename . '.csv',
213         );
214         print $content;
215     }
216     exit;
217 }
218 else {
219     output_html_with_http_headers $input, $cookie, $template->output;
220 }