Bug 21720: Use Koha::Account->add_debit in AddIssuingCharge
[koha.git] / tools / viewlog.pl
blobf93725fc3f8519a9fdb43a6c23821a41f837e91a
1 #!/usr/bin/perl
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
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::Output;
29 use C4::Log;
30 use C4::Items;
31 use C4::Serials;
32 use C4::Debug;
33 use C4::Search; # enabled_staff_search_views
35 use Koha::Items;
36 use Koha::Patrons;
38 use vars qw($debug $cgi_debug);
40 =head1 viewlog.pl
42 plugin that shows stats
44 =cut
46 my $input = new CGI;
48 $debug or $debug = $cgi_debug;
49 my $do_it = $input->param('do_it');
50 my @modules = $input->multi_param("modules");
51 my $user = $input->param("user") // '';
52 my @actions = $input->multi_param("actions");
53 my @interfaces = $input->multi_param("interfaces");
54 my $object = $input->param("object");
55 my $info = $input->param("info");
56 my $datefrom = $input->param("from");
57 my $dateto = $input->param("to");
58 my $basename = $input->param("basename");
59 my $output = $input->param("output") || "screen";
60 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
62 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
64 template_name => "tools/viewlog.tt",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { tools => 'view_system_logs' },
69 debug => 1,
73 if ( $src eq 'circ' ) {
75 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
76 use C4::Members::Attributes qw(GetBorrowerAttributes);
77 my $borrowernumber = $object;
78 my $patron = Koha::Patrons->find( $borrowernumber );
79 my $circ_info = 1;
80 unless ( $patron ) {
81 $circ_info = 0;
83 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
84 my $attributes = GetBorrowerAttributes( $borrowernumber );
85 $template->param(
86 ExtendedPatronAttributes => 1,
87 extendedattributes => $attributes
91 $template->param(
92 patron => $patron,
93 circulation => $circ_info,
97 $template->param(
98 debug => $debug,
99 C4::Search::enabled_staff_search_views,
100 subscriptionsnumber => CountSubscriptionFromBiblionumber($input->param('object')),
101 object => $object,
104 if ($do_it) {
106 my @data;
107 my ( $results, $modules, $actions, $interfaces );
108 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \@actions; } # match All means no limit
109 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
110 if ( defined $interfaces[0] && $interfaces[0] ne '' ) { $interfaces = \@interfaces; } # match All means no limit
111 $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info, $interfaces );
112 @data = @$results;
113 foreach my $result (@data) {
115 # Init additional columns for CSV export
116 $result->{'biblionumber'} = q{};
117 $result->{'biblioitemnumber'} = q{};
118 $result->{'barcode'} = q{};
120 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
122 # get item information so we can create a working link
123 my $itemnumber = $result->{'object'};
124 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
125 my $item = Koha::Items->find($itemnumber);
126 if ($item) {
127 $result->{'biblionumber'} = $item->biblionumber;
128 $result->{'biblioitemnumber'} = $item->biblionumber;
129 $result->{'barcode'} = $item->barcode;
133 #always add firstname and surname for librarian/user
134 if ( $result->{'user'} ) {
135 my $patron = Koha::Patrons->find( $result->{'user'} );
136 if ($patron) {
137 $result->{librarian} = $patron;
141 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
142 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
143 if ( $result->{'object'} ) {
144 my $patron = Koha::Patrons->find( $result->{'object'} );
145 if ($patron) {
146 $result->{patron} = $patron;
152 if ( $output eq "screen" ) {
154 # Printing results to screen
155 $template->param(
156 logview => 1,
157 total => scalar @data,
158 looprow => \@data,
159 do_it => 1,
160 datefrom => $datefrom,
161 dateto => $dateto,
162 user => $user,
163 info => $info,
164 src => $src,
165 modules => \@modules,
166 actions => \@actions,
167 interfaces => \@interfaces
170 # Used modules
171 foreach my $module (@modules) {
172 $template->param( $module => 1 );
175 output_html_with_http_headers $input, $cookie, $template->output;
177 else {
179 # Printing to a csv file
180 my $content = q{};
181 my $delimiter = C4::Context->preference('delimiter') || ',';
182 if (@data) {
183 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
184 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
186 # First line with heading
187 # Exporting bd id seems useless
188 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
189 if ( $csv->combine(@headings) ) {
190 $content .= $csv->string() . "\n";
193 # Lines of logs
194 foreach my $line (@data) {
195 my @cells = map { $line->{$_} } @headings;
196 if ( $csv->combine(@cells) ) {
197 $content .= $csv->string() . "\n";
202 # Output
203 print $input->header(
204 -type => 'text/csv',
205 -attachment => $basename . '.csv',
207 print $content;
209 exit;
211 else {
212 output_html_with_http_headers $input, $cookie, $template->output;