Bug 7309 - Add NORMARCslim2intranetDetail.xsl for detail view in intranet
[koha.git] / reports / stats.screen.pl
blob1d3eaef332b0b43f11b368deda34e1c43677d3ef
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 warnings; FIXME - Bug 2505
20 use CGI;
21 use C4::Output;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Stats;
25 use C4::Accounts;
26 use C4::Debug;
27 use Date::Manip;
29 my $input = new CGI;
30 my $time = $input->param('time');
31 my $time2 = $input->param('time2');
32 my $op = $input->param('submit');
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 template_name => "reports/stats_screen.tmpl",
37 query => $input,
38 type => "intranet",
39 authnotrequired => 1,
40 flagsrequired => { reports => '*' },
41 debug => 1,
45 ( $time = "today" ) if !$time;
46 ( $time2 = "tomorrow" ) if !$time2;
48 my $date = ParseDate($time);
49 my $date2 = ParseDate($time2);
50 $date = UnixDate( $date, '%Y-%m-%d' );
51 $date2 = UnixDate( $date2, '%Y-%m-%d' );
52 $debug and warn "MASON: TIME: $time, $time2";
53 $debug and warn "MASON: DATE: $date, $date2";
55 # get a list of every payment
56 my @payments = TotalPaid( $date, $date2 );
58 my $count = @payments;
60 $debug and warn "MASON: number of payments=$count\n";
62 my $i = 0;
63 my $totalcharges = 0;
64 my $totalcredits = 0;
65 my $totalpaid = 0;
66 my $totalwritten = 0;
67 my @loop1;
68 my @loop2;
70 # lets get a a list of all individual item charges paid for by that payment
72 foreach my $payment (@payments) {
74 my @charges;
75 if ( $payment->{'type'} ne 'writeoff' ) {
77 @charges = getcharges(
78 $payment->{'borrowernumber'},
79 $payment->{'timestamp'},
80 $payment->{'proccode'}
82 $totalcharges++;
83 my $count = @charges;
85 # getting each of the charges and putting them into a array to be printed out
86 #this loops per charge per person
87 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
88 my $hour = substr( $payment->{'timestamp'}, 8, 2 );
89 my $min = substr( $payment->{'timestamp'}, 10, 2 );
90 my $sec = substr( $payment->{'timestamp'}, 12, 2 );
91 my $time = "$hour:$min:$sec";
92 my $time2 = "$payment->{'date'}";
94 # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
95 my $branch = $payment->{'branch'};
97 # lets build up a row
98 my %rows1 = (
99 branch => $branch,
100 datetime => $payment->{'datetime'},
101 surname => $payment->{'surname'},
102 firstname => $payment->{'firstname'},
103 description => $charges[$i2]->{'description'},
104 accounttype => $charges[$i2]->{'accounttype'},
105 amount => sprintf( "%.2f", $charges[$i2]->{'amount'} )
106 , # rounding amounts to 2dp
107 type => $payment->{'type'},
108 value => sprintf( "%.2f", $payment->{'value'} )
109 ); # rounding amounts to 2dp
111 push( @loop1, \%rows1 );
114 $totalpaid = $totalpaid + $payment->{'value'};
115 $debug and warn "totalpaid = $totalpaid";
117 else {
118 ++$totalwritten;
123 #get credits and append to the bottom of payments
124 my @credits = getcredits( $date, $date2 );
126 my $count = @credits;
127 my $i = 0;
129 while ( $i < $count ) {
131 my %rows2 = (
132 creditbranch => $credits[$i]->{'branchcode'},
133 creditdate => $credits[$i]->{'date'},
134 creditsurname => $credits[$i]->{'surname'},
135 creditfirstname => $credits[$i]->{'firstname'},
136 creditdescription => $credits[$i]->{'description'},
137 creditaccounttype => $credits[$i]->{'accounttype'},
138 creditamount => sprintf( "%.2f", $credits[$i]->{'amount'} )
141 push( @loop2, \%rows2 );
142 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
143 $i++; #increment the while loop
146 #takes off first char minus sign "-100.00"
147 $totalcredits = substr( $totalcredits, 1 );
149 my $totalrefunds = 0;
150 my @loop3;
151 my @refunds = getrefunds( $date, $date2 );
152 $count = @refunds;
153 $i = 0;
155 while ( $i < $count ) {
157 my %rows3 = (
158 refundbranch => $refunds[$i]->{'branchcode'},
159 refunddate => $refunds[$i]->{'datetime'},
160 refundsurname => $refunds[$i]->{'surname'},
161 refundfirstname => $refunds[$i]->{'firstname'},
162 refunddescription => $refunds[$i]->{'description'},
163 refundaccounttype => $refunds[$i]->{'accounttype'},
164 refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} )
167 push( @loop3, \%rows3 );
168 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
169 $i++; #increment the while loop
172 my $totalcash = $totalpaid - $totalrefunds;
174 if ( $op eq 'To Excel' ) {
176 my $csv = Text::CSV_XS->new(
178 'quote_char' => '"',
179 'escape_char' => '"',
180 'sep_char' => ',',
181 'binary' => 1
185 print $input->header(
186 -type => 'application/vnd.ms-excel',
187 -attachment => "stats.csv",
189 print
190 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
192 $DB::single = 1;
194 for my $row (@loop1) {
195 my @array = (
196 $row->{'branch'}, $row->{'datetime'},
197 $row->{'surname'}, $row->{'firstname'},
198 $row->{'description'}, $row->{'accounttype'},
199 $row->{'amount'}, $row->{'type'},
200 $row->{'value'}
203 $csv->combine(@array);
204 my $string = $csv->string(@array);
205 print $string, "\n";
207 print ",,,,,,,\n";
208 print
209 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
211 for my $row (@loop2) {
213 my @array = (
214 $row->{'creditbranch'}, $row->{'creditdate'},
215 $row->{'creditsurname'}, $row->{'creditfirstname'},
216 $row->{'creditdescription'}, $row->{'creditaccounttype'},
217 $row->{'creditamount'}
220 $csv->combine(@array);
221 my $string = $csv->string(@array);
222 print $string, "\n";
224 print ",,,,,,,\n";
225 print
226 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
228 for my $row (@loop3) {
229 my @array = (
230 $row->{'refundbranch'}, $row->{'refunddate'},
231 $row->{'refundsurname'}, $row->{'refundfirstname'},
232 $row->{'refunddescription'}, $row->{'refundaccounttype'},
233 $row->{'refundamount'}
236 $csv->combine(@array);
237 my $string = $csv->string(@array);
238 print $string, "\n";
242 print ",,,,,,,\n";
243 print ",,,,,,,\n";
244 print ",,Total Amount Paid, $totalpaid\n";
245 print ",,Total Number Written, $totalwritten\n";
246 print ",,Total Amount Credits, $totalcredits\n";
247 print ",,Total Amount Refunds, $totalrefunds\n";
249 else {
250 $template->param(
251 date => $time,
252 date2 => $time2,
253 loop1 => \@loop1,
254 loop2 => \@loop2,
255 loop3 => \@loop3,
256 totalpaid => $totalpaid,
257 totalcredits => $totalcredits,
258 totalwritten => $totalwritten,
259 totalrefund => $totalrefunds,
260 totalcash => $totalcash,
261 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
263 output_html_with_http_headers $input, $cookie, $template->output;