Fixing the date entry that I broke when adding the tables
[koha.git] / reports / stats.screen.pl
bloba7161994d9803e3e1766470bc312e56419a22f17
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 Date::Manip;
24 use C4::Stats;
26 &Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005
28 my $input = new CGI;
29 my $time = $input->param('time');
30 my $time2 = $input->param('time2');
32 if ( $input->param('submit') eq "To Excel"
33 || $input->param('submit_x') eq "To Excel" )
35 print $input->redirect(
36 "/cgi-bin/koha/stats.print.pl?time=$time&time2=$time2");
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41 template_name => "stats_screen.tmpl",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 1,
45 flagsrequired => { reports => 1 },
46 debug => 1,
50 my $date;
51 my $date2;
52 if ( $time eq 'yesterday' ) {
53 $date = ParseDate('yesterday');
54 $date2 = ParseDate('today');
56 if ( $time eq 'today' ) {
57 $date = ParseDate('today');
58 $date2 = ParseDate('tomorrow');
60 if ( $time eq 'daybefore' ) {
61 $date = ParseDate('2 days ago');
62 $date2 = ParseDate('yesterday');
64 if ( $time eq 'month' ) {
65 $date = ParseDate('1 month ago');
66 $date2 = ParseDate('today');
69 if ( $time =~ /\// ) {
70 $date = ParseDate($time);
71 $date2 = ParseDateDelta('+ 1 day');
72 $date2 = DateCalc( $date, $date2 );
75 # if time is blank
76 if ( $time eq '' ) {
77 $date = ParseDate('today');
78 $date2 = ParseDate('tomorrow');
81 # if script is called with a start and finsh date range...
82 if ( $time ne '' && $time2 ne '' ) {
83 $date = ParseDate($time);
84 $date2 = ParseDate($time2);
87 my $date = UnixDate( $date, '%Y-%m-%d' );
88 my $date2 = UnixDate( $date2, '%Y-%m-%d' );
90 # warn "MASON: TIME: $time, $time2";
91 # warn "MASON: DATE: $date, $date2";
93 # get a list of every payment
94 my @payments = TotalPaid( $date, $date2, 0 );
96 my $count = @payments;
98 # print "MASON: number of payments=$count\n";
100 my $i = 0;
101 my $totalcharges = 0;
102 my $totalcredits = 0;
103 my $totalpaid = 0;
104 my $totalwritten = 0;
105 my @loop1;
106 my @loop2;
108 # lets get a a list of all individual item charges paid for by that payment
109 while ( $i < $count ) {
111 my $count;
112 my @charges;
114 if ( $payments[$i]{'type'} ne 'writeoff' ) {
116 @charges = getcharges(
117 $payments[$i]{'borrowernumber'},
118 $payments[$i]{'timestamp'},
119 $payments[$i]{'proccode'}
121 $totalcharges++;
122 $count = @charges;
124 # getting each of the charges and putting them into a array to be printed out
125 #this loops per charge per person
126 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
127 my $hour = substr( $payments[$i]{'timestamp'}, 8, 2 );
128 my $min = substr( $payments[$i]{'timestamp'}, 10, 2 );
129 my $sec = substr( $payments[$i]{'timestamp'}, 12, 2 );
130 my $time = "$hour:$min:$sec";
131 my $time2 = "$payments[$i]{'date'}";
133 # my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
134 my $branch = $payments[$i]{'branch'};
136 # if ($payments[$i]{'borrowernumber'} == 18265){
137 # warn "$payments[$i]{'branch'} $branch $payments[$i]{'borrowernumber'}";#
139 # lets build up a row
140 my %rows1 = (
141 branch => $branch,
142 datetime => $payments[$i]->{'datetime'},
143 surname => $payments[$i]->{'surname'},
144 firstname => $payments[$i]->{'firstname'},
145 description => $charges[$i2]->{'description'},
146 accounttype => $charges[$i2]->{'accounttype'},
147 amount => sprintf( "%.2f", $charges[$i2]->{'amount'} )
148 , # rounding amounts to 2dp
149 type => $payments[$i]->{'type'},
150 value => sprintf( "%.2f", $payments[$i]->{'value'} )
151 ); # rounding amounts to 2dp
153 push( @loop1, \%rows1 );
154 $totalpaid = $totalpaid + $payments[$i]->{'value'};
157 else {
158 ++$totalwritten;
161 $i++; #increment the while loop
164 #get credits and append to the bottom of payments
165 my @credits = getcredits( $date, $date2 );
167 my $count = @credits;
168 my $i = 0;
170 while ( $i < $count ) {
172 my %rows2 = (
173 creditbranch => $credits[$i]->{'branchcode'},
174 creditdate => $credits[$i]->{'date'},
175 creditsurname => $credits[$i]->{'surname'},
176 creditfirstname => $credits[$i]->{'firstname'},
177 creditdescription => $credits[$i]->{'description'},
178 creditaccounttype => $credits[$i]->{'accounttype'},
179 creditamount => sprintf( "%.2f", $credits[$i]->{'amount'} )
182 push( @loop2, \%rows2 );
183 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
184 $i++; #increment the while loop
187 #takes off first char minus sign "-100.00"
188 $totalcredits = substr( $totalcredits, 1 );
190 my $totalrefunds = 0;
191 my @loop3;
192 my @refunds = getrefunds( $date, $date2 );
193 $count = @refunds;
194 $i = 0;
196 while ( $i < $count ) {
198 my %rows2 = (
199 refundbranch => $refunds[$i]->{'branchcode'},
200 refunddate => $refunds[$i]->{'date'},
201 refundsurname => $refunds[$i]->{'surname'},
202 refundfirstname => $refunds[$i]->{'firstname'},
203 refunddescription => $refunds[$i]->{'description'},
204 refundaccounttype => $refunds[$i]->{'accounttype'},
205 refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} )
208 push( @loop3, \%rows2 );
209 $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
210 $i++; #increment the while loop
213 my $totalcash = $totalpaid - $totalrefunds;
215 $template->param(
216 date => $time,
217 date2 => $time2,
218 loop1 => \@loop1,
219 loop2 => \@loop2,
220 loop3 => \@loop3,
221 totalpaid => $totalpaid,
222 totalcredits => $totalcredits,
223 totalwritten => $totalwritten,
224 totalrefund => $totalrefunds,
225 totalcash => $totalcash
228 output_html_with_http_headers $input, $cookie, $template->output;