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