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.tmpl",
43 flagsrequired
=> { reports
=> '*' },
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";
73 # lets get a a list of all individual item charges paid for by that payment
75 foreach my $payment (@payments) {
78 if ( $payment->{'type'} ne 'writeoff' ) {
80 @charges = getcharges
(
81 $payment->{'borrowernumber'},
82 $payment->{'timestamp'},
83 $payment->{'proccode'}
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
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";
126 #get credits and append to the bottom of payments
127 my @credits = getcredits
( $date, $date2 );
129 my $count = @credits;
132 while ( $i < $count ) {
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;
154 my @refunds = getrefunds
( $date, $date2 );
158 while ( $i < $count ) {
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(
182 'escape_char' => '"',
188 print $input->header(
189 -type
=> 'application/vnd.ms-excel',
190 -attachment
=> "stats.csv",
193 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
197 for my $row (@loop1) {
199 $row->{'branch'}, $row->{'datetime'},
200 $row->{'surname'}, $row->{'firstname'},
201 $row->{'description'}, $row->{'accounttype'},
202 $row->{'amount'}, $row->{'type'},
206 $csv->combine(@array);
207 my $string = $csv->string(@array);
212 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
214 for my $row (@loop2) {
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);
229 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
231 for my $row (@loop3) {
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);
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";
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;