Input.t permissions fix
[koha.git] / circ / stats.pl
1 #!/usr/bin/perl
2
3
4 #written 14/1/2000
5 #script to display reports
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Output;
28 use C4::Auth;
29 use Date::Manip;
30 use C4::Stats;
31
32 my $input = new CGI;
33 my $time  = $input->param('time');
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "circ/stats.tmpl",
38         query           => $input,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { reports => 1 },
42         debug           => 1,
43     }
44 );
45
46 my $date;
47 my $date2;
48 if ( $time eq '' ) {
49     $template->param(notime => '1');
50     output_html_with_http_headers $input, $cookie, $template->output;
51     exit;
52 }
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     warn "d : $date // d2 : $date2";
69 }
70 if ( $time =~ /\// ) {
71     $date  = ParseDate($time);
72     $date2 = ParseDateDelta('+ 1 day');
73     $date2 = DateCalc( $date, $date2 );
74 }
75 $date  = UnixDate( $date,  '%Y-%m-%d' );
76 $date2 = UnixDate( $date2, '%Y-%m-%d' );
77 warn "d : $date // d2 : $date2";
78 my @payments = TotalPaid( $date, $date2 );
79 my $count    = @payments;
80 my $total    = 0;
81 my $oldtime;
82 my $totalw = 0;
83 my @loop;
84 my %row;
85 my $i = 0;
86
87 while ( $i < $count ) {
88     warn " pay : " . $payments[$i]{'timestamp'};
89     my $time     = $payments[$i]{'datetime'};
90     my $payments = $payments[$i]{'value'};
91     my $charge   = 0;
92     my @temp     = split( / /, $payments[$i]{'datetime'} );
93     my $date     = $temp[0];
94     my @charges  =
95       getcharges( $payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'} );
96     my $count        = @charges;
97     my $temptotalf   = 0;
98     my $temptotalr   = 0;
99     my $temptotalres = 0;
100     my $temptotalren = 0;
101     my $temptotalw   = 0;
102
103     for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
104         $charge += $charges[$i2]->{'amount'};
105         %row = (
106             name   => $charges[$i2]->{'description'},
107             type   => $charges[$i2]->{'accounttype'},
108             time   => $charges[$i2]->{'timestamp'},
109             amount => $charges[$i2]->{'amount'},
110             branch => $charges[$i2]->{'amountoutstanding'}
111         );
112         push( @loop, \%row );
113         if ( $payments[$i]{'accountytpe'} ne 'W' ) {
114             if ( $charges[$i2]->{'accounttype'} eq 'Rent' ) {
115                 $temptotalr +=
116                   $charges[$i2]->{'amount'} -
117                   $charges[$i2]->{'amountoutstanding'};
118             }
119             if (   $charges[$i2]->{'accounttype'} eq 'F'
120                 || $charges[$i2]->{'accounttype'} eq 'FU'
121                 || $charges[$i2]->{'accounttype'} eq 'FN' )
122             {
123                 $temptotalf +=
124                   $charges[$i2]->{'amount'} -
125                   $charges[$i2]->{'amountoutstanding'};
126             }
127             if ( $charges[$i2]->{'accounttype'} eq 'Res' ) {
128                 $temptotalres +=
129                   $charges[$i2]->{'amount'} -
130                   $charges[$i2]->{'amountoutstanding'};
131             }
132             if ( $charges[$i2]->{'accounttype'} eq 'R' ) {
133                 $temptotalren +=
134                   $charges[$i2]->{'amount'} -
135                   $charges[$i2]->{'amountoutstanding'};
136             }
137         }
138     }
139     my $hour = substr( $payments[$i]{'timestamp'}, 8,  2 );
140     my $min  = substr( $payments[$i]{'timestamp'}, 10, 2 );
141     my $sec  = substr( $payments[$i]{'timestamp'}, 12, 2 );
142     my $time = "$hour:$min:$sec";
143     my $time2 = "$payments[$i]{'date'}";
144     my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
145     my $borrowernumber = $payments[$i]{'borrowernumber'};
146     my $oldtime        = $payments[$i]{'timestamp'};
147     my $oldtype        = $payments[$i]{'accounttype'};
148
149     while ($borrowernumber eq $payments[$i]{'borrowernumber'}
150         && $oldtype == $payments[$i]{'accounttype'}
151         && $oldtime eq $payments[$i]{'timestamp'} )
152     {
153         my $hour = substr( $payments[$i]{'timestamp'}, 8,  2 );
154         my $min  = substr( $payments[$i]{'timestamp'}, 10, 2 );
155         my $sec  = substr( $payments[$i]{'timestamp'}, 12, 2 );
156         my $time = "$hour:$min:$sec";
157         my $time2 = "$payments[$i]{'date'}";
158         my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
159         if ( $payments[$i]{'accounttype'} eq 'W' ) {
160             $totalw += $payments[$i]{'amount'};
161         }
162         else {
163             $payments[$i]{'amount'} = $payments[$i]{'amount'} * -1;
164             $total += $payments[$i]{'amount'};
165         }
166
167         %row = (
168             name => "<b>"
169               . $payments[$i]{'firstname'}
170               . $payments[$i]{'surname'} . "</b>",
171             type   => $payments[$i]{'accounttype'},
172             time   => $payments[$i]{'date'},
173             amount => $payments[$i]{'amount'},
174             branch => $branch
175         );
176         push( @loop, \%row );
177         $oldtype        = $payments[$i]{'accounttype'};
178         $oldtime        = $payments[$i]{'timestamp'};
179         $borrowernumber = $payments[$i]{'borrowernumber'};
180         $i++;
181     }
182 }
183
184 $template->param(
185     loop1  => \@loop,
186     totalw => $totalw,
187     total  => $total
188 );
189
190 output_html_with_http_headers $input, $cookie, $template->output;
191