Bug 9788: Improvements when calling GetReservesFromItemnumber
[koha.git] / reports / stats.screen.pl
blob16b8f8aa4b753c47ce87ca3594e73830c1c0e1c9
1 #!/usr/bin/perl
3 # Copyright Katipo Communications 2006
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Stats;
28 use C4::Accounts;
29 use C4::Debug;
30 use Date::Manip;
32 my $input = new CGI;
33 my $time = $input->param('time');
34 my $time2 = $input->param('time2');
35 my $op = $input->param('submit');
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 template_name => "reports/stats_screen.tmpl",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 1,
43 flagsrequired => { reports => '*' },
44 debug => 1,
48 ( $time = "today" ) if !$time;
49 ( $time2 = "tomorrow" ) if !$time2;
51 my $date = ParseDate($time);
52 my $date2 = ParseDate($time2);
53 $date = UnixDate( $date, '%Y-%m-%d' );
54 $date2 = UnixDate( $date2, '%Y-%m-%d' );
55 $debug and warn "MASON: TIME: $time, $time2";
56 $debug and warn "MASON: DATE: $date, $date2";
58 # get a list of every payment
59 my @payments = TotalPaid( $date, $date2 );
61 my $count = @payments;
63 $debug and warn "MASON: number of payments=$count\n";
65 my $i = 0;
66 my $totalcharges = 0;
67 my $totalcredits = 0;
68 my $totalpaid = 0;
69 my $totalwritten = 0;
70 my @loop1;
71 my @loop2;
73 # lets get a a list of all individual item charges paid for by that payment
75 foreach my $payment (@payments) {
77 my @charges;
78 if ( $payment->{'type'} ne 'writeoff' ) {
80 @charges = getcharges(
81 $payment->{'borrowernumber'},
82 $payment->{'timestamp'},
83 $payment->{'proccode'}
85 $totalcharges++;
86 my $count = @charges;
88 # getting each of the charges and putting them into a array to be printed out
89 #this loops per charge per person
90 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
91 my $hour = substr( $payment->{'timestamp'}, 8, 2 );
92 my $min = substr( $payment->{'timestamp'}, 10, 2 );
93 my $sec = substr( $payment->{'timestamp'}, 12, 2 );
94 my $time = "$hour:$min:$sec";
95 my $time2 = "$payment->{'date'}";
97 # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
98 my $branch = $payment->{'branch'};
100 # lets build up a row
101 my %rows1 = (
102 branch => $branch,
103 datetime => $payment->{'datetime'},
104 surname => $payment->{'surname'},
105 firstname => $payment->{'firstname'},
106 description => $charges[$i2]->{'description'},
107 accounttype => $charges[$i2]->{'accounttype'},
108 amount => sprintf( "%.2f", $charges[$i2]->{'amount'} )
109 , # rounding amounts to 2dp
110 type => $payment->{'type'},
111 value => sprintf( "%.2f", $payment->{'value'} )
112 ); # rounding amounts to 2dp
114 push( @loop1, \%rows1 );
117 $totalpaid = $totalpaid + $payment->{'value'};
118 $debug and warn "totalpaid = $totalpaid";
120 else {
121 ++$totalwritten;
126 #get credits and append to the bottom of payments
127 my @credits = getcredits( $date, $date2 );
129 my $count = @credits;
130 my $i = 0;
132 while ( $i < $count ) {
134 my %rows2 = (
135 creditbranch => $credits[$i]->{'branchcode'},
136 creditdate => $credits[$i]->{'date'},
137 creditsurname => $credits[$i]->{'surname'},
138 creditfirstname => $credits[$i]->{'firstname'},
139 creditdescription => $credits[$i]->{'description'},
140 creditaccounttype => $credits[$i]->{'accounttype'},
141 creditamount => sprintf( "%.2f", $credits[$i]->{'amount'} )
144 push( @loop2, \%rows2 );
145 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
146 $i++; #increment the while loop
149 #takes off first char minus sign "-100.00"
150 $totalcredits = substr( $totalcredits, 1 );
152 my $totalrefunds = 0;
153 my @loop3;
154 my @refunds = getrefunds( $date, $date2 );
155 $count = @refunds;
156 $i = 0;
158 while ( $i < $count ) {
160 my %rows3 = (
161 refundbranch => $refunds[$i]->{'branchcode'},
162 refunddate => $refunds[$i]->{'datetime'},
163 refundsurname => $refunds[$i]->{'surname'},
164 refundfirstname => $refunds[$i]->{'firstname'},
165 refunddescription => $refunds[$i]->{'description'},
166 refundaccounttype => $refunds[$i]->{'accounttype'},
167 refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} )
170 push( @loop3, \%rows3 );
171 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
172 $i++; #increment the while loop
175 my $totalcash = $totalpaid - $totalrefunds;
177 if ( $op eq 'To Excel' ) {
179 my $csv = Text::CSV_XS->new(
181 'quote_char' => '"',
182 'escape_char' => '"',
183 'sep_char' => ',',
184 'binary' => 1
188 print $input->header(
189 -type => 'application/vnd.ms-excel',
190 -attachment => "stats.csv",
192 print
193 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
195 $DB::single = 1;
197 for my $row (@loop1) {
198 my @array = (
199 $row->{'branch'}, $row->{'datetime'},
200 $row->{'surname'}, $row->{'firstname'},
201 $row->{'description'}, $row->{'accounttype'},
202 $row->{'amount'}, $row->{'type'},
203 $row->{'value'}
206 $csv->combine(@array);
207 my $string = $csv->string(@array);
208 print $string, "\n";
210 print ",,,,,,,\n";
211 print
212 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
214 for my $row (@loop2) {
216 my @array = (
217 $row->{'creditbranch'}, $row->{'creditdate'},
218 $row->{'creditsurname'}, $row->{'creditfirstname'},
219 $row->{'creditdescription'}, $row->{'creditaccounttype'},
220 $row->{'creditamount'}
223 $csv->combine(@array);
224 my $string = $csv->string(@array);
225 print $string, "\n";
227 print ",,,,,,,\n";
228 print
229 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
231 for my $row (@loop3) {
232 my @array = (
233 $row->{'refundbranch'}, $row->{'refunddate'},
234 $row->{'refundsurname'}, $row->{'refundfirstname'},
235 $row->{'refunddescription'}, $row->{'refundaccounttype'},
236 $row->{'refundamount'}
239 $csv->combine(@array);
240 my $string = $csv->string(@array);
241 print $string, "\n";
245 print ",,,,,,,\n";
246 print ",,,,,,,\n";
247 print ",,Total Amount Paid, $totalpaid\n";
248 print ",,Total Number Written, $totalwritten\n";
249 print ",,Total Amount Credits, $totalcredits\n";
250 print ",,Total Amount Refunds, $totalrefunds\n";
252 else {
253 $template->param(
254 date => $time,
255 date2 => $time2,
256 loop1 => \@loop1,
257 loop2 => \@loop2,
258 loop3 => \@loop3,
259 totalpaid => $totalpaid,
260 totalcredits => $totalcredits,
261 totalwritten => $totalwritten,
262 totalrefund => $totalrefunds,
263 totalcash => $totalcash,
265 output_html_with_http_headers $input, $cookie, $template->output;