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