Merge remote-tracking branch 'origin/new/bug_929'
[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
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 use strict;
25 #use warnings; FIXME - Bug 2505
26 use CGI;
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use Date::Manip;
31 use C4::Stats;
32 use C4::Debug;
33
34 use vars qw($debug);
35
36 my $input = new CGI;
37 my $time  = $input->param('time') || '';
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "circ/stats.tmpl",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
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 elsif ( $time eq 'today' ) {
57     $date  = ParseDate('today');
58     $date2 = ParseDate('tomorrow');
59 }
60 elsif ( $time eq 'daybefore' ) {
61     $date  = ParseDate('2 days ago');
62     $date2 = ParseDate('yesterday');
63 }
64 elsif ( $time eq 'month' ) {
65     $date  = ParseDate('1 month ago');
66     $date2 = ParseDate('today');
67 }
68 elsif ( $time =~ /\// ) {
69     $date  = ParseDate($time);
70     $date2 = ParseDateDelta('+ 1 day');
71     $date2 = DateCalc( $date, $date2 );
72 } else {
73     $template->param(notime => '1');    # TODO: add error feedback if time sent, but unrecognized
74     output_html_with_http_headers $input, $cookie, $template->output;
75     exit;
76 }
77
78 $debug and warn "d : $date // d2 : $date2";
79 $date  = UnixDate( $date,  '%Y-%m-%d' );
80 $date2 = UnixDate( $date2, '%Y-%m-%d' );
81 $debug and warn "d : $date // d2 : $date2";
82 my @payments = TotalPaid( $date, $date2 );
83 my $count    = @payments;
84 my $total    = 0;
85 my $totalw   = 0;
86 my $oldtime;
87 my @loop;
88 my %row;
89 my $i = 0;
90
91 while ( $i < $count ) {
92     $debug and warn " pay : " . $payments[$i]{'timestamp'};
93     my $time     = $payments[$i]{'datetime'};
94     my $payments = $payments[$i]{'value'};
95     my $charge   = 0;
96     my @temp     = split(/ /, $payments[$i]{'datetime'});
97     my $date     = $temp[0];
98     my @charges  =
99       getcharges( $payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'} );
100     my $count        = @charges;
101     my $temptotalf   = 0;
102     my $temptotalr   = 0;
103     my $temptotalres = 0;
104     my $temptotalren = 0;
105     my $temptotalw   = 0;
106
107     # FIXME: way too much logic to live only here in a report script
108     for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
109         $charge += $charges[$i2]->{'amount'};
110         %row = (
111             name   => $charges[$i2]->{'description'},
112             type   => $charges[$i2]->{'accounttype'},
113             time   => $charges[$i2]->{'timestamp'},
114             amount => $charges[$i2]->{'amount'},
115             branch => $charges[$i2]->{'amountoutstanding'}
116         );
117         push( @loop, \%row );
118         if ( $payments[$i]{'accountytpe'} ne 'W' ) {
119             if ( $charges[$i2]->{'accounttype'} eq 'Rent' ) {
120                 $temptotalr +=
121                   $charges[$i2]->{'amount'} -
122                   $charges[$i2]->{'amountoutstanding'};
123             }
124             if (   $charges[$i2]->{'accounttype'} eq 'F'
125                 || $charges[$i2]->{'accounttype'} eq 'FU'
126                 || $charges[$i2]->{'accounttype'} eq 'FN' )
127             {
128                 $temptotalf +=
129                   $charges[$i2]->{'amount'} -
130                   $charges[$i2]->{'amountoutstanding'};
131             }
132             if ( $charges[$i2]->{'accounttype'} eq 'Res' ) {
133                 $temptotalres +=
134                   $charges[$i2]->{'amount'} -
135                   $charges[$i2]->{'amountoutstanding'};
136             }
137             if ( $charges[$i2]->{'accounttype'} eq 'R' ) {
138                 $temptotalren +=
139                   $charges[$i2]->{'amount'} -
140                   $charges[$i2]->{'amountoutstanding'};
141             }
142         }
143     }
144     my $time2 = $payments[$i]{'date'};
145     my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
146     my $borrowernumber = $payments[$i]{'borrowernumber'};
147     my $oldtime        = $payments[$i]{'timestamp'};
148     my $oldtype        = $payments[$i]{'accounttype'};
149
150     while ($borrowernumber eq $payments[$i]{'borrowernumber'}
151         && $oldtype == $payments[$i]{'accounttype'}
152         && $oldtime eq $payments[$i]{'timestamp'} )
153     {
154         my $xtime2 = $payments[$i]{'date'};
155         my $branch = Getpaidbranch( $xtime2, $payments[$i]{'borrowernumber'} );
156         if ( $payments[$i]{'accounttype'} eq 'W' ) {
157             $totalw += $payments[$i]{'amount'};
158         }
159         else {
160             $payments[$i]{'amount'} = $payments[$i]{'amount'} * -1;
161             $total += $payments[$i]{'amount'};
162         }
163
164         #FIXME: display layer HTML
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