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
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.
22 #use warnings; FIXME - Bug 2505
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.tt",
42 flagsrequired
=> { reports
=> '*' },
46 ( $time = "today" ) if !$time;
47 ( $time2 = "tomorrow" ) if !$time2;
49 my $date = ParseDate
($time);
50 my $date2 = ParseDate
($time2);
51 $date = UnixDate
( $date, '%Y-%m-%d' );
52 $date2 = UnixDate
( $date2, '%Y-%m-%d' );
53 $debug and warn "MASON: TIME: $time, $time2";
54 $debug and warn "MASON: DATE: $date, $date2";
56 # get a list of every payment
57 my @payments = TotalPaid
( $date, $date2 );
59 my $count = @payments;
61 $debug and warn "MASON: number of payments=$count\n";
71 # lets get a a list of all individual item charges paid for by that payment
73 foreach my $payment (@payments) {
76 if ( $payment->{'type'} ne 'writeoff' ) {
78 @charges = getcharges
(
79 $payment->{'borrowernumber'},
80 $payment->{'timestamp'},
81 $payment->{'proccode'}
86 # getting each of the charges and putting them into a array to be printed out
87 #this loops per charge per person
88 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
89 my $hour = substr( $payment->{'timestamp'}, 8, 2 );
90 my $min = substr( $payment->{'timestamp'}, 10, 2 );
91 my $sec = substr( $payment->{'timestamp'}, 12, 2 );
92 my $time = "$hour:$min:$sec";
93 my $time2 = "$payment->{'date'}";
95 # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
96 my $branch = $payment->{'branch'};
101 datetime
=> $payment->{'datetime'},
102 surname
=> $payment->{'surname'},
103 firstname
=> $payment->{'firstname'},
104 description
=> $charges[$i2]->{'description'},
105 accounttype
=> $charges[$i2]->{'accounttype'},
106 amount
=> sprintf( "%.2f", $charges[$i2]->{'amount'} )
107 , # rounding amounts to 2dp
108 type
=> $payment->{'type'},
109 value
=> sprintf( "%.2f", $payment->{'value'} )
110 ); # rounding amounts to 2dp
112 push( @loop1, \
%rows1 );
115 $totalpaid = $totalpaid + $payment->{'value'};
116 $debug and warn "totalpaid = $totalpaid";
124 #get credits and append to the bottom of payments
125 my @credits = getcredits
( $date, $date2 );
127 my $count = @credits;
130 while ( $i < $count ) {
133 creditbranch
=> $credits[$i]->{'branchcode'},
134 creditdate
=> $credits[$i]->{'date'},
135 creditsurname
=> $credits[$i]->{'surname'},
136 creditfirstname
=> $credits[$i]->{'firstname'},
137 creditdescription
=> $credits[$i]->{'description'},
138 creditaccounttype
=> $credits[$i]->{'accounttype'},
139 creditamount
=> sprintf( "%.2f", $credits[$i]->{'amount'} )
142 push( @loop2, \
%rows2 );
143 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
144 $i++; #increment the while loop
147 #takes off first char minus sign "-100.00"
148 $totalcredits = substr( $totalcredits, 1 );
150 my $totalrefunds = 0;
152 my @refunds = getrefunds
( $date, $date2 );
156 while ( $i < $count ) {
159 refundbranch
=> $refunds[$i]->{'branchcode'},
160 refunddate
=> $refunds[$i]->{'datetime'},
161 refundsurname
=> $refunds[$i]->{'surname'},
162 refundfirstname
=> $refunds[$i]->{'firstname'},
163 refunddescription
=> $refunds[$i]->{'description'},
164 refundaccounttype
=> $refunds[$i]->{'accounttype'},
165 refundamount
=> sprintf( "%.2f", $refunds[$i]->{'amount'} )
168 push( @loop3, \
%rows3 );
169 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
170 $i++; #increment the while loop
173 my $totalcash = $totalpaid - $totalrefunds;
175 if ( $op eq 'To Excel' ) {
177 my $csv = Text
::CSV_XS
->new(
180 'escape_char' => '"',
186 print $input->header(
187 -type
=> 'application/vnd.ms-excel',
188 -attachment
=> "stats.csv",
191 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
195 for my $row (@loop1) {
197 $row->{'branch'}, $row->{'datetime'},
198 $row->{'surname'}, $row->{'firstname'},
199 $row->{'description'}, $row->{'accounttype'},
200 $row->{'amount'}, $row->{'type'},
204 $csv->combine(@array);
205 my $string = $csv->string(@array);
210 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
212 for my $row (@loop2) {
215 $row->{'creditbranch'}, $row->{'creditdate'},
216 $row->{'creditsurname'}, $row->{'creditfirstname'},
217 $row->{'creditdescription'}, $row->{'creditaccounttype'},
218 $row->{'creditamount'}
221 $csv->combine(@array);
222 my $string = $csv->string(@array);
227 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
229 for my $row (@loop3) {
231 $row->{'refundbranch'}, $row->{'refunddate'},
232 $row->{'refundsurname'}, $row->{'refundfirstname'},
233 $row->{'refunddescription'}, $row->{'refundaccounttype'},
234 $row->{'refundamount'}
237 $csv->combine(@array);
238 my $string = $csv->string(@array);
245 print ",,Total Amount Paid, $totalpaid\n";
246 print ",,Total Number Written, $totalwritten\n";
247 print ",,Total Amount Credits, $totalcredits\n";
248 print ",,Total Amount Refunds, $totalrefunds\n";
257 totalpaid
=> $totalpaid,
258 totalcredits
=> $totalcredits,
259 totalwritten
=> $totalwritten,
260 totalrefund
=> $totalrefunds,
261 totalcash
=> $totalcash,
263 output_html_with_http_headers
$input, $cookie, $template->output;