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