synch fr & en
[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::Context;
23 use Date::Manip;
24 use C4::Stats;
25
26 &Date_Init("DateFormat=non-US");    # set non-USA date, eg:19/08/2005
27
28 my $input = new CGI;
29 my $time  = $input->param('time');
30 my $time2 = $input->param('time2');
31
32 if (   $input->param('submit') eq "To Excel"
33     || $input->param('submit_x') eq "To Excel" )
34 {
35     print $input->redirect(
36         "/cgi-bin/koha/stats.print.pl?time=$time&time2=$time2");
37 }
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "stats_screen.tmpl",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 1,
45         flagsrequired   => { reports => 1 },
46         debug           => 1,
47     }
48 );
49
50 my $date;
51 my $date2;
52 if ( $time eq 'yesterday' ) {
53     $date  = ParseDate('yesterday');
54     $date2 = ParseDate('today');
55 }
56 if ( $time eq 'today' ) {
57     $date  = ParseDate('today');
58     $date2 = ParseDate('tomorrow');
59 }
60 if ( $time eq 'daybefore' ) {
61     $date  = ParseDate('2 days ago');
62     $date2 = ParseDate('yesterday');
63 }
64 if ( $time eq 'month' ) {
65     $date  = ParseDate('1 month ago');
66     $date2 = ParseDate('today');
67
68 }
69 if ( $time =~ /\// ) {
70     $date  = ParseDate($time);
71     $date2 = ParseDateDelta('+ 1 day');
72     $date2 = DateCalc( $date, $date2 );
73 }
74
75 # if time is blank
76 if ( $time eq '' ) {
77     $date  = ParseDate('today');
78     $date2 = ParseDate('tomorrow');
79 }
80
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);
85 }
86
87 my $date  = UnixDate( $date,  '%Y-%m-%d' );
88 my $date2 = UnixDate( $date2, '%Y-%m-%d' );
89
90 # warn "MASON: TIME: $time, $time2";
91 # warn "MASON: DATE: $date, $date2";
92
93 # get a list of every payment
94 my @payments = TotalPaid( $date, $date2, 0 );
95
96 my $count = @payments;
97
98 # print "MASON: number of payments=$count\n";
99
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;
107
108 # lets get a a list of all individual item charges paid for by that payment
109 while ( $i < $count ) {
110
111     my $count;
112     my @charges;
113
114     if ( $payments[$i]{'type'} ne 'writeoff' ) {
115
116         @charges = getcharges(
117             $payments[$i]{'borrowernumber'},
118             $payments[$i]{'timestamp'},
119             $payments[$i]{'proccode'}
120         );
121         $totalcharges++;
122         $count = @charges;
123
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'}";
132
133 #               my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
134             my $branch = $payments[$i]{'branch'};
135
136 #              if ($payments[$i]{'borrowernumber'} == 18265){
137 #               warn "$payments[$i]{'branch'} $branch $payments[$i]{'borrowernumber'}";#
138 #                  }
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
152
153             push( @loop1, \%rows1 );
154             $totalpaid = $totalpaid + $payments[$i]->{'value'};
155         }
156     }
157     else {
158         ++$totalwritten;
159     }
160
161     $i++;            #increment the while loop
162 }
163
164 #get credits and append to the bottom of payments
165 my @credits = getcredits( $date, $date2 );
166
167 my $count = @credits;
168 my $i     = 0;
169
170 while ( $i < $count ) {
171
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'} )
180     );
181
182     push( @loop2, \%rows2 );
183     $totalcredits = $totalcredits + $credits[$i]->{'amount'};
184     $i++;    #increment the while loop
185 }
186
187 #takes off first char minus sign "-100.00"
188 $totalcredits = substr( $totalcredits, 1 );
189
190 my $totalrefunds = 0;
191 my @loop3;
192 my @refunds = getrefunds( $date, $date2 );
193 $count = @refunds;
194 $i     = 0;
195
196 while ( $i < $count ) {
197
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'} )
206     );
207
208     push( @loop3, \%rows2 );
209     $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
210     $i++;    #increment the while loop
211 }
212
213 my $totalcash = $totalpaid - $totalrefunds;
214
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
226 );
227
228 output_html_with_http_headers $input, $cookie, $template->output;
229