circ/stats.pl - debugify warnings
[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 use vars qw($debug);
33
34 BEGIN {
35         $debug = $ENV{DEBUG} || 0;
36 }
37
38 my $input = new CGI;
39 my $time  = $input->param('time');
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "circ/stats.tmpl",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { reports => 1 },
48         debug           => 1,
49     }
50 );
51
52 my $date;
53 my $date2;
54 if ( $time eq '' ) {
55     $template->param(notime => '1');
56     output_html_with_http_headers $input, $cookie, $template->output;
57     exit;
58 }
59 if ( $time eq 'yesterday' ) {
60     $date  = ParseDate('yesterday');
61     $date2 = ParseDate('today');
62 }
63 if ( $time eq 'today' ) {
64     $date  = ParseDate('today');
65     $date2 = ParseDate('tomorrow');
66 }
67 if ( $time eq 'daybefore' ) {
68     $date  = ParseDate('2 days ago');
69     $date2 = ParseDate('yesterday');
70 }
71 if ( $time eq 'month' ) {
72     $date  = ParseDate('1 month ago');
73     $date2 = ParseDate('today');
74     $debug and warn "d : $date // d2 : $date2";
75 }
76 if ( $time =~ /\// ) {
77     $date  = ParseDate($time);
78     $date2 = ParseDateDelta('+ 1 day');
79     $date2 = DateCalc( $date, $date2 );
80 }
81 $date  = UnixDate( $date,  '%Y-%m-%d' );
82 $date2 = UnixDate( $date2, '%Y-%m-%d' );
83 $debug and warn "d : $date // d2 : $date2";
84 my @payments = TotalPaid( $date, $date2 );
85 my $count    = @payments;
86 my $total    = 0;
87 my $totalw   = 0;
88 my $oldtime;
89 my @loop;
90 my %row;
91 my $i = 0;
92
93 while ( $i < $count ) {
94     $debug and warn " pay : " . $payments[$i]{'timestamp'};
95     my $time     = $payments[$i]{'datetime'};
96     my $payments = $payments[$i]{'value'};
97     my $charge   = 0;
98     my @temp     = split( / /, $payments[$i]{'datetime'} );
99     my $date     = $temp[0];
100     my @charges  =
101       getcharges( $payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'} );
102     my $count        = @charges;
103     my $temptotalf   = 0;
104     my $temptotalr   = 0;
105     my $temptotalres = 0;
106     my $temptotalren = 0;
107     my $temptotalw   = 0;
108
109     for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
110         $charge += $charges[$i2]->{'amount'};
111         %row = (
112             name   => $charges[$i2]->{'description'},
113             type   => $charges[$i2]->{'accounttype'},
114             time   => $charges[$i2]->{'timestamp'},
115             amount => $charges[$i2]->{'amount'},
116             branch => $charges[$i2]->{'amountoutstanding'}
117         );
118         push( @loop, \%row );
119         if ( $payments[$i]{'accountytpe'} ne 'W' ) {
120             if ( $charges[$i2]->{'accounttype'} eq 'Rent' ) {
121                 $temptotalr +=
122                   $charges[$i2]->{'amount'} -
123                   $charges[$i2]->{'amountoutstanding'};
124             }
125             if (   $charges[$i2]->{'accounttype'} eq 'F'
126                 || $charges[$i2]->{'accounttype'} eq 'FU'
127                 || $charges[$i2]->{'accounttype'} eq 'FN' )
128             {
129                 $temptotalf +=
130                   $charges[$i2]->{'amount'} -
131                   $charges[$i2]->{'amountoutstanding'};
132             }
133             if ( $charges[$i2]->{'accounttype'} eq 'Res' ) {
134                 $temptotalres +=
135                   $charges[$i2]->{'amount'} -
136                   $charges[$i2]->{'amountoutstanding'};
137             }
138             if ( $charges[$i2]->{'accounttype'} eq 'R' ) {
139                 $temptotalren +=
140                   $charges[$i2]->{'amount'} -
141                   $charges[$i2]->{'amountoutstanding'};
142             }
143         }
144     }
145     my $time2 = $payments[$i]{'date'};
146     my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
147     my $borrowernumber = $payments[$i]{'borrowernumber'};
148     my $oldtime        = $payments[$i]{'timestamp'};
149     my $oldtype        = $payments[$i]{'accounttype'};
150
151     while ($borrowernumber eq $payments[$i]{'borrowernumber'}
152         && $oldtype == $payments[$i]{'accounttype'}
153         && $oldtime eq $payments[$i]{'timestamp'} )
154     {
155         my $xtime2 = $payments[$i]{'date'};
156         my $branch = Getpaidbranch( $xtime2, $payments[$i]{'borrowernumber'} );
157         if ( $payments[$i]{'accounttype'} eq 'W' ) {
158             $totalw += $payments[$i]{'amount'};
159         }
160         else {
161             $payments[$i]{'amount'} = $payments[$i]{'amount'} * -1;
162             $total += $payments[$i]{'amount'};
163         }
164
165         %row = (
166             name => "<b>"
167               . $payments[$i]{'firstname'}
168               . $payments[$i]{'surname'} . "</b>",
169             type   => $payments[$i]{'accounttype'},
170             time   => $payments[$i]{'date'},
171             amount => $payments[$i]{'amount'},
172             branch => $branch
173         );
174         push( @loop, \%row );
175         $oldtype        = $payments[$i]{'accounttype'};
176         $oldtime        = $payments[$i]{'timestamp'};
177         $borrowernumber = $payments[$i]{'borrowernumber'};
178         $i++;
179     }
180 }
181
182 $template->param(
183     loop1  => \@loop,
184     totalw => $totalw,
185     total  => $total
186 );
187
188 output_html_with_http_headers $input, $cookie, $template->output;
189