Add system preference search to administration home page (Bug 3181)
[koha.git] / reports / stats.screen.pl
blobed38459524e4dccb249074fc527fdb731b90d3f8
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
18 use strict;
19 use CGI;
20 use C4::Output;
21 use C4::Auth;
22 use C4::Context;
23 use C4::Stats;
24 use C4::Accounts;
25 use C4::Debug;
26 use Date::Manip;
28 my $input = new CGI;
29 my $time = $input->param('time');
30 my $time2 = $input->param('time2');
31 my $op = $input->param('submit');
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35 template_name => "reports/stats_screen.tmpl",
36 query => $input,
37 type => "intranet",
38 authnotrequired => 1,
39 flagsrequired => { reports => 1 },
40 debug => 1,
44 ( $time = "today" ) if !$time;
45 ( $time2 = "tomorrow" ) if !$time2;
47 my $date = ParseDate($time);
48 my $date2 = ParseDate($time2);
49 $date = UnixDate( $date, '%Y-%m-%d' );
50 $date2 = UnixDate( $date2, '%Y-%m-%d' );
51 $debug and warn "MASON: TIME: $time, $time2";
52 $debug and warn "MASON: DATE: $date, $date2";
54 # get a list of every payment
55 my @payments = TotalPaid( $date, $date2 );
57 my $count = @payments;
59 $debug and warn "MASON: number of payments=$count\n";
61 my $i = 0;
62 my $totalcharges = 0;
63 my $totalcredits = 0;
64 my $totalpaid = 0;
65 my $totalwritten = 0;
66 my @loop1;
67 my @loop2;
69 # lets get a a list of all individual item charges paid for by that payment
71 foreach my $payment (@payments) {
73 my @charges;
74 if ( $payment->{'type'} ne 'writeoff' ) {
76 @charges = getcharges(
77 $payment->{'borrowernumber'},
78 $payment->{'timestamp'},
79 $payment->{'proccode'}
81 $totalcharges++;
82 my $count = @charges;
84 # getting each of the charges and putting them into a array to be printed out
85 #this loops per charge per person
86 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
87 my $hour = substr( $payment->{'timestamp'}, 8, 2 );
88 my $min = substr( $payment->{'timestamp'}, 10, 2 );
89 my $sec = substr( $payment->{'timestamp'}, 12, 2 );
90 my $time = "$hour:$min:$sec";
91 my $time2 = "$payment->{'date'}";
93 # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
94 my $branch = $payment->{'branch'};
96 # lets build up a row
97 my %rows1 = (
98 branch => $branch,
99 datetime => $payment->{'datetime'},
100 surname => $payment->{'surname'},
101 firstname => $payment->{'firstname'},
102 description => $charges[$i2]->{'description'},
103 accounttype => $charges[$i2]->{'accounttype'},
104 amount => sprintf( "%.2f", $charges[$i2]->{'amount'} )
105 , # rounding amounts to 2dp
106 type => $payment->{'type'},
107 value => sprintf( "%.2f", $payment->{'value'} )
108 ); # rounding amounts to 2dp
110 push( @loop1, \%rows1 );
113 $totalpaid = $totalpaid + $payment->{'value'};
114 $debug and warn "totalpaid = $totalpaid";
116 else {
117 ++$totalwritten;
122 #get credits and append to the bottom of payments
123 my @credits = getcredits( $date, $date2 );
125 my $count = @credits;
126 my $i = 0;
128 while ( $i < $count ) {
130 my %rows2 = (
131 creditbranch => $credits[$i]->{'branchcode'},
132 creditdate => $credits[$i]->{'date'},
133 creditsurname => $credits[$i]->{'surname'},
134 creditfirstname => $credits[$i]->{'firstname'},
135 creditdescription => $credits[$i]->{'description'},
136 creditaccounttype => $credits[$i]->{'accounttype'},
137 creditamount => sprintf( "%.2f", $credits[$i]->{'amount'} )
140 push( @loop2, \%rows2 );
141 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
142 $i++; #increment the while loop
145 #takes off first char minus sign "-100.00"
146 $totalcredits = substr( $totalcredits, 1 );
148 my $totalrefunds = 0;
149 my @loop3;
150 my @refunds = getrefunds( $date, $date2 );
151 $count = @refunds;
152 $i = 0;
154 while ( $i < $count ) {
156 my %rows3 = (
157 refundbranch => $refunds[$i]->{'branchcode'},
158 refunddate => $refunds[$i]->{'datetime'},
159 refundsurname => $refunds[$i]->{'surname'},
160 refundfirstname => $refunds[$i]->{'firstname'},
161 refunddescription => $refunds[$i]->{'description'},
162 refundaccounttype => $refunds[$i]->{'accounttype'},
163 refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} )
166 push( @loop3, \%rows3 );
167 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
168 $i++; #increment the while loop
171 my $totalcash = $totalpaid - $totalrefunds;
173 if ( $op eq 'To Excel' ) {
175 my $csv = Text::CSV_XS->new(
177 'quote_char' => '"',
178 'escape_char' => '"',
179 'sep_char' => ',',
180 'binary' => 1
184 print $input->header(
185 -type => 'application/vnd.ms-excel',
186 -attachment => "stats.csv",
188 print
189 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
191 $DB::single = 1;
193 for my $row (@loop1) {
194 my @array = (
195 $row->{'branch'}, $row->{'datetime'},
196 $row->{'surname'}, $row->{'firstname'},
197 $row->{'description'}, $row->{'accounttype'},
198 $row->{'amount'}, $row->{'type'},
199 $row->{'value'}
202 $csv->combine(@array);
203 my $string = $csv->string(@array);
204 print $string, "\n";
206 print ",,,,,,,\n";
207 print
208 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
210 for my $row (@loop2) {
212 my @array = (
213 $row->{'creditbranch'}, $row->{'creditdate'},
214 $row->{'creditsurname'}, $row->{'creditfirstname'},
215 $row->{'creditdescription'}, $row->{'creditaccounttype'},
216 $row->{'creditamount'}
219 $csv->combine(@array);
220 my $string = $csv->string(@array);
221 print $string, "\n";
223 print ",,,,,,,\n";
224 print
225 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
227 for my $row (@loop3) {
228 my @array = (
229 $row->{'refundbranch'}, $row->{'refunddate'},
230 $row->{'refundsurname'}, $row->{'refundfirstname'},
231 $row->{'refunddescription'}, $row->{'refundaccounttype'},
232 $row->{'refundamount'}
235 $csv->combine(@array);
236 my $string = $csv->string(@array);
237 print $string, "\n";
241 print ",,,,,,,\n";
242 print ",,,,,,,\n";
243 print ",,Total Amount Paid, $totalpaid\n";
244 print ",,Total Number Written, $totalwritten\n";
245 print ",,Total Amount Credits, $totalcredits\n";
246 print ",,Total Amount Refunds, $totalrefunds\n";
248 else {
249 $template->param(
250 date => $time,
251 date2 => $time2,
252 loop1 => \@loop1,
253 loop2 => \@loop2,
254 loop3 => \@loop3,
255 totalpaid => $totalpaid,
256 totalcredits => $totalcredits,
257 totalwritten => $totalwritten,
258 totalrefund => $totalrefunds,
259 totalcash => $totalcash,
260 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
262 output_html_with_http_headers $input, $cookie, $template->output;