circ subdir - Dates.pm integration and warnings fixes.
[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 $totalw   = 0;
82 my $oldtime;
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 $time2 = $payments[$i]{'date'};
140     my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
141     my $borrowernumber = $payments[$i]{'borrowernumber'};
142     my $oldtime        = $payments[$i]{'timestamp'};
143     my $oldtype        = $payments[$i]{'accounttype'};
144
145     while ($borrowernumber eq $payments[$i]{'borrowernumber'}
146         && $oldtype == $payments[$i]{'accounttype'}
147         && $oldtime eq $payments[$i]{'timestamp'} )
148     {
149         my $xtime2 = $payments[$i]{'date'};
150         my $branch = Getpaidbranch( $xtime2, $payments[$i]{'borrowernumber'} );
151         if ( $payments[$i]{'accounttype'} eq 'W' ) {
152             $totalw += $payments[$i]{'amount'};
153         }
154         else {
155             $payments[$i]{'amount'} = $payments[$i]{'amount'} * -1;
156             $total += $payments[$i]{'amount'};
157         }
158
159         %row = (
160             name => "<b>"
161               . $payments[$i]{'firstname'}
162               . $payments[$i]{'surname'} . "</b>",
163             type   => $payments[$i]{'accounttype'},
164             time   => $payments[$i]{'date'},
165             amount => $payments[$i]{'amount'},
166             branch => $branch
167         );
168         push( @loop, \%row );
169         $oldtype        = $payments[$i]{'accounttype'};
170         $oldtime        = $payments[$i]{'timestamp'};
171         $borrowernumber = $payments[$i]{'borrowernumber'};
172         $i++;
173     }
174 }
175
176 $template->param(
177     loop1  => \@loop,
178     totalw => $totalw,
179     total  => $total
180 );
181
182 output_html_with_http_headers $input, $cookie, $template->output;
183