bug-2149, added new block to C4::Letters::SendAlerts() to email 'account creation...
[koha.git] / reports / stats.screen.pl
blobb15a50a0207b5bdfc71da4b8fbbe8ba82238fd71
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 #use HTML::Template;
29 #use Text::CSV_XS;
30 #use Data::Dumper;
33 my $input = new CGI;
34 my $time = $input->param('time');
35 my $time2 = $input->param('time2');
36 my $op = $input->param('submit');
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40 template_name => "reports/stats_screen.tmpl",
41 query => $input,
42 type => "intranet",
43 authnotrequired => 1,
44 flagsrequired => { borrowers => 1 },
45 debug => 1,
49 ( $time = "today" ) if !$time;
50 ( $time2 = "tomorrow" ) if !$time2;
52 my $date = ParseDate($time);
53 my $date2 = ParseDate($time2);
54 $date = UnixDate( $date, '%Y-%m-%d' );
55 $date2 = UnixDate( $date2, '%Y-%m-%d' );
56 $debug and warn "MASON: TIME: $time, $time2";
57 $debug and warn "MASON: DATE: $date, $date2";
59 # get a list of every payment
60 my @payments = TotalPaid( $date, $date2 );
62 my $count = @payments;
64 $debug and warn "MASON: number of payments=$count\n";
66 my $i = 0;
67 my $totalcharges = 0;
68 my $totalcredits = 0;
69 my $totalpaid = 0;
70 my $totalwritten = 0;
71 my @loop1;
72 my @loop2;
74 # lets get a a list of all individual item charges paid for by that payment
76 foreach my $payment (@payments) {
78 my @charges;
79 if ( $payment->{'type'} ne 'writeoff' ) {
81 @charges = getcharges(
82 $payment->{'borrowernumber'},
83 $payment->{'timestamp'},
84 $payment->{'proccode'}
86 $totalcharges++;
87 my $count = @charges;
89 # getting each of the charges and putting them into a array to be printed out
90 #this loops per charge per person
91 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
92 my $hour = substr( $payment->{'timestamp'}, 8, 2 );
93 my $min = substr( $payment->{'timestamp'}, 10, 2 );
94 my $sec = substr( $payment->{'timestamp'}, 12, 2 );
95 my $time = "$hour:$min:$sec";
96 my $time2 = "$payment->{'date'}";
98 # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
99 my $branch = $payment->{'branch'};
101 # lets build up a row
102 my %rows1 = (
103 branch => $branch,
104 datetime => $payment->{'datetime'},
105 surname => $payment->{'surname'},
106 firstname => $payment->{'firstname'},
107 description => $charges[$i2]->{'description'},
108 accounttype => $charges[$i2]->{'accounttype'},
109 amount => sprintf( "%.2f", $charges[$i2]->{'amount'} )
110 , # rounding amounts to 2dp
111 type => $payment->{'type'},
112 value => sprintf( "%.2f", $payment->{'value'} )
113 ); # rounding amounts to 2dp
115 push( @loop1, \%rows1 );
118 $totalpaid = $totalpaid + $payment->{'value'};
119 $debug and warn "totalpaid = $totalpaid";
121 else {
122 ++$totalwritten;
127 #get credits and append to the bottom of payments
128 my @credits = getcredits( $date, $date2 );
130 my $count = @credits;
131 my $i = 0;
133 while ( $i < $count ) {
135 my %rows2 = (
136 creditbranch => $credits[$i]->{'branchcode'},
137 creditdate => $credits[$i]->{'date'},
138 creditsurname => $credits[$i]->{'surname'},
139 creditfirstname => $credits[$i]->{'firstname'},
140 creditdescription => $credits[$i]->{'description'},
141 creditaccounttype => $credits[$i]->{'accounttype'},
142 creditamount => sprintf( "%.2f", $credits[$i]->{'amount'} )
145 push( @loop2, \%rows2 );
146 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
147 $i++; #increment the while loop
150 #takes off first char minus sign "-100.00"
151 $totalcredits = substr( $totalcredits, 1 );
153 my $totalrefunds = 0;
154 my @loop3;
155 my @refunds = getrefunds( $date, $date2 );
156 $count = @refunds;
157 $i = 0;
159 while ( $i < $count ) {
161 my %rows3 = (
162 refundbranch => $refunds[$i]->{'branchcode'},
163 refunddate => $refunds[$i]->{'datetime'},
164 refundsurname => $refunds[$i]->{'surname'},
165 refundfirstname => $refunds[$i]->{'firstname'},
166 refunddescription => $refunds[$i]->{'description'},
167 refundaccounttype => $refunds[$i]->{'accounttype'},
168 refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} )
171 push( @loop3, \%rows3 );
172 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
173 $i++; #increment the while loop
176 my $totalcash = $totalpaid - $totalrefunds;
178 if ( $op eq 'To Excel' ) {
180 my $csv = Text::CSV_XS->new(
182 'quote_char' => '"',
183 'escape_char' => '"',
184 'sep_char' => ',',
185 'binary' => 1
189 print $input->header(
190 -type => 'application/vnd.ms-excel',
191 -attachment => "stats.csv",
193 print
194 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
196 $DB::single = 1;
198 for my $row (@loop1) {
199 my @array = (
200 $row->{'branch'}, $row->{'datetime'},
201 $row->{'surname'}, $row->{'firstname'},
202 $row->{'description'}, $row->{'accounttype'},
203 $row->{'amount'}, $row->{'type'},
204 $row->{'value'}
207 $csv->combine(@array);
208 my $string = $csv->string(@array);
209 print $string, "\n";
211 print ",,,,,,,\n";
212 print
213 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
215 for my $row (@loop2) {
217 my @array = (
218 $row->{'creditbranch'}, $row->{'creditdate'},
219 $row->{'creditsurname'}, $row->{'creditfirstname'},
220 $row->{'creditdescription'}, $row->{'creditaccounttype'},
221 $row->{'creditamount'}
224 $csv->combine(@array);
225 my $string = $csv->string(@array);
226 print $string, "\n";
228 print ",,,,,,,\n";
229 print
230 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
232 for my $row (@loop3) {
233 my @array = (
234 $row->{'refundbranch'}, $row->{'refunddate'},
235 $row->{'refundsurname'}, $row->{'refundfirstname'},
236 $row->{'refunddescription'}, $row->{'refundaccounttype'},
237 $row->{'refundamount'}
240 $csv->combine(@array);
241 my $string = $csv->string(@array);
242 print $string, "\n";
246 print ",,,,,,,\n";
247 print ",,,,,,,\n";
248 print ",,Total Amount Paid, $totalpaid\n";
249 print ",,Total Number Written, $totalwritten\n";
250 print ",,Total Amount Credits, $totalcredits\n";
251 print ",,Total Amount Refunds, $totalrefunds\n";
253 else {
254 $template->param(
255 date => $time,
256 date2 => $time2,
257 loop1 => \@loop1,
258 loop2 => \@loop2,
259 loop3 => \@loop3,
260 totalpaid => $totalpaid,
261 totalcredits => $totalcredits,
262 totalwritten => $totalwritten,
263 totalrefund => $totalrefunds,
264 totalcash => $totalcash,
265 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
267 output_html_with_http_headers $input, $cookie, $template->output;